import {z} from 'zod' export type CreateApiCardsCardIdNetworkTokensArgs = {card_id: string}; export type CreateApiCardsCardIdNetworkTokensOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const includedResourceType = z.enum(["card_updates", "card_update_subscriptions", "network_tokens"]); export const networkTokenAttributes = z.object({payment_account_reference: z.string(), network_token: z.string().min(13).max(19), last4: z.string().max(4), bin: z.string().min(6).max(10), exp_month: z.number().int().gte(1).lte(12), exp_year: z.number().int().gte(0).lte(99), created_at: z.string(), updated_at: z.string(), state: z.string(), reason_code: z.string().optional(), reason_text: z.string().optional()}); export const cardAsset = z.object({type: z.enum(["CARD_SYMBOL", "DIGITAL_CARD_ART", "DIGITAL_CARD_ART_BACKGROUND"]), mime_type: z.string().optional(), width: z.number().int().optional(), height: z.number().int().optional(), download_url: z.string()}); export const cardArt = z.object({background_color: z.string().optional(), foreground_color: z.string().optional(), label_color: z.string().optional(), issuer_name: z.string().optional(), contact_website: z.string().optional(), contact_number: z.string().optional(), contact_name: z.string().optional(), short_description: z.string().optional(), long_description: z.string().optional(), assets: z.array(cardAsset).optional()}); export const networkTokenResource = z.object({id: z.string(), type: includedResourceType, attributes: networkTokenAttributes.optional(), meta: z.object({card_art: cardArt.optional()}).optional(), relationships: z.record(z.string(), z.object({links: z.object({self: z.union([z.string().min(1), z.object({href: z.string().min(1).nullable().optional(), rel: z.string().nullable().optional(), describedby: z.string().nullable().optional(), title: z.string().nullable().optional(), type: z.string().nullable().optional(), hreflang: z.union([z.string(), z.array(z.string())]).nullable().optional(), meta: z.object({}).nullable().optional()})]).nullable().optional(), related: z.union([z.string().min(1), z.object({href: z.string().min(1).nullable().optional(), rel: z.string().nullable().optional(), describedby: z.string().nullable().optional(), title: z.string().nullable().optional(), type: z.string().nullable().optional(), hreflang: z.union([z.string(), z.array(z.string())]).nullable().optional(), meta: z.object({}).nullable().optional()})]).nullable().optional()}).nullable().optional(), data: z.union([z.object({id: z.string(), type: z.string(), meta: z.object({}).optional()}), z.array(z.object({id: z.string(), type: z.string(), meta: z.object({}).optional()}))]).nullable().optional(), meta: z.object({}).nullable().optional()})).optional(), links: z.record(z.string(), z.union([z.string().min(1), z.object({href: z.string().min(1).nullable().optional(), rel: z.string().nullable().optional(), describedby: z.string().nullable().optional(), title: z.string().nullable().optional(), type: z.string().nullable().optional(), hreflang: z.union([z.string(), z.array(z.string())]).nullable().optional(), meta: z.object({}).nullable().optional()}), z.string()]).nullable()).nullable().optional()}); export const networkTokenResourceResponse = z.object({data: networkTokenResource}); 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 }; /** * Provision * * POST /cards/{card_id}/network-tokens */ export const createApiCardsCardIdNetworkTokens = async (args: CreateApiCardsCardIdNetworkTokensArgs, options?: CreateApiCardsCardIdNetworkTokensOptions) => { const {baseUrl = "https://sandbox.vgsapi.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.api+json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/cards/${encodeURIComponent(String(args.card_id))}/network-tokens${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(networkTokenResourceResponse, await res.json()) };