import {z} from 'zod' export type CreateApiGoogleSearchJobPostingsArgs = {body: {query: string, location?: string | undefined, job_source?: string | undefined, country_code?: string | undefined, date_posted?: ('Any time' | 'Past 24 hours' | 'Past 3 days' | 'Past Week' | 'Past Month') | undefined, job_types?: (Array<'Contract' | 'Full-time' | 'Part-time' | 'Internship'>) | undefined, page?: number | undefined, num_pages?: number | undefined}}; export type CreateApiGoogleSearchJobPostingsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const googleJobPosting = z.object({id: z.string().optional(), url: z.string().optional(), title: z.string().optional(), location: z.string().optional(), description: z.string().optional(), job_type: z.string().nullable().optional(), date_posted: z.string().nullable().optional(), publisher_name: z.string().optional(), publisher_url: z.string().optional(), company_name: z.string().optional(), company_website_url: z.string().nullable().optional()}); export const createApiGoogleSearchJobPostingsResponse = z.object({job_postings: z.array(googleJobPosting).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 }; /** * Search job postings on Google * * POST /google/search-job-postings */ export const createApiGoogleSearchJobPostings = async (args: CreateApiGoogleSearchJobPostingsArgs, options?: CreateApiGoogleSearchJobPostingsOptions) => { const {baseUrl = "https://api.exfunc.dev", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/google/search-job-postings${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(createApiGoogleSearchJobPostingsResponse, await res.json()) };