import {z} from 'zod' export type GetApiVacancyArgs = {token: string, page?: number | undefined, per_page?: number | undefined, recruiter_id?: number | undefined, status?: number | undefined}; export type GetApiVacancyOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const vacancyListResponse = z.object({result: z.object({current_page: z.number().int().optional(), data: z.array(z.object({id: z.number().int().optional(), name: z.string().optional(), description: z.string().optional(), demand: z.string().optional(), addition: z.string().optional(), responsibility: z.string().optional(), working_conditions: z.string().optional(), number_of_positions: z.number().int().optional(), residence: z.string().optional(), hot_flag: z.number().int().optional(), published: z.number().int().optional(), status: z.number().int().optional(), work_types: z.array(z.object({type: z.number().int().optional(), name: z.string().optional()})).optional(), salary: z.array(z.object({from: z.number().int().optional(), to: z.number().int().optional(), currency: z.string().optional()})).optional(), responsible: z.array(z.object({id: z.number().int().optional(), name: z.string().optional()})).optional(), created_at: z.string().optional(), close_date: z.string().optional()})).optional(), first_page_url: z.string().optional(), from: z.number().int().optional(), last_page: z.number().int().optional(), last_page_url: z.string().optional(), next_page_url: z.string().nullable().optional(), path: z.string().nullable().optional(), per_page: z.number().int().optional(), prev_page_url: z.string().nullable().optional(), to: z.number().int().optional(), total: z.number().int().optional()}).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 }; /** * Information on job openings * * GET /vacancy */ export const getApiVacancy = async (args: GetApiVacancyArgs, options?: GetApiVacancyOptions) => { const {baseUrl = "https://hurma.work/api/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.token != null) headers.set("token", String(args.token)) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.recruiter_id != null) query.append("recruiter_id", String(args.recruiter_id)) if (args.status != null) query.append("status", String(args.status)) const url = `${baseUrl.replace(/\/+$/, '')}/vacancy${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(vacancyListResponse, await res.json()) };