@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.
- package/README.md +3 -0
- package/internals/package.json +8 -0
- package/internals/server/package.json +8 -0
- package/lib/apis/common/handler.d.ts +27 -0
- package/lib/apis/common/handler.js +73 -0
- package/lib/apis/common/internalPost.d.ts +16 -0
- package/lib/apis/common/internalPost.js +65 -0
- package/lib/apis/common/publicApis.d.ts +8 -0
- package/lib/apis/common/publicApis.js +44 -0
- package/lib/apis/index.d.ts +203 -0
- package/lib/apis/index.js +215 -0
- package/lib/apis/server.d.ts +151 -0
- package/lib/apis/server.js +162 -0
- package/lib/errors/CanceledError.d.ts +17 -0
- package/lib/errors/CanceledError.js +32 -0
- package/lib/errors/RestApiError.d.ts +4 -0
- package/lib/errors/RestApiError.js +15 -0
- package/lib/errors/assertValidatonError.d.ts +5 -0
- package/lib/errors/assertValidatonError.js +17 -0
- package/lib/errors/index.d.ts +4 -0
- package/lib/errors/index.js +15 -0
- package/lib/errors/validation.d.ts +6 -0
- package/lib/errors/validation.js +19 -0
- package/lib/index.d.ts +2 -2
- package/lib/index.js +10 -6
- package/lib/internals/index.d.ts +19 -0
- package/lib/internals/index.js +30 -0
- package/lib/internals/server.d.ts +20 -0
- package/lib/internals/server.js +30 -0
- package/lib/server.d.ts +2 -0
- package/lib/server.js +14 -0
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/lib/types/index.d.ts +90 -37
- package/lib/types/index.js +0 -23
- package/lib/utils/constants.d.ts +8 -0
- package/lib/utils/constants.js +13 -0
- package/lib/utils/createCancellableOperation.d.ts +12 -0
- package/lib/utils/createCancellableOperation.js +63 -0
- package/lib/utils/index.d.ts +6 -0
- package/lib/utils/index.js +17 -0
- package/lib/utils/logger.d.ts +2 -0
- package/lib/utils/logger.js +7 -0
- package/lib/utils/normalizeHeaders.d.ts +1 -0
- package/lib/utils/normalizeHeaders.js +13 -0
- package/lib/utils/parseSigningInfo.d.ts +14 -0
- package/lib/utils/parseSigningInfo.js +41 -0
- package/lib/utils/resolveApiUrl.d.ts +12 -0
- package/lib/utils/resolveApiUrl.js +40 -0
- package/lib/utils/resolveCredentials.d.ts +5 -0
- package/lib/utils/resolveCredentials.js +15 -0
- package/lib/utils/serviceError.d.ts +10 -0
- package/lib/utils/serviceError.js +30 -0
- package/lib-esm/apis/common/handler.d.ts +27 -0
- package/lib-esm/apis/common/handler.js +69 -0
- package/lib-esm/apis/common/internalPost.d.ts +16 -0
- package/lib-esm/apis/common/internalPost.js +59 -0
- package/lib-esm/apis/common/publicApis.d.ts +8 -0
- package/lib-esm/apis/common/publicApis.js +35 -0
- package/lib-esm/apis/index.d.ts +203 -0
- package/lib-esm/apis/index.js +206 -0
- package/lib-esm/apis/server.d.ts +151 -0
- package/lib-esm/apis/server.js +153 -0
- package/lib-esm/errors/CanceledError.d.ts +17 -0
- package/lib-esm/errors/CanceledError.js +27 -0
- package/lib-esm/errors/RestApiError.d.ts +4 -0
- package/lib-esm/errors/RestApiError.js +11 -0
- package/lib-esm/errors/assertValidatonError.d.ts +5 -0
- package/lib-esm/errors/assertValidatonError.js +13 -0
- package/lib-esm/errors/index.d.ts +4 -0
- package/lib-esm/errors/index.js +6 -0
- package/lib-esm/errors/validation.d.ts +6 -0
- package/lib-esm/errors/validation.js +16 -0
- package/lib-esm/index.d.ts +2 -2
- package/lib-esm/index.js +2 -3
- package/lib-esm/internals/index.d.ts +19 -0
- package/lib-esm/internals/index.js +24 -0
- package/lib-esm/internals/server.d.ts +20 -0
- package/lib-esm/internals/server.js +24 -0
- package/lib-esm/server.d.ts +2 -0
- package/lib-esm/server.js +4 -0
- package/lib-esm/tsconfig.tsbuildinfo +1 -0
- package/lib-esm/types/index.d.ts +90 -37
- package/lib-esm/types/index.js +1 -23
- package/lib-esm/utils/constants.d.ts +8 -0
- package/lib-esm/utils/constants.js +10 -0
- package/lib-esm/utils/createCancellableOperation.d.ts +12 -0
- package/lib-esm/utils/createCancellableOperation.js +59 -0
- package/lib-esm/utils/index.d.ts +6 -0
- package/lib-esm/utils/index.js +8 -0
- package/lib-esm/utils/logger.d.ts +2 -0
- package/lib-esm/utils/logger.js +4 -0
- package/lib-esm/utils/normalizeHeaders.d.ts +1 -0
- package/lib-esm/utils/normalizeHeaders.js +9 -0
- package/lib-esm/utils/parseSigningInfo.d.ts +14 -0
- package/lib-esm/utils/parseSigningInfo.js +37 -0
- package/lib-esm/utils/resolveApiUrl.d.ts +12 -0
- package/lib-esm/utils/resolveApiUrl.js +36 -0
- package/lib-esm/utils/resolveCredentials.d.ts +5 -0
- package/lib-esm/utils/resolveCredentials.js +11 -0
- package/lib-esm/utils/serviceError.d.ts +10 -0
- package/lib-esm/utils/serviceError.js +25 -0
- package/package.json +113 -105
- package/server/package.json +8 -0
- package/src/apis/common/handler.ts +109 -0
- package/src/apis/common/internalPost.ts +81 -0
- package/src/apis/common/publicApis.ts +99 -0
- package/src/apis/index.ts +239 -0
- package/src/apis/server.ts +209 -0
- package/src/errors/CanceledError.ts +33 -0
- package/src/errors/RestApiError.ts +17 -0
- package/src/errors/assertValidatonError.ts +19 -0
- package/src/errors/index.ts +7 -0
- package/src/errors/validation.ts +20 -0
- package/src/index.ts +2 -2
- package/src/internals/index.ts +31 -0
- package/src/internals/server.ts +37 -0
- package/src/server.ts +5 -0
- package/src/types/index.ts +95 -44
- package/src/utils/constants.ts +15 -0
- package/src/utils/createCancellableOperation.ts +94 -0
- package/src/utils/index.ts +9 -0
- package/src/utils/logger.ts +6 -0
- package/src/utils/normalizeHeaders.ts +10 -0
- package/src/utils/parseSigningInfo.ts +52 -0
- package/src/utils/resolveApiUrl.ts +51 -0
- package/src/utils/resolveCredentials.ts +17 -0
- package/src/utils/serviceError.ts +35 -0
- package/lib/.tsbuildinfo +0 -3
- package/lib/RestAPI.d.ts +0 -108
- package/lib/RestAPI.js +0 -282
- package/lib/RestAPI.js.map +0 -1
- package/lib/RestClient.d.ts +0 -138
- package/lib/RestClient.js +0 -368
- package/lib/RestClient.js.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/types/index.js.map +0 -1
- package/lib-esm/.tsbuildinfo +0 -3
- package/lib-esm/RestAPI.d.ts +0 -108
- package/lib-esm/RestAPI.js +0 -280
- package/lib-esm/RestAPI.js.map +0 -1
- package/lib-esm/RestClient.d.ts +0 -138
- package/lib-esm/RestClient.js +0 -366
- package/lib-esm/RestClient.js.map +0 -1
- package/lib-esm/index.js.map +0 -1
- package/lib-esm/types/index.js.map +0 -1
- package/src/RestAPI.ts +0 -338
- package/src/RestClient.ts +0 -417
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { AmplifyClassV6 } from '@aws-amplify/core';
|
|
5
|
+
import {
|
|
6
|
+
GetInput,
|
|
7
|
+
GetOperation,
|
|
8
|
+
PostInput,
|
|
9
|
+
PostOperation,
|
|
10
|
+
PutInput,
|
|
11
|
+
PutOperation,
|
|
12
|
+
DeleteInput,
|
|
13
|
+
DeleteOperation,
|
|
14
|
+
HeadInput,
|
|
15
|
+
HeadOperation,
|
|
16
|
+
PatchInput,
|
|
17
|
+
PatchOperation,
|
|
18
|
+
ApiInput,
|
|
19
|
+
RestApiOptionsBase,
|
|
20
|
+
} from '../../types';
|
|
21
|
+
import {
|
|
22
|
+
resolveApiUrl,
|
|
23
|
+
createCancellableOperation,
|
|
24
|
+
logger,
|
|
25
|
+
parseSigningInfo,
|
|
26
|
+
} from '../../utils';
|
|
27
|
+
import { transferHandler } from './handler';
|
|
28
|
+
|
|
29
|
+
const publicHandler = (
|
|
30
|
+
amplify: AmplifyClassV6,
|
|
31
|
+
options: ApiInput<RestApiOptionsBase>,
|
|
32
|
+
method: string
|
|
33
|
+
) =>
|
|
34
|
+
createCancellableOperation(async abortSignal => {
|
|
35
|
+
const { apiName, options: apiOptions = {}, path: apiPath } = options;
|
|
36
|
+
const url = resolveApiUrl(
|
|
37
|
+
amplify,
|
|
38
|
+
apiName,
|
|
39
|
+
apiPath,
|
|
40
|
+
apiOptions?.queryParams
|
|
41
|
+
);
|
|
42
|
+
const libraryOptionsHeaders =
|
|
43
|
+
await amplify.libraryOptions?.API?.REST?.headers?.({
|
|
44
|
+
apiName,
|
|
45
|
+
});
|
|
46
|
+
const { headers: invocationHeaders = {} } = apiOptions;
|
|
47
|
+
const headers = {
|
|
48
|
+
// custom headers from invocation options should precede library options
|
|
49
|
+
...libraryOptionsHeaders,
|
|
50
|
+
...invocationHeaders,
|
|
51
|
+
};
|
|
52
|
+
const signingServiceInfo = parseSigningInfo(url, {
|
|
53
|
+
amplify,
|
|
54
|
+
apiName,
|
|
55
|
+
});
|
|
56
|
+
logger.debug(
|
|
57
|
+
method,
|
|
58
|
+
url,
|
|
59
|
+
headers,
|
|
60
|
+
`IAM signing options: ${JSON.stringify(signingServiceInfo)}`
|
|
61
|
+
);
|
|
62
|
+
return transferHandler(
|
|
63
|
+
amplify,
|
|
64
|
+
{
|
|
65
|
+
...apiOptions,
|
|
66
|
+
url,
|
|
67
|
+
method,
|
|
68
|
+
headers,
|
|
69
|
+
abortSignal,
|
|
70
|
+
},
|
|
71
|
+
signingServiceInfo
|
|
72
|
+
);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
export const get = (amplify: AmplifyClassV6, input: GetInput): GetOperation =>
|
|
76
|
+
publicHandler(amplify, input, 'GET');
|
|
77
|
+
|
|
78
|
+
export const post = (
|
|
79
|
+
amplify: AmplifyClassV6,
|
|
80
|
+
input: PostInput
|
|
81
|
+
): PostOperation => publicHandler(amplify, input, 'POST');
|
|
82
|
+
|
|
83
|
+
export const put = (amplify: AmplifyClassV6, input: PutInput): PutOperation =>
|
|
84
|
+
publicHandler(amplify, input, 'PUT');
|
|
85
|
+
|
|
86
|
+
export const del = (
|
|
87
|
+
amplify: AmplifyClassV6,
|
|
88
|
+
input: DeleteInput
|
|
89
|
+
): DeleteOperation => publicHandler(amplify, input, 'DELETE');
|
|
90
|
+
|
|
91
|
+
export const head = (
|
|
92
|
+
amplify: AmplifyClassV6,
|
|
93
|
+
input: HeadInput
|
|
94
|
+
): HeadOperation => publicHandler(amplify, input, 'HEAD');
|
|
95
|
+
|
|
96
|
+
export const patch = (
|
|
97
|
+
amplify: AmplifyClassV6,
|
|
98
|
+
input: PatchInput
|
|
99
|
+
): PatchOperation => publicHandler(amplify, input, 'PATCH');
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { Amplify } from '@aws-amplify/core';
|
|
5
|
+
import {
|
|
6
|
+
get as commonGet,
|
|
7
|
+
post as commonPost,
|
|
8
|
+
put as commonPut,
|
|
9
|
+
del as commonDel,
|
|
10
|
+
head as commonHead,
|
|
11
|
+
patch as commonPatch,
|
|
12
|
+
} from './common/publicApis';
|
|
13
|
+
import {
|
|
14
|
+
DeleteInput,
|
|
15
|
+
DeleteOperation,
|
|
16
|
+
GetInput,
|
|
17
|
+
GetOperation,
|
|
18
|
+
HeadInput,
|
|
19
|
+
HeadOperation,
|
|
20
|
+
PatchInput,
|
|
21
|
+
PatchOperation,
|
|
22
|
+
PostInput,
|
|
23
|
+
PostOperation,
|
|
24
|
+
PutInput,
|
|
25
|
+
PutOperation,
|
|
26
|
+
} from '../types';
|
|
27
|
+
import { RestApiError } from '../errors';
|
|
28
|
+
|
|
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
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
export const get = (input: GetInput): GetOperation => commonGet(Amplify, input);
|
|
69
|
+
|
|
70
|
+
/**
|
|
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
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
export const post = (input: PostInput): PostOperation =>
|
|
110
|
+
commonPost(Amplify, input);
|
|
111
|
+
|
|
112
|
+
/**
|
|
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
|
+
* ```
|
|
149
|
+
*/
|
|
150
|
+
export const put = (input: PutInput): PutOperation => commonPut(Amplify, input);
|
|
151
|
+
|
|
152
|
+
/**
|
|
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
|
+
* ```
|
|
171
|
+
*/
|
|
172
|
+
export const del = (input: DeleteInput): DeleteOperation =>
|
|
173
|
+
commonDel(Amplify, input);
|
|
174
|
+
|
|
175
|
+
/**
|
|
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
|
+
*
|
|
195
|
+
*/
|
|
196
|
+
export const head = (input: HeadInput): HeadOperation =>
|
|
197
|
+
commonHead(Amplify, input);
|
|
198
|
+
|
|
199
|
+
/**
|
|
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
|
+
* ```
|
|
237
|
+
*/
|
|
238
|
+
export const patch = (input: PatchInput): PatchOperation =>
|
|
239
|
+
commonPatch(Amplify, input);
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
AmplifyServer,
|
|
6
|
+
getAmplifyServerContext,
|
|
7
|
+
} from '@aws-amplify/core/internals/adapter-core';
|
|
8
|
+
import {
|
|
9
|
+
get as commonGet,
|
|
10
|
+
post as commonPost,
|
|
11
|
+
put as commonPut,
|
|
12
|
+
del as commonDel,
|
|
13
|
+
head as commonHead,
|
|
14
|
+
patch as commonPatch,
|
|
15
|
+
} from './common/publicApis';
|
|
16
|
+
import {
|
|
17
|
+
DeleteInput,
|
|
18
|
+
DeleteOperation,
|
|
19
|
+
GetInput,
|
|
20
|
+
GetOperation,
|
|
21
|
+
HeadInput,
|
|
22
|
+
HeadOperation,
|
|
23
|
+
PatchInput,
|
|
24
|
+
PatchOperation,
|
|
25
|
+
PostInput,
|
|
26
|
+
PostOperation,
|
|
27
|
+
PutInput,
|
|
28
|
+
PutOperation,
|
|
29
|
+
} from '../types';
|
|
30
|
+
import { RestApiError } from '../errors';
|
|
31
|
+
|
|
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}
|
|
56
|
+
*/
|
|
57
|
+
export const get = (
|
|
58
|
+
contextSpec: AmplifyServer.ContextSpec,
|
|
59
|
+
input: GetInput
|
|
60
|
+
): GetOperation =>
|
|
61
|
+
commonGet(getAmplifyServerContext(contextSpec).amplify, input);
|
|
62
|
+
|
|
63
|
+
/**
|
|
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
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export const post = (
|
|
88
|
+
contextSpec: AmplifyServer.ContextSpec,
|
|
89
|
+
input: PostInput
|
|
90
|
+
): PostOperation =>
|
|
91
|
+
commonPost(getAmplifyServerContext(contextSpec).amplify, input);
|
|
92
|
+
|
|
93
|
+
/**
|
|
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
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
export const put = (
|
|
118
|
+
contextSpec: AmplifyServer.ContextSpec,
|
|
119
|
+
input: PutInput
|
|
120
|
+
): PutOperation =>
|
|
121
|
+
commonPut(getAmplifyServerContext(contextSpec).amplify, input);
|
|
122
|
+
|
|
123
|
+
/**
|
|
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
|
+
* ```
|
|
145
|
+
*/
|
|
146
|
+
export const del = (
|
|
147
|
+
contextSpec: AmplifyServer.ContextSpec,
|
|
148
|
+
input: DeleteInput
|
|
149
|
+
): DeleteOperation =>
|
|
150
|
+
commonDel(getAmplifyServerContext(contextSpec).amplify, input);
|
|
151
|
+
|
|
152
|
+
/**
|
|
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
|
+
* ```
|
|
174
|
+
*/
|
|
175
|
+
export const head = (
|
|
176
|
+
contextSpec: AmplifyServer.ContextSpec,
|
|
177
|
+
input: HeadInput
|
|
178
|
+
): HeadOperation =>
|
|
179
|
+
commonHead(getAmplifyServerContext(contextSpec).amplify, input);
|
|
180
|
+
|
|
181
|
+
/**
|
|
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
|
+
* ```
|
|
204
|
+
*/
|
|
205
|
+
export const patch = (
|
|
206
|
+
contextSpec: AmplifyServer.ContextSpec,
|
|
207
|
+
input: PatchInput
|
|
208
|
+
): PatchOperation =>
|
|
209
|
+
commonPatch(getAmplifyServerContext(contextSpec).amplify, input);
|
|
@@ -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;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
AmplifyError,
|
|
6
|
+
AmplifyErrorParams,
|
|
7
|
+
} from '@aws-amplify/core/internals/utils';
|
|
8
|
+
|
|
9
|
+
export class RestApiError extends AmplifyError {
|
|
10
|
+
constructor(params: AmplifyErrorParams) {
|
|
11
|
+
super(params);
|
|
12
|
+
|
|
13
|
+
// TODO: Delete the following 2 lines after we change the build target to >= es2015
|
|
14
|
+
this.constructor = RestApiError;
|
|
15
|
+
Object.setPrototypeOf(this, RestApiError.prototype);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { RestApiError } from './RestApiError';
|
|
5
|
+
import { RestApiValidationErrorCode, validationErrorMap } from './validation';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
export function assertValidationError(
|
|
11
|
+
assertion: boolean,
|
|
12
|
+
name: RestApiValidationErrorCode
|
|
13
|
+
): asserts assertion {
|
|
14
|
+
const { message, recoverySuggestion } = validationErrorMap[name];
|
|
15
|
+
|
|
16
|
+
if (!assertion) {
|
|
17
|
+
throw new RestApiError({ name, message, recoverySuggestion });
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
export { CanceledError, isCancelError } from './CanceledError';
|
|
5
|
+
export { RestApiError } from './RestApiError';
|
|
6
|
+
export { assertValidationError } from './assertValidatonError';
|
|
7
|
+
export { RestApiValidationErrorCode, validationErrorMap } from './validation';
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { AmplifyErrorMap } from '@aws-amplify/core/internals/utils';
|
|
5
|
+
|
|
6
|
+
export enum RestApiValidationErrorCode {
|
|
7
|
+
NoCredentials = 'NoCredentials',
|
|
8
|
+
InvalidApiName = 'InvalidApiName',
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const validationErrorMap: AmplifyErrorMap<RestApiValidationErrorCode> = {
|
|
12
|
+
[RestApiValidationErrorCode.NoCredentials]: {
|
|
13
|
+
message: 'Credentials should not be empty.',
|
|
14
|
+
},
|
|
15
|
+
[RestApiValidationErrorCode.InvalidApiName]: {
|
|
16
|
+
message: 'API name is invalid.',
|
|
17
|
+
recoverySuggestion:
|
|
18
|
+
'Check if the API name matches the one in your configuration or `aws-exports.js`',
|
|
19
|
+
},
|
|
20
|
+
};
|
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 {
|
|
5
|
-
export {
|
|
4
|
+
export { isCancelError } from './errors/CanceledError';
|
|
5
|
+
export { get, post, put, del, head, patch } from './apis';
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { Amplify } from '@aws-amplify/core';
|
|
4
|
+
|
|
5
|
+
import { post as internalPost } from '../apis/common/internalPost';
|
|
6
|
+
import { InternalPostInput } from '../types';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Internal-only REST POST handler to send GraphQL request to given endpoint. By default, it will use IAM to authorize
|
|
10
|
+
* the request. In some auth modes, the IAM auth has to be disabled. Here's how to set up the request auth correctly:
|
|
11
|
+
* * If auth mode is 'iam', you MUST NOT set 'authorization' header and 'x-api-key' header, since it would disable IAM
|
|
12
|
+
* auth. You MUST also set 'input.options.signingServiceInfo' option.
|
|
13
|
+
* * The including 'input.options.signingServiceInfo.service' and 'input.options.signingServiceInfo.region' are
|
|
14
|
+
* optional. If omitted, the signing service and region will be inferred from url.
|
|
15
|
+
* * If auth mode is 'none', you MUST NOT set 'options.signingServiceInfo' option.
|
|
16
|
+
* * If auth mode is 'apiKey', you MUST set 'x-api-key' custom header.
|
|
17
|
+
* * If auth mode is 'oidc' or 'lambda' or 'userPool', you MUST set 'authorization' header.
|
|
18
|
+
*
|
|
19
|
+
* To make the internal post cancellable, you must also call `updateRequestToBeCancellable()` with the promise from
|
|
20
|
+
* internal post call and the abort controller supplied to the internal post call.
|
|
21
|
+
*
|
|
22
|
+
* @internal
|
|
23
|
+
*/
|
|
24
|
+
export const post = (input: InternalPostInput) => {
|
|
25
|
+
return internalPost(Amplify, input);
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export {
|
|
29
|
+
cancel,
|
|
30
|
+
updateRequestToBeCancellable,
|
|
31
|
+
} from '../apis/common/internalPost';
|