@aws-amplify/api-rest 3.5.6-unstable.7762f1a.0 → 4.0.0

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.
Files changed (147) hide show
  1. package/README.md +3 -0
  2. package/internals/package.json +8 -0
  3. package/internals/server/package.json +8 -0
  4. package/lib/apis/common/handler.d.ts +27 -0
  5. package/lib/apis/common/handler.js +73 -0
  6. package/lib/apis/common/internalPost.d.ts +16 -0
  7. package/lib/apis/common/internalPost.js +65 -0
  8. package/lib/apis/common/publicApis.d.ts +8 -0
  9. package/lib/apis/common/publicApis.js +44 -0
  10. package/lib/apis/index.d.ts +203 -0
  11. package/lib/apis/index.js +215 -0
  12. package/lib/apis/server.d.ts +151 -0
  13. package/lib/apis/server.js +162 -0
  14. package/lib/errors/CanceledError.d.ts +17 -0
  15. package/lib/errors/CanceledError.js +32 -0
  16. package/lib/errors/RestApiError.d.ts +4 -0
  17. package/lib/errors/RestApiError.js +15 -0
  18. package/lib/errors/assertValidatonError.d.ts +5 -0
  19. package/lib/errors/assertValidatonError.js +17 -0
  20. package/lib/errors/index.d.ts +4 -0
  21. package/lib/errors/index.js +15 -0
  22. package/lib/errors/validation.d.ts +6 -0
  23. package/lib/errors/validation.js +19 -0
  24. package/lib/index.d.ts +2 -2
  25. package/lib/index.js +10 -6
  26. package/lib/internals/index.d.ts +19 -0
  27. package/lib/internals/index.js +30 -0
  28. package/lib/internals/server.d.ts +20 -0
  29. package/lib/internals/server.js +30 -0
  30. package/lib/server.d.ts +2 -0
  31. package/lib/server.js +14 -0
  32. package/lib/tsconfig.tsbuildinfo +1 -0
  33. package/lib/types/index.d.ts +90 -37
  34. package/lib/types/index.js +0 -23
  35. package/lib/utils/constants.d.ts +8 -0
  36. package/lib/utils/constants.js +13 -0
  37. package/lib/utils/createCancellableOperation.d.ts +12 -0
  38. package/lib/utils/createCancellableOperation.js +63 -0
  39. package/lib/utils/index.d.ts +6 -0
  40. package/lib/utils/index.js +17 -0
  41. package/lib/utils/logger.d.ts +2 -0
  42. package/lib/utils/logger.js +7 -0
  43. package/lib/utils/normalizeHeaders.d.ts +1 -0
  44. package/lib/utils/normalizeHeaders.js +13 -0
  45. package/lib/utils/parseSigningInfo.d.ts +14 -0
  46. package/lib/utils/parseSigningInfo.js +41 -0
  47. package/lib/utils/resolveApiUrl.d.ts +12 -0
  48. package/lib/utils/resolveApiUrl.js +40 -0
  49. package/lib/utils/resolveCredentials.d.ts +5 -0
  50. package/lib/utils/resolveCredentials.js +15 -0
  51. package/lib/utils/serviceError.d.ts +10 -0
  52. package/lib/utils/serviceError.js +30 -0
  53. package/lib-esm/apis/common/handler.d.ts +27 -0
  54. package/lib-esm/apis/common/handler.js +69 -0
  55. package/lib-esm/apis/common/internalPost.d.ts +16 -0
  56. package/lib-esm/apis/common/internalPost.js +59 -0
  57. package/lib-esm/apis/common/publicApis.d.ts +8 -0
  58. package/lib-esm/apis/common/publicApis.js +35 -0
  59. package/lib-esm/apis/index.d.ts +203 -0
  60. package/lib-esm/apis/index.js +206 -0
  61. package/lib-esm/apis/server.d.ts +151 -0
  62. package/lib-esm/apis/server.js +153 -0
  63. package/lib-esm/errors/CanceledError.d.ts +17 -0
  64. package/lib-esm/errors/CanceledError.js +27 -0
  65. package/lib-esm/errors/RestApiError.d.ts +4 -0
  66. package/lib-esm/errors/RestApiError.js +11 -0
  67. package/lib-esm/errors/assertValidatonError.d.ts +5 -0
  68. package/lib-esm/errors/assertValidatonError.js +13 -0
  69. package/lib-esm/errors/index.d.ts +4 -0
  70. package/lib-esm/errors/index.js +6 -0
  71. package/lib-esm/errors/validation.d.ts +6 -0
  72. package/lib-esm/errors/validation.js +16 -0
  73. package/lib-esm/index.d.ts +2 -2
  74. package/lib-esm/index.js +2 -3
  75. package/lib-esm/internals/index.d.ts +19 -0
  76. package/lib-esm/internals/index.js +24 -0
  77. package/lib-esm/internals/server.d.ts +20 -0
  78. package/lib-esm/internals/server.js +24 -0
  79. package/lib-esm/server.d.ts +2 -0
  80. package/lib-esm/server.js +4 -0
  81. package/lib-esm/tsconfig.tsbuildinfo +1 -0
  82. package/lib-esm/types/index.d.ts +90 -37
  83. package/lib-esm/types/index.js +1 -23
  84. package/lib-esm/utils/constants.d.ts +8 -0
  85. package/lib-esm/utils/constants.js +10 -0
  86. package/lib-esm/utils/createCancellableOperation.d.ts +12 -0
  87. package/lib-esm/utils/createCancellableOperation.js +59 -0
  88. package/lib-esm/utils/index.d.ts +6 -0
  89. package/lib-esm/utils/index.js +8 -0
  90. package/lib-esm/utils/logger.d.ts +2 -0
  91. package/lib-esm/utils/logger.js +4 -0
  92. package/lib-esm/utils/normalizeHeaders.d.ts +1 -0
  93. package/lib-esm/utils/normalizeHeaders.js +9 -0
  94. package/lib-esm/utils/parseSigningInfo.d.ts +14 -0
  95. package/lib-esm/utils/parseSigningInfo.js +37 -0
  96. package/lib-esm/utils/resolveApiUrl.d.ts +12 -0
  97. package/lib-esm/utils/resolveApiUrl.js +36 -0
  98. package/lib-esm/utils/resolveCredentials.d.ts +5 -0
  99. package/lib-esm/utils/resolveCredentials.js +11 -0
  100. package/lib-esm/utils/serviceError.d.ts +10 -0
  101. package/lib-esm/utils/serviceError.js +25 -0
  102. package/package.json +113 -105
  103. package/server/package.json +8 -0
  104. package/src/apis/common/handler.ts +109 -0
  105. package/src/apis/common/internalPost.ts +81 -0
  106. package/src/apis/common/publicApis.ts +99 -0
  107. package/src/apis/index.ts +239 -0
  108. package/src/apis/server.ts +209 -0
  109. package/src/errors/CanceledError.ts +33 -0
  110. package/src/errors/RestApiError.ts +17 -0
  111. package/src/errors/assertValidatonError.ts +19 -0
  112. package/src/errors/index.ts +7 -0
  113. package/src/errors/validation.ts +20 -0
  114. package/src/index.ts +2 -2
  115. package/src/internals/index.ts +31 -0
  116. package/src/internals/server.ts +37 -0
  117. package/src/server.ts +5 -0
  118. package/src/types/index.ts +95 -44
  119. package/src/utils/constants.ts +15 -0
  120. package/src/utils/createCancellableOperation.ts +94 -0
  121. package/src/utils/index.ts +9 -0
  122. package/src/utils/logger.ts +6 -0
  123. package/src/utils/normalizeHeaders.ts +10 -0
  124. package/src/utils/parseSigningInfo.ts +52 -0
  125. package/src/utils/resolveApiUrl.ts +51 -0
  126. package/src/utils/resolveCredentials.ts +17 -0
  127. package/src/utils/serviceError.ts +35 -0
  128. package/lib/.tsbuildinfo +0 -3
  129. package/lib/RestAPI.d.ts +0 -108
  130. package/lib/RestAPI.js +0 -282
  131. package/lib/RestAPI.js.map +0 -1
  132. package/lib/RestClient.d.ts +0 -138
  133. package/lib/RestClient.js +0 -368
  134. package/lib/RestClient.js.map +0 -1
  135. package/lib/index.js.map +0 -1
  136. package/lib/types/index.js.map +0 -1
  137. package/lib-esm/.tsbuildinfo +0 -3
  138. package/lib-esm/RestAPI.d.ts +0 -108
  139. package/lib-esm/RestAPI.js +0 -280
  140. package/lib-esm/RestAPI.js.map +0 -1
  141. package/lib-esm/RestClient.d.ts +0 -138
  142. package/lib-esm/RestClient.js +0 -366
  143. package/lib-esm/RestClient.js.map +0 -1
  144. package/lib-esm/index.js.map +0 -1
  145. package/lib-esm/types/index.js.map +0 -1
  146. package/src/RestAPI.ts +0 -338
  147. package/src/RestClient.ts +0 -417
