Skip to content

Commit

Permalink
feat: add koa-body in daruk and fix example
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaojue committed May 16, 2019
1 parent 0555099 commit 008029a
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 52 deletions.
2 changes: 0 additions & 2 deletions docs/todo-list.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Todo

Todo list of features we want to support before version v1.0.0:

- request:

- [ ] @typeParse(key:string)
Expand Down
7 changes: 2 additions & 5 deletions example/02-comments/daruk.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ interface DarukConfig {

export default function(daruk: Daruk) {
const globalConfig: DarukConfig = {
middlewareOrder: ['koa-ejs', 'koa-favicon', 'koa-bodyparser'],
// middlewareOrder: ['koa-ejs', 'koa-favicon', 'koa-bodyparser'],
middlewareOrder: ['koa-ejs', 'koa-favicon'],
middleware: {
'koa-favicon'(mid: Function) {
return mid(`${daruk.options.rootPath}/public/favicon.png`);
Expand All @@ -23,10 +24,6 @@ export default function(daruk: Daruk) {
viewExt: 'tpl'
});
return false;
},
// https://github.com/koajs/bodyparser
'koa-bodyparser'(mid: Function) {
return mid();
}
}
};
Expand Down
1 change: 1 addition & 0 deletions example/02-comments/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
},
"dependencies": {
"daruk": "lastest",
"daruk-request-id": "0.4.3",
"koa-bodyparser": "^4.2.1",
"koa-ejs": "^4.2.0",
"koa-favicon": "^2.0.1",
Expand Down
5 changes: 0 additions & 5 deletions example/02-comments/typings/context.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { Request } from 'daruk';

interface IRequest extends Request {
body: any;
}

declare module 'daruk' {
interface Context {
render: (tpl: string, data?: any) => Promise<any>;
request: IRequest;
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"http-server-shutdown": "^1.1.2",
"is": "^3.3.0",
"koa": "^2.5.2",
"koa-body": "^4.1.0",
"koa-router": "^7.4.0",
"object-assign-deep": "^0.4.0",
"reflect-metadata": "^0.1.13",
Expand Down
11 changes: 11 additions & 0 deletions src/built_in/middlewares/daruk_body.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* @fileOverview koa-body 中间件
* https://github.com/dlau/koa-body
*/

import koaBody = require('koa-body');
import { Daruk } from '../../typings/daruk';

export default (app: Daruk.DarukCore) => {
return koaBody(app.options.bodyOptions);
};
1 change: 1 addition & 0 deletions src/core/daruk_default_options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default function getDefaultOptions(rootPath: string, name: string, debug:
utilPath: join(rootPath, 'utils'),
darukConfigPath: join(rootPath, 'daruk.config'),
configPath: join(rootPath, 'config'),
bodyOptions: {},
debug,
monitor: {
enable: false,
Expand Down
7 changes: 6 additions & 1 deletion src/core/daruk_init_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,12 @@ export default class DarukInitModule {
if (this.options.monitor.enable) {
middlewareOrder.unshift('daruk_monitor');
}
middlewareOrder.unshift('daruk_request_id', 'daruk_logger', 'daruk_context_loader');
middlewareOrder.unshift(
'daruk_request_id',
'daruk_logger',
'daruk_body',
'daruk_context_loader'
);

// 再次保存 middlewareOrder,使外部对最终的 middlewareOrder 可见
this.module.middlewareOrder = middlewareOrder;
Expand Down
3 changes: 3 additions & 0 deletions types/daruk_options.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import koaBody = require('koa-body');

type RecursivePartial<T> = {
[P in keyof T]?: T[P] extends (infer U)[]
? RecursivePartial<U>[]
Expand All @@ -17,6 +19,7 @@ export interface Options {
middlewarePath: string;
rootPath: string;
debug: boolean;
bodyOptions: koaBody.IKoaBodyOptions;
monitor: {
enable: boolean;
v8AnalyticsPath: string;
Expand Down
50 changes: 16 additions & 34 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,38 +73,20 @@ declare module 'daruk' {
* copy from Koa
*/
listen(
port?: number,
hostname?: string,
backlog?: number,
listeningListener?: () => void,
): Http.Server;
listen(
port: number,
hostname?: string,
listeningListener?: () => void,
): Http.Server;
listen(
port: number,
backlog?: number,
listeningListener?: () => void,
port?: number,
hostname?: string,
backlog?: number,
listeningListener?: () => void
): Http.Server;
listen(port: number, hostname?: string, listeningListener?: () => void): Http.Server;
listen(port: number, backlog?: number, listeningListener?: () => void): Http.Server;
listen(port: number, listeningListener?: () => void): Http.Server;
listen(
path: string,
backlog?: number,
listeningListener?: () => void,
): Http.Server;
listen(path: string, backlog?: number, listeningListener?: () => void): Http.Server;
listen(path: string, listeningListener?: () => void): Http.Server;
listen(options: ListenOptions, listeningListener?: () => void): Http.Server;
listen(
handle: any,
backlog?: number,
listeningListener?: () => void,
): Http.Server;
listen(handle: any, backlog?: number, listeningListener?: () => void): Http.Server;
listen(handle: any, listeningListener?: () => void): Http.Server;



public serverReady(server: Http.Server | Https.Server): void;
public registerTimer(describe: RegisterDes | Array<RegisterDes>): void;
public registerService(describe: RegisterDes | Array<RegisterDes>): void;
Expand All @@ -118,14 +100,14 @@ declare module 'daruk' {
* Copy from Koa
*/
interface ListenOptions {
port?: number;
host?: string;
backlog?: number;
path?: string;
exclusive?: boolean;
readableAll?: boolean;
writableAll?: boolean;
}
port?: number;
host?: string;
backlog?: number;
path?: string;
exclusive?: boolean;
readableAll?: boolean;
writableAll?: boolean;
}
export interface Context extends Koa.Context {
readonly config: Config;
readonly globalModule: GlobalModule;
Expand Down
59 changes: 54 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,10 @@
"@types/keygrip" "*"
"@types/node" "*"

"@types/events@*":
version "3.0.0"
resolved "https://registry.npm.taobao.org/@types/events/download/@types/events-3.0.0.tgz#2862f3f58a9a7f7c3e78d79f130dd4d71c25c2a7"

"@types/expect@^1.20.3":
version "1.20.4"
resolved "https://registry.npm.taobao.org/@types/expect/download/@types/expect-1.20.4.tgz#8288e51737bf7e3ab5d7c77bfa695883745264e5"
Expand All @@ -308,6 +312,13 @@
"@types/express-serve-static-core" "*"
"@types/serve-static" "*"

"@types/formidable@^1.0.31":
version "1.0.31"
resolved "https://registry.npm.taobao.org/@types/formidable/download/@types/formidable-1.0.31.tgz#274f9dc2d0a1a9ce1feef48c24ca0859e7ec947b"
dependencies:
"@types/events" "*"
"@types/node" "*"

"@types/http-assert@*":
version "1.4.0"
resolved "https://registry.npm.taobao.org/@types/http-assert/download/@types/http-assert-1.4.0.tgz#41d173466e396e99a14d75f7160cc997f2f9ed8b"
Expand Down Expand Up @@ -769,6 +780,10 @@ builtin-modules@^1.1.1:
version "1.1.1"
resolved "https://registry.npm.taobao.org/builtin-modules/download/builtin-modules-1.1.1.tgz#270f076c5a72c02f5b65a47df94c5fe3a278892f"

bytes@3.1.0:
version "3.1.0"
resolved "https://registry.npm.taobao.org/bytes/download/bytes-3.1.0.tgz#f6cf7933a360e0588fa9fde85651cdc7f805d1f6"

cache-base@^1.0.1:
version "1.0.1"
resolved "https://registry.npm.taobao.org/cache-base/download/cache-base-1.0.1.tgz#0a7f46416831c8b662ee36fe4e7c59d76f666ab2"
Expand Down Expand Up @@ -973,6 +988,15 @@ cloneable-readable@^1.0.0:
process-nextick-args "^2.0.0"
readable-stream "^2.3.5"

co-body@^5.1.1:
version "5.2.0"
resolved "https://registry.npm.taobao.org/co-body/download/co-body-5.2.0.tgz#5a0a658c46029131e0e3a306f67647302f71c124"
dependencies:
inflation "^2.0.0"
qs "^6.4.0"
raw-body "^2.2.0"
type-is "^1.6.14"

co@^4.6.0:
version "4.6.0"
resolved "https://registry.npm.taobao.org/co/download/co-4.6.0.tgz#6ea6bdf3d853ae54ccb8e47bfa0bf3f9031fb184"
Expand Down Expand Up @@ -1752,7 +1776,7 @@ form-data@^2.3.1, form-data@~2.3.2:
combined-stream "^1.0.6"
mime-types "^2.1.12"

formidable@^1.2.0:
formidable@^1.1.1, formidable@^1.2.0:
version "1.2.1"
resolved "https://registry.npm.taobao.org/formidable/download/formidable-1.2.1.tgz#70fb7ca0290ee6ff961090415f4b3df3d2082659"

Expand Down Expand Up @@ -2081,7 +2105,7 @@ http-assert@^1.3.0:
deep-equal "~1.0.1"
http-errors "~1.7.2"

http-errors@^1.3.1, http-errors@^1.6.3, http-errors@~1.7.2:
http-errors@1.7.2, http-errors@^1.3.1, http-errors@^1.6.3, http-errors@~1.7.2:
version "1.7.2"
resolved "https://registry.npm.taobao.org/http-errors/download/http-errors-1.7.2.tgz#4f5029cf13239f31036e5b2e55292bcfbcc85c8f"
dependencies:
Expand Down Expand Up @@ -2113,7 +2137,7 @@ https-proxy-agent@^2.2.1:
agent-base "^4.1.0"
debug "^3.1.0"

iconv-lite@^0.4.24, iconv-lite@^0.4.4:
iconv-lite@0.4.24, iconv-lite@^0.4.24, iconv-lite@^0.4.4:
version "0.4.24"
resolved "https://registry.npm.taobao.org/iconv-lite/download/iconv-lite-0.4.24.tgz#2022b4b25fbddc21d2f524974a474aafe733908b"
dependencies:
Expand Down Expand Up @@ -2147,6 +2171,10 @@ indent-string@^3.0.0:
version "3.2.0"
resolved "https://registry.npm.taobao.org/indent-string/download/indent-string-3.2.0.tgz#4a5fd6d27cc332f37e5419a504dbb837105c9289"

inflation@^2.0.0:
version "2.0.0"
resolved "https://registry.npm.taobao.org/inflation/download/inflation-2.0.0.tgz#8b417e47c28f925a45133d914ca1fd389107f30f"

inflight@^1.0.4:
version "1.0.6"
resolved "https://registry.npm.taobao.org/inflight/download/inflight-1.0.6.tgz#49bd6331d7d02d0c09bc910a1075ba8165b56df9"
Expand Down Expand Up @@ -2531,6 +2559,14 @@ kind-of@^6.0.0, kind-of@^6.0.2:
version "6.0.2"
resolved "https://registry.npm.taobao.org/kind-of/download/kind-of-6.0.2.tgz#01146b36a6218e64e58f3a8d66de5d7fc6f6d051"

koa-body@^4.1.0:
version "4.1.0"
resolved "https://registry.npm.taobao.org/koa-body/download/koa-body-4.1.0.tgz#99295ee2e9543884e5730ae696780930b3821c44"
dependencies:
"@types/formidable" "^1.0.31"
co-body "^5.1.1"
formidable "^1.1.1"

koa-compose@^3.0.0:
version "3.2.1"
resolved "https://registry.npm.taobao.org/koa-compose/download/koa-compose-3.2.1.tgz#a85ccb40b7d986d8e5a345b3a1ace8eabcf54de7"
Expand Down Expand Up @@ -3539,7 +3575,7 @@ q@^1.5.1:
version "1.5.1"
resolved "https://registry.npm.taobao.org/q/download/q-1.5.1.tgz#7e32f75b41381291d04611f1bf14109ac00651d7"

qs@^6.5.1:
qs@^6.4.0, qs@^6.5.1:
version "6.7.0"
resolved "https://registry.npm.taobao.org/qs/download/qs-6.7.0.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fqs%2Fdownload%2Fqs-6.7.0.tgz#41dc1a015e3d581f1621776be31afb2876a9b1bc"

Expand All @@ -3551,6 +3587,15 @@ quick-lru@^1.0.0:
version "1.1.0"
resolved "https://registry.npm.taobao.org/quick-lru/download/quick-lru-1.1.0.tgz#4360b17c61136ad38078397ff11416e186dcfbb8"

raw-body@^2.2.0:
version "2.4.0"
resolved "https://registry.npm.taobao.org/raw-body/download/raw-body-2.4.0.tgz#a1ce6fb9c9bc356ca52e89256ab59059e13d0332"
dependencies:
bytes "3.1.0"
http-errors "1.7.2"
iconv-lite "0.4.24"
unpipe "1.0.0"

rc@^1.2.7:
version "1.2.8"
resolved "https://registry.npm.taobao.org/rc/download/rc-1.2.8.tgz#cd924bf5200a075b83c188cd6b9e211b7fc0d3ed"
Expand Down Expand Up @@ -4366,7 +4411,7 @@ type-detect@4.0.8, type-detect@^4.0.0, type-detect@^4.0.5:
version "4.0.8"
resolved "https://registry.npm.taobao.org/type-detect/download/type-detect-4.0.8.tgz#7646fb5f18871cfbb7749e69bd39a6388eb7450c"

type-is@^1.6.16:
type-is@^1.6.14, type-is@^1.6.16:
version "1.6.18"
resolved "https://registry.npm.taobao.org/type-is/download/type-is-1.6.18.tgz#4e552cd05df09467dcbc4ef739de89f2cf37c131"
dependencies:
Expand Down Expand Up @@ -4426,6 +4471,10 @@ unique-stream@^2.0.2:
json-stable-stringify-without-jsonify "^1.0.1"
through2-filter "^3.0.0"

unpipe@1.0.0:
version "1.0.0"
resolved "https://registry.npm.taobao.org/unpipe/download/unpipe-1.0.0.tgz#b2bf4ee8514aae6165b4817829d21b2ef49904ec"

unset-value@^1.0.0:
version "1.0.0"
resolved "https://registry.npm.taobao.org/unset-value/download/unset-value-1.0.0.tgz#8376873f7d2335179ffb1e6fc3a8ed0dfc8ab559"
Expand Down

0 comments on commit 008029a

Please sign in to comment.