@webority/web-core 0.163.0 → 0.164.0
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/README.md
CHANGED
|
@@ -278,6 +278,8 @@ const entityTag = entityTagOf(response);
|
|
|
278
278
|
await userClient.put(`/leaves/${id}`, body, withIfMatch(versionOf(response.data, entityTag)));
|
|
279
279
|
```
|
|
280
280
|
|
|
281
|
+
An endpoint's `transformResponse(data, meta)` reads the same ETag from `meta.entityTag`, and its `transformErrorResponse(error, meta)` reads it on a failure. A save that returns a new version is therefore read in the save's own endpoint, with no refetch. `entityTag` is the strong ETag without quotes, and it is undefined for a weak or missing one.
|
|
282
|
+
|
|
281
283
|
## Formatters
|
|
282
284
|
|
|
283
285
|
Called with no options, the house formatters print (zone IST, locale en-IN, empty `-`):
|
package/dist/api/tokenRefresh.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{ACCESS_TOKEN_KEY as e,REFRESH_TOKEN_KEY as t,clearSession as n,getToken as r,storeToken as i}from"../auth/authStore.js";import{Logger as a}from"../logger/logger.js";import{withCrossTabRefreshLock as o}from"./crossTabRefreshLock.js";import s from"./publicClient.js";import c from"axios";var l=`session_revoked`,u=`session_expired`,d=class e extends Error{constructor(t){super(t),this.name=`SessionEndedError`,Object.setPrototypeOf(this,e.prototype)}},f,p,m=e=>{f=e??void 0},h=e=>{p=e},g=null,_=[],v=(e,t=null)=>{_.forEach(({resolve:n,reject:r})=>{e?r(e):t!==null&&n(t)}),_=[]},y=()=>{g=null,v(new d(`[@webority/web-core] Session ended`))},b=e=>e instanceof Error?e.message:typeof e==`string`?e:`Unknown error`,x=()=>{if(!f||!p)throw Error(`User package store is not initialized. Call initUserAuth first.`);return{store:f,auth:p}},S=()=>p?.accountId,C=()=>p?.refresh.path,w=async()=>{let e=x();await e.store.dispatch(e.auth.logout())},T=async(e,t,r)=>{await n(e,{code:t,detail:r}),await w()},E=e=>{let t=e&&typeof e==`object`&&`code`in e?e.code:void 0;return t===`session_revoked`||t===`session_expired`?t:null},D=e=>{let t=e&&typeof e==`object`&&`detail`in e?e.detail:void 0;return typeof t==`string`&&t!==``&&E(e)!==null?t:null},O=e=>{if(!c.isAxiosError(e)||!e.response)return null;let t=e.response.data,n=t&&typeof t==`object`&&`code`in t?t.code:void 0;return typeof n==`string`&&p?.endSessionOn?.includes(n)?n:null},k=()=>g!==null,A=0,j=Promise.resolve(),M=null,N=()=>{A===0&&(j=new Promise(e=>{M=e})),A+=1;let e=!1;return()=>{e||(e=!0,--A,A===0&&(M?.(),M=null))}},P=()=>A===0?Promise.resolve():j,F=class e extends Error{constructor(t){super(`A session-replacing response carried no tokens: ${t}`),this.name=`ReplacementTokensMissingError`,Object.setPrototypeOf(this,e.prototype)}},I=async n=>{let r;try{r=(p?.refresh.readTokens??
|
|
1
|
+
import{ACCESS_TOKEN_KEY as e,REFRESH_TOKEN_KEY as t,clearSession as n,getToken as r,storeToken as i}from"../auth/authStore.js";import{Logger as a}from"../logger/logger.js";import{withCrossTabRefreshLock as o}from"./crossTabRefreshLock.js";import s from"./publicClient.js";import c from"axios";var l=`session_revoked`,u=`session_expired`,d=class e extends Error{constructor(t){super(t),this.name=`SessionEndedError`,Object.setPrototypeOf(this,e.prototype)}},f,p,m=e=>{f=e??void 0},h=e=>{p=e},g=null,_=[],v=(e,t=null)=>{_.forEach(({resolve:n,reject:r})=>{e?r(e):t!==null&&n(t)}),_=[]},y=()=>{g=null,v(new d(`[@webority/web-core] Session ended`))},b=e=>e instanceof Error?e.message:typeof e==`string`?e:`Unknown error`,x=()=>{if(!f||!p)throw Error(`User package store is not initialized. Call initUserAuth first.`);return{store:f,auth:p}},S=()=>p?.accountId,C=()=>p?.refresh.path,w=async()=>{let e=x();await e.store.dispatch(e.auth.logout())},T=async(e,t,r)=>{await n(e,{code:t,detail:r}),await w()},E=e=>{let t=e&&typeof e==`object`&&`code`in e?e.code:void 0;return t===`session_revoked`||t===`session_expired`?t:null},D=e=>{let t=e&&typeof e==`object`&&`detail`in e?e.detail:void 0;return typeof t==`string`&&t!==``&&E(e)!==null?t:null},O=e=>{if(!c.isAxiosError(e)||!e.response)return null;let t=e.response.data,n=t&&typeof t==`object`&&`code`in t?t.code:void 0;return typeof n==`string`&&p?.endSessionOn?.includes(n)?n:null},k=()=>g!==null,A=0,j=Promise.resolve(),M=null,N=()=>{A===0&&(j=new Promise(e=>{M=e})),A+=1;let e=!1;return()=>{e||(e=!0,--A,A===0&&(M?.(),M=null))}},P=()=>A===0?Promise.resolve():j,F=class e extends Error{constructor(t){super(`A session-replacing response carried no tokens: ${t}`),this.name=`ReplacementTokensMissingError`,Object.setPrototypeOf(this,e.prototype)}},I=async n=>{let r;try{r=(p?.refresh.readTokens??K)(n)}catch(e){throw new F(b(e))}await i(e,r.accessToken,r.accessTokenExpiry),await i(t,r.refreshToken,r.refreshTokenExpiry)},L=async e=>{let t=(await r(`accessToken`))?.token??null;return t!==null&&t!==e?t:null},R=()=>new Promise((e,t)=>{_.push({resolve:e,reject:t})}),z=e=>e instanceof d||c.isAxiosError(e)&&(e.response?.status===400||e.response?.status===401),B=e=>!c.isAxiosError(e)||e.response?.status!==401?null:E(e.response.data),V=e=>!c.isAxiosError(e)||e.response?.status!==401?null:D(e.response.data),H=e=>z(e)||O(e)!==null,U=e=>c.isAxiosError(e)?E(e.response?.data)??`status ${e.response?.status}`:b(e),W=(e,t)=>{if(typeof e!=`string`||e===``)throw Error(`The refresh response has no "${t}".`);return e},G=(e,t)=>{let n=W(e,t);if(Number.isNaN(Date.parse(n)))throw Error(`The refresh response's "${t}" is not a date.`);return n},K=e=>{if(typeof e!=`object`||!e)throw Error(`The refresh response is not an object.`);return{accessToken:W(`accessToken`in e?e.accessToken:void 0,`accessToken`),accessTokenExpiry:G(`accessTokenExpiresDateTimeUtc`in e?e.accessTokenExpiresDateTimeUtc:void 0,`accessTokenExpiresDateTimeUtc`),refreshToken:W(`refreshToken`in e?e.refreshToken:void 0,`refreshToken`),refreshTokenExpiry:G(`refreshTokenExpiresDateTimeUtc`in e?e.refreshTokenExpiresDateTimeUtc:void 0,`refreshTokenExpiresDateTimeUtc`)}},q=async n=>{await P();let o=await r(t);if(!o?.token)throw new d(`No refresh token available`);if(Date.parse(o.expiry)<=Date.now())throw new d(`Refresh token expired`);let c={accessToken:(await r(`accessToken`))?.token??null,refreshToken:o.token},l=n.body?await n.body(c):c,u=n.headers?await n.headers():void 0;a.info(`Refreshing the access token`);let{data:f}=await s.post(n.path,l,{...u?{headers:u}:{},skipErrorLog:!0}),p=(n.readTokens??K)(f);await P();let m=(await r(`refreshToken`))?.token===o.token?null:await r(e);return m?.token?(a.info(`Keeping the session that replaced this one during the refresh`),m.token):(await i(e,p.accessToken,p.accessTokenExpiry),await i(t,p.refreshToken,p.refreshTokenExpiry),a.info(`Access token refreshed`),p.accessToken)},J=3e4,Y=async(t,n)=>{let i=await r(e);return i?.token&&i.token!==n&&Date.parse(i.expiry)>Date.now()+J?(a.info(`Adopting the access token another tab refreshed`),i.token):q(t)},X=()=>{if(g)return g;let e=(async()=>{let e=null;try{let{refresh:t}=x().auth;e=(await r(`accessToken`))?.token??null;let n=await o(()=>Y(t,e));return v(null,n),n}catch(t){if(z(t)){await P();let t=null;try{t=await L(e)}catch(e){a.error(`Reading the stored token after a failed refresh failed:`,b(e))}if(t)return a.info(`The session was replaced during the refresh; continuing with the new one`),v(null,t),t}let n=O(t);if(n!==null)throw a.info(`Session ended, the refresh was refused: ${n}`),v(t,null),await T(`terminated`,n,D(c.isAxiosError(t)?t.response?.data:void 0)),t;let r=z(t);if(r)a.info(`Session ended, the refresh was refused:`,U(t));else{let e=c.isAxiosError(t)?t.response?.data:void 0;a.error(`Token refresh failed:`,e??b(t))}throw v(t,null),r&&await T(`rejected`,B(t),V(t)),t}})().finally(()=>{g===e&&(g=null)});return g=e,e};export{u as SESSION_EXPIRED_CODE,l as SESSION_REVOKED_CODE,d as SessionEndedError,N as beginSessionReplacement,L as currentTokenIfReplaced,w as dispatchLogout,T as endSessionAndLogout,O as endSessionCodeOf,S as getAccountIdProvider,b as getErrorMessage,C as getRefreshPath,m as injectStore,h as injectUserAuth,k as isRefreshInFlight,H as isSessionEnd,R as queueBehindRefresh,X as refreshAccessToken,y as resetRefreshState,E as sessionCodeOf,D as sessionDetailOf,I as storeReplacementSession,P as waitForSessionReplacement};
|
|
@@ -3,7 +3,7 @@ import type { AxiosInstance } from "axios";
|
|
|
3
3
|
* Resolves `baseURL` PER REQUEST rather than at module evaluation.
|
|
4
4
|
*
|
|
5
5
|
* `axios.create({ baseURL: getConfigValue('baseUrl') })` reads the value once,
|
|
6
|
-
* when the module is first imported
|
|
6
|
+
* when the module is first imported, which happens the moment anything touches
|
|
7
7
|
* the package barrel, long before an app's startup code can call `setConfig`.
|
|
8
8
|
* The result was a client permanently pinned to `undefined` and a `setConfig`
|
|
9
9
|
* that appeared to do nothing, with no error anywhere: requests just went to a
|
|
@@ -11,7 +11,7 @@ export interface FormatCurrencyOptions extends Omit<Intl.NumberFormatOptions, "s
|
|
|
11
11
|
minimumFractionDigits?: number;
|
|
12
12
|
/** Maximum fraction digits. Defaults to the currency's own. */
|
|
13
13
|
maximumFractionDigits?: number;
|
|
14
|
-
/** Strip the currency symbol
|
|
14
|
+
/** Strip the currency symbol; useful when the symbol is rendered separately. */
|
|
15
15
|
stripSymbol?: boolean;
|
|
16
16
|
/**
|
|
17
17
|
* What a missing amount prints. Defaults to the configured placeholder once the app has
|
|
@@ -28,7 +28,7 @@ export interface TokenRefreshConfig {
|
|
|
28
28
|
headers?: () => Record<string, string> | Promise<Record<string, string>>;
|
|
29
29
|
/**
|
|
30
30
|
* Reads the new tokens from the response body. Defaults to the Webority API's
|
|
31
|
-
* `{ accessToken,
|
|
31
|
+
* `{ accessToken, accessTokenExpiresDateTimeUtc, refreshToken, refreshTokenExpiresDateTimeUtc }`.
|
|
32
32
|
*/
|
|
33
33
|
readTokens?: (data: unknown) => TokenRefreshResult;
|
|
34
34
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webority/web-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.164.0",
|
|
4
4
|
"description": "Platform layer for Webority React web portals: HTTP clients with single-flight token refresh, auth store, API error extraction, RTK Query base query, logger, formatters, storage, version check and route guards. No UI.",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE",
|
|
6
6
|
"type": "module",
|
|
@@ -98,7 +98,7 @@
|
|
|
98
98
|
},
|
|
99
99
|
"sideEffects": false,
|
|
100
100
|
"dependencies": {
|
|
101
|
-
"@webority/api-contract": "0.
|
|
101
|
+
"@webority/api-contract": "0.164.0"
|
|
102
102
|
},
|
|
103
103
|
"peerDependencies": {
|
|
104
104
|
"@microsoft/applicationinsights-web": "^3.4.5",
|
|
@@ -120,7 +120,7 @@
|
|
|
120
120
|
"@types/node": "25.6.0",
|
|
121
121
|
"@types/react": "^19.3.0",
|
|
122
122
|
"@vitest/coverage-v8": "5.1.0",
|
|
123
|
-
"@webority/biome-config": "0.
|
|
123
|
+
"@webority/biome-config": "0.164.0",
|
|
124
124
|
"axios": "^1.18.0",
|
|
125
125
|
"jsdom": "30.1.1",
|
|
126
126
|
"react": "^19.3.1",
|