import {z} from 'zod' export type CreateJobPost = {title?: string | undefined, salary_range?: string | undefined, job_type?: string | undefined, location?: string | undefined, deadline?: string | undefined, job_mode?: string | undefined, experience_level?: string | undefined, benefits?: string | undefined, company_name?: string | undefined, description?: string | undefined, key_responsibilities?: string | undefined, qualifications?: string | undefined}; export type CreateApiJobsArgs = {body: CreateJobPost}; export type CreateApiJobsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const jobPost = z.object({id: z.string().optional(), title: z.string().optional(), salary_range: z.string().optional(), job_type: z.string().optional(), location: z.string().optional(), deadline: z.string().optional(), job_mode: z.string().optional(), experience_level: z.string().optional(), how_to_apply: z.string().optional(), benefits: z.string().optional(), company_name: z.string().optional(), description: z.string().optional(), key_responsibilities: z.string().optional(), qualifications: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const jobPostResponse = z.object({status: z.string().optional(), status_code: z.number().int().optional(), message: z.string().optional(), data: jobPost.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 new job post * * POST /jobs */ export const createApiJobs = async (args: CreateApiJobsArgs, options?: CreateApiJobsOptions) => { const {baseUrl = "https://staging.api-golang.boilerplate.hng.tech/api/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/jobs${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(jobPostResponse, await res.json()) };