import {z} from 'zod' export type GetApiCustomersCustomerIdSubscriptionsArgs = {customerId: string, limit?: number | undefined, lastEvaluatedKey?: string | undefined, expandLines?: boolean | undefined}; export type GetApiCustomersCustomerIdSubscriptionsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const subscriptionDynamo = z.object({SubscriptionId: z.string().optional(), SubscriptionStatus: z.string().optional(), NextBillingDate: z.string().optional()}); export const subscriptionKeyDynamo = z.object({PK: z.object({S: z.string().optional()}).optional(), SK: z.object({S: z.string().optional()}).optional()}); export const getApiCustomersCustomerIdSubscriptionsResponse = z.object({Items: z.array(subscriptionDynamo).optional(), lastEvaluatedKey: subscriptionKeyDynamo.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 }; /** * Get customer's subscriptions * * GET /customers/{customerId}/subscriptions */ export const getApiCustomersCustomerIdSubscriptions = async (args: GetApiCustomersCustomerIdSubscriptionsArgs, options?: GetApiCustomersCustomerIdSubscriptionsOptions) => { const {baseUrl = "https://api.awtomic.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.lastEvaluatedKey != null) query.append("lastEvaluatedKey", String(args.lastEvaluatedKey)) if (args.expandLines != null) query.append("expandLines", String(args.expandLines)) if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/customers/${encodeURIComponent(String(args.customerId))}/subscriptions${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(getApiCustomersCustomerIdSubscriptionsResponse, await res.json()) };