import {z} from 'zod' export type GetApiTokenConnectionsArgs = {'filter[connectionType]'?: ('inbound' | 'outbound') | undefined, 'filter[live]'?: boolean | undefined, 'filter[searchTerm]'?: string | undefined, 'filter[includeDeleted]'?: boolean | undefined}; export type GetApiTokenConnectionsOptions = {baseUrl?: string | undefined, BearerToken?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiTokenConnectionsResponse = z.object({paging: z.object({next: z.string().optional()}).optional(), results: z.array(z.object({name: z.string(), description: z.string().optional(), connectionType: z.enum(["inbound", "outbound"]), id: z.string().optional(), liveVersionId: z.string().nullable().optional(), workspaceId: z.string().optional(), updatedAt: z.string().optional(), createdAt: z.string().optional(), deletedAt: z.string().nullable().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 }; /** * Filter & list connections * * GET /token/connections */ export const getApiTokenConnections = async (args: GetApiTokenConnectionsArgs, options?: GetApiTokenConnectionsOptions) => { const {baseUrl = "https://api.staging.payrails.io", fetch: fetchFn = fetch, BearerToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['filter[connectionType]'] != null) query.append("filter[connectionType]", String(args['filter[connectionType]'])) if (args['filter[live]'] != null) query.append("filter[live]", String(args['filter[live]'])) if (args['filter[searchTerm]'] != null) query.append("filter[searchTerm]", String(args['filter[searchTerm]'])) if (args['filter[includeDeleted]'] != null) query.append("filter[includeDeleted]", String(args['filter[includeDeleted]'])) if (BearerToken !== undefined) headers.set('authorization', `Bearer ${BearerToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/token/connections${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(getApiTokenConnectionsResponse, await res.json()) };