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

Commit

Permalink
Write TypeScript definition for library
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Dec 30, 2016
1 parent 0636176 commit 3ce8b3f
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 32 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ var githubAuth = new ClientOAuth2({
clientSecret: '123',
accessTokenUri: 'https://github.com/login/oauth/access_token',
authorizationUri: 'https://github.com/login/oauth/authorize',
authorizationGrants: ['credentials'],
redirectUri: 'http://example.com/auth/github/callback',
scopes: ['notifications', 'gist']
})
Expand Down
93 changes: 93 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
declare class ClientOAuth2 {
code: ClientOAuth2.CodeFlow;
token: ClientOAuth2.TokenFlow;
owner: ClientOAuth2.OwnerFlow;
credentials: ClientOAuth2.CredentialsFlow;
jwt: ClientOAuth2.JwtBearerFlow;

constructor(options: ClientOAuth2.Options, request: ClientOAuth2.Request);

createToken(data: ClientOAuth2.Data): ClientOAuth2.Token;
createToken(accessToken: string, data: ClientOAuth2.Data): ClientOAuth2.Token;
createToken(accessToken: string, refreshToken: string, data: ClientOAuth2.Data): ClientOAuth2.Token;
createToken(accessToken: string, refreshToken: string, type: string, data: ClientOAuth2.Data): ClientOAuth2.Token;
}

declare namespace ClientOAuth2 {
export interface Data {
[key: string]: string
}

export interface Options {
clientId?: string
clientSecret?: string
accessTokenUri?: string
authorizationUri?: string
redirectUri?: string
scopes?: string
state?: string
body?: {
[key: string]: string | string[];
};
query?: {
[key: string]: string | string[];
};
headers?: {
[key: string]: string | string[];
};
}

export interface Request {
(method: string, url: string, body: string, headers: { [key: string]: string | string[] }): Promise<{ status: number, body: string }>;
}

export interface RequestObject {
url: string;
headers?: {
[key: string]: string | string[];
};
}

export class Token {
client: ClientOAuth2;
data: Data;
tokenType: string;
accessToken: string;
refreshToken: string;

constructor(client: ClientOAuth2, data: Data);
expiresIn(duration: number | Date): Date;
sign<T extends RequestObject>(requestObj: T): T;
refresh(options?: Options): Promise<Token>;
expired(): boolean;
}

export class CodeFlow {
constructor(client: ClientOAuth2);
getUri(options?: Options): string;
getToken(uri: string, options?: Options): Promise<Token>;
}

export class TokenFlow {
constructor(client: ClientOAuth2);
getUri(options?: Options): string;
getToken(uri: string, options?: Options): Promise<Token>;
}

export class OwnerFlow {
constructor(client: ClientOAuth2);
getToken(username: string, password: string, options?: Options): Promise<Token>;
}

export class CredentialsFlow {
constructor(client: ClientOAuth2);
getToken(options?: Options): Promise<Token>;
}

export class JwtBearerFlow {
constructor(client: ClientOAuth2);
getToken(token: string, options?: Options): Promise<Token>;
}
}

