import {z} from 'zod' export type DefaultReceivingLocation = {'created-at'?: unknown | undefined, id?: number | undefined, item: DefaultReceivingLocation, 'item-id'?: number | undefined, 'updated-at'?: unknown | undefined, warehouse: DefaultReceivingLocation, 'warehouse-id'?: number | undefined, 'warehouse-location': DefaultReceivingLocation, 'warehouse-location-id'?: number | undefined}; export type CreateApiDefaultReceivingLocationsArgs = {return_object?: ('limited' | 'shallow' | 'none') | undefined, filter?: string | undefined, body: DefaultReceivingLocation}; export type CreateApiDefaultReceivingLocationsOptions = {baseUrl?: string | undefined, OAuth2AccessCode?: string | undefined, fetch?: typeof fetch | undefined}; export const defaultReceivingLocation: z.ZodType = z.object({"created-at": z.string().optional(), id: z.number().int().optional(), item: z.lazy(() => defaultReceivingLocation), "item-id": z.number().int().optional(), "updated-at": z.string().optional(), warehouse: z.lazy(() => defaultReceivingLocation), "warehouse-id": z.number().int().optional(), "warehouse-location": z.lazy(() => defaultReceivingLocation), "warehouse-location-id": z.number().int().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 }; /** * /default_receiving_locations * * POST /default_receiving_locations * * Also accepts auth: OAuth2ClientCredential */ export const createApiDefaultReceivingLocations = async (args: CreateApiDefaultReceivingLocationsArgs, options?: CreateApiDefaultReceivingLocationsOptions) => { const {baseUrl = "https://r35.coupadev.com/api", fetch: fetchFn = fetch, OAuth2AccessCode} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.return_object != null) query.append("return_object", String(args.return_object)) if (args.filter != null) query.append("filter", String(args.filter)) if (OAuth2AccessCode !== undefined) headers.set('authorization', `Bearer ${OAuth2AccessCode}`) const url = `${baseUrl.replace(/\/+$/, '')}/default_receiving_locations${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(defaultReceivingLocation, await res.json()) };