Skip to content

Commit 721e301

Browse files
authored
Merge 85565c5 into cea3a43
2 parents cea3a43 + 85565c5 commit 721e301

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/modules/connectors.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { AxiosInstance } from "axios";
22
import {
33
ConnectorIntegrationType,
44
ConnectorAccessTokenResponse,
5+
ConnectorConnectionResponse,
56
ConnectorsModule,
67
} from "./connectors.types.js";
78

@@ -34,5 +35,23 @@ export function createConnectorsModule(
3435
// @ts-expect-error
3536
return response.access_token;
3637
},
38+
39+
async getConnection(
40+
integrationType: ConnectorIntegrationType
41+
): Promise<ConnectorConnectionResponse> {
42+
if (!integrationType || typeof integrationType !== "string") {
43+
throw new Error("Integration type is required and must be a string");
44+
}
45+
46+
const response = await axios.get<ConnectorAccessTokenResponse>(
47+
`/apps/${appId}/external-auth/tokens/${integrationType}`
48+
);
49+
50+
const data = response as unknown as ConnectorAccessTokenResponse;
51+
return {
52+
accessToken: data.access_token,
53+
connectionConfig: data.connection_config ?? null,
54+
};
55+
},
3756
};
3857
}

src/modules/connectors.types.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ export type ConnectorIntegrationType = keyof ConnectorIntegrationTypeRegistry ex
2222
*/
2323
export interface ConnectorAccessTokenResponse {
2424
access_token: string;
25+
integration_type: string;
26+
connection_config: Record<string, string> | null;
27+
}
28+
29+
/**
30+
* Camel-cased connection details returned by {@linkcode ConnectorsModule.getConnection | getConnection()}.
31+
*/
32+
export interface ConnectorConnectionResponse {
33+
/** The OAuth access token for the external service. */
34+
accessToken: string;
35+
/** Key-value configuration for the connection, or `null` if the connector does not provide one. */
36+
connectionConfig: Record<string, string> | null;
2537
}
2638

2739
/**
@@ -87,4 +99,36 @@ export interface ConnectorsModule {
8799
* ```
88100
*/
89101
getAccessToken(integrationType: ConnectorIntegrationType): Promise<string>;
102+
103+
/**
104+
* Retrieves the OAuth access token and connection configuration for a specific external integration type.
105+
*
106+
* Returns both the OAuth token and any additional connection configuration
107+
* that the connector provides. This is useful when the external service requires
108+
* extra parameters beyond the access token (e.g., a shop domain, account ID, or API base URL).
109+
*
110+
* @param integrationType - The type of integration, such as `'googlecalendar'`, `'slack'`, or `'github'`.
111+
* @returns Promise resolving to a {@link ConnectorConnectionResponse} with `accessToken` and `connectionConfig`.
112+
*
113+
* @example
114+
* ```typescript
115+
* // Basic usage
116+
* const connection = await base44.asServiceRole.connectors.getConnection('googlecalendar');
117+
* console.log(connection.accessToken);
118+
* console.log(connection.connectionConfig);
119+
* ```
120+
*
121+
* @example
122+
* ```typescript
123+
* // Using connection config for a service that requires extra parameters
124+
* const connection = await base44.asServiceRole.connectors.getConnection('shopify');
125+
* const { accessToken, connectionConfig } = connection;
126+
*
127+
* const response = await fetch(
128+
* `https://${connectionConfig?.shop}/admin/api/2024-01/products.json`,
129+
* { headers: { 'X-Shopify-Access-Token': accessToken } }
130+
* );
131+
* ```
132+
*/
133+
getConnection(integrationType: ConnectorIntegrationType): Promise<ConnectorConnectionResponse>;
90134
}

0 commit comments

Comments
 (0)