import {z} from 'zod' export type GetApiWorkLocationsArgs = {companyID: string, workLocationID?: string | undefined, taxableLocationID?: string | undefined, state?: Array | undefined, work_site_id?: string | undefined}; export type GetApiWorkLocationsOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiWorkLocationsResponse = z.object({success: z.boolean().optional(), data: z.array(z.object({workLocationID: z.string().optional(), companyID: z.string().optional(), taxableLocationID: z.string().optional(), name: z.string().optional(), work_site_id: z.string().optional()})).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 }; /** * Get Work Locations * * GET /workLocations */ export const getApiWorkLocations = async (args: GetApiWorkLocationsArgs, options?: GetApiWorkLocationsOptions) => { const {baseUrl = "https://api.zeal.com", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.companyID != null) query.append("companyID", String(args.companyID)) if (args.workLocationID != null) query.append("workLocationID", String(args.workLocationID)) if (args.taxableLocationID != null) query.append("taxableLocationID", String(args.taxableLocationID)) for (const item of args.state ?? []) query.append("state", String(item)) if (args.work_site_id != null) query.append("work_site_id", String(args.work_site_id)) if (sec0 !== undefined) headers.set("Authorization", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/workLocations${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiWorkLocationsResponse, await res.json()) };