Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add tsd #1027

Merged
merged 4 commits into from
Jun 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
780 changes: 780 additions & 0 deletions index.d.ts

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
"egg"
],
"dependencies": {
"@types/accepts": "^1.3.2",
"@types/koa": "^2.0.39",
"@types/koa-router": "^7.0.22",
"accepts": "^1.3.3",
"agentkeepalive": "^3.2.0",
"cluster-client": "^1.6.4",
Expand Down Expand Up @@ -68,15 +71,19 @@
"runscript": "^1.2.1",
"spy": "^1.0.0",
"supertest": "^3.0.0",
"ts-node": "^3.0.6",
"typescript": "^2.3.4",
"webstorm-disable-index": "^1.1.2"
},
"main": "index.js",
"types": "index.d.ts",
"files": [
"app",
"config",
"bin",
"lib",
"index.js"
"index.js",
"index.d.ts"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

d.ts 的 entry 配置要配么

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

d.ts 的 entry 配置 是什么意思?

ts 默认会找包里面的 index.d.ts ,找不到找 @types/egg

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

就是如果不放 index.d.ts 是否可以指定,类似 main

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html 可以配置 types 字段,要配一个么?默认是 index.d.ts

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

配吧

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

加好了

],
"scripts": {
"lint": "eslint app config lib test *.js",
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/apps/app-ts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.js
node_modules
18 changes: 18 additions & 0 deletions test/fixtures/apps/app-ts/app/controller/foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Controller } from 'egg';

// add user controller and service
declare module 'egg' {
interface IController {
foo: FooController;
}
}

// controller
export default class FooController extends Controller {
async getData() {
this.ctx.body = await this.ctx.service.foo.bar();
}
async getBar() {
this.ctx.body = await this.service.foo.bar();
}
}
7 changes: 7 additions & 0 deletions test/fixtures/apps/app-ts/app/router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Application } from 'egg';

export default (app: Application) => {
const controller = app.controller;
app.get('/foo', controller.foo.getData);
app.post('/', controller.foo.getData);
}
14 changes: 14 additions & 0 deletions test/fixtures/apps/app-ts/app/service/foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Service } from 'egg';

// add user controller and service
declare module 'egg' {
interface IService {
foo: FooService;
}
}

export default class FooService extends Service {
async bar() {
return { env: this.config.env };
}
}
3 changes: 3 additions & 0 deletions test/fixtures/apps/app-ts/config/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
keys: 'foo',
}
4 changes: 4 additions & 0 deletions test/fixtures/apps/app-ts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "app-ts",
"version": "1.0.0"
}
12 changes: 12 additions & 0 deletions test/fixtures/apps/app-ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"target": "es6",
"baseUrl": ".",
"paths": {
"egg": ["../../../../index"]
},
"module": "commonjs",
"lib": ["es7"],
"strict": true
}
}
41 changes: 41 additions & 0 deletions test/ts/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

const request = require('supertest');
const mm = require('egg-mock');
const runscript = require('runscript');
const path = require('path');
const utils = require('../utils');
const baseDir = path.join(__dirname, '../fixtures/apps/app-ts');

describe('test/ts/index.test.js', () => {
before(function* () {
if (process.env.CI) {
yield runscript('tsc && npmlink ../../../../', { cwd: baseDir });
} else {
yield runscript('tsc && npm link ../../../../', { cwd: baseDir });
}
});

describe('compiler code', () => {

afterEach(mm.restore);
let app;
before(function* () {
app = utils.app('apps/app-ts');
yield app.ready();
});
after(function* () {
yield app.close();
});

it('controller run ok', done => {
request(app.callback())
.get('/foo')
.expect(200)
.expect({ env: 'unittest' })
.end(done);
});
});

});