import {z} from 'zod' export type UUID = string; export type GetApiSubscriptionsIdArgs = {id: UUID}; export type GetApiSubscriptionsIdOptions = {baseUrl?: string | undefined, apiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const uUID = z.string().min(25).max(25); export const amount = z.number().int().gte(0); export const currency = z.enum(["JPY", "USD", "EUR", "TWD", "KRW", "PLN", "GBP", "HKD", "SGD", "NZD", "AUD", "IDR", "MYR", "PHP", "THB", "CNY", "BRL", "CHF", "CAD", "VND"]); export const locale = z.enum(["ja", "en", "ko"]); export const subscriptionPeriod = z.enum(["weekly", "monthly", "yearly"]); export const subscription = z.object({id: uUID, resource: z.literal("subscription"), status: z.string(), amount: amount, currency: currency, customer: z.object({id: z.number().int(), uuid: uUID, merchant_id: z.number().int(), created_at: z.string(), updated_at: z.string(), email: z.string().nullable(), currency: currency, locale: locale, name: z.string().nullable(), phone: z.string().nullable(), archived_at: z.string().nullable()}), period: subscriptionPeriod, day: z.number().int(), payment_details: z.object({type: z.string(), month: z.string(), year: z.string(), email: z.string()}), retry_count: z.number().int(), retry_at: z.string().nullable(), next_capture_at: z.string(), created_at: z.string(), ended_at: z.string().nullable(), metadata: z.object({}), payments: z.array(uUID)}); 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 }; /** * Subscription: Show * * GET /subscriptions/{id} */ export const getApiSubscriptionsId = async (args: GetApiSubscriptionsIdArgs, options?: GetApiSubscriptionsIdOptions) => { const {baseUrl = "https://komoju.com/api/v1", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiKey !== undefined) headers.set('authorization', `Basic ${btoa(`${apiKey.username}:${apiKey.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/subscriptions/${encodeURIComponent(String(args.id))}${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(subscription, await res.json()) };