import {z} from 'zod' export type UpdateApiApiProjectsArgs = {project: string, revision: string, 'Content-Length'?: number | undefined, schedule_from?: string | undefined, clear_schedule?: Array | undefined, clear_schedule_all?: boolean | undefined, skip_schedule?: boolean | undefined, body: BodyInit}; export type UpdateApiApiProjectsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const restProjectMetadata = z.object({key: z.string().optional(), value: z.string().optional()}); export const restProject = z.object({archiveMd5: z.string().optional(), archiveType: z.string(), createdAt: z.string(), deletedAt: z.string().optional(), id: z.string(), metadata: z.array(restProjectMetadata).optional(), name: z.string(), revision: z.string(), updatedAt: 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 }; /** * Upload a project archive as a new project or a new revision of an existing project * * PUT /api/projects */ export const updateApiApiProjects = async (args: UpdateApiApiProjectsArgs, options?: UpdateApiApiProjectsOptions) => { const {baseUrl = "https://api-workflow.treasuredata.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/gzip") headers.set('accept', "application/json") if (args.project != null) query.append("project", String(args.project)) if (args.revision != null) query.append("revision", String(args.revision)) if (args['Content-Length'] != null) headers.set("Content-Length", String(args['Content-Length'])) if (args.schedule_from != null) query.append("schedule_from", String(args.schedule_from)) for (const item of args.clear_schedule ?? []) query.append("clear_schedule", String(item)) if (args.clear_schedule_all != null) query.append("clear_schedule_all", String(args.clear_schedule_all)) if (args.skip_schedule != null) query.append("skip_schedule", String(args.skip_schedule)) if (ApiKeyAuth !== undefined) headers.set('authorization', `TD1 ${ApiKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/projects${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: args.body}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(restProject, await res.json()) };