import {z} from 'zod' export type LocationRequest = {name?: string | undefined, address1?: string | undefined, address2?: string | undefined, city?: string | undefined, state?: string | undefined, postal_code?: string | undefined, country_id?: string | undefined, custom_value1?: (string | null) | undefined, custom_value2?: (string | null) | undefined, custom_value3?: (string | null) | undefined, custom_value4?: (string | null) | undefined, is_shipping_location?: boolean | undefined, assigned_user_id?: string | undefined, client_id?: string | undefined, vendor_id?: string | undefined}; export type CreateApiApiV1LocationsArgs = {'X-API-TOKEN': string, 'X-Requested-With': string, index?: string | undefined, body: LocationRequest}; export type CreateApiApiV1LocationsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const location = z.object({id: z.string().optional(), name: z.string().optional(), address1: z.string().optional(), address2: z.string().optional(), city: z.string().optional(), state: z.string().optional(), postal_code: z.string().optional(), country_id: z.string().optional(), custom_value1: z.string().nullable().optional(), custom_value2: z.string().nullable().optional(), custom_value3: z.string().nullable().optional(), custom_value4: z.string().nullable().optional(), is_deleted: z.boolean().optional(), is_shipping_location: z.boolean().optional(), user_id: z.string().optional(), assigned_user_id: z.string().optional(), client_id: z.string().optional(), vendor_id: z.string().optional(), created_at: z.number().int().optional(), updated_at: 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 }; /** * Create location * * POST /api/v1/locations */ export const createApiApiV1Locations = async (args: CreateApiApiV1LocationsArgs, options?: CreateApiApiV1LocationsOptions) => { const {baseUrl = "https://demo.invoiceninja.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-API-TOKEN'] != null) headers.set("X-API-TOKEN", String(args['X-API-TOKEN'])) if (args['X-Requested-With'] != null) headers.set("X-Requested-With", String(args['X-Requested-With'])) if (args.index != null) query.append("index", String(args.index)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/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(location, await res.json()) };