import {z} from 'zod' export type CreateApiGlassdoorSearchJobPostingsArgs = {body: {query?: string | undefined, location?: string | undefined, date_posted?: ('Any time' | 'Past 24 hours' | 'Past 3 days' | 'Past Week' | 'Past Month') | undefined, next_token?: (string | null) | undefined}}; export type CreateApiGlassdoorSearchJobPostingsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const glassdoorJobPosting = z.object({url: z.string().optional(), title: z.string().optional(), location: z.string().optional(), description: z.string().optional(), date_posted: z.string().optional(), company_name: z.string().optional(), company_url: z.string().nullable().optional(), company_website_url: z.string().nullable().optional(), salary: z.object({currency: z.string().optional(), period: z.string().optional(), percentiles: z.object({p10: z.number().optional(), p50: z.number().optional(), p90: z.number().optional()}).optional()}).nullable().optional()}); export const createApiGlassdoorSearchJobPostingsResponse = z.object({job_postings: z.array(glassdoorJobPosting).optional(), next_token: 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 }; /** * Search job postings on Glassdoor * * POST /glassdoor/search-job-postings */ export const createApiGlassdoorSearchJobPostings = async (args: CreateApiGlassdoorSearchJobPostingsArgs, options?: CreateApiGlassdoorSearchJobPostingsOptions) => { 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(/\/+$/, '')}/glassdoor/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(createApiGlassdoorSearchJobPostingsResponse, await res.json()) };