import {z} from 'zod' export type ConnectProxyRequest = {app_slug: string, url: string, method?: string | undefined, params?: ({[key: string]: unknown} | null) | undefined, body?: ({[key: string]: unknown} | null) | undefined, headers?: ({[key: string]: string} | null) | undefined, account_id?: (string | null) | undefined, effect: 'read' | 'external', summary?: (string | null) | undefined, idempotency_key?: (string | null) | undefined, decide_by?: (string | null) | undefined}; export type CreateApiV1TeamsTeamIdConnectProxyArgs = {team_id: string, authorization?: (string | null) | undefined, 'x-darwin-token'?: (string | null) | undefined, 'x-darwin-run-id'?: (string | null) | undefined, body: ConnectProxyRequest}; export type CreateApiV1TeamsTeamIdConnectProxyOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const createApiV1TeamsTeamIdConnectProxyResponse = z.record(z.string(), z.unknown()); 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 }; /** * Proxy Connected App * * POST /v1/teams/{team_id}/connect/proxy */ export const createApiV1TeamsTeamIdConnectProxy = async (args: CreateApiV1TeamsTeamIdConnectProxyArgs, options: CreateApiV1TeamsTeamIdConnectProxyOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.authorization != null) headers.set("authorization", String(args.authorization)) if (args['x-darwin-token'] != null) headers.set("x-darwin-token", String(args['x-darwin-token'])) if (args['x-darwin-run-id'] != null) headers.set("x-darwin-run-id", String(args['x-darwin-run-id'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1/teams/${encodeURIComponent(String(args.team_id))}/connect/proxy${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(createApiV1TeamsTeamIdConnectProxyResponse, await res.json()) };