import {z} from 'zod' export type GetApiV1StoragesMatchingPurposeArgs = {'App-ID': string, 'Auth-Token': string, purpose: string, storage_id?: string | undefined}; export type GetApiV1StoragesMatchingPurposeOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const storageSchema = z.object({default: z.boolean().optional(), description: z.string().nullable().optional(), id: 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(), 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 }; /** * Returns a remote storage matching type * * GET /v1/storages/matching/{purpose}/ */ export const getApiV1StoragesMatchingPurpose = async (args: GetApiV1StoragesMatchingPurposeArgs, options: GetApiV1StoragesMatchingPurposeOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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'])) if (args.storage_id != null) query.append("storage_id", String(args.storage_id)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/storages/matching/${encodeURIComponent(String(args.purpose))}/${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(storageSchema, await res.json()) };