amazon-creators-api 1.2.1 → 1.2.2
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 +1 -10
- package/dist/ApiClient.d.ts +107 -84
- package/dist/ApiClient.js +455 -304
- package/dist/auth/OAuth2TokenManager.d.ts +19 -0
- package/dist/auth/OAuth2TokenManager.js +55 -35
- package/package.json +3 -7
|
@@ -1,4 +1,23 @@
|
|
|
1
1
|
export = OAuth2TokenManager;
|
|
2
|
+
/**
|
|
3
|
+
* Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License").
|
|
6
|
+
* You may not use this file except in compliance with the License.
|
|
7
|
+
* A copy of the License is located at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* or in the "license" file accompanying this file. This file is distributed
|
|
12
|
+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
|
|
13
|
+
* express or implied. See the License for the specific language governing
|
|
14
|
+
* permissions and limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* OAuth2TokenManager
|
|
18
|
+
* Manages OAuth2 token lifecycle including acquisition, caching, and refresh
|
|
19
|
+
*
|
|
20
|
+
*/
|
|
2
21
|
/**
|
|
3
22
|
* OAuth2 token manager that handles token acquisition, caching, and automatic refresh
|
|
4
23
|
* Note: ApiClient automatically manages token caching for optimal performance.
|
|
@@ -24,15 +24,11 @@ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e
|
|
|
24
24
|
* express or implied. See the License for the specific language governing
|
|
25
25
|
* permissions and limitations under the License.
|
|
26
26
|
*/
|
|
27
|
-
|
|
28
27
|
/**
|
|
29
28
|
* OAuth2TokenManager
|
|
30
29
|
* Manages OAuth2 token lifecycle including acquisition, caching, and refresh
|
|
31
30
|
*
|
|
32
31
|
*/
|
|
33
|
-
|
|
34
|
-
var superagent = require("superagent");
|
|
35
|
-
|
|
36
32
|
/**
|
|
37
33
|
* OAuth2 token manager that handles token acquisition, caching, and automatic refresh
|
|
38
34
|
* Note: ApiClient automatically manages token caching for optimal performance.
|
|
@@ -101,61 +97,85 @@ var OAuth2TokenManager = /*#__PURE__*/function () {
|
|
|
101
97
|
key: "refreshToken",
|
|
102
98
|
value: (function () {
|
|
103
99
|
var _refreshToken = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2() {
|
|
104
|
-
var response,
|
|
100
|
+
var requestBody, headers, response, responseText, data, failure, expiresInMs, _t, _t2;
|
|
105
101
|
return _regenerator().w(function (_context2) {
|
|
106
102
|
while (1) switch (_context2.p = _context2.n) {
|
|
107
103
|
case 0:
|
|
108
104
|
_context2.p = 0;
|
|
109
|
-
if (
|
|
110
|
-
|
|
111
|
-
|
|
105
|
+
if (this.config.isLwa()) {
|
|
106
|
+
// LWA (v3.x) uses JSON body
|
|
107
|
+
headers = {
|
|
108
|
+
'Content-Type': 'application/json'
|
|
109
|
+
};
|
|
110
|
+
requestBody = JSON.stringify({
|
|
111
|
+
grant_type: this.config.getGrantType(),
|
|
112
|
+
client_id: this.config.getCredentialId(),
|
|
113
|
+
client_secret: this.config.getCredentialSecret(),
|
|
114
|
+
scope: this.config.getScope()
|
|
115
|
+
});
|
|
116
|
+
} else {
|
|
117
|
+
// Cognito (v2.x) uses form-encoded
|
|
118
|
+
headers = {
|
|
119
|
+
'Content-Type': 'application/x-www-form-urlencoded'
|
|
120
|
+
};
|
|
121
|
+
requestBody = new URLSearchParams({
|
|
122
|
+
'grant_type': this.config.getGrantType(),
|
|
123
|
+
'client_id': this.config.getCredentialId(),
|
|
124
|
+
'client_secret': this.config.getCredentialSecret(),
|
|
125
|
+
'scope': this.config.getScope()
|
|
126
|
+
}).toString();
|
|
112
127
|
}
|
|
113
128
|
_context2.n = 1;
|
|
114
|
-
return
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
scope: this.config.getScope()
|
|
129
|
+
return fetch(this.config.getCognitoEndpoint(), {
|
|
130
|
+
method: 'POST',
|
|
131
|
+
headers: headers,
|
|
132
|
+
body: requestBody
|
|
119
133
|
});
|
|
120
134
|
case 1:
|
|
121
135
|
response = _context2.v;
|
|
122
|
-
_context2.n =
|
|
123
|
-
|
|
136
|
+
_context2.n = 2;
|
|
137
|
+
return response.text();
|
|
124
138
|
case 2:
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
'scope': this.config.getScope()
|
|
131
|
-
}).toString();
|
|
132
|
-
_context2.n = 3;
|
|
133
|
-
return superagent.post(this.config.getCognitoEndpoint()).set('Content-Type', 'application/x-www-form-urlencoded').send(requestBody);
|
|
134
|
-
case 3:
|
|
135
|
-
response = _context2.v;
|
|
139
|
+
responseText = _context2.v;
|
|
140
|
+
_context2.p = 3;
|
|
141
|
+
data = responseText ? JSON.parse(responseText) : {};
|
|
142
|
+
_context2.n = 5;
|
|
143
|
+
break;
|
|
136
144
|
case 4:
|
|
137
|
-
|
|
145
|
+
_context2.p = 4;
|
|
146
|
+
_t = _context2.v;
|
|
147
|
+
throw new Error("Invalid token response format: ".concat(responseText));
|
|
148
|
+
case 5:
|
|
149
|
+
if (response.ok) {
|
|
150
|
+
_context2.n = 6;
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
failure = new Error("OAuth2 token request failed with status ".concat(response.status));
|
|
154
|
+
failure.status = response.status;
|
|
155
|
+
failure.body = data;
|
|
156
|
+
throw failure;
|
|
157
|
+
case 6:
|
|
138
158
|
if (data.access_token) {
|
|
139
|
-
_context2.n =
|
|
159
|
+
_context2.n = 7;
|
|
140
160
|
break;
|
|
141
161
|
}
|
|
142
162
|
throw new Error('No access token received from OAuth2 endpoint');
|
|
143
|
-
case
|
|
163
|
+
case 7:
|
|
144
164
|
this.accessToken = data.access_token;
|
|
145
165
|
// Set expiration time with a 30-second buffer to avoid edge cases
|
|
146
166
|
expiresInMs = (data.expires_in - 30) * 1000;
|
|
147
167
|
this.expiresAt = Date.now() + expiresInMs;
|
|
148
168
|
return _context2.a(2, this.accessToken);
|
|
149
|
-
case
|
|
150
|
-
_context2.p =
|
|
151
|
-
|
|
169
|
+
case 8:
|
|
170
|
+
_context2.p = 8;
|
|
171
|
+
_t2 = _context2.v;
|
|
152
172
|
// Clear existing token on failure
|
|
153
173
|
this.clearToken();
|
|
154
|
-
throw
|
|
155
|
-
case
|
|
174
|
+
throw _t2;
|
|
175
|
+
case 9:
|
|
156
176
|
return _context2.a(2);
|
|
157
177
|
}
|
|
158
|
-
}, _callee2, this, [[0,
|
|
178
|
+
}, _callee2, this, [[3, 4], [0, 8]]);
|
|
159
179
|
}));
|
|
160
180
|
function refreshToken() {
|
|
161
181
|
return _refreshToken.apply(this, arguments);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "amazon-creators-api",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.2",
|
|
4
4
|
"description": "Amazon Creators API NodeJS SDK",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"test": "mocha --require @babel/register --recursive --allow-empty"
|
|
22
22
|
},
|
|
23
23
|
"engines": {
|
|
24
|
-
"node": ">=
|
|
24
|
+
"node": ">=18.0.0"
|
|
25
25
|
},
|
|
26
26
|
"keywords": [
|
|
27
27
|
"amazon",
|
|
@@ -30,11 +30,7 @@
|
|
|
30
30
|
"api-client"
|
|
31
31
|
],
|
|
32
32
|
"browser": {
|
|
33
|
-
"fs": false
|
|
34
|
-
"superagent": "superagent/lib/node/index.js"
|
|
35
|
-
},
|
|
36
|
-
"dependencies": {
|
|
37
|
-
"superagent": "^5.3.0"
|
|
33
|
+
"fs": false
|
|
38
34
|
},
|
|
39
35
|
"devDependencies": {
|
|
40
36
|
"@babel/cli": "^7.0.0",
|