export = ClientOAuth2;
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
"version": "3.2.0",
"description": "Straight-forward execution of OAuth 2.0 flows and authenticated API requests",
"main": "src/client-oauth2.js",
"typings": "index.d.ts",
"files": [
"src/",
"index.d.ts",
"LICENSE"
],
"browser": {
Expand Down
50 changes: 25 additions & 25 deletions src/client-oauth2.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ var ERROR_RESPONSES = {
/**
* Support base64 in node like how it works in the browser.
*
* @param {String} string
* @return {String}
* @param {string} string
* @return {string}
*/
function btoaBuffer (string) {
return new Buffer(string).toString('base64')
Expand Down Expand Up @@ -105,7 +105,7 @@ function expects (obj, props) {
* Pull an authentication error from the response data.
*
* @param {Object} data
* @return {String}
* @return {string}
*/
function getAuthError (body) {
var message = ERROR_RESPONSES[body.error] ||
Expand All @@ -123,7 +123,7 @@ function getAuthError (body) {
/**
* Attempt to parse response body as JSON, fall back to parsing as a query string.
*
* @param {String} body
* @param {string} body
* @return {Object}
*/
function parseResponseBody (body) {
Expand All @@ -138,7 +138,7 @@ function parseResponseBody (body) {
* Sanitize the scopes option to be a string.
*
* @param {Array} scopes
* @return {String}
* @return {string}
*/
function sanitizeScope (scopes) {
return Array.isArray(scopes) ? scopes.join(' ') : toString(scopes)
Expand All @@ -148,8 +148,8 @@ function sanitizeScope (scopes) {
* Create a request uri based on an options object and token type.
*
* @param {Object} options
* @param {String} tokenType
* @return {String}
* @param {string} tokenType
* @return {string}
*/
function createUri (options, tokenType) {
// Check the required parameters are set.
Expand All @@ -174,9 +174,9 @@ function createUri (options, tokenType) {
/**
* Create basic auth header.
*
* @param {String} username
* @param {String} password
* @return {String}
* @param {string} username
* @param {string} password
* @return {string}
*/
function auth (username, password) {
return 'Basic ' + btoa(toString(username) + ':' + toString(password))
Expand All @@ -185,8 +185,8 @@ function auth (username, password) {
/**
* Ensure a value is a string.
*
* @param {String} str
* @return {String}
* @param {string} str
* @return {string}
*/
function toString (str) {
return str == null ? '' : String(str)
Expand Down Expand Up @@ -229,9 +229,9 @@ ClientOAuth2.Token = ClientOAuth2Token
/**
* Create a new token from existing data.
*
* @param {String} access
* @param {String} [refresh]
* @param {String} [type]
* @param {string} access
* @param {string} [refresh]
* @param {string} [type]
* @param {Object} [data]
* @return {Object}
*/
Expand Down Expand Up @@ -302,7 +302,7 @@ function ClientOAuth2Token (client, data) {
/**
* Expire the token after some time.
*
* @param {Number|Date} duration Seconds from now to expire, or a date to expire on.
* @param {number|Date} duration Seconds from now to expire, or a date to expire on.
* @return {Date}
*/
ClientOAuth2Token.prototype.expiresIn = function (duration) {
Expand Down Expand Up @@ -384,7 +384,7 @@ ClientOAuth2Token.prototype.refresh = function (options) {
/**
* Check whether the token has expired.
*
* @return {Boolean}
* @return {boolean}
*/
ClientOAuth2Token.prototype.expired = function () {
return Date.now() > this.expires.getTime()
Expand All @@ -404,8 +404,8 @@ function OwnerFlow (client) {
/**
* Make a request on behalf of the user credentials to get an acces token.
*
* @param {String} username
* @param {String} password
* @param {string} username
* @param {string} password
* @return {Promise}
*/
OwnerFlow.prototype.getToken = function (username, password, options) {
Expand Down Expand Up @@ -446,7 +446,7 @@ function TokenFlow (client) {
* Get the uri to redirect the user to for implicit authentication.
*
* @param {Object} options
* @return {String}
* @return {string}
*/
TokenFlow.prototype.getUri = function (options) {
options = extend(this.client.options, options)
Expand All @@ -457,7 +457,7 @@ TokenFlow.prototype.getUri = function (options) {
/**
* Get the user access token from the uri.
*
* @param {String} uri
* @param {string} uri
* @param {Object} [options]
* @return {Promise}
*/
Expand Down Expand Up @@ -559,7 +559,7 @@ function CodeFlow (client) {
/**
* Generate the uri for doing the first redirect.
*
* @return {String}
* @return {string}
*/
CodeFlow.prototype.getUri = function (options) {
options = extend(this.client.options, options)
Expand All @@ -571,7 +571,7 @@ CodeFlow.prototype.getUri = function (options) {
* Get the code token from the redirected uri and make another request for
* the user access token.
*
* @param {String} uri
* @param {string} uri
* @param {Object} [options]
* @return {Promise}
*/
Expand Down Expand Up @@ -645,8 +645,8 @@ function JwtBearerFlow (client) {
/**
* Request an access token using a JWT token.
*
* @param {string} token A JWT token.
* @param {Object} [options]
* @param {string} token A JWT token.
* @param {Object} [options]
* @return {Promise}
*/
JwtBearerFlow.prototype.getToken = function (token, options) {
Expand Down
6 changes: 3 additions & 3 deletions src/request/browser.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* Make a request using `XMLHttpRequest`.
*
* @param {String} method
* @param {String} url
* @param {String} body
* @param {string} method
* @param {string} url
* @param {string} body
* @param {Object} headers
* @returns {Promise}
*/
Expand Down
6 changes: 3 additions & 3 deletions src/request/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ var popsicle = require('popsicle')
/**
* Make a request using node.
*
* @param {String} method
* @param {String} url
* @param {String} body
* @param {string} method
* @param {string} url
* @param {string} body
* @param {Object} headers
* @returns {Promise}
*/
Expand Down

0 comments on commit 3ce8b3f

Please sign in to comment.