import {z} from 'zod' export type ApiV1CandidateCustomPropertyItemArray = {id?: number | undefined, value?: (Array | null) | undefined}; export type ApiV1CandidateCustomPropertyItemString = {id?: number | undefined, value?: (string | null) | undefined}; export type ApiV1CandidateCustomPropertyItemInteger = {id?: number | undefined, value?: (number | null) | undefined}; export type CandidateCreate = {name: string, phone?: (string | null) | undefined, email?: (string | null) | undefined, description?: (string | null) | undefined, hurma_vacancy_id?: (number | null) | undefined, cv?: (string | null) | undefined, position?: (string | null) | undefined, birth_date?: (string | null) | undefined, sex?: (string | null) | undefined, skype?: (string | null) | undefined, linked_in?: (string | null) | undefined, telegram?: (string | null) | undefined, current_job?: (string | null) | undefined, job_comment?: (string | null) | undefined, education?: (string | null) | undefined, residence?: (string | null) | undefined, relocate?: (string | null) | undefined, duties?: (string | null) | undefined, additional_info?: (string | null) | undefined, custom_properties?: (Array | null) | undefined}; export type CreateApiCandidateArgs = {token: string, body: CandidateCreate}; export type CreateApiCandidateOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const candidateCreateResponse = z.object({result: z.string().optional(), error: z.boolean().optional(), code: z.number().int().optional(), messages: z.array(z.string()).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 candidate * * POST /candidate */ export const createApiCandidate = async (args: CreateApiCandidateArgs, options?: CreateApiCandidateOptions) => { const {baseUrl = "https://hurma.work/api/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.token != null) headers.set("token", String(args.token)) const url = `${baseUrl.replace(/\/+$/, '')}/candidate${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(candidateCreateResponse, await res.json()) };