From 522a00d0fe5906dfe7135d5247e149cb9e8bdc87 Mon Sep 17 00:00:00 2001 From: s3xysteak <86149525+s3xysteak@users.noreply.github.com> Date: Sun, 28 Jul 2024 17:36:19 +0800 Subject: [PATCH] feat!: hooks do not override hooks created by useFetch.create --- src/core/ctx.ts | 18 ++++++++++++++---- src/core/useFetch.ts | 26 +++++++++++++++++++------- src/core/usePagination.ts | 26 ++++++++++++++++++++------ test/general.test.ts | 21 +++++++++++++++++++++ test/pagination.test.ts | 21 +++++++++++++++++++++ 5 files changed, 95 insertions(+), 17 deletions(-) diff --git a/src/core/ctx.ts b/src/core/ctx.ts index 00e1041..c176c6d 100644 --- a/src/core/ctx.ts +++ b/src/core/ctx.ts @@ -1,15 +1,21 @@ import { $fetch } from 'ofetch' import type { ResponseType, UseFetchOptions } from './types' -export function createContext(userOptions: UseFetchOptions) { - const options = resolveOptions(userOptions) +export function createContext( + userOptions: UseFetchOptions, + defaultOptions: UseFetchOptions, +) { + const options = resolveOptions(userOptions, defaultOptions) return { ...options, } } -function resolveOptions(options: UseFetchOptions) { +function resolveOptions( + options: UseFetchOptions, + defaultOptions: UseFetchOptions, +) { const { immediate = true, watch = [], @@ -43,7 +49,10 @@ function resolveOptions(options: UseFetchOptions) { window, ...optionsWatch - } = options + } = { + ...defaultOptions, + ...options, + } return { optionsComposable: { @@ -80,5 +89,6 @@ function resolveOptions(options: UseFetchOptions) { timeout, window, }, + optionsDefault: defaultOptions, } } diff --git a/src/core/useFetch.ts b/src/core/useFetch.ts index 8ee1ea1..608b8a2 100644 --- a/src/core/useFetch.ts +++ b/src/core/useFetch.ts @@ -21,7 +21,7 @@ export function createUseFetch(defaultOptions: UseFetchOptions = {}) { _req: UseFetchParams, options: UseFetchOptions = {}, ): UseFetchReturns { - const ctx = createContext(Object.assign(options, defaultOptions)) + const ctx = createContext(options, defaultOptions) const status = ref('idle') const pending = ref(false) @@ -41,14 +41,26 @@ export function createUseFetch(defaultOptions: UseFetchOptions = {}) { ...Object.fromEntries( Object.entries(toValue(watchOptions)).map(([k, v]) => [k, toValue(v)]), ), - onResponse($fetchCtx) { + onRequest(context) { + ctx.optionsDefault?.onRequest?.(context) + ctx.options$fetch?.onRequest?.(context) + }, + onRequestError(context) { + status.value = 'error' + error.value = context.error + ctx.optionsDefault?.onRequestError?.(context as any) + ctx.options$fetch?.onRequestError?.(context as any) + }, + onResponse(context) { status.value = 'success' - ctx.options$fetch?.onResponse?.($fetchCtx) + ctx.optionsDefault?.onResponse?.(context) + ctx.options$fetch?.onResponse?.(context) }, - onRequestError($fetchCtx) { + onResponseError(context) { status.value = 'error' - error.value = $fetchCtx.error - ctx.options$fetch?.onResponseError?.($fetchCtx as any) + error.value = context.error ?? null + ctx.optionsDefault?.onResponseError?.(context) + ctx.options$fetch?.onResponseError?.(context as any) }, }) @@ -89,7 +101,7 @@ export function createUseFetch(defaultOptions: UseFetchOptions = {}) { } useFetch.create = newDefaultOptions => - createUseFetch(Object.assign(defaultOptions, newDefaultOptions)) + createUseFetch({ ...defaultOptions, ...newDefaultOptions }) return useFetch } diff --git a/src/core/usePagination.ts b/src/core/usePagination.ts index e46ebfb..5bf3ee1 100644 --- a/src/core/usePagination.ts +++ b/src/core/usePagination.ts @@ -25,7 +25,7 @@ export function createUsePagination(defaultOptions: UsePaginationOptions = useFetch = defaultUseFetch, ...useFetchOptions - } = Object.assign(defaultOptions, options) + } = { ...defaultOptions, ...options } const pageSize = ref(defaultPageSize) const pageCurrent = ref(1) @@ -33,16 +33,30 @@ export function createUsePagination(defaultOptions: UsePaginationOptions = const val = useFetch(_req, { ...useFetchOptions, watch: useFetchOptions.watch !== false && [pageSize, pageCurrent, ...toArray(useFetchOptions.watch || [])], - onRequest(ctx) { + onRequest(context) { + defaultOptions.onRequest?.(context) + const assignPaginationKey = (p: Record = {}) => Object.assign(p, { [pageCurrentKey]: p[pageCurrentKey] || toValue(pageCurrent), [pageSizeKey]: p[pageSizeKey] || toValue(pageSize), }) - ctx.options.query = assignPaginationKey(ctx.options.query) - ctx.options.params = assignPaginationKey(ctx.options.params) - useFetchOptions?.onRequest?.(ctx) + context.options.query = assignPaginationKey(context.options.query) + context.options.params = assignPaginationKey(context.options.params) + useFetchOptions?.onRequest?.(context) + }, + onRequestError(context) { + defaultOptions?.onResponseError?.(context as any) + useFetchOptions?.onResponseError?.(context as any) + }, + onResponse(context) { + defaultOptions?.onResponse?.(context) + useFetchOptions?.onResponse?.(context) + }, + onResponseError(context) { + defaultOptions?.onResponseError?.(context) + useFetchOptions?.onResponseError?.(context as any) }, }) @@ -60,7 +74,7 @@ export function createUsePagination(defaultOptions: UsePaginationOptions = } } - usePagination.create = newDefaultOptions => createUsePagination(Object.assign(defaultOptions, newDefaultOptions)) + usePagination.create = newDefaultOptions => createUsePagination({ ...defaultOptions, ...newDefaultOptions }) return usePagination } diff --git a/test/general.test.ts b/test/general.test.ts index 6594c46..ec08334 100644 --- a/test/general.test.ts +++ b/test/general.test.ts @@ -105,4 +105,25 @@ createTest(3001, (listener, getURL) => { await e2() expect(d2.value).toBe('ok') }) + + it('useFetch.create - hook', async () => { + let target + const u = useFetch.create({ + immediate: false, + onRequest({ options }) { + if (!(options.headers instanceof Headers)) + options.headers = new Headers(options.headers) + + options.headers.append('foo', 'bar') + }, + }) + const { execute } = u(getURL('ok'), { + onRequest({ options }) { + // @ts-expect-error - test + target = options.headers.get('foo') + }, + }) + await execute() + expect(target).toBe('bar') + }) }) diff --git a/test/pagination.test.ts b/test/pagination.test.ts index 4208e3a..82fa300 100644 --- a/test/pagination.test.ts +++ b/test/pagination.test.ts @@ -83,4 +83,25 @@ createTest(3002, (listener, getURL) => { await next(data) expect(key!).toBe('2') }) + + it('usePagination.create - hook', async () => { + let target + const u = usePagination.create({ + immediate: false, + onRequest({ options }) { + if (!(options.headers instanceof Headers)) + options.headers = new Headers(options.headers) + + options.headers.append('foo', 'bar') + }, + }) + const { execute } = u(getURL('ok'), { + onRequest({ options }) { + // @ts-expect-error - test + target = options.headers.get('foo') + }, + }) + await execute() + expect(target).toBe('bar') + }) })