import {z} from 'zod' export type Company = {id?: number | undefined, departmentId?: number | undefined, name: string, code?: (string | null) | undefined, email?: (string | null) | undefined, phone?: (string | null) | undefined, web?: (string | null) | undefined, countryCode?: (string | null) | undefined, state?: (string | null) | undefined, city?: (string | null) | undefined, town?: (string | null) | undefined, zip?: (string | null) | undefined, address?: (string | null) | undefined, createdUserId?: number | undefined, createdDate?: (string | null) | undefined, updatedUserId?: number | undefined, updatedDate?: (string | null) | undefined, customFields?: (string | null) | undefined, photoUrl?: (string | null) | undefined}; export type CreateApiV1CompaniesArgs = {body: Company}; export type CreateApiV1CompaniesOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const company = z.object({id: z.number().int().optional(), departmentId: z.number().int().optional(), name: z.string().min(1).max(200), code: z.string().max(200).nullable().optional(), email: z.string().max(100).nullable().optional(), phone: z.string().max(20).nullable().optional(), web: z.string().max(200).nullable().optional(), countryCode: z.string().max(3).nullable().optional(), state: z.string().max(100).nullable().optional(), city: z.string().max(100).nullable().optional(), town: z.string().max(100).nullable().optional(), zip: z.string().max(10).nullable().optional(), address: z.string().max(200).nullable().optional(), createdUserId: z.number().int().optional(), createdDate: z.string().nullable().optional(), updatedUserId: z.number().int().optional(), updatedDate: z.string().nullable().optional(), customFields: z.string().max(4096).nullable().optional(), photoUrl: z.string().max(512).nullable().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 company * * POST /v1/companies * * Also accepts auth: API Key */ export const createApiV1Companies = async (args: CreateApiV1CompaniesArgs, options: CreateApiV1CompaniesOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/companies${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(company, await res.json()) };