@aws-amplify/api-rest 4.0.1-console-preview.a1c533e.0 → 4.0.1-console-preview.23dc225.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.
- package/lib/apis/common/handler.js +14 -3
- package/lib/apis/common/internalPost.js +1 -1
- package/lib/apis/index.d.ts +178 -0
- package/lib/apis/index.js +178 -0
- package/lib/apis/server.d.ts +125 -0
- package/lib/apis/server.js +125 -0
- package/lib/errors/CanceledError.d.ts +17 -0
- package/lib/errors/CanceledError.js +35 -0
- package/lib/errors/index.d.ts +1 -1
- package/lib/errors/index.js +4 -4
- package/lib/index.d.ts +1 -1
- package/lib/index.js +2 -2
- package/lib/server.d.ts +1 -1
- package/lib/server.js +2 -2
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/types/index.d.ts +1 -1
- package/lib/utils/createCancellableOperation.js +10 -15
- package/lib/utils/index.d.ts +0 -1
- package/lib/utils/index.js +0 -1
- package/lib/utils/resolveApiUrl.js +4 -3
- package/lib/utils/resolveCredentials.d.ts +1 -6
- package/lib-esm/apis/common/handler.js +14 -3
- package/lib-esm/apis/common/internalPost.js +1 -1
- package/lib-esm/apis/index.d.ts +178 -0
- package/lib-esm/apis/index.js +178 -0
- package/lib-esm/apis/server.d.ts +125 -0
- package/lib-esm/apis/server.js +125 -0
- package/lib-esm/errors/CanceledError.d.ts +17 -0
- package/lib-esm/errors/CanceledError.js +31 -0
- package/lib-esm/errors/index.d.ts +1 -1
- package/lib-esm/errors/index.js +1 -1
- package/lib-esm/index.d.ts +1 -1
- package/lib-esm/index.js +1 -1
- package/lib-esm/server.d.ts +1 -1
- package/lib-esm/server.js +1 -1
- package/lib-esm/tsconfig.tsbuildinfo +1 -1
- package/lib-esm/types/index.d.ts +1 -1
- package/lib-esm/utils/createCancellableOperation.js +12 -17
- package/lib-esm/utils/index.d.ts +0 -1
- package/lib-esm/utils/index.js +0 -1
- package/lib-esm/utils/resolveApiUrl.js +4 -3
- package/lib-esm/utils/resolveCredentials.d.ts +1 -6
- package/package.json +5 -5
- package/src/apis/common/handler.ts +9 -2
- package/src/apis/common/internalPost.ts +1 -1
- package/src/apis/index.ts +179 -0
- package/src/apis/server.ts +126 -0
- package/src/errors/CanceledError.ts +33 -0
- package/src/errors/index.ts +1 -1
- package/src/index.ts +1 -1
- package/src/server.ts +1 -1
- package/src/types/index.ts +1 -1
- package/src/utils/createCancellableOperation.ts +5 -6
- package/src/utils/index.ts +0 -2
- package/src/utils/resolveApiUrl.ts +7 -3
- package/lib/errors/CancelledError.d.ts +0 -14
- package/lib/errors/CancelledError.js +0 -31
- package/lib/utils/polyfills/index.d.ts +0 -0
- package/lib/utils/polyfills/index.js +0 -4
- package/lib/utils/polyfills/index.native.d.ts +0 -1
- package/lib/utils/polyfills/index.native.js +0 -6
- package/lib-esm/errors/CancelledError.d.ts +0 -14
- package/lib-esm/errors/CancelledError.js +0 -27
- package/lib-esm/utils/polyfills/index.d.ts +0 -0
- package/lib-esm/utils/polyfills/index.js +0 -4
- package/lib-esm/utils/polyfills/index.native.d.ts +0 -1
- package/lib-esm/utils/polyfills/index.native.js +0 -4
- package/src/errors/CancelledError.ts +0 -26
- package/src/utils/polyfills/index.native.ts +0 -5
- package/src/utils/polyfills/index.ts +0 -4
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
import { __assign, __read } from "tslib";
|
|
4
|
+
import { AmplifyUrl, AmplifyUrlSearchParams, } from '@aws-amplify/core/internals/utils';
|
|
4
5
|
import { RestApiError, RestApiValidationErrorCode, assertValidationError, validationErrorMap, } from '../errors';
|
|
5
6
|
/**
|
|
6
7
|
* Resolve the REST API request URL by:
|
|
@@ -17,14 +18,14 @@ export var resolveApiUrl = function (amplify, apiName, path, queryParams) {
|
|
|
17
18
|
var urlStr = (_d = (_c = (_b = (_a = amplify.getConfig()) === null || _a === void 0 ? void 0 : _a.API) === null || _b === void 0 ? void 0 : _b.REST) === null || _c === void 0 ? void 0 : _c[apiName]) === null || _d === void 0 ? void 0 : _d.endpoint;
|
|
18
19
|
assertValidationError(!!urlStr, RestApiValidationErrorCode.InvalidApiName);
|
|
19
20
|
try {
|
|
20
|
-
var url = new
|
|
21
|
+
var url = new AmplifyUrl(urlStr + path);
|
|
21
22
|
if (queryParams) {
|
|
22
|
-
var mergedQueryParams_1 = new
|
|
23
|
+
var mergedQueryParams_1 = new AmplifyUrlSearchParams(url.searchParams);
|
|
23
24
|
Object.entries(queryParams).forEach(function (_a) {
|
|
24
25
|
var _b = __read(_a, 2), key = _b[0], value = _b[1];
|
|
25
26
|
mergedQueryParams_1.set(key, value);
|
|
26
27
|
});
|
|
27
|
-
url.search = new
|
|
28
|
+
url.search = new AmplifyUrlSearchParams(mergedQueryParams_1).toString();
|
|
28
29
|
}
|
|
29
30
|
return url;
|
|
30
31
|
}
|
|
@@ -2,9 +2,4 @@ import { AmplifyClassV6 } from '@aws-amplify/core';
|
|
|
2
2
|
/**
|
|
3
3
|
* @internal
|
|
4
4
|
*/
|
|
5
|
-
export declare const resolveCredentials: (amplify: AmplifyClassV6) => Promise<
|
|
6
|
-
accessKeyId: string;
|
|
7
|
-
secretAccessKey: string;
|
|
8
|
-
sessionToken?: string | undefined;
|
|
9
|
-
expiration?: Date | undefined;
|
|
10
|
-
}>;
|
|
5
|
+
export declare const resolveCredentials: (amplify: AmplifyClassV6) => Promise<import("@aws-amplify/core/lib-esm/libraryUtils").AWSCredentials>;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aws-amplify/api-rest",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "4.0.1-console-preview.
|
|
4
|
+
"version": "4.0.1-console-preview.23dc225.0+23dc225",
|
|
5
5
|
"description": "Api-rest category of aws-amplify",
|
|
6
6
|
"main": "./lib/index.js",
|
|
7
7
|
"module": "./lib-esm/index.js",
|
|
@@ -52,11 +52,11 @@
|
|
|
52
52
|
"tslib": "^2.5.0"
|
|
53
53
|
},
|
|
54
54
|
"peerDependencies": {
|
|
55
|
-
"@aws-amplify/core": "6.0.1-console-preview.
|
|
55
|
+
"@aws-amplify/core": "6.0.1-console-preview.23dc225.0+23dc225"
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
|
-
"@aws-amplify/core": "6.0.1-console-preview.
|
|
59
|
-
"@aws-amplify/react-native": "1.0.1-console-preview.
|
|
58
|
+
"@aws-amplify/core": "6.0.1-console-preview.23dc225.0+23dc225",
|
|
59
|
+
"@aws-amplify/react-native": "1.0.1-console-preview.23dc225.0+23dc225",
|
|
60
60
|
"typescript": "5.0.2"
|
|
61
61
|
},
|
|
62
62
|
"size-limit": [
|
|
@@ -111,5 +111,5 @@
|
|
|
111
111
|
"<rootDir>/setupTests.ts"
|
|
112
112
|
]
|
|
113
113
|
},
|
|
114
|
-
"gitHead": "
|
|
114
|
+
"gitHead": "23dc22531b963319f2b421f6f7c6cf867635b20e"
|
|
115
115
|
}
|
|
@@ -76,6 +76,7 @@ export const transferHandler = async (
|
|
|
76
76
|
};
|
|
77
77
|
|
|
78
78
|
const isIamAuthApplicable = iamAuthApplicable(request, signingServiceInfo);
|
|
79
|
+
let response: RestApiResponse;
|
|
79
80
|
if (isIamAuthApplicable) {
|
|
80
81
|
const signingInfoFromUrl = parseSigningInfo(url);
|
|
81
82
|
const signingService =
|
|
@@ -83,17 +84,23 @@ export const transferHandler = async (
|
|
|
83
84
|
const signingRegion =
|
|
84
85
|
signingServiceInfo?.region ?? signingInfoFromUrl.region;
|
|
85
86
|
const credentials = await resolveCredentials(amplify);
|
|
86
|
-
|
|
87
|
+
response = await authenticatedHandler(request, {
|
|
87
88
|
...baseOptions,
|
|
88
89
|
credentials,
|
|
89
90
|
region: signingRegion,
|
|
90
91
|
service: signingService,
|
|
91
92
|
});
|
|
92
93
|
} else {
|
|
93
|
-
|
|
94
|
+
response = await unauthenticatedHandler(request, {
|
|
94
95
|
...baseOptions,
|
|
95
96
|
});
|
|
96
97
|
}
|
|
98
|
+
// Clean-up un-modeled properties from response.
|
|
99
|
+
return {
|
|
100
|
+
statusCode: response.statusCode,
|
|
101
|
+
headers: response.headers,
|
|
102
|
+
body: response.body,
|
|
103
|
+
};
|
|
97
104
|
};
|
|
98
105
|
|
|
99
106
|
const iamAuthApplicable = (
|
|
@@ -60,7 +60,7 @@ export const cancel = (
|
|
|
60
60
|
const controller = cancelTokenMap.get(promise);
|
|
61
61
|
if (controller) {
|
|
62
62
|
controller.abort(message);
|
|
63
|
-
if (controller.signal.reason !== message) {
|
|
63
|
+
if (message && controller.signal.reason !== message) {
|
|
64
64
|
// In runtimes where `AbortSignal.reason` is not supported, we track the reason ourselves.
|
|
65
65
|
// @ts-expect-error reason is read-only property.
|
|
66
66
|
controller.signal['reason'] = message;
|
package/src/apis/index.ts
CHANGED
|
@@ -24,37 +24,216 @@ import {
|
|
|
24
24
|
PutInput,
|
|
25
25
|
PutOperation,
|
|
26
26
|
} from '../types';
|
|
27
|
+
import { RestApiError } from '../errors';
|
|
27
28
|
|
|
28
29
|
/**
|
|
29
30
|
* GET HTTP request
|
|
31
|
+
* @param {GetInput} input - Input for GET operation
|
|
32
|
+
* @returns {GetOperation} Operation for GET request
|
|
33
|
+
* @throws - {@link RestApiError}
|
|
34
|
+
* @example
|
|
35
|
+
* Send a GET request
|
|
36
|
+
* ```js
|
|
37
|
+
* import { get, isCancelError } from '@aws-amplify/api';
|
|
38
|
+
*
|
|
39
|
+
* const { body } = await get({
|
|
40
|
+
* apiName,
|
|
41
|
+
* path,
|
|
42
|
+
* options: {
|
|
43
|
+
* headers, // Optional, A map of custom header key/values
|
|
44
|
+
* body, // Optional, JSON object or FormData
|
|
45
|
+
* queryParams, // Optional, A map of query strings
|
|
46
|
+
* }
|
|
47
|
+
* }).response;
|
|
48
|
+
* const data = await body.json();
|
|
49
|
+
* ```
|
|
50
|
+
* @example
|
|
51
|
+
* Cancel a GET request
|
|
52
|
+
*
|
|
53
|
+
* ```js
|
|
54
|
+
* import { get, isCancelError } from '@aws-amplify/api';
|
|
55
|
+
*
|
|
56
|
+
* const { response, cancel } = get({apiName, path, options});
|
|
57
|
+
* cancel(message);
|
|
58
|
+
* try {
|
|
59
|
+
* await response;
|
|
60
|
+
* } cache (e) {
|
|
61
|
+
* if (isCancelError(e)) {
|
|
62
|
+
* // handle request cancellation
|
|
63
|
+
* }
|
|
64
|
+
* //...
|
|
65
|
+
* }
|
|
66
|
+
* ```
|
|
30
67
|
*/
|
|
31
68
|
export const get = (input: GetInput): GetOperation => commonGet(Amplify, input);
|
|
32
69
|
|
|
33
70
|
/**
|
|
34
71
|
* POST HTTP request
|
|
72
|
+
* @param {PostInput} input - Input for POST operation
|
|
73
|
+
* @returns {PostOperation} Operation for POST request
|
|
74
|
+
* @throws - {@link RestApiError}
|
|
75
|
+
* @example
|
|
76
|
+
* Send a POST request
|
|
77
|
+
* ```js
|
|
78
|
+
* import { post, isCancelError } from '@aws-amplify/api';
|
|
79
|
+
*
|
|
80
|
+
* const { body } = await post({
|
|
81
|
+
* apiName,
|
|
82
|
+
* path,
|
|
83
|
+
* options: {
|
|
84
|
+
* headers, // Optional, A map of custom header key/values
|
|
85
|
+
* body, // Optional, JSON object or FormData
|
|
86
|
+
* queryParams, // Optional, A map of query strings
|
|
87
|
+
* }
|
|
88
|
+
* }).response;
|
|
89
|
+
* const data = await body.json();
|
|
90
|
+
* ```
|
|
91
|
+
* @example
|
|
92
|
+
* Cancel a POST request
|
|
93
|
+
*
|
|
94
|
+
* ```js
|
|
95
|
+
* import { post, isCancelError } from '@aws-amplify/api';
|
|
96
|
+
*
|
|
97
|
+
* const { response, cancel } = post({apiName, path, options});
|
|
98
|
+
* cancel(message);
|
|
99
|
+
* try {
|
|
100
|
+
* await response;
|
|
101
|
+
* } cache (e) {
|
|
102
|
+
* if (isCancelError(e)) {
|
|
103
|
+
* // handle request cancellation
|
|
104
|
+
* }
|
|
105
|
+
* //...
|
|
106
|
+
* }
|
|
107
|
+
* ```
|
|
35
108
|
*/
|
|
36
109
|
export const post = (input: PostInput): PostOperation =>
|
|
37
110
|
commonPost(Amplify, input);
|
|
38
111
|
|
|
39
112
|
/**
|
|
40
113
|
* PUT HTTP request
|
|
114
|
+
* @param {PutInput} input - Input for PUT operation
|
|
115
|
+
* @returns {PutOperation} Operation for PUT request
|
|
116
|
+
* @throws - {@link RestApiError}
|
|
117
|
+
* @example
|
|
118
|
+
* Send a PUT request
|
|
119
|
+
* ```js
|
|
120
|
+
* import { put, isCancelError } from '@aws-amplify/api';
|
|
121
|
+
*
|
|
122
|
+
* const { body } = await put({
|
|
123
|
+
* apiName,
|
|
124
|
+
* path,
|
|
125
|
+
* options: {
|
|
126
|
+
* headers, // Optional, A map of custom header key/values
|
|
127
|
+
* body, // Optional, JSON object or FormData
|
|
128
|
+
* queryParams, // Optional, A map of query strings
|
|
129
|
+
* }
|
|
130
|
+
* }).response;
|
|
131
|
+
* const data = await body.json();
|
|
132
|
+
* ```
|
|
133
|
+
* @example
|
|
134
|
+
* Cancel a PUT request
|
|
135
|
+
* ```js
|
|
136
|
+
* import { put, isCancelError } from '@aws-amplify/api';
|
|
137
|
+
*
|
|
138
|
+
* const { response, cancel } = put({apiName, path, options});
|
|
139
|
+
* cancel(message);
|
|
140
|
+
* try {
|
|
141
|
+
* await response;
|
|
142
|
+
* } cache (e) {
|
|
143
|
+
* if (isCancelError(e)) {
|
|
144
|
+
* // handle request cancellation
|
|
145
|
+
* }
|
|
146
|
+
* //...
|
|
147
|
+
* }
|
|
148
|
+
* ```
|
|
41
149
|
*/
|
|
42
150
|
export const put = (input: PutInput): PutOperation => commonPut(Amplify, input);
|
|
43
151
|
|
|
44
152
|
/**
|
|
45
153
|
* DELETE HTTP request
|
|
154
|
+
* @param {DeleteInput} input - Input for DELETE operation
|
|
155
|
+
* @returns {DeleteOperation} Operation for DELETE request
|
|
156
|
+
* @throws - {@link RestApiError}
|
|
157
|
+
* @example
|
|
158
|
+
* Send a DELETE request
|
|
159
|
+
* ```js
|
|
160
|
+
* import { del } from '@aws-amplify/api';
|
|
161
|
+
*
|
|
162
|
+
* const { statusCode } = await del({
|
|
163
|
+
* apiName,
|
|
164
|
+
* path,
|
|
165
|
+
* options: {
|
|
166
|
+
* headers, // Optional, A map of custom header key/values
|
|
167
|
+
* queryParams, // Optional, A map of query strings
|
|
168
|
+
* }
|
|
169
|
+
* }).response;
|
|
170
|
+
* ```
|
|
46
171
|
*/
|
|
47
172
|
export const del = (input: DeleteInput): DeleteOperation =>
|
|
48
173
|
commonDel(Amplify, input);
|
|
49
174
|
|
|
50
175
|
/**
|
|
51
176
|
* HEAD HTTP request
|
|
177
|
+
* @param {HeadInput} input - Input for HEAD operation
|
|
178
|
+
* @returns {HeadOperation} Operation for HEAD request
|
|
179
|
+
* @throws - {@link RestApiError}
|
|
180
|
+
* @example
|
|
181
|
+
* Send a HEAD request
|
|
182
|
+
* ```js
|
|
183
|
+
* import { head, isCancelError } from '@aws-amplify/api';
|
|
184
|
+
*
|
|
185
|
+
* const { headers, statusCode } = await head({
|
|
186
|
+
* apiName,
|
|
187
|
+
* path,
|
|
188
|
+
* options: {
|
|
189
|
+
* headers, // Optional, A map of custom header key/values
|
|
190
|
+
* queryParams, // Optional, A map of query strings
|
|
191
|
+
* }
|
|
192
|
+
* }),response;
|
|
193
|
+
* ```
|
|
194
|
+
*
|
|
52
195
|
*/
|
|
53
196
|
export const head = (input: HeadInput): HeadOperation =>
|
|
54
197
|
commonHead(Amplify, input);
|
|
55
198
|
|
|
56
199
|
/**
|
|
57
200
|
* PATCH HTTP request
|
|
201
|
+
* @param {PatchInput} input - Input for PATCH operation
|
|
202
|
+
* @returns {PatchOperation} Operation for PATCH request
|
|
203
|
+
* @throws - {@link RestApiError}
|
|
204
|
+
* @example
|
|
205
|
+
* Send a PATCH request
|
|
206
|
+
* ```js
|
|
207
|
+
* import { patch } from '@aws-amplify/api';
|
|
208
|
+
*
|
|
209
|
+
* const { body } = await patch({
|
|
210
|
+
* apiName,
|
|
211
|
+
* path,
|
|
212
|
+
* options: {
|
|
213
|
+
* headers, // Optional, A map of custom header key/values
|
|
214
|
+
* body, // Optional, JSON object or FormData
|
|
215
|
+
* queryParams, // Optional, A map of query strings
|
|
216
|
+
* }
|
|
217
|
+
* }).response;
|
|
218
|
+
* const data = await body.json();
|
|
219
|
+
* ```
|
|
220
|
+
*
|
|
221
|
+
* @example
|
|
222
|
+
* Cancel a PATCH request
|
|
223
|
+
* ```js
|
|
224
|
+
* import { patch, isCancelError } from '@aws-amplify/api';
|
|
225
|
+
*
|
|
226
|
+
* const { response, cancel } = patch({apiName, path, options});
|
|
227
|
+
* cancel(message);
|
|
228
|
+
* try {
|
|
229
|
+
* await response;
|
|
230
|
+
* } cache (e) {
|
|
231
|
+
* if (isCancelError(e)) {
|
|
232
|
+
* // handle request cancellation
|
|
233
|
+
* }
|
|
234
|
+
* //...
|
|
235
|
+
* }
|
|
236
|
+
* ```
|
|
58
237
|
*/
|
|
59
238
|
export const patch = (input: PatchInput): PatchOperation =>
|
|
60
239
|
commonPatch(Amplify, input);
|
package/src/apis/server.ts
CHANGED
|
@@ -27,9 +27,32 @@ import {
|
|
|
27
27
|
PutInput,
|
|
28
28
|
PutOperation,
|
|
29
29
|
} from '../types';
|
|
30
|
+
import { RestApiError } from '../errors';
|
|
30
31
|
|
|
31
32
|
/**
|
|
32
33
|
* GET HTTP request (server-side)
|
|
34
|
+
* @param {AmplifyServer.ContextSpec} contextSpec - The context spec used to get the Amplify server context.
|
|
35
|
+
* @param {GetInput} input - Input for GET operation.
|
|
36
|
+
* @throws - {@link RestApiError}
|
|
37
|
+
* @example
|
|
38
|
+
* Send a GET request
|
|
39
|
+
* ```js
|
|
40
|
+
* import { get } from 'aws-amplify/api/server';
|
|
41
|
+
* //...
|
|
42
|
+
* const restApiResponse = await runWithAmplifyServerContext({
|
|
43
|
+
* nextServerContext: { request, response },
|
|
44
|
+
* operation: async (contextSpec) => {
|
|
45
|
+
* try {
|
|
46
|
+
* const { body } = await get(contextSpec, input).response;
|
|
47
|
+
* return await body.json();
|
|
48
|
+
* } catch (error) {
|
|
49
|
+
* console.log(error);
|
|
50
|
+
* return false;
|
|
51
|
+
* }
|
|
52
|
+
* },
|
|
53
|
+
* });
|
|
54
|
+
* ```
|
|
55
|
+
* @see {@link clientGet}
|
|
33
56
|
*/
|
|
34
57
|
export const get = (
|
|
35
58
|
contextSpec: AmplifyServer.ContextSpec,
|
|
@@ -39,6 +62,27 @@ export const get = (
|
|
|
39
62
|
|
|
40
63
|
/**
|
|
41
64
|
* POST HTTP request (server-side)
|
|
65
|
+
* @param {AmplifyServer.ContextSpec} contextSpec - The context spec used to get the Amplify server context.
|
|
66
|
+
* @param {PostInput} input - Input for POST operation.
|
|
67
|
+
* @throws - {@link RestApiError}
|
|
68
|
+
* @example
|
|
69
|
+
* Send a POST request
|
|
70
|
+
* ```js
|
|
71
|
+
* import { post } from 'aws-amplify/api/server';
|
|
72
|
+
* //...
|
|
73
|
+
* const restApiResponse = await runWithAmplifyServerContext({
|
|
74
|
+
* nextServerContext: { request, response },
|
|
75
|
+
* operation: async (contextSpec) => {
|
|
76
|
+
* try {
|
|
77
|
+
* const { body } = await post(contextSpec, input).response;
|
|
78
|
+
* return await body.json();
|
|
79
|
+
* } catch (error) {
|
|
80
|
+
* console.log(error);
|
|
81
|
+
* return false;
|
|
82
|
+
* }
|
|
83
|
+
* },
|
|
84
|
+
* });
|
|
85
|
+
* ```
|
|
42
86
|
*/
|
|
43
87
|
export const post = (
|
|
44
88
|
contextSpec: AmplifyServer.ContextSpec,
|
|
@@ -48,6 +92,27 @@ export const post = (
|
|
|
48
92
|
|
|
49
93
|
/**
|
|
50
94
|
* PUT HTTP request (server-side)
|
|
95
|
+
* @param {AmplifyServer.ContextSpec} contextSpec - The context spec used to get the Amplify server context.
|
|
96
|
+
* @param {PutInput} input - Input for PUT operation.
|
|
97
|
+
* @throws - {@link RestApiError}
|
|
98
|
+
* @example
|
|
99
|
+
* Send a PUT request
|
|
100
|
+
* ```js
|
|
101
|
+
* import { put } from 'aws-amplify/api/server';
|
|
102
|
+
* //...
|
|
103
|
+
* const restApiResponse = await runWithAmplifyServerContext({
|
|
104
|
+
* nextServerContext: { request, response },
|
|
105
|
+
* operation: async (contextSpec) => {
|
|
106
|
+
* try {
|
|
107
|
+
* const { body } = await put(contextSpec, input).response;
|
|
108
|
+
* return await body.json();
|
|
109
|
+
* } catch (error) {
|
|
110
|
+
* console.log(error);
|
|
111
|
+
* return false;
|
|
112
|
+
* }
|
|
113
|
+
* },
|
|
114
|
+
* });
|
|
115
|
+
* ```
|
|
51
116
|
*/
|
|
52
117
|
export const put = (
|
|
53
118
|
contextSpec: AmplifyServer.ContextSpec,
|
|
@@ -57,6 +122,26 @@ export const put = (
|
|
|
57
122
|
|
|
58
123
|
/**
|
|
59
124
|
* DELETE HTTP request (server-side)
|
|
125
|
+
* @param {AmplifyServer.ContextSpec} contextSpec - The context spec used to get the Amplify server context.
|
|
126
|
+
* @param {DeleteInput} input - Input for DELETE operation.
|
|
127
|
+
* @throws - {@link RestApiError}
|
|
128
|
+
* @example
|
|
129
|
+
* Send a DELETE request
|
|
130
|
+
* ```js
|
|
131
|
+
* import { del } from 'aws-amplify/api/server';
|
|
132
|
+
* //...
|
|
133
|
+
* const restApiResponse = await runWithAmplifyServerContext({
|
|
134
|
+
* nextServerContext: { request, response },
|
|
135
|
+
* operation: async (contextSpec) => {
|
|
136
|
+
* try {
|
|
137
|
+
* const { headers } = await del(contextSpec, input).response;
|
|
138
|
+
* } catch (error) {
|
|
139
|
+
* console.log(error);
|
|
140
|
+
* return false;
|
|
141
|
+
* }
|
|
142
|
+
* },
|
|
143
|
+
* });
|
|
144
|
+
* ```
|
|
60
145
|
*/
|
|
61
146
|
export const del = (
|
|
62
147
|
contextSpec: AmplifyServer.ContextSpec,
|
|
@@ -66,6 +151,26 @@ export const del = (
|
|
|
66
151
|
|
|
67
152
|
/**
|
|
68
153
|
* HEAD HTTP request (server-side)
|
|
154
|
+
* @param {AmplifyServer.ContextSpec} contextSpec - The context spec used to get the Amplify server context.
|
|
155
|
+
* @param {HeadInput} input - Input for HEAD operation.
|
|
156
|
+
* @throws - {@link RestApiError}
|
|
157
|
+
* @example
|
|
158
|
+
* Send a HEAD request
|
|
159
|
+
* ```js
|
|
160
|
+
* import { head } from 'aws-amplify/api/server';
|
|
161
|
+
* //...
|
|
162
|
+
* const restApiResponse = await runWithAmplifyServerContext({
|
|
163
|
+
* nextServerContext: { request, response },
|
|
164
|
+
* operation: async (contextSpec) => {
|
|
165
|
+
* try {
|
|
166
|
+
* const { headers } = await head(contextSpec, input).response;
|
|
167
|
+
* } catch (error) {
|
|
168
|
+
* console.log(error);
|
|
169
|
+
* return false;
|
|
170
|
+
* }
|
|
171
|
+
* },
|
|
172
|
+
* });
|
|
173
|
+
* ```
|
|
69
174
|
*/
|
|
70
175
|
export const head = (
|
|
71
176
|
contextSpec: AmplifyServer.ContextSpec,
|
|
@@ -75,6 +180,27 @@ export const head = (
|
|
|
75
180
|
|
|
76
181
|
/**
|
|
77
182
|
* PATCH HTTP request (server-side)
|
|
183
|
+
* @param {AmplifyServer.ContextSpec} contextSpec - The context spec used to get the Amplify server context.
|
|
184
|
+
* @param {PatchInput} input - Input for PATCH operation.
|
|
185
|
+
* @throws - {@link RestApiError}
|
|
186
|
+
* @example
|
|
187
|
+
* Send a PATCH request
|
|
188
|
+
* ```js
|
|
189
|
+
* import { patch } from 'aws-amplify/api/server';
|
|
190
|
+
* //...
|
|
191
|
+
* const restApiResponse = await runWithAmplifyServerContext({
|
|
192
|
+
* nextServerContext: { request, response },
|
|
193
|
+
* operation: async (contextSpec) => {
|
|
194
|
+
* try {
|
|
195
|
+
* const { body } = await patch(contextSpec, input).response;
|
|
196
|
+
* return await body.json();
|
|
197
|
+
* } catch (error) {
|
|
198
|
+
* console.log(error);
|
|
199
|
+
* return false;
|
|
200
|
+
* }
|
|
201
|
+
* },
|
|
202
|
+
* });
|
|
203
|
+
* ```
|
|
78
204
|
*/
|
|
79
205
|
export const patch = (
|
|
80
206
|
contextSpec: AmplifyServer.ContextSpec,
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { AmplifyErrorParams } from '@aws-amplify/core/internals/utils';
|
|
5
|
+
import { RestApiError } from './RestApiError';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Internal-only class for CanceledError.
|
|
9
|
+
*
|
|
10
|
+
* @internal
|
|
11
|
+
*/
|
|
12
|
+
export class CanceledError extends RestApiError {
|
|
13
|
+
constructor(params: Partial<AmplifyErrorParams> = {}) {
|
|
14
|
+
super({
|
|
15
|
+
name: 'CanceledError',
|
|
16
|
+
message: 'Request is canceled by user',
|
|
17
|
+
...params,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
// TODO: Delete the following 2 lines after we change the build target to >= es2015
|
|
21
|
+
this.constructor = CanceledError;
|
|
22
|
+
Object.setPrototypeOf(this, CanceledError.prototype);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Check if an error is caused by user calling `cancel()` in REST API.
|
|
28
|
+
*
|
|
29
|
+
* @note This function works **ONLY** for errors thrown by REST API. For GraphQL APIs, use `client.isCancelError(error)`
|
|
30
|
+
* instead. `client` is generated from `generateClient()` API from `aws-amplify/api`.
|
|
31
|
+
*/
|
|
32
|
+
export const isCancelError = (error: unknown): error is CanceledError =>
|
|
33
|
+
!!error && error instanceof CanceledError;
|
package/src/errors/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
|
-
export {
|
|
4
|
+
export { CanceledError, isCancelError } from './CanceledError';
|
|
5
5
|
export { RestApiError } from './RestApiError';
|
|
6
6
|
export { assertValidationError } from './assertValidatonError';
|
|
7
7
|
export { RestApiValidationErrorCode, validationErrorMap } from './validation';
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
|
-
export { isCancelError } from './errors/
|
|
4
|
+
export { isCancelError } from './errors/CanceledError';
|
|
5
5
|
export { get, post, put, del, head, patch } from './apis';
|
package/src/server.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
|
-
export { isCancelError } from './errors/
|
|
4
|
+
export { isCancelError } from './errors/CanceledError';
|
|
5
5
|
export { get, post, put, del, head, patch } from './apis/server';
|
package/src/types/index.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
4
|
import { HttpResponse } from '@aws-amplify/core/internals/aws-client-utils';
|
|
5
|
-
import {
|
|
5
|
+
import { CanceledError } from '../errors';
|
|
6
6
|
import { Operation } from '../types';
|
|
7
7
|
import { parseRestApiServiceError } from './serviceError';
|
|
8
8
|
import { logger } from './logger';
|
|
@@ -59,13 +59,12 @@ export function createCancellableOperation(
|
|
|
59
59
|
} catch (error: any) {
|
|
60
60
|
const abortSignal = internalPostAbortSignal ?? publicApisAbortSignal;
|
|
61
61
|
if (error.name === 'AbortError' || abortSignal?.aborted === true) {
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
message: abortSignal.reason ?? error.message,
|
|
62
|
+
const canceledError = new CanceledError({
|
|
63
|
+
...(abortSignal.reason ? { message: abortSignal.reason } : undefined),
|
|
65
64
|
underlyingError: error,
|
|
66
65
|
});
|
|
67
66
|
logger.debug(error);
|
|
68
|
-
throw
|
|
67
|
+
throw canceledError;
|
|
69
68
|
}
|
|
70
69
|
logger.debug(error);
|
|
71
70
|
throw error;
|
|
@@ -82,7 +81,7 @@ export function createCancellableOperation(
|
|
|
82
81
|
publicApisAbortController.abort(abortMessage);
|
|
83
82
|
// Abort reason is not widely support enough across runtimes and and browsers, so we set it
|
|
84
83
|
// if it is not already set.
|
|
85
|
-
if (publicApisAbortSignal.reason !== abortMessage) {
|
|
84
|
+
if (abortMessage && publicApisAbortSignal.reason !== abortMessage) {
|
|
86
85
|
type AbortSignalWithReasonSupport = Omit<AbortSignal, 'reason'> & {
|
|
87
86
|
reason?: string;
|
|
88
87
|
};
|
package/src/utils/index.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
|
-
import './polyfills';
|
|
5
|
-
|
|
6
4
|
export { createCancellableOperation } from './createCancellableOperation';
|
|
7
5
|
export { resolveCredentials } from './resolveCredentials';
|
|
8
6
|
export { parseSigningInfo } from './parseSigningInfo';
|
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
4
|
import { AmplifyClassV6 } from '@aws-amplify/core';
|
|
5
|
+
import {
|
|
6
|
+
AmplifyUrl,
|
|
7
|
+
AmplifyUrlSearchParams,
|
|
8
|
+
} from '@aws-amplify/core/internals/utils';
|
|
5
9
|
import {
|
|
6
10
|
RestApiError,
|
|
7
11
|
RestApiValidationErrorCode,
|
|
@@ -28,13 +32,13 @@ export const resolveApiUrl = (
|
|
|
28
32
|
const urlStr = amplify.getConfig()?.API?.REST?.[apiName]?.endpoint;
|
|
29
33
|
assertValidationError(!!urlStr, RestApiValidationErrorCode.InvalidApiName);
|
|
30
34
|
try {
|
|
31
|
-
const url = new
|
|
35
|
+
const url = new AmplifyUrl(urlStr + path);
|
|
32
36
|
if (queryParams) {
|
|
33
|
-
const mergedQueryParams = new
|
|
37
|
+
const mergedQueryParams = new AmplifyUrlSearchParams(url.searchParams);
|
|
34
38
|
Object.entries(queryParams).forEach(([key, value]) => {
|
|
35
39
|
mergedQueryParams.set(key, value);
|
|
36
40
|
});
|
|
37
|
-
url.search = new
|
|
41
|
+
url.search = new AmplifyUrlSearchParams(mergedQueryParams).toString();
|
|
38
42
|
}
|
|
39
43
|
return url;
|
|
40
44
|
} catch (error) {
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { AmplifyErrorParams } from '@aws-amplify/core/internals/utils';
|
|
2
|
-
import { RestApiError } from './RestApiError';
|
|
3
|
-
/**
|
|
4
|
-
* Internal-only class for CancelledError.
|
|
5
|
-
*
|
|
6
|
-
* @internal
|
|
7
|
-
*/
|
|
8
|
-
export declare class CancelledError extends RestApiError {
|
|
9
|
-
constructor(params: AmplifyErrorParams);
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* Check if an error is caused by user calling `cancel()` REST API.
|
|
13
|
-
*/
|
|
14
|
-
export declare const isCancelError: (error: unknown) => boolean;
|