import {z} from 'zod' export type AgreementScope = 'platform' | 'product'; export type ProductType = 'brokerage' | 'retirement_account'; export type AgreementAcceptance = 'required' | 'not_required'; export type GetApiAgreementsArgs = {cursor?: string | undefined, limit?: number | undefined, scope?: AgreementScope | undefined, product?: ProductType | undefined, acceptance?: AgreementAcceptance | undefined, 'LMG-Data-Privacy-Access-Principal': string, 'LMG-Data-Privacy-Access-Justification': string}; export type GetApiAgreementsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const agreementType = z.enum(["pricing_table", "privacy_policy", "terms_and_conditions"]); export const agreementScope = z.enum(["platform", "product"]); export const productType = z.enum(["brokerage", "retirement_account"]); export const agreementAcceptance = z.enum(["required", "not_required"]); export const agreementResponse = z.object({id: z.string(), type: agreementType, url: z.string(), language: z.string(), name: z.string(), scope: agreementScope, product: productType.nullable().optional(), acceptance: agreementAcceptance, effective_at: z.string()}); export const pagination = z.object({next_cursor: z.string().optional()}); export const encodedPaginatedCollectionResponseAgreementResponse = z.object({data: z.array(agreementResponse), pagination: pagination}); 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 }; /** * Get Latest Agreements * * GET /agreements */ export const getApiAgreements = async (args: GetApiAgreementsArgs, options?: GetApiAgreementsOptions) => { const {baseUrl = "https://sandbox.api.lemon.markets/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.scope != null) query.append("scope", String(args.scope)) if (args.product != null) query.append("product", String(args.product)) if (args.acceptance != null) query.append("acceptance", String(args.acceptance)) if (args['LMG-Data-Privacy-Access-Principal'] != null) headers.set("LMG-Data-Privacy-Access-Principal", String(args['LMG-Data-Privacy-Access-Principal'])) if (args['LMG-Data-Privacy-Access-Justification'] != null) headers.set("LMG-Data-Privacy-Access-Justification", String(args['LMG-Data-Privacy-Access-Justification'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/agreements${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(encodedPaginatedCollectionResponseAgreementResponse, await res.json()) };