@getflip/bridge
Advanced tools
Comparing version 0.1.0-alpha.4 to 0.1.0-alpha.5
@@ -44,13 +44,19 @@ 'use strict'; | ||
const handler = (event) => { | ||
if (!isAllowedOrigin(event.origin)) ; | ||
if (event.data.id === request.id) { | ||
log("handleResponse", event.data); | ||
window.removeEventListener("message", handler); | ||
if (event.data.error) { | ||
reject(event.data.error); | ||
} | ||
else { | ||
resolve(event.data.result); | ||
} | ||
if (!isResponse(event.data) || event.data.id !== request.id) { | ||
return; | ||
} | ||
if (!isAllowedOrigin(event.origin)) { | ||
reject({ | ||
code: exports.BridgeErrorCode.FORBIDDEN_ORIGIN, | ||
}); | ||
return; | ||
} | ||
log("handleResponse", event.data); | ||
window.removeEventListener("message", handler); | ||
if (event.data.error) { | ||
reject(event.data.error); | ||
} | ||
else { | ||
resolve(event.data.result); | ||
} | ||
}; | ||
@@ -61,6 +67,9 @@ window.addEventListener("message", handler); | ||
} | ||
function isResponse(message) { | ||
return "id" in message && ("result" in message || "error" in message); | ||
} | ||
function isAllowedOrigin(origin) { | ||
// TODO: check origin | ||
console.log(origin); | ||
return true; | ||
var _a; | ||
const hostAppOrigin = (_a = window.flipBridgeOptions) === null || _a === void 0 ? void 0 : _a.hostAppOrigin; | ||
return origin === hostAppOrigin; | ||
} | ||
@@ -67,0 +76,0 @@ |
@@ -1,4 +0,5 @@ | ||
import { BridgeError, BridgeRequest } from "../types"; | ||
import { BridgeError, BridgeRequest, BridgeResponse } from "../types"; | ||
export declare function postMessage(message: BridgeRequest): void; | ||
export declare function makeRequest<Result>(request: BridgeRequest): Promise<Result | BridgeError>; | ||
export declare function isResponse(message: Object): message is BridgeResponse; | ||
export declare function isAllowedOrigin(origin: string): boolean; |
@@ -21,2 +21,5 @@ import { log } from "../logging"; | ||
const handler = (event) => { | ||
if (!isResponse(event.data) || event.data.id !== request.id) { | ||
return; | ||
} | ||
if (!isAllowedOrigin(event.origin)) { | ||
@@ -28,12 +31,10 @@ reject({ | ||
} | ||
if (event.data.id === request.id) { | ||
log("handleResponse", event.data); | ||
window.removeEventListener("message", handler); | ||
if (event.data.error) { | ||
reject(event.data.error); | ||
} | ||
else { | ||
resolve(event.data.result); | ||
} | ||
log("handleResponse", event.data); | ||
window.removeEventListener("message", handler); | ||
if (event.data.error) { | ||
reject(event.data.error); | ||
} | ||
else { | ||
resolve(event.data.result); | ||
} | ||
}; | ||
@@ -44,7 +45,10 @@ window.addEventListener("message", handler); | ||
} | ||
export function isResponse(message) { | ||
return "id" in message && ("result" in message || "error" in message); | ||
} | ||
export function isAllowedOrigin(origin) { | ||
// TODO: check origin | ||
console.log(origin); | ||
return true; | ||
var _a; | ||
const hostAppOrigin = (_a = window.flipBridgeOptions) === null || _a === void 0 ? void 0 : _a.hostAppOrigin; | ||
return origin === hostAppOrigin; | ||
} | ||
//# sourceMappingURL=messaging.js.map |
@@ -5,2 +5,2 @@ import { BridgeMethod, BridgeRequest } from "../types"; | ||
}>; | ||
export type GetNavigateResult = boolean; | ||
export type NavigateResult = boolean; |
@@ -6,3 +6,3 @@ import { FlipTheme } from "@getflip/swirl-components/dist/types/components/flip-theme-provider/flip-theme-provider"; | ||
activeTheme: FlipTheme; | ||
preferredTheme: FlipTheme; | ||
preferredTheme: FlipTheme | undefined; | ||
}; |
import { GetAvailableLangsResult, GetLangResult } from "./i18n"; | ||
import { GetNavigateResult } from "./navigation"; | ||
import { NavigateResult } from "./navigation"; | ||
import { GetThemeResult } from "./theming"; | ||
@@ -35,3 +35,3 @@ export type BridgeOptions = { | ||
[BridgeMethod.GET_THEME]: GetThemeResult; | ||
[BridgeMethod.NAVIGATE]: GetNavigateResult; | ||
[BridgeMethod.NAVIGATE]: NavigateResult; | ||
}; |
{ | ||
"name": "@getflip/bridge", | ||
"version": "0.1.0-alpha.4", | ||
"version": "0.1.0-alpha.5", | ||
"description": "Flip JavaScript Bridge for external integrations.", | ||
@@ -16,3 +16,5 @@ "main": "dist/index.cjs.js", | ||
"dev": "tsc -w", | ||
"start": "yarn dev" | ||
"start": "yarn dev", | ||
"test": "jest", | ||
"test:watch": "jest --watchAll" | ||
}, | ||
@@ -24,4 +26,10 @@ "dependencies": { | ||
"devDependencies": { | ||
"@babel/core": "^7.20.5", | ||
"@babel/preset-env": "^7.20.2", | ||
"@babel/preset-typescript": "^7.18.6", | ||
"@rollup/plugin-typescript": "^10.0.1", | ||
"@types/jest": "^27.0.3", | ||
"@types/uuid": "^8.3.4", | ||
"babel-jest": "^29.3.1", | ||
"jest": "^27.4.5", | ||
"rimraf": "^3.0.2", | ||
@@ -28,0 +36,0 @@ "rollup": "^3.5.1", |
@@ -13,2 +13,3 @@ # Flip Bridge | ||
- [Theming](#theming) | ||
- [Error Handling](#error-handling) | ||
- [Development](#development) | ||
@@ -29,2 +30,14 @@ | ||
**Before using the provided functions, you have to call the `initFlipBridge` | ||
function to set up the Flip Bridge.** | ||
```js | ||
import { initFlipBridge } from "@getflip/bridge"; | ||
initFlipBridge({ | ||
debug: true, | ||
hostAppOrigin: "http://localhost:4200", // has to be the origin of the targeted host app | ||
}); | ||
``` | ||
### Internationalization | ||
@@ -82,4 +95,11 @@ | ||
**Returns** `'light' | 'dark'` | ||
**Returns** | ||
```js | ||
{ | ||
activeTheme: "light" | "dark"; | ||
preferredTheme: "light" | "dark" | undefined; | ||
} | ||
``` | ||
**Example** | ||
@@ -90,5 +110,27 @@ | ||
const currentTheme = await getTheme(); // e.g. 'light' | ||
const theme = await getTheme(); | ||
``` | ||
## Error Handling | ||
All provided functions return promises that throw an error if the execution | ||
failed. The errors have the following format. | ||
```js | ||
{ | ||
code: BridgeErrorCode; // e.g. 'FORBIDDEN_ORIGIN' | ||
} | ||
``` | ||
### `FORBIDDEN_ORIGIN` | ||
Thrown when the origin of the requesting app is not allowed by the host app. | ||
Please check if your app is correctly registered in the Flip Partner Dashboard | ||
and the `hostAppOrigin` option is set (see [Usage](#usage)). | ||
### `INVALID_REQUEST` | ||
The host app identified the request as invalid. This typically occurs when the | ||
provided parameters are invalid. | ||
## Development | ||
@@ -99,3 +141,2 @@ | ||
``` | ||
yarn | ||
yarn dev | ||
@@ -102,0 +143,0 @@ ``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
44460
70
594
143
11