@aws-amplify/api-rest 0.0.0-unstable-1ca644b-20260128091132

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (167) hide show
  1. package/README.md +3 -0
  2. package/dist/cjs/apis/common/baseHandlers/authenticatedHandler.js +17 -0
  3. package/dist/cjs/apis/common/baseHandlers/authenticatedHandler.js.map +1 -0
  4. package/dist/cjs/apis/common/baseHandlers/unauthenticatedHandler.js +13 -0
  5. package/dist/cjs/apis/common/baseHandlers/unauthenticatedHandler.js.map +1 -0
  6. package/dist/cjs/apis/common/internalPost.js +98 -0
  7. package/dist/cjs/apis/common/internalPost.js.map +1 -0
  8. package/dist/cjs/apis/common/publicApis.js +57 -0
  9. package/dist/cjs/apis/common/publicApis.js.map +1 -0
  10. package/dist/cjs/apis/common/transferHandler.js +92 -0
  11. package/dist/cjs/apis/common/transferHandler.js.map +1 -0
  12. package/dist/cjs/apis/index.js +217 -0
  13. package/dist/cjs/apis/index.js.map +1 -0
  14. package/dist/cjs/apis/server.js +163 -0
  15. package/dist/cjs/apis/server.js.map +1 -0
  16. package/dist/cjs/errors/CanceledError.js +37 -0
  17. package/dist/cjs/errors/CanceledError.js.map +1 -0
  18. package/dist/cjs/errors/RestApiError.js +17 -0
  19. package/dist/cjs/errors/RestApiError.js.map +1 -0
  20. package/dist/cjs/errors/assertValidatonError.js +18 -0
  21. package/dist/cjs/errors/assertValidatonError.js.map +1 -0
  22. package/dist/cjs/errors/index.js +17 -0
  23. package/dist/cjs/errors/index.js.map +1 -0
  24. package/dist/cjs/errors/validation.js +17 -0
  25. package/dist/cjs/errors/validation.js.map +1 -0
  26. package/dist/cjs/index.js +16 -0
  27. package/dist/cjs/index.js.map +1 -0
  28. package/dist/cjs/internals/index.js +12 -0
  29. package/dist/cjs/internals/index.js.map +1 -0
  30. package/dist/cjs/internals/server.js +32 -0
  31. package/dist/cjs/internals/server.js.map +1 -0
  32. package/dist/cjs/server.js +16 -0
  33. package/dist/cjs/server.js.map +1 -0
  34. package/dist/cjs/types/index.js +4 -0
  35. package/dist/cjs/types/index.js.map +1 -0
  36. package/dist/cjs/utils/constants.js +15 -0
  37. package/dist/cjs/utils/constants.js.map +1 -0
  38. package/dist/cjs/utils/createCancellableOperation.js +76 -0
  39. package/dist/cjs/utils/createCancellableOperation.js.map +1 -0
  40. package/dist/cjs/utils/index.js +19 -0
  41. package/dist/cjs/utils/index.js.map +1 -0
  42. package/dist/cjs/utils/isIamAuthApplicable.js +39 -0
  43. package/dist/cjs/utils/isIamAuthApplicable.js.map +1 -0
  44. package/dist/cjs/utils/logger.js +9 -0
  45. package/dist/cjs/utils/logger.js.map +1 -0
  46. package/dist/cjs/utils/parseSigningInfo.js +43 -0
  47. package/dist/cjs/utils/parseSigningInfo.js.map +1 -0
  48. package/dist/cjs/utils/resolveApiUrl.js +48 -0
  49. package/dist/cjs/utils/resolveApiUrl.js.map +1 -0
  50. package/dist/cjs/utils/resolveHeaders.js +34 -0
  51. package/dist/cjs/utils/resolveHeaders.js.map +1 -0
  52. package/dist/cjs/utils/resolveLibraryOptions.js +16 -0
  53. package/dist/cjs/utils/resolveLibraryOptions.js.map +1 -0
  54. package/dist/cjs/utils/serviceError.js +98 -0
  55. package/dist/cjs/utils/serviceError.js.map +1 -0
  56. package/dist/esm/apis/common/baseHandlers/authenticatedHandler.d.ts +5 -0
  57. package/dist/esm/apis/common/baseHandlers/authenticatedHandler.mjs +16 -0
  58. package/dist/esm/apis/common/baseHandlers/authenticatedHandler.mjs.map +1 -0
  59. package/dist/esm/apis/common/baseHandlers/unauthenticatedHandler.d.ts +5 -0
  60. package/dist/esm/apis/common/baseHandlers/unauthenticatedHandler.mjs +12 -0
  61. package/dist/esm/apis/common/baseHandlers/unauthenticatedHandler.mjs.map +1 -0
  62. package/dist/esm/apis/common/internalPost.d.ts +43 -0
  63. package/dist/esm/apis/common/internalPost.mjs +98 -0
  64. package/dist/esm/apis/common/internalPost.mjs.map +1 -0
  65. package/dist/esm/apis/common/publicApis.d.ts +8 -0
  66. package/dist/esm/apis/common/publicApis.mjs +56 -0
  67. package/dist/esm/apis/common/publicApis.mjs.map +1 -0
  68. package/dist/esm/apis/common/transferHandler.d.ts +26 -0
  69. package/dist/esm/apis/common/transferHandler.mjs +95 -0
  70. package/dist/esm/apis/common/transferHandler.mjs.map +1 -0
  71. package/dist/esm/apis/index.d.ts +203 -0
  72. package/dist/esm/apis/index.mjs +210 -0
  73. package/dist/esm/apis/index.mjs.map +1 -0
  74. package/dist/esm/apis/server.d.ts +150 -0
  75. package/dist/esm/apis/server.mjs +156 -0
  76. package/dist/esm/apis/server.mjs.map +1 -0
  77. package/dist/esm/errors/CanceledError.d.ts +20 -0
  78. package/dist/esm/errors/CanceledError.mjs +34 -0
  79. package/dist/esm/errors/CanceledError.mjs.map +1 -0
  80. package/dist/esm/errors/RestApiError.d.ts +4 -0
  81. package/dist/esm/errors/RestApiError.mjs +15 -0
  82. package/dist/esm/errors/RestApiError.mjs.map +1 -0
  83. package/dist/esm/errors/assertValidatonError.d.ts +5 -0
  84. package/dist/esm/errors/assertValidatonError.mjs +17 -0
  85. package/dist/esm/errors/assertValidatonError.mjs.map +1 -0
  86. package/dist/esm/errors/index.d.ts +4 -0
  87. package/dist/esm/errors/index.mjs +5 -0
  88. package/dist/esm/errors/index.mjs.map +1 -0
  89. package/dist/esm/errors/validation.d.ts +5 -0
  90. package/dist/esm/errors/validation.mjs +15 -0
  91. package/dist/esm/errors/validation.mjs.map +1 -0
  92. package/dist/esm/index.d.ts +2 -0
  93. package/dist/esm/index.mjs +3 -0
  94. package/dist/esm/index.mjs.map +1 -0
  95. package/dist/esm/internals/index.d.ts +2 -0
  96. package/dist/esm/internals/index.mjs +2 -0
  97. package/dist/esm/internals/index.mjs.map +1 -0
  98. package/dist/esm/internals/server.d.ts +20 -0
  99. package/dist/esm/internals/server.mjs +28 -0
  100. package/dist/esm/internals/server.mjs.map +1 -0
  101. package/dist/esm/server.d.ts +2 -0
  102. package/dist/esm/server.mjs +3 -0
  103. package/dist/esm/server.mjs.map +1 -0
  104. package/dist/esm/types/index.d.ts +122 -0
  105. package/dist/esm/types/index.mjs +2 -0
  106. package/dist/esm/types/index.mjs.map +1 -0
  107. package/dist/esm/utils/constants.d.ts +8 -0
  108. package/dist/esm/utils/constants.mjs +13 -0
  109. package/dist/esm/utils/constants.mjs.map +1 -0
  110. package/dist/esm/utils/createCancellableOperation.d.ts +12 -0
  111. package/dist/esm/utils/createCancellableOperation.mjs +77 -0
  112. package/dist/esm/utils/createCancellableOperation.mjs.map +1 -0
  113. package/dist/esm/utils/index.d.ts +6 -0
  114. package/dist/esm/utils/index.mjs +7 -0
  115. package/dist/esm/utils/index.mjs.map +1 -0
  116. package/dist/esm/utils/isIamAuthApplicable.d.ts +32 -0
  117. package/dist/esm/utils/isIamAuthApplicable.mjs +35 -0
  118. package/dist/esm/utils/isIamAuthApplicable.mjs.map +1 -0
  119. package/dist/esm/utils/logger.d.ts +2 -0
  120. package/dist/esm/utils/logger.mjs +8 -0
  121. package/dist/esm/utils/logger.mjs.map +1 -0
  122. package/dist/esm/utils/parseSigningInfo.d.ts +14 -0
  123. package/dist/esm/utils/parseSigningInfo.mjs +41 -0
  124. package/dist/esm/utils/parseSigningInfo.mjs.map +1 -0
  125. package/dist/esm/utils/resolveApiUrl.d.ts +12 -0
  126. package/dist/esm/utils/resolveApiUrl.mjs +48 -0
  127. package/dist/esm/utils/resolveApiUrl.mjs.map +1 -0
  128. package/dist/esm/utils/resolveHeaders.d.ts +1 -0
  129. package/dist/esm/utils/resolveHeaders.mjs +31 -0
  130. package/dist/esm/utils/resolveHeaders.mjs.map +1 -0
  131. package/dist/esm/utils/resolveLibraryOptions.d.ts +8 -0
  132. package/dist/esm/utils/resolveLibraryOptions.mjs +13 -0
  133. package/dist/esm/utils/resolveLibraryOptions.mjs.map +1 -0
  134. package/dist/esm/utils/serviceError.d.ts +13 -0
  135. package/dist/esm/utils/serviceError.mjs +97 -0
  136. package/dist/esm/utils/serviceError.mjs.map +1 -0
  137. package/internals/package.json +8 -0
  138. package/internals/server/package.json +7 -0
  139. package/package.json +94 -0
  140. package/server/package.json +7 -0
  141. package/src/apis/common/baseHandlers/authenticatedHandler.ts +28 -0
  142. package/src/apis/common/baseHandlers/unauthenticatedHandler.ts +22 -0
  143. package/src/apis/common/internalPost.ts +123 -0
  144. package/src/apis/common/publicApis.ts +118 -0
  145. package/src/apis/common/transferHandler.ts +154 -0
  146. package/src/apis/index.ts +241 -0
  147. package/src/apis/server.ts +210 -0
  148. package/src/errors/CanceledError.ts +37 -0
  149. package/src/errors/RestApiError.ts +14 -0
  150. package/src/errors/assertValidatonError.ts +19 -0
  151. package/src/errors/index.ts +7 -0
  152. package/src/errors/validation.ts +16 -0
  153. package/src/index.ts +5 -0
  154. package/src/internals/index.ts +8 -0
  155. package/src/internals/server.ts +37 -0
  156. package/src/server.ts +5 -0
  157. package/src/types/index.ts +138 -0
  158. package/src/utils/constants.ts +15 -0
  159. package/src/utils/createCancellableOperation.ts +108 -0
  160. package/src/utils/index.ts +9 -0
  161. package/src/utils/isIamAuthApplicable.ts +44 -0
  162. package/src/utils/logger.ts +6 -0
  163. package/src/utils/parseSigningInfo.ts +53 -0
  164. package/src/utils/resolveApiUrl.ts +59 -0
  165. package/src/utils/resolveHeaders.ts +36 -0
  166. package/src/utils/resolveLibraryOptions.ts +14 -0
  167. package/src/utils/serviceError.ts +109 -0
