@open-mercato/shared 0.10.1-develop.8258.1.62a94b74a2 → 0.10.1-develop.8270.1.ab18f01dc9
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/lib/frontend/organizationEvents.js +6 -0
- package/dist/lib/frontend/organizationEvents.js.map +2 -2
- package/dist/lib/version.js +1 -1
- package/dist/lib/version.js.map +1 -1
- package/package.json +2 -2
- package/src/lib/frontend/__tests__/organizationEvents.initialScope.test.ts +13 -0
- package/src/lib/frontend/organizationEvents.ts +11 -0
|
@@ -28,6 +28,11 @@ function getCurrentOrganizationScope() {
|
|
|
28
28
|
function getCurrentOrganizationScopeVersion() {
|
|
29
29
|
return currentVersion;
|
|
30
30
|
}
|
|
31
|
+
function readRequestOrganizationScope() {
|
|
32
|
+
if (typeof document === "undefined") return null;
|
|
33
|
+
const hasOrganizationCookie = document.cookie.split(";").some((entry) => entry.trim().startsWith("om_selected_org="));
|
|
34
|
+
return hasOrganizationCookie ? readPersistedScope() : null;
|
|
35
|
+
}
|
|
31
36
|
function emitOrganizationScopeChanged(detail) {
|
|
32
37
|
if (typeof window === "undefined" || typeof CustomEvent === "undefined") return;
|
|
33
38
|
const previousScope = hasEmitted ? currentScope : initialPersistedScope;
|
|
@@ -59,6 +64,7 @@ export {
|
|
|
59
64
|
emitOrganizationScopeChanged,
|
|
60
65
|
getCurrentOrganizationScope,
|
|
61
66
|
getCurrentOrganizationScopeVersion,
|
|
67
|
+
readRequestOrganizationScope,
|
|
62
68
|
subscribeOrganizationScopeChanged
|
|
63
69
|
};
|
|
64
70
|
//# sourceMappingURL=organizationEvents.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/lib/frontend/organizationEvents.ts"],
|
|
4
|
-
"sourcesContent": ["export const ORGANIZATION_SCOPE_CHANGED_EVENT = 'om:organization-scope-changed'\n\nexport type OrganizationScopeChangedDetail = {\n organizationId: string | null\n tenantId: string | null\n}\n\nfunction readPersistedScope(): OrganizationScopeChangedDetail {\n if (typeof document === 'undefined') return { organizationId: null, tenantId: null }\n const cookies = document.cookie.split(';').map((entry) => entry.trim())\n const readSelection = (name: string): string | null => {\n const entry = cookies.find((cookie) => cookie.startsWith(`${name}=`))\n const raw = entry?.slice(name.length + 1)\n if (!raw) return null\n try { return decodeURIComponent(raw) || null } catch { return raw }\n }\n const organizationId = readSelection('om_selected_org')\n return {\n organizationId: organizationId === '__all__' ? null : organizationId,\n tenantId: readSelection('om_selected_tenant'),\n }\n}\n\nconst initialPersistedScope = readPersistedScope()\nlet currentScope: OrganizationScopeChangedDetail = { organizationId: null, tenantId: null }\nlet currentVersion = 0\nlet hasEmitted = false\n\nexport function getCurrentOrganizationScope(): OrganizationScopeChangedDetail {\n return { ...currentScope }\n}\n\nexport function getCurrentOrganizationScopeVersion(): number {\n return currentVersion\n}\n\nexport function emitOrganizationScopeChanged(detail: OrganizationScopeChangedDetail): void {\n if (typeof window === 'undefined' || typeof CustomEvent === 'undefined') return\n \n // Detect actual changes\n const previousScope = hasEmitted ? currentScope : initialPersistedScope\n const hasChanged = \n previousScope.organizationId !== detail.organizationId ||\n previousScope.tenantId !== detail.tenantId\n // Guard so the very first emission always syncs subscribers, even when the\n // initial scope matches the default all-orgs `{ null, null }` state.\n const isFirstEmit = !hasEmitted\n \n // Update module-level state\n currentScope = { ...detail }\n hasEmitted = true\n \n // Increment version only if actual change detected\n if (hasChanged) {\n currentVersion++\n }\n \n // Only dispatch on real scope changes (or the first emission). Suppressing\n // no-op re-emits avoids spurious DataTable refreshes, chrome refetches, and\n // scope-hook churn when navigating between pages with the same scope.\n if (hasChanged || isFirstEmit) {\n window.dispatchEvent(new CustomEvent<OrganizationScopeChangedDetail>(ORGANIZATION_SCOPE_CHANGED_EVENT, { detail }))\n }\n}\n\nexport function subscribeOrganizationScopeChanged(\n handler: (detail: OrganizationScopeChangedDetail) => void\n): () => void {\n if (typeof window === 'undefined') return () => {}\n const listener = (event: Event) => {\n const detail = (event as CustomEvent<OrganizationScopeChangedDetail>).detail ?? { organizationId: null, tenantId: null }\n handler(detail)\n }\n window.addEventListener(ORGANIZATION_SCOPE_CHANGED_EVENT, listener as EventListener)\n return () => {\n window.removeEventListener(ORGANIZATION_SCOPE_CHANGED_EVENT, listener as EventListener)\n }\n}\n"],
|
|
5
|
-
"mappings": "AAAO,MAAM,mCAAmC;AAOhD,SAAS,qBAAqD;AAC5D,MAAI,OAAO,aAAa,YAAa,QAAO,EAAE,gBAAgB,MAAM,UAAU,KAAK;AACnF,QAAM,UAAU,SAAS,OAAO,MAAM,GAAG,EAAE,IAAI,CAAC,UAAU,MAAM,KAAK,CAAC;AACtE,QAAM,gBAAgB,CAAC,SAAgC;AACrD,UAAM,QAAQ,QAAQ,KAAK,CAAC,WAAW,OAAO,WAAW,GAAG,IAAI,GAAG,CAAC;AACpE,UAAM,MAAM,OAAO,MAAM,KAAK,SAAS,CAAC;AACxC,QAAI,CAAC,IAAK,QAAO;AACjB,QAAI;AAAE,aAAO,mBAAmB,GAAG,KAAK;AAAA,IAAK,QAAQ;AAAE,aAAO;AAAA,IAAI;AAAA,EACpE;AACA,QAAM,iBAAiB,cAAc,iBAAiB;AACtD,SAAO;AAAA,IACL,gBAAgB,mBAAmB,YAAY,OAAO;AAAA,IACtD,UAAU,cAAc,oBAAoB;AAAA,EAC9C;AACF;AAEA,MAAM,wBAAwB,mBAAmB;AACjD,IAAI,eAA+C,EAAE,gBAAgB,MAAM,UAAU,KAAK;AAC1F,IAAI,iBAAiB;AACrB,IAAI,aAAa;AAEV,SAAS,8BAA8D;AAC5E,SAAO,EAAE,GAAG,aAAa;AAC3B;AAEO,SAAS,qCAA6C;AAC3D,SAAO;AACT;AAEO,SAAS,6BAA6B,QAA8C;AACzF,MAAI,OAAO,WAAW,eAAe,OAAO,gBAAgB,YAAa;AAGzE,QAAM,gBAAgB,aAAa,eAAe;AAClD,QAAM,aACJ,cAAc,mBAAmB,OAAO,kBACxC,cAAc,aAAa,OAAO;AAGpC,QAAM,cAAc,CAAC;AAGrB,iBAAe,EAAE,GAAG,OAAO;AAC3B,eAAa;AAGb,MAAI,YAAY;AACd;AAAA,EACF;AAKA,MAAI,cAAc,aAAa;AAC7B,WAAO,cAAc,IAAI,YAA4C,kCAAkC,EAAE,OAAO,CAAC,CAAC;AAAA,EACpH;AACF;AAEO,SAAS,kCACd,SACY;AACZ,MAAI,OAAO,WAAW,YAAa,QAAO,MAAM;AAAA,EAAC;AACjD,QAAM,WAAW,CAAC,UAAiB;AACjC,UAAM,SAAU,MAAsD,UAAU,EAAE,gBAAgB,MAAM,UAAU,KAAK;AACvH,YAAQ,MAAM;AAAA,EAChB;AACA,SAAO,iBAAiB,kCAAkC,QAAyB;AACnF,SAAO,MAAM;AACX,WAAO,oBAAoB,kCAAkC,QAAyB;AAAA,EACxF;AACF;",
|
|
4
|
+
"sourcesContent": ["export const ORGANIZATION_SCOPE_CHANGED_EVENT = 'om:organization-scope-changed'\n\nexport type OrganizationScopeChangedDetail = {\n organizationId: string | null\n tenantId: string | null\n}\n\nfunction readPersistedScope(): OrganizationScopeChangedDetail {\n if (typeof document === 'undefined') return { organizationId: null, tenantId: null }\n const cookies = document.cookie.split(';').map((entry) => entry.trim())\n const readSelection = (name: string): string | null => {\n const entry = cookies.find((cookie) => cookie.startsWith(`${name}=`))\n const raw = entry?.slice(name.length + 1)\n if (!raw) return null\n try { return decodeURIComponent(raw) || null } catch { return raw }\n }\n const organizationId = readSelection('om_selected_org')\n return {\n organizationId: organizationId === '__all__' ? null : organizationId,\n tenantId: readSelection('om_selected_tenant'),\n }\n}\n\nconst initialPersistedScope = readPersistedScope()\nlet currentScope: OrganizationScopeChangedDetail = { organizationId: null, tenantId: null }\nlet currentVersion = 0\nlet hasEmitted = false\n\nexport function getCurrentOrganizationScope(): OrganizationScopeChangedDetail {\n return { ...currentScope }\n}\n\nexport function getCurrentOrganizationScopeVersion(): number {\n return currentVersion\n}\n\n/**\n * The scope a request sent now runs under, read from the cookies the server resolves it from.\n * Null while no organization cookie exists: the server then picks the organization itself, so\n * the browser cannot name it. A blank or all-organizations cookie is a real selection.\n */\nexport function readRequestOrganizationScope(): OrganizationScopeChangedDetail | null {\n if (typeof document === 'undefined') return null\n const hasOrganizationCookie = document.cookie.split(';').some((entry) => entry.trim().startsWith('om_selected_org='))\n return hasOrganizationCookie ? readPersistedScope() : null\n}\n\nexport function emitOrganizationScopeChanged(detail: OrganizationScopeChangedDetail): void {\n if (typeof window === 'undefined' || typeof CustomEvent === 'undefined') return\n \n // Detect actual changes\n const previousScope = hasEmitted ? currentScope : initialPersistedScope\n const hasChanged = \n previousScope.organizationId !== detail.organizationId ||\n previousScope.tenantId !== detail.tenantId\n // Guard so the very first emission always syncs subscribers, even when the\n // initial scope matches the default all-orgs `{ null, null }` state.\n const isFirstEmit = !hasEmitted\n \n // Update module-level state\n currentScope = { ...detail }\n hasEmitted = true\n \n // Increment version only if actual change detected\n if (hasChanged) {\n currentVersion++\n }\n \n // Only dispatch on real scope changes (or the first emission). Suppressing\n // no-op re-emits avoids spurious DataTable refreshes, chrome refetches, and\n // scope-hook churn when navigating between pages with the same scope.\n if (hasChanged || isFirstEmit) {\n window.dispatchEvent(new CustomEvent<OrganizationScopeChangedDetail>(ORGANIZATION_SCOPE_CHANGED_EVENT, { detail }))\n }\n}\n\nexport function subscribeOrganizationScopeChanged(\n handler: (detail: OrganizationScopeChangedDetail) => void\n): () => void {\n if (typeof window === 'undefined') return () => {}\n const listener = (event: Event) => {\n const detail = (event as CustomEvent<OrganizationScopeChangedDetail>).detail ?? { organizationId: null, tenantId: null }\n handler(detail)\n }\n window.addEventListener(ORGANIZATION_SCOPE_CHANGED_EVENT, listener as EventListener)\n return () => {\n window.removeEventListener(ORGANIZATION_SCOPE_CHANGED_EVENT, listener as EventListener)\n }\n}\n"],
|
|
5
|
+
"mappings": "AAAO,MAAM,mCAAmC;AAOhD,SAAS,qBAAq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AG,OAAO;AAC3B,eAAa;AAGb,MAAI,YAAY;AACd;AAAA,EACF;AAKA,MAAI,cAAc,aAAa;AAC7B,WAAO,cAAc,IAAI,YAA4C,kCAAkC,EAAE,OAAO,CAAC,CAAC;AAAA,EACpH;AACF;AAEO,SAAS,kCACd,SACY;AACZ,MAAI,OAAO,WAAW,YAAa,QAAO,MAAM;AAAA,EAAC;AACjD,QAAM,WAAW,CAAC,UAAiB;AACjC,UAAM,SAAU,MAAsD,UAAU,EAAE,gBAAgB,MAAM,UAAU,KAAK;AACvH,YAAQ,MAAM;AAAA,EAChB;AACA,SAAO,iBAAiB,kCAAkC,QAAyB;AACnF,SAAO,MAAM;AACX,WAAO,oBAAoB,kCAAkC,QAAyB;AAAA,EACxF;AACF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/lib/version.js
CHANGED
package/dist/lib/version.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/lib/version.ts"],
|
|
4
|
-
"sourcesContent": ["// Build-time generated version\nexport const APP_VERSION = '0.10.1-develop.
|
|
4
|
+
"sourcesContent": ["// Build-time generated version\nexport const APP_VERSION = '0.10.1-develop.8270.1.ab18f01dc9';\nexport const appVersion = APP_VERSION;\n"],
|
|
5
5
|
"mappings": "AACO,MAAM,cAAc;AACpB,MAAM,aAAa;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-mercato/shared",
|
|
3
|
-
"version": "0.10.1-develop.
|
|
3
|
+
"version": "0.10.1-develop.8270.1.ab18f01dc9",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -130,7 +130,7 @@
|
|
|
130
130
|
"@mikro-orm/core": "^7.1.14",
|
|
131
131
|
"@mikro-orm/decorators": "^7.1.14",
|
|
132
132
|
"@mikro-orm/postgresql": "^7.1.14",
|
|
133
|
-
"@open-mercato/cache": "0.10.1-develop.
|
|
133
|
+
"@open-mercato/cache": "0.10.1-develop.8270.1.ab18f01dc9",
|
|
134
134
|
"@types/html-to-text": "^9.0.4",
|
|
135
135
|
"@types/sanitize-html": "^2.16.1",
|
|
136
136
|
"dotenv": "^17.4.2",
|
|
@@ -80,4 +80,17 @@ describe('organization scope bootstrap', () => {
|
|
|
80
80
|
expect(events.getCurrentOrganizationScope()).toEqual({ organizationId: 'organization-one', tenantId: 'tenant%bad' })
|
|
81
81
|
expect(events.getCurrentOrganizationScopeVersion()).toBe(0)
|
|
82
82
|
})
|
|
83
|
+
|
|
84
|
+
it('names the scope a request runs under only when an organization cookie exists', () => {
|
|
85
|
+
const events = loadEvents()
|
|
86
|
+
expect(events.readRequestOrganizationScope()).toBeNull()
|
|
87
|
+
document.cookie = 'om_selected_tenant=tenant-one; path=/'
|
|
88
|
+
expect(events.readRequestOrganizationScope()).toBeNull()
|
|
89
|
+
document.cookie = 'om_selected_org=organization%2Done; path=/'
|
|
90
|
+
expect(events.readRequestOrganizationScope()).toEqual({ organizationId: 'organization-one', tenantId: 'tenant-one' })
|
|
91
|
+
document.cookie = 'om_selected_org=__all__; path=/'
|
|
92
|
+
expect(events.readRequestOrganizationScope()).toEqual({ organizationId: null, tenantId: 'tenant-one' })
|
|
93
|
+
document.cookie = 'om_selected_org=; path=/'
|
|
94
|
+
expect(events.readRequestOrganizationScope()).toEqual({ organizationId: null, tenantId: 'tenant-one' })
|
|
95
|
+
})
|
|
83
96
|
})
|
|
@@ -34,6 +34,17 @@ export function getCurrentOrganizationScopeVersion(): number {
|
|
|
34
34
|
return currentVersion
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
/**
|
|
38
|
+
* The scope a request sent now runs under, read from the cookies the server resolves it from.
|
|
39
|
+
* Null while no organization cookie exists: the server then picks the organization itself, so
|
|
40
|
+
* the browser cannot name it. A blank or all-organizations cookie is a real selection.
|
|
41
|
+
*/
|
|
42
|
+
export function readRequestOrganizationScope(): OrganizationScopeChangedDetail | null {
|
|
43
|
+
if (typeof document === 'undefined') return null
|
|
44
|
+
const hasOrganizationCookie = document.cookie.split(';').some((entry) => entry.trim().startsWith('om_selected_org='))
|
|
45
|
+
return hasOrganizationCookie ? readPersistedScope() : null
|
|
46
|
+
}
|
|
47
|
+
|
|
37
48
|
export function emitOrganizationScopeChanged(detail: OrganizationScopeChangedDetail): void {
|
|
38
49
|
if (typeof window === 'undefined' || typeof CustomEvent === 'undefined') return
|
|
39
50
|
|