@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,241 @@
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
+
6
+ import {
7
+ DeleteInput,
8
+ DeleteOperation,
9
+ GetInput,
10
+ GetOperation,
11
+ HeadInput,
12
+ HeadOperation,
13
+ PatchInput,
14
+ PatchOperation,
15
+ PostInput,
16
+ PostOperation,
17
+ PutInput,
18
+ PutOperation,
19
+ } from '../types';
20
+ import { RestApiError } from '../errors';
21
+
22
+ import {
23
+ del as commonDel,
24
+ get as commonGet,
25
+ head as commonHead,
26
+ patch as commonPatch,
27
+ post as commonPost,
28
+ put as commonPut,
29
+ } from './common/publicApis';
30
+
31
+ /**
32
+ * GET HTTP request
33
+ * @param {GetInput} input - Input for GET operation
34
+ * @returns {GetOperation} Operation for GET request
35
+ * @throws - {@link RestApiError}
36
+ * @example
37
+ * Send a GET request
38
+ * ```js
39
+ * import { get, isCancelError } from '@aws-amplify/api';
40
+ *
41
+ * const { body } = await get({
42
+ * apiName,
43
+ * path,
44
+ * options: {
45
+ * headers, // Optional, A map of custom header key/values
46
+ * body, // Optional, JSON object or FormData
47
+ * queryParams, // Optional, A map of query strings
48
+ * }
49
+ * }).response;
50
+ * const data = await body.json();
51
+ * ```
52
+ * @example
53
+ * Cancel a GET request
54
+ *
55
+ * ```js
56
+ * import { get, isCancelError } from '@aws-amplify/api';
57
+ *
58
+ * const { response, cancel } = get({apiName, path, options});
59
+ * cancel(message);
60
+ * try {
61
+ * await response;
62
+ * } catch (e) {
63
+ * if (isCancelError(e)) {
64
+ * // handle request cancellation
65
+ * }
66
+ * //...
67
+ * }
68
+ * ```
69
+ */
70
+ export const get = (input: GetInput): GetOperation => commonGet(Amplify, input);
71
+
72
+ /**
73
+ * POST HTTP request
74
+ * @param {PostInput} input - Input for POST operation
75
+ * @returns {PostOperation} Operation for POST request
76
+ * @throws - {@link RestApiError}
77
+ * @example
78
+ * Send a POST request
79
+ * ```js
80
+ * import { post, isCancelError } from '@aws-amplify/api';
81
+ *
82
+ * const { body } = await post({
83
+ * apiName,
84
+ * path,
85
+ * options: {
86
+ * headers, // Optional, A map of custom header key/values
87
+ * body, // Optional, JSON object or FormData
88
+ * queryParams, // Optional, A map of query strings
89
+ * }
90
+ * }).response;
91
+ * const data = await body.json();
92
+ * ```
93
+ * @example
94
+ * Cancel a POST request
95
+ *
96
+ * ```js
97
+ * import { post, isCancelError } from '@aws-amplify/api';
98
+ *
99
+ * const { response, cancel } = post({apiName, path, options});
100
+ * cancel(message);
101
+ * try {
102
+ * await response;
103
+ * } catch (e) {
104
+ * if (isCancelError(e)) {
105
+ * // handle request cancellation
106
+ * }
107
+ * //...
108
+ * }
109
+ * ```
110
+ */
111
+ export const post = (input: PostInput): PostOperation =>
112
+ commonPost(Amplify, input);
113
+
114
+ /**
115
+ * PUT HTTP request
116
+ * @param {PutInput} input - Input for PUT operation
117
+ * @returns {PutOperation} Operation for PUT request
118
+ * @throws - {@link RestApiError}
119
+ * @example
120
+ * Send a PUT request
121
+ * ```js
122
+ * import { put, isCancelError } from '@aws-amplify/api';
123
+ *
124
+ * const { body } = await put({
125
+ * apiName,
126
+ * path,
127
+ * options: {
128
+ * headers, // Optional, A map of custom header key/values
129
+ * body, // Optional, JSON object or FormData
130
+ * queryParams, // Optional, A map of query strings
131
+ * }
132
+ * }).response;
133
+ * const data = await body.json();
134
+ * ```
135
+ * @example
136
+ * Cancel a PUT request
137
+ * ```js
138
+ * import { put, isCancelError } from '@aws-amplify/api';
139
+ *
140
+ * const { response, cancel } = put({apiName, path, options});
141
+ * cancel(message);
142
+ * try {
143
+ * await response;
144
+ * } catch (e) {
145
+ * if (isCancelError(e)) {
146
+ * // handle request cancellation
147
+ * }
148
+ * //...
149
+ * }
150
+ * ```
151
+ */
152
+ export const put = (input: PutInput): PutOperation => commonPut(Amplify, input);
153
+
154
+ /**
155
+ * DELETE HTTP request
156
+ * @param {DeleteInput} input - Input for DELETE operation
157
+ * @returns {DeleteOperation} Operation for DELETE request
158
+ * @throws - {@link RestApiError}
159
+ * @example
160
+ * Send a DELETE request
161
+ * ```js
162
+ * import { del } from '@aws-amplify/api';
163
+ *
164
+ * const { statusCode } = await del({
165
+ * apiName,
166
+ * path,
167
+ * options: {
168
+ * headers, // Optional, A map of custom header key/values
169
+ * queryParams, // Optional, A map of query strings
170
+ * }
171
+ * }).response;
172
+ * ```
173
+ */
174
+ export const del = (input: DeleteInput): DeleteOperation =>
175
+ commonDel(Amplify, input);
176
+
177
+ /**
178
+ * HEAD HTTP request
179
+ * @param {HeadInput} input - Input for HEAD operation
180
+ * @returns {HeadOperation} Operation for HEAD request
181
+ * @throws - {@link RestApiError}
182
+ * @example
183
+ * Send a HEAD request
184
+ * ```js
185
+ * import { head, isCancelError } from '@aws-amplify/api';
186
+ *
187
+ * const { headers, statusCode } = await head({
188
+ * apiName,
189
+ * path,
190
+ * options: {
191
+ * headers, // Optional, A map of custom header key/values
192
+ * queryParams, // Optional, A map of query strings
193
+ * }
194
+ * }),response;
195
+ * ```
196
+ *
197
+ */
198
+ export const head = (input: HeadInput): HeadOperation =>
199
+ commonHead(Amplify, input);
200
+
201
+ /**
202
+ * PATCH HTTP request
203
+ * @param {PatchInput} input - Input for PATCH operation
204
+ * @returns {PatchOperation} Operation for PATCH request
205
+ * @throws - {@link RestApiError}
206
+ * @example
207
+ * Send a PATCH request
208
+ * ```js
209
+ * import { patch } from '@aws-amplify/api';
210
+ *
211
+ * const { body } = await patch({
212
+ * apiName,
213
+ * path,
214
+ * options: {
215
+ * headers, // Optional, A map of custom header key/values
216
+ * body, // Optional, JSON object or FormData
217
+ * queryParams, // Optional, A map of query strings
218
+ * }
219
+ * }).response;
220
+ * const data = await body.json();
221
+ * ```
222
+ *
223
+ * @example
224
+ * Cancel a PATCH request
225
+ * ```js
226
+ * import { patch, isCancelError } from '@aws-amplify/api';
227
+ *
228
+ * const { response, cancel } = patch({apiName, path, options});
229
+ * cancel(message);
230
+ * try {
231
+ * await response;
232
+ * } catch (e) {
233
+ * if (isCancelError(e)) {
234
+ * // handle request cancellation
235
+ * }
236
+ * //...
237
+ * }
238
+ * ```
239
+ */
240
+ export const patch = (input: PatchInput): PatchOperation =>
241
+ commonPatch(Amplify, input);
@@ -0,0 +1,210 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import {
5
+ AmplifyServer,
6
+ getAmplifyServerContext,
7
+ } from '@aws-amplify/core/internals/adapter-core';
8
+
9
+ import {
10
+ DeleteInput,
11
+ DeleteOperation,
12
+ GetInput,
13
+ GetOperation,
14
+ HeadInput,
15
+ HeadOperation,
16
+ PatchInput,
17
+ PatchOperation,
18
+ PostInput,
19
+ PostOperation,
20
+ PutInput,
21
+ PutOperation,
22
+ } from '../types';
23
+ import { RestApiError } from '../errors';
24
+
25
+ import {
26
+ del as commonDel,
27
+ get as commonGet,
28
+ head as commonHead,
29
+ patch as commonPatch,
30
+ post as commonPost,
31
+ put as commonPut,
32
+ } from './common/publicApis';
33
+
34
+ /**
35
+ * GET HTTP request (server-side)
36
+ * @param {AmplifyServer.ContextSpec} contextSpec - The context spec used to get the Amplify server context.
37
+ * @param {GetInput} input - Input for GET operation.
38
+ * @throws - {@link RestApiError}
39
+ * @example
40
+ * Send a GET request
41
+ * ```js
42
+ * import { get } from 'aws-amplify/api/server';
43
+ * //...
44
+ * const restApiResponse = await runWithAmplifyServerContext({
45
+ * nextServerContext: { request, response },
46
+ * operation: async (contextSpec) => {
47
+ * try {
48
+ * const { body } = await get(contextSpec, input).response;
49
+ * return await body.json();
50
+ * } catch (error) {
51
+ * console.log(error);
52
+ * return false;
53
+ * }
54
+ * },
55
+ * });
56
+ * ```
57
+ */
58
+ export const get = (
59
+ contextSpec: AmplifyServer.ContextSpec,
60
+ input: GetInput,
61
+ ): GetOperation =>
62
+ commonGet(getAmplifyServerContext(contextSpec).amplify, input);
63
+
64
+ /**
65
+ * POST HTTP request (server-side)
66
+ * @param {AmplifyServer.ContextSpec} contextSpec - The context spec used to get the Amplify server context.
67
+ * @param {PostInput} input - Input for POST operation.
68
+ * @throws - {@link RestApiError}
69
+ * @example
70
+ * Send a POST request
71
+ * ```js
72
+ * import { post } from 'aws-amplify/api/server';
73
+ * //...
74
+ * const restApiResponse = await runWithAmplifyServerContext({
75
+ * nextServerContext: { request, response },
76
+ * operation: async (contextSpec) => {
77
+ * try {
78
+ * const { body } = await post(contextSpec, input).response;
79
+ * return await body.json();
80
+ * } catch (error) {
81
+ * console.log(error);
82
+ * return false;
83
+ * }
84
+ * },
85
+ * });
86
+ * ```
87
+ */
88
+ export const post = (
89
+ contextSpec: AmplifyServer.ContextSpec,
90
+ input: PostInput,
91
+ ): PostOperation =>
92
+ commonPost(getAmplifyServerContext(contextSpec).amplify, input);
93
+
94
+ /**
95
+ * PUT HTTP request (server-side)
96
+ * @param {AmplifyServer.ContextSpec} contextSpec - The context spec used to get the Amplify server context.
97
+ * @param {PutInput} input - Input for PUT operation.
98
+ * @throws - {@link RestApiError}
99
+ * @example
100
+ * Send a PUT request
101
+ * ```js
102
+ * import { put } from 'aws-amplify/api/server';
103
+ * //...
104
+ * const restApiResponse = await runWithAmplifyServerContext({
105
+ * nextServerContext: { request, response },
106
+ * operation: async (contextSpec) => {
107
+ * try {
108
+ * const { body } = await put(contextSpec, input).response;
109
+ * return await body.json();
110
+ * } catch (error) {
111
+ * console.log(error);
112
+ * return false;
113
+ * }
114
+ * },
115
+ * });
116
+ * ```
117
+ */
118
+ export const put = (
119
+ contextSpec: AmplifyServer.ContextSpec,
120
+ input: PutInput,
121
+ ): PutOperation =>
122
+ commonPut(getAmplifyServerContext(contextSpec).amplify, input);
123
+
124
+ /**
125
+ * DELETE HTTP request (server-side)
126
+ * @param {AmplifyServer.ContextSpec} contextSpec - The context spec used to get the Amplify server context.
127
+ * @param {DeleteInput} input - Input for DELETE operation.
128
+ * @throws - {@link RestApiError}
129
+ * @example
130
+ * Send a DELETE request
131
+ * ```js
132
+ * import { del } from 'aws-amplify/api/server';
133
+ * //...
134
+ * const restApiResponse = await runWithAmplifyServerContext({
135
+ * nextServerContext: { request, response },
136
+ * operation: async (contextSpec) => {
137
+ * try {
138
+ * const { headers } = await del(contextSpec, input).response;
139
+ * } catch (error) {
140
+ * console.log(error);
141
+ * return false;
142
+ * }
143
+ * },
144
+ * });
145
+ * ```
146
+ */
147
+ export const del = (
148
+ contextSpec: AmplifyServer.ContextSpec,
149
+ input: DeleteInput,
150
+ ): DeleteOperation =>
151
+ commonDel(getAmplifyServerContext(contextSpec).amplify, input);
152
+
153
+ /**
154
+ * HEAD HTTP request (server-side)
155
+ * @param {AmplifyServer.ContextSpec} contextSpec - The context spec used to get the Amplify server context.
156
+ * @param {HeadInput} input - Input for HEAD operation.
157
+ * @throws - {@link RestApiError}
158
+ * @example
159
+ * Send a HEAD request
160
+ * ```js
161
+ * import { head } from 'aws-amplify/api/server';
162
+ * //...
163
+ * const restApiResponse = await runWithAmplifyServerContext({
164
+ * nextServerContext: { request, response },
165
+ * operation: async (contextSpec) => {
166
+ * try {
167
+ * const { headers } = await head(contextSpec, input).response;
168
+ * } catch (error) {
169
+ * console.log(error);
170
+ * return false;
171
+ * }
172
+ * },
173
+ * });
174
+ * ```
175
+ */
176
+ export const head = (
177
+ contextSpec: AmplifyServer.ContextSpec,
178
+ input: HeadInput,
179
+ ): HeadOperation =>
180
+ commonHead(getAmplifyServerContext(contextSpec).amplify, input);
181
+
182
+ /**
183
+ * PATCH HTTP request (server-side)
184
+ * @param {AmplifyServer.ContextSpec} contextSpec - The context spec used to get the Amplify server context.
185
+ * @param {PatchInput} input - Input for PATCH operation.
186
+ * @throws - {@link RestApiError}
187
+ * @example
188
+ * Send a PATCH request
189
+ * ```js
190
+ * import { patch } from 'aws-amplify/api/server';
191
+ * //...
192
+ * const restApiResponse = await runWithAmplifyServerContext({
193
+ * nextServerContext: { request, response },
194
+ * operation: async (contextSpec) => {
195
+ * try {
196
+ * const { body } = await patch(contextSpec, input).response;
197
+ * return await body.json();
198
+ * } catch (error) {
199
+ * console.log(error);
200
+ * return false;
201
+ * }
202
+ * },
203
+ * });
204
+ * ```
205
+ */
206
+ export const patch = (
207
+ contextSpec: AmplifyServer.ContextSpec,
208
+ input: PatchInput,
209
+ ): PatchOperation =>
210
+ commonPatch(getAmplifyServerContext(contextSpec).amplify, input);
@@ -0,0 +1,37 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { AmplifyErrorParams } from '@aws-amplify/core/internals/utils';
5
+
6
+ import { RestApiError } from './RestApiError';
7
+
8
+ /**
9
+ * Internal-only class for CanceledError.
10
+ *
11
+ * @internal
12
+ */
13
+ export class CanceledError extends RestApiError {
14
+ constructor(params: Partial<AmplifyErrorParams> = {}) {
15
+ super({
16
+ name: 'CanceledError',
17
+ message: 'Request is canceled by user',
18
+ ...params,
19
+ });
20
+
21
+ // TODO: Delete the following 2 lines after we change the build target to >= es2015
22
+ this.constructor = CanceledError;
23
+ Object.setPrototypeOf(this, CanceledError.prototype);
24
+ }
25
+ }
26
+
27
+ /**
28
+ * Check if an error is caused by user calling `cancel()` in REST API.
29
+ *
30
+ * @note This function works **ONLY** for errors thrown by REST API. For GraphQL APIs, use `client.isCancelError(error)`
31
+ * instead. `client` is generated from `generateClient()` API from `aws-amplify/api`.
32
+ *
33
+ * @param {unknown} error The unknown exception to be checked.
34
+ * @returns - A boolean indicating if the error was from an upload cancellation
35
+ */
36
+ export const isCancelError = (error: unknown): error is CanceledError =>
37
+ !!error && error instanceof CanceledError;
@@ -0,0 +1,14 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { ApiError, ApiErrorParams } from '@aws-amplify/core/internals/utils';
5
+
6
+ export class RestApiError extends ApiError {
7
+ constructor(params: ApiErrorParams) {
8
+ super(params);
9
+
10
+ // TODO: Delete the following 2 lines after we change the build target to >= es2015
11
+ this.constructor = RestApiError;
12
+ Object.setPrototypeOf(this, RestApiError.prototype);
13
+ }
14
+ }
@@ -0,0 +1,19 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { RestApiError } from './RestApiError';
5
+ import { RestApiValidationErrorCode, validationErrorMap } from './validation';
6
+
7
+ /**
8
+ * @internal
9
+ */
10
+ export function assertValidationError(
11
+ assertion: boolean,
12
+ name: RestApiValidationErrorCode,
13
+ ): asserts assertion {
14
+ const { message, recoverySuggestion } = validationErrorMap[name];
15
+
16
+ if (!assertion) {
17
+ throw new RestApiError({ name, message, recoverySuggestion });
18
+ }
19
+ }
@@ -0,0 +1,7 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export { CanceledError, isCancelError } from './CanceledError';
5
+ export { RestApiError } from './RestApiError';
6
+ export { assertValidationError } from './assertValidatonError';
7
+ export { RestApiValidationErrorCode, validationErrorMap } from './validation';
@@ -0,0 +1,16 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { AmplifyErrorMap } from '@aws-amplify/core/internals/utils';
5
+
6
+ export enum RestApiValidationErrorCode {
7
+ InvalidApiName = 'InvalidApiName',
8
+ }
9
+
10
+ export const validationErrorMap: AmplifyErrorMap<RestApiValidationErrorCode> = {
11
+ [RestApiValidationErrorCode.InvalidApiName]: {
12
+ message: 'API name is invalid.',
13
+ recoverySuggestion:
14
+ 'Check if the API name matches the one in your configuration or `aws-exports.js`',
15
+ },
16
+ };
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export { isCancelError } from './errors/CanceledError';
5
+ export { get, post, put, del, head, patch } from './apis';
@@ -0,0 +1,8 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export { post } from '../apis/common/internalPost';
5
+ export {
6
+ cancel,
7
+ updateRequestToBeCancellable,
8
+ } from '../apis/common/internalPost';
@@ -0,0 +1,37 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import {
4
+ AmplifyServer,
5
+ getAmplifyServerContext,
6
+ } from '@aws-amplify/core/internals/adapter-core';
7
+
8
+ import { post as internalPost } from '../apis/common/internalPost';
9
+ import { InternalPostInput } from '../types';
10
+
11
+ /**
12
+ * Internal-only REST POST handler to send GraphQL request to given endpoint. By default, it will use IAM to authorize
13
+ * the request. In some auth modes, the IAM auth has to be disabled. Here's how to set up the request auth correctly:
14
+ * * If auth mode is 'iam', you MUST NOT set 'authorization' header and 'x-api-key' header, since it would disable IAM
15
+ * auth. You MUST also set 'input.options.signingServiceInfo' option.
16
+ * * The including 'input.options.signingServiceInfo.service' and 'input.options.signingServiceInfo.region' are
17
+ * optional. If omitted, the signing service and region will be inferred from url.
18
+ * * If auth mode is 'none', you MUST NOT set 'options.signingServiceInfo' option.
19
+ * * If auth mode is 'apiKey', you MUST set 'x-api-key' custom header.
20
+ * * If auth mode is 'oidc' or 'lambda' or 'userPool', you MUST set 'authorization' header.
21
+ *
22
+ * To make the internal post cancellable, you must also call `updateRequestToBeCancellable()` with the promise from
23
+ * internal post call and the abort controller supplied to the internal post call.
24
+ *
25
+ * @internal
26
+ */
27
+ export const post = (
28
+ contextSpec: AmplifyServer.ContextSpec,
29
+ input: InternalPostInput,
30
+ ) => {
31
+ return internalPost(getAmplifyServerContext(contextSpec).amplify, input);
32
+ };
33
+
34
+ export {
35
+ cancel,
36
+ updateRequestToBeCancellable,
37
+ } from '../apis/common/internalPost';
package/src/server.ts ADDED
@@ -0,0 +1,5 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export { isCancelError } from './errors/CanceledError';
5
+ export { get, post, put, del, head, patch } from './apis/server';