@aws-amplify/api-rest 3.5.6-unstable.7762f1a.0 → 4.0.0

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