21 lines
708 B
TypeScript
21 lines
708 B
TypeScript
export default defineNuxtPlugin({
|
|
name: 'open-fetch-policy-auth',
|
|
setup(nuxtApp) {
|
|
const { policyApiToken } = useRuntimeConfig().public
|
|
const token = typeof policyApiToken === 'string' ? policyApiToken : ''
|
|
if (!token) return
|
|
|
|
nuxtApp.hook('openFetch:onRequest:policy', (ctx) => {
|
|
const headers = ctx.options.headers
|
|
if (headers instanceof Headers) {
|
|
if (!headers.has('Authorization')) headers.set('Authorization', `Bearer ${token}`)
|
|
} else {
|
|
const h = (headers ?? {}) as Record<string, string>
|
|
if (!h.Authorization && !h.authorization) {
|
|
ctx.options.headers = { ...h, Authorization: `Bearer ${token}` }
|
|
}
|
|
}
|
|
})
|
|
}
|
|
})
|