import {z} from 'zod' export type PropertyHrisLocationAddress = {address1?: string | undefined, address2?: string | undefined, city?: string | undefined, region?: string | undefined, region_code?: string | undefined, postal_code?: string | undefined, country?: string | undefined, country_code?: string | undefined}; export type HrisTelephone = {telephone: string, type?: ('WORK' | 'HOME' | 'OTHER' | 'FAX' | 'MOBILE') | undefined}; export type PropertyHrisLocationTelephones = Array; export type HrisMetadata = {id?: string | undefined, value?: ({[key: string]: unknown} | string | number | boolean | Array | string | number | boolean>) | undefined, namespace?: string | undefined, format?: ('TEXT' | 'NUMBER' | 'DATE' | 'BOOLEAN' | 'FILE' | 'TEXTAREA' | 'SINGLE_SELECT' | 'MULTIPLE_SELECT' | 'MEASUREMENT' | 'PRICE' | 'YES_NO' | 'CURRENCY' | 'URL') | undefined, extra_data?: ({[key: string]: unknown} | string | number | boolean | Array | string | number | boolean>) | undefined, slug?: string | undefined}; export type PropertyHrisLocationMetadata = Array; export type HrisLocation = {id?: string | undefined, created_at?: string | undefined, updated_at?: string | undefined, name?: string | undefined, description?: string | undefined, address?: PropertyHrisLocationAddress | undefined, parent_id?: string | undefined, external_identifier?: string | undefined, telephones?: PropertyHrisLocationTelephones | undefined, timezone?: string | undefined, currency?: string | undefined, language_locale?: string | undefined, is_active?: boolean | undefined, is_hq?: boolean | undefined, company_id?: string | undefined, metadata?: PropertyHrisLocationMetadata | undefined, raw?: {[key: string]: unknown} | undefined}; export type CreateApiHrisConnectionIdLocationArgs = {fields?: (Array<'id' | 'created_at' | 'updated_at' | 'name' | 'description' | 'address' | 'parent_id' | 'external_identifier' | 'telephones' | 'timezone' | 'currency' | 'language_locale' | 'is_active' | 'is_hq' | 'company_id' | 'metadata' | 'raw'>) | undefined, raw?: string | undefined, connection_id: string, body: HrisLocation}; export type CreateApiHrisConnectionIdLocationOptions = {baseUrl?: string | undefined, jwt?: string | undefined, fetch?: typeof fetch | undefined}; export const propertyHrisLocationAddress = z.object({address1: z.string().optional(), address2: z.string().optional(), city: z.string().optional(), region: z.string().optional(), region_code: z.string().optional(), postal_code: z.string().optional(), country: z.string().optional(), country_code: z.string().optional()}); export const hrisTelephone = z.object({telephone: z.string(), type: z.enum(["WORK", "HOME", "OTHER", "FAX", "MOBILE"]).optional()}); export const propertyHrisLocationTelephones = z.array(hrisTelephone); export const hrisMetadata = z.object({id: z.string().optional(), value: z.union([z.record(z.string(), z.unknown()), z.string(), z.number(), z.boolean(), z.array(z.union([z.object({}), z.string(), z.number(), z.boolean()]))]).optional(), namespace: z.string().optional(), format: z.enum(["TEXT", "NUMBER", "DATE", "BOOLEAN", "FILE", "TEXTAREA", "SINGLE_SELECT", "MULTIPLE_SELECT", "MEASUREMENT", "PRICE", "YES_NO", "CURRENCY", "URL"]).optional(), extra_data: z.union([z.record(z.string(), z.unknown()), z.string(), z.number(), z.boolean(), z.array(z.union([z.object({}), z.string(), z.number(), z.boolean()]))]).optional(), slug: z.string().optional()}); export const propertyHrisLocationMetadata = z.array(hrisMetadata); export const hrisLocation = z.object({id: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), name: z.string().optional(), description: z.string().optional(), address: propertyHrisLocationAddress.optional(), parent_id: z.string().optional(), external_identifier: z.string().optional(), telephones: propertyHrisLocationTelephones.optional(), timezone: z.string().optional(), currency: z.string().optional(), language_locale: z.string().optional(), is_active: z.boolean().optional(), is_hq: z.boolean().optional(), company_id: z.string().optional(), metadata: propertyHrisLocationMetadata.optional(), raw: z.record(z.string(), z.unknown()).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 a location * * POST /hris/{connection_id}/location */ export const createApiHrisConnectionIdLocation = async (args: CreateApiHrisConnectionIdLocationArgs, options?: CreateApiHrisConnectionIdLocationOptions) => { const {baseUrl = "https://api.unified.to", fetch: fetchFn = fetch, jwt} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") for (const item of args.fields ?? []) query.append("fields", String(item)) if (args.raw != null) query.append("raw", String(args.raw)) if (jwt !== undefined) headers.set("authorization", jwt) const url = `${baseUrl.replace(/\/+$/, '')}/hris/${encodeURIComponent(String(args.connection_id))}/location${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(hrisLocation, await res.json()) };