@greensecurity/javascript-sdk 0.46.0-beta.10 → 0.46.0-beta.12
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/commonjs/__tests__/apps/contract.test.js +288 -4
- package/dist/commonjs/__tests__/apps/contract.test.js.map +1 -1
- package/dist/commonjs/__tests__/apps/devpreview-routes.test.d.ts +2 -0
- package/dist/commonjs/__tests__/apps/devpreview-routes.test.d.ts.map +1 -0
- package/dist/commonjs/__tests__/apps/devpreview-routes.test.js +86 -0
- package/dist/commonjs/__tests__/apps/devpreview-routes.test.js.map +1 -0
- package/dist/commonjs/__tests__/apps/kit/example.test.js +84 -4
- package/dist/commonjs/__tests__/apps/kit/example.test.js.map +1 -1
- package/dist/commonjs/__tests__/apps/kit/pages.test.d.ts +2 -0
- package/dist/commonjs/__tests__/apps/kit/pages.test.d.ts.map +1 -0
- package/dist/commonjs/__tests__/apps/kit/pages.test.js +119 -0
- package/dist/commonjs/__tests__/apps/kit/pages.test.js.map +1 -0
- package/dist/commonjs/__tests__/apps/kit/render.test.js +24 -0
- package/dist/commonjs/__tests__/apps/kit/render.test.js.map +1 -1
- package/dist/commonjs/__tests__/apps/kit/store.test.d.ts +2 -0
- package/dist/commonjs/__tests__/apps/kit/store.test.d.ts.map +1 -0
- package/dist/commonjs/__tests__/apps/kit/store.test.js +123 -0
- package/dist/commonjs/__tests__/apps/kit/store.test.js.map +1 -0
- package/dist/commonjs/__tests__/apps/post-install-page.test.d.ts +2 -0
- package/dist/commonjs/__tests__/apps/post-install-page.test.d.ts.map +1 -0
- package/dist/commonjs/__tests__/apps/post-install-page.test.js +110 -0
- package/dist/commonjs/__tests__/apps/post-install-page.test.js.map +1 -0
- package/dist/commonjs/__tests__/apps/routes.test.d.ts +2 -0
- package/dist/commonjs/__tests__/apps/routes.test.d.ts.map +1 -0
- package/dist/commonjs/__tests__/apps/routes.test.js +238 -0
- package/dist/commonjs/__tests__/apps/routes.test.js.map +1 -0
- package/dist/commonjs/__tests__/vendors.test.js +1 -1
- package/dist/commonjs/__tests__/webhooks.test.js +1 -1
- package/dist/commonjs/__tests__/webhooks.test.js.map +1 -1
- package/dist/commonjs/__tests__/zones.test.js +22 -4
- package/dist/commonjs/__tests__/zones.test.js.map +1 -1
- package/dist/commonjs/apps/components.d.ts +321 -0
- package/dist/commonjs/apps/components.d.ts.map +1 -1
- package/dist/commonjs/apps/components.js +168 -2
- package/dist/commonjs/apps/components.js.map +1 -1
- package/dist/commonjs/apps/context.d.ts +24 -0
- package/dist/commonjs/apps/context.d.ts.map +1 -1
- package/dist/commonjs/apps/context.js.map +1 -1
- package/dist/commonjs/apps/devpreview.d.ts +20 -3
- package/dist/commonjs/apps/devpreview.d.ts.map +1 -1
- package/dist/commonjs/apps/devpreview.js +18 -7
- package/dist/commonjs/apps/devpreview.js.map +1 -1
- package/dist/commonjs/apps/hooks/index.d.ts +33 -2
- package/dist/commonjs/apps/hooks/index.d.ts.map +1 -1
- package/dist/commonjs/apps/hooks/index.js +41 -1
- package/dist/commonjs/apps/hooks/index.js.map +1 -1
- package/dist/commonjs/apps/hooks/store.d.ts +48 -0
- package/dist/commonjs/apps/hooks/store.d.ts.map +1 -0
- package/dist/commonjs/apps/hooks/store.js +87 -0
- package/dist/commonjs/apps/hooks/store.js.map +1 -0
- package/dist/commonjs/apps/manifest.d.ts +64 -3
- package/dist/commonjs/apps/manifest.d.ts.map +1 -1
- package/dist/commonjs/apps/manifest.js +128 -27
- package/dist/commonjs/apps/manifest.js.map +1 -1
- package/dist/commonjs/apps/protocol.d.ts +1 -1
- package/dist/commonjs/apps/protocol.d.ts.map +1 -1
- package/dist/commonjs/apps/route.d.ts +11 -1
- package/dist/commonjs/apps/route.d.ts.map +1 -1
- package/dist/commonjs/apps/route.js +13 -7
- package/dist/commonjs/apps/route.js.map +1 -1
- package/dist/commonjs/apps/routes.d.ts +65 -0
- package/dist/commonjs/apps/routes.d.ts.map +1 -0
- package/dist/commonjs/apps/routes.js +202 -0
- package/dist/commonjs/apps/routes.js.map +1 -0
- package/dist/commonjs/apps/surfaces.d.ts +20 -2
- package/dist/commonjs/apps/surfaces.d.ts.map +1 -1
- package/dist/commonjs/apps/surfaces.js +45 -7
- package/dist/commonjs/apps/surfaces.js.map +1 -1
- package/dist/commonjs/apps/testing/index.d.ts +17 -1
- package/dist/commonjs/apps/testing/index.d.ts.map +1 -1
- package/dist/commonjs/apps/testing/index.js +50 -1
- package/dist/commonjs/apps/testing/index.js.map +1 -1
- package/dist/commonjs/apps/ui/index.d.ts +13 -0
- package/dist/commonjs/apps/ui/index.d.ts.map +1 -1
- package/dist/commonjs/apps/ui/index.js +19 -2
- package/dist/commonjs/apps/ui/index.js.map +1 -1
- package/dist/commonjs/lib/config.d.ts +2 -2
- package/dist/commonjs/lib/config.js +2 -2
- package/dist/esm/__tests__/apps/contract.test.js +289 -5
- package/dist/esm/__tests__/apps/contract.test.js.map +1 -1
- package/dist/esm/__tests__/apps/devpreview-routes.test.d.ts +2 -0
- package/dist/esm/__tests__/apps/devpreview-routes.test.d.ts.map +1 -0
- package/dist/esm/__tests__/apps/devpreview-routes.test.js +84 -0
- package/dist/esm/__tests__/apps/devpreview-routes.test.js.map +1 -0
- package/dist/esm/__tests__/apps/kit/example.test.js +85 -5
- package/dist/esm/__tests__/apps/kit/example.test.js.map +1 -1
- package/dist/esm/__tests__/apps/kit/pages.test.d.ts +2 -0
- package/dist/esm/__tests__/apps/kit/pages.test.d.ts.map +1 -0
- package/dist/esm/__tests__/apps/kit/pages.test.js +117 -0
- package/dist/esm/__tests__/apps/kit/pages.test.js.map +1 -0
- package/dist/esm/__tests__/apps/kit/render.test.js +25 -1
- package/dist/esm/__tests__/apps/kit/render.test.js.map +1 -1
- package/dist/esm/__tests__/apps/kit/store.test.d.ts +2 -0
- package/dist/esm/__tests__/apps/kit/store.test.d.ts.map +1 -0
- package/dist/esm/__tests__/apps/kit/store.test.js +121 -0
- package/dist/esm/__tests__/apps/kit/store.test.js.map +1 -0
- package/dist/esm/__tests__/apps/post-install-page.test.d.ts +2 -0
- package/dist/esm/__tests__/apps/post-install-page.test.d.ts.map +1 -0
- package/dist/esm/__tests__/apps/post-install-page.test.js +108 -0
- package/dist/esm/__tests__/apps/post-install-page.test.js.map +1 -0
- package/dist/esm/__tests__/apps/routes.test.d.ts +2 -0
- package/dist/esm/__tests__/apps/routes.test.d.ts.map +1 -0
- package/dist/esm/__tests__/apps/routes.test.js +236 -0
- package/dist/esm/__tests__/apps/routes.test.js.map +1 -0
- package/dist/esm/__tests__/vendors.test.js +1 -1
- package/dist/esm/__tests__/webhooks.test.js +1 -1
- package/dist/esm/__tests__/webhooks.test.js.map +1 -1
- package/dist/esm/__tests__/zones.test.js +22 -4
- package/dist/esm/__tests__/zones.test.js.map +1 -1
- package/dist/esm/apps/components.d.ts +321 -0
- package/dist/esm/apps/components.d.ts.map +1 -1
- package/dist/esm/apps/components.js +163 -1
- package/dist/esm/apps/components.js.map +1 -1
- package/dist/esm/apps/context.d.ts +24 -0
- package/dist/esm/apps/context.d.ts.map +1 -1
- package/dist/esm/apps/context.js.map +1 -1
- package/dist/esm/apps/devpreview.d.ts +20 -3
- package/dist/esm/apps/devpreview.d.ts.map +1 -1
- package/dist/esm/apps/devpreview.js +19 -8
- package/dist/esm/apps/devpreview.js.map +1 -1
- package/dist/esm/apps/hooks/index.d.ts +33 -2
- package/dist/esm/apps/hooks/index.d.ts.map +1 -1
- package/dist/esm/apps/hooks/index.js +37 -1
- package/dist/esm/apps/hooks/index.js.map +1 -1
- package/dist/esm/apps/hooks/store.d.ts +48 -0
- package/dist/esm/apps/hooks/store.d.ts.map +1 -0
- package/dist/esm/apps/hooks/store.js +84 -0
- package/dist/esm/apps/hooks/store.js.map +1 -0
- package/dist/esm/apps/manifest.d.ts +64 -3
- package/dist/esm/apps/manifest.d.ts.map +1 -1
- package/dist/esm/apps/manifest.js +116 -26
- package/dist/esm/apps/manifest.js.map +1 -1
- package/dist/esm/apps/protocol.d.ts +1 -1
- package/dist/esm/apps/protocol.d.ts.map +1 -1
- package/dist/esm/apps/route.d.ts +11 -1
- package/dist/esm/apps/route.d.ts.map +1 -1
- package/dist/esm/apps/route.js +14 -8
- package/dist/esm/apps/route.js.map +1 -1
- package/dist/esm/apps/routes.d.ts +65 -0
- package/dist/esm/apps/routes.d.ts.map +1 -0
- package/dist/esm/apps/routes.js +194 -0
- package/dist/esm/apps/routes.js.map +1 -0
- package/dist/esm/apps/surfaces.d.ts +20 -2
- package/dist/esm/apps/surfaces.d.ts.map +1 -1
- package/dist/esm/apps/surfaces.js +39 -2
- package/dist/esm/apps/surfaces.js.map +1 -1
- package/dist/esm/apps/testing/index.d.ts +17 -1
- package/dist/esm/apps/testing/index.d.ts.map +1 -1
- package/dist/esm/apps/testing/index.js +49 -1
- package/dist/esm/apps/testing/index.js.map +1 -1
- package/dist/esm/apps/ui/index.d.ts +13 -0
- package/dist/esm/apps/ui/index.d.ts.map +1 -1
- package/dist/esm/apps/ui/index.js +17 -1
- package/dist/esm/apps/ui/index.js.map +1 -1
- package/dist/esm/lib/config.d.ts +2 -2
- package/dist/esm/lib/config.js +2 -2
- package/examples/apps/hello-green/src/store.ts +15 -0
- package/jsr.json +1 -1
- package/package.json +1 -1
- package/src/__tests__/apps/contract.test.ts +305 -4
- package/src/__tests__/apps/devpreview-routes.test.ts +103 -0
- package/src/__tests__/apps/kit/example.test.ts +94 -5
- package/src/__tests__/apps/kit/pages.test.ts +137 -0
- package/src/__tests__/apps/kit/render.test.ts +35 -1
- package/src/__tests__/apps/kit/store.test.ts +149 -0
- package/src/__tests__/apps/post-install-page.test.ts +126 -0
- package/src/__tests__/apps/routes.test.ts +318 -0
- package/src/__tests__/vendors.test.ts +1 -1
- package/src/__tests__/webhooks.test.ts +1 -1
- package/src/__tests__/zones.test.ts +22 -4
- package/src/apps/components.ts +183 -1
- package/src/apps/context.ts +25 -0
- package/src/apps/devpreview.ts +20 -7
- package/src/apps/hooks/index.ts +42 -2
- package/src/apps/hooks/store.ts +115 -0
- package/src/apps/manifest.ts +151 -27
- package/src/apps/protocol.ts +1 -1
- package/src/apps/route.ts +22 -10
- package/src/apps/routes.ts +230 -0
- package/src/apps/surfaces.ts +45 -12
- package/src/apps/testing/index.ts +48 -2
- package/src/apps/ui/index.ts +18 -1
- package/src/lib/config.ts +2 -2
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Hand-written (not generated by Speakeasy). Part of the third-party app platform contract;
|
|
3
|
+
* see docs/third-party-apps/fullpageview-plan.md.
|
|
4
|
+
*
|
|
5
|
+
* App page routes (`ui_extensions.routes[]`): the path grammar, the uniqueness rule and the one
|
|
6
|
+
* matcher every consumer shares (the host, `apps-cli validate`, the contract tests). Deliberately
|
|
7
|
+
* dependency-free (no zod) so the host renderer can import it without bundling zod; `manifest.ts`
|
|
8
|
+
* re-exports it because it is part of the manifest contract.
|
|
9
|
+
*
|
|
10
|
+
* The server mirrors the grammar and `normalizeRoutePath` in Ruby and reads the same fixtures
|
|
11
|
+
* (`src/__tests__/apps/fixtures/routes.json`).
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** A static segment: lower-case letters, digits and inner hyphens. */
|
|
15
|
+
const STATIC_SEGMENT = "[a-z0-9](?:[a-z0-9-]*[a-z0-9])?";
|
|
16
|
+
/** A param segment: `:` and a lower-case name. */
|
|
17
|
+
const PARAM_SEGMENT = ":[a-z][a-z0-9_]*";
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A declared route path: `/`, or `/seg(/seg)*` where each segment is static or a `:param`. No dots,
|
|
21
|
+
* percent-escapes, upper case, empty segments or trailing slash, so no path can climb out of the
|
|
22
|
+
* app's own space. `pages` is not reserved inside that space: `/pages/foo` is an ordinary path.
|
|
23
|
+
*/
|
|
24
|
+
export const ROUTE_PATH_PATTERN =
|
|
25
|
+
"^/(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|:[a-z][a-z0-9_]*)(?:/(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|:[a-z][a-z0-9_]*))*)?$";
|
|
26
|
+
|
|
27
|
+
/** A concrete path an app navigates to (`{ type: "appPage" }`): the same grammar without params. */
|
|
28
|
+
export const CONCRETE_ROUTE_PATH_PATTERN = `^/(?:${STATIC_SEGMENT}(?:/${STATIC_SEGMENT})*)?$`;
|
|
29
|
+
|
|
30
|
+
export const ROUTE_PATH_MAX_CHARS = 200;
|
|
31
|
+
/** Segments in one path; `/` has none. */
|
|
32
|
+
export const ROUTE_PATH_MAX_SEGMENTS = 8;
|
|
33
|
+
/** Routes in one manifest. */
|
|
34
|
+
export const APP_ROUTES_MAX = 50;
|
|
35
|
+
|
|
36
|
+
/** What every param segment becomes in {@link normalizeRoutePath}. */
|
|
37
|
+
const PARAM_PLACEHOLDER = ":_";
|
|
38
|
+
|
|
39
|
+
const ROUTE_PATH_RE = new RegExp(ROUTE_PATH_PATTERN);
|
|
40
|
+
const CONCRETE_ROUTE_PATH_RE = new RegExp(CONCRETE_ROUTE_PATH_PATTERN);
|
|
41
|
+
const PARAM_SEGMENT_RE = new RegExp(`^${PARAM_SEGMENT}$`);
|
|
42
|
+
|
|
43
|
+
/** The segments of a path in the grammar; `/` has none. */
|
|
44
|
+
function segmentsOf(path: string): string[] {
|
|
45
|
+
return path === "/" ? [] : path.slice(1).split("/");
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function withinLimits(path: string): boolean {
|
|
49
|
+
return path.length <= ROUTE_PATH_MAX_CHARS && segmentsOf(path).length <= ROUTE_PATH_MAX_SEGMENTS;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** A declared route path: the {@link ROUTE_PATH_PATTERN} grammar, ≤ 200 characters and ≤ 8 segments. */
|
|
53
|
+
export function isRoutePath(value: unknown): value is string {
|
|
54
|
+
return typeof value === "string" && ROUTE_PATH_RE.test(value) && withinLimits(value);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** A concrete route path (no `:param` segments) within the same limits as a declared one. */
|
|
58
|
+
export function isConcreteRoutePath(value: unknown): value is string {
|
|
59
|
+
return typeof value === "string" && CONCRETE_ROUTE_PATH_RE.test(value) && withinLimits(value);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Renames every param segment to `:_`. Two paths that normalize to the same string claim the same
|
|
64
|
+
* URLs (`/vendors/:id` and `/vendors/:vendor_id`), so a manifest may declare only one of them.
|
|
65
|
+
*/
|
|
66
|
+
export function normalizeRoutePath(path: string): string {
|
|
67
|
+
return path
|
|
68
|
+
.split("/")
|
|
69
|
+
.map((segment) => (segment.startsWith(":") ? PARAM_PLACEHOLDER : segment))
|
|
70
|
+
.join("/");
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface RouteSetIssue {
|
|
74
|
+
/** Index of the offending entry in `routes`. */
|
|
75
|
+
index: number;
|
|
76
|
+
field: "path" | "portals" | "navigation";
|
|
77
|
+
message: string;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The rules over a set of routes that a pattern alone cannot express: the segment cap, a param
|
|
82
|
+
* name used twice in one path, two paths claiming the same URLs, and a repeated portal. Grammar,
|
|
83
|
+
* length and the per-field limits are the schema's job; a path that fails the grammar is skipped
|
|
84
|
+
* here. The zod schema and `apps-cli validate` both report these, and the server repeats them.
|
|
85
|
+
*/
|
|
86
|
+
export function routeSetIssues(
|
|
87
|
+
routes: ReadonlyArray<{ path: string; portals?: readonly string[] | undefined; navigation?: unknown }>,
|
|
88
|
+
): RouteSetIssue[] {
|
|
89
|
+
const issues: RouteSetIssue[] = [];
|
|
90
|
+
const claimed = new Map<string, string>();
|
|
91
|
+
|
|
92
|
+
routes.forEach((route, index) => {
|
|
93
|
+
const portals = route.portals ?? [];
|
|
94
|
+
if (new Set(portals).size !== portals.length) {
|
|
95
|
+
issues.push({ index, field: "portals", message: "portals must not repeat" });
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (!ROUTE_PATH_RE.test(route.path)) return;
|
|
99
|
+
|
|
100
|
+
const segments = segmentsOf(route.path);
|
|
101
|
+
if (segments.length > ROUTE_PATH_MAX_SEGMENTS) {
|
|
102
|
+
issues.push({
|
|
103
|
+
index,
|
|
104
|
+
field: "path",
|
|
105
|
+
message: `${route.path} has ${segments.length} segments; the limit is ${ROUTE_PATH_MAX_SEGMENTS}`,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const params = segments.filter((s) => s.startsWith(":"));
|
|
110
|
+
if (new Set(params).size !== params.length) {
|
|
111
|
+
issues.push({ index, field: "path", message: `${route.path} uses a param name twice` });
|
|
112
|
+
}
|
|
113
|
+
// A sidebar entry links to one fixed address, and a path with params has none.
|
|
114
|
+
if (params.length > 0 && route.navigation !== undefined) {
|
|
115
|
+
issues.push({
|
|
116
|
+
index,
|
|
117
|
+
field: "navigation",
|
|
118
|
+
message: `${route.path} has params, so it cannot have a navigation entry; add one to a path without params`,
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const normalized = normalizeRoutePath(route.path);
|
|
123
|
+
const earlier = claimed.get(normalized);
|
|
124
|
+
if (earlier === undefined) {
|
|
125
|
+
claimed.set(normalized, route.path);
|
|
126
|
+
} else {
|
|
127
|
+
issues.push({
|
|
128
|
+
index,
|
|
129
|
+
field: "path",
|
|
130
|
+
message: `${route.path} is declared twice (as ${earlier}); each path takes one page`,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
return issues;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export interface AppRouteMatch<R> {
|
|
139
|
+
route: R;
|
|
140
|
+
/** Param values by name, percent-decoded. Always strings: the app parses ids itself. */
|
|
141
|
+
params: Record<string, string>;
|
|
142
|
+
/** Index of `route` in the list passed to {@link matchAppRoute}. */
|
|
143
|
+
index: number;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Decodes one pathname segment into a param value, or returns `null` when it cannot be one: a
|
|
148
|
+
* malformed escape, an empty value, `.` or `..`, or a value containing `/` or `:` (raw or
|
|
149
|
+
* encoded). A param therefore never carries a path or a host-looking value into the app.
|
|
150
|
+
*/
|
|
151
|
+
function paramValue(segment: string): string | null {
|
|
152
|
+
if (segment.includes(":")) return null;
|
|
153
|
+
let value: string;
|
|
154
|
+
try {
|
|
155
|
+
value = decodeURIComponent(segment);
|
|
156
|
+
} catch {
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
if (value === "" || value === "." || value === ".." || value.includes("/") || value.includes(":")) return null;
|
|
160
|
+
return value;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Finds the route a pathname in the app's own space renders (`/reports/7` for
|
|
165
|
+
* `/{portal}/apps/{appId}/pages/reports/7`), or `null` when none does.
|
|
166
|
+
*
|
|
167
|
+
* `pathname` is URL-encoded, as it appears in a URL: `/` or `/seg(/seg)*` with no empty segment,
|
|
168
|
+
* trailing slash, query or fragment (anything else matches nothing). A host that receives decoded
|
|
169
|
+
* segments encodes each with `encodeURIComponent` before joining them.
|
|
170
|
+
*
|
|
171
|
+
* A static route segment matches only the identical pathname segment. A param segment matches any
|
|
172
|
+
* other segment that decodes to a plain value (see `paramValue`); a segment containing `:` never
|
|
173
|
+
* matches a param. When several routes match, the one with a static segment at the first position
|
|
174
|
+
* where they differ wins (static before param, left to right); among equals, the first declared.
|
|
175
|
+
* Routes whose path is not in the grammar are ignored, so a stale or hand-edited feed entry can
|
|
176
|
+
* never match.
|
|
177
|
+
*/
|
|
178
|
+
export function matchAppRoute<R extends { path: string }>(
|
|
179
|
+
routes: readonly R[],
|
|
180
|
+
pathname: string,
|
|
181
|
+
): AppRouteMatch<R> | null {
|
|
182
|
+
if (!pathname.startsWith("/") || /[?#]/.test(pathname)) return null;
|
|
183
|
+
const segments = segmentsOf(pathname);
|
|
184
|
+
if (segments.some((s) => s === "")) return null;
|
|
185
|
+
|
|
186
|
+
let best: { match: AppRouteMatch<R>; rank: boolean[] } | null = null;
|
|
187
|
+
|
|
188
|
+
for (const [index, route] of routes.entries()) {
|
|
189
|
+
const candidate = matchOne(route, index, segments);
|
|
190
|
+
if (candidate !== null && (best === null || outranks(candidate.rank, best.rank))) best = candidate;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return best === null ? null : best.match;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** Matches one route against the pathname's segments, with its static/param rank. */
|
|
197
|
+
function matchOne<R extends { path: string }>(
|
|
198
|
+
route: R,
|
|
199
|
+
index: number,
|
|
200
|
+
segments: readonly string[],
|
|
201
|
+
): { match: AppRouteMatch<R>; rank: boolean[] } | null {
|
|
202
|
+
if (!isRoutePath(route.path)) return null;
|
|
203
|
+
const pattern = segmentsOf(route.path);
|
|
204
|
+
if (pattern.length !== segments.length) return null;
|
|
205
|
+
|
|
206
|
+
const params: Record<string, string> = {};
|
|
207
|
+
const rank: boolean[] = [];
|
|
208
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
209
|
+
const expected = pattern[i] ?? "";
|
|
210
|
+
const actual = segments[i] ?? "";
|
|
211
|
+
if (PARAM_SEGMENT_RE.test(expected)) {
|
|
212
|
+
const value = paramValue(actual);
|
|
213
|
+
if (value === null) return null;
|
|
214
|
+
params[expected.slice(1)] = value;
|
|
215
|
+
rank.push(false);
|
|
216
|
+
} else {
|
|
217
|
+
if (expected !== actual) return null;
|
|
218
|
+
rank.push(true);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
return { match: { route, params, index }, rank };
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** Whether `a` has a static segment at the first position where it and `b` differ. */
|
|
225
|
+
function outranks(a: readonly boolean[], b: readonly boolean[]): boolean {
|
|
226
|
+
for (let i = 0; i < a.length; i++) {
|
|
227
|
+
if (a[i] !== b[i]) return a[i] === true;
|
|
228
|
+
}
|
|
229
|
+
return false;
|
|
230
|
+
}
|
package/src/apps/surfaces.ts
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
* Hand-written (not generated by Speakeasy). Part of the third-party app platform contract;
|
|
3
3
|
* see docs/third-party-apps/plan.md. Host (web-mobile-client) and SDK both import from here.
|
|
4
4
|
*/
|
|
5
|
+
import { isConcreteRoutePath } from "./routes.js";
|
|
5
6
|
|
|
6
7
|
export type Portal = "vendor" | "company" | "facility" | "system";
|
|
7
8
|
|
|
@@ -13,7 +14,9 @@ export type SurfaceKind =
|
|
|
13
14
|
| "list.row_action"
|
|
14
15
|
| "detail"
|
|
15
16
|
| "detail.tabs"
|
|
16
|
-
| "settings"
|
|
17
|
+
| "settings"
|
|
18
|
+
/** An app page: one of the app's own `ui_extensions.routes`, rendered full screen. */
|
|
19
|
+
| "page";
|
|
17
20
|
|
|
18
21
|
/**
|
|
19
22
|
* The kind of record a surface is showing. These match the public API resource each one is
|
|
@@ -22,15 +25,7 @@ export type SurfaceKind =
|
|
|
22
25
|
* and have no public read endpoint yet.
|
|
23
26
|
*/
|
|
24
27
|
export type ObjectType =
|
|
25
|
-
| "
|
|
26
|
-
| "vendor_credential"
|
|
27
|
-
| "facility"
|
|
28
|
-
| "company"
|
|
29
|
-
| "vendor_scan"
|
|
30
|
-
| "invoice"
|
|
31
|
-
| "event"
|
|
32
|
-
| "visitor"
|
|
33
|
-
| "contact";
|
|
28
|
+
"vendor" | "vendor_credential" | "facility" | "company" | "vendor_scan" | "invoice" | "event" | "visitor" | "contact";
|
|
34
29
|
|
|
35
30
|
export interface SurfaceDef {
|
|
36
31
|
readonly id: string;
|
|
@@ -53,6 +48,29 @@ export interface SurfaceDef {
|
|
|
53
48
|
readonly wave: 1 | 2;
|
|
54
49
|
}
|
|
55
50
|
|
|
51
|
+
/**
|
|
52
|
+
* Host URL segment reserved for app pages: `/{portal}/apps/{appId}/pages{path}`. No host route may
|
|
53
|
+
* be defined under it except the screens that render app pages, so an app path can never shadow a
|
|
54
|
+
* host page. It is not reserved inside an app's own space: `/pages/foo` is an ordinary app path.
|
|
55
|
+
*/
|
|
56
|
+
export const APP_PAGES_SEGMENT = "pages";
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* The host pathname of an app page: `/{portal}/apps/{appId}/pages{path}`, with the root path `/`
|
|
60
|
+
* at `/{portal}/apps/{appId}/pages`. Throws unless `appId` is a positive integer and `path` is a
|
|
61
|
+
* concrete route path (`isConcreteRoutePath`), so no input can resolve outside the app's own pages
|
|
62
|
+
* (`/../settings`, `//evil.example`).
|
|
63
|
+
*/
|
|
64
|
+
export function appPagePathname(portal: Portal, appId: number, path: string): string {
|
|
65
|
+
if (!Number.isSafeInteger(appId) || appId <= 0) {
|
|
66
|
+
throw new RangeError(`appPagePathname: app id must be a positive integer, got ${String(appId)}`);
|
|
67
|
+
}
|
|
68
|
+
if (!isConcreteRoutePath(path)) {
|
|
69
|
+
throw new RangeError(`appPagePathname: not a concrete route path: ${JSON.stringify(path)}`);
|
|
70
|
+
}
|
|
71
|
+
return `/${portal}/apps/${appId}/${APP_PAGES_SEGMENT}${path === "/" ? "" : path}`;
|
|
72
|
+
}
|
|
73
|
+
|
|
56
74
|
type Entry = [id: string, kind: SurfaceKind, objectType: ObjectType | null, route: string, idParam: string | null];
|
|
57
75
|
|
|
58
76
|
/** Expands a list surface into the surface itself and its row-action surface. */
|
|
@@ -118,7 +136,15 @@ function staffPortal(portal: "facility" | "system"): Entry[] {
|
|
|
118
136
|
}
|
|
119
137
|
|
|
120
138
|
function define(portal: Portal | null, wave: 1 | 2, entries: Entry[]): SurfaceDef[] {
|
|
121
|
-
return entries.map(([id, kind, objectType, route, idParam]) => ({
|
|
139
|
+
return entries.map(([id, kind, objectType, route, idParam]) => ({
|
|
140
|
+
id,
|
|
141
|
+
portal,
|
|
142
|
+
kind,
|
|
143
|
+
objectType,
|
|
144
|
+
route,
|
|
145
|
+
idParam,
|
|
146
|
+
wave,
|
|
147
|
+
}));
|
|
122
148
|
}
|
|
123
149
|
|
|
124
150
|
export const SURFACES: readonly SurfaceDef[] = Object.freeze([
|
|
@@ -128,6 +154,10 @@ export const SURFACES: readonly SurfaceDef[] = Object.freeze([
|
|
|
128
154
|
...define("system", 2, staffPortal("system")),
|
|
129
155
|
// The app's own settings page. Every portal has one, so the route names the portal as a segment.
|
|
130
156
|
...define(null, 1, [["settings", "settings", null, "/[portal]/apps/[appId]/settings", "appId"]]),
|
|
157
|
+
// Every app page (`ui_extensions.routes`), under the one prefix reserved for apps. Like
|
|
158
|
+
// `settings` it has no portal of its own, and it is not a view viewport or a `surface` target:
|
|
159
|
+
// apps declare pages as routes and reach them with `{ type: "appPage" }`.
|
|
160
|
+
...define(null, 1, [["page", "page", null, `/[portal]/apps/[appId]/${APP_PAGES_SEGMENT}/[...path]`, "appId"]]),
|
|
131
161
|
]);
|
|
132
162
|
|
|
133
163
|
/**
|
|
@@ -152,7 +182,10 @@ export function drawerDefaultFor(portal: Portal): SurfaceId {
|
|
|
152
182
|
return `gs.${portal}.drawer.default`;
|
|
153
183
|
}
|
|
154
184
|
|
|
155
|
-
/**
|
|
185
|
+
/**
|
|
186
|
+
* Surfaces the host renders for a portal in the given wave (and earlier waves). Portal-less
|
|
187
|
+
* surfaces (`settings`, `page`) are never included: they belong to an app, not to a portal screen.
|
|
188
|
+
*/
|
|
156
189
|
export function surfacesForPortal(portal: Portal, wave: 1 | 2 = 1): SurfaceDef[] {
|
|
157
190
|
return SURFACES.filter((s) => s.portal === portal && s.wave <= wave);
|
|
158
191
|
}
|
|
@@ -13,15 +13,24 @@
|
|
|
13
13
|
* await view.find("Button")!.trigger("onPress");
|
|
14
14
|
* expect(view.find("FocusView")!.props["shown"]).toBe(true);
|
|
15
15
|
* ```
|
|
16
|
+
*
|
|
17
|
+
* An app page gets its route through the context:
|
|
18
|
+
*
|
|
19
|
+
* ```ts
|
|
20
|
+
* const page = await renderForTest(<ReportDetail />, {
|
|
21
|
+
* context: { environment: { route: mockPageRoute("/reports/:id", "/reports/7") } },
|
|
22
|
+
* });
|
|
23
|
+
* ```
|
|
16
24
|
*/
|
|
17
25
|
|
|
18
26
|
import { polyfillInstalled } from "../runtime/polyfill.js";
|
|
19
27
|
import { RemoteReceiver, type RemoteReceiverElement, type RemoteReceiverNode, type RemoteReceiverRoot } from "@remote-dom/core/receivers";
|
|
20
28
|
import type { ReactElement } from "react";
|
|
21
29
|
import { COMPONENTS, type ComponentName, type ComponentSpec } from "../components.js";
|
|
22
|
-
import { PROTOCOL_VERSION, type AppTokens, type ExtensionContext } from "../context.js";
|
|
30
|
+
import { PROTOCOL_VERSION, type AppTokens, type ExtensionContext, type PageRoute } from "../context.js";
|
|
23
31
|
import type { HostApi, RemoteConnection, RemoteMutationRecord } from "../protocol.js";
|
|
24
32
|
import { REMOTE_FRAGMENT_TAG } from "../runtime/elements.js";
|
|
33
|
+
import { matchAppRoute } from "../routes.js";
|
|
25
34
|
import { createSandboxExports, type SandboxRuntime } from "../runtime/sandbox.js";
|
|
26
35
|
|
|
27
36
|
// Remote-dom node type constants (`NODE_TYPE_ELEMENT`, `NODE_TYPE_TEXT`).
|
|
@@ -54,8 +63,14 @@ export function mockTokens(now: number = Date.now()): AppTokens {
|
|
|
54
63
|
* platform permissions such as `app_secrets.write` out of `auth.user.scope`: the server grants
|
|
55
64
|
* them to the app's role only, so a real user token never carries them. The mock performs no
|
|
56
65
|
* permission checks; stub the Secret Store's 403 yourself to test a missing grant.
|
|
66
|
+
*
|
|
67
|
+
* `environment.navParams` (what this app's own `navigate` passed as `envParams`) and
|
|
68
|
+
* `environment.queryParams` (an app page's URL query) both default to `{}`; set either to test a
|
|
69
|
+
* view opened by a navigation or from a link. Setting `environment.route` puts the view on an app
|
|
70
|
+
* page (see {@link mockPageRoute}).
|
|
57
71
|
*/
|
|
58
72
|
export function getMockContext(overrides: DeepPartial<ExtensionContext> = {}): ExtensionContext {
|
|
73
|
+
const route = overrides.environment?.route;
|
|
59
74
|
const base: ExtensionContext = {
|
|
60
75
|
protocolVersion: PROTOCOL_VERSION,
|
|
61
76
|
app: { id: 1, version: "1.0.0", permissions: [], csp: { connectSrc: [], imageSrc: [] } },
|
|
@@ -65,12 +80,43 @@ export function getMockContext(overrides: DeepPartial<ExtensionContext> = {}): E
|
|
|
65
80
|
portal: "company",
|
|
66
81
|
platform: "web",
|
|
67
82
|
queryParams: {},
|
|
83
|
+
navParams: {},
|
|
68
84
|
constants: {},
|
|
69
85
|
apiBaseUrl: "https://api.example.test/api",
|
|
70
86
|
},
|
|
71
87
|
auth: mockTokens(),
|
|
72
88
|
};
|
|
73
|
-
|
|
89
|
+
// An app page is rendered on the `page` surface; an explicit `surfaceId` still wins.
|
|
90
|
+
if (route !== undefined) base.environment.surfaceId = "page";
|
|
91
|
+
const merged = deepMerge(base, overrides) as ExtensionContext;
|
|
92
|
+
if (route !== undefined) merged.environment.route = completeRoute(route);
|
|
93
|
+
return merged;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* The `environment.route` the host sends for `pathname` on the declared route `path`, with its
|
|
98
|
+
* params decoded the way the host does it (`matchAppRoute`). `pathname` defaults to `path`, so a
|
|
99
|
+
* route without params needs only one argument. Throws when `pathname` does not match `path`:
|
|
100
|
+
* the host would never render that page.
|
|
101
|
+
*
|
|
102
|
+
* ```ts
|
|
103
|
+
* mockPageRoute("/reports/:id", "/reports/7"); // { path: "/reports/:id", pathname: "/reports/7", params: { id: "7" } }
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
export function mockPageRoute(path: string, pathname: string = path): PageRoute {
|
|
107
|
+
const match = matchAppRoute([{ path }], pathname);
|
|
108
|
+
if (!match) throw new Error(`${pathname} does not match the route ${path}`);
|
|
109
|
+
return { path, pathname, params: match.params };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Fills a partial `environment.route` override: a missing pathname is the path, missing params are `{}`. */
|
|
113
|
+
function completeRoute(route: DeepPartial<PageRoute>): PageRoute {
|
|
114
|
+
const path = route.path ?? route.pathname ?? "/";
|
|
115
|
+
const params: Record<string, string> = {};
|
|
116
|
+
for (const [name, value] of Object.entries(route.params ?? {})) {
|
|
117
|
+
if (typeof value === "string") params[name] = value;
|
|
118
|
+
}
|
|
119
|
+
return { path, pathname: route.pathname ?? path, params };
|
|
74
120
|
}
|
|
75
121
|
|
|
76
122
|
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
package/src/apps/ui/index.ts
CHANGED
|
@@ -13,9 +13,16 @@ import { createUiComponent, type UiProps } from "./factory.js";
|
|
|
13
13
|
|
|
14
14
|
export type { UiProps } from "./factory.js";
|
|
15
15
|
|
|
16
|
-
// View roots. A view component renders exactly one
|
|
16
|
+
// View roots. A view component renders exactly one of these at its top: ContextView on a surface,
|
|
17
|
+
// SettingsView on the settings page, PageView on an app page.
|
|
17
18
|
export const ContextView = /* @__PURE__ */ createUiComponent("ContextView");
|
|
18
19
|
export const SettingsView = /* @__PURE__ */ createUiComponent("SettingsView");
|
|
20
|
+
/**
|
|
21
|
+
* An app page (`ui_extensions.routes`), full width inside the portal's own chrome. `title` is the
|
|
22
|
+
* page header; the `actions` slot joins the header's actions and `banner` sits above the body.
|
|
23
|
+
* Read the route's params with `usePageRoute()` from `apps/hooks`.
|
|
24
|
+
*/
|
|
25
|
+
export const PageView = /* @__PURE__ */ createUiComponent("PageView");
|
|
19
26
|
|
|
20
27
|
const FocusViewElement = /* @__PURE__ */ createUiComponent("FocusView");
|
|
21
28
|
|
|
@@ -108,3 +115,13 @@ export const Radio = /* @__PURE__ */ createUiComponent("Radio");
|
|
|
108
115
|
export const Switch = /* @__PURE__ */ createUiComponent("Switch");
|
|
109
116
|
export const DateField = /* @__PURE__ */ createUiComponent("DateField");
|
|
110
117
|
export const FormFieldGroup = /* @__PURE__ */ createUiComponent("FormFieldGroup");
|
|
118
|
+
|
|
119
|
+
// Charts. Data only: pick a palette with `theme`; `onClick` receives a `ChartClickEvent`
|
|
120
|
+
// (`index`, `label`, `value` and, on multi-series charts, `series`).
|
|
121
|
+
export const BarChart = /* @__PURE__ */ createUiComponent("BarChart");
|
|
122
|
+
export const HorizontalBarChart = /* @__PURE__ */ createUiComponent("HorizontalBarChart");
|
|
123
|
+
export const StackedBarChart = /* @__PURE__ */ createUiComponent("StackedBarChart");
|
|
124
|
+
export const LineChart = /* @__PURE__ */ createUiComponent("LineChart");
|
|
125
|
+
export const AreaChart = /* @__PURE__ */ createUiComponent("AreaChart");
|
|
126
|
+
export const ComboChart = /* @__PURE__ */ createUiComponent("ComboChart");
|
|
127
|
+
export const PieChart = /* @__PURE__ */ createUiComponent("PieChart");
|
package/src/lib/config.ts
CHANGED
|
@@ -80,8 +80,8 @@ export function serverURLFromOptions(options: SDKOptions): URL | null {
|
|
|
80
80
|
export const SDK_METADATA = {
|
|
81
81
|
language: "typescript",
|
|
82
82
|
openapiDocVersion: "0.45.0",
|
|
83
|
-
sdkVersion: "0.46.0-beta.
|
|
83
|
+
sdkVersion: "0.46.0-beta.12",
|
|
84
84
|
genVersion: "2.943.0",
|
|
85
85
|
userAgent:
|
|
86
|
-
"speakeasy-sdk/typescript 0.46.0-beta.
|
|
86
|
+
"speakeasy-sdk/typescript 0.46.0-beta.12 2.943.0 0.45.0 @greensecurity/javascript-sdk",
|
|
87
87
|
} as const;
|