From c5c427adab6a98dfb792c4d4fecb8d8e61aa8fe2 Mon Sep 17 00:00:00 2001 From: Luma Date: Fri, 11 Mar 2022 11:33:52 +0900 Subject: [PATCH] feat: function to get default key --- packages/aspida-swr/index.ts | 34 ++++++++++++++++++++++--------- packages/aspida-swrv/index.ts | 38 ++++++++++++++++++++++++++--------- 2 files changed, 54 insertions(+), 18 deletions(-) diff --git a/packages/aspida-swr/index.ts b/packages/aspida-swr/index.ts index 008ae76e..e8a123c2 100644 --- a/packages/aspida-swr/index.ts +++ b/packages/aspida-swr/index.ts @@ -7,12 +7,11 @@ type AspidaSWROption = T & { * @deprecated * Will be dropped next major release. * Use key and fetcher options instead. - * key: (_opt, getOriginalKey) => - * enabled ? getOriginalKey() : null, + * key: enabled ? undefined : null, * fetcher: enabled ? undefined : null */ enabled?: boolean - key?: ValueKey | ((opt: AspidaSWROption, getOriginalKey: () => string[]) => ValueKey) + key?: ValueKey | (() => ValueKey) fetcher?: | ((f: (opt: AspidaSWROption) => any) => (opt: AspidaSWROption) => any) | null @@ -29,6 +28,27 @@ type ResponseData Promise> = ReturnType exten ? S : never +function getAspidaSWRDefaultKey< + T extends Record & { + $get: (option: any) => Promise + $path: (option?: any) => string + } +>(api: T, ...option: Options): string[] +function getAspidaSWRDefaultKey< + T extends Record & { $path: (option?: any) => string }, + U extends { [K in keyof T]: T[K] extends (option: any) => Promise ? K : never }[keyof T] +>(api: T, method: U, ...option: Options): string[] +function getAspidaSWRDefaultKey< + T extends Record & { $path: (option?: any) => string }, + U extends { [K in keyof T]: T[K] extends (option: any) => Promise ? K : never }[keyof T] +>(api: T, maybeMethod: U, ...option: Parameters) { + const method = typeof maybeMethod === 'string' ? maybeMethod : '$get' + const opt = typeof maybeMethod === 'string' ? (option as any)[0] : maybeMethod + + return [api.$path(opt), method] +} +export { getAspidaSWRDefaultKey as getSWRDefaultKey } + function useAspidaSWR< T extends Record & { $get: (option: any) => Promise @@ -48,17 +68,13 @@ function useAspidaSWR< const enabled = opt?.enabled ?? true - const getOriginalKey = (api => () => { - return api && [api.$path(opt), method] - })(api) - const key = opt?.key !== undefined ? typeof opt.key === 'function' - ? opt.key(opt, getOriginalKey) + ? opt.key() : opt.key : enabled - ? getOriginalKey() + ? getAspidaSWRDefaultKey(api as any, method as any, opt) : null const fetcherInterv = opt?.fetcher === undefined ? (enabled ? (f: any) => f : null) : opt?.fetcher diff --git a/packages/aspida-swrv/index.ts b/packages/aspida-swrv/index.ts index 1ff88edb..a9552ea9 100644 --- a/packages/aspida-swrv/index.ts +++ b/packages/aspida-swrv/index.ts @@ -8,12 +8,11 @@ type AspidaSWRVOption = T & { * @deprecated * Will be dropped next major release. * Use key and fetcher options instead. - * key: (_opt, getOriginalKey) => - * enabled ? getOriginalKey() : null, + * key: enabled ? undefined : null, * fetcher: enabled ? undefined : null */ enabled?: boolean - key?: keyType | ((opt: AspidaSWRVOption, getOriginalKey: () => string[] | null) => keyType) + key?: keyType | (() => keyType) fetcher?: | ((f: (opt: AspidaSWRVOption) => any) => (opt: AspidaSWRVOption) => any) | null @@ -31,6 +30,31 @@ type Res Promise> = IResponse< any > +function getAspidaSWRVDefaultKey< + T extends Record & { + $get: (option: any) => Promise + $path: (option?: any) => string + } +>(api: T, ...option: Options): string[] +function getAspidaSWRVDefaultKey< + T extends Record & { $path: (option?: any) => string }, + U extends { + [K in keyof T]: T[K] extends (option: any) => Promise ? K : never + }[keyof T] +>(api: T, method: U, ...option: Options): string[] +function getAspidaSWRVDefaultKey< + T extends Record & { $path: (option?: any) => string }, + U extends { + [K in keyof T]: T[K] extends (option: any) => Promise ? K : never + }[keyof T] +>(api: T, maybeMethod: U, ...option: Parameters) { + const method = typeof maybeMethod === 'string' ? maybeMethod : '$get' + const opt = typeof maybeMethod === 'string' ? (option as any)[0] : maybeMethod + + return [api.$path(opt), method] +} +export { getAspidaSWRVDefaultKey } + function useAspidaSWRV< T extends Record & { $get: (option: any) => Promise @@ -55,17 +79,13 @@ function useAspidaSWRV< const enabled = opt?.enabled ?? true - const getOriginalKey = (api => () => { - return api && [api.$path(opt), method] - })(api) - const key = opt?.key !== undefined ? typeof opt.key === 'function' - ? opt.key(opt, getOriginalKey) + ? opt.key() : opt.key : enabled - ? getOriginalKey() + ? getAspidaSWRVDefaultKey(api as any, method, opt) : null const fetcherInterv = opt?.fetcher === undefined ? (enabled ? (f: any) => f : null) : opt?.fetcher