diff --git a/scripts/mintlify-post-processing/typedoc-plugin/typedoc-mintlify-returns.js b/scripts/mintlify-post-processing/typedoc-plugin/typedoc-mintlify-returns.js index e82f0796..4f73aa8d 100644 --- a/scripts/mintlify-post-processing/typedoc-plugin/typedoc-mintlify-returns.js +++ b/scripts/mintlify-post-processing/typedoc-plugin/typedoc-mintlify-returns.js @@ -128,9 +128,10 @@ export function extractSignatureInfo( for (let i = 0; i < lines.length; i++) { const line = lines[i]; // Match function signature: > **methodName**(...): `returnType` or `returnType`\<`generic`\> + // Method-level generics appear between the bold name and arguments. // Handle both simple types and generic types like `Promise`\<`any`\> or `Promise`\<[`TypeName`](link)\> const sigMatch = line.match( - /^>\s*\*\*(\w+)\*\*\([^)]*\):\s*`([^`]+)`(?:\\<(.+?)\\>)?/ + /^>\s*\*\*(\w+)\*\*(?:\\<.*?\\>)?\([^)]*\):\s*`([^`]+)`(?:\\<(.+?)\\>)?/ ); if (sigMatch) { const methodName = sigMatch[1]; @@ -368,7 +369,9 @@ function rewriteReturnSections(content, options) { let sigLineIdx = i - 2; // Go back past the Returns heading while ( sigLineIdx >= 0 && - !lines[sigLineIdx].match(/^>\s*\*\*\w+\*\*\(/) + !lines[sigLineIdx].match( + /^>\s*\*\*\w+\*\*(?:\\<.*?\\>)?\(/ + ) ) { sigLineIdx--; } @@ -467,7 +470,9 @@ function rewriteReturnSections(content, options) { let sigLineIdx = i - 2; // Go back past the Returns heading while ( sigLineIdx >= 0 && - !lines[sigLineIdx].match(/^>\s*\*\*\w+\*\*\(/) + !lines[sigLineIdx].match( + /^>\s*\*\*\w+\*\*(?:\\<.*?\\>)?\(/ + ) ) { sigLineIdx--; } diff --git a/scripts/mintlify-post-processing/types-to-expose.json b/scripts/mintlify-post-processing/types-to-expose.json index b5f8c2c0..8411e2f6 100644 --- a/scripts/mintlify-post-processing/types-to-expose.json +++ b/scripts/mintlify-post-processing/types-to-expose.json @@ -7,6 +7,9 @@ "AnalyticsModule", "AppLogsModule", "AuthModule", + "ConnectorApiRequest", + "ConnectorApiResponse", + "ConnectorApiResponsePhase", "ConnectorIntegrationType", "ConnectorIntegrationTypeRegistry", "ConnectorsModule", diff --git a/src/index.ts b/src/index.ts index 0114462b..6886671b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -125,6 +125,9 @@ export { Actor, type Conn } from "./actor.js"; export type { ConnectorsModule, UserConnectorsModule, + ConnectorApiRequest, + ConnectorApiResponse, + ConnectorApiResponsePhase, } from "./modules/connectors.types.js"; export type { diff --git a/src/modules/connectors.ts b/src/modules/connectors.ts index 2fd902e1..d1f823c4 100644 --- a/src/modules/connectors.ts +++ b/src/modules/connectors.ts @@ -2,12 +2,24 @@ import { AxiosInstance } from "axios"; import { ConnectorIntegrationType, ConnectorAccessTokenResponse, + ConnectorApiRequest, + ConnectorApiResponse, ConnectorConnectionResponse, + ConnectorProxyRawResponse, AppUserConnectorConnectionResponse, ConnectorsModule, UserConnectorsModule, } from "./connectors.types.js"; +const CONNECTOR_API_METHODS = new Set([ + "GET", + "POST", + "PUT", + "PATCH", + "DELETE", + "HEAD", +]); + /** * Creates the Connectors module for the Base44 SDK. * @@ -112,6 +124,68 @@ export function createConnectorsModule( connectionConfig: data.connection_config ?? null, }; }, + + async callApi( + integrationType: ConnectorIntegrationType, + request: ConnectorApiRequest + ): Promise> { + assertNonEmptyString(integrationType, "Integration type"); + return proxyCall( + axios, + `/apps/${appId}/connectors/${integrationType}/call`, + request + ); + }, + }; +} + +function assertNonEmptyString(value: unknown, label: string): void { + if (!value || typeof value !== "string") { + throw new Error(`${label} is required and must be a string`); + } +} + +/** + * POST a request to the connector proxy and normalize the response. + * + * The proxy reports upstream outcomes in the body rather than as HTTP status, so + * a provider 4xx/5xx arrives here as a resolved response with `success: false` — + * only Base44-side failures reject through the axios error interceptor. + * + * @internal + */ +async function proxyCall( + axios: AxiosInstance, + url: string, + request: ConnectorApiRequest +): Promise> { + if (!request || typeof request !== "object") { + throw new Error("Request is required and must be an object"); + } + assertNonEmptyString(request.path, "Request path"); + const method = request.method ?? "GET"; + if (!CONNECTOR_API_METHODS.has(method)) { + throw new Error( + "Request method must be one of GET, POST, PUT, PATCH, DELETE, or HEAD" + ); + } + + const response = await axios.post(url, { + method, + path: request.path, + query: request.query ?? {}, + headers: request.headers ?? {}, + body: request.body ?? null, + }); + + const data = response as unknown as ConnectorProxyRawResponse; + return { + success: data.success, + phase: data.phase, + status: data.status_code ?? null, + data: data.data as T, + headers: data.headers ?? {}, + creditsCharged: data.credits_charged ?? 0, }; } diff --git a/src/modules/connectors.types.ts b/src/modules/connectors.types.ts index a67e6923..eb0a03d8 100644 --- a/src/modules/connectors.types.ts +++ b/src/modules/connectors.types.ts @@ -48,6 +48,71 @@ export interface AppUserConnectorConnectionResponse { connectionConfig: Record | null; } +/** + * How far a metered connector call progressed through the Base44 proxy. + * + * Only `not_sent` proves that the provider did not execute the request. + * `timed_out` and `sent_unconfirmed` may have executed upstream, so do not + * automatically retry non-idempotent requests based on those phases. + */ +export type ConnectorApiResponsePhase = + | "not_sent" + | "responded" + | "timed_out" + | "sent_unconfirmed"; + +/** + * A request to forward to a metered connector's API through the Base44 proxy. + */ +export interface ConnectorApiRequest { + /** HTTP method for the upstream request. Defaults to `'GET'`. */ + method?: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD"; + /** + * Path relative to the connector's API root, starting with `/`, such as `'/2/tweets'`. + * + * Must not be an absolute URL. Query parameters may be included here or passed + * separately as {@link query}; either way they are forwarded and priced identically. + */ + path: string; + /** Query parameters. Merged into the request URL alongside any already present in {@link path}. */ + query?: Record>; + /** Extra request headers. Only headers the connector explicitly allows are forwarded; the rest are dropped. */ + headers?: Record; + /** JSON request body. Ignored for `GET`, `HEAD`, and `DELETE`. */ + body?: unknown; +} + +/** + * The upstream API's response, as returned by the Base44 connector proxy. + */ +export interface ConnectorApiResponse { + /** `true` only when the upstream API returned a 2xx status. Proxy and upstream errors are `false`. */ + success: boolean; + /** How far the call progressed. Only `not_sent` proves the provider did not execute it. */ + phase: ConnectorApiResponsePhase; + /** The upstream HTTP status code, or `null` when no response was received. */ + status: number | null; + /** The parsed upstream response body, or proxy error details when no response was received. */ + data: T; + /** The subset of upstream response headers the connector exposes, typically rate-limit counters. */ + headers: Record; + /** Integration credits billed to the workspace for this call. */ + creditsCharged: number; +} + +/** + * Raw proxy response shape. Mapped to {@link ConnectorApiResponse} before being returned. + * @internal + */ +export interface ConnectorProxyRawResponse { + success: boolean; + phase: ConnectorApiResponsePhase; + status_code: number | null; + data: unknown; + headers: Record; + credits_charged: number; +} + /** * Connectors module for managing OAuth tokens for external services. * @@ -78,6 +143,18 @@ export interface AppUserConnectorConnectionResponse { * 3. In a backend function, call {@linkcode getCurrentAppUserConnection | getCurrentAppUserConnection()} using the service role client (`base44.asServiceRole.connectors`) with the connector ID to retrieve the app user's token. * 4. Use the returned `accessToken` to call the external service's API directly. Some connectors also return a `connectionConfig` with additional values such as a subdomain for building the API URL. * + * ## Metered connectors + * + * A few [platform connectors](#shared-connectors) are backed by paid third-party APIs that charge Base44 per call. For those, the OAuth token is **not** available to your code — {@linkcode getConnection | getConnection()} rejects with a `403`. Call them with {@linkcode callApi | callApi()} instead: Base44 attaches the credential server-side, forwards the request, and bills your workspace's integration credits for the call. + * + * This applies to platform connectors only. A workspace-registered or app user connector runs on **your own** OAuth app, so the provider invoices you directly and there is nothing for Base44 to meter — those keep normal token access via {@linkcode getWorkspaceConnection | getWorkspaceConnection()} and {@linkcode getCurrentAppUserConnection | getCurrentAppUserConnection()}. + * + * Two things to keep in mind when writing against a metered connector: + * + * - **Cost varies by endpoint, sometimes sharply.** The same connector can charge two orders of magnitude more for one endpoint than another, so avoid putting an expensive call inside a loop and batch wherever the provider supports it. Each response reports what it actually cost as `creditsCharged`. + * - **Provider and transport outcomes are returned, not thrown.** A provider `4xx`/`5xx` or a connection failure comes back as `success: false` with its `phase`; authorization, quota, and invalid proxy requests reject the promise. + * - **Only `phase: 'not_sent'` proves the provider did not execute the request.** A timeout or in-flight failure may have executed upstream, so do not automatically retry a non-idempotent call unless the provider supports an idempotency key. + * * ## Available connectors * * The connectors below can be used as shared connectors or as app user connectors. For a shared platform connector, pass the integration type string to {@linkcode getConnection | getConnection()}. For a connector you register in Workspace Settings with your own OAuth app, use the connector ID with {@linkcode getWorkspaceConnection | getWorkspaceConnection()} for a shared token, or with {@linkcode getCurrentAppUserConnection | getCurrentAppUserConnection()} for a per-user token. @@ -345,6 +422,45 @@ export interface ConnectorsModule { getCurrentAppUserConnection( connectorId: string, ): Promise; + + /** + * Calls a [metered connector's](#metered-connectors) API through the Base44 proxy. + * + * Use this for a shared platform connector identified by an integration type. Base44 adds the OAuth credential to the outgoing request, forwards it, and bills the workspace for the call, so you never handle the token yourself. + * + * @param integrationType - The type of integration, such as `'x'`. See [Available connectors](#available-connectors). + * @param request - The upstream request to forward. See {@link ConnectorApiRequest}. + * @returns Promise resolving to a {@link ConnectorApiResponse}. Note that an upstream error is reported in `success` and `status`, not thrown — only Base44-side failures reject. + * + * @example + * ```typescript + * // Post to X + * const res = await base44.asServiceRole.connectors.callApi('x', { + * method: 'POST', + * path: '/2/tweets', + * body: { text: 'Shipped!' }, + * }); + * + * if (!res.success) { + * console.error('X rejected the post', res.status, res.data); + * } + * ``` + * + * @example + * ```typescript + * // Read, with query parameters and a look at what the call cost + * const res = await base44.asServiceRole.connectors.callApi('x', { + * path: '/2/tweets/search/recent', + * query: { query: 'base44', max_results: 10 }, + * }); + * + * console.log(`${res.creditsCharged} credits`, res.data); + * ``` + */ + callApi( + integrationType: ConnectorIntegrationType, + request: ConnectorApiRequest, + ): Promise>; } /** diff --git a/src/utils/axios-client.ts b/src/utils/axios-client.ts index f432e9d4..98e1dcb6 100644 --- a/src/utils/axios-client.ts +++ b/src/utils/axios-client.ts @@ -246,7 +246,9 @@ export function createAxiosClient({ const base44Error = new Base44Error( message, error.response?.status, - error.response?.data?.code, + error.response?.data?.code ?? + error.response?.headers?.get?.("x-base44-connector-error") ?? + error.response?.headers?.["x-base44-connector-error"], error.response?.data, error ); diff --git a/tests/types/connectors.types.ts b/tests/types/connectors.types.ts new file mode 100644 index 00000000..446da229 --- /dev/null +++ b/tests/types/connectors.types.ts @@ -0,0 +1,31 @@ +import type { + ConnectorApiRequest, + ConnectorApiResponse, + ConnectorApiResponsePhase, +} from "../../src/index.js"; + +const phase = "sent_unconfirmed" satisfies ConnectorApiResponsePhase; + +const request = { + method: "POST", + path: "/2/tweets", +} satisfies ConnectorApiRequest; + +const response = { + success: false, + phase, + status: null, + data: { error: "request outcome unknown" }, + headers: {}, + creditsCharged: 3, +} satisfies ConnectorApiResponse; + +const rejectsLowercaseMethod = { + // @ts-expect-error Connector methods use the uppercase wire values. + method: "post", + path: "/2/tweets", +} satisfies ConnectorApiRequest; + +void request; +void response; +void rejectsLowercaseMethod; diff --git a/tests/unit/connectors-proxy.test.ts b/tests/unit/connectors-proxy.test.ts new file mode 100644 index 00000000..6d28e74c --- /dev/null +++ b/tests/unit/connectors-proxy.test.ts @@ -0,0 +1,206 @@ +import { describe, test, expect, beforeEach, afterEach } from "vitest"; +import nock from "nock"; +import { createClient } from "../../src/index.ts"; + +describe("Connectors module – metered connector proxy", () => { + const appId = "test-app-id"; + const serverUrl = "https://base44.app"; + const serviceToken = "service-token-123"; + let base44: ReturnType; + let scope: nock.Scope; + + beforeEach(() => { + base44 = createClient({ serverUrl, appId, serviceToken }); + scope = nock(serverUrl); + }); + + afterEach(() => { + nock.cleanAll(); + }); + + const proxyResponse = { + success: true, + phase: "responded", + status_code: 201, + data: { data: { id: "1" } }, + headers: { "x-rate-limit-remaining": "42" }, + credits_charged: 3, + }; + + test("posts the normalized request to the shared-connector proxy route", async () => { + let received: any; + scope + .post(`/api/apps/${appId}/connectors/x/call`, (body) => { + received = body; + return true; + }) + .reply(200, proxyResponse); + + await base44.asServiceRole.connectors.callApi("x", { + method: "POST", + path: "/2/tweets", + body: { text: "hi" }, + }); + + expect(received.method).toBe("POST"); + expect(received.path).toBe("/2/tweets"); + expect(received.body).toEqual({ text: "hi" }); + // Absent fields are sent as empties rather than omitted, so the server + // never has to distinguish "missing" from "empty". + expect(received.query).toEqual({}); + expect(received.headers).toEqual({}); + }); + + test("defaults the method to GET", async () => { + let received: any; + scope + .post(`/api/apps/${appId}/connectors/x/call`, (body) => { + received = body; + return true; + }) + .reply(200, proxyResponse); + + await base44.asServiceRole.connectors.callApi("x", { path: "/2/users/me" }); + + expect(received.method).toBe("GET"); + }); + + test("forwards query parameters so the priced call matches the sent call", async () => { + // The server prices the merged query; dropping it client-side would make the + // quoted price and the real request disagree. + let received: any; + scope + .post(`/api/apps/${appId}/connectors/x/call`, (body) => { + received = body; + return true; + }) + .reply(200, proxyResponse); + + await base44.asServiceRole.connectors.callApi("x", { + path: "/2/tweets/search/recent", + query: { query: "base44", max_results: 10 }, + }); + + expect(received.query).toEqual({ query: "base44", max_results: 10 }); + }); + + test("maps the proxy envelope to camelCase", async () => { + scope.post(`/api/apps/${appId}/connectors/x/call`).reply(200, proxyResponse); + + const res = await base44.asServiceRole.connectors.callApi("x", { + path: "/2/tweets", + }); + + expect(res.success).toBe(true); + expect(res.phase).toBe("responded"); + expect(res.status).toBe(201); + expect(res.data).toEqual({ data: { id: "1" } }); + expect(res.headers).toEqual({ "x-rate-limit-remaining": "42" }); + expect(res.creditsCharged).toBe(3); + }); + + test("returns an upstream error instead of throwing", async () => { + // A provider 4xx is a normal outcome of a call Base44 completed (and billed), + // so it must be inspectable rather than an exception. + scope.post(`/api/apps/${appId}/connectors/x/call`).reply(200, { + success: false, + phase: "responded", + status_code: 400, + data: { title: "Invalid Request" }, + headers: {}, + credits_charged: 3, + }); + + const res = await base44.asServiceRole.connectors.callApi("x", { + method: "POST", + path: "/2/tweets", + body: {}, + }); + + expect(res.success).toBe(false); + expect(res.phase).toBe("responded"); + expect(res.status).toBe(400); + expect(res.data).toEqual({ title: "Invalid Request" }); + // Still charged: the vendor counted the request. + expect(res.creditsCharged).toBe(3); + }); + + test("rejects when Base44 itself refuses the call", async () => { + // Credits exhausted is a Base44-side failure, not an upstream outcome. + scope.post(`/api/apps/${appId}/connectors/x/call`).reply(402, { + message: "You have reached the limit of integrations for this month", + extra_data: { reason: "integration_credits_limit_reached" }, + }); + + await expect( + base44.asServiceRole.connectors.callApi("x", { path: "/2/tweets" }) + ).rejects.toMatchObject({ status: 402 }); + }); + + test("a metered connector's token request surfaces the actionable refusal", async () => { + // The backend's 403 detail names the proxy, which is what lets generated + // code (and the model that wrote it) correct itself. + scope.get(`/api/apps/${appId}/external-auth/tokens/x`).reply( + 403, + { + detail: + "Connector 'x' is metered — raw access tokens are not available for it. " + + `Call POST /api/apps/${appId}/connectors/x/call instead.`, + }, + { "X-Base44-Connector-Error": "metered_connector_requires_proxy" } + ); + + await expect( + base44.asServiceRole.connectors.getConnection("x") + ).rejects.toMatchObject({ + status: 403, + code: "metered_connector_requires_proxy", + message: expect.stringContaining("/connectors/x/call"), + }); + }); + + test.each(["post", "TRACE"])( + "rejects unsupported request method %s before sending", + async (method) => { + await expect( + base44.asServiceRole.connectors.callApi("x", { + method: method as any, + path: "/2/tweets", + }) + ).rejects.toThrow( + "Request method must be one of GET, POST, PUT, PATCH, DELETE, or HEAD" + ); + } + ); + + test.each(["not_sent", "timed_out", "sent_unconfirmed"] as const)( + "maps proxy phase %s when no upstream response is available", + async (phase) => { + scope.post(`/api/apps/${appId}/connectors/x/call`).reply(200, { + success: false, + phase, + status_code: null, + data: { error: "request outcome unknown" }, + headers: {}, + credits_charged: phase === "not_sent" ? 0 : 3, + }); + + const res = await base44.asServiceRole.connectors.callApi("x", { + path: "/2/tweets", + }); + + expect(res.phase).toBe(phase); + expect(res.status).toBeNull(); + expect(res.success).toBe(false); + } + ); + + test.each([ + ["", "/2/tweets"], + ["x", ""], + ])("rejects a missing identifier or path (%s, %s)", async (type, path) => { + await expect( + base44.asServiceRole.connectors.callApi(type, { path }) + ).rejects.toThrow(/required and must be a string/); + }); +}); diff --git a/tests/unit/typedoc-returns.test.ts b/tests/unit/typedoc-returns.test.ts new file mode 100644 index 00000000..689945d1 --- /dev/null +++ b/tests/unit/typedoc-returns.test.ts @@ -0,0 +1,23 @@ +import { describe, expect, test } from "vitest"; + +import { extractSignatureInfo } from "../../scripts/mintlify-post-processing/typedoc-plugin/typedoc-mintlify-returns.js"; + +describe("TypeDoc return signature parsing", () => { + test("recognizes method-level generics and their linked response type", () => { + const signature = + "> **callApi**\\<`T`\\>(`integrationType`, `request`): `Promise`\\<[`ConnectorApiResponse`](../interfaces/ConnectorApiResponse)\\<`T`\\>\\>"; + + const { signatureMap, linkedTypeMap } = extractSignatureInfo( + [signature], + new Set(), + () => {}, + null + ); + + expect(signatureMap.get(0)).toBe("Promise"); + expect(linkedTypeMap.get(0)).toEqual({ + typeName: "ConnectorApiResponse", + typePath: "../interfaces/ConnectorApiResponse", + }); + }); +});