import {z} from 'zod' export type CreateApiCandidatesAuthorizationArgs = {body: {email?: string | undefined, password?: string | undefined}}; export type CreateApiCandidatesAuthorizationOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiCandidatesAuthorizationResponse = z.object({id: z.number().optional(), first_name: z.string().optional(), middle_name: z.string().optional(), last_name: z.string().optional(), title: z.string().optional(), emails: z.object({primary: z.string().optional(), secondary: z.string().optional()}).optional(), address: z.object({street: z.string().optional(), city: z.string().optional(), state: z.string().optional(), postal_code: z.string().optional()}).optional(), country_code: z.string().optional(), social_media_urls: z.array(z.unknown()).optional(), website: z.string().optional(), best_time_to_call: z.string().optional(), current_employer: z.string().optional(), date_available: z.string().optional(), current_pay: z.string().optional(), desired_pay: z.string().optional(), is_willing_to_relocate: z.boolean().optional(), key_skills: z.string().optional(), notes: z.string().optional(), source: z.string().optional(), is_hot: z.boolean().optional(), is_active: z.boolean().optional(), is_registered: z.boolean().optional(), contact_id: z.number().optional(), owner_id: z.number().optional(), entered_by_id: z.number().optional(), date_created: z.string().optional(), date_modified: z.string().optional(), _links: z.object({}).optional(), _embedded: z.object({}).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 }; /** * Authorize a candidate * * POST /candidates/authorization */ export const createApiCandidatesAuthorization = async (args: CreateApiCandidatesAuthorizationArgs, options?: CreateApiCandidatesAuthorizationOptions) => { const {baseUrl = "https://api.catsone.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/candidates/authorization${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(createApiCandidatesAuthorizationResponse, await res.json()) };