Skip to content
This repository has been archived by the owner on Sep 23, 2024. It is now read-only.

Commit

Permalink
fix: 🐛 remove runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
rique-dev committed May 24, 2021
1 parent fb62641 commit 31d80e0
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 90 deletions.
17 changes: 7 additions & 10 deletions build/app.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
declare const events: readonly ["load", "onboard", "signup", "bank_list", "bank_not_found", "bank_selected", "bank_offline", "synced", "complete", "back", "exit", "error"];
declare const events: readonly ["load", "onboard", "signup", "bank_list", "find_my_bank", "bank_selected", "bank_offline", "synced", "complete", "back", "exit", "error"];
type EventsConnectKeys = typeof events[number];
type RenderIframeParams = {
container?: HTMLElement;
Expand Down Expand Up @@ -26,18 +26,15 @@ export type GuiabolsoConnectParams = {
button: RenderButtonParams['config'];
}>;
data: {
cpf: string;
cpf?: string;
email: string;
phone: string;
};
environment?: Envs;
userTrackingId: string;
clientId: string;
callbackURL: string;
/**
* only when open a new window
*/
fallbackURL?: string;
fallbackURL: string;
/**
* DOM element to render iframe or button
*/
Expand All @@ -48,7 +45,7 @@ export type CallbackPayload<K extends EventsConnectKeys> = {
onboard(): void;
signup(): void;
bank_list(): void;
bank_not_found(): void;
find_my_bank(): void;
bank_selected(): void;
bank_offline(): void;
synced(): void;
Expand All @@ -57,13 +54,13 @@ export type CallbackPayload<K extends EventsConnectKeys> = {
}): void;
back(): void;
exit(params: {
reason: 'back_finished' | 'bank_not_found' | 'unknow' | 'user_cancel';
reason: 'back_finished' | 'bank_not_found' | 'unknow' | 'user_cancel' | 'missing_params';
}): void;
error(err: Error): void;
}[K];
export function guiabolsoConnect({ container, config, data, clientId, userTrackingId, callbackURL, fallbackURL, environment, }: GuiabolsoConnectParams): Promise<{
on: <T extends "error" | "exit" | "complete" | "load" | "onboard" | "signup" | "bank_list" | "bank_not_found" | "bank_selected" | "bank_offline" | "synced" | "back">(event: T, cb: CallbackPayload<T>) => void;
on: <T extends "error" | "exit" | "complete" | "load" | "onboard" | "signup" | "bank_list" | "find_my_bank" | "bank_selected" | "bank_offline" | "synced" | "back">(event: T, cb: CallbackPayload<T>) => void;
openNewWindow: () => void;
events: readonly ["load", "onboard", "signup", "bank_list", "bank_not_found", "bank_selected", "bank_offline", "synced", "complete", "back", "exit", "error"];
events: readonly ["load", "onboard", "signup", "bank_list", "find_my_bank", "bank_selected", "bank_offline", "synced", "complete", "back", "exit", "error"];
destroy: () => void;
}>;
2 changes: 1 addition & 1 deletion build/app.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"@babel/preset-env": "^7.12.11",
"@babel/preset-typescript": "^7.12.7",
"@types/jest": "^26.0.20",
"@types/node": "^14.17.0",
"@typescript-eslint/eslint-plugin": "^4.14.2",
"@typescript-eslint/parser": "^4.14.2",
"babel-jest": "^26.6.3",
Expand Down
164 changes: 86 additions & 78 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'regenerator-runtime/runtime';
import './utils/globalthis-polyfill';

import { buildQueryParams } from './utils/build-query-params';
Expand Down Expand Up @@ -62,14 +61,19 @@ export declare type CallbackPayload<K extends EventsConnectKeys> = {
complete(params: { oauthcode: string }): void;
back(): void;
exit(params: {
reason: 'back_finished' | 'bank_not_found' | 'unknow' | 'user_cancel';
reason:
| 'back_finished'
| 'bank_not_found'
| 'unknow'
| 'user_cancel'
| 'missing_params';
}): void;
error(err: Error): void;
}[K];

setInstanceValue('isDestroyed', false);

