Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upd interceptors, fix docs #52

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"jest": "^29.5.0",
"prettier": "^2.8.4",
"tsc-alias": "^1.8.2",
"typedoc": "^0.23.26",
"typedoc": "^0.25.13",
"typescript": "^5.0.4",
"yorkie": "^2.0.0"
},
Expand Down
30 changes: 26 additions & 4 deletions packages/jac/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export const bearerAttachInterceptor: FetcherRequestInterceptor = async (
return request
}

const refreshTokenStatus = {
isUpdating: false,
isErrored: false,
};

export const refreshTokenInterceptor: FetcherErrorResponseInterceptor = async (
response: FetcherResponse<unknown>,
) => {
Expand All @@ -63,15 +68,29 @@ export const refreshTokenInterceptor: FetcherErrorResponseInterceptor = async (
const { t } = i18n.global

try {
const accessToken = localStorage.getItem('accessToken')
if (!refreshTokenStatus.isUpdating) {
refreshTokenStatus.isUpdating = true;
refreshTokenStatus.isErrored = false;

// Executes some refresh token logic in the client app
const newToken = await api.get('/refresh', {...})
localStorage.setItem('accessToken', newToken)

refreshTokenStatus.isUpdating = false;
}

// Executes some refresh token logic in the client app
toolkitBus.emit(DEFAULT_BUS_EVENTS.refreshToken)
while(refreshTokenStatus.isUpdating) {
await sleep(50);
};

if (refreshTokenStatus.isErrored) { return response; }

const accessToken = localStorage.getItem('accessToken')

const url = new URL(config.url)

return new Fetcher({ baseUrl: url.origin }).request({
endpoint: url.pathname,
endpoint: config.url.replace(url.origin, ''),
method: config.method as HTTP_METHODS,
...(config.body ? { body: config.body } : {}),
headers: {
Expand All @@ -89,6 +108,9 @@ export const refreshTokenInterceptor: FetcherErrorResponseInterceptor = async (
* emit bus error event to show user that session expired
*/

refreshTokenStatus.isErrored = true;
refreshTokenStatus.isUpdating = false;

toolkitBus.emit(DEFAULT_BUS_EVENTS.logout)
toolkitBus.emit(DEFAULT_BUS_EVENTS.error, {
title: t('api-errors.session-expired-title'),
Expand Down
37 changes: 14 additions & 23 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6425,7 +6425,7 @@ __metadata:
languageName: node
linkType: hard

"marked@npm:^4.2.12":
"marked@npm:^4.3.0":
version: 4.3.0
resolution: "marked@npm:4.3.0"
bin:
Expand Down Expand Up @@ -6525,15 +6525,6 @@ __metadata:
languageName: node
linkType: hard

"minimatch@npm:^7.1.3":
version: 7.4.6
resolution: "minimatch@npm:7.4.6"
dependencies:
brace-expansion: ^2.0.1
checksum: 1a6c8d22618df9d2a88aabeef1de5622eb7b558e9f8010be791cb6b0fa6e102d39b11c28d75b855a1e377b12edc7db8ff12a99c20353441caa6a05e78deb5da9
languageName: node
linkType: hard

"minimatch@npm:^9.0.1, minimatch@npm:^9.0.3":
version: 9.0.4
resolution: "minimatch@npm:9.0.4"
Expand Down Expand Up @@ -6804,13 +6795,13 @@ __metadata:
linkType: hard

"node-gyp-build@npm:^4.2.2, node-gyp-build@npm:^4.3.0":
version: 4.8.0
resolution: "node-gyp-build@npm:4.8.0"
version: 4.8.1
resolution: "node-gyp-build@npm:4.8.1"
bin:
node-gyp-build: bin.js
node-gyp-build-optional: optional.js
node-gyp-build-test: build-test.js
checksum: b82a56f866034b559dd3ed1ad04f55b04ae381b22ec2affe74b488d1582473ca6e7f85fccf52da085812d3de2b0bf23109e752a57709ac7b9963951c710fea40
checksum: fe6e95da6f4608c1a98655f6bf2fe4e8dd9c877cd13256056a8acaf585cc7f98718823fe9366be11b78c2f332d5a184b00cf07a4af96c9d8fea45f640c019f98
languageName: node
linkType: hard

Expand Down Expand Up @@ -7835,7 +7826,7 @@ __metadata:
languageName: node
linkType: hard

"shiki@npm:^0.14.1":
"shiki@npm:^0.14.7":
version: 0.14.7
resolution: "shiki@npm:0.14.7"
dependencies:
Expand Down Expand Up @@ -8500,19 +8491,19 @@ __metadata:
languageName: node
linkType: hard

"typedoc@npm:^0.23.26":
version: 0.23.28
resolution: "typedoc@npm:0.23.28"
"typedoc@npm:^0.25.13":
version: 0.25.13
resolution: "typedoc@npm:0.25.13"
dependencies:
lunr: ^2.3.9
marked: ^4.2.12
minimatch: ^7.1.3
shiki: ^0.14.1
marked: ^4.3.0
minimatch: ^9.0.3
shiki: ^0.14.7
peerDependencies:
typescript: 4.6.x || 4.7.x || 4.8.x || 4.9.x || 5.0.x
typescript: 4.6.x || 4.7.x || 4.8.x || 4.9.x || 5.0.x || 5.1.x || 5.2.x || 5.3.x || 5.4.x
bin:
typedoc: bin/typedoc
checksum: 40eb4e207aac1b734e09400cf03f543642cc7b11000895198dd5a0d3166315759ccf4ac30a2915153597c5c186101c72bac2f1fc12b428184a9274d3a0e44c5e
checksum: 703d1f48137300b0ef3df1998a25ae745db3ca0b126f8dd1f7262918f11243a94d24dfc916cdba2baeb5a7d85d5a94faac811caf7f4fa6b7d07144dc02f7639f
languageName: node
linkType: hard

Expand Down Expand Up @@ -8814,7 +8805,7 @@ __metadata:
jest: ^29.5.0
prettier: ^2.8.4
tsc-alias: ^1.8.2
typedoc: ^0.23.26
typedoc: ^0.25.13
typescript: ^5.0.4
yorkie: ^2.0.0
languageName: unknown
Expand Down
Loading