import {z} from 'zod' export type SessionsFilter = {type?: Array | undefined, tags?: Array | undefined, taskIdentifier?: Array | undefined, externalId?: string | undefined, status?: (Array<'ACTIVE' | 'CLOSED' | 'EXPIRED'>) | undefined, createdAt?: ({from?: string | undefined, to?: string | undefined, period?: string | undefined}) | undefined}; export type GetApiApiV1SessionsArgs = {page?: ({size?: number | undefined, after?: string | undefined, before?: string | undefined}) | undefined, filter?: SessionsFilter | undefined}; export type GetApiApiV1SessionsOptions = {baseUrl?: string | undefined, secretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const sessionTriggerConfig = z.object({basePayload: z.record(z.string(), z.unknown()), machine: z.string().optional(), queue: z.string().max(128).optional(), tags: z.array(z.string().max(128)).optional(), maxAttempts: z.number().int().gte(1).lte(10).optional(), maxDuration: z.number().int().gte(1).optional(), lockToVersion: z.string().optional(), region: z.string().optional(), idleTimeoutInSeconds: z.number().int().gte(1).lte(3600).optional()}); export const sessionObject = z.object({id: z.string(), externalId: z.string().nullable().optional(), type: z.string(), taskIdentifier: z.string(), triggerConfig: sessionTriggerConfig.optional(), currentRunId: z.string().nullable().optional(), tags: z.array(z.string()), metadata: z.record(z.string(), z.unknown()).nullable().optional(), closedAt: z.string().nullable().optional(), closedReason: z.string().nullable().optional(), expiresAt: z.string().nullable().optional(), createdAt: z.string(), updatedAt: z.string()}); export const listSessionsResult = z.object({data: z.array(sessionObject).optional(), pagination: z.object({next: z.string().optional(), previous: z.string().optional()}).optional()}); 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 }; /** * List sessions * * GET /api/v1/sessions * * Also accepts auth: publicAccessToken */ export const getApiApiV1Sessions = async (args: GetApiApiV1SessionsArgs, options?: GetApiApiV1SessionsOptions) => { const {baseUrl = "https://api.trigger.dev", fetch: fetchFn = fetch, secretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", JSON.stringify(args.page)) if (args.filter != null) query.append("filter", JSON.stringify(args.filter)) if (secretKey !== undefined) headers.set('authorization', `Bearer ${secretKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/sessions${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(listSessionsResult, await res.json()) };