export async function guiabolsoConnect({
export function guiabolsoConnect({
container,
config,
data,
Expand All @@ -82,89 +86,93 @@ export async function guiabolsoConnect({
const env =
(process.env.GUIABOSLO_CONNECT_ENVIRONMENT as Envs) || environment;
const domain = getOrigin(env);
const embedded = (await verifyCSP()) && Boolean(container);

const {
iframe: iframeConfig,
button: buttonConfig,
window: windowConfig,
...configConnect
} = config ?? {};

const queryString = buildQueryParams({
...data,
...configConnect,
callbackURL,
clientId,
fallbackURL,
userTrackingId: environment === 'sandbox' ? 'development' : userTrackingId,
embedded,
});

const src =
process.env.TEST_EMITTER || `${domain}/#/integracao${queryString}`;
return verifyCSP().then((hasCSP) => {
const embedded = hasCSP && Boolean(container);

const {
iframe: iframeConfig,
button: buttonConfig,
window: windowConfig,
...configConnect
} = config ?? {};

const queryString = buildQueryParams({
...data,
...configConnect,
callbackURL,
clientId,
fallbackURL,
userTrackingId:
environment === 'sandbox' ? 'development' : userTrackingId,
embedded,
});

const src =
process.env.TEST_EMITTER || `${domain}/#/integracao${queryString}`;

const observable = observer({
domain,
onEmmiter: ({ eventName }) => {
if (['exit', 'complete'].includes(eventName)) {
if (getInstance('windowParent')) {
getInstance('windowParent')?.close();
const observable = observer({
domain,
onEmmiter: ({ eventName }) => {
if (['exit', 'complete'].includes(eventName)) {
if (getInstance('windowParent')) {
getInstance('windowParent')?.close();
}
}
},
});

const openNewWindow = () => {
if (
process.env.GUIABOSLO_CONNECT_ENVIRONMENT === 'sandbox' &&
getInstance('isDestroyed')
) {
throw new Error("You executed destroy function. Events don't work");
}
},
});

const openNewWindow = () => {
if (
process.env.GUIABOSLO_CONNECT_ENVIRONMENT === 'sandbox' &&
getInstance('isDestroyed')
) {
throw new Error("You executed destroy function. Events don't work");
}
setInstanceValue(
'windowParent',
window.open(
src,
'gbConnectWindow',
`width=${windowConfig?.width || 992},height=${
windowConfig?.height || 820
}`
) as Window & typeof globalThis
);
};

setInstanceValue(
'windowParent',
window.open(
if (embedded) {
renderIframe({
config: iframeConfig,
src,
'gbConnectWindow',
`width=${windowConfig?.width || 992},height=${
windowConfig?.height || 820
}`
) as Window & typeof globalThis
);
};
container,
});
}

if (embedded) {
renderIframe({
config: iframeConfig,
src,
container,
});
}

if (!embedded) {
renderButton({ config: buttonConfig, container, onClick: openNewWindow });
}

return {
on: observable.on as <T extends EventsConnectKeys>(
event: T,
cb: CallbackPayload<T>
) => void,
openNewWindow,
events,
destroy: () => {
setTimeout(() => {
destroyInstances();
globalThis.document?.querySelector(`[${dataButtonStyle}]`)?.remove();
globalThis.document?.querySelector(`[${dataStyleIframe}]`)?.remove();
container?.remove();
observable.destroy();
setInstanceValue('isDestroyed', true);
}, 2000);
},
};
if (!embedded) {
renderButton({ config: buttonConfig, container, onClick: openNewWindow });
}

return {
on: observable.on as <T extends EventsConnectKeys>(
event: T,
cb: CallbackPayload<T>
) => void,
openNewWindow,
events,
destroy: () => {
setTimeout(() => {
destroyInstances();
globalThis.document?.querySelector(`[${dataButtonStyle}]`)?.remove();
globalThis.document?.querySelector(`[${dataStyleIframe}]`)?.remove();
container?.remove();
observable.destroy();
setInstanceValue('isDestroyed', true);
}, 2000);
},
};
});
}

// @ts-ignore
Expand Down
8 changes: 8 additions & 0 deletions src/process.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
declare const process: {
env: {
NODE_ENV: string;
GUIABOSLO_CONNECT_ENVIRONMENT: string;
TEST_EMITTER: string;
ALLOW_CSP: string;
};
};
1 change: 1 addition & 0 deletions src/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ window.addEventListener('load', () => {
hiddenHeader: false,
},
data: {
cpf: '99999999999',
phone: '11999999999',
email: '[email protected]',
},
Expand Down
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
{
"compilerOptions": {
"typeRoots": ["@types", "node_modules/@types", "src/process.d.ts"],
"declaration": true,
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"module": "esnext",
"moduleResolution": "node",
"allowJs": true
}
}

0 comments on commit 31d80e0

Please sign in to comment.