Skip to content

Commit

Permalink
fix: refresh Auth token if it's hard expired
Browse files Browse the repository at this point in the history
  • Loading branch information
dantio committed Mar 29, 2024
1 parent f20f688 commit 1d8410f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/api/traditional/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import {stringify} from 'qs';
import {EBayIAFTokenExpired, EBayIAFTokenInvalid, handleEBayError} from '../../errors/index.js';
import {
EBayAuthTokenIsHardExpired,
EBayIAFTokenExpired,
EBayIAFTokenInvalid,
handleEBayError
} from '../../errors/index.js';
import {ClientAlerts, Finding, Merchandising, Shopping, Trading, TraditionalApi} from '../../types/index.js';
import Api from '../index.js';
import ClientAlertsCalls from './clientAlerts/index.js';
Expand Down Expand Up @@ -150,14 +155,24 @@ export default class Traditional extends Api {
return await this.request(apiConfig, api, callName, fields);
} catch (error: any) {
// Try to refresh the token.
if (this.config.autoRefreshToken && (error.name === EBayIAFTokenExpired.name || error.name === EBayIAFTokenInvalid.name)) {
if (this.shouldRefreshToken(error)) {
return await this.request(apiConfig, api, callName, fields, true);
}

throw error;
}
};

private shouldRefreshToken(error: any) {
if (!this.config.autoRefreshToken) {
return false;
}

return error.name === EBayIAFTokenExpired.name
|| error.name === EBayIAFTokenInvalid.name
|| error.name === EBayAuthTokenIsHardExpired.name;
}

private async request(apiConfig: TraditionalApiConfig, api: TraditionalApi, callName: string, fields: Fields | null, refreshToken = false) {
try {
if (refreshToken) {
Expand Down
6 changes: 6 additions & 0 deletions src/errors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ export class EBayIAFTokenExpired extends EbayApiError {
public static readonly code = 21917053;
}

export class EBayAuthTokenIsHardExpired extends EbayApiError {
public static readonly code = 932;

This comment has been minimized.

Copy link
@nrathi

nrathi Mar 29, 2024

Out of curiosity, where did you find this code?

This comment has been minimized.

Copy link
@dantio

This comment has been minimized.

Copy link
@dantio

dantio Mar 29, 2024

Author Collaborator

I hope this code is set. I had no time to test it, therefore it's RC :)

This comment has been minimized.

Copy link
@nrathi

nrathi Mar 29, 2024

Ah but you're not exporting the code to consumers:

export declare class EBayError extends Error {
    readonly meta: any;
    readonly description: string;
    constructor(message: string, description?: string, meta?: any);
    toJSON(): {
        message: string;
        description: string;
        stack: string | undefined;
        type: string;
        meta: any;
    };
}

This comment has been minimized.

Copy link
@dantio

dantio Mar 29, 2024

Author Collaborator

Hm no. Why would you need the code? It's mapped now to EBayAuthTokenIsHardExpired Error. I think the code should also be included in the metafield.

This comment has been minimized.

Copy link
@nrathi

nrathi Mar 29, 2024

I just meant in general, because I transform eBay errors into user-facing ones, like this:

function isEBayBusinessPoliciesError(err: unknown) {
  return (
    err instanceof EBayError &&
    err.description.startsWith("User is not eligible for Business Policy.")
  );
}

=>

if (isEBayBusinessPoliciesError(err)) { 
  return "Please set up business policies in order to use this feature."
}

And I've noticed that the strings can change, so it's a bit gross to do string comparison.

This comment has been minimized.

Copy link
@dantio

dantio Mar 29, 2024

Author Collaborator

I see. It would be better to have err.code === 932

}

export class EBayIAFTokenInvalid extends EbayApiError {
public static readonly code = 21916984;
}
Expand Down Expand Up @@ -211,6 +215,8 @@ export const checkEBayResponse = (data: any) => {
throw new EBayIAFTokenInvalid(data);
case EBayTokenRequired.code:
throw new EBayTokenRequired(data);
case EBayAuthTokenIsHardExpired.code:

This comment has been minimized.

Copy link
@dantio

dantio Mar 29, 2024

Author Collaborator

@nrathi
Here is the code checked. If It's match, a custom error is thrown and the token should be refreshed.

throw new EBayAuthTokenIsHardExpired(data);
}
}

Expand Down

0 comments on commit 1d8410f

Please sign in to comment.