import {z} from 'zod' export type StorageSchema = {default?: boolean | undefined, description?: (string | null) | undefined, id?: string | undefined, last_scanned?: (string | null) | undefined, method: 'FILE' | 'HTTP' | 'FTP' | 'SFTP' | 'S3' | 'B2' | 'GCS' | 'PORTAL' | 'CUSTOM' | 'AZURE' | 'DROPBOX', name: string, purpose: 'KEYFRAMES' | 'FILES' | 'PROXIES' | 'EXPORTS' | 'ARCHIVE' | 'FACES', scanner_status?: (string | null) | undefined, settings: {[key: string]: unknown}, status?: ('ACTIVE' | 'INACTIVE' | 'FAILING') | undefined, status_message?: (string | null) | undefined, version?: string | undefined}; export type CreateApiV1StoragesArgs = {'App-ID': string, 'Auth-Token': string, body: StorageSchema}; export type CreateApiV1StoragesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const storageReadSchema = z.object({default: z.boolean().optional(), description: z.string().nullable().optional(), id: z.string().optional(), isg_version: z.string().optional(), last_scanned: z.string().nullable().optional(), method: z.enum(["FILE", "HTTP", "FTP", "SFTP", "S3", "B2", "GCS", "PORTAL", "CUSTOM", "AZURE", "DROPBOX"]), name: z.string(), purpose: z.enum(["KEYFRAMES", "FILES", "PROXIES", "EXPORTS", "ARCHIVE", "FACES"]), scanner_status: z.string().nullable().optional(), settings: z.record(z.string(), z.unknown()), status: z.enum(["ACTIVE", "INACTIVE", "FAILING"]).optional(), status_message: z.string().nullable().optional(), storage_gateway_cluster_id: z.string().optional(), storage_gateway_ids: z.array(z.string()).optional(), version: 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 }; /** * Create a new storage * * POST /v1/storages/ */ export const createApiV1Storages = async (args: CreateApiV1StoragesArgs, options: CreateApiV1StoragesOptions) => { 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/${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(storageReadSchema, await res.json()) };