@aws-amplify/api-rest 4.0.1-console-preview.b5a42af.0 → 4.0.1-console-preview.9607e4a.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/{common → apis/common}/handler.d.ts +7 -7
- package/lib/{common → apis/common}/handler.js +5 -6
- package/{lib-esm → lib/apis}/common/internalPost.d.ts +1 -1
- package/lib/{common → apis/common}/internalPost.js +12 -1
- package/lib/apis/common/publicApis.d.ts +8 -0
- package/lib/apis/common/publicApis.js +50 -0
- package/lib/apis/index.d.ts +25 -0
- package/lib/apis/index.js +45 -0
- package/lib/apis/server.d.ts +26 -0
- package/lib/apis/server.js +49 -0
- package/lib/errors/validation.d.ts +2 -1
- package/lib/errors/validation.js +5 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +8 -1
- package/lib/internals/index.d.ts +1 -1
- package/lib/internals/index.js +2 -2
- package/lib/internals/server.d.ts +1 -1
- package/lib/internals/server.js +2 -2
- package/lib/server.d.ts +2 -0
- package/lib/server.js +14 -0
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/types/index.d.ts +19 -8
- package/lib/utils/createCancellableOperation.d.ts +2 -1
- package/lib/utils/createCancellableOperation.js +29 -19
- package/lib/utils/index.d.ts +3 -1
- package/lib/utils/index.js +7 -3
- package/lib/utils/index.native.d.ts +6 -0
- package/lib/utils/index.native.js +19 -0
- package/lib/utils/logger.d.ts +2 -0
- package/lib/utils/logger.js +7 -0
- package/lib/utils/normalizeHeaders.d.ts +1 -1
- package/lib/utils/parseSigningInfo.d.ts +14 -0
- package/lib/utils/parseSigningInfo.js +42 -0
- package/lib/utils/resolveApiUrl.d.ts +12 -0
- package/lib/utils/resolveApiUrl.js +38 -0
- package/lib/utils/resolveCredentials.d.ts +2 -2
- package/lib/utils/resolveCredentials.js +1 -1
- package/lib/utils/serviceError.d.ts +1 -1
- package/lib/utils/serviceError.js +8 -6
- package/lib-esm/{common → apis/common}/handler.d.ts +7 -7
- package/lib-esm/{common → apis/common}/handler.js +5 -6
- package/{lib → lib-esm/apis}/common/internalPost.d.ts +1 -1
- package/lib-esm/{common → apis/common}/internalPost.js +12 -1
- package/lib-esm/apis/common/publicApis.d.ts +8 -0
- package/lib-esm/apis/common/publicApis.js +41 -0
- package/lib-esm/apis/index.d.ts +25 -0
- package/lib-esm/apis/index.js +36 -0
- package/lib-esm/apis/server.d.ts +26 -0
- package/lib-esm/apis/server.js +40 -0
- package/lib-esm/errors/validation.d.ts +2 -1
- package/lib-esm/errors/validation.js +5 -0
- package/lib-esm/index.d.ts +1 -0
- package/lib-esm/index.js +1 -0
- package/lib-esm/internals/index.d.ts +1 -1
- package/lib-esm/internals/index.js +2 -2
- package/lib-esm/internals/server.d.ts +1 -1
- package/lib-esm/internals/server.js +2 -2
- package/lib-esm/server.d.ts +2 -0
- package/lib-esm/server.js +4 -0
- package/lib-esm/tsconfig.tsbuildinfo +1 -1
- package/lib-esm/types/index.d.ts +19 -8
- package/lib-esm/utils/createCancellableOperation.d.ts +2 -1
- package/lib-esm/utils/createCancellableOperation.js +31 -21
- package/lib-esm/utils/index.d.ts +3 -1
- package/lib-esm/utils/index.js +3 -1
- package/lib-esm/utils/index.native.d.ts +6 -0
- package/lib-esm/utils/index.native.js +10 -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 -1
- package/lib-esm/utils/parseSigningInfo.d.ts +14 -0
- package/lib-esm/utils/parseSigningInfo.js +38 -0
- package/lib-esm/utils/resolveApiUrl.d.ts +12 -0
- package/lib-esm/utils/resolveApiUrl.js +34 -0
- package/lib-esm/utils/resolveCredentials.d.ts +2 -2
- package/lib-esm/utils/resolveCredentials.js +1 -1
- package/lib-esm/utils/serviceError.d.ts +1 -1
- package/lib-esm/utils/serviceError.js +8 -6
- package/package.json +6 -7
- package/src/{common → apis/common}/handler.ts +8 -9
- package/src/{common → apis/common}/internalPost.ts +13 -2
- package/src/apis/common/publicApis.ts +99 -0
- package/src/apis/index.ts +60 -0
- package/src/apis/server.ts +83 -0
- package/src/errors/validation.ts +6 -0
- package/src/index.ts +1 -0
- package/src/internals/index.ts +5 -2
- package/src/internals/server.ts +5 -2
- package/src/server.ts +5 -0
- package/src/types/index.ts +19 -8
- package/src/utils/createCancellableOperation.ts +37 -19
- package/src/utils/index.native.ts +13 -0
- package/src/utils/index.ts +3 -1
- package/src/utils/logger.ts +6 -0
- package/src/utils/normalizeHeaders.ts +1 -1
- package/src/utils/{parseUrl.ts → parseSigningInfo.ts} +20 -7
- package/src/utils/resolveApiUrl.ts +47 -0
- package/src/utils/resolveCredentials.ts +1 -1
- package/src/utils/serviceError.ts +9 -7
- package/lib/utils/parseUrl.d.ts +0 -10
- package/lib/utils/parseUrl.js +0 -41
- package/lib-esm/utils/parseUrl.d.ts +0 -10
- package/lib-esm/utils/parseUrl.js +0 -37
package/lib-esm/types/index.d.ts
CHANGED
|
@@ -1,17 +1,20 @@
|
|
|
1
1
|
import { DocumentType } from '@aws-amplify/core/internals/utils';
|
|
2
|
-
export type
|
|
3
|
-
export type
|
|
4
|
-
export type
|
|
5
|
-
export type
|
|
6
|
-
export type
|
|
7
|
-
export type
|
|
2
|
+
export type GetInput = ApiInput<RestApiOptionsBase>;
|
|
3
|
+
export type PostInput = ApiInput<RestApiOptionsBase>;
|
|
4
|
+
export type PutInput = ApiInput<RestApiOptionsBase>;
|
|
5
|
+
export type PatchInput = ApiInput<RestApiOptionsBase>;
|
|
6
|
+
export type DeleteInput = ApiInput<Omit<RestApiOptionsBase, 'body'>>;
|
|
7
|
+
export type HeadInput = ApiInput<Omit<RestApiOptionsBase, 'body'>>;
|
|
8
8
|
export type GetOperation = Operation<RestApiResponse>;
|
|
9
9
|
export type PostOperation = Operation<RestApiResponse>;
|
|
10
10
|
export type PutOperation = Operation<RestApiResponse>;
|
|
11
11
|
export type PatchOperation = Operation<RestApiResponse>;
|
|
12
12
|
export type DeleteOperation = Operation<Omit<RestApiResponse, 'body'>>;
|
|
13
13
|
export type HeadOperation = Operation<Omit<RestApiResponse, 'body'>>;
|
|
14
|
-
|
|
14
|
+
/**
|
|
15
|
+
* @internal
|
|
16
|
+
*/
|
|
17
|
+
export type RestApiOptionsBase = {
|
|
15
18
|
headers?: Headers;
|
|
16
19
|
queryParams?: Record<string, string>;
|
|
17
20
|
body?: DocumentType | FormData;
|
|
@@ -54,12 +57,20 @@ export interface RestApiResponse {
|
|
|
54
57
|
headers: Headers;
|
|
55
58
|
}
|
|
56
59
|
/**
|
|
57
|
-
* Input type of REST API.
|
|
58
60
|
* @internal
|
|
59
61
|
*/
|
|
60
62
|
export type ApiInput<Options> = {
|
|
63
|
+
/**
|
|
64
|
+
* Name of the REST API configured in Amplify singleton.
|
|
65
|
+
*/
|
|
61
66
|
apiName: string;
|
|
67
|
+
/**
|
|
68
|
+
* Path of the REST API.
|
|
69
|
+
*/
|
|
62
70
|
path: string;
|
|
71
|
+
/**
|
|
72
|
+
* Options to overwrite the REST API call behavior.
|
|
73
|
+
*/
|
|
63
74
|
options?: Options;
|
|
64
75
|
};
|
|
65
76
|
/**
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { HttpResponse } from '@aws-amplify/core/internals/aws-client-utils';
|
|
2
|
+
import { Operation } from '../types';
|
|
2
3
|
/**
|
|
3
4
|
* Create a cancellable operation conforming to the internal POST API interface.
|
|
4
5
|
* @internal
|
|
@@ -8,4 +9,4 @@ export declare function createCancellableOperation(handler: () => Promise<HttpRe
|
|
|
8
9
|
* Create a cancellable operation conforming to the external REST API interface.
|
|
9
10
|
* @internal
|
|
10
11
|
*/
|
|
11
|
-
export declare function createCancellableOperation(handler: (signal: AbortSignal) => Promise<HttpResponse>):
|
|
12
|
+
export declare function createCancellableOperation(handler: (signal: AbortSignal) => Promise<HttpResponse>): Operation<HttpResponse>;
|
|
@@ -1,42 +1,52 @@
|
|
|
1
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
import {
|
|
4
|
-
import { CancelledError
|
|
3
|
+
import { __awaiter, __generator } from "tslib";
|
|
4
|
+
import { CancelledError } from '../errors';
|
|
5
5
|
import { parseRestApiServiceError } from './serviceError';
|
|
6
|
+
import { logger } from './logger';
|
|
6
7
|
/**
|
|
7
8
|
* @internal
|
|
8
9
|
*/
|
|
9
10
|
export function createCancellableOperation(handler, abortController) {
|
|
10
11
|
var _this = this;
|
|
11
12
|
var isInternalPost = function (handler) { return !!abortController; };
|
|
12
|
-
|
|
13
|
+
// For creating a cancellable operation for public REST APIs, we need to create an AbortController
|
|
14
|
+
// internally. Whereas for internal POST APIs, we need to accept in the AbortController from the
|
|
15
|
+
// callers.
|
|
16
|
+
var publicApisAbortController = new AbortController();
|
|
17
|
+
var publicApisAbortSignal = publicApisAbortController.signal;
|
|
18
|
+
var internalPostAbortSignal = abortController === null || abortController === void 0 ? void 0 : abortController.signal;
|
|
13
19
|
var job = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
14
|
-
var response, error_1;
|
|
20
|
+
var response, error_1, abortSignal, cancelledError;
|
|
15
21
|
var _a;
|
|
16
22
|
return __generator(this, function (_b) {
|
|
17
23
|
switch (_b.label) {
|
|
18
24
|
case 0:
|
|
19
|
-
_b.trys.push([0,
|
|
25
|
+
_b.trys.push([0, 4, , 5]);
|
|
20
26
|
return [4 /*yield*/, (isInternalPost(handler)
|
|
21
27
|
? handler()
|
|
22
|
-
: handler(
|
|
28
|
+
: handler(publicApisAbortSignal))];
|
|
23
29
|
case 1:
|
|
24
30
|
response = _b.sent();
|
|
25
|
-
if (response.statusCode >= 300)
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
case
|
|
31
|
+
if (!(response.statusCode >= 300)) return [3 /*break*/, 3];
|
|
32
|
+
return [4 /*yield*/, parseRestApiServiceError(response)];
|
|
33
|
+
case 2: throw _b.sent();
|
|
34
|
+
case 3: return [2 /*return*/, response];
|
|
35
|
+
case 4:
|
|
30
36
|
error_1 = _b.sent();
|
|
31
|
-
|
|
32
|
-
|
|
37
|
+
abortSignal = internalPostAbortSignal !== null && internalPostAbortSignal !== void 0 ? internalPostAbortSignal : publicApisAbortSignal;
|
|
38
|
+
if (error_1.name === 'AbortError' || (abortSignal === null || abortSignal === void 0 ? void 0 : abortSignal.aborted) === true) {
|
|
39
|
+
cancelledError = new CancelledError({
|
|
33
40
|
name: error_1.name,
|
|
34
|
-
message: (_a =
|
|
41
|
+
message: (_a = abortSignal.reason) !== null && _a !== void 0 ? _a : error_1.message,
|
|
35
42
|
underlyingError: error_1,
|
|
36
43
|
});
|
|
44
|
+
logger.debug(error_1);
|
|
45
|
+
throw cancelledError;
|
|
37
46
|
}
|
|
38
|
-
|
|
39
|
-
|
|
47
|
+
logger.debug(error_1);
|
|
48
|
+
throw error_1;
|
|
49
|
+
case 5: return [2 /*return*/];
|
|
40
50
|
}
|
|
41
51
|
});
|
|
42
52
|
}); };
|
|
@@ -45,15 +55,15 @@ export function createCancellableOperation(handler, abortController) {
|
|
|
45
55
|
}
|
|
46
56
|
else {
|
|
47
57
|
var cancel = function (abortMessage) {
|
|
48
|
-
if (
|
|
58
|
+
if (publicApisAbortSignal.aborted === true) {
|
|
49
59
|
return;
|
|
50
60
|
}
|
|
51
|
-
|
|
61
|
+
publicApisAbortController.abort(abortMessage);
|
|
52
62
|
// Abort reason is not widely support enough across runtimes and and browsers, so we set it
|
|
53
63
|
// if it is not already set.
|
|
54
|
-
if (
|
|
55
|
-
|
|
56
|
-
|
|
64
|
+
if (publicApisAbortSignal.reason !== abortMessage) {
|
|
65
|
+
publicApisAbortSignal['reason'] =
|
|
66
|
+
abortMessage;
|
|
57
67
|
}
|
|
58
68
|
};
|
|
59
69
|
return { response: job(), cancel: cancel };
|
package/lib-esm/utils/index.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
export { createCancellableOperation } from './createCancellableOperation';
|
|
2
2
|
export { resolveCredentials } from './resolveCredentials';
|
|
3
|
-
export {
|
|
3
|
+
export { parseSigningInfo } from './parseSigningInfo';
|
|
4
4
|
export { parseRestApiServiceError } from './serviceError';
|
|
5
|
+
export { resolveApiUrl } from './resolveApiUrl';
|
|
6
|
+
export { logger } from './logger';
|
package/lib-esm/utils/index.js
CHANGED
|
@@ -2,5 +2,7 @@
|
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
export { createCancellableOperation } from './createCancellableOperation';
|
|
4
4
|
export { resolveCredentials } from './resolveCredentials';
|
|
5
|
-
export {
|
|
5
|
+
export { parseSigningInfo } from './parseSigningInfo';
|
|
6
6
|
export { parseRestApiServiceError } from './serviceError';
|
|
7
|
+
export { resolveApiUrl } from './resolveApiUrl';
|
|
8
|
+
export { logger } from './logger';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { createCancellableOperation } from './createCancellableOperation';
|
|
2
|
+
export { resolveCredentials } from './resolveCredentials';
|
|
3
|
+
export { parseSigningInfo } from './parseSigningInfo';
|
|
4
|
+
export { parseRestApiServiceError } from './serviceError';
|
|
5
|
+
export { resolveApiUrl } from './resolveApiUrl';
|
|
6
|
+
export { logger } from './logger';
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { loadUrlPolyfill } from '@aws-amplify/react-native';
|
|
4
|
+
loadUrlPolyfill();
|
|
5
|
+
export { createCancellableOperation } from './createCancellableOperation';
|
|
6
|
+
export { resolveCredentials } from './resolveCredentials';
|
|
7
|
+
export { parseSigningInfo } from './parseSigningInfo';
|
|
8
|
+
export { parseRestApiServiceError } from './serviceError';
|
|
9
|
+
export { resolveApiUrl } from './resolveApiUrl';
|
|
10
|
+
export { logger } from './logger';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const normalizeHeaders: (headers
|
|
1
|
+
export declare const normalizeHeaders: (headers?: Record<string, string>) => Record<string, string>;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { AmplifyClassV6 } from '@aws-amplify/core';
|
|
2
|
+
/**
|
|
3
|
+
* Infer the signing service and region from the given URL, and for REST API only, from the Amplify configuration.
|
|
4
|
+
* It supports raw API Gateway endpoint and AppSync endpoint.
|
|
5
|
+
*
|
|
6
|
+
* @internal
|
|
7
|
+
*/
|
|
8
|
+
export declare const parseSigningInfo: (url: URL, restApiOptions?: {
|
|
9
|
+
amplify: AmplifyClassV6;
|
|
10
|
+
apiName: string;
|
|
11
|
+
}) => {
|
|
12
|
+
service: string;
|
|
13
|
+
region: string;
|
|
14
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { __read } from "tslib";
|
|
4
|
+
import { APIG_HOSTNAME_PATTERN, DEFAULT_IAM_SIGNING_REGION, DEFAULT_REST_IAM_SIGNING_SERVICE, } from './constants';
|
|
5
|
+
/**
|
|
6
|
+
* Infer the signing service and region from the given URL, and for REST API only, from the Amplify configuration.
|
|
7
|
+
* It supports raw API Gateway endpoint and AppSync endpoint.
|
|
8
|
+
*
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
export var parseSigningInfo = function (url, restApiOptions) {
|
|
12
|
+
var _a, _b, _c, _d, _e;
|
|
13
|
+
var _f = (_d = (_c = (_b = (_a = restApiOptions === null || restApiOptions === void 0 ? void 0 : restApiOptions.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[restApiOptions === null || restApiOptions === void 0 ? void 0 : restApiOptions.apiName]) !== null && _d !== void 0 ? _d : {}, _g = _f.service, signingService = _g === void 0 ? DEFAULT_REST_IAM_SIGNING_SERVICE : _g, _h = _f.region, signingRegion = _h === void 0 ? DEFAULT_IAM_SIGNING_REGION : _h;
|
|
14
|
+
var hostname = url.hostname;
|
|
15
|
+
var _j = __read((_e = APIG_HOSTNAME_PATTERN.exec(hostname)) !== null && _e !== void 0 ? _e : [], 3), service = _j[1], region = _j[2];
|
|
16
|
+
if (service === DEFAULT_REST_IAM_SIGNING_SERVICE) {
|
|
17
|
+
// The configured endpoint is an API Gateway endpoint
|
|
18
|
+
// @see: https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-call-api.html
|
|
19
|
+
return {
|
|
20
|
+
service: service,
|
|
21
|
+
region: region !== null && region !== void 0 ? region : signingRegion,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
else if (service === 'appsync-api') {
|
|
25
|
+
// AppSync endpoint is internally supported because GraphQL operation will send request using POST handler.
|
|
26
|
+
// example: https://xxxx.appsync-api.us-east-1.amazonaws.com/graphql
|
|
27
|
+
return {
|
|
28
|
+
service: 'appsync',
|
|
29
|
+
region: region !== null && region !== void 0 ? region : signingRegion,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
return {
|
|
34
|
+
service: signingService,
|
|
35
|
+
region: signingRegion,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { AmplifyClassV6 } from '@aws-amplify/core';
|
|
2
|
+
/**
|
|
3
|
+
* Resolve the REST API request URL by:
|
|
4
|
+
* 1. Loading the REST API endpoint from the Amplify configuration with corresponding API name.
|
|
5
|
+
* 2. Appending the path to the endpoint.
|
|
6
|
+
* 3. Merge the query parameters from path and the queryParameter argument which is taken from the public REST API
|
|
7
|
+
* options.
|
|
8
|
+
* 4. Validating the resulting URL string.
|
|
9
|
+
*
|
|
10
|
+
* @internal
|
|
11
|
+
*/
|
|
12
|
+
export declare const resolveApiUrl: (amplify: AmplifyClassV6, apiName: string, path: string, queryParams?: Record<string, string>) => URL;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { __assign, __read } from "tslib";
|
|
4
|
+
import { RestApiError, RestApiValidationErrorCode, assertValidationError, validationErrorMap, } from '../errors';
|
|
5
|
+
/**
|
|
6
|
+
* Resolve the REST API request URL by:
|
|
7
|
+
* 1. Loading the REST API endpoint from the Amplify configuration with corresponding API name.
|
|
8
|
+
* 2. Appending the path to the endpoint.
|
|
9
|
+
* 3. Merge the query parameters from path and the queryParameter argument which is taken from the public REST API
|
|
10
|
+
* options.
|
|
11
|
+
* 4. Validating the resulting URL string.
|
|
12
|
+
*
|
|
13
|
+
* @internal
|
|
14
|
+
*/
|
|
15
|
+
export var resolveApiUrl = function (amplify, apiName, path, queryParams) {
|
|
16
|
+
var _a, _b, _c, _d;
|
|
17
|
+
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
|
+
assertValidationError(!!urlStr, RestApiValidationErrorCode.InvalidApiName);
|
|
19
|
+
try {
|
|
20
|
+
var url = new URL(urlStr + path);
|
|
21
|
+
if (queryParams) {
|
|
22
|
+
var mergedQueryParams_1 = new URLSearchParams(url.searchParams);
|
|
23
|
+
Object.entries(queryParams).forEach(function (_a) {
|
|
24
|
+
var _b = __read(_a, 2), key = _b[0], value = _b[1];
|
|
25
|
+
mergedQueryParams_1.set(key, value);
|
|
26
|
+
});
|
|
27
|
+
url.search = new URLSearchParams(mergedQueryParams_1).toString();
|
|
28
|
+
}
|
|
29
|
+
return url;
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
throw new RestApiError(__assign(__assign({ name: RestApiValidationErrorCode.InvalidApiName }, validationErrorMap[RestApiValidationErrorCode.InvalidApiName]), { recoverySuggestion: "Please make sure the REST endpoint URL is a valid URL string. Got ".concat(urlStr) }));
|
|
33
|
+
}
|
|
34
|
+
};
|
|
@@ -5,6 +5,6 @@ import { AmplifyClassV6 } from '@aws-amplify/core';
|
|
|
5
5
|
export declare const resolveCredentials: (amplify: AmplifyClassV6) => Promise<{
|
|
6
6
|
accessKeyId: string;
|
|
7
7
|
secretAccessKey: string;
|
|
8
|
-
sessionToken?: string;
|
|
9
|
-
expiration?: Date;
|
|
8
|
+
sessionToken?: string | undefined;
|
|
9
|
+
expiration?: Date | undefined;
|
|
10
10
|
}>;
|
|
@@ -12,7 +12,7 @@ export var resolveCredentials = function (amplify) { return __awaiter(void 0, vo
|
|
|
12
12
|
case 0: return [4 /*yield*/, amplify.Auth.fetchAuthSession()];
|
|
13
13
|
case 1:
|
|
14
14
|
credentials = (_a.sent()).credentials;
|
|
15
|
-
assertValidationError(!!credentials, RestApiValidationErrorCode.NoCredentials);
|
|
15
|
+
assertValidationError(!!credentials && !!credentials.accessKeyId && !!credentials.secretAccessKey, RestApiValidationErrorCode.NoCredentials);
|
|
16
16
|
return [2 /*return*/, credentials];
|
|
17
17
|
}
|
|
18
18
|
});
|
|
@@ -7,4 +7,4 @@ import { RestApiError } from '../errors';
|
|
|
7
7
|
* @internal
|
|
8
8
|
*/
|
|
9
9
|
export declare const buildRestApiServiceError: (error: Error) => RestApiError;
|
|
10
|
-
export declare const parseRestApiServiceError: (response?: HttpResponse) => Promise<RestApiError & MetadataBearer>;
|
|
10
|
+
export declare const parseRestApiServiceError: (response?: HttpResponse) => Promise<(RestApiError & MetadataBearer) | undefined>;
|
|
@@ -8,7 +8,7 @@ import { RestApiError } from '../errors';
|
|
|
8
8
|
*/
|
|
9
9
|
export var buildRestApiServiceError = function (error) {
|
|
10
10
|
var restApiError = new RestApiError({
|
|
11
|
-
name: error.name,
|
|
11
|
+
name: error === null || error === void 0 ? void 0 : error.name,
|
|
12
12
|
message: error.message,
|
|
13
13
|
underlyingError: error,
|
|
14
14
|
});
|
|
@@ -21,11 +21,13 @@ export var parseRestApiServiceError = function (response) { return __awaiter(voi
|
|
|
21
21
|
case 0: return [4 /*yield*/, parseJsonError(response)];
|
|
22
22
|
case 1:
|
|
23
23
|
parsedError = _a.sent();
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
24
|
+
if (!parsedError) {
|
|
25
|
+
// Response is not an error.
|
|
26
|
+
return [2 /*return*/];
|
|
27
|
+
}
|
|
28
|
+
return [2 /*return*/, Object.assign(buildRestApiServiceError(parsedError), {
|
|
29
|
+
$metadata: parsedError.$metadata,
|
|
30
|
+
})];
|
|
29
31
|
}
|
|
30
32
|
});
|
|
31
33
|
}); };
|
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.9607e4a.0+9607e4a",
|
|
5
5
|
"description": "Api-rest category of aws-amplify",
|
|
6
6
|
"main": "./lib/index.js",
|
|
7
7
|
"module": "./lib-esm/index.js",
|
|
@@ -47,15 +47,14 @@
|
|
|
47
47
|
"src"
|
|
48
48
|
],
|
|
49
49
|
"dependencies": {
|
|
50
|
-
"
|
|
51
|
-
"tslib": "^2.5.0",
|
|
52
|
-
"url": "0.11.0"
|
|
50
|
+
"tslib": "^2.5.0"
|
|
53
51
|
},
|
|
54
52
|
"peerDependencies": {
|
|
55
|
-
"@aws-amplify/core": "6.0.1-console-preview.
|
|
53
|
+
"@aws-amplify/core": "6.0.1-console-preview.9607e4a.0+9607e4a"
|
|
56
54
|
},
|
|
57
55
|
"devDependencies": {
|
|
58
|
-
"@aws-amplify/core": "6.0.1-console-preview.
|
|
56
|
+
"@aws-amplify/core": "6.0.1-console-preview.9607e4a.0+9607e4a",
|
|
57
|
+
"@aws-amplify/react-native": "1.0.1-console-preview.9607e4a.0+9607e4a",
|
|
59
58
|
"typescript": "5.0.2"
|
|
60
59
|
},
|
|
61
60
|
"size-limit": [
|
|
@@ -110,5 +109,5 @@
|
|
|
110
109
|
"<rootDir>/setupTests.ts"
|
|
111
110
|
]
|
|
112
111
|
},
|
|
113
|
-
"gitHead": "
|
|
112
|
+
"gitHead": "9607e4a6e188ebac2e8992f289a64fc417132e05"
|
|
114
113
|
}
|
|
@@ -12,12 +12,12 @@ import {
|
|
|
12
12
|
import { DocumentType } from '@aws-amplify/core/internals/utils';
|
|
13
13
|
|
|
14
14
|
import {
|
|
15
|
-
createCancellableOperation,
|
|
16
15
|
parseRestApiServiceError,
|
|
17
|
-
|
|
16
|
+
parseSigningInfo,
|
|
18
17
|
resolveCredentials,
|
|
19
|
-
} from '
|
|
20
|
-
import { normalizeHeaders } from '
|
|
18
|
+
} from '../../utils';
|
|
19
|
+
import { normalizeHeaders } from '../../utils/normalizeHeaders';
|
|
20
|
+
import { RestApiResponse } from '../../types';
|
|
21
21
|
|
|
22
22
|
type HandlerOptions = Omit<HttpRequest, 'body' | 'headers'> & {
|
|
23
23
|
body?: DocumentType | FormData;
|
|
@@ -35,9 +35,8 @@ type SigningServiceInfo = {
|
|
|
35
35
|
* @param amplify Amplify instance to to resolve credentials and tokens. Should use different instance in client-side
|
|
36
36
|
* and SSR
|
|
37
37
|
* @param options Options accepted from public API options when calling the handlers.
|
|
38
|
-
* @param signingServiceInfo Internal-only options
|
|
39
|
-
*
|
|
40
|
-
* used.
|
|
38
|
+
* @param signingServiceInfo Internal-only options enable IAM auth as well as to to overwrite the IAM signing service
|
|
39
|
+
* and region. If specified, and NONE of API Key header or Auth header is present, IAM auth will be used.
|
|
41
40
|
*
|
|
42
41
|
* @internal
|
|
43
42
|
*/
|
|
@@ -45,7 +44,7 @@ export const transferHandler = async (
|
|
|
45
44
|
amplify: AmplifyClassV6,
|
|
46
45
|
options: HandlerOptions & { abortSignal: AbortSignal },
|
|
47
46
|
signingServiceInfo?: SigningServiceInfo
|
|
48
|
-
) => {
|
|
47
|
+
): Promise<RestApiResponse> => {
|
|
49
48
|
const { url, method, headers, body, withCredentials, abortSignal } = options;
|
|
50
49
|
const resolvedBody = body
|
|
51
50
|
? body instanceof FormData
|
|
@@ -78,7 +77,7 @@ export const transferHandler = async (
|
|
|
78
77
|
|
|
79
78
|
const isIamAuthApplicable = iamAuthApplicable(request, signingServiceInfo);
|
|
80
79
|
if (isIamAuthApplicable) {
|
|
81
|
-
const signingInfoFromUrl =
|
|
80
|
+
const signingInfoFromUrl = parseSigningInfo(url);
|
|
82
81
|
const signingService =
|
|
83
82
|
signingServiceInfo?.service ?? signingInfoFromUrl.service;
|
|
84
83
|
const signingRegion =
|
|
@@ -3,10 +3,21 @@
|
|
|
3
3
|
|
|
4
4
|
import { AmplifyClassV6 } from '@aws-amplify/core';
|
|
5
5
|
|
|
6
|
-
import { InternalPostInput, RestApiResponse } from '
|
|
6
|
+
import { InternalPostInput, RestApiResponse } from '../../types';
|
|
7
7
|
import { transferHandler } from './handler';
|
|
8
|
-
import { createCancellableOperation } from '
|
|
8
|
+
import { createCancellableOperation } from '../../utils';
|
|
9
9
|
|
|
10
|
+
/**
|
|
11
|
+
* This weak map provides functionality to cancel a request given the promise containing the `post` request.
|
|
12
|
+
*
|
|
13
|
+
* 1. For every GraphQL POST request, an abort controller is created and supplied to the request.
|
|
14
|
+
* 2. The promise fulfilled by GraphGL POST request is then mapped to that abort controller.
|
|
15
|
+
* 3. The promise is returned to the external caller.
|
|
16
|
+
* 4. The caller can either wait for the promise to fulfill or call `cancel(promise)` to cancel the request.
|
|
17
|
+
* 5. If `cancel(promise)` is called, then the corresponding abort controller is retrieved from the map below.
|
|
18
|
+
* 6. GraphQL POST request will be rejected with the error message provided during cancel.
|
|
19
|
+
* 7. Caller can check if the error is because of cancelling by calling `isCancelError(error)`.
|
|
20
|
+
*/
|
|
10
21
|
const cancelTokenMap = new WeakMap<Promise<any>, AbortController>();
|
|
11
22
|
|
|
12
23
|
/**
|
|
@@ -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,60 @@
|
|
|
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
|
+
|
|
28
|
+
/**
|
|
29
|
+
* GET HTTP request
|
|
30
|
+
*/
|
|
31
|
+
export const get = (input: GetInput): GetOperation => commonGet(Amplify, input);
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* POST HTTP request
|
|
35
|
+
*/
|
|
36
|
+
export const post = (input: PostInput): PostOperation =>
|
|
37
|
+
commonPost(Amplify, input);
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* PUT HTTP request
|
|
41
|
+
*/
|
|
42
|
+
export const put = (input: PutInput): PutOperation => commonPut(Amplify, input);
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* DELETE HTTP request
|
|
46
|
+
*/
|
|
47
|
+
export const del = (input: DeleteInput): DeleteOperation =>
|
|
48
|
+
commonDel(Amplify, input);
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* HEAD HTTP request
|
|
52
|
+
*/
|
|
53
|
+
export const head = (input: HeadInput): HeadOperation =>
|
|
54
|
+
commonHead(Amplify, input);
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* PATCH HTTP request
|
|
58
|
+
*/
|
|
59
|
+
export const patch = (input: PatchInput): PatchOperation =>
|
|
60
|
+
commonPatch(Amplify, input);
|