@trackunit/react-core-contexts 1.26.4 → 1.27.3

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/index.cjs.js CHANGED
@@ -103,6 +103,37 @@ const createErrorLink = ({ errorHandler, token, }) => {
103
103
  });
104
104
  };
105
105
 
106
+ // Use a widened `string` key so TypeScript picks the `unknown` overload of
107
+ // `Reflect.get` instead of the typed one — otherwise reads like `module`
108
+ // resolve to `NodeModule` from @types/node.
109
+ const readGlobal = (key) => Reflect.get(globalThis, key);
110
+ const readGlobalString = (key) => {
111
+ const value = readGlobal(key);
112
+ return typeof value === "string" ? value : undefined;
113
+ };
114
+ /**
115
+ * Reads request context fields off `globalThis`.
116
+ *
117
+ * The manager bootstrap sets these as globals when the iris app loads. We must
118
+ * not access bare `global`, since iris-app/Cypress/browser bundles do not have
119
+ * the Node `global` symbol unless something polyfills `window.global = window`.
120
+ */
121
+ const getGlobalRequestContext = () => {
122
+ const manifestScopes = readGlobal("manifestScopes");
123
+ return {
124
+ language: readGlobalString("language"),
125
+ scope: readGlobalString("scope"),
126
+ module: readGlobalString("module"),
127
+ manifestScopes: Array.isArray(manifestScopes) ? manifestScopes : undefined,
128
+ };
129
+ };
130
+ /**
131
+ * Generates headers for the iris app.
132
+ *
133
+ * @param token - The token to use for the authorization header.
134
+ * @param tracingHeaders - The tracing headers to use.
135
+ * @returns {Record<string, string>} The headers to use for the iris app.
136
+ */
106
137
  const generateHeaders = (token, tracingHeaders) => {
107
138
  const headers = {
108
139
  ...Object.entries(tracingHeaders).reduce((acc, [key, value]) => {
@@ -111,7 +142,7 @@ const generateHeaders = (token, tracingHeaders) => {
111
142
  }, {}),
112
143
  Authorization: token ? `Bearer ${token}` : "",
113
144
  };
114
- const globalContext = global;
145
+ const globalContext = getGlobalRequestContext();
115
146
  if (globalContext.language) {
116
147
  headers["Accept-Language"] = globalContext.language || "";
117
148
  }
@@ -122,12 +153,23 @@ const generateHeaders = (token, tracingHeaders) => {
122
153
  headers["TU-MANIFEST-SCOPES"] = globalContext.manifestScopes.map(x => x.scope).join(",");
123
154
  }
124
155
  }
125
- // Remove Authorization header if token is null or undefined
126
156
  if (!token) {
127
157
  delete headers.Authorization;
128
158
  }
129
159
  return headers;
130
160
  };
161
+
162
+ /**
163
+ * Checks whether the iris app should target the internal GraphQL endpoint.
164
+ *
165
+ * The flag is set on `globalThis` by `TrackunitInternalProviders`. We must not
166
+ * read it through bare `global`, since iris-app/Cypress/browser bundles do not
167
+ * have the Node `global` symbol unless something polyfills `window.global = window`.
168
+ */
169
+ const isInternalGqlContext = () => {
170
+ return Reflect.get(globalThis, "gql") === "internal";
171
+ };
172
+
131
173
  const createApolloClient = ({ graphqlPublicUrl, graphqlInternalUrl, graphqlReportUrl, isDev, tracingHeaders, firstToken, errorHandler, }) => {
132
174
  let token;
133
175
  if (!token) {
@@ -194,7 +236,7 @@ const createApolloClient = ({ graphqlPublicUrl, graphqlInternalUrl, graphqlRepor
194
236
  }, sseLink, client.from([
195
237
  errorLink,
196
238
  removeTypenameLink,
197
- client.split(operation => operation.getContext().clientName === "report", reportGraphQLLink, client.split(() => global.gql === "internal", internalGraphQLLink, publicGraphQLLink)),
239
+ client.split(operation => operation.getContext().clientName === "report", reportGraphQLLink, client.split(() => isInternalGqlContext(), internalGraphQLLink, publicGraphQLLink)),
198
240
  ])),
199
241
  ]);
200
242
  return {
package/index.esm.js CHANGED
@@ -101,6 +101,37 @@ const createErrorLink = ({ errorHandler, token, }) => {
101
101
  });
102
102
  };
103
103
 
