@aws-amplify/api 5.2.2-unstable.c1fa9c7.5 → 5.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/API.ts CHANGED
@@ -1,23 +1,11 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- import { Auth } from '@aws-amplify/auth';
4
- import { Cache } from '@aws-amplify/cache';
5
3
  import { AWSAppSyncRealTimeProvider } from '@aws-amplify/pubsub';
6
- import { RestAPIClass } from '@aws-amplify/api-rest';
7
- import {
8
- GraphQLAPIClass,
9
- GraphQLOptions,
10
- GraphQLResult,
11
- GraphQLOperation,
12
- OperationTypeNode,
13
- } from '@aws-amplify/api-graphql';
14
- import {
15
- Amplify,
16
- ConsoleLogger as Logger,
17
- Credentials,
18
- } from '@aws-amplify/core';
4
+ import { GraphQLOptions, GraphQLResult } from '@aws-amplify/api-graphql';
5
+ import { Amplify, ConsoleLogger as Logger } from '@aws-amplify/core';
19
6
  import Observable from 'zen-observable-ts';
20
7
  import { GraphQLQuery, GraphQLSubscription } from './types';
8
+ import { InternalAPIClass } from './internals/InternalAPI';
21
9
 
22
10
  const logger = new Logger('API');
23
11
  /**
@@ -25,185 +13,11 @@ const logger = new Logger('API');
25
13
  * Use RestApi or GraphQLAPI to reduce your application bundle size
26
14
  * Export Cloud Logic APIs
27
15
  */
