|
| 1 | +import { |
| 2 | + $fetch, |
| 3 | + type FetchContext, |
| 4 | + type FetchError, |
| 5 | + type FetchOptions, |
| 6 | +} from 'ofetch' |
| 7 | +import type { |
| 8 | + ErrorResponse, |
| 9 | + MediaType, |
| 10 | + OperationRequestBodyContent, |
| 11 | + ResponseObjectMap, |
| 12 | + SuccessResponse, |
| 13 | +} from 'openapi-typescript-helpers' |
| 14 | + |
| 15 | +export type FetchResponseData< |
| 16 | + T, |
| 17 | + Media extends MediaType = MediaType, |
| 18 | +> = SuccessResponse<ResponseObjectMap<T>, Media> |
| 19 | + |
| 20 | +export type FetchResponseError< |
| 21 | + T, |
| 22 | + Media extends MediaType = MediaType, |
| 23 | +> = FetchError<ErrorResponse<ResponseObjectMap<T>, Media>> |
| 24 | + |
| 25 | +export type MethodOption<M, P> = 'get' extends keyof P |
| 26 | + ? { method?: M } |
| 27 | + : { method: M } |
| 28 | + |
| 29 | +export type ParamsOption<T> = T extends { parameters?: any; query?: any } |
| 30 | + ? T['parameters'] |
| 31 | + : Record<string, never> |
| 32 | + |
| 33 | +export type RequestBodyOption<T> = |
| 34 | + OperationRequestBodyContent<T> extends never |
| 35 | + ? { body?: never } |
| 36 | + : undefined extends OperationRequestBodyContent<T> |
| 37 | + ? { body?: OperationRequestBodyContent<T> } |
| 38 | + : { body: OperationRequestBodyContent<T> } |
| 39 | + |
| 40 | +export type FilterMethods<T> = { |
| 41 | + [K in keyof Omit<T, 'parameters'> as T[K] extends never | undefined |
| 42 | + ? never |
| 43 | + : K]: T[K] |
| 44 | +} |
| 45 | + |
| 46 | +export type OpenFetchOptions< |
| 47 | + Method, |
| 48 | + LowercasedMethod, |
| 49 | + Params, |
| 50 | + Operation = 'get' extends LowercasedMethod |
| 51 | + ? 'get' extends keyof Params |
| 52 | + ? Params['get'] |
| 53 | + : never |
| 54 | + : LowercasedMethod extends keyof Params |
| 55 | + ? Params[LowercasedMethod] |
| 56 | + : never, |
| 57 | +> = MethodOption<Method, Params> & |
| 58 | + ParamsOption<Operation> & |
| 59 | + RequestBodyOption<Operation> & |
| 60 | + Omit<FetchOptions, 'query' | 'body' | 'method'> |
| 61 | + |
| 62 | +export type OpenFetchClient<Paths> = < |
| 63 | + ReqT extends Extract<keyof Paths, string>, |
| 64 | + Methods extends FilterMethods<Paths[ReqT]>, |
| 65 | + Method extends |
| 66 | + | Extract<keyof Methods, string> |
| 67 | + | Uppercase<Extract<keyof Methods, string>>, |
| 68 | + LowercasedMethod extends Lowercase<Method> extends keyof FilterMethods< |
| 69 | + Paths[ReqT] |
| 70 | + > |
| 71 | + ? Lowercase<Method> |
| 72 | + : never, |
| 73 | + DefaultMethod extends 'get' extends LowercasedMethod |
| 74 | + ? 'get' |
| 75 | + : LowercasedMethod, |
| 76 | + ResT = FetchResponseData<Paths[ReqT][DefaultMethod]>, |
| 77 | +>( |
| 78 | + url: ReqT, |
| 79 | + options?: OpenFetchOptions<Method, LowercasedMethod, Methods>, |
| 80 | +) => Promise<ResT> |
| 81 | + |
| 82 | +// More flexible way to rewrite the request path, |
| 83 | +// but has problems - https://github.com/unjs/ofetch/issues/319 |
| 84 | +export function openFetchRequestInterceptor(ctx: FetchContext) { |
| 85 | + ctx.request = fillPath( |
| 86 | + ctx.request as string, |
| 87 | + (ctx.options as { path: Record<string, string> }).path, |
| 88 | + ) |
| 89 | +} |
| 90 | + |
| 91 | +export function createOpenFetch<Paths>( |
| 92 | + options: FetchOptions | ((options: FetchOptions) => FetchOptions), |
| 93 | +): OpenFetchClient<Paths> { |
| 94 | + return (url: string, opts: any) => { |
| 95 | + return $fetch( |
| 96 | + fillPath(url, opts?.path), |
| 97 | + typeof options === 'function' |
| 98 | + ? options(opts) |
| 99 | + : { |
| 100 | + ...options, |
| 101 | + ...opts, |
| 102 | + }, |
| 103 | + ) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +function fillPath(path: string, params: object = {}) { |
| 108 | + for (const [k, v] of Object.entries(params)) |
| 109 | + path = path.replace(`{${k}}`, encodeURIComponent(String(v))) |
| 110 | + return path |
| 111 | +} |
0 commit comments