Skip to content
This repository was archived by the owner on Jan 20, 2024. It is now read-only.

Commit 3ce8b3f

Browse files
committed
Write TypeScript definition for library
1 parent 0636176 commit 3ce8b3f

File tree

6 files changed

+126
-32
lines changed

6 files changed

+126
-32
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ var githubAuth = new ClientOAuth2({
2424
clientSecret: '123',
2525
accessTokenUri: 'https://github.com/login/oauth/access_token',
2626
authorizationUri: 'https://github.com/login/oauth/authorize',
27-
authorizationGrants: ['credentials'],
2827
redirectUri: 'http://example.com/auth/github/callback',
2928
scopes: ['notifications', 'gist']
3029
})

index.d.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
declare class ClientOAuth2 {
2+
code: ClientOAuth2.CodeFlow;
3+
token: ClientOAuth2.TokenFlow;
4+
owner: ClientOAuth2.OwnerFlow;
5+
credentials: ClientOAuth2.CredentialsFlow;
6+
jwt: ClientOAuth2.JwtBearerFlow;
7+
8+
constructor(options: ClientOAuth2.Options, request: ClientOAuth2.Request);
9+
10+
createToken(data: ClientOAuth2.Data): ClientOAuth2.Token;
11+
createToken(accessToken: string, data: ClientOAuth2.Data): ClientOAuth2.Token;
12+
createToken(accessToken: string, refreshToken: string, data: ClientOAuth2.Data): ClientOAuth2.Token;
13+
createToken(accessToken: string, refreshToken: string, type: string, data: ClientOAuth2.Data): ClientOAuth2.Token;
14+
}
15+
16+
declare namespace ClientOAuth2 {
17+
export interface Data {
18+
[key: string]: string
19+
}
20+
21+
export interface Options {
22+
clientId?: string
23+
clientSecret?: string
24+
accessTokenUri?: string
25+
authorizationUri?: string
26+
redirectUri?: string
27+
scopes?: string
28+
state?: string
29+
body?: {
30+
[key: string]: string | string[];
31+
};
32+
query?: {
33+
[key: string]: string | string[];
34+
};
35+
headers?: {
36+
[key: string]: string | string[];
37+
};
38+
}
39+
40+
export interface Request {
41+
(method: string, url: string, body: string, headers: { [key: string]: string | string[] }): Promise<{ status: number, body: string }>;
42+
}
43+
44+
export interface RequestObject {
45+
url: string;
46+
headers?: {
47+
[key: string]: string | string[];
48+
};
49+
}
50+
51+
export class Token {
52+
client: ClientOAuth2;
53+
data: Data;
54+
tokenType: string;
55+
accessToken: string;
56+
refreshToken: string;
57+
58+
constructor(client: ClientOAuth2, data: Data);
59+
expiresIn(duration: number | Date): Date;
60+
sign<T extends RequestObject>(requestObj: T): T;
61+
refresh(options?: Options): Promise<Token>;
62+
expired(): boolean;
63+
}
64+
65+
export class CodeFlow {
66+
constructor(client: ClientOAuth2);
67+
getUri(options?: Options): string;
68+
getToken(uri: string, options?: Options): Promise<Token>;
69+
}
70+
71+
export class TokenFlow {
72+
constructor(client: ClientOAuth2);
73+
getUri(options?: Options): string;
74+
getToken(uri: string, options?: Options): Promise<Token>;
75+
}
76+
77+
export class OwnerFlow {
78+
constructor(client: ClientOAuth2);
79+
getToken(username: string, password: string, options?: Options): Promise<Token>;
80+
}
81+
82+
export class CredentialsFlow {
83+
constructor(client: ClientOAuth2);
84+
getToken(options?: Options): Promise<Token>;
85+
}
86+
87+
export class JwtBearerFlow {
88+
constructor(client: ClientOAuth2);
89+
getToken(token: string, options?: Options): Promise<Token>;
90+
}
91+
}
92+
93+
export = ClientOAuth2;

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
"version": "3.2.0",
44
"description": "Straight-forward execution of OAuth 2.0 flows and authenticated API requests",
55
"main": "src/client-oauth2.js",
6+
"typings": "index.d.ts",
67
"files": [
78
"src/",
9+
"index.d.ts",
810
"LICENSE"
911
],
1012
"browser": {

src/client-oauth2.js

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ var ERROR_RESPONSES = {
7676
/**
7777
* Support base64 in node like how it works in the browser.
7878
*
79-
* @param {String} string
80-
* @return {String}
79+
* @param {string} string
80+
* @return {string}
8181
*/
8282
function btoaBuffer (string) {
8383
return new Buffer(string).toString('base64')
@@ -105,7 +105,7 @@ function expects (obj, props) {
105105
* Pull an authentication error from the response data.
106106
*
107107
* @param {Object} data
108-
* @return {String}
108+
* @return {string}
109109
*/
110110
function getAuthError (body) {
111111
var message = ERROR_RESPONSES[body.error] ||
@@ -123,7 +123,7 @@ function getAuthError (body) {
123123
/**
124124
* Attempt to parse response body as JSON, fall back to parsing as a query string.
125125
*
126-
* @param {String} body
126+
* @param {string} body
127127
* @return {Object}
128128
*/
129129
function parseResponseBody (body) {
@@ -138,7 +138,7 @@ function parseResponseBody (body) {
138138
* Sanitize the scopes option to be a string.
139139
*
140140
* @param {Array} scopes
141-
* @return {String}
141+
* @return {string}
142142
*/
143143
function sanitizeScope (scopes) {
144144
return Array.isArray(scopes) ? scopes.join(' ') : toString(scopes)
@@ -148,8 +148,8 @@ function sanitizeScope (scopes) {
148148
* Create a request uri based on an options object and token type.
149149
*
150150
* @param {Object} options
151-
* @param {String} tokenType
152-
* @return {String}
151+
* @param {string} tokenType
152+
* @return {string}
153153
*/
154154
function createUri (options, tokenType) {
155155
// Check the required parameters are set.
@@ -174,9 +174,9 @@ function createUri (options, tokenType) {
174174
/**
175175
* Create basic auth header.
176176
*
177-
* @param {String} username
178-
* @param {String} password
179-
* @return {String}
177+
* @param {string} username
178+
* @param {string} password
179+
* @return {string}
180180
*/
181181
function auth (username, password) {
182182
return 'Basic ' + btoa(toString(username) + ':' + toString(password))
@@ -185,8 +185,8 @@ function auth (username, password) {
185185
/**
186186
* Ensure a value is a string.
187187
*
188-
* @param {String} str
189-
* @return {String}
188+
* @param {string} str
189+
* @return {string}
190190
*/
191191
function toString (str) {
192192
return str == null ? '' : String(str)
@@ -229,9 +229,9 @@ ClientOAuth2.Token = ClientOAuth2Token
229229
/**
230230
* Create a new token from existing data.
231231
*
232-
* @param {String} access
233-
* @param {String} [refresh]
234-
* @param {String} [type]
232+
* @param {string} access
233+
* @param {string} [refresh]
234+
* @param {string} [type]
235235
* @param {Object} [data]
236236
* @return {Object}
237237
*/
@@ -302,7 +302,7 @@ function ClientOAuth2Token (client, data) {
302302
/**
303303
* Expire the token after some time.
304304
*
305-
* @param {Number|Date} duration Seconds from now to expire, or a date to expire on.
305+
* @param {number|Date} duration Seconds from now to expire, or a date to expire on.
306306
* @return {Date}
307307
*/
308308
ClientOAuth2Token.prototype.expiresIn = function (duration) {
@@ -384,7 +384,7 @@ ClientOAuth2Token.prototype.refresh = function (options) {
384384
/**
385385
* Check whether the token has expired.
386386
*
387-
* @return {Boolean}
387+
* @return {boolean}
388388
*/
389389
ClientOAuth2Token.prototype.expired = function () {
390390
return Date.now() > this.expires.getTime()
@@ -404,8 +404,8 @@ function OwnerFlow (client) {
404404
/**
405405
* Make a request on behalf of the user credentials to get an acces token.
406406
*
407-
* @param {String} username
408-
* @param {String} password
407+
* @param {string} username
408+
* @param {string} password
409409
* @return {Promise}
410410
*/
411411
OwnerFlow.prototype.getToken = function (username, password, options) {
@@ -446,7 +446,7 @@ function TokenFlow (client) {
446446
* Get the uri to redirect the user to for implicit authentication.
447447
*
448448
* @param {Object} options
449-
* @return {String}
449+
* @return {string}
450450
*/
451451
TokenFlow.prototype.getUri = function (options) {
452452
options = extend(this.client.options, options)
@@ -457,7 +457,7 @@ TokenFlow.prototype.getUri = function (options) {
457457
/**
458458
* Get the user access token from the uri.
459459
*
460-
* @param {String} uri
460+
* @param {string} uri
461461
* @param {Object} [options]
462462
* @return {Promise}
463463
*/
@@ -559,7 +559,7 @@ function CodeFlow (client) {
559559
/**
560560
* Generate the uri for doing the first redirect.
561561
*
562-
* @return {String}
562+
* @return {string}
563563
*/
564564
CodeFlow.prototype.getUri = function (options) {
565565
options = extend(this.client.options, options)
@@ -571,7 +571,7 @@ CodeFlow.prototype.getUri = function (options) {
571571
* Get the code token from the redirected uri and make another request for
572572
* the user access token.
573573
*
574-
* @param {String} uri
574+
* @param {string} uri
575575
* @param {Object} [options]
576576
* @return {Promise}
577577
*/
@@ -645,8 +645,8 @@ function JwtBearerFlow (client) {
645645
/**
646646
* Request an access token using a JWT token.
647647
*
648-
* @param {string} token A JWT token.
649-
* @param {Object} [options]
648+
* @param {string} token A JWT token.
649+
* @param {Object} [options]
650650
* @return {Promise}
651651
*/
652652
JwtBearerFlow.prototype.getToken = function (token, options) {

src/request/browser.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/**
22
* Make a request using `XMLHttpRequest`.
33
*
4-
* @param {String} method
5-
* @param {String} url
6-
* @param {String} body
4+
* @param {string} method
5+
* @param {string} url
6+
* @param {string} body
77
* @param {Object} headers
88
* @returns {Promise}
99
*/

src/request/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ var popsicle = require('popsicle')
33
/**
44
* Make a request using node.
55
*
6-
* @param {String} method
7-
* @param {String} url
8-
* @param {String} body
6+
* @param {string} method
7+
* @param {string} url
8+
* @param {string} body
99
* @param {Object} headers
1010
* @returns {Promise}
1111
*/

0 commit comments

Comments
 (0)