Skip to content

Commit 70eb523

Browse files
mrcljxandrewbranch
authored andcommitted
feat(koa): Move custom properties into DefaultContext (DefinitelyTyped#39008)
* Move custom properties into DefaultContext This allows having a strict `Koa<{}, {}>`. * Fix tests
1 parent 282aa18 commit 70eb523

File tree

6 files changed

+20
-29
lines changed

6 files changed

+20
-29
lines changed
Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,35 @@
11
import Koa = require("koa");
2-
import Router = require("koa-router");
32
import bouncer = require("koa-bouncer");
43

54
const app = new Koa();
65

76
app.use(bouncer.middleware());
8-
const router = new Router()
97

10-
router.post('/users', async (ctx) => {
8+
app.use(async ctx => {
119
ctx.validateBody('uname')
1210
.required('Username required')
1311
.isString()
14-
.trim()
12+
.trim();
1513

1614
ctx.validateBody('email')
1715
.optional()
1816
.isString()
1917
.trim()
20-
.isEmail('Invalid email format')
18+
.isEmail('Invalid email format');
2119

2220
ctx.validateBody('password1')
2321
.required('Password required')
2422
.isString()
25-
.isLength(6, 100, 'Password must be 6-100 chars')
23+
.isLength(6, 100, 'Password must be 6-100 chars');
2624

2725
ctx.validateBody('password2')
2826
.required('Password confirmation required')
2927
.isString()
30-
.eq(ctx.vals.password1, 'Passwords must match')
31-
32-
ctx.validateBody('age')
33-
.gte(18, 'Must be 18 or older')
28+
.eq(ctx.vals.password1, 'Passwords must match');
3429

35-
console.log(ctx.vals)
36-
})
30+
ctx.validateBody('age').gte(18, 'Must be 18 or older');
3731

38-
app
39-
.use(router.routes())
40-
.use(router.allowedMethods());
41-
32+
ctx.body = ctx.vals;
33+
});
4234

4335
app.listen(80);

types/koa-log/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ declare namespace koaLog {
2424

2525
function status(ctx: Koa.BaseContext): number;
2626

27-
function token(name: string, fn: (ctx: Koa.BaseContext) => string): void;
27+
function token(name: string, fn: (ctx: Koa.ParameterizedContext) => string): void;
2828

2929
function url(ctx: Koa.BaseContext): string;
3030
}

types/koa-log/koa-log-tests.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import Koa = require('koa');
22
import koaLogger = require('koa-log');
33

4-
koaLogger.morgan.token('request-body', (ctx: Koa.BaseContext) => JSON.stringify(ctx.request.body));
5-
koaLogger.morgan.token('response-body', (ctx: Koa.BaseContext) => JSON.stringify(ctx.body));
4+
koaLogger.morgan.token('request-ip', ctx => JSON.stringify(ctx.request.ip));
5+
koaLogger.morgan.token('response-body', ctx => JSON.stringify(ctx.body));
66

77
const app = new Koa();
8-
app.use(
9-
koaLogger(':status :method :url took :response-time ms :request-body :response-body')
10-
);
8+
app.use(koaLogger(':status :method :url took :response-time ms :request-ip :response-body'));

types/koa-router/koa-router-tests.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ app.use(async (ctx: Koa.ParameterizedContext<MyState, MyContext>, next) => {
7878

7979
const router3 = new Router();
8080
router3.get('/', (ctx) => {
81-
ctx.foo = "bar";
81+
ctx.state.foo = 'bar';
8282
console.log(ctx.router.params);
8383
ctx.body = "Hello World!";
8484
});

types/koa/index.d.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,12 @@ declare namespace Application {
540540
/**
541541
* This interface can be augmented by users to add types to Koa's default context
542542
*/
543-
interface DefaultContext extends DefaultContextExtends {}
543+
interface DefaultContext extends DefaultContextExtends {
544+
/**
545+
* Custom properties.
546+
*/
547+
[key: string]: any;
548+
}
544549

545550
type Middleware<
546551
StateT = DefaultState,
@@ -680,10 +685,6 @@ declare namespace Application {
680685
* Default error handling.
681686
*/
682687
onerror(err: Error): void;
683-
/**
684-
* Custom properties.
685-
*/
686-
[key: string]: any;
687688
}
688689

689690
interface Request extends BaseRequest {

types/koa__router/koa__router-tests.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ app.use(async (ctx: Koa.ParameterizedContext<MyState, MyContext>, next) => {
7575

7676
const router3 = new Router();
7777
router3.get('/', (ctx) => {
78-
ctx.foo = "bar";
78+
ctx.state.foo = 'bar';
7979
console.log(ctx.router.params);
8080
ctx.body = "Hello World!";
8181
});

0 commit comments

Comments
 (0)