import {z} from 'zod' export type ImportLocation = {type: 'common_prefix' | 'object', path: string, destination: string}; export type CommitCreation = {message: string, metadata?: {[key: string]: string} | undefined, date?: number | undefined, allow_empty?: boolean | undefined, force?: boolean | undefined}; export type ImportCreation = {paths: Array, commit: CommitCreation, force?: boolean | undefined}; export type CreateApiRepositoriesRepositoryBranchesBranchImportArgs = {body: ImportCreation}; export type CreateApiRepositoriesRepositoryBranchesBranchImportOptions = {baseUrl: string, jwtToken?: string | undefined, fetch?: typeof fetch | undefined}; export const importCreationResponse = z.object({id: z.string()}); 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 }; /** * import data from object store * * POST /repositories/{repository}/branches/{branch}/import * * Also accepts auth: basic_auth, cookie_auth */ export const createApiRepositoriesRepositoryBranchesBranchImport = async (args: CreateApiRepositoriesRepositoryBranchesBranchImportArgs, options: CreateApiRepositoriesRepositoryBranchesBranchImportOptions) => { const {baseUrl, fetch: fetchFn = fetch, jwtToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (jwtToken !== undefined) headers.set('authorization', `Bearer ${jwtToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/repositories/${encodeURIComponent(String(args.repository))}/branches/${encodeURIComponent(String(args.branch))}/import${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(importCreationResponse, await res.json()) };