import {z} from 'zod' export type GetApiApplicationsArgs = {status?: ('saved' | 'applied' | 'screening' | 'interview' | 'offer' | 'rejected') | undefined, tags?: Array | undefined, includeArchived?: boolean | undefined}; export type GetApiApplicationsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApplicationsResponse = z.array(z.object({id: z.string(), company: z.string().min(1), role: z.string().min(1), location: z.string().nullable(), salary: z.string().nullable(), status: z.enum(["saved", "applied", "screening", "interview", "offer", "rejected"]), archived: z.boolean(), resumeId: z.string().nullable(), source: z.string().nullable(), tags: z.array(z.string()), sourceUrl: z.string().nullable(), jobDescription: z.string().max(20000).nullable(), matchScore: z.number().int().gte(0).lte(100).nullable(), aiMetadata: z.record(z.string(), z.unknown()).nullable(), notes: z.string().nullable(), resumeFileUrl: z.string().nullable(), resumeFileName: z.string().nullable(), coverLetterUrl: z.string().nullable(), coverLetterName: z.string().nullable(), followUpAt: z.string().nullable(), followUpNote: z.string().nullable(), contacts: z.array(z.object({name: z.string().min(1), role: z.string().optional(), type: z.string().optional()})), activity: z.array(z.object({id: z.string().min(1), type: z.enum(["stage", "note", "created"]), text: z.string().min(1), at: z.string()})), appliedAt: z.string(), createdAt: z.string(), updatedAt: z.string()})); 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 applications * * GET /applications */ export const getApiApplications = async (args: GetApiApplicationsArgs, options?: GetApiApplicationsOptions) => { const {baseUrl = "http://localhost:3000/api/openapi", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.status != null) query.append("status", String(args.status)) for (const item of args.tags ?? []) query.append("tags", String(item)) if (args.includeArchived != null) query.append("includeArchived", String(args.includeArchived)) if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/applications${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(getApiApplicationsResponse, await res.json()) };