package/src/RestAPI.ts DELETED
@@ -1,338 +0,0 @@
1
- // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
- // SPDX-License-Identifier: Apache-2.0
3
- import { RestClient } from './RestClient';
4
- import {
5
- Amplify,
6
- ConsoleLogger as Logger,
7
- Credentials,
8
- } from '@aws-amplify/core';
9
- import { ApiInfo } from './types';
10
-
11
- const logger = new Logger('RestAPI');
12
-
13
- /**
14
- * Export Cloud Logic APIs
15
- */
16
- export class RestAPIClass {
17
- /**
18
- * @private
19
- */
20
- private _options;
21
- private _api: RestClient = null;
22
-
23
- Credentials = Credentials;
24
-
25
- /**
26
- * Initialize Rest API with AWS configuration
27
- * @param {Object} options - Configuration object for API
28
- */
29
- constructor(options) {
30
- this._options = options;
31
- logger.debug('API Options', this._options);
32
- }
33
-
34
- public getModuleName() {
35
- return 'RestAPI';
36
- }
37
-
38
- /**
39
- * Configure API part with aws configurations
40
- * @param {Object} config - Configuration of the API
41
- * @return {Object} - The current configuration
42
- */
43
- configure(options) {
44
- const { API = {}, ...otherOptions } = options || {};
45
- let opt = { ...otherOptions, ...API };
46
- logger.debug('configure Rest API', { opt });
47
-
48
- if (opt['aws_project_region']) {
49
- if (opt['aws_cloud_logic_custom']) {
50
- const custom = opt['aws_cloud_logic_custom'];
51
- opt.endpoints =
52
- typeof custom === 'string' ? JSON.parse(custom) : custom;
53
- }
54
-
55
- opt = Object.assign({}, opt, {
56
- region: opt['aws_project_region'],
57
- header: {},
58
- });
59
- }
60
-
61
- if (Array.isArray(opt.endpoints)) {
62
- // Check if endpoints has custom_headers and validate if is a function
63
- opt.endpoints.forEach(endpoint => {
64
- if (
65
- typeof endpoint.custom_header !== 'undefined' &&
66
- typeof endpoint.custom_header !== 'function'
67
- ) {
68
- logger.warn(
69
- 'Rest API ' + endpoint.name + ', custom_header should be a function'
70
- );
71
- endpoint.custom_header = undefined;
72
- }
73
- });
74
- } else if (this._options && Array.isArray(this._options.endpoints)) {
75
- opt.endpoints = this._options.endpoints;
76
- } else {
77
- opt.endpoints = [];
78
- }
79
-
80
- this._options = Object.assign({}, this._options, opt);
81
-
82
- this.createInstance();
83
-
84
- return this._options;
85
- }
86
-
87
- /**
88
- * Create an instance of API for the library
89
- * @return - A promise of true if Success
90
- */
91
- createInstance() {
92
- logger.debug('create Rest API instance');
93
- this._api = new RestClient(this._options);
94
-
95
- // Share Amplify instance with client for SSR
96
- this._api.Credentials = this.Credentials;
97
- return true;
98
- }
99
-
100
- /**
101
- * Make a GET request
102
- * @param {string} apiName - The api name of the request
103
- * @param {string} path - The path of the request
104
- * @param {json} [init] - Request extra params
105
- * @return {Promise} - A promise that resolves to an object with response status and JSON data, if successful.
106
- */
107
- get(apiName, path, init): Promise<any> {
108
- try {
109
- const apiInfo = this.getEndpointInfo(apiName, path);
110
-
111
- const cancellableToken = this._api.getCancellableToken();
112
-
113
- const initParams = Object.assign({}, init);
114
- initParams.cancellableToken = cancellableToken;
115
-
116
- const responsePromise = this._api.get(apiInfo, initParams);
117
-
118
- this._api.updateRequestToBeCancellable(responsePromise, cancellableToken);
119
-
120
- return responsePromise;
121
- } catch (err) {
122
- return Promise.reject(err.message);
123
- }
124
- }
125
-
126
- /**
127
- * Make a POST request
128
- * @param {string} apiName - The api name of the request
129
- * @param {string} path - The path of the request
130
- * @param {json} [init] - Request extra params
131
- * @return {Promise} - A promise that resolves to an object with response status and JSON data, if successful.
132
- */
133
- post(apiName, path, init): Promise<any> {
134
- try {
135
- const apiInfo = this.getEndpointInfo(apiName, path);
136
-
137
- const cancellableToken = this._api.getCancellableToken();
138
-
139
- const initParams = Object.assign({}, init);
140
- initParams.cancellableToken = cancellableToken;
141
-
142
- const responsePromise = this._api.post(apiInfo, initParams);
143
-
144
- this._api.updateRequestToBeCancellable(responsePromise, cancellableToken);
145
-
146
- return responsePromise;
147
- } catch (err) {
148
- return Promise.reject(err.message);
149
- }
150
- }
151
-
152
- /**
153
- * Make a PUT request
154
- * @param {string} apiName - The api name of the request
155
- * @param {string} path - The path of the request
156
- * @param {json} [init] - Request extra params
157
- * @return {Promise} - A promise that resolves to an object with response status and JSON data, if successful.
158
- */
159
- put(apiName, path, init): Promise<any> {
160
- try {
161
- const apiInfo = this.getEndpointInfo(apiName, path);
162
-
163
- const cancellableToken = this._api.getCancellableToken();
164
-
165
- const initParams = Object.assign({}, init);
166
- initParams.cancellableToken = cancellableToken;
167
-
168
- const responsePromise = this._api.put(apiInfo, initParams);
169
-
170
- this._api.updateRequestToBeCancellable(responsePromise, cancellableToken);
171
-
172
- return responsePromise;
173
- } catch (err) {
174
- return Promise.reject(err.message);
175
- }
176
- }
177
-
178
- /**
179
- * Make a PATCH request
180
- * @param {string} apiName - The api name of the request
181
- * @param {string} path - The path of the request
182
- * @param {json} [init] - Request extra params
183
- * @return {Promise} - A promise that resolves to an object with response status and JSON data, if successful.
184
- */
185
- patch(apiName, path, init): Promise<any> {
186
- try {
187
- const apiInfo = this.getEndpointInfo(apiName, path);
188
-
189
- const cancellableToken = this._api.getCancellableToken();
190
-
191
- const initParams = Object.assign({}, init);
192
- initParams.cancellableToken = cancellableToken;
193
-
194
- const responsePromise = this._api.patch(apiInfo, initParams);
195
-
196
- this._api.updateRequestToBeCancellable(responsePromise, cancellableToken);
197
-
198
- return responsePromise;
199
- } catch (err) {
200
- return Promise.reject(err.message);
201
- }
202
- }
203
-
204
- /**
205
- * Make a DEL request
206
- * @param {string} apiName - The api name of the request
207
- * @param {string} path - The path of the request
208
- * @param {json} [init] - Request extra params
209
- * @return {Promise} - A promise that resolves to an object with response status and JSON data, if successful.
210
- */
211
- del(apiName, path, init): Promise<any> {
212
- try {
213
- const apiInfo = this.getEndpointInfo(apiName, path);
214
-
215
- const cancellableToken = this._api.getCancellableToken();
216
-
217
- const initParams = Object.assign({}, init);
218
- initParams.cancellableToken = cancellableToken;
219
-
220
- const responsePromise = this._api.del(apiInfo, initParams);
221
-
222
- this._api.updateRequestToBeCancellable(responsePromise, cancellableToken);
223
-
224
- return responsePromise;
225
- } catch (err) {
226
- return Promise.reject(err.message);
227
- }
228
- }
229
-
230
- /**
231
- * Make a HEAD request
232
- * @param {string} apiName - The api name of the request
233
- * @param {string} path - The path of the request
234
- * @param {json} [init] - Request extra params
235
- * @return {Promise} - A promise that resolves to an object with response status and JSON data, if successful.
236
- */
237
- head(apiName, path, init): Promise<any> {
238
- try {
239
- const apiInfo = this.getEndpointInfo(apiName, path);
240
-
241
- const cancellableToken = this._api.getCancellableToken();
242
-
243
- const initParams = Object.assign({}, init);
244
- initParams.cancellableToken = cancellableToken;
245
-
246
- const responsePromise = this._api.head(apiInfo, initParams);
247
-
248
- this._api.updateRequestToBeCancellable(responsePromise, cancellableToken);
249
-
250
- return responsePromise;
251
- } catch (err) {
252
- return Promise.reject(err.message);
253
- }
254
- }
255
-
256
- /**
257
- * Checks to see if an error thrown is from an api request cancellation
258
- * @param {any} error - Any error
259
- * @return {boolean} - A boolean indicating if the error was from an api request cancellation
260
- */
261
- isCancel(error) {
262
- return this._api.isCancel(error);
263
- }
264
-
265
- /**
266
- * Cancels an inflight request
267
- * @param {any} request - request to cancel
268
- * @return {boolean} - A boolean indicating if the request was cancelled
269
- */
270
- cancel(request: Promise<any>, message?: string) {
271
- return this._api.cancel(request, message);
272
- }
273
-
274
- /**
275
- * Check if the request has a corresponding cancel token in the WeakMap.
276
- * @params request - The request promise
277
- * @return if the request has a corresponding cancel token.
278
- */
279
- hasCancelToken(request: Promise<any>) {
280
- return this._api.hasCancelToken(request);
281
- }
282
-
283
- /**
284
- * Getting endpoint for API
285
- * @param {string} apiName - The name of the api
286
- * @return {string} - The endpoint of the api
287
- */
288
- async endpoint(apiName) {
289
- return this._api.endpoint(apiName);
290
- }
291
-
292
- /**
293
- * Getting endpoint info for API
294
- * @param {string} apiName - The name of the api
295
- * @param {string} path - The path of the api that is going to accessed
296
- * @return {ApiInfo} - The endpoint information for that api-name
297
- */
298
- private getEndpointInfo(apiName: string, path: string): ApiInfo {
299
- const cloud_logic_array = this._options.endpoints;
300
-
301
- if (!Array.isArray(cloud_logic_array)) {
302
- throw new Error(`API category not configured`);
303
- }
304
-
305
- const apiConfig = cloud_logic_array.find(api => api.name === apiName);
306
-
307
- if (!apiConfig) {
308
- throw new Error(`API ${apiName} does not exist`);
309
- }
310
-
311
- const response: ApiInfo = {
312
- endpoint: apiConfig.endpoint + path,
313
- };
314
-
315
- if (typeof apiConfig.region === 'string') {
316
- response.region = apiConfig.region;
317
- } else if (typeof this._options.region === 'string') {
318
- response.region = this._options.region;
319
- }
320
-
321
- if (typeof apiConfig.service === 'string') {
322
- response.service = apiConfig.service || 'execute-api';
323
- } else {
324
- response.service = 'execute-api';
325
- }
326
-
327
- if (typeof apiConfig.custom_header === 'function') {
328
- response.custom_header = apiConfig.custom_header;
329
- } else {
330
- response.custom_header = undefined;
331
- }
332
-
333
- return response;
334
- }
335
- }
336
-
337
- export const RestAPI = new RestAPIClass(null);
338
- Amplify.register(RestAPI);