import {z} from 'zod' export type CreateApiV1AppsInstallsArgs = {body: {app: string, channel?: ('private_live' | 'private_test' | 'public' | 'testing') | undefined, code_challenge?: string | undefined, code_challenge_method?: string | undefined, expand?: Array | undefined}}; export type CreateApiV1AppsInstallsOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const appServiceResourceInstallContentSecurityPolicy = z.object({connect_src: z.array(z.string().max(5000)), image_src: z.array(z.string().max(5000))}); export const appsInstall = z.object({account: z.string().max(5000), app: z.string().max(5000), approval_required: z.boolean(), auth_code: z.string().max(5000).nullable().optional(), channel: z.enum(["private_live", "private_test", "public", "review", "testing"]), content_security_policy_granted: appServiceResourceInstallContentSecurityPolicy, content_security_policy_pending: appServiceResourceInstallContentSecurityPolicy, created: z.number().int(), created_by: z.string().max(5000).nullable().optional(), endpoints_granted: z.array(z.string().max(5000)), endpoints_pending: z.array(z.string().max(5000)), id: z.string().max(5000), livemode: z.boolean(), object: z.literal("apps.install"), permissions_granted: z.array(z.string().max(5000)), permissions_pending: z.array(z.string().max(5000)), status: z.enum(["install_failed", "installed", "installing", "uninstall_failed", "uninstalling"])}); 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 an app install * * POST /v1/apps/installs * * Also accepts auth: bearerAuth */ export const createApiV1AppsInstalls = async (args: CreateApiV1AppsInstallsArgs, options?: CreateApiV1AppsInstallsOptions) => { const {baseUrl = "https://api.stripe.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/x-www-form-urlencoded") headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/apps/installs${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: new URLSearchParams(Object.entries(args.body).flatMap(([key, value]) => (value == null ? [] : [[key, String(value)]])))}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(appsInstall, await res.json()) };