- 
  Notifications
 
You must be signed in to change notification settings  - Fork 2.1k
 
feat: add refreshTokenHandler option #7237
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| --- | ||
| "@vue-storefront/sdk": minor | ||
| --- | ||
| 
  | 
||
| [ADDED] new option to the `middlewareModule` - `refreshTokenHandler`. | ||
| This special handler can be used to handle 401 errors and refresh the token. | ||
| It is called before the generic `errorHandler`. | ||
| By default, it thrown an error which is being caught by the `errorHandler` and rethrown. | ||
| 
  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that some small diagram (even ascii) with the sequence will be useful here. Or maybe we can somehow rephrase this sentence, I think I would not understand it without my knowledge about how it works. Something like an explanation that by default we are not able to predict the mechanism of token refresh because of differences in different services therefore we do not provide any default behavior, we simply push the error to the errorHandler without any further actions.  | 
||
| 
  | 
||
| Example: | ||
| 
  | 
||
| ```ts | ||
| import { SdkHttpError } from "@vue-storefront/sdk"; | ||
| 
  | 
||
| const refereshToken = async () => { | ||
| // Refresh the token | ||
| }; | ||
| 
  | 
||
| const options: Options = { | ||
| apiUrl: "https://api.example.com", | ||
| refreshTokenHandler: async ({ | ||
| error, | ||
| methodName, | ||
| url, | ||
| params, | ||
| config, | ||
| 
  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a module config? Or what kind of config? Will I have some docs in TS? If not maybe lets explain the injected object here?  | 
||
| httpClient, | ||
| }) => { | ||
| try { | ||
| await refreshToken(); | ||
| } catch (error) { | ||
| throw new SdkHttpError({ | ||
| statusCode: 401, | ||
| message: "Unauthorized", | ||
| cause: error, | ||
| }); | ||
| } | ||
| 
  | 
||
| throw error; | ||
| }, | ||
| }; | ||
| ``` | ||