28
- export class APIClass {
29
- /**
30
- * Initialize API with AWS configuration
31
- * @param {Object} options - Configuration object for API
32
- */
33
- private _options;
34
- private _restApi: RestAPIClass;
35
- private _graphqlApi: GraphQLAPIClass;
36
-
37
- Auth = Auth;
38
- Cache = Cache;
39
- Credentials = Credentials;
40
-
41
- /**
42
- * Initialize API with AWS configuration
43
- * @param {Object} options - Configuration object for API
44
- */
45
- constructor(options) {
46
- this._options = options;
47
- this._restApi = new RestAPIClass(options);
48
- this._graphqlApi = new GraphQLAPIClass(options);
49
- logger.debug('API Options', this._options);
50
- }
51
-
16
+ export class APIClass extends InternalAPIClass {
52
17
  public getModuleName() {
53
18
  return 'API';
54
19
  }
55
20
 
56
- /**
57
- * Configure API part with aws configurations
58
- * @param {Object} config - Configuration of the API
59
- * @return {Object} - The current configuration
60
- */
61
- configure(options) {
62
- this._options = Object.assign({}, this._options, options);
63
-
64
- // Share Amplify instance with client for SSR
65
- this._restApi.Credentials = this.Credentials;
66
-
67
- this._graphqlApi.Auth = this.Auth;
68
- this._graphqlApi.Cache = this.Cache;
69
- this._graphqlApi.Credentials = this.Credentials;
70
-
71
- const restAPIConfig = this._restApi.configure(this._options);
72
- const graphQLAPIConfig = this._graphqlApi.configure(this._options);
73
-
74
- return { ...restAPIConfig, ...graphQLAPIConfig };
75
- }
76
-
77
- /**
78
- * Make a GET request
79
- * @param apiName - The api name of the request
80
- * @param path - The path of the request
81
- * @param [init] - Request extra params
82
- * @return A promise that resolves to an object with response status and JSON data, if successful.
83
- */
84
- get(
85
- apiName: string,
86
- path: string,
87
- init: { [key: string]: any }
88
- ): Promise<any> {
89
- return this._restApi.get(apiName, path, init);
90
- }
91
-
92
- /**
93
- * Make a POST request
94
- * @param apiName - The api name of the request
95
- * @param path - The path of the request
96
- * @param [init] - Request extra params
97
- * @return A promise that resolves to an object with response status and JSON data, if successful.
98
- */
99
- post(
100
- apiName: string,
101
- path: string,
102
- init: { [key: string]: any }
103
- ): Promise<any> {
104
- return this._restApi.post(apiName, path, init);
105
- }
106
-
107
- /**
108
- * Make a PUT request
109
- * @param apiName - The api name of the request
110
- * @param path - The path of the request
111
- * @param [init] - Request extra params
112
- * @return A promise that resolves to an object with response status and JSON data, if successful.
113
- */
114
- put(
115
- apiName: string,
116
- path: string,
117
- init: { [key: string]: any }
118
- ): Promise<any> {
119
- return this._restApi.put(apiName, path, init);
120
- }
121
-
122
- /**
123
- * Make a PATCH request
124
- * @param apiName - The api name of the request
125
- * @param path - The path of the request
126
- * @param [init] - Request extra params
127
- * @return A promise that resolves to an object with response status and JSON data, if successful.
128
- */
129
- patch(
130
- apiName: string,
131
- path: string,
132
- init: { [key: string]: any }
133
- ): Promise<any> {
134
- return this._restApi.patch(apiName, path, init);
135
- }
136
-
137
- /**
138
- * Make a DEL request
139
- * @param apiName - The api name of the request
140
- * @param path - The path of the request
141
- * @param [init] - Request extra params
142
- * @return A promise that resolves to an object with response status and JSON data, if successful.
143
- */
144
- del(
145
- apiName: string,
146
- path: string,
147
- init: { [key: string]: any }
148
- ): Promise<any> {
149
- return this._restApi.del(apiName, path, init);
150
- }
151
-
152
- /**
153
- * Make a HEAD request
154
- * @param apiName - The api name of the request
155
- * @param path - The path of the request
156
- * @param [init] - Request extra params
157
- * @return A promise that resolves to an object with response status and JSON data, if successful.
158
- */
159
- head(
160
- apiName: string,
161
- path: string,
162
- init: { [key: string]: any }
163
- ): Promise<any> {
164
- return this._restApi.head(apiName, path, init);
165
- }
166
-
167
- /**
168
- * Checks to see if an error thrown is from an api request cancellation
169
- * @param error - Any error
170
- * @return If the error was from an api request cancellation
171
- */
172
- isCancel(error: any): boolean {
173
- return this._restApi.isCancel(error);
174
- }
175
- /**
176
- * Cancels an inflight request for either a GraphQL request or a Rest API request.
177
- * @param request - request to cancel
178
- * @param [message] - custom error message
179
- * @return If the request was cancelled
180
- */
181
- cancel(request: Promise<any>, message?: string): boolean {
182
- if (this._restApi.hasCancelToken(request)) {
183
- return this._restApi.cancel(request, message);
184
- } else if (this._graphqlApi.hasCancelToken(request)) {
185
- return this._graphqlApi.cancel(request, message);
186
- }
187
- return false;
188
- }
189
-
190
- /**
191
- * Getting endpoint for API
192
- * @param apiName - The name of the api
193
- * @return The endpoint of the api
194
- */
195
- async endpoint(apiName: string): Promise<string> {
196
- return this._restApi.endpoint(apiName);
197
- }
198
-
199
- /**
200
- * to get the operation type
201
- * @param operation
202
- */
203
- getGraphqlOperationType(operation: GraphQLOperation): OperationTypeNode {
204
- return this._graphqlApi.getGraphqlOperationType(operation);
205
- }
206
-
207
21
  /**
208
22
  * Executes a GraphQL operation
209
23
  *
@@ -226,7 +40,7 @@ export class APIClass {
226
40
  options: GraphQLOptions,
227
41
  additionalHeaders?: { [key: string]: string }
228
42
  ): Promise<GraphQLResult<any>> | Observable<object> {
229
- return this._graphqlApi.graphql(options, additionalHeaders);
43
+ return super.graphql(options, additionalHeaders);
230
44
  }
231
45
  }
232
46
 
@@ -0,0 +1,285 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import {
4
+ GraphQLOperation,
5
+ GraphQLOptions,
6
+ GraphQLResult,
7
+ OperationTypeNode,
8
+ } from '@aws-amplify/api-graphql';
9
+ import { InternalGraphQLAPIClass } from '@aws-amplify/api-graphql/internals';
10
+ import { RestAPIClass } from '@aws-amplify/api-rest';
11
+ import { Auth } from '@aws-amplify/auth';
12
+ import { Cache } from '@aws-amplify/cache';
13
+ import {
14
+ Amplify,
15
+ ApiAction,
16
+ Category,
17
+ Credentials,
18
+ CustomUserAgentDetails,
19
+ ConsoleLogger as Logger,
20
+ } from '@aws-amplify/core';
21
+ import { AWSAppSyncRealTimeProvider } from '@aws-amplify/pubsub';
22
+ import Observable from 'zen-observable-ts';
23
+ import { GraphQLQuery, GraphQLSubscription } from '../types';
24
+
25
+ const logger = new Logger('API');
26
+ /**
27
+ * @deprecated
28
+ * Use RestApi or GraphQLAPI to reduce your application bundle size
29
+ * Export Cloud Logic APIs
30
+ */
31
+ export class InternalAPIClass {
32
+ /**
33
+ * Initialize API with AWS configuration
34
+ * @param {Object} options - Configuration object for API
35
+ */
36
+ private _options;
37
+ private _restApi: RestAPIClass;
38
+ private _graphqlApi: InternalGraphQLAPIClass;
39
+
40
+ Auth = Auth;
41
+ Cache = Cache;
42
+ Credentials = Credentials;
43
+
44
+ /**
45
+ * Initialize API with AWS configuration
46
+ * @param {Object} options - Configuration object for API
47
+ */
48
+ constructor(options) {
49
+ this._options = options;
50
+ this._restApi = new RestAPIClass(options);
51
+ this._graphqlApi = new InternalGraphQLAPIClass(options);
52
+ logger.debug('API Options', this._options);
53
+ }
54
+
55
+ public getModuleName() {
56
+ return 'InternalAPI';
57
+ }
58
+
59
+ /**
60
+ * Configure API part with aws configurations
61
+ * @param {Object} config - Configuration of the API
62
+ * @return {Object} - The current configuration
63
+ */
64
+ configure(options) {
65
+ this._options = Object.assign({}, this._options, options);
66
+
67
+ // Share Amplify instance with client for SSR
68
+ this._restApi.Credentials = this.Credentials;
69
+
70
+ this._graphqlApi.Auth = this.Auth;
71
+ this._graphqlApi.Cache = this.Cache;
72
+ this._graphqlApi.Credentials = this.Credentials;
73
+
74
+ const restAPIConfig = this._restApi.configure(this._options);
75
+ const graphQLAPIConfig = this._graphqlApi.configure(this._options);
76
+
77
+ return { ...restAPIConfig, ...graphQLAPIConfig };
78
+ }
79
+
80
+ /**
81
+ * Make a GET request
82
+ * @param apiName - The api name of the request
83
+ * @param path - The path of the request
84
+ * @param [init] - Request extra params
85
+ * @return A promise that resolves to an object with response status and JSON data, if successful.
86
+ */
87
+ get(
88
+ apiName: string,
89
+ path: string,
90
+ init: { [key: string]: any }
91
+ ): Promise<any> {
92
+ return this._restApi.get(
93
+ apiName,
94
+ path,
95
+ this.getInitWithCustomUserAgentDetails(init, ApiAction.Get)
96
+ );
97
+ }
98
+
99
+ /**
100
+ * Make a POST request
101
+ * @param apiName - The api name of the request
102
+ * @param path - The path of the request
103
+ * @param [init] - Request extra params
104
+ * @return A promise that resolves to an object with response status and JSON data, if successful.
105
+ */
106
+ post(
107
+ apiName: string,
108
+ path: string,
109
+ init: { [key: string]: any }
110
+ ): Promise<any> {
111
+ return this._restApi.post(
112
+ apiName,
113
+ path,
114
+ this.getInitWithCustomUserAgentDetails(init, ApiAction.Post)
115
+ );
116
+ }
117
+
118
+ /**
119
+ * Make a PUT request
120
+ * @param apiName - The api name of the request
121
+ * @param path - The path of the request
122
+ * @param [init] - Request extra params
123
+ * @return A promise that resolves to an object with response status and JSON data, if successful.
124
+ */
125
+ put(
126
+ apiName: string,
127
+ path: string,
128
+ init: { [key: string]: any }
129
+ ): Promise<any> {
130
+ return this._restApi.put(
131
+ apiName,
132
+ path,
133
+ this.getInitWithCustomUserAgentDetails(init, ApiAction.Put)
134
+ );
135
+ }
136
+
137
+ /**
138
+ * Make a PATCH request
139
+ * @param apiName - The api name of the request
140
+ * @param path - The path of the request
141
+ * @param [init] - Request extra params
142
+ * @return A promise that resolves to an object with response status and JSON data, if successful.
143
+ */
144
+ patch(
145
+ apiName: string,
146
+ path: string,
147
+ init: { [key: string]: any }
148
+ ): Promise<any> {
149
+ return this._restApi.patch(
150
+ apiName,
151
+ path,
152
+ this.getInitWithCustomUserAgentDetails(init, ApiAction.Patch)
153
+ );
154
+ }
155
+
156
+ /**
157
+ * Make a DEL request
158
+ * @param apiName - The api name of the request
159
+ * @param path - The path of the request
160
+ * @param [init] - Request extra params
161
+ * @return A promise that resolves to an object with response status and JSON data, if successful.
162
+ */
163
+ del(
164
+ apiName: string,
165
+ path: string,
166
+ init: { [key: string]: any }
167
+ ): Promise<any> {
168
+ return this._restApi.del(
169
+ apiName,
170
+ path,
171
+ this.getInitWithCustomUserAgentDetails(init, ApiAction.Del)
172
+ );
173
+ }
174
+
175
+ /**
176
+ * Make a HEAD request
177
+ * @param apiName - The api name of the request
178
+ * @param path - The path of the request
179
+ * @param [init] - Request extra params
180
+ * @return A promise that resolves to an object with response status and JSON data, if successful.
181
+ */
182
+ head(
183
+ apiName: string,
184
+ path: string,
185
+ init: { [key: string]: any }
186
+ ): Promise<any> {
187
+ return this._restApi.head(
188
+ apiName,
189
+ path,
190
+ this.getInitWithCustomUserAgentDetails(init, ApiAction.Head)
191
+ );
192
+ }
193
+
194
+ /**
195
+ * Checks to see if an error thrown is from an api request cancellation
196
+ * @param error - Any error
197
+ * @return If the error was from an api request cancellation
198
+ */
199
+ isCancel(error: any): boolean {
200
+ return this._restApi.isCancel(error);
201
+ }
202
+ /**
203
+ * Cancels an inflight request for either a GraphQL request or a Rest API request.
204
+ * @param request - request to cancel
205
+ * @param [message] - custom error message
206
+ * @return If the request was cancelled
207
+ */
208
+ cancel(request: Promise<any>, message?: string): boolean {
209
+ if (this._restApi.hasCancelToken(request)) {
210
+ return this._restApi.cancel(request, message);
211
+ } else if (this._graphqlApi.hasCancelToken(request)) {
212
+ return this._graphqlApi.cancel(request, message);
213
+ }
214
+ return false;
215
+ }
216
+
217
+ private getInitWithCustomUserAgentDetails(
218
+ init: { [key: string]: any },
219
+ action: ApiAction
220
+ ) {
221
+ const customUserAgentDetails: CustomUserAgentDetails = {
222
+ category: Category.API,
223
+ action,
224
+ };
225
+ const initParams = { ...init, customUserAgentDetails };
226
+ return initParams;
227
+ }
228
+
229
+ /**
230
+ * Getting endpoint for API
231
+ * @param apiName - The name of the api
232
+ * @return The endpoint of the api
233
+ */
234
+ async endpoint(apiName: string): Promise<string> {
235
+ return this._restApi.endpoint(apiName);
236
+ }
237
+
238
+ /**
239
+ * to get the operation type
240
+ * @param operation
241
+ */
242
+ getGraphqlOperationType(operation: GraphQLOperation): OperationTypeNode {
243
+ return this._graphqlApi.getGraphqlOperationType(operation);
244
+ }
245
+
246
+ /**
247
+ * Executes a GraphQL operation
248
+ *
249
+ * @param options - GraphQL Options
250
+ * @param [additionalHeaders] - headers to merge in after any `graphql_headers` set in the config
251
+ * @returns An Observable if queryType is 'subscription', else a promise of the graphql result from the query.
252
+ */
253
+ graphql<T>(
254
+ options: GraphQLOptions,
255
+ additionalHeaders?: { [key: string]: string },
256
+ customUserAgentDetails?: CustomUserAgentDetails
257
+ ): T extends GraphQLQuery<T>
258
+ ? Promise<GraphQLResult<T>>
259
+ : T extends GraphQLSubscription<T>
260
+ ? Observable<{
261
+ provider: AWSAppSyncRealTimeProvider;
262
+ value: GraphQLResult<T>;
263
+ }>
264
+ : Promise<GraphQLResult<any>> | Observable<object>;
265
+ graphql<T = any>(
266
+ options: GraphQLOptions,
267
+ additionalHeaders?: { [key: string]: string },
268
+ customUserAgentDetails?: CustomUserAgentDetails
269
+ ): Promise<GraphQLResult<any>> | Observable<object> {
270
+ const apiUserAgentDetails: CustomUserAgentDetails = {
271
+ category: Category.API,
272
+ action: ApiAction.GraphQl,
273
+ ...customUserAgentDetails,
274
+ };
275
+
276
+ return this._graphqlApi.graphql(
277
+ options,
278
+ additionalHeaders,
279
+ apiUserAgentDetails
280
+ );
281
+ }
282
+ }
283
+
284
+ export const InternalAPI = new InternalAPIClass(null);
285
+ Amplify.register(InternalAPI);
@@ -0,0 +1,3 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ export { InternalAPI, InternalAPIClass } from './InternalAPI';