Skip to content

Commit

Permalink
released v0.9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
calidion committed Jan 27, 2021
1 parent e16f660 commit 1edbe16
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 45 deletions.
58 changes: 30 additions & 28 deletions __tests__/decorators/error-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@ import Aex from "../../src/core";
import { http } from "../../src/decorators/http";

import { PostText, initRandomPort } from "../../src/util/request";
import { error } from '../../src/decorators/error';
import { error } from "../../src/decorators/error";

class User {
@http("post", "/user/login")
@error({
Hello: {
code: 100,
messages: {}
messages: {},
},
I: {
Love: {
You: {
code: 1,
messages: {
"en-US": "I Love U!",
"zh-CN": "我爱你!"
}
}
}
"zh-CN": "我爱你!",
},
},
},
},
Me: {
alias: "I"
}
alias: "I",
},
})
public all(_req: any, res: any, scope: any) {
expect(scope.error.Hello).toBeTruthy();
Expand All @@ -41,26 +41,29 @@ class User {
}

@http("post", "/user/ok")
@error({
Hello: {
code: 100,
messages: {}
@error(
{
Hello: {
code: 100,
messages: {},
},
I: {
Love: {
You: {
code: 1,
messages: {
"en-US": "I Love U!",
"zh-CN": "我爱你!",
},
},
},
},
Me: {
alias: "I",
},
},
I: {
Love: {
You: {
code: 1,
messages: {
"en-US": "I Love U!",
"zh-CN": "我爱你!"
}
}
}
},
Me: {
alias: "I"
}
}, true)
true
)
public road(_req: any, res: any, scope: any) {
expect(scope.error.HELLO).toBeTruthy();
expect(scope.error.I_LOVE_YOU).toBeTruthy();
Expand Down Expand Up @@ -113,7 +116,6 @@ test("Should use decorator error with upper case", async () => {
);
});


test("Should use decorator error with no error defined", async () => {
await PostText(
port,
Expand Down
2 changes: 1 addition & 1 deletion __tests__/decorators/filter-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Aex from "../../src/core";
import { body } from "../../src/decorators/body";
import { filter } from "../../src/decorators/filter";
import { http } from "../../src/decorators/http";
import { query } from '../../src/decorators/query';
import { query } from "../../src/decorators/query";

import {
GetStatus,
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aex/core",
"version": "0.8.9",
"version": "0.9.0",
"description": "aex",
"license": "MIT",
"repository": "calidion/aex",
Expand All @@ -20,11 +20,11 @@
"scripts": {
"clean": "rimraf lib && rimraf coverage",
"format": "prettier --write \"{src,__tests__}/**/*.ts\" --trailing-comma es5",
"lint": "tslint --force --format verbose \"src/**/*.ts\"",
"lint": "tslint --fix --force --format verbose \"src/**/*.ts\"",
"prepublishOnly": "npm run build",
"prebuild": "npm run clean && npm run format && npm run lint && echo Using TypeScript && tsc --version",
"build": "tsc --pretty",
"test": "jest --coverage",
"test": "npm run build && jest --coverage",
"coverage": "jest --coverage",
"watch": "npm run build -- --watch",
"watch:test": "jest --watch"
Expand Down
2 changes: 1 addition & 1 deletion src/core.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { createServer, IncomingMessage, Server, ServerResponse } from "http";
import { One } from "./decorators/one";
import { Scope } from "./scope";
import NotFound from "./status/404";
import { IAsyncMiddleware } from "./types";
import { processMiddleware } from "./util";
import { One } from "./decorators/one";

export class Aex {
// tslint:disable-next-line:variable-name
Expand Down
6 changes: 3 additions & 3 deletions src/decorators/body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ export function body(

if (bodyTypes.indexOf(type) === -1) {
// tslint:disable-next-line: only-arrow-functions
return function () { };
return function() {};
}
const parser: any = bodyParser;
const cb = parser[type](options);
const asyncCB = toAsyncMiddleware(cb);
// tslint:disable-next-line: only-arrow-functions
return function (target: any, _propertyKey: any, descriptor: any) {
return function(target: any, _propertyKey: any, descriptor: any) {
const origin = descriptor.value;

// tslint:disable-next-line: only-arrow-functions
descriptor.value = async function (...args: any[]) {
descriptor.value = async function(...args: any[]) {
await asyncCB.apply(
asyncCB,
args as [IncomingMessage, ServerResponse, (Scope | undefined)?]
Expand Down
7 changes: 3 additions & 4 deletions src/decorators/error.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Scope } from "../scope";
import { Generator } from "errorable";
import { Scope } from "../scope";

/**
*
Expand All @@ -10,13 +10,12 @@ export function error(
errors: { [x: string]: object } = {},
upperOrCamel: boolean = false
) {

// tslint:disable-next-line: only-arrow-functions
return function (target: any, _propertyKey: any, descriptor: any) {
return function(target: any, _propertyKey: any, descriptor: any) {
const origin = descriptor.value;

// tslint:disable-next-line: only-arrow-functions
descriptor.value = async function (...args: any[]) {
descriptor.value = async function(...args: any[]) {
const scope: Scope = args[2];
const generated = Generator.generate(errors, upperOrCamel);
Object.assign(scope.error, generated);
Expand Down
2 changes: 1 addition & 1 deletion src/decorators/query.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as qs from "querystring";
import { Scope } from '../scope';
import { Scope } from "../scope";

export function query() {
// tslint:disable-next-line: only-arrow-functions
Expand Down
8 changes: 6 additions & 2 deletions src/router.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IncomingMessage, METHODS, ServerResponse } from "http";
import { match } from "path-to-regexp";
import { Scope } from './scope';
import { Scope } from "./scope";
import NotFound from "./status/404";
import {
IAsyncHandler,
Expand Down Expand Up @@ -123,7 +123,11 @@ export class Router {
return;
}

protected enhanceRequest(params: object, req: IncomingMessage, scope?: Scope) {
protected enhanceRequest(
params: object,
req: IncomingMessage,
scope?: Scope
) {
Object.defineProperty(req, "params", {
enumerable: true,
get: () => {
Expand Down
2 changes: 1 addition & 1 deletion src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class Scope {
return this._params;
}

reset() {
public reset() {
this._body = {};
this._session = {};
this._query = {};
Expand Down
4 changes: 3 additions & 1 deletion src/util/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ export async function PostText(
options: any = {},
compare: boolean = true
) {
(options.hostname = domain), (options.port = port), (options.path = url);
options.hostname = domain;
options.port = port;
options.path = url;
options.method = method;
const res = await POST(options, body);
const od = Object.getOwnPropertyDescriptor(res, "text");
Expand Down

0 comments on commit 1edbe16

Please sign in to comment.