@milofly/app-bridge 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +5 -0
- package/LICENSE +21 -0
- package/README.md +49 -2
- package/contract.d.ts +294 -0
- package/contract.js +365 -0
- package/host.d.ts +99 -0
- package/host.js +201 -0
- package/index.d.ts +76 -0
- package/index.js +251 -0
- package/package.json +32 -4
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alper Baydeniz
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,50 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @milofly/app-bridge
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Embed your app in the Milofly store panel.
|
|
4
|
+
|
|
5
|
+
```
|
|
6
|
+
npm install @milofly/app-bridge
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
- `@milofly/app-bridge` — client for your embedded page (`createApp`).
|
|
10
|
+
- `@milofly/app-bridge/contract` — the message contract (types, limits, validators). Both the panel and your app consume it.
|
|
11
|
+
- `@milofly/app-bridge/host` — the host engine used by the Milofly panel. Apps do not need it.
|
|
12
|
+
|
|
13
|
+
## Quick start
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { createApp, MiloflyBridgeError } from "@milofly/app-bridge";
|
|
17
|
+
|
|
18
|
+
const app = await createApp({ allowedHostOrigins: ["https://app.milofly.com"] });
|
|
19
|
+
|
|
20
|
+
// Identity: send the session token to YOUR server and verify it there
|
|
21
|
+
// (@milofly/app-server → verifySessionToken, or Milofly.Apps → SessionTokenVerifier).
|
|
22
|
+
const token = await app.getSessionToken();
|
|
23
|
+
|
|
24
|
+
if (app.supports("toast")) await app.toast("Settings saved", { tone: "success" });
|
|
25
|
+
if (app.initialPath) router.go(app.initialPath); // reload / deep link of a full-page app
|
|
26
|
+
router.onChange((path) => app.navigate(path)); // the panel URL follows your app
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
`createApp` reads `mf_host` from the frame URL, removes `embed_token` from the address bar, sends
|
|
30
|
+
`milofly:ready` and waits for `milofly:init`. Everything in `app` (store, locale, surface) is
|
|
31
|
+
informational; authorization is what your server reads from the verified token.
|
|
32
|
+
|
|
33
|
+
## Messages (contract v2)
|
|
34
|
+
|
|
35
|
+
| Direction | Message | Notes |
|
|
36
|
+
|---|---|---|
|
|
37
|
+
| app → host | `milofly:ready` | Host answers with `init` every time. |
|
|
38
|
+
| host → app | `milofly:init` | `contractVersion`, `pluginKey`, `uiLocale`, `contentLocale`, `surface`, `entity`, `storeContext`, `hostOrigin`, `hostCapabilities`, `path`. |
|
|
39
|
+
| app → host | `milofly:resize` | Content height; ignored on full-page surfaces. |
|
|
40
|
+
| app → host | `milofly:token-request` | Answered with `milofly:token` or `milofly:token-error` (`reason` = access reason code). |
|
|
41
|
+
| app → host | `milofly:toast` `title` `navigate` `redirect` `open` `confirm` `loading` | Answered with `milofly:result {requestId, ok, data?, error?}`. `error`: `unsupported`, `invalid`, `denied`, `failed`. |
|
|
42
|
+
|
|
43
|
+
Limits and validation rules are in `contract.ts` (`LIMITS`, `parseAppMessage`); the host enforces them.
|
|
44
|
+
|
|
45
|
+
## Security
|
|
46
|
+
|
|
47
|
+
- The client talks only to the `mf_host` origin and only to `window.parent`; the host talks only to your
|
|
48
|
+
app origin and only to your frame window. `"*"` is never used.
|
|
49
|
+
- Pass `allowedHostOrigins` in production.
|
|
50
|
+
- The session token lives 2 minutes. `getSessionToken()` refreshes it through the host 30 seconds before expiry.
|
package/contract.d.ts
ADDED
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Milofly App Bridge — host ↔ embedded app message contract (single source).
|
|
3
|
+
*
|
|
4
|
+
* Both ends consume this file: the Milofly store panel (host, via `@milofly/app-bridge/host`)
|
|
5
|
+
* and every embedded app (via `createApp` from `@milofly/app-bridge`). A message that is not
|
|
6
|
+
* described here does not exist; a field that is not validated here is not accepted.
|
|
7
|
+
*
|
|
8
|
+
* Versioning: v1 messages (`ready`, `init`, `resize`, `token-request`, `token`, `token-error`)
|
|
9
|
+
* keep their shape, so apps written against v1 keep working. v2 adds host actions
|
|
10
|
+
* (`toast`, `title`, `navigate`, `redirect`, `open`, `confirm`, `loading`) answered with
|
|
11
|
+
* `result`, plus extra `init` fields. A breaking change bumps {@link CONTRACT_VERSION}.
|
|
12
|
+
*/
|
|
13
|
+
/** Current contract version, sent by the host in `milofly:init.contractVersion`. */
|
|
14
|
+
export declare const CONTRACT_VERSION = 2;
|
|
15
|
+
/** Every message type on the wire. */
|
|
16
|
+
export declare const MessageType: {
|
|
17
|
+
/** app → host: the app is listening; host answers with `init` (every time). */
|
|
18
|
+
readonly Ready: "milofly:ready";
|
|
19
|
+
/** host → app: context of this frame. */
|
|
20
|
+
readonly Init: "milofly:init";
|
|
21
|
+
/** app → host: content height in CSS px (ignored on full-page surfaces). */
|
|
22
|
+
readonly Resize: "milofly:resize";
|
|
23
|
+
/** app → host: ask for a fresh session token. */
|
|
24
|
+
readonly TokenRequest: "milofly:token-request";
|
|
25
|
+
/** host → app: fresh session token for a `token-request`. */
|
|
26
|
+
readonly Token: "milofly:token";
|
|
27
|
+
/** host → app: no token could be issued for a `token-request` (`reason` is an access reason code). */
|
|
28
|
+
readonly TokenError: "milofly:token-error";
|
|
29
|
+
/** app → host (v2): show a toast in the panel. */
|
|
30
|
+
readonly Toast: "milofly:toast";
|
|
31
|
+
/** app → host (v2): set the page title shown by the panel. */
|
|
32
|
+
readonly Title: "milofly:title";
|
|
33
|
+
/** app → host (v2): the app moved to another in-app path; the panel mirrors it in its own URL. */
|
|
34
|
+
readonly Navigate: "milofly:navigate";
|
|
35
|
+
/** app → host (v2): open a panel resource (order/product/customer detail). */
|
|
36
|
+
readonly Redirect: "milofly:redirect";
|
|
37
|
+
/** app → host (v2): open an external https URL in a new tab. */
|
|
38
|
+
readonly Open: "milofly:open";
|
|
39
|
+
/** app → host (v2): ask the merchant to confirm; answered with `{ confirmed }`. */
|
|
40
|
+
readonly Confirm: "milofly:confirm";
|
|
41
|
+
/** app → host (v2): show/hide the panel progress indicator. */
|
|
42
|
+
readonly Loading: "milofly:loading";
|
|
43
|
+
/** host → app (v2): answer to a v2 request. */
|
|
44
|
+
readonly Result: "milofly:result";
|
|
45
|
+
};
|
|
46
|
+
export type MessageTypeValue = (typeof MessageType)[keyof typeof MessageType];
|
|
47
|
+
/** Panel surfaces where an app frame can be rendered. */
|
|
48
|
+
export declare const Surface: {
|
|
49
|
+
readonly Fullpage: "fullpage";
|
|
50
|
+
readonly OrderDetail: "order-detail";
|
|
51
|
+
readonly CustomerDetail: "customer-detail";
|
|
52
|
+
readonly ProductDetail: "product-detail";
|
|
53
|
+
};
|
|
54
|
+
export type SurfaceKey = (typeof Surface)[keyof typeof Surface];
|
|
55
|
+
export declare const SURFACES: readonly SurfaceKey[];
|
|
56
|
+
export declare function isSurface(value: unknown): value is SurfaceKey;
|
|
57
|
+
/** Panel resources an app may point to (`redirect`) and detail surfaces show. */
|
|
58
|
+
export declare const Resource: {
|
|
59
|
+
readonly Order: "order";
|
|
60
|
+
readonly Customer: "customer";
|
|
61
|
+
readonly Product: "product";
|
|
62
|
+
};
|
|
63
|
+
export type ResourceKey = (typeof Resource)[keyof typeof Resource];
|
|
64
|
+
export declare const RESOURCES: readonly ResourceKey[];
|
|
65
|
+
export declare function isResource(value: unknown): value is ResourceKey;
|
|
66
|
+
/** Which resource a surface is about (`null` = the surface is not bound to one record). */
|
|
67
|
+
export declare const SURFACE_RESOURCE: Readonly<Record<SurfaceKey, ResourceKey | null>>;
|
|
68
|
+
/** Host actions (v2). A host lists the ones it implements in `init.hostCapabilities`. */
|
|
69
|
+
export declare const HostCapability: {
|
|
70
|
+
readonly Toast: "toast";
|
|
71
|
+
readonly Title: "title";
|
|
72
|
+
readonly Navigate: "navigate";
|
|
73
|
+
readonly Redirect: "redirect";
|
|
74
|
+
readonly Open: "open";
|
|
75
|
+
readonly Confirm: "confirm";
|
|
76
|
+
readonly Loading: "loading";
|
|
77
|
+
};
|
|
78
|
+
export type HostCapabilityKey = (typeof HostCapability)[keyof typeof HostCapability];
|
|
79
|
+
export declare const HOST_CAPABILITIES: readonly HostCapabilityKey[];
|
|
80
|
+
/** Request message type of each host action. */
|
|
81
|
+
export declare const CAPABILITY_MESSAGE: Readonly<{
|
|
82
|
+
readonly toast: "milofly:toast";
|
|
83
|
+
readonly title: "milofly:title";
|
|
84
|
+
readonly navigate: "milofly:navigate";
|
|
85
|
+
readonly redirect: "milofly:redirect";
|
|
86
|
+
readonly open: "milofly:open";
|
|
87
|
+
readonly confirm: "milofly:confirm";
|
|
88
|
+
readonly loading: "milofly:loading";
|
|
89
|
+
}>;
|
|
90
|
+
export declare const TOAST_TONES: readonly ["info", "success", "warning", "critical"];
|
|
91
|
+
export type ToastTone = (typeof TOAST_TONES)[number];
|
|
92
|
+
export declare const CONFIRM_TONES: readonly ["default", "critical"];
|
|
93
|
+
export type ConfirmTone = (typeof CONFIRM_TONES)[number];
|
|
94
|
+
/** Length limits enforced by the host (characters). */
|
|
95
|
+
export declare const LIMITS: Readonly<{
|
|
96
|
+
requestId: 128;
|
|
97
|
+
toastMessage: 300;
|
|
98
|
+
title: 120;
|
|
99
|
+
confirmTitle: 120;
|
|
100
|
+
confirmMessage: 1000;
|
|
101
|
+
confirmLabel: 40;
|
|
102
|
+
path: 2048;
|
|
103
|
+
url: 2048;
|
|
104
|
+
resourceId: 19;
|
|
105
|
+
}>;
|
|
106
|
+
/** Query parameters the host adds to the frame URL. */
|
|
107
|
+
export declare const UrlParam: {
|
|
108
|
+
/** Short-lived signed session token (the app strips it from its address bar). */
|
|
109
|
+
readonly EmbedToken: "embed_token";
|
|
110
|
+
/** Origin of the host panel. The app talks only to this origin. */
|
|
111
|
+
readonly HostOrigin: "mf_host";
|
|
112
|
+
/** In-app path to open (set when the merchant reloads or deep-links a full-page app). */
|
|
113
|
+
readonly Path: "mf_path";
|
|
114
|
+
};
|
|
115
|
+
/** `result.error` codes. */
|
|
116
|
+
export declare const ResultError: {
|
|
117
|
+
/** The host does not implement this action. */
|
|
118
|
+
readonly Unsupported: "unsupported";
|
|
119
|
+
/** The request failed validation (see {@link LIMITS} and the per-action rules). */
|
|
120
|
+
readonly Invalid: "invalid";
|
|
121
|
+
/** The host refused (e.g. the merchant is not allowed to open the resource). */
|
|
122
|
+
readonly Denied: "denied";
|
|
123
|
+
/** The host tried and failed. */
|
|
124
|
+
readonly Failed: "failed";
|
|
125
|
+
};
|
|
126
|
+
export type ResultErrorCode = (typeof ResultError)[keyof typeof ResultError];
|
|
127
|
+
/** Timings of the handshake and token refresh. */
|
|
128
|
+
export declare const TIMING: Readonly<{
|
|
129
|
+
/** Host: if the app never sends `ready`, `init` is sent this long after the frame loads. */
|
|
130
|
+
initFallbackMs: 1200;
|
|
131
|
+
/** App: how long `createApp` waits for `init`. */
|
|
132
|
+
handshakeTimeoutMs: 10000;
|
|
133
|
+
/** App: how long a token request waits for the host. */
|
|
134
|
+
tokenRequestTimeoutMs: 10000;
|
|
135
|
+
/** App: a cached token is refreshed when it expires within this many seconds. */
|
|
136
|
+
tokenRefreshMarginSeconds: 30;
|
|
137
|
+
/** App: how long a v2 request (except `confirm`) waits for `result`. */
|
|
138
|
+
requestTimeoutMs: 10000;
|
|
139
|
+
/** Host: identical token requests within this window share one issued token. */
|
|
140
|
+
hostTokenCacheMs: 5000;
|
|
141
|
+
}>;
|
|
142
|
+
/** Store context of the frame. Informational: identity comes from the verified session token. */
|
|
143
|
+
export interface StoreContext {
|
|
144
|
+
storeId: number;
|
|
145
|
+
regionId: number;
|
|
146
|
+
currencyCode: string;
|
|
147
|
+
}
|
|
148
|
+
export interface EntityRef {
|
|
149
|
+
type: ResourceKey;
|
|
150
|
+
id: string;
|
|
151
|
+
}
|
|
152
|
+
export interface InitMessage {
|
|
153
|
+
type: typeof MessageType.Init;
|
|
154
|
+
/** Absent on v1 hosts. */
|
|
155
|
+
contractVersion: number;
|
|
156
|
+
pluginKey: string;
|
|
157
|
+
/** v1 field; equals `uiLocale`. */
|
|
158
|
+
locale: string;
|
|
159
|
+
/** Language of the panel chrome. */
|
|
160
|
+
uiLocale: string;
|
|
161
|
+
/** Language of the store content being shown (region language). */
|
|
162
|
+
contentLocale: string;
|
|
163
|
+
surface: SurfaceKey;
|
|
164
|
+
/** Record the surface is about (detail surfaces), else `null`. */
|
|
165
|
+
entity: EntityRef | null;
|
|
166
|
+
storeContext: StoreContext;
|
|
167
|
+
hostOrigin: string;
|
|
168
|
+
hostCapabilities: HostCapabilityKey[];
|
|
169
|
+
/** In-app path to show first (full-page surface), else `null`. */
|
|
170
|
+
path: string | null;
|
|
171
|
+
}
|
|
172
|
+
export interface TokenMessage {
|
|
173
|
+
type: typeof MessageType.Token;
|
|
174
|
+
requestId: string;
|
|
175
|
+
token: string;
|
|
176
|
+
}
|
|
177
|
+
export interface TokenErrorMessage {
|
|
178
|
+
type: typeof MessageType.TokenError;
|
|
179
|
+
requestId: string;
|
|
180
|
+
/** Access reason code (see the API's app access gate), or `token_unavailable`. */
|
|
181
|
+
reason: string;
|
|
182
|
+
}
|
|
183
|
+
export interface ResultMessage {
|
|
184
|
+
type: typeof MessageType.Result;
|
|
185
|
+
requestId: string;
|
|
186
|
+
ok: boolean;
|
|
187
|
+
data?: unknown;
|
|
188
|
+
error?: ResultErrorCode;
|
|
189
|
+
}
|
|
190
|
+
export type HostMessage = InitMessage | TokenMessage | TokenErrorMessage | ResultMessage;
|
|
191
|
+
export interface ToastRequest {
|
|
192
|
+
message: string;
|
|
193
|
+
tone: ToastTone;
|
|
194
|
+
}
|
|
195
|
+
export interface TitleRequest {
|
|
196
|
+
title: string;
|
|
197
|
+
}
|
|
198
|
+
export interface NavigateRequest {
|
|
199
|
+
path: string;
|
|
200
|
+
}
|
|
201
|
+
export interface RedirectRequest {
|
|
202
|
+
resource: ResourceKey;
|
|
203
|
+
id: string;
|
|
204
|
+
}
|
|
205
|
+
export interface OpenRequest {
|
|
206
|
+
url: string;
|
|
207
|
+
}
|
|
208
|
+
export interface ConfirmRequest {
|
|
209
|
+
title: string;
|
|
210
|
+
message: string;
|
|
211
|
+
confirmLabel: string | null;
|
|
212
|
+
tone: ConfirmTone;
|
|
213
|
+
}
|
|
214
|
+
export interface LoadingRequest {
|
|
215
|
+
active: boolean;
|
|
216
|
+
}
|
|
217
|
+
/** Payload of each host action. */
|
|
218
|
+
export interface HostActionPayloads {
|
|
219
|
+
toast: ToastRequest;
|
|
220
|
+
title: TitleRequest;
|
|
221
|
+
navigate: NavigateRequest;
|
|
222
|
+
redirect: RedirectRequest;
|
|
223
|
+
open: OpenRequest;
|
|
224
|
+
confirm: ConfirmRequest;
|
|
225
|
+
loading: LoadingRequest;
|
|
226
|
+
}
|
|
227
|
+
/** Result data of each host action. */
|
|
228
|
+
export interface HostActionResults {
|
|
229
|
+
toast: null;
|
|
230
|
+
title: null;
|
|
231
|
+
navigate: null;
|
|
232
|
+
redirect: null;
|
|
233
|
+
open: null;
|
|
234
|
+
confirm: {
|
|
235
|
+
confirmed: boolean;
|
|
236
|
+
};
|
|
237
|
+
loading: null;
|
|
238
|
+
}
|
|
239
|
+
export type AppMessage = {
|
|
240
|
+
type: typeof MessageType.Ready;
|
|
241
|
+
} | {
|
|
242
|
+
type: typeof MessageType.Resize;
|
|
243
|
+
height: number;
|
|
244
|
+
} | {
|
|
245
|
+
type: typeof MessageType.TokenRequest;
|
|
246
|
+
requestId: string;
|
|
247
|
+
} | {
|
|
248
|
+
[K in HostCapabilityKey]: {
|
|
249
|
+
type: (typeof CAPABILITY_MESSAGE)[K];
|
|
250
|
+
capability: K;
|
|
251
|
+
requestId: string;
|
|
252
|
+
payload: HostActionPayloads[K];
|
|
253
|
+
};
|
|
254
|
+
}[HostCapabilityKey];
|
|
255
|
+
export declare function isRequestId(value: unknown): value is string;
|
|
256
|
+
/** In-app path: starts with a single `/`, no backslash, no control characters. */
|
|
257
|
+
export declare function isAppPath(value: unknown): value is string;
|
|
258
|
+
/** Positive integer record id (as string). */
|
|
259
|
+
export declare function isResourceId(value: unknown): value is string;
|
|
260
|
+
/** External URL the host may open: absolute https only. */
|
|
261
|
+
export declare function isOpenableUrl(value: unknown): value is string;
|
|
262
|
+
/** Loopback hosts allowed over plain http (local development only). */
|
|
263
|
+
export declare const LOOPBACK_HOSTNAMES: readonly string[];
|
|
264
|
+
/** Origin allowed for a host panel or an app frame: https, or http on loopback. */
|
|
265
|
+
export declare function isSecureOrigin(origin: string): boolean;
|
|
266
|
+
/**
|
|
267
|
+
* Parses a message received by the host.
|
|
268
|
+
* - `null`: not a Milofly bridge message (ignore).
|
|
269
|
+
* - `{ ok: false }`: a Milofly message that must be answered with `result` `error` (when it carries a request id).
|
|
270
|
+
*/
|
|
271
|
+
export type AppMessageParse = {
|
|
272
|
+
ok: true;
|
|
273
|
+
message: AppMessage;
|
|
274
|
+
} | {
|
|
275
|
+
ok: false;
|
|
276
|
+
type: string;
|
|
277
|
+
requestId: string | null;
|
|
278
|
+
error: typeof ResultError.Unsupported | typeof ResultError.Invalid;
|
|
279
|
+
};
|
|
280
|
+
export declare function parseAppMessage(data: unknown): AppMessageParse | null;
|
|
281
|
+
/**
|
|
282
|
+
* Validates an `init` message. Only contract v2+ is accepted: every field is required and nothing is
|
|
283
|
+
* filled in on the app's behalf (a v1 host never sends `contractVersion`; the SDK does not guess one).
|
|
284
|
+
* Returns the reason when the message is rejected so the app can report it.
|
|
285
|
+
*/
|
|
286
|
+
export declare function parseInitMessage(data: unknown): {
|
|
287
|
+
ok: true;
|
|
288
|
+
message: InitMessage;
|
|
289
|
+
} | {
|
|
290
|
+
ok: false;
|
|
291
|
+
reason: string;
|
|
292
|
+
};
|
|
293
|
+
/** Parses a `token`, `token-error` or `result` message received by the app. `null` = not one of them / invalid. */
|
|
294
|
+
export declare function parseHostReply(data: unknown): TokenMessage | TokenErrorMessage | ResultMessage | null;
|