-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(roadiz): add useRoadizWebResponse()
- Loading branch information
1 parent
c47f0e1
commit 029b080
Showing
4 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { RoadizWebResponse } from '@roadiz/types' | ||
|
||
export async function useRoadizWebResponse<T>(path: string) { | ||
const fetch = useRoadizFetchFactory() | ||
const { data } = await useAsyncData(async () => { | ||
try { | ||
const response = await fetch.raw<RoadizWebResponse>('/web_response_by_path', { | ||
method: 'GET', | ||
query: { | ||
path, | ||
}, | ||
}) | ||
const headersLink = response.headers.get('link') | ||
const alternateLinks = headersLink ? getResponseAlternateLinks(headersLink) : [] | ||
|
||
return { webResponse: response._data, alternateLinks } | ||
} catch (error) { | ||
console.error('Error=', error) | ||
|
||
return { error } | ||
} | ||
}) | ||
const webResponse = data.value?.webResponse | ||
|
||
return { | ||
alternateLinks: data.value?.alternateLinks || [], | ||
webResponse, | ||
item: webResponse?.item as T | undefined, | ||
error: data.value?.error, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Enable auto import for all files nested into folders | ||
// @see https://nuxt.com/docs/guide/directory-structure/composables#how-files-are-scanned | ||
// string | ||
export { b64DecodeUnicode } from './string/b64-decode-unicode' | ||
// Roadiz | ||
export { getResponseAlternateLinks } from './roadiz/get-response-alternate-links' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import type { RoadizAlternateLink } from '@roadiz/types' | ||
|
||
export function getResponseAlternateLinks(links: string): Array<RoadizAlternateLink> { | ||
return links | ||
.split(',') | ||
.filter((link: string) => { | ||
return link | ||
.split(';') | ||
.map((attribute) => attribute.trim()) | ||
.includes('type="text/html"') | ||
}) | ||
.map((link: string) => { | ||
const attributes = link.split(';') | ||
const title = attributes[3]?.split('title="').join('').split('"').join('').trim() || undefined | ||
|
||
return { | ||
url: attributes[0].split('<').join('').split('>').join('').trim(), | ||
locale: attributes[2].split('hreflang="').join('').split('"').join('').trim(), | ||
// Must decode translation name from base64 because headers are ASCII only | ||
title: title ? b64DecodeUnicode(title) : undefined, | ||
} as RoadizAlternateLink | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Decode base64 string to UTF-8 string on client-side and Node.js | ||
*/ | ||
export function b64DecodeUnicode(str: string) { | ||
if (!process.server) { | ||
return decodeURIComponent( | ||
Array.prototype.map | ||
.call(window.atob(str), function (c: string) { | ||
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2) | ||
}) | ||
.join(''), | ||
) | ||
} | ||
|
||
return decodeURIComponent( | ||
Array.prototype.map | ||
.call(Buffer.from(str, 'base64').toString('binary'), function (c: string) { | ||
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2) | ||
}) | ||
.join(''), | ||
) | ||
} |