-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from limbo-works/add-fetch-options
feat: add fetchoptions
- Loading branch information
Showing
5 changed files
with
62 additions
and
17 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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
<template> | ||
<div> | ||
Nuxt module playground! | ||
{{ data }} | ||
</div> | ||
</template> | ||
|
||
|
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
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
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
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 |
---|---|---|
@@ -1,15 +1,48 @@ | ||
import { sendProxy, defineEventHandler } from 'h3'; | ||
import { defineEventHandler, readBody, appendHeader, createError } from 'h3'; | ||
import { useRuntimeConfig } from '#nitro'; | ||
const config = useRuntimeConfig(); | ||
|
||
export default defineEventHandler(async (event) => { | ||
const { headers: reqHeaders = {} } = event.node.req || {}; | ||
const { headers: reqHeaders = {}, method, url } = event.node.req || {}; | ||
const target = new URL( | ||
event.node.req.url.replace(/^\/api\/data/, config.getdataEndpointUrl || '/umbraco/api/spa/getdata/'), | ||
url.replace(/^\/api\/data/, config.getdataEndpointUrl || '/umbraco/api/spa/getdata/'), | ||
config.public.apiDomain | ||
); | ||
|
||
const headers = { 'X-Api-Key': config.apiKey, cookie: reqHeaders.cookie }; | ||
const data = await sendProxy(event, target.toString(), { headers, sendStream: false }); | ||
return data; | ||
|
||
const body = | ||
method !== 'GET' && method !== 'HEAD' | ||
? await readBody(event) | ||
: undefined; | ||
|
||
const fetchOptions = config.nuxtUmbraco?.fetchOptions || {}; | ||
try { | ||
const response = await $fetch.raw(target.toString(), { | ||
method, | ||
body, | ||
...fetchOptions, | ||
|
||
headers: { | ||
'content-type': reqHeaders['content-type'] || 'application/json', | ||
cookie: reqHeaders.cookie, | ||
'X-Api-Key': config.apiKey, | ||
...(fetchOptions?.headers || {}), | ||
}, | ||
}); | ||
|
||
for (const header of ['set-cookie', 'cache-control']) { | ||
if (response.headers.has(header)) { | ||
appendHeader(event, header, response.headers.get(header)); | ||
} | ||
} | ||
|
||
return response._data; | ||
} catch (error) { | ||
return createError({ | ||
statusCode: error.response.status, | ||
statusMessage: error.message, | ||
data: error.data, | ||
}); | ||
} | ||
|
||
}); |