Skip to content

Commit

Permalink
refactor(roadiz): rename useRoadizAsyncData() to useRoadizFetch / ren…
Browse files Browse the repository at this point in the history
…ame useRoadizFetch() to useRoadizFetchFactory()
  • Loading branch information
manuelodelain committed Mar 15, 2024
1 parent b32b056 commit d3b3b9e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 47 deletions.
49 changes: 49 additions & 0 deletions composables/use-roadiz-fetch-factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import type { NitroFetchOptions, NitroFetchRequest } from 'nitropack'

export function useRoadizFetchFactory<DefaultR extends NitroFetchRequest = NitroFetchRequest>(
options?: NitroFetchOptions<DefaultR>,
) {
return $fetch.create({
onRequest(context) {
/*
* Add preview token to every request if preview mode is enabled.
*/
const { token, isActive } = useRoadizPreview()

if (isActive.value && token.value) {
context.options.query = {
...context.options.query,
_preview: '1',
}

context.options.headers = {
...context.options.headers,
Authorization: `Bearer ${token.value}`,
}
}
/*
* Add locale to every request if it is not a web response request.
*/
if (context.request.toString() !== '/web_response_by_path') {
const { $i18n } = useNuxtApp()

context.options.query = {
...context.options.query,
_locale: $i18n?.locale.value || $i18n?.defaultLocale?.toString(),
}
}
},
onResponseError(context) {
throw createError({
statusCode: context.response.status,
message: context.response.statusText,
})
},
headers: {
'accept-encoding': 'gzip, deflate',
accept: 'application/ld+json',
},
baseURL: useApiUrl(),
...options,
})
}
50 changes: 4 additions & 46 deletions composables/use-roadiz-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,7 @@
import type { NitroFetchOptions, NitroFetchRequest } from 'nitropack'
import type { AsyncDataOptions } from '#app'

export function useRoadizFetch<DefaultR extends NitroFetchRequest = NitroFetchRequest>(
options?: NitroFetchOptions<DefaultR>,
) {
return $fetch.create({
onRequest(context) {
/*
* Add preview token to every request if preview mode is enabled.
*/
const { token, isActive } = useRoadizPreview()
export function useRoadizFetch<T>(url?: string | AsyncDataOptions<T>, options?: AsyncDataOptions<T>) {
const fetch = useRoadizFetchFactory()

if (isActive.value && token.value) {
context.options.query = {
...context.options.query,
_preview: '1',
}

context.options.headers = {
...context.options.headers,
Authorization: `Bearer ${token.value}`,
}
}
/*
* Add locale to every request if it is not a web response request.
*/
if (context.request.toString() !== '/web_response_by_path') {
const { $i18n } = useNuxtApp()

context.options.query = {
...context.options.query,
_locale: $i18n?.locale.value || $i18n?.defaultLocale?.toString(),
}
}
},
onResponseError(context) {
throw createError({
statusCode: context.response.status,
message: context.response.statusText,
})
},
headers: {
'accept-encoding': 'gzip, deflate',
accept: 'application/ld+json',
},
baseURL: useApiUrl(),
...options,
})
return useAsyncData<T>(() => fetch(url), options)
}
12 changes: 11 additions & 1 deletion playground/pages/index.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
<script setup lang="ts"></script>
<script setup lang="ts">
const response = await useRoadizFetch('/web_response_by_path', {
query: {
path: '/',
},
}).catch((error) => {
console.error('error', error)
})
console.log('response', response)
</script>

<template>
<div>
Expand Down

0 comments on commit d3b3b9e

Please sign in to comment.