import {z} from 'zod' export type GetApiV1SubscriptionsArgs = {page?: number | undefined, limit?: number | undefined, order?: ('ASC' | 'DESC') | undefined, sort?: string | undefined, query?: string | undefined}; export type GetApiV1SubscriptionsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const publicPlanDto = z.object({id: z.string(), name: z.string(), amount: z.number(), currency: z.string(), interval: z.string().optional(), isActive: z.boolean().optional(), createdAt: z.string().optional()}); export const publicSubscriptionDto = z.object({id: z.string(), status: z.string(), plan: publicPlanDto.optional(), customer: z.record(z.string(), z.unknown()).optional(), nextChargeAt: z.string().optional(), createdAt: z.string().optional()}); export const getApiV1SubscriptionsResponse = z.object({status: z.boolean(), message: z.string(), code: z.number(), data: z.object({page: z.number(), limit: z.number(), total: z.number(), pageCount: z.number(), hasPreviousPage: z.boolean(), hasNextPage: z.boolean(), data: z.array(publicSubscriptionDto)})}); 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 }; /** * List subscriptions * * GET /v1/subscriptions */ export const getApiV1Subscriptions = async (args: GetApiV1SubscriptionsArgs, options?: GetApiV1SubscriptionsOptions) => { const {baseUrl = "https://api.kyshi.co", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.order != null) query.append("order", String(args.order)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.query != null) query.append("query", String(args.query)) if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/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(getApiV1SubscriptionsResponse, await res.json()) };