@kadanza/extension-sdk 0.1.0 → 0.2.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/dist/index.d.ts CHANGED
@@ -31,7 +31,9 @@ export declare const CONNECTION_EVENTS: {
31
31
  readonly updatePageSettings: "UPDATE_PAGE_SETTINGS";
32
32
  /** Parent → child: result of a page settings update. */
33
33
  readonly pageSettingsUpdated: "PAGE_SETTINGS_UPDATED";
34
- /** Childparent: extension route changed. */
34
+ /** Parentchild: ask the SPA to navigate without reloading the iframe. */
35
+ readonly requestNavigationChange: "REQUEST_NAVIGATION_CHANGE";
36
+ /** Child → parent: extension route changed (ack or spontaneous). */
35
37
  readonly navigationChange: "NAVIGATION_CHANGE";
36
38
  };
37
39
 
@@ -42,6 +44,11 @@ export declare type ConnectionEvent = (typeof CONNECTION_EVENTS)[keyof typeof CO
42
44
  export declare interface ConnectOptions {
43
45
  /** Handshake timeout in milliseconds (default 10_000). */
44
46
  timeoutMs?: number;
47
+ /**
48
+ * Routing mode announced to the parent on `HANDSHAKE_INIT`.
49
+ * HashRouter SPAs should pass `client-hash`. Default: `server`.
50
+ */
51
+ routingType?: RoutingType;
45
52
  /**
46
53
  * Enables proactive auth-token refresh. After the handshake, the SDK reads
47
54
  * the auth token's Unix-seconds `expires` value and arms a one-shot timer
@@ -78,6 +85,9 @@ export declare interface ConnectOptions {
78
85
  */
79
86
  export declare const createExtensionSDK: () => IExtensionSDK;
80
87
 
88
+ /** Default when `routingType` is omitted or unrecognized. */
89
+ export declare const DEFAULT_ROUTING_TYPE: RoutingType;
90
+
81
91
  /**
82
92
  * Tenant branding from the parent, for aligning extension UI with the host.
83
93
  *
@@ -153,6 +163,7 @@ export declare class ExtensionSDK implements IExtensionSDK {
153
163
  emitRequestAuthTokenRefresh(options?: RequestOptions): Promise<AuthToken>;
154
164
  emitUpdatePageSettings(payload: UpdatePageSettingsPayload, options?: RequestOptions): Promise<PageSettingsUpdatedPayload>;
155
165
  onLoadPageSettings(handler: (settings: PageSettings) => void): () => void;
166
+ onNavigate(handler: (payload: RequestNavigationChangePayload) => void): () => void;
156
167
  onAuthTokenRefresh(handler: (authToken: AuthToken) => void): () => void;
157
168
  emitNavigationChange(payload: NavigationChangePayload): void;
158
169
  }
@@ -163,7 +174,9 @@ export declare class ExtensionSDKHost implements IExtensionSDKHost {
163
174
  constructor(options: ExtensionSDKHostOptions);
164
175
  start(): void;
165
176
  destroy(): void;
177
+ getRoutingType(): RoutingType;
166
178
  emitLoadPageSettings(settings: PageSettings | null): void;
179
+ requestNavigationChange(payload: RequestNavigationChangePayload, options?: RequestOptions): Promise<NavigationChangePayload>;
167
180
  }
168
181
 
169
182
  /**
@@ -182,7 +195,7 @@ export declare interface ExtensionSDKHostOptions {
182
195
  * Context fields are optional; send `designTokens` whenever a tenant is in
183
196
  * context. `spaceId` / `pageId` apply to Experience Pages only.
184
197
  */
185
- resolveHandshakePayload: () => Partial<HandshakePayload> | Promise<Partial<HandshakePayload>>;
198
+ resolveHandshakePayload: (extensionSDKHost: IExtensionSDKHost) => Partial<HandshakePayload> | Promise<Partial<HandshakePayload>>;
186
199
  /**
187
200
  * Resolves a fresh auth token when the child sends `REQUEST_TOKEN_REFRESH`.
188
201
  */
@@ -192,6 +205,20 @@ export declare interface ExtensionSDKHostOptions {
192
205
  * Return `true` on success; the host emits `PAGE_SETTINGS_UPDATED`.
193
206
  */
194
207
  onUpdatePageSettings?: (settings: PageSettings) => boolean | Promise<boolean>;
208
+ /**
209
+ * Called when the child reports a route change via `NAVIGATION_CHANGE`
210
+ * (spontaneous or as ACK to {@link IExtensionSDKHost.requestNavigationChange}).
211
+ */
212
+ onNavigationChange?: (payload: NavigationChangePayload) => void;
213
+ }
214
+
215
+ /** Payload for `HANDSHAKE_INIT` (child → parent). */
216
+ export declare interface HandshakeInitPayload {
217
+ /**
218
+ * Declares how the extension routes. Omit or unknown → `server`.
219
+ * Soft navigation requires `client-hash`.
220
+ */
221
+ routingType?: RoutingType;
195
222
  }
196
223
 
197
224
  /**
@@ -230,13 +257,16 @@ export declare interface IExtensionSDK {
230
257
  * Opt-in proactive refresh can be enabled with
231
258
  * `authTokenAutoRefresh: true` (optional `authTokenBufferMs`).
232
259
  *
260
+ * Pass `routingType: "client-hash"` for HashRouter SPAs so the parent can
261
+ * soft-navigate without reloading the iframe. Default is `"server"`.
262
+ *
233
263
  * Handshake completes when the parent sends `HANDSHAKE_ACK`. Context fields
234
264
  * (`authToken`, `extensionDetails`, `designTokens`, `pageSettings`) are
235
265
  * optional — omitted values are `null`. Use them only when the current host
236
266
  * surface provides them (e.g. `spaceId` / `pageId` on Experience Pages,
237
267
  * `designTokens` when a tenant is in context).
238
268
  *
239
- * @param options - Handshake timeout and optional auth-token auto-refresh.
269
+ * @param options - Handshake timeout, routing type, and optional auth-token auto-refresh.
240
270
  * @throws {InvalidOriginError} When `tenantUrl` is missing or invalid.
241
271
  * @throws When not embedded in a parent frame, destroyed, or the handshake
242
272
  * times out.
@@ -323,6 +353,14 @@ export declare interface IExtensionSDK {
323
353
  * @returns Unsubscribe function.
324
354
  */
325
355
  onLoadPageSettings(handler: (settings: PageSettings) => void): () => void;
356
+ /**
357
+ * Registers a handler for parent-initiated `REQUEST_NAVIGATION_CHANGE`.
358
+ * Navigate the SPA to `payload.path` and acknowledge with
359
+ * {@link emitNavigationChange}.
360
+ *
361
+ * @returns Unsubscribe function.
362
+ */
363
+ onNavigate(handler: (payload: RequestNavigationChangePayload) => void): () => void;
326
364
  /**
327
365
  * Registers a handler for auth-token updates (requested or parent-pushed).
328
366
  *
@@ -332,7 +370,8 @@ export declare interface IExtensionSDK {
332
370
  /**
333
371
  * Notifies the parent that the extension's route changed.
334
372
  *
335
- * Wire: `NAVIGATION_CHANGE` (fire-and-forget).
373
+ * Wire: `NAVIGATION_CHANGE` (fire-and-forget). Also used as the ACK after
374
+ * handling {@link onNavigate}.
336
375
  *
337
376
  * @param payload - Navigation change payload (`path` within the extension).
338
377
  * @throws When not connected or destroyed.
@@ -356,12 +395,30 @@ export declare interface IExtensionSDKHost {
356
395
  * Tears down the listener. The instance cannot be restarted after destroy.
357
396
  */
358
397
  destroy(): void;
398
+ /**
399
+ * Routing type from the last `HANDSHAKE_INIT`. Defaults to `server` until
400
+ * the child connects (and for omit / unrecognized values).
401
+ */
402
+ getRoutingType(): RoutingType;
359
403
  /**
360
404
  * Asks the child to open page settings UI with the given values.
361
405
  *
362
406
  * Wire: `LOAD_PAGE_SETTINGS` (fire-and-forget).
363
407
  */
364
408
  emitLoadPageSettings(settings: PageSettings | null): void;
409
+ /**
410
+ * Asks the child SPA to navigate to `payload.path` without reloading the
411
+ * iframe. Resolves when the child ACKs with a matching `NAVIGATION_CHANGE`.
412
+ *
413
+ * Soft navigation is intended for `client-hash` extensions; the host decides
414
+ * whether to call this or fall back to an iframe `src` reload.
415
+ *
416
+ * @param payload - Target path within the extension.
417
+ * @param options - Optional request timeout (default 10s).
418
+ * @throws When destroyed, a request is already in progress, the payload is
419
+ * invalid, or the request times out.
420
+ */
421
+ requestNavigationChange(payload: RequestNavigationChangePayload, options?: RequestOptions): Promise<NavigationChangePayload>;
365
422
  }
366
423
 
367
424
  /** Thrown when `tenantUrl` is missing or not a safe postMessage origin. */
@@ -379,9 +436,21 @@ export declare function isValidExtensionUrl(urlRaw: string | null | undefined, c
379
436
 
380
437
  /** Payload for `NAVIGATION_CHANGE` (child → parent). */
381
438
  export declare interface NavigationChangePayload {
439
+ /** Path within the extension (e.g. `/settings`). */
382
440
  path: string;
441
+ /**
442
+ * Query string of the child's current route, including the leading `?`
443
+ * (e.g. `?tab=history`). Omitted or empty means no query.
444
+ *
445
+ * For `client-hash` extensions the child reads this from its hash fragment,
446
+ * not from `window.location.search`.
447
+ */
448
+ search?: string;
383
449
  }
384
450
 
451
+ /** Normalize a wire / option value to a known {@link RoutingType}. */
452
+ export declare function normalizeRoutingType(value: unknown): RoutingType;
453
+
385
454
  /** Opaque page-level settings bag owned by the extension and synced with the parent. */
386
455
  export declare type PageSettings = Record<string, unknown>;
387
456
 
@@ -410,6 +479,20 @@ export declare function postToChild(contentWindow: Window, origin: string, type:
410
479
  */
411
480
  export declare function readTenantUrlFromLocation(search?: string): string | null;
412
481
 
482
+ /** Payload for `REQUEST_NAVIGATION_CHANGE` (parent → child). */
483
+ export declare interface RequestNavigationChangePayload {
484
+ /** Path within the extension (e.g. `/settings`). */
485
+ path: string;
486
+ /**
487
+ * Query string for the target route, including the leading `?`
488
+ * (e.g. `?tab=history`). Omitted or empty means no query.
489
+ *
490
+ * For `client-hash` extensions the query belongs inside the hash fragment;
491
+ * the child SDK consumer is responsible for applying it to its router.
492
+ */
493
+ search?: string;
494
+ }
495
+
413
496
  /** Options for request/response SDK methods that wait on a parent reply. */
414
497
  export declare interface RequestOptions {
415
498
  /** Request timeout in milliseconds (default 10_000). */
@@ -426,6 +509,15 @@ export declare interface RequestOptions {
426
509
  */
427
510
  export declare function resolveAllowedOrigin(tenantUrl: string | null): string;
428
511
 
512
+ /**
513
+ * How the extension handles in-app routing.
514
+ *
515
+ * - `server` — full document loads (iframe `src` reload). Default.
516
+ * - `client-hash` — hash router SPA; supports soft navigation via
517
+ * `REQUEST_NAVIGATION_CHANGE` / `NAVIGATION_CHANGE`.
518
+ */
519
+ export declare type RoutingType = "server" | "client-hash";
520
+
429
521
  /**
430
522
  * Subscribes to `message` events from a child frame at `origin`.
431
523
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kadanza/extension-sdk",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Helpers for building extensions",
5
5
  "keywords": [
6
6
  "kadanza",