import {z} from 'zod' export type SubscriptionCreateRow = {partnerSubscriptionId?: string | undefined, zestPersonId: string, lumpSum: Record, shareClassSlug: string}; export type SubscriptionBulkCreate = {subscriptions: Array}; export type CreateApiV1SpvsSpvSlugSubscriptionsArgs = {spv_slug: string, 'Idempotency-Key'?: string | undefined, body: SubscriptionBulkCreate}; export type CreateApiV1SpvsSpvSlugSubscriptionsOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const subscriptionResult = z.object({partnerSubscriptionId: z.string().optional(), subscriptionSlug: z.string(), spvSlug: z.string(), personId: z.string(), status: z.string(), lumpSum: z.object({}), shareClassSlug: z.string()}); export const subscriptionBulkResponse = z.object({results: z.array(subscriptionResult)}); 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 }; /** * Create subscriptions for one SPV * * POST /v1/spvs/{spv_slug}/subscriptions */ export const createApiV1SpvsSpvSlugSubscriptions = async (args: CreateApiV1SpvsSpvSlugSubscriptionsArgs, options: CreateApiV1SpvsSpvSlugSubscriptionsOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = 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 (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/spvs/${encodeURIComponent(String(args.spv_slug))}/subscriptions${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(subscriptionBulkResponse, await res.json()) };