import {z} from 'zod' export type GetApiV1GoogleJobsSearchArgs = {q: string, location?: (string | null) | undefined, gl?: string | undefined, country?: (string | null) | undefined, hl?: string | undefined, language?: (string | null) | undefined, domain?: string | undefined, job_type?: ('FULLTIME' | 'PARTTIME' | 'CONTRACTOR' | 'INTERN' | null) | undefined, date_posted?: ('today' | '3days' | 'week' | 'month' | null) | undefined, ltype?: ('remote' | 'hybrid' | 'onsite' | 'work_from_home' | null) | undefined, chips?: (string | null) | undefined, uds?: (string | null) | undefined, uule?: (string | null) | undefined, lrad?: (string | null) | undefined, next_page_token?: (string | null) | undefined, mode?: ('rpc' | 'serp') | undefined}; export type GetApiV1GoogleJobsSearchOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const jobSalary = z.object({min: z.string().nullable().optional(), max: z.string().nullable().optional(), currency: z.string().nullable().optional(), period: z.string().nullable().optional()}); export const jobApplyOption = z.object({source: z.string(), title: z.string().nullable().optional(), link: z.string()}); export const jobHighlights = z.object({qualifications: z.array(z.string()).optional(), responsibilities: z.array(z.string()).optional(), benefits: z.array(z.string()).optional()}); export const jobResult = z.object({title: z.string(), company_name: z.string(), location: z.string().nullable().optional(), via: z.string().nullable().optional(), url: z.string().nullable().optional(), description: z.string().nullable().optional(), salary: jobSalary.nullable().optional(), job_type: z.string().nullable().optional(), schedule_type: z.string().nullable().optional(), work_from_home: z.boolean().optional(), posted_at: z.string().nullable().optional(), thumbnail: z.string().nullable().optional(), apply_options: z.array(jobApplyOption).optional(), apply_links: z.array(z.string()).optional(), job_highlights: jobHighlights.nullable().optional(), extensions: z.array(z.string()).optional()}); export const jobFilterOption = z.object({label: z.string(), value: z.string().nullable().optional()}); export const jobFilter = z.object({name: z.string(), options: z.array(jobFilterOption).optional()}); export const pagination = z.object({current: z.union([z.number().int(), z.string()]).optional(), next: z.string().nullable().optional(), total_results: z.number().int().nullable().optional(), total_pages: z.number().int().nullable().optional(), page_no: z.record(z.string(), z.string()).optional()}); export const jobsSearchResponse = z.object({jobs: z.array(jobResult).optional(), jobs_results: z.array(jobResult).optional(), filters: z.array(jobFilter).optional(), pagination: pagination}); 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 }; /** * Search Google Jobs * * GET /v1/google/jobs/search */ export const getApiV1GoogleJobsSearch = async (args: GetApiV1GoogleJobsSearchArgs, options?: GetApiV1GoogleJobsSearchOptions) => { const {baseUrl = "https://scrapebadger.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.q != null) query.append("q", String(args.q)) if (args.location != null) query.append("location", String(args.location)) if (args.gl != null) query.append("gl", String(args.gl)) if (args.country != null) query.append("country", String(args.country)) if (args.hl != null) query.append("hl", String(args.hl)) if (args.language != null) query.append("language", String(args.language)) if (args.domain != null) query.append("domain", String(args.domain)) if (args.job_type != null) query.append("job_type", String(args.job_type)) if (args.date_posted != null) query.append("date_posted", String(args.date_posted)) if (args.ltype != null) query.append("ltype", String(args.ltype)) if (args.chips != null) query.append("chips", String(args.chips)) if (args.uds != null) query.append("uds", String(args.uds)) if (args.uule != null) query.append("uule", String(args.uule)) if (args.lrad != null) query.append("lrad", String(args.lrad)) if (args.next_page_token != null) query.append("next_page_token", String(args.next_page_token)) if (args.mode != null) query.append("mode", String(args.mode)) if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/google/jobs/search${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(jobsSearchResponse, await res.json()) };