Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/modules/connectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AxiosInstance } from "axios";
import {
ConnectorIntegrationType,
ConnectorAccessTokenResponse,
ConnectorConnectionResponse,
ConnectorsModule,
} from "./connectors.types.js";

Expand Down Expand Up @@ -34,5 +35,23 @@ export function createConnectorsModule(
// @ts-expect-error
return response.access_token;
},

async getConnection(
integrationType: ConnectorIntegrationType
): Promise<ConnectorConnectionResponse> {
if (!integrationType || typeof integrationType !== "string") {
throw new Error("Integration type is required and must be a string");
}

const response = await axios.get<ConnectorAccessTokenResponse>(
`/apps/${appId}/external-auth/tokens/${integrationType}`
);

const data = response as unknown as ConnectorAccessTokenResponse;
return {
accessToken: data.access_token,
connectionConfig: data.connection_config ?? null,
};
},
};
}
44 changes: 44 additions & 0 deletions src/modules/connectors.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ export type ConnectorIntegrationType = keyof ConnectorIntegrationTypeRegistry ex
*/
export interface ConnectorAccessTokenResponse {
access_token: string;
integration_type: string;
connection_config: Record<string, string> | null;
}

/**
* Camel-cased connection details returned by {@linkcode ConnectorsModule.getConnection | getConnection()}.
*/
export interface ConnectorConnectionResponse {
/** The OAuth access token for the external service. */
accessToken: string;
/** Key-value configuration for the connection, or `null` if the connector does not provide one. */
connectionConfig: Record<string, string> | null;
}

/**
Expand Down Expand Up @@ -87,4 +99,36 @@ export interface ConnectorsModule {
* ```
*/
getAccessToken(integrationType: ConnectorIntegrationType): Promise<string>;

/**
* Retrieves the OAuth access token and connection configuration for a specific external integration type.
*
* Returns both the OAuth token and any additional connection configuration
* that the connector provides. This is useful when the external service requires
* extra parameters beyond the access token (e.g., a shop domain, account ID, or API base URL).
*
* @param integrationType - The type of integration, such as `'googlecalendar'`, `'slack'`, or `'github'`.
* @returns Promise resolving to a {@link ConnectorConnectionResponse} with `accessToken` and `connectionConfig`.
*
* @example
* ```typescript
* // Basic usage
* const connection = await base44.asServiceRole.connectors.getConnection('googlecalendar');
* console.log(connection.accessToken);
* console.log(connection.connectionConfig);
* ```
*
* @example
* ```typescript
* // Using connection config for a service that requires extra parameters
* const connection = await base44.asServiceRole.connectors.getConnection('shopify');
* const { accessToken, connectionConfig } = connection;
*
* const response = await fetch(
* `https://${connectionConfig?.shop}/admin/api/2024-01/products.json`,
* { headers: { 'X-Shopify-Access-Token': accessToken } }
* );
* ```
*/
getConnection(integrationType: ConnectorIntegrationType): Promise<ConnectorConnectionResponse>;
}