UNPKG

8.46 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(callback: (
89 /**
90 * contains the response parsed as JSON. If the response is not JSON, this field will be false.
91 */
92 response: Response<T>
93 ) => any): void;
94 }
95
96 interface ResponseMap<T> {
97 [id: string]: Response<T>;
98 }
99
100 /**
101 * Represents an HTTP Batch operation. Individual HTTP requests are added with the add method and the batch is executed using execute.
102 */
103 interface Batch<T> extends Promise<Response<ResponseMap<T>>> {
104 /**
105 * Adds a gapi.client.Request to the batch.
106 * @param request The HTTP request to add to this batch.
107 * @param opt_params extra parameters for this batch entry.
108 */
109 add<T>(request: Request<T>, opt_params?: {
110 /**
111 * Identifies the response for this request in the map of batch responses. If one is not provided, the system generates a random ID.
112 */
113 id: string;
114 callback(
115 /**
116 * is the response for this request only. Its format is defined by the API method being called.
117 */
118 individualResponse: Response<T>,
119 /**
120 * is the raw batch ID-response map as a string. It contains all responses to all requests in the batch.
121 */
122 rawBatchResponse: string
123 ): any
124 }): void;
125 /**
126 * Executes all requests in the batch. The supplied callback is executed on success or failure.
127 * @param callback The callback to execute when the batch returns.
128 */
129 execute(callback: (
130 /**
131 * is an ID-response map of each requests response.
132 */
133 responseMap: ResponseMap<T>,
134 /**
135 * is the same response, but as an unparsed JSON-string.
136 */
137 rawBatchResponse: string
138 ) => any): void;
139 }
140
141 /**
142 * Creates a batch object for batching individual requests.
143 */
144 function newBatch(): Batch<any>;
145 }
146
147 namespace auth {
148 /**
149 * The OAuth 2.0 token object represents the OAuth 2.0 token and any associated data.
150 */
151 interface GoogleApiOAuth2TokenObject {
152 /**
153 * The OAuth 2.0 token. Only present in successful responses
154 */
155 access_token: string;
156 /**
157 * Details about the error. Only present in error responses
158 */
159 error: string;
160 /**
161 * The duration, in seconds, the token is valid for. Only present in successful responses
162 */
163 expires_in: string;
164 /**
165 * The Google API scopes related to this token
166 */
167 state: string;
168 }
169
170 /**
171 * Initiates the OAuth 2.0 authorization process. The browser displays a popup window prompting the user authenticate and authorize.
172 * After the user authorizes, the popup closes and the callback function fires.
173 * @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
174 * URI as a query parameter.
175 * @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.
176 */
177 function authorize(
178 params: {
179 /**
180 * The application's client ID. Visit the Google Developers Console to get an OAuth 2.0 client ID.
181 */
182 client_id?: string | undefined;
183 /**
184 * 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.
185 */
186 immediate?: boolean | undefined;
187 /**
188 * The OAuth 2.0 response type property. Default: token
189 */
190 response_type?: string | undefined;
191 /**
192 * The auth scope or scopes to authorize. Auth scopes for individual APIs can be found in their documentation.
193 */
194 scope?: string | string[] | undefined;
195 },
196 callback: (authResult: GoogleApiOAuth2TokenObject) => void): void;
197
198 /**
199 * Initializes the authorization feature. Call this when the client loads to prevent popup blockers from blocking the auth window on gapi.auth.authorize calls.
200 * @param callback A callback to execute when the auth feature is ready to make authorization calls.
201 */
202 function init(callback: () => any): void;
203
204 /**
205 * Retrieves the OAuth 2.0 token for the application.
206 * @return The OAuth 2.0 token.
207 */
208 function getToken(): GoogleApiOAuth2TokenObject;
209
210 /**
211 * Sets the OAuth 2.0 token for the application.
212 * @param token The token to set.
213 */
214 function setToken(token: GoogleApiOAuth2TokenObject): void;
215 }
216}