Skip to content

Commit

Permalink
feat: add typescript support (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
songyazhao authored and dead-horse committed Sep 10, 2018
1 parent fee6e12 commit 51b1de3
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 0 deletions.
27 changes: 27 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
declare module 'egg' {
export interface EggAppConfig {
sessionRedis: {
name: string
}
}

export interface Application {
/**
* @member Application#sessionStore
* @property {Function<string>} get - get redis session store
* @property {Function<string, any, number?>} set - set the redis session store
* @property {Function<string>} destroy - destroy of redis session store
* @example
* ```js
* this.app.sessionStore.set('SESSION_KEY', { a: 1 }, 6000);
* this.app.sessionStore.get('SESSION_KEY');
* this.app.sessionStore.destroy('SESSION_KEY');
* ```
*/
sessionStore: {
get: (key: string) => Promise<any>;
set: (key: string, value: any, maxAge?: number | string) => Promise<void>;
destroy: (key: string) => Promise<void>;
};
}
}
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
"eslint": "^4.19.1",
"eslint-config-egg": "^7.0.0",
"mz-modules": "^2.1.0",
"runscript": "^1.3.0",
"supertest": "^3.1.0",
"typescript": "^3.0.3",
"webstorm-disable-index": "^1.2.0"
},
"engines": {
Expand All @@ -40,8 +42,10 @@
"ci": "npm run lint && npm run cov",
"autod": "autod"
},
"types": "index.d.ts",
"files": [
"index.js",
"index.d.ts",
"app.js",
"agent.js",
"config",
Expand Down
8 changes: 8 additions & 0 deletions test/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@

const sleep = require('mz-modules/sleep');
const request = require('supertest');
const runscript = require('runscript');
const assert = require('assert');
const mm = require('egg-mock');
const path = require('path');
const tsBaseDir = path.join(__dirname, './fixtures/ts');

describe('app.test.js', () => {
// add typescript tsc
before(async () => {
await runscript(`tsc -p ${tsBaseDir}/tsconfig.json`, { cwd: tsBaseDir });
});

[
'single',
'multi',
'ts',
].forEach(name => {
describe(name, () => {
let app;
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/ts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.js
34 changes: 34 additions & 0 deletions test/fixtures/ts/app/controller/home.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Controller } from 'egg';

// add home controller
declare module 'egg' {
interface IController {
home: HomeController;
}
}

export default class HomeController extends Controller {
async get () {
this.ctx.body = this.ctx.session;
};

async set () {
this.ctx.session = this.ctx.query;
this.ctx.body = this.ctx.session;
};

async setKey () {
this.ctx.session.key = this.ctx.query.key;
this.ctx.body = this.ctx.session;
};

async remove () {
this.ctx.session = null;
this.ctx.body = this.ctx.session;
};

async maxAge () {
this.ctx.session.maxAge = Number(this.ctx.query.maxAge);
this.ctx.body = this.ctx.session;
};
}
10 changes: 10 additions & 0 deletions test/fixtures/ts/app/router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Application } from 'egg';

export default (app: Application) => {
const controller = app.controller;
app.get('/get', controller.home.get);
app.get('/set', controller.home.set);
app.get('/setKey', controller.home.setKey);
app.get('/remove', controller.home.remove);
app.get('/maxAge', controller.home.maxAge);
}
11 changes: 11 additions & 0 deletions test/fixtures/ts/config/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default {
keys: 'keys',
redis: {
client: {
host: '127.0.0.1',
port: 6379,
db: '0',
password: '',
},
}
}
10 changes: 10 additions & 0 deletions test/fixtures/ts/config/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { EggPlugin } from 'egg';

const plugin: EggPlugin = {
redis: {
enable: true,
package: 'egg-redis',
}
};

export default plugin;
4 changes: 4 additions & 0 deletions test/fixtures/ts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "ts",
"version": "1.0.0"
}
12 changes: 12 additions & 0 deletions test/fixtures/ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"target": "es6",
"baseUrl": ".",
"module": "commonjs",
"lib": ["es7"],
"strict": true
},
"include": [
"../../../**/*"
]
}

0 comments on commit 51b1de3

Please sign in to comment.