@burdenoff/microfe-billing 2026.1108.2 → 2026.1109.1

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.
@@ -2,6 +2,8 @@ import { GatewayGraphqlDocument } from './gatewayGraphql';
2
2
  interface DirectActivityGraphqlRequestOptions<TVariables> {
3
3
  apiGatewayUrl?: string;
4
4
  authToken?: string;
5
+ /** The acting user, when known. Mutations default to the token subject. */
6
+ actorId?: string;
5
7
  orgId?: string;
6
8
  query: GatewayGraphqlDocument;
7
9
  variables?: TVariables;
@@ -1,2 +1,2 @@
1
1
  import "./gatewayGraphql.js";
2
- import "@burdenoff/fe-libs/shared/graphql/fetch";
2
+ import "./pinnedRequest.js";
@@ -2,6 +2,8 @@ import { GatewayGraphqlDocument } from './gatewayGraphql';
2
2
  interface DirectBillingGraphqlRequestOptions<TVariables> {
3
3
  apiGatewayUrl?: string;
4
4
  authToken?: string;
5
+ /** The acting user, when known. Mutations default to the token subject. */
6
+ actorId?: string;
5
7
  orgId?: string;
6
8
  query: GatewayGraphqlDocument;
7
9
  variables?: TVariables;
@@ -1,5 +1,5 @@
1
1
  import { normalizeGlobalGatewayBaseUrl as e, toGraphqlQuery as t } from "./gatewayGraphql.js";
2
- import { graphqlFetch as n } from "@burdenoff/fe-libs/shared/graphql/fetch";
2
+ import { actorBoundGraphqlFetch as n } from "./pinnedRequest.js";
3
3
  // src/billing/shared/utils/directBillingGraphql.ts
4
4
  function r(t) {
5
5
  return e(t, ["localhost:4012", "/billing-graphql"]);
@@ -9,6 +9,7 @@ async function i(e) {
9
9
  gateway: "global",
10
10
  baseUrl: r(e.apiGatewayUrl),
11
11
  authToken: e.authToken,
12
+ actorId: e.actorId,
12
13
  organizationId: e.orgId,
13
14
  query: t(e.query),
14
15
  variables: e.variables
@@ -1 +1 @@
1
- {"version":3,"file":"directBillingGraphql.js","names":["resolveDirectBillingGraphqlUrl","normalizeGlobalGatewayBaseUrl","apiGatewayUrl","directBillingGraphqlRequest","response","graphqlFetch","options","toGraphqlQuery"],"sources":["../../../../src/billing/shared/utils/directBillingGraphql.ts"],"sourcesContent":["import { graphqlFetch } from '@burdenoff/fe-libs/shared/graphql/fetch';\n\nimport {\n normalizeGlobalGatewayBaseUrl,\n toGraphqlQuery,\n type GatewayGraphqlDocument,\n} from './gatewayGraphql';\n\ninterface DirectBillingGraphqlRequestOptions<TVariables> {\n apiGatewayUrl?: string;\n authToken?: string;\n orgId?: string;\n query: GatewayGraphqlDocument;\n variables?: TVariables;\n}\n\nexport function resolveDirectBillingGraphqlUrl(apiGatewayUrl?: string): string | undefined {\n return normalizeGlobalGatewayBaseUrl(apiGatewayUrl, ['localhost:4012', '/billing-graphql']);\n}\n\nexport async function directBillingGraphqlRequest<TData, TVariables = Record<string, unknown>>(\n options: DirectBillingGraphqlRequestOptions<TVariables>\n): Promise<TData> {\n const response = await graphqlFetch<TData>({\n gateway: 'global',\n baseUrl: resolveDirectBillingGraphqlUrl(options.apiGatewayUrl),\n authToken: options.authToken,\n organizationId: options.orgId,\n query: toGraphqlQuery(options.query),\n variables: options.variables as Record<string, unknown> | undefined,\n });\n\n if (response.errors?.length || !response.data) {\n throw new Error(response.errors?.[0]?.message || 'Billing GraphQL request failed');\n }\n\n return response.data;\n}\n"],"mappings":";;;AAgBA,SAAgBA,EAA+B,GAA4C;CACzF,OAAOC,EAA8BC,GAAe,CAAC,kBAAkB,kBAAkB,CAAC;AAC5F;AAEA,eAAsBC,EACpB,GACgB;CAChB,IAAMC,IAAW,MAAMC,EAAoB;EACzC,SAAS;EACT,SAASL,EAA+BM,EAAQ,aAAa;EAC7D,WAAWA,EAAQ;EACnB,gBAAgBA,EAAQ;EACxB,OAAOC,EAAeD,EAAQ,KAAK;EACnC,WAAWA,EAAQ;CACrB,CAAC;CAED,IAAIF,EAAS,QAAQ,UAAU,CAACA,EAAS,MACvC,MAAU,MAAMA,EAAS,SAAS,EAAE,EAAE,WAAW,gCAAgC;CAGnF,OAAOA,EAAS;AAClB"}
1
+ {"version":3,"file":"directBillingGraphql.js","names":["resolveDirectBillingGraphqlUrl","normalizeGlobalGatewayBaseUrl","apiGatewayUrl","directBillingGraphqlRequest","response","actorBoundGraphqlFetch","options","toGraphqlQuery"],"sources":["../../../../src/billing/shared/utils/directBillingGraphql.ts"],"sourcesContent":["import {\n normalizeGlobalGatewayBaseUrl,\n toGraphqlQuery,\n type GatewayGraphqlDocument,\n} from './gatewayGraphql';\nimport { actorBoundGraphqlFetch } from './pinnedRequest';\n\ninterface DirectBillingGraphqlRequestOptions<TVariables> {\n apiGatewayUrl?: string;\n authToken?: string;\n /** The acting user, when known. Mutations default to the token subject. */\n actorId?: string;\n orgId?: string;\n query: GatewayGraphqlDocument;\n variables?: TVariables;\n}\n\nexport function resolveDirectBillingGraphqlUrl(apiGatewayUrl?: string): string | undefined {\n return normalizeGlobalGatewayBaseUrl(apiGatewayUrl, ['localhost:4012', '/billing-graphql']);\n}\n\nexport async function directBillingGraphqlRequest<TData, TVariables = Record<string, unknown>>(\n options: DirectBillingGraphqlRequestOptions<TVariables>\n): Promise<TData> {\n const response = await actorBoundGraphqlFetch<TData>({\n gateway: 'global',\n baseUrl: resolveDirectBillingGraphqlUrl(options.apiGatewayUrl),\n authToken: options.authToken,\n actorId: options.actorId,\n organizationId: options.orgId,\n query: toGraphqlQuery(options.query),\n variables: options.variables as Record<string, unknown> | undefined,\n });\n\n if (response.errors?.length || !response.data) {\n throw new Error(response.errors?.[0]?.message || 'Billing GraphQL request failed');\n }\n\n return response.data;\n}\n"],"mappings":";;;AAiBA,SAAgBA,EAA+B,GAA4C;CACzF,OAAOC,EAA8BC,GAAe,CAAC,kBAAkB,kBAAkB,CAAC;AAC5F;AAEA,eAAsBC,EACpB,GACgB;CAChB,IAAMC,IAAW,MAAMC,EAA8B;EACnD,SAAS;EACT,SAASL,EAA+BM,EAAQ,aAAa;EAC7D,WAAWA,EAAQ;EACnB,SAASA,EAAQ;EACjB,gBAAgBA,EAAQ;EACxB,OAAOC,EAAeD,EAAQ,KAAK;EACnC,WAAWA,EAAQ;CACrB,CAAC;CAED,IAAIF,EAAS,QAAQ,UAAU,CAACA,EAAS,MACvC,MAAU,MAAMA,EAAS,SAAS,EAAE,EAAE,WAAW,gCAAgC;CAGnF,OAAOA,EAAS;AAClB"}
@@ -4,6 +4,8 @@ export declare function toGraphqlQuery(query: GatewayGraphqlDocument): string;
4
4
  interface GatewayGraphqlRequestOptions<TVariables> {
5
5
  apiGatewayUrl?: string;
6
6
  authToken?: string;
7
+ /** The acting user, when known. Mutations default to the token subject. */
8
+ actorId?: string;
7
9
  orgId?: string;
8
10
  query: GatewayGraphqlDocument;
9
11
  variables?: TVariables;
@@ -1,4 +1,4 @@
1
- import { graphqlFetch as e } from "@burdenoff/fe-libs/shared/graphql/fetch";
1
+ import { actorBoundGraphqlFetch as e } from "./pinnedRequest.js";
2
2
  import { print as t } from "graphql";
3
3
  // src/billing/shared/utils/gatewayGraphql.ts
4
4
  function n(e) {
@@ -14,6 +14,7 @@ async function a(t) {
14
14
  gateway: "global",
15
15
  baseUrl: i(t.apiGatewayUrl),
16
16
  authToken: t.authToken,
17
+ actorId: t.actorId,
17
18
  organizationId: t.orgId,
18
19
  query: n(t.query),
19
20
  variables: t.variables
@@ -1 +1 @@
1
- {"version":3,"file":"gatewayGraphql.js","names":["toGraphqlQuery","query","print","GLOBAL_GRAPHQL_PATH","normalizeGlobalGatewayBaseUrl","trimmedUrl","apiGatewayUrl","endpoint","gatewayGraphqlRequest","response","graphqlFetch","options"],"sources":["../../../../src/billing/shared/utils/gatewayGraphql.ts"],"sourcesContent":["import { graphqlFetch } from '@burdenoff/fe-libs/shared/graphql/fetch';\nimport { print, type DocumentNode } from 'graphql';\n\nexport type GatewayGraphqlDocument = string | DocumentNode;\n\nexport function toGraphqlQuery(query: GatewayGraphqlDocument): string {\n return typeof query === 'string' ? query : print(query);\n}\n\ninterface GatewayGraphqlRequestOptions<TVariables> {\n apiGatewayUrl?: string;\n authToken?: string;\n orgId?: string;\n query: GatewayGraphqlDocument;\n variables?: TVariables;\n}\n\nconst GLOBAL_GRAPHQL_PATH = '/global/graphql';\n\nexport function normalizeGlobalGatewayBaseUrl(\n apiGatewayUrl?: string,\n legacyDirectEndpoints: string[] = []\n): string | undefined {\n const trimmedUrl = apiGatewayUrl?.trim();\n\n if (!trimmedUrl || trimmedUrl.startsWith('/')) {\n return undefined;\n }\n\n if (legacyDirectEndpoints.some((endpoint) => trimmedUrl.includes(endpoint))) {\n return undefined;\n }\n\n if (trimmedUrl.endsWith(GLOBAL_GRAPHQL_PATH)) {\n return trimmedUrl.slice(0, -GLOBAL_GRAPHQL_PATH.length) || undefined;\n }\n\n return trimmedUrl;\n}\n\nexport async function gatewayGraphqlRequest<TData, TVariables = Record<string, unknown>>(\n options: GatewayGraphqlRequestOptions<TVariables>\n): Promise<TData> {\n const response = await graphqlFetch<TData>({\n gateway: 'global',\n baseUrl: normalizeGlobalGatewayBaseUrl(options.apiGatewayUrl),\n authToken: options.authToken,\n organizationId: options.orgId,\n query: toGraphqlQuery(options.query),\n variables: options.variables as Record<string, unknown> | undefined,\n });\n\n if (response.errors?.length || !response.data) {\n throw new Error(response.errors?.[0]?.message || 'Gateway GraphQL request failed');\n }\n\n return response.data;\n}\n"],"mappings":";;;AAKA,SAAgBA,EAAe,GAAuC;CACpE,OAAO,OAAOC,KAAU,WAAWA,IAAQC,EAAMD,CAAK;AACxD;AAUA,IAAME,IAAsB;AAE5B,SAAgBC,EACd,GACA,IAAkC,CAAC,GACf;CACpB,IAAMC,IAAaC,GAAe,KAAK;CAEvC,IAAKD,KAAc,GAAW,WAAW,GAAG,KAIxC,GAAsB,MAAM,MAAaA,EAAW,SAASE,CAAQ,CAAC,GAQ1E,OAJIF,EAAW,SAASF,CAAmB,IAClCE,EAAW,MAAM,GAAG,GAA2B,KAAK,KAAA,IAGtDA;AACT;AAEA,eAAsBG,EACpB,GACgB;CAChB,IAAMC,IAAW,MAAMC,EAAoB;EACzC,SAAS;EACT,SAASN,EAA8BO,EAAQ,aAAa;EAC5D,WAAWA,EAAQ;EACnB,gBAAgBA,EAAQ;EACxB,OAAOX,EAAeW,EAAQ,KAAK;EACnC,WAAWA,EAAQ;CACrB,CAAC;CAED,IAAIF,EAAS,QAAQ,UAAU,CAACA,EAAS,MACvC,MAAU,MAAMA,EAAS,SAAS,EAAE,EAAE,WAAW,gCAAgC;CAGnF,OAAOA,EAAS;AAClB"}
1
+ {"version":3,"file":"gatewayGraphql.js","names":["toGraphqlQuery","query","print","GLOBAL_GRAPHQL_PATH","normalizeGlobalGatewayBaseUrl","trimmedUrl","apiGatewayUrl","endpoint","gatewayGraphqlRequest","response","actorBoundGraphqlFetch","options"],"sources":["../../../../src/billing/shared/utils/gatewayGraphql.ts"],"sourcesContent":["import { print, type DocumentNode } from 'graphql';\n\nimport { actorBoundGraphqlFetch } from './pinnedRequest';\n\nexport type GatewayGraphqlDocument = string | DocumentNode;\n\nexport function toGraphqlQuery(query: GatewayGraphqlDocument): string {\n return typeof query === 'string' ? query : print(query);\n}\n\ninterface GatewayGraphqlRequestOptions<TVariables> {\n apiGatewayUrl?: string;\n authToken?: string;\n /** The acting user, when known. Mutations default to the token subject. */\n actorId?: string;\n orgId?: string;\n query: GatewayGraphqlDocument;\n variables?: TVariables;\n}\n\nconst GLOBAL_GRAPHQL_PATH = '/global/graphql';\n\nexport function normalizeGlobalGatewayBaseUrl(\n apiGatewayUrl?: string,\n legacyDirectEndpoints: string[] = []\n): string | undefined {\n const trimmedUrl = apiGatewayUrl?.trim();\n\n if (!trimmedUrl || trimmedUrl.startsWith('/')) {\n return undefined;\n }\n\n if (legacyDirectEndpoints.some((endpoint) => trimmedUrl.includes(endpoint))) {\n return undefined;\n }\n\n if (trimmedUrl.endsWith(GLOBAL_GRAPHQL_PATH)) {\n return trimmedUrl.slice(0, -GLOBAL_GRAPHQL_PATH.length) || undefined;\n }\n\n return trimmedUrl;\n}\n\nexport async function gatewayGraphqlRequest<TData, TVariables = Record<string, unknown>>(\n options: GatewayGraphqlRequestOptions<TVariables>\n): Promise<TData> {\n const response = await actorBoundGraphqlFetch<TData>({\n gateway: 'global',\n baseUrl: normalizeGlobalGatewayBaseUrl(options.apiGatewayUrl),\n authToken: options.authToken,\n actorId: options.actorId,\n organizationId: options.orgId,\n query: toGraphqlQuery(options.query),\n variables: options.variables as Record<string, unknown> | undefined,\n });\n\n if (response.errors?.length || !response.data) {\n throw new Error(response.errors?.[0]?.message || 'Gateway GraphQL request failed');\n }\n\n return response.data;\n}\n"],"mappings":";;;AAMA,SAAgBA,EAAe,GAAuC;CACpE,OAAO,OAAOC,KAAU,WAAWA,IAAQC,EAAMD,CAAK;AACxD;AAYA,IAAME,IAAsB;AAE5B,SAAgBC,EACd,GACA,IAAkC,CAAC,GACf;CACpB,IAAMC,IAAaC,GAAe,KAAK;CAEvC,IAAKD,KAAc,GAAW,WAAW,GAAG,KAIxC,GAAsB,MAAM,MAAaA,EAAW,SAASE,CAAQ,CAAC,GAQ1E,OAJIF,EAAW,SAASF,CAAmB,IAClCE,EAAW,MAAM,GAAG,GAA2B,KAAK,KAAA,IAGtDA;AACT;AAEA,eAAsBG,EACpB,GACgB;CAChB,IAAMC,IAAW,MAAMC,EAA8B;EACnD,SAAS;EACT,SAASN,EAA8BO,EAAQ,aAAa;EAC5D,WAAWA,EAAQ;EACnB,SAASA,EAAQ;EACjB,gBAAgBA,EAAQ;EACxB,OAAOX,EAAeW,EAAQ,KAAK;EACnC,WAAWA,EAAQ;CACrB,CAAC;CAED,IAAIF,EAAS,QAAQ,UAAU,CAACA,EAAS,MACvC,MAAU,MAAMA,EAAS,SAAS,EAAE,EAAE,WAAW,gCAAgC;CAGnF,OAAOA,EAAS;AAClB"}
@@ -0,0 +1,48 @@
1
+ import { GraphQLFetchOptions, GraphQLResponse } from '@burdenoff/fe-libs/shared/graphql';
2
+ export declare const SIGN_IN_REQUIRED_MESSAGE = "Your session has ended. Sign in again, then retry this action.";
3
+ export declare const ACTOR_CHANGED_MESSAGE = "The active account changed before this action was sent. Review it and try again.";
4
+ /** What identifies the acting user when an action starts. */
5
+ export interface RequestActorSource {
6
+ /** The bearer the caller was given (usually the MFE's `authToken` prop). */
7
+ authToken?: string | null;
8
+ /** The acting user, when the caller knows it. Defaults to the token subject. */
9
+ actorId?: string | null;
10
+ }
11
+ /** The account and bearer an action was started with. */
12
+ export interface CapturedRequestActor {
13
+ readonly actorId: string;
14
+ readonly authToken: string;
15
+ }
16
+ /** The `sub` claim of an access token, or null when it cannot be read. */
17
+ export declare function getTokenSubject(token: string | null | undefined): string | null;
18
+ /** True when the document contains a mutation operation. */
19
+ export declare function isMutationDocument(query: string): boolean;
20
+ /**
21
+ * Record the acting user when an action starts. Call it synchronously when the
22
+ * write begins so the capture reflects the account the user acted as.
23
+ */
24
+ export declare function captureRequestActor(source: RequestActorSource): CapturedRequestActor;
25
+ /**
26
+ * Options for one operation sent on behalf of a captured actor. Billing talks
27
+ * to the global gateway only, which carries no workspace authority, so there
28
+ * is no workspace token to prepare for the captured actor.
29
+ */
30
+ export type PinnedFetchOptions = Omit<GraphQLFetchOptions, 'authToken' | 'authTokenMode' | 'skipAuthRetry' | 'workspaceToken' | 'gateway'> & {
31
+ gateway: 'global';
32
+ };
33
+ /**
34
+ * Send one operation bound to the captured actor. Returns the raw GraphQL
35
+ * response so callers keep their own error and empty-data handling.
36
+ */
37
+ export declare function pinnedFetch<T>(actor: CapturedRequestActor, options: PinnedFetchOptions): Promise<GraphQLResponse<T>>;
38
+ /** Request options of the billing wrappers: an explicit bearer plus context. */
39
+ export type ActorBoundFetchOptions = PinnedFetchOptions & {
40
+ authToken?: string;
41
+ /** The acting user, when the caller knows it. Defaults to the token subject. */
42
+ actorId?: string | null;
43
+ };
44
+ /**
45
+ * Send a billing operation. Mutations are bound to the actor that started
46
+ * them; queries keep the refreshable transport.
47
+ */
48
+ export declare function actorBoundGraphqlFetch<T>(options: ActorBoundFetchOptions): Promise<GraphQLResponse<T>>;
@@ -0,0 +1,63 @@
1
+ import { getActiveUser as e } from "@burdenoff/fe-libs/shared/providers/shell";
2
+ import { Kind as t, parse as n } from "graphql";
3
+ import { clearGraphqlFetchResponseCache as r, getStoredAuthToken as i, graphqlFetch as a, graphqlFetchPinned as o } from "@burdenoff/fe-libs/shared/graphql";
4
+ // src/billing/shared/utils/pinnedRequest.ts
5
+ var s = "Your session has ended. Sign in again, then retry this action.", c = "The active account changed before this action was sent. Review it and try again.";
6
+ function l(e) {
7
+ let t = e.split(".")[1];
8
+ if (!t) return null;
9
+ try {
10
+ let e = atob(t.replace(/-/g, "+").replace(/_/g, "/")), n = JSON.parse(e);
11
+ return n && typeof n == "object" ? n : null;
12
+ } catch {
13
+ return null;
14
+ }
15
+ }
16
+ function u(e) {
17
+ if (!e) return null;
18
+ let t = l(e)?.sub;
19
+ return typeof t == "string" && t.length > 0 ? t : null;
20
+ }
21
+ function d(e) {
22
+ let t = l(e)?.exp;
23
+ return typeof t == "number" && t * 1e3 <= Date.now();
24
+ }
25
+ function f(e) {
26
+ try {
27
+ return n(e).definitions.some((e) => e.kind === t.OPERATION_DEFINITION && e.operation === "mutation");
28
+ } catch {
29
+ return /^\s*mutation\b/.test(e.replace(/^\s*#[^\n]*\n/gm, ""));
30
+ }
31
+ }
32
+ function p(t) {
33
+ let n = e()?.id || null, r = t.actorId || u(t.authToken) || n;
34
+ if (!r || !n) throw Error(s);
35
+ if (n !== r) throw Error(c);
36
+ let a = i() || t.authToken || "";
37
+ if (!a || d(a)) throw Error(s);
38
+ return {
39
+ actorId: r,
40
+ authToken: a
41
+ };
42
+ }
43
+ async function m(t, n) {
44
+ if (e()?.id !== t.actorId) throw Error(c);
45
+ let i = await o({
46
+ ...n,
47
+ authToken: t.authToken
48
+ });
49
+ return i.errors?.length || r(), i;
50
+ }
51
+ async function h(e) {
52
+ let { actorId: t, authToken: n, ...r } = e;
53
+ return f(r.query) ? m(p({
54
+ authToken: n,
55
+ actorId: t
56
+ }), r) : a({
57
+ ...r,
58
+ ...n ? { authToken: n } : {}
59
+ });
60
+ }
61
+ export { c as ACTOR_CHANGED_MESSAGE, s as SIGN_IN_REQUIRED_MESSAGE, h as actorBoundGraphqlFetch, p as captureRequestActor, u as getTokenSubject, f as isMutationDocument, m as pinnedFetch };
62
+
63
+ //# sourceMappingURL=pinnedRequest.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pinnedRequest.js","names":["SIGN_IN_REQUIRED_MESSAGE","ACTOR_CHANGED_MESSAGE","decodeJwtClaims","payload","token","json","claims","getTokenSubject","sub","isTokenExpired","exp","isMutationDocument","parse","query","definition","Kind","captureRequestActor","activeActorId","getActiveUser","actorId","source","authToken","getStoredAuthToken","pinnedFetch","actor","response","graphqlFetchPinned","options","clearGraphqlFetchResponseCache","actorBoundGraphqlFetch","request","graphqlFetch"],"sources":["../../../../src/billing/shared/utils/pinnedRequest.ts"],"sourcesContent":["/**\n * Actor-bound transport for billing writes (BOFF-11028, following BOFF-10939).\n *\n * The default `graphqlFetch` is refreshable: when an explicit `authToken` has\n * expired it falls back to the token of whichever profile is active in the\n * browser, and its UNAUTHENTICATED retry re-reads storage. A billing write\n * started by one account (a payout account change, a withdrawal, a team\n * permission grant...) could therefore be sent with another account's bearer\n * if the user switched profile while it was in flight.\n *\n * The shared request wrappers (`gatewayGraphqlRequest`,\n * `directBillingGraphqlRequest`, `directActivityGraphqlRequest`) send every\n * operation through `actorBoundGraphqlFetch`, which splits by operation type:\n *\n * - Mutations are bound to the account that started them.\n * `captureRequestActor` identifies that account from the explicit token\n * (its `sub` claim) and refuses if the profile active in the browser is a\n * different account. `graphqlFetchPinned` then sends the captured bearer,\n * refuses an expired one before dispatch and never refreshes or replays the\n * operation under the profile token stored in the browser.\n * - Queries keep the refreshable `graphqlFetch`. They are same-actor reads on\n * long-lived pages where a silent token refresh is the expected behaviour,\n * and a refreshed read cannot change data.\n */\nimport {\n clearGraphqlFetchResponseCache,\n getStoredAuthToken,\n graphqlFetch,\n graphqlFetchPinned,\n type GraphQLFetchOptions,\n type GraphQLResponse,\n} from '@burdenoff/fe-libs/shared/graphql';\nimport { getActiveUser } from '@burdenoff/fe-libs/shared/providers/shell';\nimport { Kind, parse } from 'graphql';\n\nexport const SIGN_IN_REQUIRED_MESSAGE =\n 'Your session has ended. Sign in again, then retry this action.';\nexport const ACTOR_CHANGED_MESSAGE =\n 'The active account changed before this action was sent. Review it and try again.';\n\n/** What identifies the acting user when an action starts. */\nexport interface RequestActorSource {\n /** The bearer the caller was given (usually the MFE's `authToken` prop). */\n authToken?: string | null;\n /** The acting user, when the caller knows it. Defaults to the token subject. */\n actorId?: string | null;\n}\n\n/** The account and bearer an action was started with. */\nexport interface CapturedRequestActor {\n readonly actorId: string;\n readonly authToken: string;\n}\n\ninterface JwtClaims {\n sub?: unknown;\n exp?: unknown;\n}\n\nfunction decodeJwtClaims(token: string): JwtClaims | null {\n const payload = token.split('.')[1];\n if (!payload) return null;\n try {\n // Base64URL to Base64 before decoding; the JWT alphabet is not the atob one.\n const json = atob(payload.replace(/-/g, '+').replace(/_/g, '/'));\n const claims: unknown = JSON.parse(json);\n return claims && typeof claims === 'object' ? (claims as JwtClaims) : null;\n } catch {\n return null;\n }\n}\n\n/** The `sub` claim of an access token, or null when it cannot be read. */\nexport function getTokenSubject(token: string | null | undefined): string | null {\n if (!token) return null;\n const sub = decodeJwtClaims(token)?.sub;\n return typeof sub === 'string' && sub.length > 0 ? sub : null;\n}\n\nfunction isTokenExpired(token: string): boolean {\n const exp = decodeJwtClaims(token)?.exp;\n return typeof exp === 'number' && exp * 1000 <= Date.now();\n}\n\n/** True when the document contains a mutation operation. */\nexport function isMutationDocument(query: string): boolean {\n try {\n return parse(query).definitions.some(\n (definition) =>\n definition.kind === Kind.OPERATION_DEFINITION && definition.operation === 'mutation'\n );\n } catch {\n // An unparsable document is refused by the gateway either way; classify it\n // conservatively from its leading keyword so a write is never sent through\n // the refreshable transport.\n return /^\\s*mutation\\b/.test(query.replace(/^\\s*#[^\\n]*\\n/gm, ''));\n }\n}\n\n/**\n * Record the acting user when an action starts. Call it synchronously when the\n * write begins so the capture reflects the account the user acted as.\n */\nexport function captureRequestActor(source: RequestActorSource): CapturedRequestActor {\n const activeActorId = getActiveUser()?.id || null;\n const actorId = source.actorId || getTokenSubject(source.authToken) || activeActorId;\n if (!actorId || !activeActorId) {\n throw new Error(SIGN_IN_REQUIRED_MESSAGE);\n }\n if (activeActorId !== actorId) {\n throw new Error(ACTOR_CHANGED_MESSAGE);\n }\n // The active profile belongs to the acting user, so its stored token is that\n // user's current bearer (it may be fresher than the explicit one after a\n // refresh). `getStoredAuthToken` returns null for an expired token; the\n // explicit token is then the only candidate.\n const authToken = getStoredAuthToken() || source.authToken || '';\n if (!authToken || isTokenExpired(authToken)) {\n throw new Error(SIGN_IN_REQUIRED_MESSAGE);\n }\n return { actorId, authToken };\n}\n\n/**\n * Options for one operation sent on behalf of a captured actor. Billing talks\n * to the global gateway only, which carries no workspace authority, so there\n * is no workspace token to prepare for the captured actor.\n */\nexport type PinnedFetchOptions = Omit<\n GraphQLFetchOptions,\n 'authToken' | 'authTokenMode' | 'skipAuthRetry' | 'workspaceToken' | 'gateway'\n> & { gateway: 'global' };\n\n/**\n * Send one operation bound to the captured actor. Returns the raw GraphQL\n * response so callers keep their own error and empty-data handling.\n */\nexport async function pinnedFetch<T>(\n actor: CapturedRequestActor,\n options: PinnedFetchOptions\n): Promise<GraphQLResponse<T>> {\n // Synchronous with the dispatch below: nothing can switch the profile between\n // this check and the pinned transport reading the request options.\n if (getActiveUser()?.id !== actor.actorId) {\n throw new Error(ACTOR_CHANGED_MESSAGE);\n }\n const response = await graphqlFetchPinned<T>({ ...options, authToken: actor.authToken });\n\n // A completed write must not be followed by a refetch served from the\n // 15-second response cache with pre-write data.\n if (!response.errors?.length) {\n clearGraphqlFetchResponseCache();\n }\n return response;\n}\n\n/** Request options of the billing wrappers: an explicit bearer plus context. */\nexport type ActorBoundFetchOptions = PinnedFetchOptions & {\n authToken?: string;\n /** The acting user, when the caller knows it. Defaults to the token subject. */\n actorId?: string | null;\n};\n\n/**\n * Send a billing operation. Mutations are bound to the actor that started\n * them; queries keep the refreshable transport.\n */\nexport async function actorBoundGraphqlFetch<T>(\n options: ActorBoundFetchOptions\n): Promise<GraphQLResponse<T>> {\n const { actorId, authToken, ...request } = options;\n if (!isMutationDocument(request.query)) {\n return graphqlFetch<T>({ ...request, ...(authToken ? { authToken } : {}) });\n }\n return pinnedFetch<T>(captureRequestActor({ authToken, actorId }), request);\n}\n"],"mappings":";;;;AAmCA,IAAaA,IACX,kEACWC,IACX;AAqBF,SAASC,EAAgB,GAAiC;CACxD,IAAMC,IAAUC,EAAM,MAAM,GAAG,CAAC,CAAC;CACjC,IAAI,CAACD,GAAS,OAAO;CACrB,IAAI;EAEF,IAAME,IAAO,KAAKF,EAAQ,QAAQ,MAAM,GAAG,CAAC,CAAC,QAAQ,MAAM,GAAG,CAAC,GACzDG,IAAkB,KAAK,MAAMD,CAAI;EACvC,OAAOC,KAAU,OAAOA,KAAW,WAAYA,IAAuB;CACxE,QAAQ;EACN,OAAO;CACT;AACF;AAGA,SAAgBC,EAAgB,GAAiD;CAC/E,IAAI,CAACH,GAAO,OAAO;CACnB,IAAMI,IAAMN,EAAgBE,CAAK,CAAC,EAAE;CACpC,OAAO,OAAOI,KAAQ,YAAYA,EAAI,SAAS,IAAIA,IAAM;AAC3D;AAEA,SAASC,EAAe,GAAwB;CAC9C,IAAMC,IAAMR,EAAgBE,CAAK,CAAC,EAAE;CACpC,OAAO,OAAOM,KAAQ,YAAYA,IAAM,OAAQ,KAAK,IAAI;AAC3D;AAGA,SAAgBC,EAAmB,GAAwB;CACzD,IAAI;EACF,OAAOC,EAAMC,CAAK,CAAC,CAAC,YAAY,MAC7B,MACCC,EAAW,SAASC,EAAK,wBAAwBD,EAAW,cAAc,UAC9E;CACF,QAAQ;EAIN,OAAO,iBAAiB,KAAKD,EAAM,QAAQ,mBAAmB,EAAE,CAAC;CACnE;AACF;AAMA,SAAgBG,EAAoB,GAAkD;CACpF,IAAMC,IAAgBC,EAAc,CAAC,EAAE,MAAM,MACvCC,IAAUC,EAAO,WAAWb,EAAgBa,EAAO,SAAS,KAAKH;CACvE,IAAI,CAACE,KAAW,CAACF,GACf,MAAU,MAAMjB,CAAwB;CAE1C,IAAIiB,MAAkBE,GACpB,MAAU,MAAMlB,CAAqB;CAMvC,IAAMoB,IAAYC,EAAmB,KAAKF,EAAO,aAAa;CAC9D,IAAI,CAACC,KAAaZ,EAAeY,CAAS,GACxC,MAAU,MAAMrB,CAAwB;CAE1C,OAAO;EAAE;EAAS;CAAU;AAC9B;AAgBA,eAAsBuB,EACpB,GACA,GAC6B;CAG7B,IAAIL,EAAc,CAAC,EAAE,OAAOM,EAAM,SAChC,MAAU,MAAMvB,CAAqB;CAEvC,IAAMwB,IAAW,MAAMC,EAAsB;EAAE,GAAGC;EAAS,WAAWH,EAAM;CAAU,CAAC;CAOvF,OAHKC,EAAS,QAAQ,UACpBG,EAA+B,GAE1BH;AACT;AAaA,eAAsBI,EACpB,GAC6B;CAC7B,IAAM,EAAE,YAAS,cAAW,GAAGC,MAAYH;CAI3C,OAHKhB,EAAmBmB,EAAQ,KAAK,IAG9BP,EAAeP,EAAoB;EAAE;EAAW;CAAQ,CAAC,GAAGc,CAAO,IAFjEC,EAAgB;EAAE,GAAGD;EAAS,GAAIT,IAAY,EAAE,aAAU,IAAI,CAAC;CAAG,CAAC;AAG9E"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@burdenoff/microfe-billing",
3
- "version": "2026.1108.2",
3
+ "version": "2026.1109.1",
4
4
  "description": "Billing microfrontend for Burdenoff products",
5
5
  "type": "module",
6
6
  "files": [
@@ -47,7 +47,7 @@
47
47
  },
48
48
  "dependencies": {
49
49
  "@apollo/client": "^4.3.6",
50
- "@burdenoff/fe-libs": "2026.1027.13",
50
+ "@burdenoff/fe-libs": "2026.1102.4",
51
51
  "@tanstack/react-query": "^5.106.1",
52
52
  "clsx": "^2.1.1",
53
53
  "graphql": "^16.15.0",