1
/*
2
 * Copyright 2017-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
5
 * the License. A copy of the License is located at
6
 *
7
 *     http://aws.amazon.com/apache2.0/
8
 *
9
 * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
10
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
11
 * and limitations under the License.
12
 */
13 2
import { Auth } from '@aws-amplify/auth';
14 2
import Cache from '@aws-amplify/cache';
15 2
import { RestAPIClass } from '@aws-amplify/api-rest';
16 2
import {
17
	GraphQLAPIClass,
18
	GraphQLOptions,
19
	GraphQLResult,
20
} from '@aws-amplify/api-graphql';
21 2
import {
22
	Amplify,
23
	ConsoleLogger as Logger,
24
	Credentials,
25
} from '@aws-amplify/core';
26
import Observable from 'zen-observable-ts';
27

28 2
const logger = new Logger('API');
29
/**
30
 * @deprecated
31
 * Use RestApi or GraphQLAPI to reduce your application bundle size
32
 * Export Cloud Logic APIs
33
 */
34 2
export class APIClass {
35
	/**
36
	 * Initialize API with AWS configuration
37
	 * @param {Object} options - Configuration object for API
38
	 */
39
	private _options;
40
	private _restApi: RestAPIClass;
41
	private _graphqlApi;
42

43 2
	Auth = Auth;
44 2
	Cache = Cache;
45 2
	Credentials = Credentials;
46

47
	/**
48
	 * Initialize API with AWS configuration
49
	 * @param {Object} options - Configuration object for API
50
	 */
51
	constructor(options) {
52 2
		this._options = options;
53 2
		this._restApi = new RestAPIClass(options);
54 2
		this._graphqlApi = new GraphQLAPIClass(options);
55 2
		logger.debug('API Options', this._options);
56
	}
57

58 2
	public getModuleName() {
59 2
		return 'API';
60
	}
61

62
	/**
63
	 * Configure API part with aws configurations
64
	 * @param {Object} config - Configuration of the API
65
	 * @return {Object} - The current configuration
66
	 */
67 2
	configure(options) {
68 2
		this._options = Object.assign({}, this._options, options);
69

70
		// Share Amplify instance with client for SSR
71 2
		this._restApi.Credentials = this.Credentials;
72

73 2
		this._graphqlApi.Auth = this.Auth;
74 2
		this._graphqlApi.Cache = this.Cache;
75 2
		this._graphqlApi.Credentials = this.Credentials;
76

77 2
		const restAPIConfig = this._restApi.configure(this._options);
78 2
		const graphQLAPIConfig = this._graphqlApi.configure(this._options);
79

80 2
		return { ...restAPIConfig, ...graphQLAPIConfig };
81
	}
82

83
	/**
84
	 * Make a GET request
85
	 * @param {string} apiName - The api name of the request
86
	 * @param {string} path - The path of the request
87
	 * @param {json} [init] - Request extra params
88
	 * @return {Promise} - A promise that resolves to an object with response status and JSON data, if successful.
89
	 */
90 2
	get(apiName, path, init): Promise<any> {
91 2
		return this._restApi.get(apiName, path, init);
92
	}
93

94
	/**
95
	 * Make a POST request
96
	 * @param {string} apiName - The api name of the request
97
	 * @param {string} path - The path of the request
98
	 * @param {json} [init] - Request extra params
99
	 * @return {Promise} - A promise that resolves to an object with response status and JSON data, if successful.
100
	 */
101 2
	post(apiName, path, init): Promise<any> {
102 2
		return this._restApi.post(apiName, path, init);
103
	}
104

105
	/**
106
	 * Make a PUT request
107
	 * @param {string} apiName - The api name of the request
108
	 * @param {string} path - The path of the request
109
	 * @param {json} [init] - Request extra params
110
	 * @return {Promise} - A promise that resolves to an object with response status and JSON data, if successful.
111
	 */
112 2
	put(apiName, path, init): Promise<any> {
113 2
		return this._restApi.put(apiName, path, init);
114
	}
115

116
	/**
117
	 * Make a PATCH request
118
	 * @param {string} apiName - The api name of the request
119
	 * @param {string} path - The path of the request
120
	 * @param {json} [init] - Request extra params
121
	 * @return {Promise} - A promise that resolves to an object with response status and JSON data, if successful.
122
	 */
123 2
	patch(apiName, path, init): Promise<any> {
124 2
		return this._restApi.patch(apiName, path, init);
125
	}
126

127
	/**
128
	 * Make a DEL request
129
	 * @param {string} apiName - The api name of the request
130
	 * @param {string} path - The path of the request
131
	 * @param {json} [init] - Request extra params
132
	 * @return {Promise} - A promise that resolves to an object with response status and JSON data, if successful.
133
	 */
134 2
	del(apiName, path, init): Promise<any> {
135 2
		return this._restApi.del(apiName, path, init);
136
	}
137

138
	/**
139
	 * Make a HEAD request
140
	 * @param {string} apiName - The api name of the request
141
	 * @param {string} path - The path of the request
142
	 * @param {json} [init] - Request extra params
143
	 * @return {Promise} - A promise that resolves to an object with response status and JSON data, if successful.
144
	 */
145 2
	head(apiName, path, init): Promise<any> {
146 2
		return this._restApi.head(apiName, path, init);
147
	}
148

149
	/**
150
	 * Checks to see if an error thrown is from an api request cancellation
151
	 * @param {any} error - Any error
152
	 * @return {boolean} - A boolean indicating if the error was from an api request cancellation
153
	 */
154 2
	isCancel(error) {
155 0
		return this._restApi.isCancel(error);
156
	}
157
	/**
158
	 * Cancels an inflight request
159
	 * @param {any} request - request to cancel
160
	 * @return {boolean} - A boolean indicating if the request was cancelled
161
	 */
162 2
	cancel(request: Promise<any>, message?: string) {
163 0
		return this._restApi.cancel(request, message);
164
	}
165

166
	/**
167
	 * Getting endpoint for API
168
	 * @param {string} apiName - The name of the api
169
	 * @return {string} - The endpoint of the api
170
	 */
171 2
	async endpoint(apiName) {
172 2
		return this._restApi.endpoint(apiName);
173
	}
174

175
	/**
176
	 * to get the operation type
177
	 * @param operation
178
	 */
179 2
	getGraphqlOperationType(operation) {
180 2
		return this._graphqlApi.getGraphqlOperationType(operation);
181
	}
182

183
	/**
184
	 * Executes a GraphQL operation
185
	 *
186
	 * @param {GraphQLOptions} GraphQL Options
187
	 * @param {object} additionalHeaders headers to merge in after any `graphql_headers` set in the config
188
	 * @returns {Promise<GraphQLResult> | Observable<object>}
189
	 */
190 2
	graphql(
191
		options: GraphQLOptions,
192
		additionalHeaders?: { [key: string]: string }
193
	): Promise<GraphQLResult> | Observable<object> {
194 2
		return this._graphqlApi.graphql(options, additionalHeaders);
195
	}
196 2
}
197

198 2
export const API = new APIClass(null);
199 2
Amplify.register(API);

Read our documentation on viewing source code .

Loading