@voyant-travel/apps 0.4.0 → 0.5.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.
@@ -253,6 +253,32 @@ export declare const appCredentialRevocationSchema: z.ZodObject<{
253
253
  installationId: z.ZodString;
254
254
  actorId: z.ZodString;
255
255
  }, z.core.$strict>;
256
+ export declare const appSessionTokenIssueSchema: z.ZodObject<{
257
+ entity: z.ZodOptional<z.ZodObject<{
258
+ type: z.ZodString;
259
+ id: z.ZodString;
260
+ }, z.core.$strict>>;
261
+ slot: z.ZodOptional<z.ZodString>;
262
+ }, z.core.$strict>;
263
+ export declare const appSessionTokenExchangeSchema: z.ZodPipe<z.ZodObject<{
264
+ session_token: z.ZodString;
265
+ client_id: z.ZodString;
266
+ client_secret: z.ZodOptional<z.ZodString>;
267
+ viewer_scopes: z.ZodDefault<z.ZodArray<z.ZodString>>;
268
+ contextual_scopes: z.ZodOptional<z.ZodArray<z.ZodString>>;
269
+ }, z.core.$strict>, z.ZodTransform<{
270
+ client_secret: string | undefined;
271
+ session_token: string;
272
+ client_id: string;
273
+ viewer_scopes: string[];
274
+ contextual_scopes?: string[] | undefined;
275
+ }, {
276
+ session_token: string;
277
+ client_id: string;
278
+ viewer_scopes: string[];
279
+ client_secret?: string | undefined;
280
+ contextual_scopes?: string[] | undefined;
281
+ }>>;
256
282
  export type AppManifest = z.infer<typeof appManifestSchema>;
257
283
  export type CreateCustomAppRegistrationInput = z.infer<typeof createCustomAppRegistrationSchema>;
258
284
  export type ReleaseManifestUploadInput = z.infer<typeof releaseManifestUploadSchema>;
@@ -261,3 +287,5 @@ export type AppListQuery = z.infer<typeof appListQuerySchema>;
261
287
  export type AppWebhookReplayInput = z.infer<typeof appWebhookReplaySchema>;
262
288
  export type AppOAuthAuthorizeQuery = z.infer<typeof appOAuthAuthorizeQuerySchema>;
263
289
  export type AppOAuthTokenInput = z.infer<typeof appOAuthTokenSchema>;
290
+ export type AppSessionTokenIssueInput = z.infer<typeof appSessionTokenIssueSchema>;
291
+ export type AppSessionTokenExchangeInput = z.infer<typeof appSessionTokenExchangeSchema>;
package/dist/contracts.js CHANGED
@@ -271,3 +271,28 @@ export const appCredentialRevocationSchema = z
271
271
  actorId: z.string().trim().min(1),
272
272
  })
273
273
  .strict();
