@selfhelp/shared 1.15.3 → 1.17.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 +41 -0
- package/dist/plugin-sdk/index.d.mts +134 -2
- package/dist/plugin-sdk/index.d.ts +134 -2
- package/dist/plugin-sdk/index.js +14 -1
- package/dist/plugin-sdk/index.mjs +12 -1
- package/package.json +1 -1
- package/src/plugin-sdk/__tests__/host-services.test.ts +162 -0
- package/src/plugin-sdk/host-services.ts +136 -0
- package/src/plugin-sdk/index.ts +8 -0
- package/src/plugin-sdk/version.ts +12 -1
package/CHANGELOG.md
CHANGED
|
@@ -9,6 +9,47 @@ All notable changes to `@selfhelp/shared` will be documented in this file.
|
|
|
9
9
|
|
|
10
10
|
This project follows semantic versioning.
|
|
11
11
|
|
|
12
|
+
## v1.17.0
|
|
13
|
+
|
|
14
|
+
**Mobile host-navigation capability** — adds the optional
|
|
15
|
+
`IMobileHostServices.navigate(target, external?)` so a plugin's mobile package can
|
|
16
|
+
honour an in-content redirect (e.g. a survey's "redirect on completion") through
|
|
17
|
+
the host router instead of touching `window.location`. The host owns the router,
|
|
18
|
+
so it routes an internal CMS page keyword / in-app path through its own navigation
|
|
19
|
+
stack and only leaves the app for an explicit external URL — keeping redirects
|
|
20
|
+
correct in BOTH the native app and the CMS web preview iframe (where a raw
|
|
21
|
+
`location` assignment navigates and breaks the embedded frame). Additive only —
|
|
22
|
+
the method is optional, so `^1.x` consumers and older hosts/plugins are unaffected.
|
|
23
|
+
|
|
24
|
+
### Added (additive, non-breaking)
|
|
25
|
+
|
|
26
|
+
- **`IMobileHostServices.navigate?(target, external?)`** — optional host-owned
|
|
27
|
+
navigation; a plugin MUST feature-detect (`if (host.navigate)`) and degrade
|
|
28
|
+
gracefully on an older host.
|
|
29
|
+
- **`MOBILE_RENDERER_VERSION`** bumped `0.2.0 -> 0.3.0` (additive renderer
|
|
30
|
+
feature). Plugins that need host navigation declare `compatibility.mobile`
|
|
31
|
+
including `0.3.0`; they gate against it through `isMobileRendererCompatible`.
|
|
32
|
+
|
|
33
|
+
## v1.16.0
|
|
34
|
+
|
|
35
|
+
**Mobile host-services bridge** — a typed contract so a plugin's mobile package
|
|
36
|
+
routes protected API calls through the native host (which owns the access token,
|
|
37
|
+
the single 401-refresh round-trip, and session-expiry) instead of injecting raw
|
|
38
|
+
tokens into plugin/WebView code. Additive only — new `plugin-sdk` module +
|
|
39
|
+
exports — so `^1.x` consumers are unaffected; the SurveyJS mobile WebView
|
|
40
|
+
renderer (`@selfhelp/sh2-shp-survey-js-mobile`) and the mobile host adopt it.
|
|
41
|
+
|
|
42
|
+
### Added (additive, non-breaking)
|
|
43
|
+
|
|
44
|
+
- **`plugin-sdk/host-services.ts`** new exports (via `@selfhelp/shared/plugin-sdk`):
|
|
45
|
+
- `IMobileHostServices` (`apiBaseUrl()`, `getAccessToken()`, `request()`),
|
|
46
|
+
`IMobileHostRequest`, `IMobileHostResponse<TData>`, `TMobileHostMethod`;
|
|
47
|
+
- the singleton registry `setMobileHostServices()` / `getMobileHostServices()`
|
|
48
|
+
(host registers at boot; the plugin consumes at runtime).
|
|
49
|
+
- **`MOBILE_RENDERER_VERSION`** bumped `0.1.0 -> 0.2.0` (additive renderer
|
|
50
|
+
feature). Plugins declaring `compatibility.mobile: ^0.2.0` gate against it
|
|
51
|
+
through `isMobileRendererCompatible`.
|
|
52
|
+
|
|
12
53
|
## v1.15.3
|
|
13
54
|
|
|
14
55
|
**Live Preview shared theme + language sync** — extends the bridge contract so a
|
|
@@ -30,8 +30,19 @@ declare const PLUGIN_API_VERSION: "0.1.0";
|
|
|
30
30
|
*
|
|
31
31
|
* Bump rules mirror the SDK contract: patch = additive fix, minor = additive
|
|
32
32
|
* renderer feature, major = breaking mobile renderer change.
|
|
33
|
+
*
|
|
34
|
+
* `0.2.0` adds the typed mobile host-services bridge (`IMobileHostServices` +
|
|
35
|
+
* `set/getMobileHostServices`) so a plugin's mobile package routes protected
|
|
36
|
+
* API calls through the native host (token + 401-refresh owned host-side)
|
|
37
|
+
* instead of injecting raw tokens — an additive renderer feature.
|
|
38
|
+
*
|
|
39
|
+
* `0.3.0` adds the optional `IMobileHostServices.navigate(target, external)`
|
|
40
|
+
* host-navigation capability so a plugin can honour an in-content redirect
|
|
41
|
+
* through the host router (correct in the native app AND the CMS web preview
|
|
42
|
+
* iframe) instead of touching `window.location` — an additive renderer feature.
|
|
43
|
+
* It is optional, so older plugins/hosts stay compatible.
|
|
33
44
|
*/
|
|
34
|
-
declare const MOBILE_RENDERER_VERSION: "0.
|
|
45
|
+
declare const MOBILE_RENDERER_VERSION: "0.3.0";
|
|
35
46
|
interface ISemver {
|
|
36
47
|
major: number;
|
|
37
48
|
minor: number;
|
|
@@ -692,6 +703,127 @@ declare function defineMobilePlugin(registration: IMobilePluginRegistration): IM
|
|
|
692
703
|
*/
|
|
693
704
|
declare function definePluginRealtimeTopic<TPayload>(descriptor: IPluginRealtimeTopic<TPayload>): IPluginRealtimeTopic<TPayload>;
|
|
694
705
|
|
|
706
|
+
/**
|
|
707
|
+
* Mobile host-services bridge (mobile renderer contract, `MOBILE_RENDERER_VERSION`).
|
|
708
|
+
*
|
|
709
|
+
* A plugin's mobile package renders its UI (commonly inside an isolated
|
|
710
|
+
* WebView), but it MUST NOT own authenticated backend access. Instead the
|
|
711
|
+
* native mobile host registers an `IMobileHostServices` implementation at
|
|
712
|
+
* boot, and the plugin performs every protected API call through it. This
|
|
713
|
+
* keeps the access token, the 401-refresh round-trip, session-expiry, the
|
|
714
|
+
* API base URL, and client headers (`X-Client-Type: mobile`, locale) in the
|
|
715
|
+
* host — never inside plugin/WebView code.
|
|
716
|
+
*
|
|
717
|
+
* Design constraints:
|
|
718
|
+
* - Framework-agnostic: no `react` / `react-native` / `axios` imports here.
|
|
719
|
+
* The host wires its own HTTP client (e.g. the shared axios singleton)
|
|
720
|
+
* into `request`.
|
|
721
|
+
* - Singleton: the host calls {@link setMobileHostServices} once during
|
|
722
|
+
* boot; the plugin calls {@link getMobileHostServices} at runtime.
|
|
723
|
+
* - The token is intentionally NOT a required input to plugin calls. A
|
|
724
|
+
* plugin may read {@link IMobileHostServices.getAccessToken} only for
|
|
725
|
+
* non-sensitive bootstrapping; it must never implement its own refresh.
|
|
726
|
+
*/
|
|
727
|
+
/** HTTP method accepted by the host request bridge. */
|
|
728
|
+
type TMobileHostMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
729
|
+
/**
|
|
730
|
+
* A protected backend request the plugin asks the host to perform. The
|
|
731
|
+
* host prepends {@link IMobileHostServices.apiBaseUrl}, attaches the bearer
|
|
732
|
+
* token + client headers, and handles a single 401-refresh retry.
|
|
733
|
+
*/
|
|
734
|
+
interface IMobileHostRequest {
|
|
735
|
+
/**
|
|
736
|
+
* Path relative to the host API base, e.g.
|
|
737
|
+
* `/cms-api/v1/plugins/<id>/published/<key>`. Absolute URLs are rejected
|
|
738
|
+
* by the host so a plugin cannot redirect protected calls off-origin.
|
|
739
|
+
*/
|
|
740
|
+
path: string;
|
|
741
|
+
/** HTTP method. Defaults to `GET`. */
|
|
742
|
+
method?: TMobileHostMethod;
|
|
743
|
+
/** JSON-serialisable request body (sent as `application/json`). */
|
|
744
|
+
body?: unknown;
|
|
745
|
+
/** Extra request headers (e.g. a plugin runtime-config echo header). */
|
|
746
|
+
headers?: Record<string, string>;
|
|
747
|
+
/** Query parameters appended to the path. */
|
|
748
|
+
query?: Record<string, string | number | boolean>;
|
|
749
|
+
}
|
|
750
|
+
/**
|
|
751
|
+
* Result of a host request. `data` is the parsed response body (the SelfHelp
|
|
752
|
+
* API envelope `{ status, message, data, ... }` for API routes), so the
|
|
753
|
+
* plugin reads `response.data.data` for the inner payload. `reason` mirrors
|
|
754
|
+
* the backend error discriminator (e.g. `already_submitted_once`) so the
|
|
755
|
+
* plugin can branch on lifecycle errors without re-parsing the body.
|
|
756
|
+
*/
|
|
757
|
+
interface IMobileHostResponse<TData = unknown> {
|
|
758
|
+
/** True for 2xx responses. */
|
|
759
|
+
ok: boolean;
|
|
760
|
+
/** HTTP status code (0 when the request never reached the server). */
|
|
761
|
+
status: number;
|
|
762
|
+
/** Parsed response body, or `null` when there is no JSON body. */
|
|
763
|
+
data: TData | null;
|
|
764
|
+
/** Backend error `reason` discriminator, when present. */
|
|
765
|
+
reason?: string;
|
|
766
|
+
/** Human-readable error message for non-ok responses. */
|
|
767
|
+
error?: string;
|
|
768
|
+
/**
|
|
769
|
+
* True when the call failed because the session could not be
|
|
770
|
+
* authenticated even after the host's refresh attempt (a 401 the host
|
|
771
|
+
* could not recover). The plugin should surface a "session expired"
|
|
772
|
+
* state rather than a generic error.
|
|
773
|
+
*/
|
|
774
|
+
sessionExpired?: boolean;
|
|
775
|
+
}
|
|
776
|
+
/**
|
|
777
|
+
* Authenticated host services exposed to a plugin's mobile package. The
|
|
778
|
+
* host owns the token + refresh; the plugin only describes the request.
|
|
779
|
+
*/
|
|
780
|
+
interface IMobileHostServices {
|
|
781
|
+
/** Resolved API base URL the host calls (e.g. `https://cms.example.com`). */
|
|
782
|
+
apiBaseUrl(): string;
|
|
783
|
+
/**
|
|
784
|
+
* Current access token, or `null` when unauthenticated. Exposed only for
|
|
785
|
+
* non-sensitive bootstrapping (e.g. deciding whether to show a login
|
|
786
|
+
* prompt). Plugins must route protected calls through {@link request},
|
|
787
|
+
* which attaches the token host-side, instead of injecting this value.
|
|
788
|
+
*/
|
|
789
|
+
getAccessToken(): string | null;
|
|
790
|
+
/** Perform an authenticated request with host-managed 401 refresh. */
|
|
791
|
+
request<TData = unknown>(req: IMobileHostRequest): Promise<IMobileHostResponse<TData>>;
|
|
792
|
+
/**
|
|
793
|
+
* Navigate the host app on the user's behalf to honour an in-content
|
|
794
|
+
* redirect (e.g. a survey's "redirect on completion") WITHOUT the plugin
|
|
795
|
+
* touching `window.location`. The host owns the router, so it routes an
|
|
796
|
+
* internal CMS page keyword / in-app path through its own navigation stack
|
|
797
|
+
* and only leaves the app for an explicit external URL. This keeps redirects
|
|
798
|
+
* correct in EVERY host surface — the native app AND the CMS web preview,
|
|
799
|
+
* where a raw `location` assignment would navigate (and break) the embedded
|
|
800
|
+
* iframe instead of the app.
|
|
801
|
+
*
|
|
802
|
+
* @param target CMS page keyword / in-app path, or an absolute URL when
|
|
803
|
+
* `external` is true.
|
|
804
|
+
* @param external When true the target is a full off-app URL the host opens
|
|
805
|
+
* in the system browser (native) / a new tab (web) instead of
|
|
806
|
+
* routing inside the app. Defaults to false.
|
|
807
|
+
*
|
|
808
|
+
* Optional: a host registered by an older app binary (renderer < 0.3.0) may
|
|
809
|
+
* omit it, so a plugin MUST feature-detect (`if (host.navigate)`) and degrade
|
|
810
|
+
* gracefully rather than assume it exists.
|
|
811
|
+
*/
|
|
812
|
+
navigate?(target: string, external?: boolean): void;
|
|
813
|
+
}
|
|
814
|
+
/**
|
|
815
|
+
* Register the host services implementation. Called once by the native
|
|
816
|
+
* mobile host during boot (before any plugin renders). Re-registering
|
|
817
|
+
* replaces the previous implementation (e.g. on a server switch).
|
|
818
|
+
*/
|
|
819
|
+
declare function setMobileHostServices(services: IMobileHostServices | null): void;
|
|
820
|
+
/**
|
|
821
|
+
* Read the registered host services. Returns `null` when no host has
|
|
822
|
+
* registered yet — a plugin must treat that as "host too old / not ready"
|
|
823
|
+
* and degrade gracefully (it must never fall back to its own token logic).
|
|
824
|
+
*/
|
|
825
|
+
declare function getMobileHostServices(): IMobileHostServices | null;
|
|
826
|
+
|
|
695
827
|
/**
|
|
696
828
|
* `usePluginRealtime` — universal React hook for plugin Mercure topic
|
|
697
829
|
* subscription.
|
|
@@ -874,4 +1006,4 @@ declare function isPluginRuntimeShimSpecifier(value: unknown): value is TPluginR
|
|
|
874
1006
|
*/
|
|
875
1007
|
declare const PLUGIN_RUNTIME_IMPORT_MAP: Readonly<Record<TPluginRuntimeShimSpecifier, string>>;
|
|
876
1008
|
|
|
877
|
-
export { type IAdminPageDefinition, type IFieldRendererDefinition, type IMenuItemDefinition, type IMobilePluginApi, type IMobilePluginRegistration, type IPluginApi, type IPluginFeatureFlag, type IPluginFieldRendererProps, type IPluginHealthCheck, type IPluginLock, type IPluginLockEntry, type IPluginLockMigration, type IPluginLockMobileEntry, type IPluginRealtimePublisher, type IPluginRealtimeTopic, type IPluginRegistration, type IPluginRegistry, type IPluginRelease, type IRealtimeTransport, type IRegistryReleaseRef, type IRegistrySignatureBlock, type IRichTextEditorAdapter, type IRichTextEditorAdapterOptions, type IRichTextEditorValue, type IStyleDefinition, type IUsePluginRealtimeOptions, type IUsePluginRealtimeResult, MOBILE_RENDERER_VERSION, PLUGIN_API_VERSION, PLUGIN_REGISTRY_CHANNELS, PLUGIN_RUNTIME_GLOBAL_KEY, PLUGIN_RUNTIME_IMPORT_MAP, PLUGIN_RUNTIME_SHIM_BASE_PATH, PLUGIN_RUNTIME_SHIM_SPECIFIERS, type TPluginRegistryChannel, type TPluginRuntimeShimSpecifier, type TRealtimeTransportFactory, assertCmsCompatibility, assertPluginApiVersion, assertPluginVersionSemantics, buildPluginRuntimeShimPath, compareSemver, defineMobilePlugin, definePlugin, definePluginRealtimeTopic, isMobileRendererCompatible, isPluginApiCompatible, isPluginRuntimeShimSpecifier, parseSemver, satisfiesSemverRange, usePluginRealtime };
|
|
1009
|
+
export { type IAdminPageDefinition, type IFieldRendererDefinition, type IMenuItemDefinition, type IMobileHostRequest, type IMobileHostResponse, type IMobileHostServices, type IMobilePluginApi, type IMobilePluginRegistration, type IPluginApi, type IPluginFeatureFlag, type IPluginFieldRendererProps, type IPluginHealthCheck, type IPluginLock, type IPluginLockEntry, type IPluginLockMigration, type IPluginLockMobileEntry, type IPluginRealtimePublisher, type IPluginRealtimeTopic, type IPluginRegistration, type IPluginRegistry, type IPluginRelease, type IRealtimeTransport, type IRegistryReleaseRef, type IRegistrySignatureBlock, type IRichTextEditorAdapter, type IRichTextEditorAdapterOptions, type IRichTextEditorValue, type IStyleDefinition, type IUsePluginRealtimeOptions, type IUsePluginRealtimeResult, MOBILE_RENDERER_VERSION, PLUGIN_API_VERSION, PLUGIN_REGISTRY_CHANNELS, PLUGIN_RUNTIME_GLOBAL_KEY, PLUGIN_RUNTIME_IMPORT_MAP, PLUGIN_RUNTIME_SHIM_BASE_PATH, PLUGIN_RUNTIME_SHIM_SPECIFIERS, type TMobileHostMethod, type TPluginRegistryChannel, type TPluginRuntimeShimSpecifier, type TRealtimeTransportFactory, assertCmsCompatibility, assertPluginApiVersion, assertPluginVersionSemantics, buildPluginRuntimeShimPath, compareSemver, defineMobilePlugin, definePlugin, definePluginRealtimeTopic, getMobileHostServices, isMobileRendererCompatible, isPluginApiCompatible, isPluginRuntimeShimSpecifier, parseSemver, satisfiesSemverRange, setMobileHostServices, usePluginRealtime };
|
|
@@ -30,8 +30,19 @@ declare const PLUGIN_API_VERSION: "0.1.0";
|
|
|
30
30
|
*
|
|
31
31
|
* Bump rules mirror the SDK contract: patch = additive fix, minor = additive
|
|
32
32
|
* renderer feature, major = breaking mobile renderer change.
|
|
33
|
+
*
|
|
34
|
+
* `0.2.0` adds the typed mobile host-services bridge (`IMobileHostServices` +
|
|
35
|
+
* `set/getMobileHostServices`) so a plugin's mobile package routes protected
|
|
36
|
+
* API calls through the native host (token + 401-refresh owned host-side)
|
|
37
|
+
* instead of injecting raw tokens — an additive renderer feature.
|
|
38
|
+
*
|
|
39
|
+
* `0.3.0` adds the optional `IMobileHostServices.navigate(target, external)`
|
|
40
|
+
* host-navigation capability so a plugin can honour an in-content redirect
|
|
41
|
+
* through the host router (correct in the native app AND the CMS web preview
|
|
42
|
+
* iframe) instead of touching `window.location` — an additive renderer feature.
|
|
43
|
+
* It is optional, so older plugins/hosts stay compatible.
|
|
33
44
|
*/
|
|
34
|
-
declare const MOBILE_RENDERER_VERSION: "0.
|
|
45
|
+
declare const MOBILE_RENDERER_VERSION: "0.3.0";
|
|
35
46
|
interface ISemver {
|
|
36
47
|
major: number;
|
|
37
48
|
minor: number;
|
|
@@ -692,6 +703,127 @@ declare function defineMobilePlugin(registration: IMobilePluginRegistration): IM
|
|
|
692
703
|
*/
|
|
693
704
|
declare function definePluginRealtimeTopic<TPayload>(descriptor: IPluginRealtimeTopic<TPayload>): IPluginRealtimeTopic<TPayload>;
|
|
694
705
|
|
|
706
|
+
/**
|
|
707
|
+
* Mobile host-services bridge (mobile renderer contract, `MOBILE_RENDERER_VERSION`).
|
|
708
|
+
*
|
|
709
|
+
* A plugin's mobile package renders its UI (commonly inside an isolated
|
|
710
|
+
* WebView), but it MUST NOT own authenticated backend access. Instead the
|
|
711
|
+
* native mobile host registers an `IMobileHostServices` implementation at
|
|
712
|
+
* boot, and the plugin performs every protected API call through it. This
|
|
713
|
+
* keeps the access token, the 401-refresh round-trip, session-expiry, the
|
|
714
|
+
* API base URL, and client headers (`X-Client-Type: mobile`, locale) in the
|
|
715
|
+
* host — never inside plugin/WebView code.
|
|
716
|
+
*
|
|
717
|
+
* Design constraints:
|
|
718
|
+
* - Framework-agnostic: no `react` / `react-native` / `axios` imports here.
|
|
719
|
+
* The host wires its own HTTP client (e.g. the shared axios singleton)
|
|
720
|
+
* into `request`.
|
|
721
|
+
* - Singleton: the host calls {@link setMobileHostServices} once during
|
|
722
|
+
* boot; the plugin calls {@link getMobileHostServices} at runtime.
|
|
723
|
+
* - The token is intentionally NOT a required input to plugin calls. A
|
|
724
|
+
* plugin may read {@link IMobileHostServices.getAccessToken} only for
|
|
725
|
+
* non-sensitive bootstrapping; it must never implement its own refresh.
|
|
726
|
+
*/
|
|
727
|
+
/** HTTP method accepted by the host request bridge. */
|
|
728
|
+
type TMobileHostMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
729
|
+
/**
|
|
730
|
+
* A protected backend request the plugin asks the host to perform. The
|
|
731
|
+
* host prepends {@link IMobileHostServices.apiBaseUrl}, attaches the bearer
|
|
732
|
+
* token + client headers, and handles a single 401-refresh retry.
|
|
733
|
+
*/
|
|
734
|
+
interface IMobileHostRequest {
|
|
735
|
+
/**
|
|
736
|
+
* Path relative to the host API base, e.g.
|
|
737
|
+
* `/cms-api/v1/plugins/<id>/published/<key>`. Absolute URLs are rejected
|
|
738
|
+
* by the host so a plugin cannot redirect protected calls off-origin.
|
|
739
|
+
*/
|
|
740
|
+
path: string;
|
|
741
|
+
/** HTTP method. Defaults to `GET`. */
|
|
742
|
+
method?: TMobileHostMethod;
|
|
743
|
+
/** JSON-serialisable request body (sent as `application/json`). */
|
|
744
|
+
body?: unknown;
|
|
745
|
+
/** Extra request headers (e.g. a plugin runtime-config echo header). */
|
|
746
|
+
headers?: Record<string, string>;
|
|
747
|
+
/** Query parameters appended to the path. */
|
|
748
|
+
query?: Record<string, string | number | boolean>;
|
|
749
|
+
}
|
|
750
|
+
/**
|
|
751
|
+
* Result of a host request. `data` is the parsed response body (the SelfHelp
|
|
752
|
+
* API envelope `{ status, message, data, ... }` for API routes), so the
|
|
753
|
+
* plugin reads `response.data.data` for the inner payload. `reason` mirrors
|
|
754
|
+
* the backend error discriminator (e.g. `already_submitted_once`) so the
|
|
755
|
+
* plugin can branch on lifecycle errors without re-parsing the body.
|
|
756
|
+
*/
|
|
757
|
+
interface IMobileHostResponse<TData = unknown> {
|
|
758
|
+
/** True for 2xx responses. */
|
|
759
|
+
ok: boolean;
|
|
760
|
+
/** HTTP status code (0 when the request never reached the server). */
|
|
761
|
+
status: number;
|
|
762
|
+
/** Parsed response body, or `null` when there is no JSON body. */
|
|
763
|
+
data: TData | null;
|
|
764
|
+
/** Backend error `reason` discriminator, when present. */
|
|
765
|
+
reason?: string;
|
|
766
|
+
/** Human-readable error message for non-ok responses. */
|
|
767
|
+
error?: string;
|
|
768
|
+
/**
|
|
769
|
+
* True when the call failed because the session could not be
|
|
770
|
+
* authenticated even after the host's refresh attempt (a 401 the host
|
|
771
|
+
* could not recover). The plugin should surface a "session expired"
|
|
772
|
+
* state rather than a generic error.
|
|
773
|
+
*/
|
|
774
|
+
sessionExpired?: boolean;
|
|
775
|
+
}
|
|
776
|
+
/**
|
|
777
|
+
* Authenticated host services exposed to a plugin's mobile package. The
|
|
778
|
+
* host owns the token + refresh; the plugin only describes the request.
|
|
779
|
+
*/
|
|
780
|
+
interface IMobileHostServices {
|
|
781
|
+
/** Resolved API base URL the host calls (e.g. `https://cms.example.com`). */
|
|
782
|
+
apiBaseUrl(): string;
|
|
783
|
+
/**
|
|
784
|
+
* Current access token, or `null` when unauthenticated. Exposed only for
|
|
785
|
+
* non-sensitive bootstrapping (e.g. deciding whether to show a login
|
|
786
|
+
* prompt). Plugins must route protected calls through {@link request},
|
|
787
|
+
* which attaches the token host-side, instead of injecting this value.
|
|
788
|
+
*/
|
|
789
|
+
getAccessToken(): string | null;
|
|
790
|
+
/** Perform an authenticated request with host-managed 401 refresh. */
|
|
791
|
+
request<TData = unknown>(req: IMobileHostRequest): Promise<IMobileHostResponse<TData>>;
|
|
792
|
+
/**
|
|
793
|
+
* Navigate the host app on the user's behalf to honour an in-content
|
|
794
|
+
* redirect (e.g. a survey's "redirect on completion") WITHOUT the plugin
|
|
795
|
+
* touching `window.location`. The host owns the router, so it routes an
|
|
796
|
+
* internal CMS page keyword / in-app path through its own navigation stack
|
|
797
|
+
* and only leaves the app for an explicit external URL. This keeps redirects
|
|
798
|
+
* correct in EVERY host surface — the native app AND the CMS web preview,
|
|
799
|
+
* where a raw `location` assignment would navigate (and break) the embedded
|
|
800
|
+
* iframe instead of the app.
|
|
801
|
+
*
|
|
802
|
+
* @param target CMS page keyword / in-app path, or an absolute URL when
|
|
803
|
+
* `external` is true.
|
|
804
|
+
* @param external When true the target is a full off-app URL the host opens
|
|
805
|
+
* in the system browser (native) / a new tab (web) instead of
|
|
806
|
+
* routing inside the app. Defaults to false.
|
|
807
|
+
*
|
|
808
|
+
* Optional: a host registered by an older app binary (renderer < 0.3.0) may
|
|
809
|
+
* omit it, so a plugin MUST feature-detect (`if (host.navigate)`) and degrade
|
|
810
|
+
* gracefully rather than assume it exists.
|
|
811
|
+
*/
|
|
812
|
+
navigate?(target: string, external?: boolean): void;
|
|
813
|
+
}
|
|
814
|
+
/**
|
|
815
|
+
* Register the host services implementation. Called once by the native
|
|
816
|
+
* mobile host during boot (before any plugin renders). Re-registering
|
|
817
|
+
* replaces the previous implementation (e.g. on a server switch).
|
|
818
|
+
*/
|
|
819
|
+
declare function setMobileHostServices(services: IMobileHostServices | null): void;
|
|
820
|
+
/**
|
|
821
|
+
* Read the registered host services. Returns `null` when no host has
|
|
822
|
+
* registered yet — a plugin must treat that as "host too old / not ready"
|
|
823
|
+
* and degrade gracefully (it must never fall back to its own token logic).
|
|
824
|
+
*/
|
|
825
|
+
declare function getMobileHostServices(): IMobileHostServices | null;
|
|
826
|
+
|
|
695
827
|
/**
|
|
696
828
|
* `usePluginRealtime` — universal React hook for plugin Mercure topic
|
|
697
829
|
* subscription.
|
|
@@ -874,4 +1006,4 @@ declare function isPluginRuntimeShimSpecifier(value: unknown): value is TPluginR
|
|
|
874
1006
|
*/
|
|
875
1007
|
declare const PLUGIN_RUNTIME_IMPORT_MAP: Readonly<Record<TPluginRuntimeShimSpecifier, string>>;
|
|
876
1008
|
|
|
877
|
-
export { type IAdminPageDefinition, type IFieldRendererDefinition, type IMenuItemDefinition, type IMobilePluginApi, type IMobilePluginRegistration, type IPluginApi, type IPluginFeatureFlag, type IPluginFieldRendererProps, type IPluginHealthCheck, type IPluginLock, type IPluginLockEntry, type IPluginLockMigration, type IPluginLockMobileEntry, type IPluginRealtimePublisher, type IPluginRealtimeTopic, type IPluginRegistration, type IPluginRegistry, type IPluginRelease, type IRealtimeTransport, type IRegistryReleaseRef, type IRegistrySignatureBlock, type IRichTextEditorAdapter, type IRichTextEditorAdapterOptions, type IRichTextEditorValue, type IStyleDefinition, type IUsePluginRealtimeOptions, type IUsePluginRealtimeResult, MOBILE_RENDERER_VERSION, PLUGIN_API_VERSION, PLUGIN_REGISTRY_CHANNELS, PLUGIN_RUNTIME_GLOBAL_KEY, PLUGIN_RUNTIME_IMPORT_MAP, PLUGIN_RUNTIME_SHIM_BASE_PATH, PLUGIN_RUNTIME_SHIM_SPECIFIERS, type TPluginRegistryChannel, type TPluginRuntimeShimSpecifier, type TRealtimeTransportFactory, assertCmsCompatibility, assertPluginApiVersion, assertPluginVersionSemantics, buildPluginRuntimeShimPath, compareSemver, defineMobilePlugin, definePlugin, definePluginRealtimeTopic, isMobileRendererCompatible, isPluginApiCompatible, isPluginRuntimeShimSpecifier, parseSemver, satisfiesSemverRange, usePluginRealtime };
|
|
1009
|
+
export { type IAdminPageDefinition, type IFieldRendererDefinition, type IMenuItemDefinition, type IMobileHostRequest, type IMobileHostResponse, type IMobileHostServices, type IMobilePluginApi, type IMobilePluginRegistration, type IPluginApi, type IPluginFeatureFlag, type IPluginFieldRendererProps, type IPluginHealthCheck, type IPluginLock, type IPluginLockEntry, type IPluginLockMigration, type IPluginLockMobileEntry, type IPluginRealtimePublisher, type IPluginRealtimeTopic, type IPluginRegistration, type IPluginRegistry, type IPluginRelease, type IRealtimeTransport, type IRegistryReleaseRef, type IRegistrySignatureBlock, type IRichTextEditorAdapter, type IRichTextEditorAdapterOptions, type IRichTextEditorValue, type IStyleDefinition, type IUsePluginRealtimeOptions, type IUsePluginRealtimeResult, MOBILE_RENDERER_VERSION, PLUGIN_API_VERSION, PLUGIN_REGISTRY_CHANNELS, PLUGIN_RUNTIME_GLOBAL_KEY, PLUGIN_RUNTIME_IMPORT_MAP, PLUGIN_RUNTIME_SHIM_BASE_PATH, PLUGIN_RUNTIME_SHIM_SPECIFIERS, type TMobileHostMethod, type TPluginRegistryChannel, type TPluginRuntimeShimSpecifier, type TRealtimeTransportFactory, assertCmsCompatibility, assertPluginApiVersion, assertPluginVersionSemantics, buildPluginRuntimeShimPath, compareSemver, defineMobilePlugin, definePlugin, definePluginRealtimeTopic, getMobileHostServices, isMobileRendererCompatible, isPluginApiCompatible, isPluginRuntimeShimSpecifier, parseSemver, satisfiesSemverRange, setMobileHostServices, usePluginRealtime };
|
package/dist/plugin-sdk/index.js
CHANGED
|
@@ -35,18 +35,20 @@ __export(plugin_sdk_exports, {
|
|
|
35
35
|
defineMobilePlugin: () => defineMobilePlugin,
|
|
36
36
|
definePlugin: () => definePlugin,
|
|
37
37
|
definePluginRealtimeTopic: () => definePluginRealtimeTopic,
|
|
38
|
+
getMobileHostServices: () => getMobileHostServices,
|
|
38
39
|
isMobileRendererCompatible: () => isMobileRendererCompatible,
|
|
39
40
|
isPluginApiCompatible: () => isPluginApiCompatible,
|
|
40
41
|
isPluginRuntimeShimSpecifier: () => isPluginRuntimeShimSpecifier,
|
|
41
42
|
parseSemver: () => parseSemver,
|
|
42
43
|
satisfiesSemverRange: () => satisfiesSemverRange,
|
|
44
|
+
setMobileHostServices: () => setMobileHostServices,
|
|
43
45
|
usePluginRealtime: () => usePluginRealtime
|
|
44
46
|
});
|
|
45
47
|
module.exports = __toCommonJS(plugin_sdk_exports);
|
|
46
48
|
|
|
47
49
|
// src/plugin-sdk/version.ts
|
|
48
50
|
var PLUGIN_API_VERSION = "0.1.0";
|
|
49
|
-
var MOBILE_RENDERER_VERSION = "0.
|
|
51
|
+
var MOBILE_RENDERER_VERSION = "0.3.0";
|
|
50
52
|
var SEMVER_REGEX = /^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?$/;
|
|
51
53
|
function parseSemver(version) {
|
|
52
54
|
const match = SEMVER_REGEX.exec(version);
|
|
@@ -363,6 +365,15 @@ function toRegistryRecord(styles) {
|
|
|
363
365
|
return out;
|
|
364
366
|
}
|
|
365
367
|
|
|
368
|
+
// src/plugin-sdk/host-services.ts
|
|
369
|
+
var registeredHostServices = null;
|
|
370
|
+
function setMobileHostServices(services) {
|
|
371
|
+
registeredHostServices = services;
|
|
372
|
+
}
|
|
373
|
+
function getMobileHostServices() {
|
|
374
|
+
return registeredHostServices;
|
|
375
|
+
}
|
|
376
|
+
|
|
366
377
|
// src/plugin-sdk/realtime.ts
|
|
367
378
|
var import_react = require("react");
|
|
368
379
|
var DEFAULT_ENDPOINT = "/api/plugins/events";
|
|
@@ -548,10 +559,12 @@ var PLUGIN_RUNTIME_IMPORT_MAP = Object.freeze(
|
|
|
548
559
|
defineMobilePlugin,
|
|
549
560
|
definePlugin,
|
|
550
561
|
definePluginRealtimeTopic,
|
|
562
|
+
getMobileHostServices,
|
|
551
563
|
isMobileRendererCompatible,
|
|
552
564
|
isPluginApiCompatible,
|
|
553
565
|
isPluginRuntimeShimSpecifier,
|
|
554
566
|
parseSemver,
|
|
555
567
|
satisfiesSemverRange,
|
|
568
|
+
setMobileHostServices,
|
|
556
569
|
usePluginRealtime
|
|
557
570
|
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// src/plugin-sdk/version.ts
|
|
2
2
|
var PLUGIN_API_VERSION = "0.1.0";
|
|
3
|
-
var MOBILE_RENDERER_VERSION = "0.
|
|
3
|
+
var MOBILE_RENDERER_VERSION = "0.3.0";
|
|
4
4
|
var SEMVER_REGEX = /^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?$/;
|
|
5
5
|
function parseSemver(version) {
|
|
6
6
|
const match = SEMVER_REGEX.exec(version);
|
|
@@ -317,6 +317,15 @@ function toRegistryRecord(styles) {
|
|
|
317
317
|
return out;
|
|
318
318
|
}
|
|
319
319
|
|
|
320
|
+
// src/plugin-sdk/host-services.ts
|
|
321
|
+
var registeredHostServices = null;
|
|
322
|
+
function setMobileHostServices(services) {
|
|
323
|
+
registeredHostServices = services;
|
|
324
|
+
}
|
|
325
|
+
function getMobileHostServices() {
|
|
326
|
+
return registeredHostServices;
|
|
327
|
+
}
|
|
328
|
+
|
|
320
329
|
// src/plugin-sdk/realtime.ts
|
|
321
330
|
import { useEffect, useRef, useState } from "react";
|
|
322
331
|
var DEFAULT_ENDPOINT = "/api/plugins/events";
|
|
@@ -501,10 +510,12 @@ export {
|
|
|
501
510
|
defineMobilePlugin,
|
|
502
511
|
definePlugin,
|
|
503
512
|
definePluginRealtimeTopic,
|
|
513
|
+
getMobileHostServices,
|
|
504
514
|
isMobileRendererCompatible,
|
|
505
515
|
isPluginApiCompatible,
|
|
506
516
|
isPluginRuntimeShimSpecifier,
|
|
507
517
|
parseSemver,
|
|
508
518
|
satisfiesSemverRange,
|
|
519
|
+
setMobileHostServices,
|
|
509
520
|
usePluginRealtime
|
|
510
521
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@selfhelp/shared",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.17.0",
|
|
4
4
|
"description": "Shared TypeScript package for SelfHelp web frontend and mobile app: style registry, types, API contracts, Mantine theme tokens, condition + interpolation engines, CMS class allow-list.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/*
|
|
2
|
+
SPDX-FileCopyrightText: 2026 Humdek, University of Bern
|
|
3
|
+
SPDX-License-Identifier: MPL-2.0
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Mobile host-services bridge contract (mobile renderer axis 0.3.0).
|
|
7
|
+
*
|
|
8
|
+
* The shared package only owns the *contract* + the singleton registry; the
|
|
9
|
+
* real 401-refresh / session-expired logic lives in the native host's
|
|
10
|
+
* implementation. These tests assert:
|
|
11
|
+
* - the singleton is null before a host registers and after it clears,
|
|
12
|
+
* - a registered host is returned to plugin code,
|
|
13
|
+
* - the documented response contract (ok / reason / sessionExpired) lets a
|
|
14
|
+
* plugin branch on lifecycle + refresh outcomes through a representative
|
|
15
|
+
* fake host that performs a single 401 -> refresh -> retry, and reports
|
|
16
|
+
* session-expired when the refresh itself fails,
|
|
17
|
+
* - `isMobileRendererCompatible` covers the bumped 0.3.0 host,
|
|
18
|
+
* - the host may expose the optional `navigate()` capability a plugin
|
|
19
|
+
* feature-detects to honour an in-content redirect through the host router.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
23
|
+
|
|
24
|
+
import {
|
|
25
|
+
type IMobileHostRequest,
|
|
26
|
+
type IMobileHostServices,
|
|
27
|
+
getMobileHostServices,
|
|
28
|
+
setMobileHostServices,
|
|
29
|
+
} from '../host-services';
|
|
30
|
+
import { MOBILE_RENDERER_VERSION, isMobileRendererCompatible } from '../version';
|
|
31
|
+
|
|
32
|
+
afterEach(() => {
|
|
33
|
+
setMobileHostServices(null);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
describe('mobile host-services singleton', () => {
|
|
37
|
+
it('is null until the host registers an implementation', () => {
|
|
38
|
+
expect(getMobileHostServices()).toBeNull();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('returns the host implementation the host registered', () => {
|
|
42
|
+
const services: IMobileHostServices = {
|
|
43
|
+
apiBaseUrl: () => 'https://cms.example.com',
|
|
44
|
+
getAccessToken: () => 'token-abc',
|
|
45
|
+
request: () => Promise.resolve({ ok: true, status: 200, data: null }),
|
|
46
|
+
};
|
|
47
|
+
setMobileHostServices(services);
|
|
48
|
+
expect(getMobileHostServices()).toBe(services);
|
|
49
|
+
expect(getMobileHostServices()?.apiBaseUrl()).toBe('https://cms.example.com');
|
|
50
|
+
expect(getMobileHostServices()?.getAccessToken()).toBe('token-abc');
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('clears back to null when the host deregisters (e.g. server switch)', () => {
|
|
54
|
+
setMobileHostServices({
|
|
55
|
+
apiBaseUrl: () => '',
|
|
56
|
+
getAccessToken: () => null,
|
|
57
|
+
request: () => Promise.resolve({ ok: true, status: 204, data: null }),
|
|
58
|
+
});
|
|
59
|
+
setMobileHostServices(null);
|
|
60
|
+
expect(getMobileHostServices()).toBeNull();
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('exposes the optional navigate() a renderer-0.3.0 host registers', () => {
|
|
64
|
+
const navigate = vi.fn();
|
|
65
|
+
setMobileHostServices({
|
|
66
|
+
apiBaseUrl: () => 'https://cms.example.com',
|
|
67
|
+
getAccessToken: () => null,
|
|
68
|
+
request: () => Promise.resolve({ ok: true, status: 200, data: null }),
|
|
69
|
+
navigate,
|
|
70
|
+
});
|
|
71
|
+
const host = getMobileHostServices();
|
|
72
|
+
expect(typeof host?.navigate).toBe('function');
|
|
73
|
+
host?.navigate?.('contact', false);
|
|
74
|
+
expect(navigate).toHaveBeenCalledWith('contact', false);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('omits navigate on an older host so a plugin must feature-detect it', () => {
|
|
78
|
+
setMobileHostServices({
|
|
79
|
+
apiBaseUrl: () => 'https://cms.example.com',
|
|
80
|
+
getAccessToken: () => null,
|
|
81
|
+
request: () => Promise.resolve({ ok: true, status: 200, data: null }),
|
|
82
|
+
});
|
|
83
|
+
expect(getMobileHostServices()?.navigate).toBeUndefined();
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
describe('host request contract (host owns token + refresh)', () => {
|
|
88
|
+
/**
|
|
89
|
+
* Representative host: the first protected call 401s, the host refreshes
|
|
90
|
+
* once and replays the request; if the refresh fails it reports
|
|
91
|
+
* `sessionExpired`. This mirrors the native axios interceptor so plugin
|
|
92
|
+
* code can be written against the contract.
|
|
93
|
+
*/
|
|
94
|
+
function makeHost(options: { refreshSucceeds: boolean }): {
|
|
95
|
+
services: IMobileHostServices;
|
|
96
|
+
calls: () => number;
|
|
97
|
+
} {
|
|
98
|
+
let token = 'expired';
|
|
99
|
+
let attempts = 0;
|
|
100
|
+
const refresh = vi.fn(() => {
|
|
101
|
+
if (!options.refreshSucceeds) return false;
|
|
102
|
+
token = 'fresh';
|
|
103
|
+
return true;
|
|
104
|
+
});
|
|
105
|
+
const services: IMobileHostServices = {
|
|
106
|
+
apiBaseUrl: () => 'https://cms.example.com',
|
|
107
|
+
getAccessToken: () => token,
|
|
108
|
+
request: async <TData>(_req: IMobileHostRequest) => {
|
|
109
|
+
attempts++;
|
|
110
|
+
if (token === 'expired') {
|
|
111
|
+
const recovered = refresh();
|
|
112
|
+
if (!recovered) {
|
|
113
|
+
return { ok: false, status: 401, data: null, sessionExpired: true } as const;
|
|
114
|
+
}
|
|
115
|
+
attempts++;
|
|
116
|
+
}
|
|
117
|
+
return { ok: true, status: 200, data: { data: { responseId: 'R_1' } } as TData };
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
return { services, calls: () => attempts };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
it('recovers a 401 with a single refresh + retry and returns the payload', async () => {
|
|
124
|
+
const { services, calls } = makeHost({ refreshSucceeds: true });
|
|
125
|
+
setMobileHostServices(services);
|
|
126
|
+
|
|
127
|
+
const res = await getMobileHostServices()!.request<{ data: { responseId: string } }>({
|
|
128
|
+
path: '/cms-api/v1/plugins/sh2-shp-survey-js/published/key/submit',
|
|
129
|
+
method: 'POST',
|
|
130
|
+
body: { answers: {} },
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
expect(res.ok).toBe(true);
|
|
134
|
+
expect(res.status).toBe(200);
|
|
135
|
+
expect(res.data?.data.responseId).toBe('R_1');
|
|
136
|
+
expect(calls()).toBe(2); // initial 401 + replay
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('reports sessionExpired when the refresh itself fails', async () => {
|
|
140
|
+
const { services } = makeHost({ refreshSucceeds: false });
|
|
141
|
+
setMobileHostServices(services);
|
|
142
|
+
|
|
143
|
+
const res = await getMobileHostServices()!.request({
|
|
144
|
+
path: '/cms-api/v1/plugins/sh2-shp-survey-js/published/key/progress',
|
|
145
|
+
method: 'PUT',
|
|
146
|
+
body: {},
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
expect(res.ok).toBe(false);
|
|
150
|
+
expect(res.status).toBe(401);
|
|
151
|
+
expect(res.sessionExpired).toBe(true);
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
describe('mobile renderer compatibility at the bumped version', () => {
|
|
156
|
+
it('advertises 0.3.0 and gates plugin compatibility.mobile ranges against it', () => {
|
|
157
|
+
expect(MOBILE_RENDERER_VERSION).toBe('0.3.0');
|
|
158
|
+
expect(isMobileRendererCompatible('^0.3.0')).toBe(true);
|
|
159
|
+
expect(isMobileRendererCompatible('>=0.2.0')).toBe(true);
|
|
160
|
+
expect(isMobileRendererCompatible('^0.1.0')).toBe(false);
|
|
161
|
+
});
|
|
162
|
+
});
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/*
|
|
2
|
+
SPDX-FileCopyrightText: 2026 Humdek, University of Bern
|
|
3
|
+
SPDX-License-Identifier: MPL-2.0
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Mobile host-services bridge (mobile renderer contract, `MOBILE_RENDERER_VERSION`).
|
|
7
|
+
*
|
|
8
|
+
* A plugin's mobile package renders its UI (commonly inside an isolated
|
|
9
|
+
* WebView), but it MUST NOT own authenticated backend access. Instead the
|
|
10
|
+
* native mobile host registers an `IMobileHostServices` implementation at
|
|
11
|
+
* boot, and the plugin performs every protected API call through it. This
|
|
12
|
+
* keeps the access token, the 401-refresh round-trip, session-expiry, the
|
|
13
|
+
* API base URL, and client headers (`X-Client-Type: mobile`, locale) in the
|
|
14
|
+
* host — never inside plugin/WebView code.
|
|
15
|
+
*
|
|
16
|
+
* Design constraints:
|
|
17
|
+
* - Framework-agnostic: no `react` / `react-native` / `axios` imports here.
|
|
18
|
+
* The host wires its own HTTP client (e.g. the shared axios singleton)
|
|
19
|
+
* into `request`.
|
|
20
|
+
* - Singleton: the host calls {@link setMobileHostServices} once during
|
|
21
|
+
* boot; the plugin calls {@link getMobileHostServices} at runtime.
|
|
22
|
+
* - The token is intentionally NOT a required input to plugin calls. A
|
|
23
|
+
* plugin may read {@link IMobileHostServices.getAccessToken} only for
|
|
24
|
+
* non-sensitive bootstrapping; it must never implement its own refresh.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/** HTTP method accepted by the host request bridge. */
|
|
28
|
+
export type TMobileHostMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* A protected backend request the plugin asks the host to perform. The
|
|
32
|
+
* host prepends {@link IMobileHostServices.apiBaseUrl}, attaches the bearer
|
|
33
|
+
* token + client headers, and handles a single 401-refresh retry.
|
|
34
|
+
*/
|
|
35
|
+
export interface IMobileHostRequest {
|
|
36
|
+
/**
|
|
37
|
+
* Path relative to the host API base, e.g.
|
|
38
|
+
* `/cms-api/v1/plugins/<id>/published/<key>`. Absolute URLs are rejected
|
|
39
|
+
* by the host so a plugin cannot redirect protected calls off-origin.
|
|
40
|
+
*/
|
|
41
|
+
path: string;
|
|
42
|
+
/** HTTP method. Defaults to `GET`. */
|
|
43
|
+
method?: TMobileHostMethod;
|
|
44
|
+
/** JSON-serialisable request body (sent as `application/json`). */
|
|
45
|
+
body?: unknown;
|
|
46
|
+
/** Extra request headers (e.g. a plugin runtime-config echo header). */
|
|
47
|
+
headers?: Record<string, string>;
|
|
48
|
+
/** Query parameters appended to the path. */
|
|
49
|
+
query?: Record<string, string | number | boolean>;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Result of a host request. `data` is the parsed response body (the SelfHelp
|
|
54
|
+
* API envelope `{ status, message, data, ... }` for API routes), so the
|
|
55
|
+
* plugin reads `response.data.data` for the inner payload. `reason` mirrors
|
|
56
|
+
* the backend error discriminator (e.g. `already_submitted_once`) so the
|
|
57
|
+
* plugin can branch on lifecycle errors without re-parsing the body.
|
|
58
|
+
*/
|
|
59
|
+
export interface IMobileHostResponse<TData = unknown> {
|
|
60
|
+
/** True for 2xx responses. */
|
|
61
|
+
ok: boolean;
|
|
62
|
+
/** HTTP status code (0 when the request never reached the server). */
|
|
63
|
+
status: number;
|
|
64
|
+
/** Parsed response body, or `null` when there is no JSON body. */
|
|
65
|
+
data: TData | null;
|
|
66
|
+
/** Backend error `reason` discriminator, when present. */
|
|
67
|
+
reason?: string;
|
|
68
|
+
/** Human-readable error message for non-ok responses. */
|
|
69
|
+
error?: string;
|
|
70
|
+
/**
|
|
71
|
+
* True when the call failed because the session could not be
|
|
72
|
+
* authenticated even after the host's refresh attempt (a 401 the host
|
|
73
|
+
* could not recover). The plugin should surface a "session expired"
|
|
74
|
+
* state rather than a generic error.
|
|
75
|
+
*/
|
|
76
|
+
sessionExpired?: boolean;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Authenticated host services exposed to a plugin's mobile package. The
|
|
81
|
+
* host owns the token + refresh; the plugin only describes the request.
|
|
82
|
+
*/
|
|
83
|
+
export interface IMobileHostServices {
|
|
84
|
+
/** Resolved API base URL the host calls (e.g. `https://cms.example.com`). */
|
|
85
|
+
apiBaseUrl(): string;
|
|
86
|
+
/**
|
|
87
|
+
* Current access token, or `null` when unauthenticated. Exposed only for
|
|
88
|
+
* non-sensitive bootstrapping (e.g. deciding whether to show a login
|
|
89
|
+
* prompt). Plugins must route protected calls through {@link request},
|
|
90
|
+
* which attaches the token host-side, instead of injecting this value.
|
|
91
|
+
*/
|
|
92
|
+
getAccessToken(): string | null;
|
|
93
|
+
/** Perform an authenticated request with host-managed 401 refresh. */
|
|
94
|
+
request<TData = unknown>(req: IMobileHostRequest): Promise<IMobileHostResponse<TData>>;
|
|
95
|
+
/**
|
|
96
|
+
* Navigate the host app on the user's behalf to honour an in-content
|
|
97
|
+
* redirect (e.g. a survey's "redirect on completion") WITHOUT the plugin
|
|
98
|
+
* touching `window.location`. The host owns the router, so it routes an
|
|
99
|
+
* internal CMS page keyword / in-app path through its own navigation stack
|
|
100
|
+
* and only leaves the app for an explicit external URL. This keeps redirects
|
|
101
|
+
* correct in EVERY host surface — the native app AND the CMS web preview,
|
|
102
|
+
* where a raw `location` assignment would navigate (and break) the embedded
|
|
103
|
+
* iframe instead of the app.
|
|
104
|
+
*
|
|
105
|
+
* @param target CMS page keyword / in-app path, or an absolute URL when
|
|
106
|
+
* `external` is true.
|
|
107
|
+
* @param external When true the target is a full off-app URL the host opens
|
|
108
|
+
* in the system browser (native) / a new tab (web) instead of
|
|
109
|
+
* routing inside the app. Defaults to false.
|
|
110
|
+
*
|
|
111
|
+
* Optional: a host registered by an older app binary (renderer < 0.3.0) may
|
|
112
|
+
* omit it, so a plugin MUST feature-detect (`if (host.navigate)`) and degrade
|
|
113
|
+
* gracefully rather than assume it exists.
|
|
114
|
+
*/
|
|
115
|
+
navigate?(target: string, external?: boolean): void;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
let registeredHostServices: IMobileHostServices | null = null;
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Register the host services implementation. Called once by the native
|
|
122
|
+
* mobile host during boot (before any plugin renders). Re-registering
|
|
123
|
+
* replaces the previous implementation (e.g. on a server switch).
|
|
124
|
+
*/
|
|
125
|
+
export function setMobileHostServices(services: IMobileHostServices | null): void {
|
|
126
|
+
registeredHostServices = services;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Read the registered host services. Returns `null` when no host has
|
|
131
|
+
* registered yet — a plugin must treat that as "host too old / not ready"
|
|
132
|
+
* and degrade gracefully (it must never fall back to its own token logic).
|
|
133
|
+
*/
|
|
134
|
+
export function getMobileHostServices(): IMobileHostServices | null {
|
|
135
|
+
return registeredHostServices;
|
|
136
|
+
}
|
package/src/plugin-sdk/index.ts
CHANGED
|
@@ -88,6 +88,14 @@ export type {
|
|
|
88
88
|
|
|
89
89
|
export { definePlugin, defineMobilePlugin, definePluginRealtimeTopic } from './define';
|
|
90
90
|
|
|
91
|
+
export type {
|
|
92
|
+
IMobileHostServices,
|
|
93
|
+
IMobileHostRequest,
|
|
94
|
+
IMobileHostResponse,
|
|
95
|
+
TMobileHostMethod,
|
|
96
|
+
} from './host-services';
|
|
97
|
+
export { setMobileHostServices, getMobileHostServices } from './host-services';
|
|
98
|
+
|
|
91
99
|
export type {
|
|
92
100
|
IRealtimeTransport,
|
|
93
101
|
TRealtimeTransportFactory,
|
|
@@ -31,8 +31,19 @@ export const PLUGIN_API_VERSION = '0.1.0' as const;
|
|
|
31
31
|
*
|
|
32
32
|
* Bump rules mirror the SDK contract: patch = additive fix, minor = additive
|
|
33
33
|
* renderer feature, major = breaking mobile renderer change.
|
|
34
|
+
*
|
|
35
|
+
* `0.2.0` adds the typed mobile host-services bridge (`IMobileHostServices` +
|
|
36
|
+
* `set/getMobileHostServices`) so a plugin's mobile package routes protected
|
|
37
|
+
* API calls through the native host (token + 401-refresh owned host-side)
|
|
38
|
+
* instead of injecting raw tokens — an additive renderer feature.
|
|
39
|
+
*
|
|
40
|
+
* `0.3.0` adds the optional `IMobileHostServices.navigate(target, external)`
|
|
41
|
+
* host-navigation capability so a plugin can honour an in-content redirect
|
|
42
|
+
* through the host router (correct in the native app AND the CMS web preview
|
|
43
|
+
* iframe) instead of touching `window.location` — an additive renderer feature.
|
|
44
|
+
* It is optional, so older plugins/hosts stay compatible.
|
|
34
45
|
*/
|
|
35
|
-
export const MOBILE_RENDERER_VERSION = '0.
|
|
46
|
+
export const MOBILE_RENDERER_VERSION = '0.3.0' as const;
|
|
36
47
|
|
|
37
48
|
export interface ISemver {
|
|
38
49
|
major: number;
|