UNPKG

6.21 kBJavaScriptView Raw
1import { __assign, __awaiter, __generator } from "tslib";
2// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3// SPDX-License-Identifier: Apache-2.0
4import { Auth } from '@aws-amplify/auth';
5import { Cache } from '@aws-amplify/cache';
6import { RestAPIClass } from '@aws-amplify/api-rest';
7import { GraphQLAPIClass, } from '@aws-amplify/api-graphql';
8import { Amplify, ConsoleLogger as Logger, Credentials, } from '@aws-amplify/core';
9var logger = new Logger('API');
10/**
11 * @deprecated
12 * Use RestApi or GraphQLAPI to reduce your application bundle size
13 * Export Cloud Logic APIs
14 */
15var APIClass = /** @class */ (function () {
16 /**
17 * Initialize API with AWS configuration
18 * @param {Object} options - Configuration object for API
19 */
20 function APIClass(options) {
21 this.Auth = Auth;
22 this.Cache = Cache;
23 this.Credentials = Credentials;
24 this._options = options;
25 this._restApi = new RestAPIClass(options);
26 this._graphqlApi = new GraphQLAPIClass(options);
27 logger.debug('API Options', this._options);
28 }
29 APIClass.prototype.getModuleName = function () {
30 return 'API';
31 };
32 /**
33 * Configure API part with aws configurations
34 * @param {Object} config - Configuration of the API
35 * @return {Object} - The current configuration
36 */
37 APIClass.prototype.configure = function (options) {
38 this._options = Object.assign({}, this._options, options);
39 // Share Amplify instance with client for SSR
40 this._restApi.Credentials = this.Credentials;
41 this._graphqlApi.Auth = this.Auth;
42 this._graphqlApi.Cache = this.Cache;
43 this._graphqlApi.Credentials = this.Credentials;
44 var restAPIConfig = this._restApi.configure(this._options);
45 var graphQLAPIConfig = this._graphqlApi.configure(this._options);
46 return __assign(__assign({}, restAPIConfig), graphQLAPIConfig);
47 };
48 /**
49 * Make a GET request
50 * @param apiName - The api name of the request
51 * @param path - The path of the request
52 * @param [init] - Request extra params
53 * @return A promise that resolves to an object with response status and JSON data, if successful.
54 */
55 APIClass.prototype.get = function (apiName, path, init) {
56 return this._restApi.get(apiName, path, init);
57 };
58 /**
59 * Make a POST request
60 * @param apiName - The api name of the request
61 * @param path - The path of the request
62 * @param [init] - Request extra params
63 * @return A promise that resolves to an object with response status and JSON data, if successful.
64 */
65 APIClass.prototype.post = function (apiName, path, init) {
66 return this._restApi.post(apiName, path, init);
67 };
68 /**
69 * Make a PUT request
70 * @param apiName - The api name of the request
71 * @param path - The path of the request
72 * @param [init] - Request extra params
73 * @return A promise that resolves to an object with response status and JSON data, if successful.
74 */
75 APIClass.prototype.put = function (apiName, path, init) {
76 return this._restApi.put(apiName, path, init);
77 };
78 /**
79 * Make a PATCH request
80 * @param apiName - The api name of the request
81 * @param path - The path of the request
82 * @param [init] - Request extra params
83 * @return A promise that resolves to an object with response status and JSON data, if successful.
84 */
85 APIClass.prototype.patch = function (apiName, path, init) {
86 return this._restApi.patch(apiName, path, init);
87 };
88 /**
89 * Make a DEL request
90 * @param apiName - The api name of the request
91 * @param path - The path of the request
92 * @param [init] - Request extra params
93 * @return A promise that resolves to an object with response status and JSON data, if successful.
94 */
95 APIClass.prototype.del = function (apiName, path, init) {
96 return this._restApi.del(apiName, path, init);
97 };
98 /**
99 * Make a HEAD request
100 * @param apiName - The api name of the request
101 * @param path - The path of the request
102 * @param [init] - Request extra params
103 * @return A promise that resolves to an object with response status and JSON data, if successful.
104 */
105 APIClass.prototype.head = function (apiName, path, init) {
106 return this._restApi.head(apiName, path, init);
107 };
108 /**
109 * Checks to see if an error thrown is from an api request cancellation
110 * @param error - Any error
111 * @return If the error was from an api request cancellation
112 */
113 APIClass.prototype.isCancel = function (error) {
114 return this._restApi.isCancel(error);
115 };
116 /**
117 * Cancels an inflight request for either a GraphQL request or a Rest API request.
118 * @param request - request to cancel
119 * @param [message] - custom error message
120 * @return If the request was cancelled
121 */
122 APIClass.prototype.cancel = function (request, message) {
123 if (this._restApi.hasCancelToken(request)) {
124 return this._restApi.cancel(request, message);
125 }
126 else if (this._graphqlApi.hasCancelToken(request)) {
127 return this._graphqlApi.cancel(request, message);
128 }
129 return false;
130 };
131 /**
132 * Getting endpoint for API
133 * @param apiName - The name of the api
134 * @return The endpoint of the api
135 */
136 APIClass.prototype.endpoint = function (apiName) {
137 return __awaiter(this, void 0, void 0, function () {
138 return __generator(this, function (_a) {
139 return [2 /*return*/, this._restApi.endpoint(apiName)];
140 });
141 });
142 };
143 /**
144 * to get the operation type
145 * @param operation
146 */
147 APIClass.prototype.getGraphqlOperationType = function (operation) {
148 return this._graphqlApi.getGraphqlOperationType(operation);
149 };
150 APIClass.prototype.graphql = function (options, additionalHeaders) {
151 return this._graphqlApi.graphql(options, additionalHeaders);
152 };
153 return APIClass;
154}());
155export { APIClass };
156export var API = new APIClass(null);
157Amplify.register(API);
158//# sourceMappingURL=API.js.map
\No newline at end of file