import {z} from 'zod' export type GetApiApiSessionsIdArgs = {id: number}; export type GetApiApiSessionsIdOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const attempt = z.object({cancelRequested: z.boolean(), createdAt: z.string(), done: z.boolean(), finishedAt: z.string().optional(), id: z.string(), params: z.object({}), poolId: z.string().optional(), retryAttemptName: z.string().optional(), status: z.enum(["blocked", "queued", "running", "canceling", "success", "error", "killed"]), success: z.boolean()}); export const idAndName = z.object({id: z.string(), name: z.string()}); export const nameOptionalId = z.object({id: z.string().optional(), name: z.string()}); export const restSession = z.object({id: z.string(), lastAttempt: attempt.optional(), project: idAndName, sessionTime: z.string(), sessionUuid: z.string().optional(), workflow: nameOptionalId}); 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 }; /** * Get a session * * GET /api/sessions/{id} */ export const getApiApiSessionsId = async (args: GetApiApiSessionsIdArgs, options?: GetApiApiSessionsIdOptions) => { const {baseUrl = "https://api-workflow.treasuredata.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set('authorization', `TD1 ${ApiKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/sessions/${encodeURIComponent(String(args.id))}${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(restSession, await res.json()) };