UNPKG

8.6 kBTypeScriptView Raw
1// Type definitions for non-npm package Google API client 1.0
2// Project: https://developers.google.com
3// Definitions by: Maxim Mazurok <https://github.com/Maxim-Mazurok>
4// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5// TypeScript Version: 2.3
6
7declare namespace gapi {
8 /**
9 * Pragmatically initialize gapi class member.
10 */
11 function load(api: string, callback: () => void): void;
12
13 namespace client {
14 /**
15 * Loads the client library interface to a particular API. The new API interface will be in the form gapi.client.api.collection.method.
16 * @param name The name of the API to load.
17 * @param version The version of the API to load
18 * @param callback the function that is called once the API interface is loaded
19 */
20 function load(name: string, version: string, callback: () => any): void;
21 function load(name: string, version: string): Promise<void>;
22
23 /**
24 * Creates a HTTP request for making RESTful requests.
25 * An object encapsulating the various arguments for this method.
26 */
27 function request(args: {
28 /**
29 * The URL to handle the request
30 */
31 path: string;
32 /**
33 * The HTTP request method to use. Default is GET
34 */
35 method?: string | undefined;
36 /**
37 * URL params in key-value pair form
38 */
39 params?: any;
40 /**
41 * Additional HTTP request headers
42 */
43 headers?: any;
44 /**
45 * The HTTP request body (applies to PUT or POST).
46 */
47 body?: any;
48 // /**
49 // * If supplied, the request is executed immediately and no gapi.client.HttpRequest object is returned
50 // */
51 // callback?: () => any;
52 }): Request<any>;
53
54 /**
55 * Sets the API key for the application.
56 * @param apiKey The API key to set
57 */
58 function setApiKey(apiKey: string): void;
59
60 /**
61 * An object containing information about the HTTP response
62 */
63 interface Response<T> {
64 // The JSON-parsed result.
65 result: T;
66
67 // The raw response string.
68 body: string;
69
70 // The map of HTTP response headers.
71 headers?: { [headerName: string]: string } | undefined;
72
73 // HTTP status
74 status?: number | undefined;
75
76 // HTTP status text
77 statusText?: string | undefined;
78 }
79
80 /**
81 * An object encapsulating an HTTP request. This object is not instantiated directly, rather it is returned by gapi.client.request.
82 */
83 interface Request<T> extends Promise<Response<T>> {
84 /**
85 * Executes the request and runs the supplied callback on response.
86 * @param callback The callback function which executes when the request succeeds or fails.
87 */
88 execute(
89 callback: (
90 /**
91 * contains the response parsed as JSON. If the response is not JSON, this field will be false.
92 */
93 response: Response<T>,
94 ) => any,
95 ): void;
96 }
97
98 interface ResponseMap<T> {
99 [id: string]: Response<T>;
100 }
101
102 /**
103 * Represents an HTTP Batch operation. Individual HTTP requests are added with the add method and the batch is executed using execute.
104 */
105 interface Batch<T> extends Promise<Response<ResponseMap<T>>> {
106 /**
107 * Adds a gapi.client.Request to the batch.
108 * @param request The HTTP request to add to this batch.
109 * @param opt_params extra parameters for this batch entry.
110 */
111 add<T>(request: Request<T>, opt_params?: {
112 /**
113 * Identifies the response for this request in the map of batch responses. If one is not provided, the system generates a random ID.
114 */
115 id: string;
116 callback(
117 /**
118 * is the response for this request only. Its format is defined by the API method being called.
119 */
120 individualResponse: Response<T>,
121 /**
122 * is the raw batch ID-response map as a string. It contains all responses to all requests in the batch.
123 */
124 rawBatchResponse: string,
125 ): any;
126 }): void;
127 /**
128 * Executes all requests in the batch. The supplied callback is executed on success or failure.
129 * @param callback The callback to execute when the batch returns.
130 */
131 execute(
132 callback: (
133 /**
134 * is an ID-response map of each requests response.
135 */
136 responseMap: ResponseMap<T>,
137 /**
138 * is the same response, but as an unparsed JSON-string.
139 */
140 rawBatchResponse: string,
141 ) => any,
142 ): void;
143 }
144
145 /**
146 * Creates a batch object for batching individual requests.
147 */
148 function newBatch(): Batch<any>;
149 }
150
151 namespace auth {
152 /**
153 * The OAuth 2.0 token object represents the OAuth 2.0 token and any associated data.
154 */
155 interface GoogleApiOAuth2TokenObject {
156 /**
157 * The OAuth 2.0 token. Only present in successful responses
158 */
159 access_token: string;
160 /**
161 * Details about the error. Only present in error responses
162 */
163 error: string;
164 /**
165 * The duration, in seconds, the token is valid for. Only present in successful responses
166 */
167 expires_in: string;
168 /**
169 * The Google API scopes related to this token
170 */
171 state: string;
172 }
173
174 /**
175 * Initiates the OAuth 2.0 authorization process. The browser displays a popup window prompting the user authenticate and authorize.
176 * After the user authorizes, the popup closes and the callback function fires.
177 * @param params A key/value map of parameters for the request. If the key is not one of the expected OAuth 2.0 parameters, it is added to the
178 * URI as a query parameter.
179 * @param callback The function to call once the login process is complete. The function takes an OAuth 2.0 token object as its only parameter.
180 */
181 function authorize(
182 params: {
183 /**
184 * The application's client ID. Visit the Google Developers Console to get an OAuth 2.0 client ID.
185 */
186 client_id?: string | undefined;
187 /**
188 * If true, then login uses "immediate mode", which means that the token is refreshed behind the scenes, and no UI is shown to the user.
189 */
190 immediate?: boolean | undefined;
191 /**
192 * The OAuth 2.0 response type property. Default: token
193 */
194 response_type?: string | undefined;
195 /**
196 * The auth scope or scopes to authorize. Auth scopes for individual APIs can be found in their documentation.
197 */
198 scope?: string | string[] | undefined;
199 },
200 callback: (authResult: GoogleApiOAuth2TokenObject) => void,
201 ): void;
202
203 /**
204 * Initializes the authorization feature. Call this when the client loads to prevent popup blockers from blocking the auth window on gapi.auth.authorize calls.
205 * @param callback A callback to execute when the auth feature is ready to make authorization calls.
206 */
207 function init(callback: () => any): void;
208
209 /**
210 * Retrieves the OAuth 2.0 token for the application.
211 * @return The OAuth 2.0 token.
212 */
213 function getToken(): GoogleApiOAuth2TokenObject;
214
215 /**
216 * Sets the OAuth 2.0 token for the application.
217 * @param token The token to set.
218 */
219 function setToken(token: GoogleApiOAuth2TokenObject): void;
220 }
221}