274
+ const sessionTokenEntitySchema = z
275
+ .object({
276
+ type: z.string().trim().min(1).max(80),
277
+ id: z.string().trim().min(1).max(200),
278
+ })
279
+ .strict();
280
+ export const appSessionTokenIssueSchema = z
281
+ .object({
282
+ entity: sessionTokenEntitySchema.optional(),
283
+ slot: z.string().trim().min(1).max(80).optional(),
284
+ })
285
+ .strict();
286
+ export const appSessionTokenExchangeSchema = z
287
+ .object({
288
+ session_token: z.string().trim().min(1),
289
+ client_id: z.string().trim().min(1),
290
+ client_secret: z.string().trim().optional(),
291
+ viewer_scopes: z.array(scopeSchema).default([]),
292
+ contextual_scopes: z.array(scopeSchema).optional(),
293
+ })
294
+ .strict()
295
+ .transform((input) => ({
296
+ ...input,
297
+ client_secret: input.client_secret?.trim() || undefined,
298
+ }));
@@ -0,0 +1,72 @@
1
+ /**
2
+ * Installation-backed admin extension resolution (RFC §6, Phase 3).
3
+ *
4
+ * Replaces the static self-hosted descriptor list as the source of activated
5
+ * admin extensions: descriptors come from `app_extension_installations` for
6
+ * `active` installations only, so pausing, degrading, or uninstalling an app
7
+ * immediately drops its pages and slots. Slot extensions are filtered by
8
+ * release-pinned `extensionApi` compatibility, and host-rendered labels (nav
9
+ * entries, extension titles) plus the resolved app locale + text direction come
10
+ * from the installed release's declared locales and `app_release_localizations`.
11
+ *
12
+ * The returned descriptors are the exact shape the admin host consumes; static
13
+ * deployment-graph descriptors remain supported alongside these.
14
+ */
15
+ import { type UiExtensionDescriptor } from "@voyant-travel/admin-extension-sdk";
16
+ import type { PostgresJsDatabase } from "drizzle-orm/postgres-js";
17
+ import { type AppTextDirection, type HostLabelRow } from "./locale-resolution.js";
18
+ /** A resolved slot/dashboard extension plus the app locale its frame renders in. */
19
+ export interface ResolvedUiSlotExtension {
20
+ descriptor: UiExtensionDescriptor;
21
+ installationId: string;
22
+ appId: string;
23
+ appLocale: string;
24
+ direction: AppTextDirection;
25
+ }
26
+ /** A resolved full-page app extension mounted under the app-owned admin route. */
27
+ export interface ResolvedAppPage {
28
+ /** Stable per-installation key (`<installationId>:<manifest key>`). */
29
+ key: string;
30
+ installationId: string;
31
+ appId: string;
32
+ /** App-owned admin sub-path declared by the manifest (e.g. `/settings`). */
33
+ path: string;
34
+ entryUrl: string;
35
+ /** Host-rendered page title, locale-resolved with deterministic fallback. */
36
+ title: string;
37
+ /** Host-rendered navigation label for the page's nav entry. */
38
+ navLabel: string;
39
+ appLocale: string;
40
+ direction: AppTextDirection;
41
+ }
42
+ export interface ResolvedInstalledExtensions {
43
+ slots: ResolvedUiSlotExtension[];
44
+ pages: ResolvedAppPage[];
45
+ }
46
+ export interface ResolveInstalledExtensionsInput {
47
+ deploymentId: string;
48
+ /** The staff member's active admin locale. */
49
+ activeLocale: string;
50
+ /** The extension API version the host implements (defaults to the SDK's). */
51
+ extensionApiVersion?: string;
52
+ }
53
+ /** One row of an active installation's active extension, as read from the store. */
54
+ export interface InstalledExtensionRow {
55
+ installationId: string;
56
+ appId: string;
57
+ extensionKey: string;
58
+ descriptor: Record<string, unknown>;
59
+ releaseId: string;
60
+ defaultLocale: string;
61
+ supportedLocales: readonly string[];
62
+ }
63
+ export declare function resolveInstalledExtensions(db: PostgresJsDatabase, input: ResolveInstalledExtensionsInput): Promise<ResolvedInstalledExtensions>;
64
+ /**
65
+ * Pure assembly of resolved descriptors from store rows and localization data.
66
+ * Applies compatibility filtering, locale resolution, and host-label lookup.
67
+ * The `rows` are assumed to already be scoped to active installations/extensions.
68
+ */
69
+ export declare function assembleInstalledExtensions(rows: readonly InstalledExtensionRow[], localizationsByRelease: Map<string, HostLabelRow[]>, options: {
70
+ activeLocale: string;
71
+ extensionApiVersion?: string;
72
+ }): ResolvedInstalledExtensions;
@@ -0,0 +1,159 @@
1
+ /**
2
+ * Installation-backed admin extension resolution (RFC §6, Phase 3).
3
+ *
4
+ * Replaces the static self-hosted descriptor list as the source of activated
5
+ * admin extensions: descriptors come from `app_extension_installations` for
6
+ * `active` installations only, so pausing, degrading, or uninstalling an app
7
+ * immediately drops its pages and slots. Slot extensions are filtered by
8
+ * release-pinned `extensionApi` compatibility, and host-rendered labels (nav
9
+ * entries, extension titles) plus the resolved app locale + text direction come
10
+ * from the installed release's declared locales and `app_release_localizations`.
11
+ *
12
+ * The returned descriptors are the exact shape the admin host consumes; static
13
+ * deployment-graph descriptors remain supported alongside these.
14
+ */
15
+ import { ADMIN_UI_EXTENSION_API_VERSION, isUiExtensionCompatible, } from "@voyant-travel/admin-extension-sdk";
16
+ import { and, eq, inArray } from "drizzle-orm";
17
+ import { createHostLabelResolver, resolveAppLocale, } from "./locale-resolution.js";
18
+ import { appExtensionInstallations, appInstallations, appReleaseLocalizations, appReleases, } from "./schema.js";
19
+ export async function resolveInstalledExtensions(db, input) {
20
+ const rows = await db
21
+ .select({
22
+ installationId: appInstallations.id,
23
+ appId: appInstallations.appId,
24
+ extensionKey: appExtensionInstallations.extensionKey,
25
+ descriptor: appExtensionInstallations.descriptor,
26
+ releaseId: appExtensionInstallations.releaseId,
27
+ defaultLocale: appReleases.defaultLocale,
28
+ supportedLocales: appReleases.supportedLocales,
29
+ })
30
+ .from(appExtensionInstallations)
31
+ .innerJoin(appInstallations, eq(appExtensionInstallations.installationId, appInstallations.id))
32
+ .innerJoin(appReleases, eq(appExtensionInstallations.releaseId, appReleases.id))
33
+ .where(and(eq(appInstallations.deploymentId, input.deploymentId),
34
+ // Active installations only: paused/degraded/uninstalled apps unmount.
35
+ eq(appInstallations.status, "active"), eq(appExtensionInstallations.status, "active")));
36
+ const localizationsByRelease = await loadLocalizations(db, rows.map((row) => row.releaseId));
37
+ return assembleInstalledExtensions(rows, localizationsByRelease, {
38
+ activeLocale: input.activeLocale,
39
+ extensionApiVersion: input.extensionApiVersion,
40
+ });
41
+ }
42
+ /**
43
+ * Pure assembly of resolved descriptors from store rows and localization data.
44
+ * Applies compatibility filtering, locale resolution, and host-label lookup.
45
+ * The `rows` are assumed to already be scoped to active installations/extensions.
46
+ */
47
+ export function assembleInstalledExtensions(rows, localizationsByRelease, options) {
48
+ const version = options.extensionApiVersion ?? ADMIN_UI_EXTENSION_API_VERSION;
49
+ const slots = [];
50
+ const pages = [];
51
+ for (const row of rows) {
52
+ const declaration = {
53
+ defaultLocale: row.defaultLocale,
54
+ supportedLocales: row.supportedLocales,
55
+ };
56
+ const locale = resolveAppLocale(options.activeLocale, declaration);
57
+ const labels = createHostLabelResolver(localizationsByRelease.get(row.releaseId) ?? [], locale.appLocale, declaration.defaultLocale);
58
+ if (row.extensionKey.startsWith("page:")) {
59
+ const page = parsePageDescriptor(row.descriptor);
60
+ if (!page)
61
+ continue;
62
+ const title = labels.resolve(page.titleKey, ["extension", "navigation"]) ?? page.key;
63
+ const navLabel = labels.resolve(page.titleKey, ["navigation", "extension"]) ?? title;
64
+ pages.push({
65
+ key: `${row.installationId}:${page.key}`,
66
+ installationId: row.installationId,
67
+ appId: row.appId,
68
+ path: page.path,
69
+ entryUrl: page.entryUrl,
70
+ title,
71
+ navLabel,
72
+ appLocale: locale.appLocale,
73
+ direction: locale.direction,
74
+ });
75
+ continue;
76
+ }
77
+ if (row.extensionKey.startsWith("slot:")) {
78
+ const slot = parseSlotDescriptor(row.descriptor);
79
+ if (!slot)
80
+ continue;
81
+ // Release-pinned compatibility filter: an incompatible extension is not
82
+ // mounted (the host's render-time check is defense-in-depth).
83
+ if (!isUiExtensionCompatible(slot.extensionApi, version))
84
+ continue;
85
+ const displayName = labels.resolve(slot.titleKey, ["extension", "navigation"]) ?? slot.key;
86
+ slots.push({
87
+ descriptor: {
88
+ key: `${row.installationId}:${slot.key}`,
89
+ version: slot.version,
90
+ displayName,
91
+ extensionApi: slot.extensionApi,
92
+ entryUrl: slot.entryUrl,
93
+ slots: slot.slots,
94
+ ...(slot.config ? { config: slot.config } : {}),
95
+ },
96
+ installationId: row.installationId,
97
+ appId: row.appId,
98
+ appLocale: locale.appLocale,
99
+ direction: locale.direction,
100
+ });
101
+ }
102
+ }
103
+ return { slots, pages };
104
+ }
105
+ async function loadLocalizations(db, releaseIds) {
106
+ const unique = [...new Set(releaseIds)];
107
+ const grouped = new Map();
108
+ if (unique.length === 0)
109
+ return grouped;
110
+ const rows = await db
111
+ .select({
112
+ releaseId: appReleaseLocalizations.releaseId,
113
+ locale: appReleaseLocalizations.locale,
114
+ surface: appReleaseLocalizations.surface,
115
+ messageKey: appReleaseLocalizations.messageKey,
116
+ text: appReleaseLocalizations.text,
117
+ })
118
+ .from(appReleaseLocalizations)
119
+ .where(inArray(appReleaseLocalizations.releaseId, unique));
120
+ for (const row of rows) {
121
+ const list = grouped.get(row.releaseId) ?? [];
122
+ list.push({
123
+ locale: row.locale,
124
+ surface: row.surface,
125
+ messageKey: row.messageKey,
126
+ text: row.text,
127
+ });
128
+ grouped.set(row.releaseId, list);
129
+ }
130
+ return grouped;
131
+ }
132
+ function parsePageDescriptor(value) {
133
+ const key = stringField(value.key);
134
+ const titleKey = stringField(value.titleKey);
135
+ const path = stringField(value.path);
136
+ const entryUrl = stringField(value.entryUrl);
137
+ if (!key || !titleKey || !path || !entryUrl)
138
+ return null;
139
+ return { key, titleKey, path, entryUrl };
140
+ }
141
+ function parseSlotDescriptor(value) {
142
+ const key = stringField(value.key);
143
+ const titleKey = stringField(value.titleKey);
144
+ const version = stringField(value.version);
145
+ const extensionApi = stringField(value.extensionApi);
146
+ const entryUrl = stringField(value.entryUrl);
147
+ const slots = Array.isArray(value.slots)
148
+ ? value.slots.filter((slot) => typeof slot === "string")
149
+ : [];
150
+ if (!key || !titleKey || !version || !extensionApi || !entryUrl || slots.length === 0)
151
+ return null;
152
+ const config = value.config && typeof value.config === "object" && !Array.isArray(value.config)
153
+ ? value.config
154
+ : undefined;
155
+ return { key, titleKey, version, extensionApi, entryUrl, slots, config };
156
+ }
157
+ function stringField(value) {
158
+ return typeof value === "string" && value.length > 0 ? value : null;
159
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,111 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { assembleInstalledExtensions } from "./extension-resolution.js";
3
+ function slotRow(overrides = {}) {
4
+ return {
5
+ installationId: "apin_1",
6
+ appId: "app_1",
7
+ extensionKey: "slot:reports",
8
+ releaseId: "aprl_1",
9
+ defaultLocale: "en",
10
+ supportedLocales: ["en", "pt"],
11
+ descriptor: {
12
+ key: "reports",
13
+ titleKey: "reports.title",
14
+ version: "1.0.0",
15
+ extensionApi: "^1",
16
+ entryUrl: "https://app.example.com/reports",
17
+ slots: ["dashboard.header"],
18
+ },
19
+ ...overrides,
20
+ };
21
+ }
22
+ function pageRow(overrides = {}) {
23
+ return {
24
+ installationId: "apin_1",
25
+ appId: "app_1",
26
+ extensionKey: "page:settings",
27
+ releaseId: "aprl_1",
28
+ defaultLocale: "en",
29
+ supportedLocales: ["en", "pt"],
30
+ descriptor: {
31
+ key: "settings",
32
+ titleKey: "settings.title",
33
+ path: "/settings",
34
+ entryUrl: "https://app.example.com/settings",
35
+ },
36
+ ...overrides,
37
+ };
38
+ }
39
+ const localizations = new Map([
40
+ [
41
+ "aprl_1",
42
+ [
43
+ { locale: "en", surface: "extension", messageKey: "reports.title", text: "Reports" },
44
+ { locale: "pt", surface: "extension", messageKey: "reports.title", text: "Relatórios" },
45
+ { locale: "en", surface: "navigation", messageKey: "settings.title", text: "Settings" },
46
+ { locale: "en", surface: "extension", messageKey: "settings.title", text: "App Settings" },
47
+ ],
48
+ ],
49
+ ]);
50
+ describe("assembleInstalledExtensions", () => {
51
+ it("resolves slot descriptors with host labels and locale/direction", () => {
52
+ const result = assembleInstalledExtensions([slotRow()], localizations, { activeLocale: "pt" });
53
+ expect(result.slots).toHaveLength(1);
54
+ const slot = result.slots[0];
55
+ expect(slot?.descriptor.key).toBe("apin_1:reports");
56
+ expect(slot?.descriptor.displayName).toBe("Relatórios");
57
+ expect(slot?.descriptor.slots).toEqual(["dashboard.header"]);
58
+ expect(slot?.appLocale).toBe("pt");
59
+ expect(slot?.direction).toBe("ltr");
60
+ });
61
+ it("resolves full-page descriptors with title and nav label", () => {
62
+ const result = assembleInstalledExtensions([pageRow()], localizations, { activeLocale: "en" });
63
+ expect(result.pages).toHaveLength(1);
64
+ const page = result.pages[0];
65
+ expect(page?.key).toBe("apin_1:settings");
66
+ expect(page?.path).toBe("/settings");
67
+ expect(page?.title).toBe("App Settings");
68
+ expect(page?.navLabel).toBe("Settings");
69
+ });
70
+ it("filters out extensionApi-incompatible slot extensions", () => {
71
+ const incompatible = slotRow({
72
+ descriptor: {
73
+ key: "legacy",
74
+ titleKey: "reports.title",
75
+ version: "1.0.0",
76
+ extensionApi: "^0.9",
77
+ entryUrl: "https://app.example.com/legacy",
78
+ slots: ["dashboard.header"],
79
+ },
80
+ });
81
+ const result = assembleInstalledExtensions([incompatible], localizations, {
82
+ activeLocale: "en",
83
+ extensionApiVersion: "1.1.0",
84
+ });
85
+ expect(result.slots).toHaveLength(0);
86
+ });
87
+ it("falls back to the descriptor key when no host label exists", () => {
88
+ const result = assembleInstalledExtensions([slotRow({ releaseId: "aprl_none" })], new Map(), {
89
+ activeLocale: "en",
90
+ });
91
+ expect(result.slots[0]?.descriptor.displayName).toBe("reports");
92
+ });
93
+ it("falls back to the default locale for an unsupported active locale", () => {
94
+ const result = assembleInstalledExtensions([slotRow()], localizations, { activeLocale: "de" });
95
+ expect(result.slots[0]?.appLocale).toBe("en");
96
+ expect(result.slots[0]?.descriptor.displayName).toBe("Reports");
97
+ });
98
+ it("skips malformed descriptors (fail-soft)", () => {
99
+ const malformed = slotRow({ descriptor: { key: "broken" } });
100
+ const result = assembleInstalledExtensions([malformed], localizations, { activeLocale: "en" });
101
+ expect(result.slots).toHaveLength(0);
102
+ expect(result.pages).toHaveLength(0);
103
+ });
104
+ it("gives each installation a unique descriptor key", () => {
105
+ const result = assembleInstalledExtensions([slotRow(), slotRow({ installationId: "apin_2" })], localizations, { activeLocale: "en" });
106
+ expect(result.slots.map((slot) => slot.descriptor.key)).toEqual([
107
+ "apin_1:reports",
108
+ "apin_2:reports",
109
+ ]);
110
+ });
111
+ });
package/dist/index.d.ts CHANGED
@@ -5,12 +5,16 @@ export * from "./app-api-service.js";
5
5
  export * from "./compiler.js";
6
6
  export * from "./consent.js";
7
7
  export * from "./contracts.js";
8
+ export * from "./extension-resolution.js";
8
9
  export * from "./ingestion.js";
9
10
  export * from "./installation-service.js";
11
+ export * from "./locale-resolution.js";
10
12
  export * from "./oauth-crypto.js";
11
13
  export * from "./oauth-service.js";
12
14
  export { createAppsAdminRoutes } from "./routes.js";
13
- export { appAccessCredentialStatusEnum, appAccessCredentials, appAccessTokenModeEnum, appAuditEventKindEnum, appAuditEvents, appCredentialKindEnum, appCredentials, appDistributionEnum, appExtensionInstallations, appGrantStatusEnum, appGrants, appInstallationRegistrationStatusEnum, appInstallationSettings, appInstallationStatusEnum, appInstallations, appInstallationUpdatePolicyEnum, appLifecycleStateEnum, appOAuthAuthorizationCodes, appOAuthRefreshTokens, appRedirectUris, appReleaseArtifactStateEnum, appReleaseArtifacts, appReleaseLocalizations, appReleaseStateEnum, appReleases, appSecretReferences, apps, appWebhookSubscriptionStatusEnum, appWebhookSubscriptions, } from "./schema.js";
15
+ export { appAccessCredentialStatusEnum, appAccessCredentials, appAccessTokenModeEnum, appAuditEventKindEnum, appAuditEvents, appCredentialKindEnum, appCredentials, appDistributionEnum, appExtensionInstallations, appGrantStatusEnum, appGrants, appInstallationRegistrationStatusEnum, appInstallationSettings, appInstallationStatusEnum, appInstallations, appInstallationUpdatePolicyEnum, appLifecycleStateEnum, appOAuthAuthorizationCodes, appOAuthRefreshTokens, appRedirectUris, appReleaseArtifactStateEnum, appReleaseArtifacts, appReleaseLocalizations, appReleaseStateEnum, appReleases, appSecretReferences, appSessionTokens, apps, appWebhookSubscriptionStatusEnum, appWebhookSubscriptions, } from "./schema.js";
14
16
  export { createAppsService } from "./service.js";
17
+ export * from "./session-token.js";
18
+ export * from "./session-token-service.js";
15
19
  export type { AppWebhookDeliveryOptions } from "./webhook-delivery.js";
16
20
  export { createAppWebhookDeliveryStore, createAppWebhookEventQueue, enqueueAppWebhookEvent, listAppWebhookHealth, replayAppWebhookDelivery, } from "./webhook-delivery.js";
package/dist/index.js CHANGED
@@ -5,11 +5,15 @@ export * from "./app-api-service.js";
5
5
  export * from "./compiler.js";
6
6
  export * from "./consent.js";
7
7
  export * from "./contracts.js";
8
+ export * from "./extension-resolution.js";
8
9
  export * from "./ingestion.js";
9
10
  export * from "./installation-service.js";
11
+ export * from "./locale-resolution.js";
10
12
  export * from "./oauth-crypto.js";
11
13
  export * from "./oauth-service.js";
12
14
  export { createAppsAdminRoutes } from "./routes.js";
13
- export { appAccessCredentialStatusEnum, appAccessCredentials, appAccessTokenModeEnum, appAuditEventKindEnum, appAuditEvents, appCredentialKindEnum, appCredentials, appDistributionEnum, appExtensionInstallations, appGrantStatusEnum, appGrants, appInstallationRegistrationStatusEnum, appInstallationSettings, appInstallationStatusEnum, appInstallations, appInstallationUpdatePolicyEnum, appLifecycleStateEnum, appOAuthAuthorizationCodes, appOAuthRefreshTokens, appRedirectUris, appReleaseArtifactStateEnum, appReleaseArtifacts, appReleaseLocalizations, appReleaseStateEnum, appReleases, appSecretReferences, apps, appWebhookSubscriptionStatusEnum, appWebhookSubscriptions, } from "./schema.js";
15
+ export { appAccessCredentialStatusEnum, appAccessCredentials, appAccessTokenModeEnum, appAuditEventKindEnum, appAuditEvents, appCredentialKindEnum, appCredentials, appDistributionEnum, appExtensionInstallations, appGrantStatusEnum, appGrants, appInstallationRegistrationStatusEnum, appInstallationSettings, appInstallationStatusEnum, appInstallations, appInstallationUpdatePolicyEnum, appLifecycleStateEnum, appOAuthAuthorizationCodes, appOAuthRefreshTokens, appRedirectUris, appReleaseArtifactStateEnum, appReleaseArtifacts, appReleaseLocalizations, appReleaseStateEnum, appReleases, appSecretReferences, appSessionTokens, apps, appWebhookSubscriptionStatusEnum, appWebhookSubscriptions, } from "./schema.js";
14
16
  export { createAppsService } from "./service.js";
17
+ export * from "./session-token.js";
18
+ export * from "./session-token-service.js";
15
19
  export { createAppWebhookDeliveryStore, createAppWebhookEventQueue, enqueueAppWebhookEvent, listAppWebhookHealth, replayAppWebhookDelivery, } from "./webhook-delivery.js";
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Deterministic app-locale resolution for the extension host (RFC §6.1).
3
+ *
4
+ * The host always passes the staff member's active locale to the frame. It also
5
+ * resolves an "app locale" against the installed release's declared locales so
6
+ * host-rendered labels (nav entries, extension titles) and the frame's initial
7
+ * direction are chosen consistently:
8
+ *
9
+ * 1. exact active-locale match;
10
+ * 2. progressively less specific language match (`pt-BR` → `pt`);
11
+ * 3. the app's declared default locale.
12
+ *
13
+ * Everything here is pure and BCP 47 aware only to the extent of splitting on
14
+ * the primary subtag — the app remains authoritative for its in-frame
15
+ * translations and may choose a more sophisticated fallback itself.
16
+ */
17
+ export type AppTextDirection = "ltr" | "rtl";
18
+ /** Text direction for a locale, from its primary language subtag. */
19
+ export declare function resolveTextDirection(locale: string): AppTextDirection;
20
+ export interface AppLocaleDeclaration {
21
+ defaultLocale: string;
22
+ supportedLocales: readonly string[];
23
+ }
24
+ export interface ResolvedAppLocale {
25
+ /** The active admin locale the host requested resolution for. */
26
+ requestedLocale: string;
27
+ /** The declared locale the host resolved to (exact → language → default). */
28
+ appLocale: string;
29
+ /** Text direction for {@link appLocale}. */
30
+ direction: AppTextDirection;
31
+ }
32
+ /**
33
+ * Resolve the app locale for an active admin locale against a release's
34
+ * declared locales. The returned `appLocale` is always one of the declared
35
+ * tags (falling back to `defaultLocale`), preserving the declared casing.
36
+ */
37
+ export declare function resolveAppLocale(activeLocale: string, declaration: AppLocaleDeclaration): ResolvedAppLocale;
38
+ /** One flattened host-rendered localization row (matches `app_release_localizations`). */
39
+ export interface HostLabelRow {
40
+ locale: string;
41
+ surface: string;
42
+ messageKey: string;
43
+ text: string;
44
+ }
45
+ export interface HostLabelResolver {
46
+ /**
47
+ * Resolve a host-rendered label for a message key, trying the resolved app
48
+ * locale first and then the default locale (host labels use the platform
49
+ * algorithm, never the app's in-frame fallback). Returns null when no
50
+ * declared surface carries the key.
51
+ */
52
+ resolve(messageKey: string, surfaces?: readonly string[]): string | null;
53
+ }
54
+ /**
55
+ * Build a resolver over a release's localization rows for a resolved app locale
56
+ * with default-locale fallback. Missing translations fall back deterministically
57
+ * rather than throwing, so an incomplete non-default locale never blocks mount.
58
+ */
59
+ export declare function createHostLabelResolver(rows: readonly HostLabelRow[], appLocale: string, defaultLocale: string): HostLabelResolver;
@@ -0,0 +1,113 @@
1
+ /**
2
+ * Deterministic app-locale resolution for the extension host (RFC §6.1).
3
+ *
4
+ * The host always passes the staff member's active locale to the frame. It also
5
+ * resolves an "app locale" against the installed release's declared locales so
6
+ * host-rendered labels (nav entries, extension titles) and the frame's initial
7
+ * direction are chosen consistently:
8
+ *
9
+ * 1. exact active-locale match;
10
+ * 2. progressively less specific language match (`pt-BR` → `pt`);
11
+ * 3. the app's declared default locale.
12
+ *
13
+ * Everything here is pure and BCP 47 aware only to the extent of splitting on
14
+ * the primary subtag — the app remains authoritative for its in-frame
15
+ * translations and may choose a more sophisticated fallback itself.
16
+ */
17
+ /**
18
+ * Primary language subtags written right-to-left. Kept small and explicit; the
19
+ * frame receives the resolved direction so it never has to infer it from an
20
+ * incomplete language list.
21
+ */
22
+ const RTL_LANGUAGES = new Set([
23
+ "ar", // Arabic
24
+ "arc", // Aramaic
25
+ "ckb", // Central Kurdish (Sorani)
26
+ "dv", // Divehi
27
+ "fa", // Persian
28
+ "he", // Hebrew
29
+ "ku", // Kurdish
30
+ "ps", // Pashto
31
+ "sd", // Sindhi
32
+ "syr", // Syriac
33
+ "ur", // Urdu
34
+ "yi", // Yiddish
35
+ ]);
36
+ /** The primary language subtag of a BCP 47 tag, lowercased. */
37
+ function primaryLanguage(locale) {
38
+ return locale.trim().toLowerCase().split(/[-_]/)[0] ?? "";
39
+ }
40
+ /** Text direction for a locale, from its primary language subtag. */
41
+ export function resolveTextDirection(locale) {
42
+ return RTL_LANGUAGES.has(primaryLanguage(locale)) ? "rtl" : "ltr";
43
+ }
44
+ /**
45
+ * Resolve the app locale for an active admin locale against a release's
46
+ * declared locales. The returned `appLocale` is always one of the declared
47
+ * tags (falling back to `defaultLocale`), preserving the declared casing.
48
+ */
49
+ export function resolveAppLocale(activeLocale, declaration) {
50
+ const requested = activeLocale.trim();
51
+ const normalized = requested.toLowerCase();
52
+ const supported = declaration.supportedLocales;
53
+ const exact = supported.find((candidate) => candidate.toLowerCase() === normalized);
54
+ if (exact) {
55
+ return { requestedLocale: requested, appLocale: exact, direction: resolveTextDirection(exact) };
56
+ }
57
+ const requestedLanguage = primaryLanguage(requested);
58
+ if (requestedLanguage) {
59
+ const languageMatch = supported.find((candidate) => primaryLanguage(candidate) === requestedLanguage);
60
+ if (languageMatch) {
61
+ return {
62
+ requestedLocale: requested,
63
+ appLocale: languageMatch,
64
+ direction: resolveTextDirection(languageMatch),
65
+ };
66
+ }
67
+ }
68
+ return {
69
+ requestedLocale: requested,
70
+ appLocale: declaration.defaultLocale,
71
+ direction: resolveTextDirection(declaration.defaultLocale),
72
+ };
73
+ }
74
+ const DEFAULT_LABEL_SURFACES = ["extension", "navigation", "setup", "app"];
75
+ /**
76
+ * Build a resolver over a release's localization rows for a resolved app locale
77
+ * with default-locale fallback. Missing translations fall back deterministically
78
+ * rather than throwing, so an incomplete non-default locale never blocks mount.
79
+ */
80
+ export function createHostLabelResolver(rows, appLocale, defaultLocale) {
81
+ // locale → surface → messageKey → text
82
+ const byLocale = new Map();
83
+ for (const row of rows) {
84
+ const locale = row.locale.toLowerCase();
85
+ let surfaces = byLocale.get(locale);
86
+ if (!surfaces) {
87
+ surfaces = new Map();
88
+ byLocale.set(locale, surfaces);
89
+ }
90
+ let keys = surfaces.get(row.surface);
91
+ if (!keys) {
92
+ keys = new Map();
93
+ surfaces.set(row.surface, keys);
94
+ }
95
+ keys.set(row.messageKey, row.text);
96
+ }
97
+ const localePreference = [appLocale.toLowerCase(), defaultLocale.toLowerCase()];
98
+ return {
99
+ resolve(messageKey, surfaces = DEFAULT_LABEL_SURFACES) {
100
+ for (const locale of localePreference) {
101
+ const surfaceMap = byLocale.get(locale);
102
+ if (!surfaceMap)
103
+ continue;
104
+ for (const surface of surfaces) {
105
+ const text = surfaceMap.get(surface)?.get(messageKey);
106
+ if (text !== undefined)
107
+ return text;
108
+ }
109
+ }
110
+ return null;
111
+ },
112
+ };
113
+ }
@@ -0,0 +1 @@
1
+ export {};