Skip to content

Commit

Permalink
feat(roadiz): add useRoadizWebResponse()
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelodelain committed Mar 15, 2024
1 parent c47f0e1 commit 029b080
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
31 changes: 31 additions & 0 deletions composables/use-roadiz-web-response.ts
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,
}
}
6 changes: 6 additions & 0 deletions utils/index.ts
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'
23 changes: 23 additions & 0 deletions utils/roadiz/get-response-alternate-links.ts
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
})
}
22 changes: 22 additions & 0 deletions utils/string/b64-decode-unicode.ts
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(''),
)
}

0 comments on commit 029b080

Please sign in to comment.