import {z} from 'zod' export type GetApiApiV1OpportunitiesArgs = {page?: number | undefined, pageSize?: number | undefined, archived?: boolean | undefined, name?: string | undefined, stageId?: string | undefined, ownerEmail?: string | undefined, type?: ('regular' | 'speculative' | 'job' | 'generic') | undefined, status?: ('open' | 'won' | 'lost') | undefined, candidateId?: string | undefined, createdAfter?: (string | string) | undefined, createdBefore?: (string | string) | undefined, updatedAfter?: (string | string) | undefined, updatedBefore?: (string | string) | undefined, includeDeleted?: ('true' | 'false') | undefined}; export type GetApiApiV1OpportunitiesOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV1OpportunitiesResponse = z.object({status: z.literal("ok"), data: z.array(z.object({id: z.string(), name: z.string(), type: z.enum(["regular", "speculative", "job", "generic"]), revision: z.enum(["v1", "v2"]), status: z.enum(["open", "won", "lost"]), value: z.number().int().nullable(), archived: z.boolean(), conversionLikelihood: z.number().int().nullable(), conversionReasoning: z.string().nullable(), conversionUpdatedAt: z.string().nullable(), stage: z.object({id: z.string(), name: z.string(), position: z.number().int(), type: z.enum(["default_stage", "converted"])}), stageUpdatedAt: z.string().nullable(), owner: z.object({id: z.string(), name: z.string(), email: z.string()}).nullable(), speculativePersonId: z.string().nullable().optional(), rejectionReason: z.string().nullable(), rejectedAt: z.string().nullable(), createdAt: z.string(), updatedAt: z.string(), deletedAt: z.string().nullable()})), total: z.number().int(), page: z.number().int(), pageSize: z.number().int(), hasMore: z.boolean()}); 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 opportunities * * GET /api/v1/opportunities */ export const getApiApiV1Opportunities = async (args: GetApiApiV1OpportunitiesArgs, options: GetApiApiV1OpportunitiesOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.archived != null) query.append("archived", String(args.archived)) if (args.name != null) query.append("name", String(args.name)) if (args.stageId != null) query.append("stageId", String(args.stageId)) if (args.ownerEmail != null) query.append("ownerEmail", String(args.ownerEmail)) if (args.type != null) query.append("type", String(args.type)) if (args.status != null) query.append("status", String(args.status)) if (args.candidateId != null) query.append("candidateId", String(args.candidateId)) if (args.createdAfter != null) query.append("createdAfter", String(args.createdAfter)) if (args.createdBefore != null) query.append("createdBefore", String(args.createdBefore)) if (args.updatedAfter != null) query.append("updatedAfter", String(args.updatedAfter)) if (args.updatedBefore != null) query.append("updatedBefore", String(args.updatedBefore)) if (args.includeDeleted != null) query.append("includeDeleted", String(args.includeDeleted)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/opportunities${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(getApiApiV1OpportunitiesResponse, await res.json()) };