import {z} from 'zod' export type PriceActive = boolean; export type PriceNickname = string; export type Metadata = Record; export type UpdatePriceRequest = {active?: PriceActive | undefined, nickname?: PriceNickname | undefined, metadata?: Metadata | undefined}; export type UpdateApiPricesIdArgs = {body: UpdatePriceRequest}; export type UpdateApiPricesIdOptions = {baseUrl?: string | undefined, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const priceId = z.string(); export const priceProductId = z.string(); export const paymentCurrency = z.string(); export const priceUnitAmount = z.number().int().gte(1); export const priceType = z.string(); export const priceInterval = z.enum(["minute", "hour", "day", "week", "month", "year"]); export const priceIntervalCount = z.number().int().gte(1); export const priceTrialPeriodDays = z.number().int().gte(1).lte(1825); export const priceRecurring = z.object({interval: priceInterval, intervalCount: priceIntervalCount, trialPeriodDays: priceTrialPeriodDays.optional()}); export const priceActive = z.boolean(); export const priceNickname = z.string().max(100); export const metadata = z.object({}); export const createdAt = z.number().int(); export const updatedAt = z.number().int(); export const price = z.object({id: priceId, productId: priceProductId, currency: paymentCurrency, unitAmount: priceUnitAmount, type: priceType, recurring: priceRecurring.optional(), active: priceActive, nickname: priceNickname.optional(), metadata: metadata.optional(), createdAt: createdAt, updatedAt: updatedAt.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 }; /** * Update Price * * PUT /prices/{id} */ export const updateApiPricesId = async (args: UpdateApiPricesIdArgs, options?: UpdateApiPricesIdOptions) => { const {baseUrl = "https://api.monei.com/v1", fetch: fetchFn = fetch, APIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKey !== undefined) headers.set("Authorization", APIKey) const url = `${baseUrl.replace(/\/+$/, '')}/prices/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(price, await res.json()) };