99 lines
4.4 KiB
JavaScript
99 lines
4.4 KiB
JavaScript
/**
|
|
* Creates the Connectors module for the Base44 SDK.
|
|
*
|
|
* @param axios - Axios instance (should be service role client)
|
|
* @param appId - Application ID
|
|
* @returns Connectors module with methods to retrieve OAuth tokens
|
|
* @internal
|
|
*/
|
|
export function createConnectorsModule(axios, appId) {
|
|
return {
|
|
/**
|
|
* Retrieve an OAuth access token for a specific external integration type.
|
|
* @deprecated Use getConnection(integrationType) and use the returned accessToken (and connectionConfig when needed) instead.
|
|
*/
|
|
// @ts-expect-error Return type mismatch with interface - implementation returns string, interface expects string but implementation is typed as ConnectorAccessTokenResponse
|
|
async getAccessToken(integrationType) {
|
|
if (!integrationType || typeof integrationType !== "string") {
|
|
throw new Error("Integration type is required and must be a string");
|
|
}
|
|
const response = await axios.get(`/apps/${appId}/external-auth/tokens/${integrationType}`);
|
|
// @ts-expect-error
|
|
return response.access_token;
|
|
},
|
|
async getConnection(integrationType) {
|
|
var _a;
|
|
if (!integrationType || typeof integrationType !== "string") {
|
|
throw new Error("Integration type is required and must be a string");
|
|
}
|
|
const response = await axios.get(`/apps/${appId}/external-auth/tokens/${integrationType}`);
|
|
const data = response;
|
|
return {
|
|
accessToken: data.access_token,
|
|
connectionConfig: (_a = data.connection_config) !== null && _a !== void 0 ? _a : null,
|
|
};
|
|
},
|
|
async getWorkspaceConnection(connectorId) {
|
|
var _a;
|
|
if (!connectorId || typeof connectorId !== "string") {
|
|
throw new Error("Connector ID is required and must be a string");
|
|
}
|
|
const response = await axios.get(`/apps/${appId}/external-auth/tokens/connectors/${connectorId}`);
|
|
const data = response;
|
|
return {
|
|
accessToken: data.access_token,
|
|
connectionConfig: (_a = data.connection_config) !== null && _a !== void 0 ? _a : null,
|
|
};
|
|
},
|
|
/**
|
|
* @deprecated Use getCurrentAppUserConnection(connectorId) and use the returned accessToken (and connectionConfig when needed) instead.
|
|
*/
|
|
async getCurrentAppUserAccessToken(connectorId) {
|
|
if (!connectorId || typeof connectorId !== "string") {
|
|
throw new Error("Connector ID is required and must be a string");
|
|
}
|
|
const response = await axios.get(`/apps/${appId}/app-user-auth/connectors/${connectorId}/token`);
|
|
const data = response;
|
|
return data.access_token;
|
|
},
|
|
async getCurrentAppUserConnection(connectorId) {
|
|
var _a;
|
|
if (!connectorId || typeof connectorId !== "string") {
|
|
throw new Error("Connector ID is required and must be a string");
|
|
}
|
|
const response = await axios.get(`/apps/${appId}/app-user-auth/connectors/${connectorId}/token`);
|
|
const data = response;
|
|
return {
|
|
accessToken: data.access_token,
|
|
connectionConfig: (_a = data.connection_config) !== null && _a !== void 0 ? _a : null,
|
|
};
|
|
},
|
|
};
|
|
}
|
|
/**
|
|
* Creates the user-scoped Connectors module (app-user OAuth flows).
|
|
*
|
|
* @param axios - Axios instance (user-scoped client)
|
|
* @param appId - Application ID
|
|
* @returns User connectors module with app-user OAuth methods
|
|
* @internal
|
|
*/
|
|
export function createUserConnectorsModule(axios, appId) {
|
|
return {
|
|
async connectAppUser(connectorId) {
|
|
if (!connectorId || typeof connectorId !== "string") {
|
|
throw new Error("Connector ID is required and must be a string");
|
|
}
|
|
const response = await axios.post(`/apps/${appId}/app-user-auth/connectors/${connectorId}/initiate`);
|
|
const data = response;
|
|
return data.redirect_url;
|
|
},
|
|
async disconnectAppUser(connectorId) {
|
|
if (!connectorId || typeof connectorId !== "string") {
|
|
throw new Error("Connector ID is required and must be a string");
|
|
}
|
|
await axios.delete(`/apps/${appId}/app-user-auth/connectors/${connectorId}`);
|
|
},
|
|
};
|
|
}
|