asma-core-helpers 0.27.1 → 0.28.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.
Files changed (30) hide show
  1. package/lib/collaboration/applyOperation.d.ts.map +1 -1
  2. package/lib/collaboration/applyOperation.js +19 -0
  3. package/lib/collaboration/applyOperation.js.map +1 -1
  4. package/lib/collaboration/schemas.d.ts +25 -0
  5. package/lib/collaboration/schemas.d.ts.map +1 -1
  6. package/lib/collaboration/schemas.js +23 -0
  7. package/lib/collaboration/schemas.js.map +1 -1
  8. package/lib/collaboration/templateDocument.d.ts +57 -0
  9. package/lib/collaboration/templateDocument.d.ts.map +1 -1
  10. package/lib/collaboration/templateDocument.js.map +1 -1
  11. package/lib/helpers/createTadaBrowserClient.d.ts +3 -1
  12. package/lib/helpers/createTadaBrowserClient.d.ts.map +1 -1
  13. package/lib/helpers/createTadaBrowserClient.js +12 -5
  14. package/lib/helpers/createTadaBrowserClient.js.map +1 -1
  15. package/lib/helpers/generateSrvAuthBindings.d.ts +1 -0
  16. package/lib/helpers/generateSrvAuthBindings.d.ts.map +1 -1
  17. package/lib/helpers/generateSrvAuthBindings.js +7 -0
  18. package/lib/helpers/generateSrvAuthBindings.js.map +1 -1
  19. package/lib/helpers/initTadaBrowserClient.d.ts.map +1 -1
  20. package/lib/helpers/initTadaBrowserClient.js +2 -1
  21. package/lib/helpers/initTadaBrowserClient.js.map +1 -1
  22. package/package.json +1 -1
  23. package/src/collaboration/applyOperation.test.ts +58 -0
  24. package/src/collaboration/applyOperation.ts +21 -0
  25. package/src/collaboration/schemas.test.ts +53 -0
  26. package/src/collaboration/schemas.ts +25 -0
  27. package/src/collaboration/templateDocument.ts +60 -1
  28. package/src/helpers/createTadaBrowserClient.ts +16 -4
  29. package/src/helpers/generateSrvAuthBindings.ts +11 -0
  30. package/src/helpers/initTadaBrowserClient.ts +7 -1
@@ -36,6 +36,8 @@ export interface CreateTadaBrowserClientOptions {
36
36
  url: string
37
37
  /** Resolves the current JWT. Typically `getCachedJwt` from asma-core-react. */
38
38
  getJwt: () => Promise<string | undefined>
39
+ /** Returns the synchronously cached JWT. Typically `getJwtToken` from auth bindings. */
40
+ getJwtToken: () => string | undefined
39
41
  /** Returns true when the cached JWT is still valid. Typically `isJwtValid` from asma-core-react. */
40
42
  isJwtValid: () => boolean
41
43
  /** Optional service-specific exchanges injected before fetchExchange. */
@@ -56,6 +58,7 @@ export interface CreateTadaBrowserClientOptions {
56
58
  export function createTadaBrowserClient({
57
59
  url,
58
60
  getJwt,
61
+ getJwtToken,
59
62
  isJwtValid,
60
63
  resolveExchanges,
61
64
  clientOptions,
@@ -63,7 +66,9 @@ export function createTadaBrowserClient({
63
66
  let tadaClient: Client | undefined
64
67
 
65
68
  const getClient = async () => {
66
- tadaClient = await buildClient({ clientOptions })
69
+ if (!tadaClient) {
70
+ tadaClient = await buildClient({ clientOptions })
71
+ }
67
72
  return tadaClient
68
73
  }
69
74
 
@@ -100,9 +105,14 @@ export function createTadaBrowserClient({
100
105
  if (!anonymous) {
101
106
  _clientOptions.exchanges.unshift(
102
107
  authExchange(async (utils) => {
103
- const token = await getJwt()
108
+ let token = await getJwt()
109
+
104
110
  return {
105
111
  addAuthToOperation(operation) {
112
+ if (!token) {
113
+ return operation
114
+ }
115
+
106
116
  return utils.appendHeaders(operation, {
107
117
  Authorization: `Bearer ${token}`,
108
118
  })
@@ -111,10 +121,12 @@ export function createTadaBrowserClient({
111
121
  return error.response.status === 401
112
122
  },
113
123
  async refreshAuth() {
114
- tadaClient = await buildClient({ clientOptions: opts?.clientOptions })
124
+ token = await getJwt()
115
125
  },
116
126
  willAuthError() {
117
- return !isJwtValid()
127
+ const tokenChanged = token !== getJwtToken()
128
+ const tokenInvalid = !isJwtValid()
129
+ return tokenChanged || tokenInvalid
118
130
  },
119
131
  }
120
132
  }),
@@ -38,6 +38,17 @@ export async function getCachedJwtInternal() {
38
38
  }
39
39
  return getCachedJwt()
40
40
  }
41
+
42
+ export function getJwtTokenInternal(): string | undefined {
43
+ const getJwtToken = realWindow.__ASMA__SHELL__?.auth_bindings?.getJwtToken
44
+
45
+ if (!getJwtToken) {
46
+ throw new Error(
47
+ 'getJwtToken is not defined! please make sure that generateSrvAuthBindings is called before getJwtToken',
48
+ )
49
+ }
50
+ return getJwtToken()
51
+ }
41
52
  export async function checkForRegisteredSubdomainInternal() {
42
53
  const checkForRegisteredSubdomain = realWindow.__ASMA__SHELL__?.auth_bindings?.checkForRegisteredSubdomain
43
54
 
@@ -15,7 +15,12 @@
15
15
  */
16
16
  import { createTadaBrowserClient, type ResolveExchanges } from './createTadaBrowserClient.js'
17
17
  import { EnvConfigsFnInternal, type IEnvironmentUrlsGenQLOnly } from './generateEnvConfigsBindings.js'
18
- import { getCachedJwtInternal, getSrvUrlsInternal, isJwtValidInternal } from './generateSrvAuthBindings.js'
18
+ import {
19
+ getCachedJwtInternal,
20
+ getJwtTokenInternal,
21
+ getSrvUrlsInternal,
22
+ isJwtValidInternal,
23
+ } from './generateSrvAuthBindings.js'
19
24
 
20
25
  /**
21
26
  * Resolves the GraphQL base URL for a service from EnvConfigs, preferring the
@@ -65,6 +70,7 @@ export function initTadaBrowserClient({
65
70
  return createTadaBrowserClient({
66
71
  url: `${resolveSrvUrl(service)}${path}`,
67
72
  getJwt: getCachedJwtInternal,
73
+ getJwtToken: getJwtTokenInternal,
68
74
  isJwtValid: isJwtValidInternal,
69
75
  resolveExchanges,
70
76
  })