@sp-api-sdk/auth
Advanced tools
Comparing version 2.1.4 to 2.2.0
@@ -19,4 +19,4 @@ "use strict"; | ||
scopes; | ||
accessToken; | ||
accessTokenExpiration; | ||
#accessToken; | ||
#accessTokenExpiration; | ||
constructor(parameters) { | ||
@@ -44,4 +44,4 @@ const clientId = parameters.clientId ?? node_process_1.default.env.LWA_CLIENT_ID; | ||
async getAccessToken() { | ||
if (!this.accessToken || | ||
(this.accessTokenExpiration && Date.now() >= this.accessTokenExpiration.getTime())) { | ||
if (!this.#accessToken || | ||
(this.#accessTokenExpiration && Date.now() >= this.#accessTokenExpiration.getTime())) { | ||
const body = { | ||
@@ -64,4 +64,4 @@ client_id: this.clientId, | ||
expiration.setSeconds(expiration.getSeconds() + data.expires_in); | ||
this.accessToken = data.access_token; | ||
this.accessTokenExpiration = expiration; | ||
this.#accessToken = data.access_token; | ||
this.#accessTokenExpiration = expiration; | ||
} | ||
@@ -75,4 +75,10 @@ catch (error) { | ||
} | ||
return this.accessToken; | ||
return this.#accessToken; | ||
} | ||
/** | ||
* Access token expiration date | ||
*/ | ||
get accessTokenExpiration() { | ||
return this.#accessTokenExpiration; | ||
} | ||
} | ||
@@ -79,0 +85,0 @@ exports.SellingPartnerApiAuth = SellingPartnerApiAuth; |
@@ -13,4 +13,4 @@ import process from 'node:process'; | ||
scopes; | ||
accessToken; | ||
accessTokenExpiration; | ||
#accessToken; | ||
#accessTokenExpiration; | ||
constructor(parameters) { | ||
@@ -38,4 +38,4 @@ const clientId = parameters.clientId ?? process.env.LWA_CLIENT_ID; | ||
async getAccessToken() { | ||
if (!this.accessToken || | ||
(this.accessTokenExpiration && Date.now() >= this.accessTokenExpiration.getTime())) { | ||
if (!this.#accessToken || | ||
(this.#accessTokenExpiration && Date.now() >= this.#accessTokenExpiration.getTime())) { | ||
const body = { | ||
@@ -58,4 +58,4 @@ client_id: this.clientId, | ||
expiration.setSeconds(expiration.getSeconds() + data.expires_in); | ||
this.accessToken = data.access_token; | ||
this.accessTokenExpiration = expiration; | ||
this.#accessToken = data.access_token; | ||
this.#accessTokenExpiration = expiration; | ||
} | ||
@@ -69,6 +69,12 @@ catch (error) { | ||
} | ||
return this.accessToken; | ||
return this.#accessToken; | ||
} | ||
/** | ||
* Access token expiration date | ||
*/ | ||
get accessTokenExpiration() { | ||
return this.#accessTokenExpiration; | ||
} | ||
} | ||
export { SellingPartnerApiAuthError } from './error'; | ||
export { AuthorizationScope } from './types/scope'; |
@@ -13,2 +13,3 @@ import { type RequireExactlyOne } from 'type-fest'; | ||
export declare class SellingPartnerApiAuth { | ||
#private; | ||
private readonly clientId; | ||
@@ -18,4 +19,2 @@ private readonly clientSecret; | ||
private readonly scopes?; | ||
private accessToken?; | ||
private accessTokenExpiration?; | ||
constructor(parameters: RequireExactlyOne<SellingPartnerAuthParameters, 'refreshToken' | 'scopes'>); | ||
@@ -26,4 +25,8 @@ /** | ||
getAccessToken(): Promise<string>; | ||
/** | ||
* Access token expiration date | ||
*/ | ||
protected get accessTokenExpiration(): Date | undefined; | ||
} | ||
export { SellingPartnerApiAuthError } from './error'; | ||
export { AuthorizationScope } from './types/scope'; |
@@ -5,3 +5,3 @@ { | ||
"description": "Amazon Selling Partner API authentication package", | ||
"version": "2.1.4", | ||
"version": "2.2.0", | ||
"main": "dist/cjs/index.js", | ||
@@ -22,3 +22,3 @@ "module": "dist/es/index.js", | ||
"dependencies": { | ||
"axios": "^1.7.7", | ||
"axios": "^1.7.8", | ||
"read-pkg-up": "^7.0.1" | ||
@@ -45,3 +45,3 @@ }, | ||
], | ||
"gitHead": "56483dede2148dad3e8939b5177fc571ba95c196" | ||
"gitHead": "0216c483bb4c4a7966c1d1b2eeab3572a6b40348" | ||
} |
@@ -5,5 +5,10 @@ # `@sp-api-sdk/auth` | ||
[![npm version](https://badgen.net/npm/v/@sp-api-sdk/auth)](https://www.npmjs.com/package/@sp-api-sdk/auth) | ||
[![XO code style](https://badgen.net/badge/code%20style/XO/cyan)](https://github.com/xojs/xo) | ||
## Installing | ||
- `npm install @sp-api-sdk/auth` | ||
```sh | ||
npm install @sp-api-sdk/auth | ||
``` | ||
@@ -32,3 +37,3 @@ ## Default values from the environment | ||
clientSecret: "", | ||
scopes: Object.values(AuthorizationScope), // Or choose the only ones you need | ||
scopes: [AuthorizationScope.NOTIFICATIONS, AuthorizationScope.CLIENT_CREDENTIAL_ROTATION], // Or choose the only ones you need | ||
}); | ||
@@ -41,2 +46,42 @@ | ||
`getAccessToken()` caches the access token for its whole duration, it will only request a new token if the current one has expired. | ||
`getAccessToken()` caches the access token in memory for its whole duration, it will only request a new token if the current one has expired. | ||
## Subclassing | ||
You can subclass `SellingPartnerApiAuth` to add custom logic, for example, caching the access token in a store. | ||
```typescript | ||
import { SellingPartnerApiAuth } from "@sp-api-sdk/auth"; | ||
import { storeToken, getToken } from "./token-store"; | ||
class StoredSellingPartnerApiAuth extends SellingPartnerApiAuth { | ||
async getAccessToken() { | ||
let token = await getToken(); | ||
if (token) { | ||
return token; | ||
} | ||
token = await super.getAccessToken(); | ||
await storeToken(token, { ttl: this.accessTokenExpiration }); | ||
return token; | ||
} | ||
} | ||
``` | ||
## License | ||
MIT | ||
## Miscellaneous | ||
``` | ||
╚⊙ ⊙╝ | ||
╚═(███)═╝ | ||
╚═(███)═╝ | ||
╚═(███)═╝ | ||
╚═(███)═╝ | ||
╚═(███)═╝ | ||
╚═(███)═╝ | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16090
309
85
Updatedaxios@^1.7.8