import {z} from 'zod' export type GetApiPlansPlanCodeEntitlementsFeatureCodeArgs = {plan_code: string, feature_code: string}; export type GetApiPlansPlanCodeEntitlementsFeatureCodeOptions = {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 planEntitlementPrivilege = z.object({code: z.string().max(255), name: z.string().max(255).optional(), value_type: privilegeValueType, config: privilegeConfig.optional(), value: z.unknown()}); export const planEntitlementResponse = z.object({feature_code: z.string().max(255), name: z.string().max(255).optional(), description: z.string().max(600).optional(), feature_privileges: z.array(planEntitlementPrivilege)}); 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 }; /** * Retrieve an entitlement * * GET /plans/{plan_code}/entitlements/{feature_code} */ export const getApiPlansPlanCodeEntitlementsFeatureCode = async (args: GetApiPlansPlanCodeEntitlementsFeatureCodeArgs, options?: GetApiPlansPlanCodeEntitlementsFeatureCodeOptions) => { const {baseUrl = "https://api-m.sandbox.paypal.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/plans/${encodeURIComponent(String(args.plan_code))}/entitlements/${encodeURIComponent(String(args.feature_code))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(planEntitlementResponse, await res.json()) };