import {z} from 'zod' export type RemoteTaskSecretMaterialBody = {type: 'token', value: string} | {password: string, type: 'basic', username: string}; export type CreateRemoteTaskAuthBody = {type: 'none'} | {secret: RemoteTaskSecretMaterialBody, type: 'bearer'} | {secret: RemoteTaskSecretMaterialBody, type: 'basic'} | {header_name: string, secret: RemoteTaskSecretMaterialBody, type: 'api_key_header'}; export type CreateRemoteTaskHeaderBody = {key: string, secret?: (RemoteTaskSecretMaterialBody | null) | undefined, value?: (string | null) | undefined}; export type CreateRemoteTaskSigningBody = {enabled: boolean, keyId?: (string | null) | undefined, secret?: (RemoteTaskSecretMaterialBody | null) | undefined}; export type CreateApiApiOrgIdTasksTestArgs = {org_id: string, body: {auth?: (CreateRemoteTaskAuthBody | null) | undefined, contentType?: (string | null) | undefined, customHeaders?: (Array | null) | undefined, description?: (string | null) | undefined, endpoint: string, httpMethod?: (string | null) | undefined, maxAttempts?: (number | null) | undefined, maxConcurrency?: (number | null) | undefined, name: string, requestTemplate?: (string | null) | undefined, responseSchema?: (string | null) | undefined, signing?: (CreateRemoteTaskSigningBody | null) | undefined, timeoutMs?: (number | null) | undefined, input: unknown, metadata: unknown}}; export type CreateApiApiOrgIdTasksTestOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const verificationReportBody = z.object({latencyMs: z.number().int(), parsedOutput: z.unknown(), rawRequest: z.string(), rawResponse: z.string(), statusCode: z.number().int().gte(0).nullable().optional()}); export const createApiApiOrgIdTasksTestResponse = z.object({error: z.string().nullable().optional(), report: verificationReportBody, verified: z.boolean()}); 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 }; /** * Test an inline remote task * * POST /api/{org_id}/tasks/test */ export const createApiApiOrgIdTasksTest = async (args: CreateApiApiOrgIdTasksTestArgs, options: CreateApiApiOrgIdTasksTestOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/tasks/test${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(createApiApiOrgIdTasksTestResponse, await res.json()) };