import {z} from 'zod' export type CreateApiContractsArgs = {'document-name': string, 'template-id': string, 'defined-term'?: string | undefined, url?: string | undefined, 'contract-id'?: string | undefined, 'party-b-defined-term'?: string | undefined, 'document-id'?: string | undefined, 'party-a-defined-term'?: string | undefined, 'contract-position-id'?: string | undefined, 'review-level'?: string | undefined, 'is-public'?: string | undefined}; export type CreateApiContractsOptions = {baseUrl?: string | undefined, lambdaApiAuthorizer?: string | undefined, fetch?: typeof fetch | undefined}; export const contractUpload = z.object({contract_id: z.string().optional(), document_id: 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 }; /** * Upload a contract * * POST /contracts * * Also accepts auth: api_key */ export const createApiContracts = async (args: CreateApiContractsArgs, options?: CreateApiContractsOptions) => { const {baseUrl = "https://api.termscout.com", fetch: fetchFn = fetch, lambdaApiAuthorizer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['document-name'] != null) query.append("document-name", String(args['document-name'])) if (args['template-id'] != null) query.append("template-id", String(args['template-id'])) if (args['defined-term'] != null) query.append("defined-term", String(args['defined-term'])) if (args.url != null) query.append("url", String(args.url)) if (args['contract-id'] != null) query.append("contract-id", String(args['contract-id'])) if (args['party-b-defined-term'] != null) query.append("party-b-defined-term", String(args['party-b-defined-term'])) if (args['document-id'] != null) query.append("document-id", String(args['document-id'])) if (args['party-a-defined-term'] != null) query.append("party-a-defined-term", String(args['party-a-defined-term'])) if (args['contract-position-id'] != null) query.append("contract-position-id", String(args['contract-position-id'])) if (args['review-level'] != null) query.append("review-level", String(args['review-level'])) if (args['is-public'] != null) query.append("is-public", String(args['is-public'])) if (lambdaApiAuthorizer !== undefined) headers.set("Authorization", lambdaApiAuthorizer) const url = `${baseUrl.replace(/\/+$/, '')}/contracts${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(contractUpload, await res.json()) };