import {z} from 'zod' export type AddCompanyAddressCommand = {street?: (Array | null) | undefined, city?: (string | null) | undefined, state?: (string | null) | undefined, postalCode?: (string | null) | undefined, countryCode?: (string | null) | undefined, phone?: (string | null) | undefined, fax?: (string | null) | undefined, url?: (string | null) | undefined, name: string, isPrimaryAddress?: boolean | undefined}; export type CreateApiCompaniesCompanyIdAddressesArgs = {companyId: number, body: AddCompanyAddressCommand}; export type CreateApiCompaniesCompanyIdAddressesOptions = {baseUrl: string, jobadderIdentity?: string | undefined, fetch?: typeof fetch | undefined}; export const modelLinks = z.object({self: z.string().nullable().optional()}); export const companyAddressRepresentation = z.object({addressId: z.string(), name: z.string().nullable().optional(), street: z.array(z.string()).nullable().optional(), city: z.string().nullable().optional(), state: z.string().nullable().optional(), postalCode: z.string().nullable().optional(), country: z.string().nullable().optional(), countryCode: z.string().nullable().optional(), phone: z.string().nullable().optional(), fax: z.string().nullable().optional(), url: z.string().nullable().optional(), links: modelLinks.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 }; /** * Add an address * * POST /companies/{companyId}/addresses */ export const createApiCompaniesCompanyIdAddresses = async (args: CreateApiCompaniesCompanyIdAddressesArgs, options: CreateApiCompaniesCompanyIdAddressesOptions) => { const {baseUrl, fetch: fetchFn = fetch, jobadderIdentity} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (jobadderIdentity !== undefined) headers.set('authorization', `Bearer ${jobadderIdentity}`) const url = `${baseUrl.replace(/\/+$/, '')}/companies/${encodeURIComponent(String(args.companyId))}/addresses${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(companyAddressRepresentation, await res.json()) };