@aws-amplify/api-rest 3.5.6-api-v6-models.b3abc9b.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,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.parseSigningInfo = void 0;
|
|
6
|
+
const constants_1 = require("./constants");
|
|
7
|
+
/**
|
|
8
|
+
* Infer the signing service and region from the given URL, and for REST API only, from the Amplify configuration.
|
|
9
|
+
* It supports raw API Gateway endpoint and AppSync endpoint.
|
|
10
|
+
*
|
|
11
|
+
* @internal
|
|
12
|
+
*/
|
|
13
|
+
const parseSigningInfo = (url, restApiOptions) => {
|
|
14
|
+
const { service: signingService = constants_1.DEFAULT_REST_IAM_SIGNING_SERVICE, region: signingRegion = constants_1.DEFAULT_IAM_SIGNING_REGION, } = restApiOptions?.amplify.getConfig()?.API?.REST?.[restApiOptions?.apiName] ??
|
|
15
|
+
{};
|
|
16
|
+
const { hostname } = url;
|
|
17
|
+
const [, service, region] = constants_1.APIG_HOSTNAME_PATTERN.exec(hostname) ?? [];
|
|
18
|
+
if (service === constants_1.DEFAULT_REST_IAM_SIGNING_SERVICE) {
|
|
19
|
+
// The configured endpoint is an API Gateway endpoint
|
|
20
|
+
// @see: https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-call-api.html
|
|
21
|
+
return {
|
|
22
|
+
service,
|
|
23
|
+
region: region ?? signingRegion,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
else if (service === 'appsync-api') {
|
|
27
|
+
// AppSync endpoint is internally supported because GraphQL operation will send request using POST handler.
|
|
28
|
+
// example: https://xxxx.appsync-api.us-east-1.amazonaws.com/graphql
|
|
29
|
+
return {
|
|
30
|
+
service: 'appsync',
|
|
31
|
+
region: region ?? signingRegion,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
return {
|
|
36
|
+
service: signingService,
|
|
37
|
+
region: signingRegion,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
exports.parseSigningInfo = parseSigningInfo;
|
|
@@ -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,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.resolveApiUrl = void 0;
|
|
6
|
+
const utils_1 = require("@aws-amplify/core/internals/utils");
|
|
7
|
+
const errors_1 = require("../errors");
|
|
8
|
+
/**
|
|
9
|
+
* Resolve the REST API request URL by:
|
|
10
|
+
* 1. Loading the REST API endpoint from the Amplify configuration with corresponding API name.
|
|
11
|
+
* 2. Appending the path to the endpoint.
|
|
12
|
+
* 3. Merge the query parameters from path and the queryParameter argument which is taken from the public REST API
|
|
13
|
+
* options.
|
|
14
|
+
* 4. Validating the resulting URL string.
|
|
15
|
+
*
|
|
16
|
+
* @internal
|
|
17
|
+
*/
|
|
18
|
+
const resolveApiUrl = (amplify, apiName, path, queryParams) => {
|
|
19
|
+
const urlStr = amplify.getConfig()?.API?.REST?.[apiName]?.endpoint;
|
|
20
|
+
(0, errors_1.assertValidationError)(!!urlStr, errors_1.RestApiValidationErrorCode.InvalidApiName);
|
|
21
|
+
try {
|
|
22
|
+
const url = new utils_1.AmplifyUrl(urlStr + path);
|
|
23
|
+
if (queryParams) {
|
|
24
|
+
const mergedQueryParams = new utils_1.AmplifyUrlSearchParams(url.searchParams);
|
|
25
|
+
Object.entries(queryParams).forEach(([key, value]) => {
|
|
26
|
+
mergedQueryParams.set(key, value);
|
|
27
|
+
});
|
|
28
|
+
url.search = new utils_1.AmplifyUrlSearchParams(mergedQueryParams).toString();
|
|
29
|
+
}
|
|
30
|
+
return url;
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
throw new errors_1.RestApiError({
|
|
34
|
+
name: errors_1.RestApiValidationErrorCode.InvalidApiName,
|
|
35
|
+
...errors_1.validationErrorMap[errors_1.RestApiValidationErrorCode.InvalidApiName],
|
|
36
|
+
recoverySuggestion: `Please make sure the REST endpoint URL is a valid URL string. Got ${urlStr}`,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
exports.resolveApiUrl = resolveApiUrl;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.resolveCredentials = void 0;
|
|
6
|
+
const errors_1 = require("../errors");
|
|
7
|
+
/**
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
const resolveCredentials = async (amplify) => {
|
|
11
|
+
const { credentials } = await amplify.Auth.fetchAuthSession();
|
|
12
|
+
(0, errors_1.assertValidationError)(!!credentials && !!credentials.accessKeyId && !!credentials.secretAccessKey, errors_1.RestApiValidationErrorCode.NoCredentials);
|
|
13
|
+
return credentials;
|
|
14
|
+
};
|
|
15
|
+
exports.resolveCredentials = resolveCredentials;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { MetadataBearer } from '@aws-sdk/types';
|
|
2
|
+
import { HttpResponse } from '@aws-amplify/core/internals/aws-client-utils';
|
|
3
|
+
import { RestApiError } from '../errors';
|
|
4
|
+
/**
|
|
5
|
+
* Internal-only method to create a new RestApiError from a service error.
|
|
6
|
+
*
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
export declare const buildRestApiServiceError: (error: Error) => RestApiError;
|
|
10
|
+
export declare const parseRestApiServiceError: (response?: HttpResponse) => Promise<(RestApiError & MetadataBearer) | undefined>;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseRestApiServiceError = exports.buildRestApiServiceError = void 0;
|
|
4
|
+
const aws_client_utils_1 = require("@aws-amplify/core/internals/aws-client-utils");
|
|
5
|
+
const errors_1 = require("../errors");
|
|
6
|
+
/**
|
|
7
|
+
* Internal-only method to create a new RestApiError from a service error.
|
|
8
|
+
*
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
const buildRestApiServiceError = (error) => {
|
|
12
|
+
const restApiError = new errors_1.RestApiError({
|
|
13
|
+
name: error?.name,
|
|
14
|
+
message: error.message,
|
|
15
|
+
underlyingError: error,
|
|
16
|
+
});
|
|
17
|
+
return restApiError;
|
|
18
|
+
};
|
|
19
|
+
exports.buildRestApiServiceError = buildRestApiServiceError;
|
|
20
|
+
const parseRestApiServiceError = async (response) => {
|
|
21
|
+
const parsedError = await (0, aws_client_utils_1.parseJsonError)(response);
|
|
22
|
+
if (!parsedError) {
|
|
23
|
+
// Response is not an error.
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
return Object.assign((0, exports.buildRestApiServiceError)(parsedError), {
|
|
27
|
+
$metadata: parsedError.$metadata,
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
exports.parseRestApiServiceError = parseRestApiServiceError;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { AmplifyClassV6 } from '@aws-amplify/core';
|
|
2
|
+
import { HttpRequest, Headers } from '@aws-amplify/core/internals/aws-client-utils';
|
|
3
|
+
import { DocumentType } from '@aws-amplify/core/internals/utils';
|
|
4
|
+
import { RestApiResponse } from '../../types';
|
|
5
|
+
type HandlerOptions = Omit<HttpRequest, 'body' | 'headers'> & {
|
|
6
|
+
body?: DocumentType | FormData;
|
|
7
|
+
headers?: Headers;
|
|
8
|
+
withCredentials?: boolean;
|
|
9
|
+
};
|
|
10
|
+
type SigningServiceInfo = {
|
|
11
|
+
service?: string;
|
|
12
|
+
region?: string;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Make REST API call with best-effort IAM auth.
|
|
16
|
+
* @param amplify Amplify instance to to resolve credentials and tokens. Should use different instance in client-side
|
|
17
|
+
* and SSR
|
|
18
|
+
* @param options Options accepted from public API options when calling the handlers.
|
|
19
|
+
* @param signingServiceInfo Internal-only options enable IAM auth as well as to to overwrite the IAM signing service
|
|
20
|
+
* and region. If specified, and NONE of API Key header or Auth header is present, IAM auth will be used.
|
|
21
|
+
*
|
|
22
|
+
* @internal
|
|
23
|
+
*/
|
|
24
|
+
export declare const transferHandler: (amplify: AmplifyClassV6, options: HandlerOptions & {
|
|
25
|
+
abortSignal: AbortSignal;
|
|
26
|
+
}, signingServiceInfo?: SigningServiceInfo) => Promise<RestApiResponse>;
|
|
27
|
+
export {};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { unauthenticatedHandler, getRetryDecider, jitteredBackoff, authenticatedHandler, } from '@aws-amplify/core/internals/aws-client-utils';
|
|
2
|
+
import { parseRestApiServiceError, parseSigningInfo, resolveCredentials, } from '../../utils';
|
|
3
|
+
import { normalizeHeaders } from '../../utils/normalizeHeaders';
|
|
4
|
+
/**
|
|
5
|
+
* Make REST API call with best-effort IAM auth.
|
|
6
|
+
* @param amplify Amplify instance to to resolve credentials and tokens. Should use different instance in client-side
|
|
7
|
+
* and SSR
|
|
8
|
+
* @param options Options accepted from public API options when calling the handlers.
|
|
9
|
+
* @param signingServiceInfo Internal-only options enable IAM auth as well as to to overwrite the IAM signing service
|
|
10
|
+
* and region. If specified, and NONE of API Key header or Auth header is present, IAM auth will be used.
|
|
11
|
+
*
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
14
|
+
export const transferHandler = async (amplify, options, signingServiceInfo) => {
|
|
15
|
+
const { url, method, headers, body, withCredentials, abortSignal } = options;
|
|
16
|
+
const resolvedBody = body
|
|
17
|
+
? body instanceof FormData
|
|
18
|
+
? body
|
|
19
|
+
: JSON.stringify(body ?? '')
|
|
20
|
+
: undefined;
|
|
21
|
+
const resolvedHeaders = {
|
|
22
|
+
...normalizeHeaders(headers),
|
|
23
|
+
...(resolvedBody
|
|
24
|
+
? {
|
|
25
|
+
'content-type': body instanceof FormData
|
|
26
|
+
? 'multipart/form-data'
|
|
27
|
+
: 'application/json; charset=UTF-8',
|
|
28
|
+
}
|
|
29
|
+
: {}),
|
|
30
|
+
};
|
|
31
|
+
const request = {
|
|
32
|
+
url,
|
|
33
|
+
headers: resolvedHeaders,
|
|
34
|
+
method,
|
|
35
|
+
body: resolvedBody,
|
|
36
|
+
};
|
|
37
|
+
const baseOptions = {
|
|
38
|
+
retryDecider: getRetryDecider(parseRestApiServiceError),
|
|
39
|
+
computeDelay: jitteredBackoff,
|
|
40
|
+
withCrossDomainCredentials: withCredentials,
|
|
41
|
+
abortSignal,
|
|
42
|
+
};
|
|
43
|
+
const isIamAuthApplicable = iamAuthApplicable(request, signingServiceInfo);
|
|
44
|
+
let response;
|
|
45
|
+
if (isIamAuthApplicable) {
|
|
46
|
+
const signingInfoFromUrl = parseSigningInfo(url);
|
|
47
|
+
const signingService = signingServiceInfo?.service ?? signingInfoFromUrl.service;
|
|
48
|
+
const signingRegion = signingServiceInfo?.region ?? signingInfoFromUrl.region;
|
|
49
|
+
const credentials = await resolveCredentials(amplify);
|
|
50
|
+
response = await authenticatedHandler(request, {
|
|
51
|
+
...baseOptions,
|
|
52
|
+
credentials,
|
|
53
|
+
region: signingRegion,
|
|
54
|
+
service: signingService,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
response = await unauthenticatedHandler(request, {
|
|
59
|
+
...baseOptions,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
// Clean-up un-modeled properties from response.
|
|
63
|
+
return {
|
|
64
|
+
statusCode: response.statusCode,
|
|
65
|
+
headers: response.headers,
|
|
66
|
+
body: response.body,
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
const iamAuthApplicable = ({ headers }, signingServiceInfo) => !headers.authorization && !headers['x-api-key'] && !!signingServiceInfo;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { AmplifyClassV6 } from '@aws-amplify/core';
|
|
2
|
+
import { InternalPostInput, RestApiResponse } from '../../types';
|
|
3
|
+
/**
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
export declare const post: (amplify: AmplifyClassV6, { url, options, abortController }: InternalPostInput) => Promise<RestApiResponse>;
|
|
7
|
+
/**
|
|
8
|
+
* Cancels a request given the promise returned by `post`.
|
|
9
|
+
* If the request is already completed, this function does nothing.
|
|
10
|
+
* It MUST be used after `updateRequestToBeCancellable` is called.
|
|
11
|
+
*/
|
|
12
|
+
export declare const cancel: (promise: Promise<RestApiResponse>, message?: string) => boolean;
|
|
13
|
+
/**
|
|
14
|
+
* MUST be used to make a promise including internal `post` API call cancellable.
|
|
15
|
+
*/
|
|
16
|
+
export declare const updateRequestToBeCancellable: (promise: Promise<any>, controller: AbortController) => void;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { transferHandler } from './handler';
|
|
4
|
+
import { createCancellableOperation } from '../../utils';
|
|
5
|
+
/**
|
|
6
|
+
* This weak map provides functionality to cancel a request given the promise containing the `post` request.
|
|
7
|
+
*
|
|
8
|
+
* 1. For every GraphQL POST request, an abort controller is created and supplied to the request.
|
|
9
|
+
* 2. The promise fulfilled by GraphGL POST request is then mapped to that abort controller.
|
|
10
|
+
* 3. The promise is returned to the external caller.
|
|
11
|
+
* 4. The caller can either wait for the promise to fulfill or call `cancel(promise)` to cancel the request.
|
|
12
|
+
* 5. If `cancel(promise)` is called, then the corresponding abort controller is retrieved from the map below.
|
|
13
|
+
* 6. GraphQL POST request will be rejected with the error message provided during cancel.
|
|
14
|
+
* 7. Caller can check if the error is because of cancelling by calling `isCancelError(error)`.
|
|
15
|
+
*/
|
|
16
|
+
const cancelTokenMap = new WeakMap();
|
|
17
|
+
/**
|
|
18
|
+
* @internal
|
|
19
|
+
*/
|
|
20
|
+
export const post = (amplify, { url, options, abortController }) => {
|
|
21
|
+
const controller = abortController ?? new AbortController();
|
|
22
|
+
const responsePromise = createCancellableOperation(async () => {
|
|
23
|
+
const response = transferHandler(amplify, {
|
|
24
|
+
url,
|
|
25
|
+
method: 'POST',
|
|
26
|
+
...options,
|
|
27
|
+
abortSignal: controller.signal,
|
|
28
|
+
}, options?.signingServiceInfo);
|
|
29
|
+
return response;
|
|
30
|
+
}, controller);
|
|
31
|
+
const responseWithCleanUp = responsePromise.finally(() => {
|
|
32
|
+
cancelTokenMap.delete(responseWithCleanUp);
|
|
33
|
+
});
|
|
34
|
+
return responseWithCleanUp;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Cancels a request given the promise returned by `post`.
|
|
38
|
+
* If the request is already completed, this function does nothing.
|
|
39
|
+
* It MUST be used after `updateRequestToBeCancellable` is called.
|
|
40
|
+
*/
|
|
41
|
+
export const cancel = (promise, message) => {
|
|
42
|
+
const controller = cancelTokenMap.get(promise);
|
|
43
|
+
if (controller) {
|
|
44
|
+
controller.abort(message);
|
|
45
|
+
if (message && controller.signal.reason !== message) {
|
|
46
|
+
// In runtimes where `AbortSignal.reason` is not supported, we track the reason ourselves.
|
|
47
|
+
// @ts-expect-error reason is read-only property.
|
|
48
|
+
controller.signal['reason'] = message;
|
|
49
|
+
}
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
return false;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* MUST be used to make a promise including internal `post` API call cancellable.
|
|
56
|
+
*/
|
|
57
|
+
export const updateRequestToBeCancellable = (promise, controller) => {
|
|
58
|
+
cancelTokenMap.set(promise, controller);
|
|
59
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { AmplifyClassV6 } from '@aws-amplify/core';
|
|
2
|
+
import { GetInput, GetOperation, PostInput, PostOperation, PutInput, PutOperation, DeleteInput, DeleteOperation, HeadInput, HeadOperation, PatchInput, PatchOperation } from '../../types';
|
|
3
|
+
export declare const get: (amplify: AmplifyClassV6, input: GetInput) => GetOperation;
|
|
4
|
+
export declare const post: (amplify: AmplifyClassV6, input: PostInput) => PostOperation;
|
|
5
|
+
export declare const put: (amplify: AmplifyClassV6, input: PutInput) => PutOperation;
|
|
6
|
+
export declare const del: (amplify: AmplifyClassV6, input: DeleteInput) => DeleteOperation;
|
|
7
|
+
export declare const head: (amplify: AmplifyClassV6, input: HeadInput) => HeadOperation;
|
|
8
|
+
export declare const patch: (amplify: AmplifyClassV6, input: PatchInput) => PatchOperation;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { resolveApiUrl, createCancellableOperation, logger, parseSigningInfo, } from '../../utils';
|
|
4
|
+
import { transferHandler } from './handler';
|
|
5
|
+
const publicHandler = (amplify, options, method) => createCancellableOperation(async (abortSignal) => {
|
|
6
|
+
const { apiName, options: apiOptions = {}, path: apiPath } = options;
|
|
7
|
+
const url = resolveApiUrl(amplify, apiName, apiPath, apiOptions?.queryParams);
|
|
8
|
+
const libraryOptionsHeaders = await amplify.libraryOptions?.API?.REST?.headers?.({
|
|
9
|
+
apiName,
|
|
10
|
+
});
|
|
11
|
+
const { headers: invocationHeaders = {} } = apiOptions;
|
|
12
|
+
const headers = {
|
|
13
|
+
// custom headers from invocation options should precede library options
|
|
14
|
+
...libraryOptionsHeaders,
|
|
15
|
+
...invocationHeaders,
|
|
16
|
+
};
|
|
17
|
+
const signingServiceInfo = parseSigningInfo(url, {
|
|
18
|
+
amplify,
|
|
19
|
+
apiName,
|
|
20
|
+
});
|
|
21
|
+
logger.debug(method, url, headers, `IAM signing options: ${JSON.stringify(signingServiceInfo)}`);
|
|
22
|
+
return transferHandler(amplify, {
|
|
23
|
+
...apiOptions,
|
|
24
|
+
url,
|
|
25
|
+
method,
|
|
26
|
+
headers,
|
|
27
|
+
abortSignal,
|
|
28
|
+
}, signingServiceInfo);
|
|
29
|
+
});
|
|
30
|
+
export const get = (amplify, input) => publicHandler(amplify, input, 'GET');
|
|
31
|
+
export const post = (amplify, input) => publicHandler(amplify, input, 'POST');
|
|
32
|
+
export const put = (amplify, input) => publicHandler(amplify, input, 'PUT');
|
|
33
|
+
export const del = (amplify, input) => publicHandler(amplify, input, 'DELETE');
|
|
34
|
+
export const head = (amplify, input) => publicHandler(amplify, input, 'HEAD');
|
|
35
|
+
export const patch = (amplify, input) => publicHandler(amplify, input, 'PATCH');
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { DeleteInput, DeleteOperation, GetInput, GetOperation, HeadInput, HeadOperation, PatchInput, PatchOperation, PostInput, PostOperation, PutInput, PutOperation } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* GET HTTP request
|
|
4
|
+
* @param {GetInput} input - Input for GET operation
|
|
5
|
+
* @returns {GetOperation} Operation for GET request
|
|
6
|
+
* @throws - {@link RestApiError}
|
|
7
|
+
* @example
|
|
8
|
+
* Send a GET request
|
|
9
|
+
* ```js
|
|
10
|
+
* import { get, isCancelError } from '@aws-amplify/api';
|
|
11
|
+
*
|
|
12
|
+
* const { body } = await get({
|
|
13
|
+
* apiName,
|
|
14
|
+
* path,
|
|
15
|
+
* options: {
|
|
16
|
+
* headers, // Optional, A map of custom header key/values
|
|
17
|
+
* body, // Optional, JSON object or FormData
|
|
18
|
+
* queryParams, // Optional, A map of query strings
|
|
19
|
+
* }
|
|
20
|
+
* }).response;
|
|
21
|
+
* const data = await body.json();
|
|
22
|
+
* ```
|
|
23
|
+
* @example
|
|
24
|
+
* Cancel a GET request
|
|
25
|
+
*
|
|
26
|
+
* ```js
|
|
27
|
+
* import { get, isCancelError } from '@aws-amplify/api';
|
|
28
|
+
*
|
|
29
|
+
* const { response, cancel } = get({apiName, path, options});
|
|
30
|
+
* cancel(message);
|
|
31
|
+
* try {
|
|
32
|
+
* await response;
|
|
33
|
+
* } cache (e) {
|
|
34
|
+
* if (isCancelError(e)) {
|
|
35
|
+
* // handle request cancellation
|
|
36
|
+
* }
|
|
37
|
+
* //...
|
|
38
|
+
* }
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export declare const get: (input: GetInput) => GetOperation;
|
|
42
|
+
/**
|
|
43
|
+
* POST HTTP request
|
|
44
|
+
* @param {PostInput} input - Input for POST operation
|
|
45
|
+
* @returns {PostOperation} Operation for POST request
|
|
46
|
+
* @throws - {@link RestApiError}
|
|
47
|
+
* @example
|
|
48
|
+
* Send a POST request
|
|
49
|
+
* ```js
|
|
50
|
+
* import { post, isCancelError } from '@aws-amplify/api';
|
|
51
|
+
*
|
|
52
|
+
* const { body } = await post({
|
|
53
|
+
* apiName,
|
|
54
|
+
* path,
|
|
55
|
+
* options: {
|
|
56
|
+
* headers, // Optional, A map of custom header key/values
|
|
57
|
+
* body, // Optional, JSON object or FormData
|
|
58
|
+
* queryParams, // Optional, A map of query strings
|
|
59
|
+
* }
|
|
60
|
+
* }).response;
|
|
61
|
+
* const data = await body.json();
|
|
62
|
+
* ```
|
|
63
|
+
* @example
|
|
64
|
+
* Cancel a POST request
|
|
65
|
+
*
|
|
66
|
+
* ```js
|
|
67
|
+
* import { post, isCancelError } from '@aws-amplify/api';
|
|
68
|
+
*
|
|
69
|
+
* const { response, cancel } = post({apiName, path, options});
|
|
70
|
+
* cancel(message);
|
|
71
|
+
* try {
|
|
72
|
+
* await response;
|
|
73
|
+
* } cache (e) {
|
|
74
|
+
* if (isCancelError(e)) {
|
|
75
|
+
* // handle request cancellation
|
|
76
|
+
* }
|
|
77
|
+
* //...
|
|
78
|
+
* }
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
export declare const post: (input: PostInput) => PostOperation;
|
|
82
|
+
/**
|
|
83
|
+
* PUT HTTP request
|
|
84
|
+
* @param {PutInput} input - Input for PUT operation
|
|
85
|
+
* @returns {PutOperation} Operation for PUT request
|
|
86
|
+
* @throws - {@link RestApiError}
|
|
87
|
+
* @example
|
|
88
|
+
* Send a PUT request
|
|
89
|
+
* ```js
|
|
90
|
+
* import { put, isCancelError } from '@aws-amplify/api';
|
|
91
|
+
*
|
|
92
|
+
* const { body } = await put({
|
|
93
|
+
* apiName,
|
|
94
|
+
* path,
|
|
95
|
+
* options: {
|
|
96
|
+
* headers, // Optional, A map of custom header key/values
|
|
97
|
+
* body, // Optional, JSON object or FormData
|
|
98
|
+
* queryParams, // Optional, A map of query strings
|
|
99
|
+
* }
|
|
100
|
+
* }).response;
|
|
101
|
+
* const data = await body.json();
|
|
102
|
+
* ```
|
|
103
|
+
* @example
|
|
104
|
+
* Cancel a PUT request
|
|
105
|
+
* ```js
|
|
106
|
+
* import { put, isCancelError } from '@aws-amplify/api';
|
|
107
|
+
*
|
|
108
|
+
* const { response, cancel } = put({apiName, path, options});
|
|
109
|
+
* cancel(message);
|
|
110
|
+
* try {
|
|
111
|
+
* await response;
|
|
112
|
+
* } cache (e) {
|
|
113
|
+
* if (isCancelError(e)) {
|
|
114
|
+
* // handle request cancellation
|
|
115
|
+
* }
|
|
116
|
+
* //...
|
|
117
|
+
* }
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
export declare const put: (input: PutInput) => PutOperation;
|
|
121
|
+
/**
|
|
122
|
+
* DELETE HTTP request
|
|
123
|
+
* @param {DeleteInput} input - Input for DELETE operation
|
|
124
|
+
* @returns {DeleteOperation} Operation for DELETE request
|
|
125
|
+
* @throws - {@link RestApiError}
|
|
126
|
+
* @example
|
|
127
|
+
* Send a DELETE request
|
|
128
|
+
* ```js
|
|
129
|
+
* import { del } from '@aws-amplify/api';
|
|
130
|
+
*
|
|
131
|
+
* const { statusCode } = await del({
|
|
132
|
+
* apiName,
|
|
133
|
+
* path,
|
|
134
|
+
* options: {
|
|
135
|
+
* headers, // Optional, A map of custom header key/values
|
|
136
|
+
* queryParams, // Optional, A map of query strings
|
|
137
|
+
* }
|
|
138
|
+
* }).response;
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
export declare const del: (input: DeleteInput) => DeleteOperation;
|
|
142
|
+
/**
|
|
143
|
+
* HEAD HTTP request
|
|
144
|
+
* @param {HeadInput} input - Input for HEAD operation
|
|
145
|
+
* @returns {HeadOperation} Operation for HEAD request
|
|
146
|
+
* @throws - {@link RestApiError}
|
|
147
|
+
* @example
|
|
148
|
+
* Send a HEAD request
|
|
149
|
+
* ```js
|
|
150
|
+
* import { head, isCancelError } from '@aws-amplify/api';
|
|
151
|
+
*
|
|
152
|
+
* const { headers, statusCode } = await head({
|
|
153
|
+
* apiName,
|
|
154
|
+
* path,
|
|
155
|
+
* options: {
|
|
156
|
+
* headers, // Optional, A map of custom header key/values
|
|
157
|
+
* queryParams, // Optional, A map of query strings
|
|
158
|
+
* }
|
|
159
|
+
* }),response;
|
|
160
|
+
* ```
|
|
161
|
+
*
|
|
162
|
+
*/
|
|
163
|
+
export declare const head: (input: HeadInput) => HeadOperation;
|
|
164
|
+
/**
|
|
165
|
+
* PATCH HTTP request
|
|
166
|
+
* @param {PatchInput} input - Input for PATCH operation
|
|
167
|
+
* @returns {PatchOperation} Operation for PATCH request
|
|
168
|
+
* @throws - {@link RestApiError}
|
|
169
|
+
* @example
|
|
170
|
+
* Send a PATCH request
|
|
171
|
+
* ```js
|
|
172
|
+
* import { patch } from '@aws-amplify/api';
|
|
173
|
+
*
|
|
174
|
+
* const { body } = await patch({
|
|
175
|
+
* apiName,
|
|
176
|
+
* path,
|
|
177
|
+
* options: {
|
|
178
|
+
* headers, // Optional, A map of custom header key/values
|
|
179
|
+
* body, // Optional, JSON object or FormData
|
|
180
|
+
* queryParams, // Optional, A map of query strings
|
|
181
|
+
* }
|
|
182
|
+
* }).response;
|
|
183
|
+
* const data = await body.json();
|
|
184
|
+
* ```
|
|
185
|
+
*
|
|
186
|
+
* @example
|
|
187
|
+
* Cancel a PATCH request
|
|
188
|
+
* ```js
|
|
189
|
+
* import { patch, isCancelError } from '@aws-amplify/api';
|
|
190
|
+
*
|
|
191
|
+
* const { response, cancel } = patch({apiName, path, options});
|
|
192
|
+
* cancel(message);
|
|
193
|
+
* try {
|
|
194
|
+
* await response;
|
|
195
|
+
* } cache (e) {
|
|
196
|
+
* if (isCancelError(e)) {
|
|
197
|
+
* // handle request cancellation
|
|
198
|
+
* }
|
|
199
|
+
* //...
|
|
200
|
+
* }
|
|
201
|
+
* ```
|
|
202
|
+
*/
|
|
203
|
+
export declare const patch: (input: PatchInput) => PatchOperation;
|