From 6edf22d5e1077f46ea2a77f5fe76d3d827a8fa32 Mon Sep 17 00:00:00 2001 From: manchenkoff Date: Wed, 2 Oct 2024 14:08:45 +0200 Subject: [PATCH 1/3] chore: use specific ofetch version for development --- package.json | 1 + pnpm-lock.yaml | 21 ++++++++++++--------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 745c66c..3e0f128 100644 --- a/package.json +++ b/package.json @@ -49,6 +49,7 @@ "changelogen": "^0.5.5", "eslint": "^9.9.1", "nuxt": "^3.13.2", + "ofetch": "^1.4.0", "typescript": "^5.5.4", "vitest": "^2.0.5", "vue-tsc": "^2.1.2" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6ab2f9d..9d9cdf7 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -42,6 +42,9 @@ importers: nuxt: specifier: ^3.13.2 version: 3.13.2(@parcel/watcher@2.4.1)(@types/node@22.7.4)(eslint@9.11.1(jiti@1.21.6))(ioredis@5.4.1)(magicast@0.3.5)(optionator@0.9.4)(rollup@4.22.2)(terser@5.33.0)(typescript@5.5.4)(vite@5.4.7(@types/node@22.7.4)(terser@5.33.0))(vue-tsc@2.1.6(typescript@5.5.4)) + ofetch: + specifier: ^1.4.0 + version: 1.4.0 typescript: specifier: ^5.5.4 version: 5.5.4 @@ -3001,8 +3004,8 @@ packages: resolution: {integrity: sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==} engines: {node: '>=0.10.0'} - ofetch@1.3.4: - resolution: {integrity: sha512-KLIET85ik3vhEfS+3fDlc/BAZiAp+43QEC/yCo5zkNoY2YaKvNkOaFr/6wCFgFH1kuYQM5pMNi0Tg8koiIemtw==} + ofetch@1.4.0: + resolution: {integrity: sha512-MuHgsEhU6zGeX+EMh+8mSMrYTnsqJQQrpM00Q6QHMKNqQ0bKy0B43tk8tL1wg+CnsSTy1kg4Ir2T5Ig6rD+dfQ==} ohash@1.1.4: resolution: {integrity: sha512-FlDryZAahJmEF3VR3w1KogSEdWX3WhA5GPakFx4J81kEAiHyLMpdLLElS8n8dfNadMgAne/MywcvmogzscVt4g==} @@ -5049,7 +5052,7 @@ snapshots: jiti: 1.21.6 mri: 1.2.0 nanoid: 5.0.7 - ofetch: 1.3.4 + ofetch: 1.4.0 package-manager-detector: 0.2.0 parse-git-config: 3.0.0 pathe: 1.1.2 @@ -5077,7 +5080,7 @@ snapshots: magic-string: 0.30.11 nitropack: 2.9.7(magicast@0.3.5) node-fetch-native: 1.6.4 - ofetch: 1.3.4 + ofetch: 1.4.0 pathe: 1.1.2 perfect-debounce: 1.0.0 radix3: 1.1.2 @@ -5989,7 +5992,7 @@ snapshots: convert-gitmoji: 0.1.5 mri: 1.2.0 node-fetch-native: 1.6.4 - ofetch: 1.3.4 + ofetch: 1.4.0 open: 10.1.0 pathe: 1.1.2 pkg-types: 1.2.0 @@ -7348,7 +7351,7 @@ snapshots: mlly: 1.7.1 mri: 1.2.0 node-fetch-native: 1.6.4 - ofetch: 1.3.4 + ofetch: 1.4.0 ohash: 1.1.4 openapi-typescript: 6.7.6 pathe: 1.1.2 @@ -7484,7 +7487,7 @@ snapshots: nitropack: 2.9.7(magicast@0.3.5) nuxi: 3.13.2 nypm: 0.3.11 - ofetch: 1.3.4 + ofetch: 1.4.0 ohash: 1.1.4 pathe: 1.1.2 perfect-debounce: 1.0.0 @@ -7568,7 +7571,7 @@ snapshots: object-assign@4.1.1: {} - ofetch@1.3.4: + ofetch@1.4.0: dependencies: destr: 2.0.3 node-fetch-native: 1.6.4 @@ -8494,7 +8497,7 @@ snapshots: lru-cache: 10.4.3 mri: 1.2.0 node-fetch-native: 1.6.4 - ofetch: 1.3.4 + ofetch: 1.4.0 ufo: 1.5.4 optionalDependencies: ioredis: 5.4.1 From 8160e8ca55d4917ce32d77feabba935c9b721d08 Mon Sep 17 00:00:00 2001 From: manchenkoff Date: Wed, 2 Oct 2024 14:09:18 +0200 Subject: [PATCH 2/3] feat: improve headers logging readability --- src/runtime/httpFactory.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/runtime/httpFactory.ts b/src/runtime/httpFactory.ts index a75750e..7cab1d1 100644 --- a/src/runtime/httpFactory.ts +++ b/src/runtime/httpFactory.ts @@ -81,7 +81,7 @@ export function createHttpClient(nuxtApp: NuxtApp, logger: ConsolaInstance): $Fe logger.trace( `Request headers for "${context.request.toString()}"`, - context.options.headers, + Object.fromEntries(context.options.headers.entries()), ) }, @@ -94,7 +94,7 @@ export function createHttpClient(nuxtApp: NuxtApp, logger: ConsolaInstance): $Fe logger.trace( `Response headers for "${context.request.toString()}"`, - context.response?.headers, + context.response ? Object.fromEntries(context.response.headers.entries()) : {}, ) }, From 22602b6bc9fb70f054558ce5b30ca097210a8b9e Mon Sep 17 00:00:00 2001 From: manchenkoff Date: Wed, 2 Oct 2024 14:09:58 +0200 Subject: [PATCH 3/3] fix: use normalized Headers object for interceptors --- src/runtime/interceptors/common/request.ts | 6 ++---- src/runtime/interceptors/cookie/request.ts | 15 ++++++++------- src/runtime/interceptors/token/request.ts | 3 ++- src/runtime/utils/headers.ts | 7 +++++++ 4 files changed, 19 insertions(+), 12 deletions(-) create mode 100644 src/runtime/utils/headers.ts diff --git a/src/runtime/interceptors/common/request.ts b/src/runtime/interceptors/common/request.ts index d401470..8486bdf 100644 --- a/src/runtime/interceptors/common/request.ts +++ b/src/runtime/interceptors/common/request.ts @@ -1,5 +1,6 @@ import type { FetchContext } from 'ofetch' import type { ConsolaInstance } from 'consola' +import { appendRequestHeaders } from '../../utils/headers' import type { NuxtApp } from '#app' /** @@ -16,10 +17,7 @@ export default async function handleRequestHeaders( const method = ctx.options.method?.toLowerCase() ?? 'get' const headersToAdd = { Accept: 'application/json' } - ctx.options.headers = Object.assign( - ctx.options.headers || {}, - headersToAdd, - ) + ctx.options.headers = appendRequestHeaders(ctx.options.headers, headersToAdd) // https://laravel.com/docs/10.x/routing#form-method-spoofing if (method === 'put' && ctx.options.body instanceof FormData) { diff --git a/src/runtime/interceptors/cookie/request.ts b/src/runtime/interceptors/cookie/request.ts index ff5c73c..41e72e8 100644 --- a/src/runtime/interceptors/cookie/request.ts +++ b/src/runtime/interceptors/cookie/request.ts @@ -2,6 +2,7 @@ import type { FetchContext } from 'ofetch' import type { ConsolaInstance } from 'consola' import { useSanctumConfig } from '../../composables/useSanctumConfig' import type { ModuleOptions } from '../../types/options' +import { appendRequestHeaders } from '../../utils/headers' import { type NuxtApp, useCookie, useRequestHeaders, useRequestURL } from '#app' const SECURE_METHODS = new Set(['post', 'delete', 'put', 'patch']) @@ -15,10 +16,10 @@ const COOKIE_OPTIONS: { readonly: true } = { readonly: true } * @returns Headers collection to pass to the API */ function useServerHeaders( - headers: HeadersInit | undefined, + headers: Headers, config: ModuleOptions, logger: ConsolaInstance, -): HeadersInit { +): Headers { const clientHeaders = useRequestHeaders(['cookie', 'user-agent']) const origin = config.origin ?? useRequestURL().origin @@ -34,7 +35,7 @@ function useServerHeaders( Object.keys(headersToAdd), ) - return Object.assign(headers || {}, headersToAdd) + return appendRequestHeaders(headers, headersToAdd) } /** @@ -67,10 +68,10 @@ async function initCsrfCookie( * @returns Headers collection to pass to the API */ async function useCsrfHeader( - headers: HeadersInit | undefined, + headers: Headers, config: ModuleOptions, logger: ConsolaInstance, -): Promise { +): Promise { if (config.csrf.cookie === undefined) { throw new Error('`sanctum.csrf.cookie` is not defined') } @@ -88,14 +89,14 @@ async function useCsrfHeader( if (!csrfToken.value) { logger.warn(`${config.csrf.cookie} cookie is missing, unable to set ${config.csrf.header} header`) - return headers || {} + return headers } const headersToAdd = { [config.csrf.header]: csrfToken.value } logger.debug(`[request] added csrf token header`, Object.keys(headersToAdd)) - return Object.assign(headers || {}, headersToAdd) + return appendRequestHeaders(headers, headersToAdd) } /** diff --git a/src/runtime/interceptors/token/request.ts b/src/runtime/interceptors/token/request.ts index 441a245..cd8907c 100644 --- a/src/runtime/interceptors/token/request.ts +++ b/src/runtime/interceptors/token/request.ts @@ -1,6 +1,7 @@ import type { FetchContext } from 'ofetch' import type { ConsolaInstance } from 'consola' import { useSanctumAppConfig } from '../../composables/useSanctumAppConfig' +import { appendRequestHeaders } from '../../utils/headers' import type { NuxtApp } from '#app' /** @@ -34,5 +35,5 @@ export default async function handleRequestTokenHeader( Object.keys(headersToAdd), ) - ctx.options.headers = Object.assign(ctx.options.headers || {}, headersToAdd) + ctx.options.headers = appendRequestHeaders(ctx.options.headers, headersToAdd) } diff --git a/src/runtime/utils/headers.ts b/src/runtime/utils/headers.ts new file mode 100644 index 0000000..5c75ae9 --- /dev/null +++ b/src/runtime/utils/headers.ts @@ -0,0 +1,7 @@ +export function appendRequestHeaders(headers: Headers, append: Record): Headers { + for (const [key, value] of Object.entries(append)) { + headers.set(key, value) + } + + return headers +}