|
| 1 | +import { type UseMutationOptions, useMutation } from "@tanstack/react-query" |
| 2 | +import { makeHeaders, type DefError, type DefResponse, getKey } from "client/api/helpers" |
| 3 | +import type { ClientDefinition } from "server/api/helpers" |
| 4 | +import { replaceParams } from "shared/replaceParams" |
| 5 | +import type { Prettify } from "shared/typeHelpers" |
| 6 | + |
| 7 | +type MutData = "Querystring" | "Params" | "Headers" | "Body" |
| 8 | + |
| 9 | +type DefVariables<Def extends ClientDefinition> = |
| 10 | + object extends Prettify<Pick<Def["schema"], MutData & keyof Def["schema"]>> |
| 11 | + ? null |
| 12 | + : Prettify<Pick<Def["schema"], MutData & keyof Def["schema"]>> |
| 13 | + |
| 14 | +type MissingKeys<from, provided extends Partial<from>> = from extends object |
| 15 | + ? object extends provided |
| 16 | + ? from |
| 17 | + : Pick< |
| 18 | + from, |
| 19 | + { |
| 20 | + [key in keyof from]: key extends keyof provided ? never : key |
| 21 | + }[keyof from] |
| 22 | + > |
| 23 | + : void |
| 24 | + |
| 25 | +export function useApiMutation< |
| 26 | + Def extends ClientDefinition, |
| 27 | + Early extends Partial<DefVariables<Def>> = object, |
| 28 | +>( |
| 29 | + { url, method }: Def, |
| 30 | + early?: Early | null, |
| 31 | + options?: Omit< |
| 32 | + UseMutationOptions< |
| 33 | + Prettify<DefResponse<Def>>, |
| 34 | + Prettify<DefError<Def>>, |
| 35 | + Prettify<MissingKeys<DefVariables<Def>, Early>> |
| 36 | + >, |
| 37 | + "mutationKey" | "mutationFn" |
| 38 | + > |
| 39 | +) { |
| 40 | + return useMutation< |
| 41 | + Prettify<DefResponse<Def>>, |
| 42 | + Prettify<DefError<Def>>, |
| 43 | + Prettify<MissingKeys<DefVariables<Def>, Early>> |
| 44 | + >({ |
| 45 | + ...options, |
| 46 | + mutationKey: getKey(url, method, early), |
| 47 | + async mutationFn(lazy: Prettify<MissingKeys<DefVariables<Def>, Early>>) { |
| 48 | + const data = { ...early, ...lazy } as unknown as DefVariables<Def> |
| 49 | + // Params are placed in the pathname |
| 50 | + const replaced = replaceParams(url, data?.Params ?? {}) |
| 51 | + // Querystring is placed in the search params |
| 52 | + const withBody = data?.Querystring |
| 53 | + ? `${replaced}?${new URLSearchParams(data.Querystring).toString()}` |
| 54 | + : replaced |
| 55 | + // Body is stringified and placed in the body |
| 56 | + const body = data?.Body ? JSON.stringify(data.Body) : undefined |
| 57 | + // Headers are placed in the headers |
| 58 | + const headers = |
| 59 | + makeHeaders(data?.Headers as Record<string, unknown>) ?? body |
| 60 | + ? new Headers() |
| 61 | + : undefined |
| 62 | + if (body) headers?.set("Content-Type", "application/json") |
| 63 | + const response = await fetch(withBody, { |
| 64 | + method, |
| 65 | + headers, |
| 66 | + body, |
| 67 | + }) |
| 68 | + const result = await response.json().catch(() => {}) |
| 69 | + if (response.status < 200 || response.status >= 300) throw result |
| 70 | + return result as Prettify<DefResponse<Def>> |
| 71 | + }, |
| 72 | + }) |
| 73 | +} |
0 commit comments