@integration-app/sdk
Advanced tools
Comparing version 0.0.14 to 0.0.15
export declare function init(options: InitOptions): void; | ||
export declare function startImport(options: NewImportOptions): void; | ||
export declare function findIntegratedApps(options?: InitOptions): Promise<any>; | ||
export declare function findConnections(options?: InitOptions): Promise<any>; | ||
export declare function openNewConnection(options: NewConnectionOptions): void; | ||
export declare function openNewFlow(options: NewFlowOptions): void; | ||
@@ -22,2 +24,8 @@ interface InitOptions { | ||
} | ||
interface NewConnectionOptions extends InitOptions { | ||
integratedAppKey: string; | ||
onSuccess?: (connection: any) => void; | ||
onCancel?: () => void; | ||
onFailure?: (error: any) => void; | ||
} | ||
export {}; |
@@ -15,3 +15,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.openNewFlow = exports.findIntegratedApps = exports.startImport = exports.init = void 0; | ||
exports.openNewFlow = exports.openNewConnection = exports.findConnections = exports.findIntegratedApps = exports.startImport = exports.init = void 0; | ||
const axios_1 = __importDefault(require("axios")); | ||
@@ -49,2 +49,40 @@ const iframe_1 = require("./iframe"); | ||
exports.findIntegratedApps = findIntegratedApps; | ||
function findConnections(options = {}) { | ||
return get('connections', options); | ||
} | ||
exports.findConnections = findConnections; | ||
function openNewConnection(options) { | ||
const requestId = (Math.random() + 1).toString(36).substring(12); | ||
const listenerFunc = (event) => __awaiter(this, void 0, void 0, function* () { | ||
const message = event.data || {}; | ||
if (message.source == 'integration.app' && message.requestId == requestId) { | ||
if (message.type == 'newConnectionCreated') { | ||
options.onSuccess && options.onSuccess(message.connection); | ||
} | ||
else if (message.type == 'newConnectionCancel') { | ||
options.onCancel && options.onCancel(); | ||
} | ||
else if (message.type == 'newConnectionFailure') { | ||
options.onFailure && options.onFailure(message.error); | ||
} | ||
cleanup(); | ||
} | ||
}); | ||
window.addEventListener('message', listenerFunc); | ||
const url = new URL(`oauth/new/${options.integratedAppKey}`, getApiUri(options)); | ||
url.searchParams.append('token', getUserAccessToken(options)); | ||
url.searchParams.append('requestId', requestId); | ||
const popup = window.open(url.toString()); | ||
const cancelCheckInterval = setInterval(() => { | ||
if (popup === null || popup === void 0 ? void 0 : popup.closed) { | ||
options.onCancel && options.onCancel(); | ||
cleanup(); | ||
} | ||
}, 1000); | ||
function cleanup() { | ||
clearInterval(cancelCheckInterval); | ||
window.removeEventListener('message', listenerFunc); | ||
} | ||
} | ||
exports.openNewConnection = openNewConnection; | ||
function openNewFlow(options) { | ||
@@ -108,3 +146,3 @@ const uri = getEmbedUri('flows/new', options, { | ||
params.method = method; | ||
params.baseURL = options.apiUri || apiUri || DEFAULT_API_URI; | ||
params.baseURL = getApiUri(options); | ||
params.headers = Object.assign(Object.assign({}, (params.headers || {})), { Authorization: `Bearer ${options.userAccessToken}` }); | ||
@@ -115,1 +153,11 @@ const response = yield axios_1.default.request(params); | ||
} | ||
function getApiUri(options = {}) { | ||
return options.apiUri || apiUri || DEFAULT_API_URI; | ||
} | ||
function getUserAccessToken(options = {}) { | ||
const token = options.userAccessToken || userAccessToken; | ||
if (!token) { | ||
throw new Error('userAccessToken is not provided'); | ||
} | ||
return token; | ||
} |
{ | ||
"name": "@integration-app/sdk", | ||
"version": "0.0.14", | ||
"version": "0.0.15", | ||
"description": "JavaScript SDK for Integration.app", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -37,2 +37,46 @@ import Axios, { AxiosRequestConfig } from 'axios' | ||
export function findConnections(options: InitOptions = {}) { | ||
return get('connections', options) | ||
} | ||
export function openNewConnection(options: NewConnectionOptions) { | ||
const requestId = (Math.random() + 1).toString(36).substring(12) | ||
const listenerFunc = async (event: any) => { | ||
const message = event.data || {} | ||
if (message.source == 'integration.app' && message.requestId == requestId) { | ||
if (message.type == 'newConnectionCreated') { | ||
options.onSuccess && options.onSuccess(message.connection) | ||
} else if (message.type == 'newConnectionCancel') { | ||
options.onCancel && options.onCancel() | ||
} else if (message.type == 'newConnectionFailure') { | ||
options.onFailure && options.onFailure(message.error) | ||
} | ||
cleanup() | ||
} | ||
} | ||
window.addEventListener('message', listenerFunc) | ||
const url = new URL( | ||
`oauth/new/${options.integratedAppKey}`, | ||
getApiUri(options), | ||
) | ||
url.searchParams.append('token', getUserAccessToken(options)) | ||
url.searchParams.append('requestId', requestId) | ||
const popup = window.open(url.toString()) | ||
const cancelCheckInterval = setInterval(() => { | ||
if (popup?.closed) { | ||
options.onCancel && options.onCancel() | ||
cleanup() | ||
} | ||
}, 1000) | ||
function cleanup() { | ||
clearInterval(cancelCheckInterval) | ||
window.removeEventListener('message', listenerFunc) | ||
} | ||
} | ||
export function openNewFlow(options: NewFlowOptions) { | ||
@@ -101,3 +145,3 @@ const uri = getEmbedUri('flows/new', options, { | ||
params.method = method | ||
params.baseURL = options.apiUri || apiUri || DEFAULT_API_URI | ||
params.baseURL = getApiUri(options) | ||
params.headers = { | ||
@@ -111,2 +155,14 @@ ...(params.headers || {}), | ||
function getApiUri(options: InitOptions = {}) { | ||
return options.apiUri || apiUri || DEFAULT_API_URI | ||
} | ||
function getUserAccessToken(options: InitOptions = {}) { | ||
const token = options.userAccessToken || userAccessToken | ||
if (!token) { | ||
throw new Error('userAccessToken is not provided') | ||
} | ||
return token | ||
} | ||
interface InitOptions { | ||
@@ -131,1 +187,8 @@ apiUri?: string | ||
} | ||
interface NewConnectionOptions extends InitOptions { | ||
integratedAppKey: string | ||
onSuccess?: (connection: any) => void | ||
onCancel?: () => void | ||
onFailure?: (error: any) => void | ||
} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
331250
3228