import {z} from 'zod' export type GetApiApiJobsArgs = {category?: string | undefined, remote?: boolean | undefined, country?: string | undefined, q?: string | undefined, company?: string | undefined, from_employer?: boolean | undefined, include_closed?: boolean | undefined, after?: string | undefined, before?: string | undefined, before_id?: string | undefined, limit?: number | undefined}; export type GetApiApiJobsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const jobSalary = z.object({currency: z.string().nullable().optional(), max: z.number().int().nullable().optional(), min: z.number().int().nullable().optional()}); export const job = z.object({apply_url: z.string().optional(), categories: z.array(z.string()), company: z.string(), country: z.string().nullable(), description: z.string().optional(), employment_type: z.enum(["FULL_TIME", "PART_TIME", "CONTRACTOR", "TEMPORARY", "INTERN", "OTHER", "null"]).nullable(), expires_at: z.string(), from_employer: z.boolean(), hybrid: z.boolean(), id: z.string(), location: z.string(), noozra_url: z.string(), posted_at: z.string(), remote: z.boolean(), salary: jobSalary.optional(), source: z.string(), title: z.string(), url: z.string()}); export const jobsResponse = z.object({count: z.number().int(), jobs: z.array(job), next_before: z.string().nullable(), next_before_id: z.string().nullable()}); 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 }; /** * List job listings * * GET /api/jobs */ export const getApiApiJobs = async (args: GetApiApiJobsArgs, options?: GetApiApiJobsOptions) => { const {baseUrl = "https://noozra.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.category != null) query.append("category", String(args.category)) if (args.remote != null) query.append("remote", String(args.remote)) if (args.country != null) query.append("country", String(args.country)) if (args.q != null) query.append("q", String(args.q)) if (args.company != null) query.append("company", String(args.company)) if (args.from_employer != null) query.append("from_employer", String(args.from_employer)) if (args.include_closed != null) query.append("include_closed", String(args.include_closed)) if (args.after != null) query.append("after", String(args.after)) if (args.before != null) query.append("before", String(args.before)) if (args.before_id != null) query.append("before_id", String(args.before_id)) if (args.limit != null) query.append("limit", String(args.limit)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/jobs${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(jobsResponse, await res.json()) };