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 FeatureUpdateRequest = {name?: string | undefined, description?: string | undefined, privileges?: Array | undefined}; export type UpdateApiFeaturesFeatureCodeArgs = {feature_code: string, body: FeatureUpdateRequest}; export type UpdateApiFeaturesFeatureCodeOptions = {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 }; /** * Update a feature * * PUT /features/{feature_code} */ export const updateApiFeaturesFeatureCode = async (args: UpdateApiFeaturesFeatureCodeArgs, options?: UpdateApiFeaturesFeatureCodeOptions) => { 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/${encodeURIComponent(String(args.feature_code))}${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(featureResponse, await res.json()) };