Skip to content

Commit

Permalink
Merge pull request #61 from yeojz/feature/typescript-def
Browse files Browse the repository at this point in the history
Adding initial typescript definition
  • Loading branch information
yeojz committed Jun 11, 2018
2 parents 33e6242 + 24416cd commit 536b40c
Show file tree
Hide file tree
Showing 24 changed files with 3,303 additions and 3,822 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ coverage
/totp.js
/utils.js
/lib-test.js

audit.txt
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ before_script:
- npm run clean
script:
- npm run test
- npm run lint
- npm run lint:js
after_script:
- npm run coveralls
before_deploy:
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ For example: `feat(browser): description`, `fix: description`, `chore(readme): d

To only run linting:

`npm run lint`
`npm run lint:js`

To run tests and coverage:

Expand Down
7 changes: 6 additions & 1 deletion circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ general:
- gh-pages
test:
pre:
- npm install --no-save typescript
- npm run clean
- npm run build
override:
- npm run test
- npm run lint
- npm run lint:js
- npm run lint:ts
deployment:
coverage:
branch: [master, /^hotfix\/.*$/, /^feature\/.*$/]
Expand All @@ -25,6 +28,7 @@ deployment:
- npx conventional-github-releaser -p angular
- npm run clean
- npm run build
- npm run build:types
- cd dist && npm publish
- git config --global user.email $CI_DEPLOY_EMAIL
- git config --global user.name $CI_DEPLOY_NAME
Expand All @@ -36,4 +40,5 @@ deployment:
- npx conventional-github-releaser -p angular
- npm run clean
- npm run build
- npm run build:types
- cd dist && npm publish --tag next
6,835 changes: 3,048 additions & 3,787 deletions package-lock.json

Large diffs are not rendered by default.

25 changes: 18 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@
"name": "otplib",
"version": "9.0.0",
"description": "HMAC-based (HOTP) and Time-based (TOTP) One-Time Password library",
"main": "otplib.js",
"main": "./index.js",
"types": "./index.d.ts",
"scripts": {
"build": "./scripts/build.sh",
"build:browser": "webpack --config ./scripts/webpack.config.js",
"build:docs": "jsdoc --configure jsdoc.json --verbose",
"build:module": "./scripts/build-module.sh",
"build:rollup": "rollup -c scripts/rollup.config.js",
"build:transpile": "babel dist --out-dir dist",
"build:types": "./scripts/build-types.sh",
"clean": "rimraf dist coverage docs",
"coveralls": "cat ./coverage/lcov.info | coveralls",
"deploy-gh-pages": "./scripts/build-site.sh",
"deploy-package": "./scripts/build-publish.sh",
"lint": "eslint \"packages/**/**\"",
"lint:js": "eslint \"packages/**/**?(.spec|).js\"",
"lint:format": "prettier --write \"{packages,scripts,site/scripts,site/public}/**/**.js\"",
"lint:ts": "dtslint ./packages/types-ts",
"test": "jest --coverage",
"test:watch": "jest --coverage --watch"
},
Expand Down Expand Up @@ -51,6 +54,7 @@
"babel-preset-env": "^1.6.1",
"coveralls": "^3.0.0",
"create-hmac": "^1.1.4",
"dtslint": "^0.3.0",
"eslint": "^4.18.2",
"eslint-config-prettier": "^2.9.0",
"eslint-plugin-prettier": "^2.6.0",
Expand All @@ -62,27 +66,34 @@
"rollup": "^0.59.0",
"rollup-plugin-cleanup": "^2.0.0",
"rollup-plugin-node-resolve": "^3.0.0",
"webpack": "^4.0.1",
"webpack-cli": "^2.0.10"
"webpack": "^4.12.0",
"webpack-cli": "^3.0.3"
},
"dependencies": {
"thirty-two": "1.0.2"
},
"jest": {
"coverageDirectory": "./coverage/",
"coveragePathIgnorePatterns": [
"<rootDir>/scripts/*"
"<rootDir>/scripts/*",
"<rootDir>/dist/*"
],
"modulePaths": [
"<rootDir>/packages/"
],
"resetMocks": true,
"setupFiles": [],
"testPathIgnorePatterns": [
"/dist/",
"/node_modules/",
"<rootDir>/dist/*",
"<rootDir>/scripts/*",
"<rootDir>/tests/"
]
}
},
"repl": [
{
"name": "otplib",
"module": "./dist/index"
}
]
}
9 changes: 9 additions & 0 deletions packages/otplib-authenticator/Authenticator.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ class Authenticator extends TOTP {
super();
}

/**
* returns an Authenticator class
*
* @return {class}
*/
getClass() {
return Authenticator;
}

/**
* getter for defaultOptions
*
Expand Down
4 changes: 4 additions & 0 deletions packages/otplib-authenticator/Authenticator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ describe('Authenticator', () => {
expect(lib.Authenticator).toEqual(Authenticator);
});

it('method: getClass returns the class', () => {
expect(lib.getClass()).toEqual(Authenticator);
});

it('exposes authenticator functions as utils', () => {
expect(Object.keys(lib.utils)).toEqual([
'check',
Expand Down
9 changes: 9 additions & 0 deletions packages/otplib-hotp/HOTP.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ class HOTP {
this._options = this.defaultOptions;
}

/**
* returns a HOTP class
*
* @return {class}
*/
getClass() {
return HOTP;
}

/**
* getter for defaultOptions
*
Expand Down
4 changes: 4 additions & 0 deletions packages/otplib-hotp/HOTP.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ describe('HOTP', () => {
expect(lib.HOTP).toEqual(HOTP);
});

it('method: getClass returns the class', () => {
expect(lib.getClass()).toEqual(HOTP);
});

it('should have expected default options', () => {
const options = lib.options;
expect(options).toEqual({});
Expand Down
9 changes: 9 additions & 0 deletions packages/otplib-totp/TOTP.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ class TOTP extends HOTP {
super();
}

/**
* returns a TOTP class
*
* @return {class}
*/
getClass() {
return TOTP;
}

/**
* getter for defaultOptions
*
Expand Down
4 changes: 4 additions & 0 deletions packages/otplib-totp/TOTP.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ describe('TOTP', () => {
expect(lib.TOTP).toEqual(TOTP);
});

it('method: getClass returns the class', () => {
expect(lib.getClass()).toEqual(TOTP);
});

it('should have expected default options', () => {
const options = lib.options;
expect(options).toEqual({
Expand Down
7 changes: 0 additions & 7 deletions packages/otplib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ import crypto from 'crypto';
*
* ```js
* {
* Authenticator // class
* HOTP // class
* TOTP // class
*
* authenticator // instance
* hotp // instance
* totp // instance
Expand All @@ -28,9 +24,6 @@ hotp.options = { crypto };
totp.options = { crypto };

export default {
Authenticator: authenticator.Authenticator,
HOTP: hotp.HOTP,
TOTP: totp.TOTP,
authenticator,
hotp,
totp
Expand Down
12 changes: 0 additions & 12 deletions packages/otplib/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,14 @@ import authenticator from 'otplib-authenticator';
import otplib from './index';

describe('index', () => {
it('should expose hotp class', () => {
expect(otplib.HOTP).toEqual(hotp.HOTP);
});

it('should expose an instance of HOTP', () => {
expect(otplib.hotp).toBeInstanceOf(hotp.HOTP);
});

it('should expose totp class', () => {
expect(otplib.TOTP).toEqual(totp.TOTP);
});

it('should expose an instance of TOTP', () => {
expect(otplib.totp).toBeInstanceOf(totp.TOTP);
});

it('should expose authenticator class', () => {
expect(otplib.Authenticator).toEqual(authenticator.Authenticator);
});

it('should expose an instance of Authenticator', () => {
expect(otplib.authenticator).toBeInstanceOf(authenticator.Authenticator);
});
Expand Down
20 changes: 20 additions & 0 deletions packages/types-ts/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
declare module 'otplib' {
const authenticator: Authenticator;
const hotp: HOTP;
const totp: TOTP;
}

declare module 'otplib/authenticator' {
const authenticator: Authenticator;
export = authenticator;
}

declare module 'otplib/totp' {
const totp: TOTP;
export = totp;
}

declare module 'otplib/hotp' {
const hotp: HOTP;
export = hotp;
}
50 changes: 50 additions & 0 deletions packages/types-ts/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import * as otplib from 'otplib';
import authenticator = require('otplib/authenticator');
import hotp = require('otplib/hotp');
import totp = require('otplib/totp');

const SECRET = '1234567890';
let secret = '';
let token = '';

secret = otplib.authenticator.generateSecret(20);
token = otplib.authenticator.generate(secret);

otplib.authenticator.check(token, secret); // $ExpectType boolean
otplib.authenticator.checkDelta(token, secret); // $ExpectType number | null
otplib.authenticator.decode('test'); // $ExpectType string
otplib.authenticator.encode('test'); // $ExpectType string
otplib.authenticator.verify({ secret, token }); // $ExpectType boolean
otplib.authenticator.keyuri('me', 'otplib-test', secret); // $ExpectType string
otplib.authenticator.Authenticator;
otplib.authenticator.getClass();
authenticator.check(token, secret); // $ExpectType boolean
authenticator.checkDelta(token, secret); // $ExpectType number | null
authenticator.decode('test'); // $ExpectType string
authenticator.encode('test'); // $ExpectType string
authenticator.verify({ secret, token }); // $ExpectType boolean
authenticator.keyuri('me', 'otplib-test', secret); // $ExpectType string
authenticator.Authenticator;
authenticator.getClass();

token = otplib.totp.generate(SECRET);
otplib.totp.check(token, SECRET); // $ExpectType boolean
otplib.totp.checkDelta(token, SECRET); // $ExpectType number | null
otplib.totp.verify({ secret: SECRET, token }); // $ExpectType boolean
otplib.totp.TOTP;
otplib.totp.getClass();
totp.check(token, SECRET); // $ExpectType boolean
totp.checkDelta(token, SECRET); // $ExpectType number | null
totp.verify({ secret: SECRET, token }); // $ExpectType boolean
totp.TOTP;
totp.getClass();

token = otplib.hotp.generate(SECRET, 1);
otplib.hotp.check(token, SECRET, 1); // $ExpectType boolean
otplib.hotp.verify({ secret: SECRET, token, counter: 1 }); // $ExpectType boolean
otplib.hotp.HOTP;
otplib.hotp.getClass();
hotp.check(token, SECRET, 1); // $ExpectType boolean
hotp.verify({ secret: SECRET, token, counter: 1 }); // $ExpectType boolean
hotp.HOTP;
hotp.getClass();
32 changes: 32 additions & 0 deletions packages/types-ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"compilerOptions": {
"target": "es2015",
"module": "commonjs",
"sourceMap": true,
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"lib": [
"es2015"
],
"baseUrl": "../../dist",
"paths": {
"otplib": [
"index"
],
"otplib/hotp": [
"hotp"
],
"otplib/totp": [
"totp"
],
"otplib/authenticator": [
"authenticator"
]
}
},
"exclude": [
"node_modules"
]
}
3 changes: 3 additions & 0 deletions packages/types-ts/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "dtslint/dtslint.json"
}
Loading

0 comments on commit 536b40c

Please sign in to comment.