import {z} from 'zod' export type CreateUploadSessionRequest = {graph_inclusion?: ('auto' | 'include' | 'exclude') | undefined, labels?: {[key: string]: string} | undefined, on_conflict?: ('reject' | 'new_version') | undefined, path: string, source_modified_at?: (string | null) | undefined, total_size_bytes: number, ttl_seconds?: number | undefined}; export type CreateApiV1WorkspacesWorkspaceIdUploadSessionsArgs = {workspace_id: string, 'Idempotency-Key'?: (string | null) | undefined, body: CreateUploadSessionRequest}; export type CreateApiV1WorkspacesWorkspaceIdUploadSessionsOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const createUploadSessionResponse = z.object({chunk_size: z.number().int(), expires_at: z.string(), session_id: z.string(), session_token: z.string(), total_parts: z.number().int()}); 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_upload_session * * POST /v1/workspaces/{workspace_id}/upload-sessions * * Also accepts auth: UploadGrantHeader */ export const createApiV1WorkspacesWorkspaceIdUploadSessions = async (args: CreateApiV1WorkspacesWorkspaceIdUploadSessionsArgs, options?: CreateApiV1WorkspacesWorkspaceIdUploadSessionsOptions) => { const {baseUrl = "https://ndi-api.nace.ai", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (APIKeyHeader !== undefined) headers.set("X-API-Key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/workspaces/${encodeURIComponent(String(args.workspace_id))}/upload-sessions${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(createUploadSessionResponse, await res.json()) };