@@ -0,0 +1,97 @@
1
+ import { parseJsonError } from '@aws-amplify/core/internals/aws-client-utils';
2
+ import { RestApiError } from '../errors/RestApiError.mjs';
3
+ import '../errors/validation.mjs';
4
+
5
+ /**
6
+ * Parses both AWS and non-AWS error responses coming from the users' backend code.
7
+ * * AWS errors generated by the AWS services(e.g. API Gateway, Bedrock). They can be Signature errors,
8
+ * ClockSkew errors, etc. These responses will be parsed to errors with proper name and message from the AWS
9
+ * services.
10
+ * * non-AWS errors thrown by the user code. They can contain any headers or body. Users need to access the
11
+ * error.response to get the headers and body and parse them accordingly. The JS error name and message will
12
+ * be `UnknownError` and `Unknown error` respectively.
13
+ */
14
+ const parseRestApiServiceError = async (response) => {
15
+ if (!response) {
16
+ // Response is not considered an error.
17
+ return;
18
+ }
19
+ const parsedAwsError = await parseJsonError(stubErrorResponse(response));
20
+ if (!parsedAwsError) ;
21
+ else {
22
+ const bodyText = await response.body?.text();
23
+ return buildRestApiError(parsedAwsError, {
24
+ statusCode: response.statusCode,
25
+ headers: response.headers,
26
+ body: bodyText,
27
+ });
28
+ }
29
+ };
30
+ /**
31
+ * The response object needs to be stub here because the parseAwsJsonError assumes the response body to be valid JSON.
32
+ * Although this is true for AWS services, it is not true for responses from user's code. Once the response body is
33
+ * unwrapped as JSON(and fail), it cannot be read as text again. Therefore, we need to stub the response body here to
34
+ * make sure we can read the error response body as a JSON, and may fall back to read as text if it is not a valid JSON.
35
+ */
36
+ const stubErrorResponse = (response) => {
37
+ let bodyTextPromise;
38
+ const bodyProxy = new Proxy(response.body, {
39
+ get(target, prop, receiver) {
40
+ if (prop === 'json') {
41
+ // For potential AWS errors, error parser will try to parse the body as JSON first.
42
+ return async () => {
43
+ if (!bodyTextPromise) {
44
+ bodyTextPromise = target.text();
45
+ }
46
+ try {
47
+ return JSON.parse(await bodyTextPromise);
48
+ }
49
+ catch (error) {
50
+ // If response body is not a valid JSON, we stub it to be an empty object and eventually parsed
51
+ // as an unknown error
52
+ return {};
53
+ }
54
+ };
55
+ }
56
+ else if (prop === 'text') {
57
+ // For non-AWS errors, users can access the body as a string as a fallback.
58
+ return async () => {
59
+ if (!bodyTextPromise) {
60
+ bodyTextPromise = target.text();
61
+ }
62
+ return bodyTextPromise;
63
+ };
64
+ }
65
+ else {
66
+ return Reflect.get(target, prop, receiver);
67
+ }
68
+ },
69
+ });
70
+ const responseProxy = new Proxy(response, {
71
+ get(target, prop, receiver) {
72
+ if (prop === 'body') {
73
+ return bodyProxy;
74
+ }
75
+ else {
76
+ return Reflect.get(target, prop, receiver);
77
+ }
78
+ },
79
+ });
80
+ return responseProxy;
81
+ };
82
+ /**
83
+ * Utility to create a new RestApiError from a service error.
84
+ */
85
+ const buildRestApiError = (error, response) => {
86
+ const restApiError = new RestApiError({
87
+ name: error?.name,
88
+ message: error.message,
89
+ underlyingError: error,
90
+ response,
91
+ });
92
+ // $metadata is only required for backwards compatibility.
93
+ return Object.assign(restApiError, { $metadata: error.$metadata });
94
+ };
95
+
96
+ export { parseRestApiServiceError };
97
+ //# sourceMappingURL=serviceError.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"serviceError.mjs","sources":["../../../src/utils/serviceError.ts"],"sourcesContent":["import { parseJsonError as parseAwsJsonError, } from '@aws-amplify/core/internals/aws-client-utils';\nimport { RestApiError } from '../errors';\n/**\n * Parses both AWS and non-AWS error responses coming from the users' backend code.\n * * AWS errors generated by the AWS services(e.g. API Gateway, Bedrock). They can be Signature errors,\n * ClockSkew errors, etc. These responses will be parsed to errors with proper name and message from the AWS\n * services.\n * * non-AWS errors thrown by the user code. They can contain any headers or body. Users need to access the\n * error.response to get the headers and body and parse them accordingly. The JS error name and message will\n * be `UnknownError` and `Unknown error` respectively.\n */\nexport const parseRestApiServiceError = async (response) => {\n if (!response) {\n // Response is not considered an error.\n return;\n }\n const parsedAwsError = await parseAwsJsonError(stubErrorResponse(response));\n if (!parsedAwsError) {\n // Response is not considered an error.\n }\n else {\n const bodyText = await response.body?.text();\n return buildRestApiError(parsedAwsError, {\n statusCode: response.statusCode,\n headers: response.headers,\n body: bodyText,\n });\n }\n};\n/**\n * The response object needs to be stub here because the parseAwsJsonError assumes the response body to be valid JSON.\n * Although this is true for AWS services, it is not true for responses from user's code. Once the response body is\n * unwrapped as JSON(and fail), it cannot be read as text again. Therefore, we need to stub the response body here to\n * make sure we can read the error response body as a JSON, and may fall back to read as text if it is not a valid JSON.\n */\nconst stubErrorResponse = (response) => {\n let bodyTextPromise;\n const bodyProxy = new Proxy(response.body, {\n get(target, prop, receiver) {\n if (prop === 'json') {\n // For potential AWS errors, error parser will try to parse the body as JSON first.\n return async () => {\n if (!bodyTextPromise) {\n bodyTextPromise = target.text();\n }\n try {\n return JSON.parse(await bodyTextPromise);\n }\n catch (error) {\n // If response body is not a valid JSON, we stub it to be an empty object and eventually parsed\n // as an unknown error\n return {};\n }\n };\n }\n else if (prop === 'text') {\n // For non-AWS errors, users can access the body as a string as a fallback.\n return async () => {\n if (!bodyTextPromise) {\n bodyTextPromise = target.text();\n }\n return bodyTextPromise;\n };\n }\n else {\n return Reflect.get(target, prop, receiver);\n }\n },\n });\n const responseProxy = new Proxy(response, {\n get(target, prop, receiver) {\n if (prop === 'body') {\n return bodyProxy;\n }\n else {\n return Reflect.get(target, prop, receiver);\n }\n },\n });\n return responseProxy;\n};\n/**\n * Utility to create a new RestApiError from a service error.\n */\nconst buildRestApiError = (error, response) => {\n const restApiError = new RestApiError({\n name: error?.name,\n message: error.message,\n underlyingError: error,\n response,\n });\n // $metadata is only required for backwards compatibility.\n return Object.assign(restApiError, { $metadata: error.$metadata });\n};\n"],"names":["parseAwsJsonError"],"mappings":"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"}
@@ -0,0 +1,8 @@
1
+ {
2
+ "name": "@aws-amplify/api-rest/internals",
3
+ "types": "../dist/esm/internals/index.d.ts",
4
+ "main": "../dist/cjs/internals/index.js",
5
+ "module": "../dist/esm/internals/index.mjs",
6
+ "react-native": "../dist/cjs/internals/index.js",
7
+ "sideEffects": false
8
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "name": "@aws-amplify/api-rest/internals/server",
3
+ "types": "../../dist/esm/internals/server.d.ts",
4
+ "main": "../../dist/cjs/internals/server.js",
5
+ "module": "../../dist/esm/internals/server.mjs",
6
+ "sideEffects": false
7
+ }
package/package.json ADDED
@@ -0,0 +1,94 @@
1
+ {
2
+ "name": "@aws-amplify/api-rest",
3
+ "private": false,
4
+ "version": "0.0.0-unstable-1ca644b-20260128091132",
5
+ "description": "Api-rest category of aws-amplify",
6
+ "main": "./dist/cjs/index.js",
7
+ "module": "./dist/esm/index.mjs",
8
+ "typings": "./dist/esm/index.d.ts",
9
+ "react-native": "./dist/cjs/index.js",
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "scripts": {
14
+ "test": "npm run lint && jest -w 1 --coverage --logHeapUsage",
15
+ "test:watch": "jest -w 1 --watch",
16
+ "build-with-test": "npm test && npm build",
17
+ "build:umd": "webpack && webpack --config ./webpack.config.dev.js",
18
+ "build:esm-cjs": "rollup --forceExit -c rollup.config.mjs",
19
+ "build:watch": "npm run build:esm-cjs -- --watch",
20
+ "build": "npm run clean && npm run build:esm-cjs && npm run build:umd",
21
+ "clean": "npm run clean:size && rimraf dist lib lib-esm",
22
+ "clean:size": "rimraf dual-publish-tmp tmp*",
23
+ "format": "echo \"Not implemented\"",
24
+ "lint": "eslint '**/*.{ts,tsx}' && npm run ts-coverage",
25
+ "lint:fix": "eslint '**/*.{ts,tsx}' --fix",
26
+ "ts-coverage": "typescript-coverage-report -p ./tsconfig.build.json -t 70.0"
27
+ },
28
+ "exports": {
29
+ ".": {
30
+ "react-native": "./dist/cjs/index.js",
31
+ "types": "./dist/esm/index.d.ts",
32
+ "import": "./dist/esm/index.mjs",
33
+ "require": "./dist/cjs/index.js"
34
+ },
35
+ "./server": {
36
+ "types": "./dist/esm/server.d.ts",
37
+ "import": "./dist/esm/server.mjs",
38
+ "require": "./dist/cjs/server.js"
39
+ },
40
+ "./internals": {
41
+ "react-native": "./dist/cjs/internals/index.js",
42
+ "types": "./dist/esm/internals/index.d.ts",
43
+ "import": "./dist/esm/internals/index.mjs",
44
+ "require": "./dist/cjs/internals/index.js"
45
+ },
46
+ "./internals/server": {
47
+ "types": "./dist/esm/internals/server.d.ts",
48
+ "import": "./dist/esm/internals/server.mjs",
49
+ "require": "./dist/cjs/internals/server.js"
50
+ },
51
+ "./package.json": "./package.json"
52
+ },
53
+ "typesVersions": {
54
+ ">=4.2": {
55
+ "server": [
56
+ "./dist/esm/server.d.ts"
57
+ ],
58
+ "internals": [
59
+ "./dist/esm/internals/index.d.ts"
60
+ ],
61
+ "internals/server": [
62
+ "./dist/esm/internals/server.d.ts"
63
+ ]
64
+ }
65
+ },
66
+ "repository": {
67
+ "type": "git",
68
+ "url": "https://github.com/aws-amplify/amplify-js.git"
69
+ },
70
+ "author": "Amazon Web Services",
71
+ "license": "Apache-2.0",
72
+ "bugs": {
73
+ "url": "https://github.com/aws/aws-amplify/issues"
74
+ },
75
+ "homepage": "https://aws-amplify.github.io/",
76
+ "files": [
77
+ "dist/cjs",
78
+ "dist/esm",
79
+ "src",
80
+ "internals",
81
+ "server"
82
+ ],
83
+ "dependencies": {
84
+ "tslib": "^2.5.0"
85
+ },
86
+ "peerDependencies": {
87
+ "@aws-amplify/core": "0.0.0-unstable-1ca644b-20260128091132"
88
+ },
89
+ "devDependencies": {
90
+ "@aws-amplify/core": "0.0.0-unstable-1ca644b-20260128091132",
91
+ "@aws-amplify/react-native": "0.0.0-unstable-1ca644b-20260128091132",
92
+ "@aws-sdk/types": "3.723.0"
93
+ }
94
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "name": "@aws-amplify/api-rest/server",
3
+ "types": "../dist/esm/server.d.ts",
4
+ "main": "../dist/cjs/server.js",
5
+ "module": "../dist/esm/server.mjs",
6
+ "sideEffects": false
7
+ }
@@ -0,0 +1,28 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import {
4
+ HttpRequest,
5
+ HttpResponse,
6
+ RetryOptions,
7
+ SigningOptions,
8
+ UserAgentOptions,
9
+ fetchTransferHandler,
10
+ retryMiddlewareFactory,
11
+ signingMiddlewareFactory,
12
+ userAgentMiddlewareFactory,
13
+ } from '@aws-amplify/core/internals/aws-client-utils';
14
+ import { composeTransferHandler } from '@aws-amplify/core/internals/aws-client-utils/composers';
15
+
16
+ /**
17
+ * @internal
18
+ */
19
+ export const authenticatedHandler = composeTransferHandler<
20
+ [UserAgentOptions, RetryOptions<HttpResponse>, SigningOptions],
21
+ HttpRequest,
22
+ HttpResponse,
23
+ typeof fetchTransferHandler
24
+ >(fetchTransferHandler, [
25
+ userAgentMiddlewareFactory,
26
+ retryMiddlewareFactory,
27
+ signingMiddlewareFactory,
28
+ ]);
@@ -0,0 +1,22 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import {
4
+ HttpRequest,
5
+ HttpResponse,
6
+ RetryOptions,
7
+ UserAgentOptions,
8
+ fetchTransferHandler,
9
+ retryMiddlewareFactory,
10
+ userAgentMiddlewareFactory,
11
+ } from '@aws-amplify/core/internals/aws-client-utils';
12
+ import { composeTransferHandler } from '@aws-amplify/core/internals/aws-client-utils/composers';
13
+
14
+ /**
15
+ * @internal
16
+ */
17
+ export const unauthenticatedHandler = composeTransferHandler<
18
+ [UserAgentOptions, RetryOptions<HttpResponse>],
19
+ HttpRequest,
20
+ HttpResponse,
21
+ typeof fetchTransferHandler
22
+ >(fetchTransferHandler, [userAgentMiddlewareFactory, retryMiddlewareFactory]);
@@ -0,0 +1,123 @@
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
+
6
+ import { InternalPostInput, RestApiResponse } from '../../types';
7
+ import { createCancellableOperation } from '../../utils';
8
+ import { CanceledError } from '../../errors';
9
+ import { isIamAuthApplicableForGraphQL } from '../../utils/isIamAuthApplicable';
10
+
11
+ import { transferHandler } from './transferHandler';
12
+
13
+ /**
14
+ * This weak map provides functionality to cancel a request given the promise containing the `post` request.
15
+ *
16
+ * 1. For every GraphQL POST request, an abort controller is created and supplied to the request.
17
+ * 2. The promise fulfilled by GraphGL POST request is then mapped to that abort controller.
18
+ * 3. The promise is returned to the external caller.
19
+ * 4. The caller can either wait for the promise to fulfill or call `cancel(promise)` to cancel the request.
20
+ * 5. If `cancel(promise)` is called, then the corresponding abort controller is retrieved from the map below.
21
+ * 6. GraphQL POST request will be rejected with the error message provided during cancel.
22
+ * 7. Caller can check if the error is because of cancelling by calling `isCancelError(error)`.
23
+ */
24
+ const cancelTokenMap = new WeakMap<Promise<any>, AbortController>();
25
+
26
+ /**
27
+ * @internal
28
+ *
29
+ * REST POST handler to send GraphQL request to given endpoint. By default, it will use IAM to authorize
30
+ * the request. In some auth modes, the IAM auth has to be disabled. Here's how to set up the request auth correctly:
31
+ * * If auth mode is 'iam', you MUST NOT set 'authorization' header and 'x-api-key' header, since it would disable IAM
32
+ * auth. You MUST also set 'input.options.signingServiceInfo' option.
33
+ * * The including 'input.options.signingServiceInfo.service' and 'input.options.signingServiceInfo.region' are
34
+ * optional. If omitted, the signing service and region will be inferred from url.
35
+ * * If auth mode is 'none', you MUST NOT set 'options.signingServiceInfo' option.
36
+ * * If auth mode is 'apiKey', you MUST set 'x-api-key' custom header.
37
+ * * If auth mode is 'oidc' or 'lambda' or 'userPool', you MUST set 'authorization' header.
38
+ *
39
+ * To make the internal post cancellable, you must also call `updateRequestToBeCancellable()` with the promise from
40
+ * internal post call and the abort controller supplied to the internal post call.
41
+ *
42
+ * @param amplify the AmplifyClassV6 instance - it may be the singleton used on Web, or an instance created within
43
+ * a context created by `runWithAmplifyServerContext`
44
+ * @param postInput an object of {@link InternalPostInput}
45
+ * @param postInput.url The URL that the POST request sends to
46
+ * @param postInput.options Options of the POST request
47
+ * @param postInput.abortController The abort controller used to cancel the POST request
48
+ * @returns a {@link RestApiResponse}
49
+ *
50
+ * @throws an {@link AmplifyError} with `Network Error` as the `message` when the external resource is unreachable due to one
51
+ * of the following reasons:
52
+ * 1. no network connection
53
+ * 2. CORS error
54
+ * @throws a {@link CanceledError} when the ongoing POST request get cancelled
55
+ */
56
+ export const post = (
57
+ amplify: AmplifyClassV6,
58
+ { url, options, abortController }: InternalPostInput,
59
+ ): Promise<RestApiResponse> => {
60
+ const controller = abortController ?? new AbortController();
61
+ const responsePromise = createCancellableOperation(
62
+ async () => {
63
+ const response = transferHandler(
64
+ amplify,
65
+ {
66
+ url,
67
+ method: 'POST',
68
+ ...options,
69
+ abortSignal: controller.signal,
70
+ retryStrategy: {
71
+ strategy: 'jittered-exponential-backoff',
72
+ },
73
+ },
74
+ isIamAuthApplicableForGraphQL,
75
+ options?.signingServiceInfo,
76
+ );
77
+
78
+ return response;
79
+ },
80
+ controller,
81
+ 'internal', // operation Type
82
+ );
83
+
84
+ const responseWithCleanUp = responsePromise.finally(() => {
85
+ cancelTokenMap.delete(responseWithCleanUp);
86
+ });
87
+
88
+ return responseWithCleanUp;
89
+ };
90
+
91
+ /**
92
+ * Cancels a request given the promise returned by `post`.
93
+ * If the request is already completed, this function does nothing.
94
+ * It MUST be used after `updateRequestToBeCancellable` is called.
95
+ */
96
+ export const cancel = (
97
+ promise: Promise<RestApiResponse>,
98
+ message?: string,
99
+ ): boolean => {
100
+ const controller = cancelTokenMap.get(promise);
101
+ if (controller) {
102
+ controller.abort(message);
103
+ if (message && controller.signal.reason !== message) {
104
+ // In runtimes where `AbortSignal.reason` is not supported, we track the reason ourselves.
105
+ // @ts-expect-error reason is read-only property.
106
+ controller.signal.reason = message;
107
+ }
108
+
109
+ return true;
110
+ }
111
+
112
+ return false;
113
+ };
114
+
115
+ /**
116
+ * MUST be used to make a promise including internal `post` API call cancellable.
117
+ */
118
+ export const updateRequestToBeCancellable = (
119
+ promise: Promise<any>,
120
+ controller: AbortController,
121
+ ) => {
122
+ cancelTokenMap.set(promise, controller);
123
+ };
@@ -0,0 +1,118 @@
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
+
6
+ import {
7
+ ApiInput,
8
+ DeleteInput,
9
+ DeleteOperation,
10
+ GetInput,
11
+ GetOperation,
12
+ HeadInput,
13
+ HeadOperation,
14
+ PatchInput,
15
+ PatchOperation,
16
+ PostInput,
17
+ PostOperation,
18
+ PutInput,
19
+ PutOperation,
20
+ RestApiOptionsBase,
21
+ } from '../../types';
22
+ import {
23
+ createCancellableOperation,
24
+ logger,
25
+ parseSigningInfo,
26
+ resolveApiUrl,
27
+ } from '../../utils';
28
+ import { isIamAuthApplicableForRest } from '../../utils/isIamAuthApplicable';
29
+
30
+ import { transferHandler } from './transferHandler';
31
+
32
+ const publicHandler = (
33
+ amplify: AmplifyClassV6,
34
+ options: ApiInput<RestApiOptionsBase>,
35
+ method: string,
36
+ ) => {
37
+ const { apiName, options: apiOptions = {}, path: apiPath } = options;
38
+ const libraryConfigTimeout = amplify.libraryOptions?.API?.REST?.timeout?.({
39
+ apiName,
40
+ method,
41
+ });
42
+ const timeout = apiOptions?.timeout || libraryConfigTimeout || undefined;
43
+ const publicApisAbortController = new AbortController();
44
+ const abortSignal = publicApisAbortController.signal;
45
+
46
+ return createCancellableOperation(
47
+ async () => {
48
+ const url = resolveApiUrl(
49
+ amplify,
50
+ apiName,
51
+ apiPath,
52
+ apiOptions?.queryParams,
53
+ );
54
+ const libraryConfigHeaders =
55
+ await amplify.libraryOptions?.API?.REST?.headers?.({
56
+ apiName,
57
+ });
58
+ const { headers: invocationHeaders = {} } = apiOptions;
59
+ const headers = {
60
+ // custom headers from invocation options should precede library options
61
+ ...libraryConfigHeaders,
62
+ ...invocationHeaders,
63
+ };
64
+ const signingServiceInfo = parseSigningInfo(url, {
65
+ amplify,
66
+ apiName,
67
+ });
68
+ logger.debug(
69
+ method,
70
+ url,
71
+ headers,
72
+ `IAM signing options: ${JSON.stringify(signingServiceInfo)}`,
73
+ );
74
+
75
+ return transferHandler(
76
+ amplify,
77
+ {
78
+ ...apiOptions,
79
+ url,
80
+ method,
81
+ headers,
82
+ abortSignal,
83
+ },
84
+ isIamAuthApplicableForRest,
85
+ signingServiceInfo,
86
+ );
87
+ },
88
+ publicApisAbortController,
89
+ 'public', // operation Type
90
+ timeout,
91
+ );
92
+ };
93
+
94
+ export const get = (amplify: AmplifyClassV6, input: GetInput): GetOperation =>
95
+ publicHandler(amplify, input, 'GET');
96
+
97
+ export const post = (
98
+ amplify: AmplifyClassV6,
99
+ input: PostInput,
100
+ ): PostOperation => publicHandler(amplify, input, 'POST');
101
+
102
+ export const put = (amplify: AmplifyClassV6, input: PutInput): PutOperation =>
103
+ publicHandler(amplify, input, 'PUT');
104
+
105
+ export const del = (
106
+ amplify: AmplifyClassV6,
107
+ input: DeleteInput,
108
+ ): DeleteOperation => publicHandler(amplify, input, 'DELETE');
109
+
110
+ export const head = (
111
+ amplify: AmplifyClassV6,
112
+ input: HeadInput,
113
+ ): HeadOperation => publicHandler(amplify, input, 'HEAD');
114
+
115
+ export const patch = (
116
+ amplify: AmplifyClassV6,
117
+ input: PatchInput,
118
+ ): PatchOperation => publicHandler(amplify, input, 'PATCH');
@@ -0,0 +1,154 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { AmplifyClassV6 } from '@aws-amplify/core';
4
+ import {
5
+ Headers,
6
+ HttpRequest,
7
+ RetryOptions,
8
+ getRetryDecider,
9
+ jitteredBackoff,
10
+ } from '@aws-amplify/core/internals/aws-client-utils';
11
+ import {
12
+ AWSCredentials,
13
+ DocumentType,
14
+ RESTAuthMode,
15
+ RetryStrategy,
16
+ } from '@aws-amplify/core/internals/utils';
17
+
18
+ import {
19
+ logger,
20
+ parseRestApiServiceError,
21
+ parseSigningInfo,
22
+ resolveLibraryOptions,
23
+ } from '../../utils';
24
+ import { resolveHeaders } from '../../utils/resolveHeaders';
25
+ import { RestApiResponse, SigningServiceInfo } from '../../types';
26
+
27
+ import { authenticatedHandler } from './baseHandlers/authenticatedHandler';
28
+ import { unauthenticatedHandler } from './baseHandlers/unauthenticatedHandler';
29
+
30
+ type HandlerOptions = Omit<HttpRequest, 'body' | 'headers'> & {
31
+ body?: DocumentType | FormData;
32
+ headers?: Headers;
33
+ withCredentials?: boolean;
34
+ retryStrategy?: RetryStrategy;
35
+ defaultAuthMode?: RESTAuthMode;
36
+ };
37
+
38
+ type RetryDecider = RetryOptions['retryDecider'];
39
+
40
+ /**
41
+ * Make REST API call with best-effort IAM auth.
42
+ * @param amplify Amplify instance to to resolve credentials and tokens. Should use different instance in client-side
43
+ * and SSR
44
+ * @param options Options accepted from public API options when calling the handlers.
45
+ * @param signingServiceInfo Internal-only options enable IAM auth as well as to to overwrite the IAM signing service
46
+ * and region. If specified, and NONE of API Key header or Auth header is present, IAM auth will be used.
47
+ * @param iamAuthApplicable Callback function that is used to determine if IAM Auth should be used or not.
48
+ *
49
+ * @internal
50
+ */
51
+ export const transferHandler = async (
52
+ amplify: AmplifyClassV6,
53
+ options: HandlerOptions & { abortSignal: AbortSignal },
54
+ iamAuthApplicable: (
55
+ { headers }: HttpRequest,
56
+ signingServiceInfo?: SigningServiceInfo,
57
+ ) => boolean,
58
+ signingServiceInfo?: SigningServiceInfo,
59
+ ): Promise<RestApiResponse> => {
60
+ const {
61
+ url,
62
+ method,
63
+ headers,
64
+ body,
65
+ withCredentials,
66
+ abortSignal,
67
+ retryStrategy,
68
+ } = options;
69
+ const resolvedBody = body
70
+ ? body instanceof FormData
71
+ ? body
72
+ : JSON.stringify(body ?? '')
73
+ : undefined;
74
+ const resolvedHeaders: Headers = resolveHeaders(headers, body);
75
+ const request = {
76
+ url,
77
+ headers: resolvedHeaders,
78
+ method,
79
+ body: resolvedBody,
80
+ };
81
+ const {
82
+ retryStrategy: libraryRetryStrategy,
83
+ defaultAuthMode: libraryDefaultAuthMode,
84
+ } = resolveLibraryOptions(amplify);
85
+ const baseOptions = {
86
+ retryDecider: getRetryDeciderFromStrategy(
87
+ retryStrategy ?? libraryRetryStrategy,
88
+ ),
89
+ computeDelay: jitteredBackoff,
90
+ withCrossDomainCredentials: withCredentials,
91
+ abortSignal,
92
+ };
93
+
94
+ const defaultAuthMode = options.defaultAuthMode ?? libraryDefaultAuthMode;
95
+
96
+ let credentials: AWSCredentials | null = null;
97
+ if (defaultAuthMode !== 'none') {
98
+ credentials = await resolveCredentials(amplify);
99
+ }
100
+
101
+ let response: RestApiResponse;
102
+ const isIamAuthApplicable = iamAuthApplicable(request, signingServiceInfo);
103
+
104
+ if (isIamAuthApplicable && credentials) {
105
+ const signingInfoFromUrl = parseSigningInfo(url);
106
+ const signingService =
107
+ signingServiceInfo?.service ?? signingInfoFromUrl.service;
108
+ const signingRegion =
109
+ signingServiceInfo?.region ?? signingInfoFromUrl.region;
110
+ response = await authenticatedHandler(request, {
111
+ ...baseOptions,
112
+ credentials,
113
+ region: signingRegion,
114
+ service: signingService,
115
+ });
116
+ } else {
117
+ response = await unauthenticatedHandler(request, {
118
+ ...baseOptions,
119
+ });
120
+ }
121
+
122
+ // Clean-up un-modeled properties from response.
123
+ return {
124
+ statusCode: response.statusCode,
125
+ headers: response.headers,
126
+ body: response.body,
127
+ };
128
+ };
129
+
130
+ const getRetryDeciderFromStrategy = (
131
+ retryStrategy: RetryStrategy | undefined,
132
+ ): RetryDecider => {
133
+ const strategy = retryStrategy?.strategy;
134
+ if (strategy === 'no-retry') {
135
+ return () => Promise.resolve({ retryable: false });
136
+ }
137
+
138
+ return getRetryDecider(parseRestApiServiceError);
139
+ };
140
+
141
+ const resolveCredentials = async (
142
+ amplify: AmplifyClassV6,
143
+ ): Promise<AWSCredentials | null> => {
144
+ try {
145
+ const { credentials } = await amplify.Auth.fetchAuthSession();
146
+ if (credentials) {
147
+ return credentials;
148
+ }
149
+ } catch (e) {
150
+ logger.debug('No credentials available, the request will be unsigned.');
151
+ }
152
+
153
+ return null;
154
+ };