Skip to content

Commit

Permalink
feat: (:label:) use typescript for development
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Tian committed Jul 22, 2019
1 parent c06527c commit 69c410e
Show file tree
Hide file tree
Showing 16 changed files with 149 additions and 34 deletions.
5 changes: 4 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ jobs:
- run:
name: lint
command: npm run lint
- run:
name: build
command: npm run build
- run:
name: test
command: npm test
Expand All @@ -25,4 +28,4 @@ jobs:
key: node-v1-{{ .Branch }}-{{ checksum "package-lock.json" }}
- run:
name: release
command: npm run semantic-release || true
command: npm run semantic-release
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
node_modules/
cover/
.idea
.idea
lib/useragent.d.ts
lib/useragent.js
index.d.ts
index.js
19 changes: 10 additions & 9 deletions index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import UserAgent from './lib/useragent'

export default async (ctx: any, next: () => Promise<any>) => {
const {header} = ctx.request
const source = header['user-agent']

ctx.userAgent = new UserAgent(source)

await next()
}
17 changes: 9 additions & 8 deletions lib/useragent.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions lib/useragent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const useragent = require('express-useragent-x');

export default class UserAgent {
private readonly agent: any

constructor(source = 'unknown') {
this.agent = useragent.parse(source);

return this.agent;
}
}

module.exports = UserAgent;
58 changes: 53 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
"main": "index.js",
"scripts": {
"test": "ava",
"lint": "eslint ./",
"semantic-release": "semantic-release"
"lint": "tslint ./",
"build": "(rm index.d.ts index.js lib/useragent.d.ts lib/useragent.js || echo 'deleted') && tsc --declaration -p tsconfig.json",
"ci": "npm run build && npm test",
"semantic-release": "semantic-release --branch temp/x"
},
"keywords": [
"koa",
Expand All @@ -30,7 +32,9 @@
"husky": "^2.7.0",
"koa": "^2.7.0",
"semantic-release": "^15.13.18",
"supertest": "^4.0.2"
"supertest": "^4.0.2",
"tslint": "^5.18.0",
"typescript": "3.5.1"
},
"engines": {
"node": ">=7.6.0"
Expand Down
2 changes: 1 addition & 1 deletion test/android-phone.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* */

const test = require('ava');
const UserAgent = require('../lib/useragent');
const UserAgent = require('../lib/useragent.js');

test('Andriod Phone', (t) => {
let source = '';
Expand Down
2 changes: 1 addition & 1 deletion test/android-tablet.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* */

const test = require('ava');
const UserAgent = require('../lib/useragent');
const UserAgent = require('../lib/useragent.js');

test('Andriod Tablet', (t) => {
let source = '';
Expand Down
4 changes: 3 additions & 1 deletion test/bots.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
* @author Luiz Freneda <lfreneda@gmail.com>
*/
const test = require('ava');
const UserAgent = require('../lib/useragent');
const UserAgent = require('../lib/useragent.js');

console.log(UserAgent)

test('Arbitrary bot UA', (t) => {
const source = 'sockbot/3.1.0-RC1 (Linux x86_64) (nodejs 5.10.1) (owner:fred user:george)';
Expand Down
2 changes: 1 addition & 1 deletion test/browsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

const test = require('ava');
const UserAgent = require('../lib/useragent');
const UserAgent = require('../lib/useragent.js');

test('iPad 2', (t) => {
const s = 'Mozilla/5.0 (iPad; CPU OS 5_0 like Mac OS X) AppleWebKit/534.46'
Expand Down
2 changes: 1 addition & 1 deletion test/ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

const test = require('ava');
const UserAgent = require('../lib/useragent');
const UserAgent = require('../lib/useragent.js');

test('iPad 2', (t) => {
const source = 'Mozilla/5.0 (iPad; CPU OS 5_0 like Mac OS X) AppleWebKit/534.46'
Expand Down
2 changes: 1 addition & 1 deletion test/koa.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const test = require('ava');
const request = require('supertest');
const Koa = require('koa');

const userAgent = require('../');
const userAgent = require('../index.js').default;

test('koa middleware', async (t) => {
const app = new Koa();
Expand Down
2 changes: 1 addition & 1 deletion test/slik.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const test = require('ava');
const UserAgent = require('../lib/useragent');
const UserAgent = require('../lib/useragent.js');

test('Macintosh Silk', (t) => {
const source = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-us; Silk/1.0.13.81_10003810) '
Expand Down
29 changes: 29 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"compileOnSave": true,
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"noImplicitAny": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"charset": "utf8",
"allowJs": false,
"pretty": true,
"noEmitOnError": false,
"noUnusedLocals": true,
"noUnusedParameters": true,
"allowUnreachableCode": false,
"allowUnusedLabels": false,
"strictPropertyInitialization": false,
"noFallthroughCasesInSwitch": true,
"skipLibCheck": true,
"skipDefaultLibCheck": true,
"inlineSourceMap": true,
"importHelpers": true,
"esModuleInterop": true
},
"exclude": [
"node_modules*",
"test"
]
}

0 comments on commit 69c410e

Please sign in to comment.