Skip to content

Commit

Permalink
Merge pull request #7 from limbo-works/add-fetch-options
Browse files Browse the repository at this point in the history
feat: add fetchoptions
  • Loading branch information
tfjordside authored Nov 19, 2024
2 parents 99531c0 + 8193ff3 commit d13d567
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 17 deletions.
1 change: 1 addition & 0 deletions .playground/app.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<template>
<div>
Nuxt module playground!
{{ data }}
</div>
</template>

Expand Down
9 changes: 7 additions & 2 deletions .playground/nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ export default defineNuxtConfig({
MyModule
],

myModule: {
addPlugin: true
nuxtUmbraco: {
addApiProxy: true,
addPlugin: true,
fetchOptions: {
timeout: 5000,
retry: 2
},
},

runtimeConfig: {
Expand Down
18 changes: 12 additions & 6 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,38 @@ import {
export interface ModuleOptions {
addPlugin: boolean;
addApiProxy: boolean;
fetchOptions: Object;
};

export default defineNuxtModule<ModuleOptions>({
meta: {
name: 'nuxt-umbraco',
configKey: 'NuxtUmbraco',
configKey: 'nuxtUmbraco',
compatibility: {
nuxt: '^3.0.0'
}
},

defaults: {
addPlugin: true,
addApiProxy: true,
fetchOptions: null,
},

setup (options, nuxt) {
const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url));
const { resolve } = createResolver(import.meta.url);
console.log(resolve(runtimeDir, 'server/api/data'));

if (options.fetchOptions) {
nuxt.options.runtimeConfig.nuxtUmbraco = {
fetchOptions: options.fetchOptions,
}
};


if (options.addPlugin) {
nuxt.options.build.transpile.push(runtimeDir);
addPlugin(resolve(runtimeDir, 'plugin'));
}

if (options.addApiProxy) {
addServerHandler({
route: '/api/data',
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ export default defineNuxtPlugin((nuxtApp) => {
const headers = useRequestHeaders();
let appHost = '';

if (process.server) {
if (import.meta.server) {
appHost = headers.host;
}

if (process.client) {
if (import.meta.client) {
const { hostname } = new URL(window.location.href);
appHost = hostname;
}
Expand All @@ -35,7 +35,7 @@ export default defineNuxtPlugin((nuxtApp) => {

const urlSearchParams = new URLSearchParams({
appHost,
navContext: process.server,
navContext: import.meta.server,
navLevels: 2,
url: decodeURI(config.route),
...config.params
Expand Down
45 changes: 39 additions & 6 deletions src/runtime/server/api/data.js
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,
});
}

});

0 comments on commit d13d567

Please sign in to comment.