import {z} from 'zod' export type StoragePermissionsSchema = {method: 'FILE' | 'HTTP' | 'FTP' | 'SFTP' | 'S3' | 'B2' | 'GCS' | 'PORTAL' | 'CUSTOM' | 'AZURE' | 'DROPBOX', purpose: 'KEYFRAMES' | 'FILES' | 'PROXIES' | 'EXPORTS' | 'ARCHIVE' | 'FACES', settings: {[key: string]: unknown}}; export type CreateApiV1StoragesVerificationsPermissionsArgs = {'App-ID': string, 'Auth-Token': string, body: StoragePermissionsSchema}; export type CreateApiV1StoragesVerificationsPermissionsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const createApiV1StoragesVerificationsPermissionsResponse = z.object({cors: z.boolean().optional(), cors_error: z.string().optional(), delete_access: z.boolean().optional(), delete_access_error: z.string().optional(), error_message: z.string().optional(), read_access: z.boolean().optional(), read_access_error: z.string().optional(), write_access: z.boolean().optional(), write_access_error: z.string().optional()}); 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 }; /** * Verify permissions prior to creating a storage * * POST /v1/storages/verifications/permissions/ */ export const createApiV1StoragesVerificationsPermissions = async (args: CreateApiV1StoragesVerificationsPermissionsArgs, options: CreateApiV1StoragesVerificationsPermissionsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['App-ID'] != null) headers.set("App-ID", String(args['App-ID'])) if (args['Auth-Token'] != null) headers.set("Auth-Token", String(args['Auth-Token'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1/storages/verifications/permissions/${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(createApiV1StoragesVerificationsPermissionsResponse, await res.json()) };