import {z} from 'zod' export type PatchApiV1EntitiesNameItemsIdArgs = {name: string, id: string, field: string, newValue: string}; export type PatchApiV1EntitiesNameItemsIdOptions = {baseUrl?: string | undefined, oAuthKeycloak?: string | undefined, fetch?: typeof fetch | undefined}; export const requestAccepted = z.object({status: z.string().optional(), code: z.number().int().optional(), type: z.string().optional(), detail: z.string().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Patch a single data item, by ID * * PATCH /v1/entities/{name}/items/{id} */ export const patchApiV1EntitiesNameItemsId = async (args: PatchApiV1EntitiesNameItemsIdArgs, options?: PatchApiV1EntitiesNameItemsIdOptions) => { const {baseUrl = "https://virtserver.swaggerhub.com/Viable-Data/Reference-Data-Service-API/0.0.1", fetch: fetchFn = fetch, oAuthKeycloak} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.field != null) query.append("field", String(args.field)) if (args.newValue != null) query.append("newValue", String(args.newValue)) if (oAuthKeycloak !== undefined) headers.set('authorization', `Bearer ${oAuthKeycloak}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/entities/${encodeURIComponent(String(args.name))}/items/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(requestAccepted, await res.json()) };