UNPKG

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