@trackunit/react-core-contexts 1.3.128 → 1.3.130

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.
Files changed (3) hide show
  1. package/index.cjs.js +39 -31
  2. package/index.esm.js +39 -31
  3. package/package.json +4 -4
package/index.cjs.js CHANGED
@@ -34,6 +34,31 @@ function _interopNamespaceDefault(e) {
34
34
 
35
35
  var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);
36
36
 
37
+ const generateHeaders = (token, tracingHeaders) => {
38
+ const headers = {
39
+ ...Object.entries(tracingHeaders).reduce((acc, [key, value]) => {
40
+ acc[key] = String(value);
41
+ return acc;
42
+ }, {}),
43
+ Authorization: token ? `Bearer ${token}` : "",
44
+ };
45
+ const globalContext = global;
46
+ if (globalContext.language) {
47
+ headers["Accept-Language"] = globalContext.language || "";
48
+ }
49
+ const irisAppId = globalContext.scope ? `${globalContext.scope}/${globalContext.module}` : null;
50
+ if (irisAppId) {
51
+ headers["TU-IRIS-APP-ID"] = irisAppId;
52
+ if (Array.isArray(globalContext.manifestScopes)) {
53
+ headers["TU-MANIFEST-SCOPES"] = globalContext.manifestScopes.map(x => x.scope).join(",");
54
+ }
55
+ }
56
+ // Remove Authorization header if token is null or undefined
57
+ if (!token) {
58
+ delete headers.Authorization;
59
+ }
60
+ return headers;
61
+ };
37
62
  const createApolloClient = ({ graphqlManagerUrl, graphqlPublicUrl, graphqlInternalUrl, graphqlReportUrl, isDev, tracingHeaders, firstToken, errorHandler, }) => {
38
63
  let token;
39
64
  if (!token) {
@@ -51,28 +76,12 @@ const createApolloClient = ({ graphqlManagerUrl, graphqlPublicUrl, graphqlIntern
51
76
  const reportGraphQLLink = client.createHttpLink({
52
77
  uri: request => graphqlReportUrl + "/" + request.operationName,
53
78
  });
54
- const authLink = context.setContext(async (_, { headers }) => {
55
- const newHeaders = {
56
- ...headers,
57
- ...tracingHeaders,
58
- Authorization: token ? `Bearer ${token}` : null,
59
- };
60
- if (global.language) {
61
- newHeaders["Accept-Language"] = global.language;
62
- }
63
- // remove once the token has this information
64
- const irisAppId = global.scope
65
- ? global.scope + "/" + global.module
66
- : null;
67
- if (irisAppId) {
68
- newHeaders["TU-IRIS-APP-ID"] = irisAppId;
69
- const manifestScopes = global.manifestScopes;
70
- if (Array.isArray(manifestScopes)) {
71
- newHeaders["TU-MANIFEST-SCOPES"] = manifestScopes.map(x => x.scope).join(",");
72
- }
73
- }
79
+ const authLink = context.setContext(async (_, { headers: existingHeaders }) => {
74
80
  return {
75
- headers: newHeaders,
81
+ headers: {
82
+ ...existingHeaders,
83
+ ...generateHeaders(token, tracingHeaders),
84
+ },
76
85
  };
77
86
  });
78
87
  const errorLink = error.onError(({ graphQLErrors, networkError, operation, forward }) => {
@@ -172,11 +181,7 @@ const createApolloClient = ({ graphqlManagerUrl, graphqlPublicUrl, graphqlIntern
172
181
  }
173
182
  const sseLink = new SSELink({
174
183
  url: `${graphqlInternalUrl}/stream`,
175
- headers: () => {
176
- return {
177
- Authorization: `Bearer ${token}`,
178
- };
179
- },
184
+ headers: () => generateHeaders(token, tracingHeaders),
180
185
  });
181
186
  // Split links based on operation type
182
187
  const splitLink = client.from([
@@ -216,7 +221,7 @@ const createApolloClient = ({ graphqlManagerUrl, graphqlPublicUrl, graphqlIntern
216
221
  };
217
222
  const useApolloClient = () => {
218
223
  const { graphqlManagerUrl, graphqlPublicUrl, graphqlInternalUrl, graphqlReportUrl, environment, tracingHeaders } = reactCoreHooks.useEnvironment();
219
- const { token } = reactCoreHooks.useToken();
224
+ const { token: currentToken } = reactCoreHooks.useToken();
220
225
  const errorHandler = reactCoreHooks.useErrorHandler();
221
226
  const [client] = React.useState(() => {
222
227
  return createApolloClient({
@@ -229,13 +234,16 @@ const useApolloClient = () => {
229
234
  graphqlReportUrl,
230
235
  isDev: environment === "dev",
231
236
  tracingHeaders,
232
- firstToken: token,
237
+ firstToken: currentToken,
233
238
  errorHandler,
234
239
  });
235
240
  });
236
- if (client.getToken() !== token) {
237
- client.setToken(token);
238
- }
241
+ React.useMemo(() => {
242
+ if (client.getToken() !== currentToken) {
243
+ client.setToken(currentToken);
244
+ }
245
+ return currentToken;
246
+ }, [client, currentToken]);
239
247
  return client;
240
248
  };
241
249
  /**
package/index.esm.js CHANGED
@@ -14,6 +14,31 @@ import { registerTranslations, initializeTranslationsForApp } from '@trackunit/i
14
14
  import { Spinner } from '@trackunit/react-components';
15
15
  import { AnalyticsContextRuntime, setupHostConnector, AssetSortingRuntime, ConfirmationDialogRuntime, EnvironmentRuntime, FilterBarRuntime, AssetsFilterBarRuntime, CustomersFilterBarRuntime, SitesFilterBarRuntime, ModalDialogRuntime, NavigationRuntime, OemBrandingContextRuntime, ThemeCssRuntime, TimeRangeRuntime, ToastRuntime, TokenRuntime, CurrentUserRuntime, CurrentUserPreferenceRuntime, UserSubscriptionRuntime, WidgetConfigRuntime } from '@trackunit/iris-app-runtime-core';
16
16
 
17
+ const generateHeaders = (token, tracingHeaders) => {
18
+ const headers = {
19
+ ...Object.entries(tracingHeaders).reduce((acc, [key, value]) => {
20
+ acc[key] = String(value);
21
+ return acc;
22
+ }, {}),
23
+ Authorization: token ? `Bearer ${token}` : "",
24
+ };
25
+ const globalContext = global;
26
+ if (globalContext.language) {
27
+ headers["Accept-Language"] = globalContext.language || "";
28
+ }
29
+ const irisAppId = globalContext.scope ? `${globalContext.scope}/${globalContext.module}` : null;
30
+ if (irisAppId) {
31
+ headers["TU-IRIS-APP-ID"] = irisAppId;
32
+ if (Array.isArray(globalContext.manifestScopes)) {
33
+ headers["TU-MANIFEST-SCOPES"] = globalContext.manifestScopes.map(x => x.scope).join(",");
34
+ }
35
+ }
36
+ // Remove Authorization header if token is null or undefined
37
+ if (!token) {
38
+ delete headers.Authorization;
39
+ }
40
+ return headers;
41
+ };
17
42
  const createApolloClient = ({ graphqlManagerUrl, graphqlPublicUrl, graphqlInternalUrl, graphqlReportUrl, isDev, tracingHeaders, firstToken, errorHandler, }) => {
18
43
  let token;
19
44
  if (!token) {
@@ -31,28 +56,12 @@ const createApolloClient = ({ graphqlManagerUrl, graphqlPublicUrl, graphqlIntern
31
56
  const reportGraphQLLink = createHttpLink({
32
57
  uri: request => graphqlReportUrl + "/" + request.operationName,
33
58
  });
34
- const authLink = setContext(async (_, { headers }) => {
35
- const newHeaders = {
36
- ...headers,
37
- ...tracingHeaders,
38
- Authorization: token ? `Bearer ${token}` : null,
39
- };
40
- if (global.language) {
41
- newHeaders["Accept-Language"] = global.language;
42
- }
43
- // remove once the token has this information
44
- const irisAppId = global.scope
45
- ? global.scope + "/" + global.module
46
- : null;
47
- if (irisAppId) {
48
- newHeaders["TU-IRIS-APP-ID"] = irisAppId;
49
- const manifestScopes = global.manifestScopes;
50
- if (Array.isArray(manifestScopes)) {
51
- newHeaders["TU-MANIFEST-SCOPES"] = manifestScopes.map(x => x.scope).join(",");
52
- }
53
- }
59
+ const authLink = setContext(async (_, { headers: existingHeaders }) => {
54
60
  return {
55
- headers: newHeaders,
61
+ headers: {
62
+ ...existingHeaders,
63
+ ...generateHeaders(token, tracingHeaders),
64
+ },
56
65
  };
57
66
  });
58
67
  const errorLink = onError(({ graphQLErrors, networkError, operation, forward }) => {
@@ -152,11 +161,7 @@ const createApolloClient = ({ graphqlManagerUrl, graphqlPublicUrl, graphqlIntern
152
161
  }
153
162
  const sseLink = new SSELink({
154
163
  url: `${graphqlInternalUrl}/stream`,
155
- headers: () => {
156
- return {
157
- Authorization: `Bearer ${token}`,
158
- };
159
- },
164
+ headers: () => generateHeaders(token, tracingHeaders),
160
165
  });
161
166
  // Split links based on operation type
162
167
  const splitLink = from([
@@ -196,7 +201,7 @@ const createApolloClient = ({ graphqlManagerUrl, graphqlPublicUrl, graphqlIntern
196
201
  };
197
202
  const useApolloClient = () => {
198
203
  const { graphqlManagerUrl, graphqlPublicUrl, graphqlInternalUrl, graphqlReportUrl, environment, tracingHeaders } = useEnvironment();
199
- const { token } = useToken();
204
+ const { token: currentToken } = useToken();
200
205
  const errorHandler = useErrorHandler();
201
206
  const [client] = useState(() => {
202
207
  return createApolloClient({
@@ -209,13 +214,16 @@ const useApolloClient = () => {
209
214
  graphqlReportUrl,
210
215
  isDev: environment === "dev",
211
216
  tracingHeaders,
212
- firstToken: token,
217
+ firstToken: currentToken,
213
218
  errorHandler,
214
219
  });
215
220
  });
216
- if (client.getToken() !== token) {
217
- client.setToken(token);
218
- }
221
+ useMemo(() => {
222
+ if (client.getToken() !== currentToken) {
223
+ client.setToken(currentToken);
224
+ }
225
+ return currentToken;
226
+ }, [client, currentToken]);
219
227
  return client;
220
228
  };
221
229
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/react-core-contexts",
3
- "version": "1.3.128",
3
+ "version": "1.3.130",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "engines": {
@@ -13,10 +13,10 @@
13
13
  "jest-fetch-mock": "^3.0.3",
14
14
  "@trackunit/iris-app-api": "1.3.101",
15
15
  "@trackunit/react-core-contexts-api": "1.4.100",
16
- "@trackunit/react-core-hooks": "1.3.102",
16
+ "@trackunit/react-core-hooks": "1.3.103",
17
17
  "@trackunit/i18n-library-translation": "1.3.106",
18
- "@trackunit/react-components": "1.4.114",
19
- "@trackunit/iris-app-runtime-core": "1.4.101",
18
+ "@trackunit/react-components": "1.4.115",
19
+ "@trackunit/iris-app-runtime-core": "1.4.102",
20
20
  "graphql": "^16.10.0",
21
21
  "graphql-sse": "^2.5.4"
22
22
  },