@integration-app/sdk
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -1,11 +0,23 @@ | ||
interface IntegrationEmbedOptions { | ||
accessToken: string; | ||
export declare function init(options: InitOptions): void; | ||
export declare function getFlowsUri(options: FlowsUriOptions): string; | ||
export declare function getNewFlowUri(options: NewFlowUriOptions): string; | ||
export declare function setImportDataCallback(callback: DataImportCallbackFunction): void; | ||
interface InitOptions { | ||
apiUri: string; | ||
uiUri: string; | ||
userAccessToken: string; | ||
} | ||
interface OptionalInitOptions { | ||
apiUri?: string; | ||
uiUri?: string; | ||
userAccessToken?: string; | ||
} | ||
export declare class IntegrationEmbed { | ||
private options; | ||
constructor(options: IntegrationEmbedOptions); | ||
generateUrl(blueprintId: string, flowConfig?: any): string; | ||
interface FlowsUriOptions extends OptionalInitOptions { | ||
blueprintSlug: string; | ||
} | ||
interface NewFlowUriOptions extends OptionalInitOptions { | ||
blueprintSlug: string; | ||
flowConfig?: any; | ||
} | ||
declare type DataImportCallbackFunction = (data: any[]) => void; | ||
export {}; |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.IntegrationEmbed = void 0; | ||
class IntegrationEmbed { | ||
constructor(options) { | ||
this.options = options; | ||
exports.setImportDataCallback = exports.getNewFlowUri = exports.getFlowsUri = exports.init = void 0; | ||
function init(options) { | ||
apiUri = options.apiUri; | ||
uiUri = options.uiUri; | ||
userAccessToken = options.userAccessToken; | ||
} | ||
exports.init = init; | ||
function getFlowsUri(options) { | ||
return getEmbedUri('flows', options, { | ||
blueprintSlug: options.blueprintSlug, | ||
}); | ||
} | ||
exports.getFlowsUri = getFlowsUri; | ||
function getNewFlowUri(options) { | ||
return getEmbedUri('flows/new', options, { | ||
blueprintSlug: options.blueprintSlug, | ||
flowConfig: options.flowConfig | ||
? JSON.stringify(options.flowConfig) | ||
: undefined, | ||
}); | ||
} | ||
exports.getNewFlowUri = getNewFlowUri; | ||
function setImportDataCallback(callback) { | ||
if (dataImportCallback) { | ||
window.removeEventListener('message', dataImportCallback); | ||
} | ||
generateUrl(blueprintId, flowConfig) { | ||
const embedUrl = new URL(`${this.options.uiUri}/embed`); | ||
embedUrl.searchParams.set('token', this.options.accessToken); | ||
embedUrl.searchParams.set('blueprintId', blueprintId); | ||
if (flowConfig) { | ||
embedUrl.searchParams.set('flowConfig', flowConfig); | ||
dataImportCallback = (event) => __awaiter(this, void 0, void 0, function* () { | ||
const message = event.data || {}; | ||
if (message.source == 'integration.app' && | ||
message.type == 'data-imported') { | ||
callback(message.data); | ||
} | ||
return embedUrl.href; | ||
}); | ||
window.addEventListener('message', dataImportCallback); | ||
} | ||
exports.setImportDataCallback = setImportDataCallback; | ||
function getEmbedUri(page, options, params) { | ||
const baseUri = options.uiUri || uiUri; | ||
const token = options.userAccessToken || userAccessToken; | ||
if (!baseUri) { | ||
throw new Error('uiUri should be provided either in options or via sdk.init()'); | ||
} | ||
if (!token) { | ||
throw new Error('userAccessToken should be provided either in options or via sdk.init()'); | ||
} | ||
const embedUrl = new URL(`${baseUri}/embed/${page}`); | ||
embedUrl.searchParams.set('token', token); | ||
for (const [key, value] of Object.entries(params)) { | ||
embedUrl.searchParams.set(key, value); | ||
} | ||
return embedUrl.href; | ||
} | ||
exports.IntegrationEmbed = IntegrationEmbed; | ||
// Global values set in .init | ||
let apiUri; | ||
let uiUri; | ||
let userAccessToken; | ||
// We can have only one data listener per process / window | ||
// for simplicity | ||
let dataImportCallback; |
{ | ||
"name": "@integration-app/sdk", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "JavaScript SDK for Integration.app", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -1,19 +0,89 @@ | ||
interface IntegrationEmbedOptions { | ||
accessToken: string | ||
apiUri?: string | ||
uiUri?: string | ||
export function init(options: InitOptions) { | ||
apiUri = options.apiUri | ||
uiUri = options.uiUri | ||
userAccessToken = options.userAccessToken | ||
} | ||
export class IntegrationEmbed { | ||
constructor(private options: IntegrationEmbedOptions) {} | ||
export function getFlowsUri(options: FlowsUriOptions) { | ||
return getEmbedUri('flows', options, { | ||
blueprintSlug: options.blueprintSlug, | ||
}) | ||
} | ||
public generateUrl(blueprintId: string, flowConfig?: any) { | ||
const embedUrl = new URL(`${this.options.uiUri}/embed`) | ||
embedUrl.searchParams.set('token', this.options.accessToken) | ||
embedUrl.searchParams.set('blueprintId', blueprintId) | ||
if (flowConfig) { | ||
embedUrl.searchParams.set('flowConfig', flowConfig) | ||
export function getNewFlowUri(options: NewFlowUriOptions) { | ||
return getEmbedUri('flows/new', options, { | ||
blueprintSlug: options.blueprintSlug, | ||
flowConfig: options.flowConfig | ||
? JSON.stringify(options.flowConfig) | ||
: undefined, | ||
}) | ||
} | ||
export function setImportDataCallback(callback: DataImportCallbackFunction) { | ||
if (dataImportCallback) { | ||
window.removeEventListener('message', dataImportCallback) | ||
} | ||
dataImportCallback = async (event: any) => { | ||
const message = event.data || {} | ||
if ( | ||
message.source == 'integration.app' && | ||
message.type == 'data-imported' | ||
) { | ||
callback(message.data) | ||
} | ||
return embedUrl.href | ||
} | ||
window.addEventListener('message', dataImportCallback) | ||
} | ||
function getEmbedUri(page: string, options: OptionalInitOptions, params?: any) { | ||
const baseUri = options.uiUri || uiUri | ||
const token = options.userAccessToken || userAccessToken | ||
if (!baseUri) { | ||
throw new Error( | ||
'uiUri should be provided either in options or via sdk.init()', | ||
) | ||
} | ||
if (!token) { | ||
throw new Error( | ||
'userAccessToken should be provided either in options or via sdk.init()', | ||
) | ||
} | ||
const embedUrl = new URL(`${baseUri}/embed/${page}`) | ||
embedUrl.searchParams.set('token', token) | ||
for (const [key, value] of Object.entries(params)) { | ||
embedUrl.searchParams.set(key, value as string) | ||
} | ||
return embedUrl.href | ||
} | ||
interface InitOptions { | ||
apiUri: string | ||
uiUri: string | ||
userAccessToken: string | ||
} | ||
interface OptionalInitOptions { | ||
apiUri?: string | ||
uiUri?: string | ||
userAccessToken?: string | ||
} | ||
interface FlowsUriOptions extends OptionalInitOptions { | ||
blueprintSlug: string | ||
} | ||
interface NewFlowUriOptions extends OptionalInitOptions { | ||
blueprintSlug: string | ||
flowConfig?: any | ||
} | ||
type DataImportCallbackFunction = (data: any[]) => void | ||
// Global values set in .init | ||
let apiUri: string | ||
let uiUri: string | ||
let userAccessToken: string | ||
// We can have only one data listener per process / window | ||
// for simplicity | ||
let dataImportCallback: (event: any) => void |
{ | ||
"compilerOptions": { | ||
"target": "es6", | ||
"lib": [ | ||
"ES2017", | ||
"DOM" | ||
], | ||
"module": "commonjs", | ||
@@ -5,0 +9,0 @@ "declaration": true, |
6492
191