-
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.
- Loading branch information
Showing
11 changed files
with
2,516 additions
and
26 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,5 +1,7 @@ | ||
<script setup lang="ts"></script> | ||
|
||
<template> | ||
<RouterView /> | ||
<Suspense> | ||
<RouterView /> | ||
</Suspense> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { | ||
$fetch, | ||
type FetchContext, | ||
type FetchError, | ||
type FetchOptions, | ||
ofetch, | ||
Check failure on line 6 in src/composables/fetch.ts GitHub Actions / typecheck
|
||
} from 'ofetch' | ||
import type { | ||
ErrorResponse, | ||
FilterKeys, | ||
MediaType, | ||
OperationRequestBodyContent, | ||
ResponseObjectMap, | ||
SuccessResponse, | ||
} from 'openapi-typescript-helpers' | ||
|
||
export type FetchResponseData<T> = FilterKeys< | ||
SuccessResponse<ResponseObjectMap<T>>, | ||
MediaType | ||
> | ||
export type FetchResponseError<T> = FetchError< | ||
FilterKeys<ErrorResponse<ResponseObjectMap<T>>, MediaType> | ||
> | ||
|
||
export type MethodOption<M, P> = 'get' extends keyof P | ||
? { method?: M } | ||
: { method: M } | ||
|
||
export type ParamsOption<T> = T extends { parameters?: any; query?: any } | ||
? T['parameters'] | ||
: Record<string, never> | ||
|
||
export type RequestBodyOption<T> = | ||
OperationRequestBodyContent<T> extends never | ||
? { body?: never } | ||
: undefined extends OperationRequestBodyContent<T> | ||
? { body?: OperationRequestBodyContent<T> } | ||
: { body: OperationRequestBodyContent<T> } | ||
|
||
export type FilterMethods<T> = { | ||
[K in keyof Omit<T, 'parameters'> as T[K] extends never | undefined | ||
? never | ||
: K]: T[K] | ||
} | ||
|
||
type OpenFetchOptions< | ||
Method, | ||
LowercasedMethod, | ||
Params, | ||
Operation = 'get' extends LowercasedMethod | ||
? 'get' extends keyof Params | ||
? Params['get'] | ||
: never | ||
: LowercasedMethod extends keyof Params | ||
? Params[LowercasedMethod] | ||
: never, | ||
> = MethodOption<Method, Params> & | ||
ParamsOption<Operation> & | ||
RequestBodyOption<Operation> & | ||
Omit<FetchOptions, 'query' | 'body' | 'method'> | ||
|
||
export type OpenFetchClient<Paths> = < | ||
ReqT extends Extract<keyof Paths, string>, | ||
Methods extends FilterMethods<Paths[ReqT]>, | ||
Method extends | ||
| Extract<keyof Methods, string> | ||
| Uppercase<Extract<keyof Methods, string>>, | ||
LowercasedMethod extends Lowercase<Method> extends keyof FilterMethods< | ||
Paths[ReqT] | ||
> | ||
? Lowercase<Method> | ||
: never, | ||
DefaultMethod extends 'get' extends LowercasedMethod | ||
? 'get' | ||
: LowercasedMethod, | ||
ResT = FetchResponseData<Paths[ReqT][DefaultMethod]>, | ||
>( | ||
url: ReqT, | ||
options?: OpenFetchOptions<Method, LowercasedMethod, Methods>, | ||
) => Promise<ResT> | ||
|
||
// More flexible way to rewrite the request path, | ||
// but has problems - https://github.com/unjs/ofetch/issues/319 | ||
export function openFetchRequestInterceptor(ctx: FetchContext) { | ||
ctx.request = fillPath( | ||
ctx.request as string, | ||
(ctx.options as { path: Record<string, string> }).path, | ||
) | ||
} | ||
|
||
export function createOpenFetch<Paths>( | ||
options: FetchOptions | ((options: FetchOptions) => FetchOptions), | ||
): OpenFetchClient<Paths> { | ||
return (url: string, opts: any) => { | ||
console.log('### :: file: fetch.ts:95 :: opts:', opts) | ||
return $fetch( | ||
fillPath(url, opts?.path), | ||
typeof options === 'function' | ||
? options(opts) | ||
: { | ||
...options, | ||
...opts, | ||
}, | ||
) | ||
} | ||
} | ||
|
||
export function fillPath(path: string, params: object = {}) { | ||
for (const [k, v] of Object.entries(params)) | ||
path = path.replace(`{${k}}`, encodeURIComponent(String(v))) | ||
return path | ||
} |
Oops, something went wrong.