import {z} from 'zod' export type PrivilegeValueType = 'STRING' | 'BOOLEAN' | 'INTEGER' | 'SELECT'; export type PrivilegeConfig = {select_options?: Array | undefined}; export type FeaturePrivilegeBase = {code: string, name?: string | undefined, value_type?: PrivilegeValueType | undefined, config?: PrivilegeConfig | undefined}; export type FeatureRequest = {code: string, name?: string | undefined, description?: string | undefined, privileges?: Array | undefined}; export type CreateApiFeaturesArgs = {body: FeatureRequest}; export type CreateApiFeaturesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const privilegeValueType = z.enum(["STRING", "BOOLEAN", "INTEGER", "SELECT"]); export const privilegeConfig = z.object({select_options: z.array(z.string()).optional()}); export const featurePrivilegeBase = z.object({code: z.string().max(255), name: z.string().max(255).optional(), value_type: privilegeValueType.optional(), config: privilegeConfig.optional()}); export const dateTimeWithZone = z.string(); export const featureResponse = z.object({code: z.string().max(255), name: z.string().max(255).optional(), description: z.string().max(600).optional(), privileges: z.array(featurePrivilegeBase).optional(), created_at: dateTimeWithZone}); 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 }; /** * Create a feature * * POST /features */ export const createApiFeatures = async (args: CreateApiFeaturesArgs, options?: CreateApiFeaturesOptions) => { const {baseUrl = "https://api-m.sandbox.paypal.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/features${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(featureResponse, await res.json()) };