import {z} from 'zod' export type ProviderCommercialAcceptanceRequest = {event_type: 'pilot_company', provider_acceptance_reference: string} | {event_type: 'terms_acceptance', offer_id: string, offer_version: number, exact_terms_sha256: string, provider_acceptance_reference: string} | {event_type: 'terms_renewal', offer_id: string, related_acceptance_event_id: string, offer_version: number, exact_terms_sha256: string, provider_acceptance_reference: string}; export type CreateApiProviderCommercialAcceptancesArgs = {'Idempotency-Key': string, body: ProviderCommercialAcceptanceRequest}; export type CreateApiProviderCommercialAcceptancesOptions = {baseUrl?: string | undefined, ProviderKey?: string | undefined, fetch?: typeof fetch | undefined}; export const providerCommercialAcceptanceEvent = z.object({id: z.string(), provider_claim_id: z.string(), provider_offer_id: z.string().optional(), provider_api_key_id: z.number().int(), event_type: z.enum(["pilot_company", "terms_acceptance", "terms_renewal"]), related_acceptance_event_id: z.string().optional(), offer_version: z.number().int().gte(1).optional(), terms_contract_version: z.literal("nhs-provider-commercial-terms-v1").optional(), exact_terms_sha256: z.string().optional(), provider_acceptance_reference: z.string().min(8).max(200), provider_accepted_at: z.string(), valid_until: z.string().optional(), created_at: z.string()}); export const providerCommercialAcceptanceResponse = z.object({acceptance: providerCommercialAcceptanceEvent, created: z.boolean(), idempotent_replay: z.boolean(), provider_authenticated: z.literal(true), owner_verification_required: z.literal(true), commercial_proof_created: z.literal(false), evidence_scope: 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 }; /** * Record one provider-authenticated commercial acceptance * * POST /provider/commercial-acceptances */ export const createApiProviderCommercialAcceptances = async (args: CreateApiProviderCommercialAcceptancesArgs, options?: CreateApiProviderCommercialAcceptancesOptions) => { const {baseUrl = "https://nothumansearch.ai/api/v1", fetch: fetchFn = fetch, ProviderKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (ProviderKey !== undefined) headers.set("X-NHS-Provider-Key", ProviderKey) const url = `${baseUrl.replace(/\/+$/, '')}/provider/commercial-acceptances${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(providerCommercialAcceptanceResponse, await res.json()) };