104
+ // Use a widened `string` key so TypeScript picks the `unknown` overload of
105
+ // `Reflect.get` instead of the typed one — otherwise reads like `module`
106
+ // resolve to `NodeModule` from @types/node.
107
+ const readGlobal = (key) => Reflect.get(globalThis, key);
108
+ const readGlobalString = (key) => {
109
+ const value = readGlobal(key);
110
+ return typeof value === "string" ? value : undefined;
111
+ };
112
+ /**
113
+ * Reads request context fields off `globalThis`.
114
+ *
115
+ * The manager bootstrap sets these as globals when the iris app loads. We must
116
+ * not access bare `global`, since iris-app/Cypress/browser bundles do not have
117
+ * the Node `global` symbol unless something polyfills `window.global = window`.
118
+ */
119
+ const getGlobalRequestContext = () => {
120
+ const manifestScopes = readGlobal("manifestScopes");
121
+ return {
122
+ language: readGlobalString("language"),
123
+ scope: readGlobalString("scope"),
124
+ module: readGlobalString("module"),
125
+ manifestScopes: Array.isArray(manifestScopes) ? manifestScopes : undefined,
126
+ };
127
+ };
128
+ /**
129
+ * Generates headers for the iris app.
130
+ *
131
+ * @param token - The token to use for the authorization header.
132
+ * @param tracingHeaders - The tracing headers to use.
133
+ * @returns {Record<string, string>} The headers to use for the iris app.
134
+ */
104
135
  const generateHeaders = (token, tracingHeaders) => {
105
136
  const headers = {
106
137
  ...Object.entries(tracingHeaders).reduce((acc, [key, value]) => {
@@ -109,7 +140,7 @@ const generateHeaders = (token, tracingHeaders) => {
109
140
  }, {}),
110
141
  Authorization: token ? `Bearer ${token}` : "",
111
142
  };
112
- const globalContext = global;
143
+ const globalContext = getGlobalRequestContext();
113
144
  if (globalContext.language) {
114
145
  headers["Accept-Language"] = globalContext.language || "";
115
146
  }
@@ -120,12 +151,23 @@ const generateHeaders = (token, tracingHeaders) => {
120
151
  headers["TU-MANIFEST-SCOPES"] = globalContext.manifestScopes.map(x => x.scope).join(",");
121
152
  }
122
153
  }
123
- // Remove Authorization header if token is null or undefined
124
154
  if (!token) {
125
155
  delete headers.Authorization;
126
156
  }
127
157
  return headers;
128
158
  };
159
+
160
+ /**
161
+ * Checks whether the iris app should target the internal GraphQL endpoint.
162
+ *
163
+ * The flag is set on `globalThis` by `TrackunitInternalProviders`. We must not
164
+ * read it through bare `global`, since iris-app/Cypress/browser bundles do not
165
+ * have the Node `global` symbol unless something polyfills `window.global = window`.
166
+ */
167
+ const isInternalGqlContext = () => {
168
+ return Reflect.get(globalThis, "gql") === "internal";
169
+ };
170
+
129
171
  const createApolloClient = ({ graphqlPublicUrl, graphqlInternalUrl, graphqlReportUrl, isDev, tracingHeaders, firstToken, errorHandler, }) => {
130
172
  let token;
131
173
  if (!token) {
@@ -192,7 +234,7 @@ const createApolloClient = ({ graphqlPublicUrl, graphqlInternalUrl, graphqlRepor
192
234
  }, sseLink, from([
193
235
  errorLink,
194
236
  removeTypenameLink,
195
- split(operation => operation.getContext().clientName === "report", reportGraphQLLink, split(() => global.gql === "internal", internalGraphQLLink, publicGraphQLLink)),
237
+ split(operation => operation.getContext().clientName === "report", reportGraphQLLink, split(() => isInternalGqlContext(), internalGraphQLLink, publicGraphQLLink)),
196
238
  ])),
197
239
  ]);
198
240
  return {
package/package.json CHANGED
@@ -1,20 +1,20 @@
1
1
  {
2
2
  "name": "@trackunit/react-core-contexts",
3
- "version": "1.26.4",
3
+ "version": "1.27.3",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "engines": {
7
7
  "node": ">=24.x"
8
8
  },
9
9
  "dependencies": {
10
- "@trackunit/iris-app-api": "1.18.3",
11
- "@trackunit/iris-app-runtime-core-api": "1.14.34",
12
- "@trackunit/react-core-hooks": "1.15.36",
13
- "@trackunit/i18n-library-translation": "1.19.4",
14
- "@trackunit/react-components": "1.22.27",
15
- "@trackunit/iris-app-runtime-core": "1.15.34",
10
+ "@trackunit/iris-app-api": "1.19.2",
11
+ "@trackunit/iris-app-runtime-core-api": "1.15.1",
12
+ "@trackunit/react-core-hooks": "1.16.3",
13
+ "@trackunit/i18n-library-translation": "1.20.3",
14
+ "@trackunit/react-components": "1.23.2",
15
+ "@trackunit/iris-app-runtime-core": "1.16.2",
16
16
  "graphql-sse": "^2.5.4",
17
- "@trackunit/react-core-contexts-api": "1.15.34"
17
+ "@trackunit/react-core-contexts-api": "1.16.1"
18
18
  },
19
19
  "peerDependencies": {
20
20
  "@apollo/client": "^3.13.8",
@@ -0,0 +1,9 @@
1
+ import type { TracingHeaders } from "@trackunit/iris-app-runtime-core-api";
2
+ /**
3
+ * Generates headers for the iris app.
4
+ *
5
+ * @param token - The token to use for the authorization header.
6
+ * @param tracingHeaders - The tracing headers to use.
7
+ * @returns {Record<string, string>} The headers to use for the iris app.
8
+ */
9
+ export declare const generateHeaders: (token: string | undefined, tracingHeaders: TracingHeaders) => Record<string, string>;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Checks whether the iris app should target the internal GraphQL endpoint.
3
+ *
4
+ * The flag is set on `globalThis` by `TrackunitInternalProviders`. We must not
5
+ * read it through bare `global`, since iris-app/Cypress/browser bundles do not
6
+ * have the Node `global` symbol unless something polyfills `window.global = window`.
7
+ */
8
+ export declare const isInternalGqlContext: () => boolean;