import {z} from 'zod' export type ApplicationId = string; export type CreateApiV2ApplicationsApplicationIdRefreshArgs = {application_id: ApplicationId, 'Getpaid-Idempotency-Key': string}; export type CreateApiV2ApplicationsApplicationIdRefreshOptions = {baseUrl?: string | undefined, OAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const applicationId = z.string().min(30).max(50); export const updatedAt = z.string().min(24).max(24); export const applicationExpiresAt = z.string().min(24).max(24); export const dateTime = z.string().min(24).max(24); export const applicationFlow = z.object({next_step: z.object({type: z.literal("redirect"), url: z.string().max(1024), expires_at: dateTime.optional()})}); export const refreshApplicationResponse = z.object({id: applicationId, updated_at: updatedAt, expires_at: applicationExpiresAt, flow: applicationFlow}); 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 }; /** * Refresh an application * * POST /v2/applications/{application_id}/refresh */ export const createApiV2ApplicationsApplicationIdRefresh = async (args: CreateApiV2ApplicationsApplicationIdRefreshArgs, options?: CreateApiV2ApplicationsApplicationIdRefreshOptions) => { const {baseUrl = "https://api.getpaid.io", fetch: fetchFn = fetch, OAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['Getpaid-Idempotency-Key'] != null) headers.set("Getpaid-Idempotency-Key", String(args['Getpaid-Idempotency-Key'])) if (OAuth !== undefined) headers.set('authorization', `Bearer ${OAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/applications/${encodeURIComponent(String(args.application_id))}/refresh${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(refreshApplicationResponse, await res.json()) };