import {z} from 'zod' export type GetApiApiWorkflowsIdTruncatedSessionTimeArgs = {id: number, session_time: string, mode?: ('second' | 'minute' | 'hour' | 'day' | 'schedule' | 'next_schedule') | undefined}; export type GetApiApiWorkflowsIdTruncatedSessionTimeOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const idAndName = z.object({id: z.string(), name: z.string()}); export const restWorkflowSessionTime = z.object({project: idAndName, revision: z.string(), sessionTime: z.string(), timezone: 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 }; /** * Get truncated local time based on the time zone of a workflow * * GET /api/workflows/{id}/truncated_session_time */ export const getApiApiWorkflowsIdTruncatedSessionTime = async (args: GetApiApiWorkflowsIdTruncatedSessionTimeArgs, options?: GetApiApiWorkflowsIdTruncatedSessionTimeOptions) => { 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 (args.session_time != null) query.append("session_time", String(args.session_time)) if (args.mode != null) query.append("mode", String(args.mode)) if (ApiKeyAuth !== undefined) headers.set('authorization', `TD1 ${ApiKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/workflows/${encodeURIComponent(String(args.id))}/truncated_session_time${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(restWorkflowSessionTime, await res.json()) };