Skip to content

Commit

Permalink
fix: add decorators oldFunc.call
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaojue committed Feb 25, 2021
1 parent d219aae commit c6329b5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
17 changes: 16 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
# [2.3.0](https://github.com/darukjs/daruk/compare/2.2.2...2.3.0) (2021-01-15)
## [2.3.4](https://github.com/darukjs/daruk/compare/v2.3.3...v2.3.4) (2021-02-25)



## [2.3.3](https://github.com/darukjs/daruk/compare/v2.3.1...v2.3.3) (2021-02-20)


### Bug Fixes

* Daruk deps with requestContainer ([742f978](https://github.com/darukjs/daruk/commit/742f9789eea1de81056b9d0149445a5dca39fe11))
* fix tests for plugin rebind ([27bba60](https://github.com/darukjs/daruk/commit/27bba60a72739badc59cc79098689b6772971374))
* fix typings url ([6d5832f](https://github.com/darukjs/daruk/commit/6d5832f5e6202fa82d1f989a211fa90994efc9ca))



## [2.3.1](https://github.com/darukjs/daruk/compare/2.2.2...v2.3.1) (2021-01-15)


### Bug Fixes
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "daruk",
"version": "2.3.3",
"version": "2.3.4",
"description": "a node.js web framework",
"main": "build/index.js",
"scripts": {
Expand Down
9 changes: 6 additions & 3 deletions src/decorators/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export function validate(method: method, key: string, validateFunc: validateFunc
const ret = validateFunc(value);
if (is.string(ret)) ctx.validateError.push(ret);
}
await oldFunc(ctx, next);
// tslint:disable-next-line:no-invalid-this
await oldFunc.call(this, ctx, next);
await next();
};
};
Expand Down Expand Up @@ -51,7 +52,8 @@ export function required(config: { body?: string[]; query?: string[]; params?: s
check(body, config.body, 'body') ||
check(query, config.query, 'query') ||
check(params, config.params, 'params');
await oldFunc(ctx, next);
// tslint:disable-next-line:no-invalid-this
await oldFunc.call(this, ctx, next);
await next();
};
};
Expand Down Expand Up @@ -97,7 +99,8 @@ export function typeParse(config: { body?: ParseType; query?: ParseType; params?
ctx.parseBody = parse(config.body, ctx.request.body);
ctx.parseQuery = parse(config.query, ctx.query);
ctx.parseParams = parse(config.params, ctx.params);
await oldFunc(ctx, next);
// tslint:disable-next-line:no-invalid-this
await oldFunc.call(this, ctx, next);
await next();
};
};
Expand Down
12 changes: 8 additions & 4 deletions src/decorators/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ export function json() {
type('application/json')(proto, propertyKey, descriptor);

descriptor.value = async function jsonWrap(ctx: koa.Context, next: () => Promise<void>) {
const val = await oldFunc(ctx);
// tslint:disable-next-line:no-invalid-this
const val = await oldFunc.call(this, ctx);
// 确保是Object类型
ctx.body = { ...val };
await next();
Expand Down Expand Up @@ -102,7 +103,8 @@ export function type(type: string) {
return (proto: Object, propertyKey: string, descriptor: PropertyDescriptor) => {
const oldFunc = descriptor.value;
descriptor.value = async function typeWrap(ctx: koa.Context, next: () => Promise<void>) {
await oldFunc(ctx);
// tslint:disable-next-line:no-invalid-this
await oldFunc.call(this, ctx);
ctx.type = type;
await next();
};
Expand Down Expand Up @@ -134,7 +136,8 @@ export function header(key: string | { [key: string]: string }, value?: string)
return (proto: Object, propertyKey: string, descriptor: PropertyDescriptor) => {
const oldFunc = descriptor.value;
descriptor.value = async function headerWrap(ctx: koa.Context, next: () => Promise<void>) {
await oldFunc(ctx);
// tslint:disable-next-line:no-invalid-this
await oldFunc.call(this, ctx);
ctx.set(headers);
await next();
};
Expand All @@ -151,7 +154,8 @@ export function cache(callback: (cacheKey: string, shouldCacheData?: string) =>
if (cacheData) {
ctx.body = cacheData;
} else {
await oldFunc(ctx);
// tslint:disable-next-line:no-invalid-this
await oldFunc.call(this, ctx);
await callback(cacheKey, ctx.body);
}
await next();
Expand Down

0 comments on commit c6329b5

Please sign in to comment.