Skip to content
This repository has been archived by the owner on Jul 13, 2022. It is now read-only.

Commit

Permalink
fix mapParser err
Browse files Browse the repository at this point in the history
  • Loading branch information
hal-wang committed Mar 17, 2021
1 parent 6f3d90d commit c5c50f1
Show file tree
Hide file tree
Showing 14 changed files with 270 additions and 116 deletions.
5 changes: 3 additions & 2 deletions src/Action/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ export default abstract class Action {
//#region will be set before doing
// eslint-disable-next-line @typescript-eslint/no-explicit-any
readonly requestParams: RequestParams = <any>undefined;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
readonly realPath: string = <any>undefined;
//#endregion

/** docs of action */
docs?: ApiDocs;

realPath?: string;

protected readonly base = HttpResult.base;
protected readonly ok = HttpResult.ok;
protected readonly accepted = HttpResult.accepted;
Expand Down
60 changes: 55 additions & 5 deletions src/Map/MapParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export default class MapParser {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const actionClass = require(filePath).default;
const action = new actionClass() as Action;
action.realPath = this.realPath;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(action as any).realPath = this.realPath;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(action as any).requestParams = this.requestParams;
return action;
Expand All @@ -54,17 +55,19 @@ export default class MapParser {
let mapPath;

if (!this.isMethodNecessary) {
mapPath = linq
const matchedPaths = linq
.from(map)
.where((item) => this.isSimplePathMatched(item))
.firstOrDefault();
.toArray();
mapPath = this.getMostLikePath(matchedPaths);
if (mapPath) return mapPath;
}

mapPath = linq
const matchedPaths = linq
.from(map)
.where((item) => this.isMethodPathMatched(item, true))
.firstOrDefault();
.toArray();
mapPath = this.getMostLikePath(matchedPaths);
if (mapPath) return mapPath;

const otherMethodPathCount = linq
Expand Down Expand Up @@ -119,6 +122,53 @@ export default class MapParser {
return true;
}

private getMostLikePath(mapPaths: string[]): string | undefined {
if (!mapPaths || !mapPaths.length) return;
if (mapPaths.length == 1) return mapPaths[0];

const pathsParts = <{ path: string; parts: string[] }[]>[];
mapPaths.forEach((path) => {
pathsParts.push({
path: path,
parts: path.toLowerCase().split("/"),
});
});

const minPartsCount = Math.min(
...linq
.from(pathsParts)
.select((pp) => pp.parts.length)
.toArray()
);
for (let i = 0; i < minPartsCount; i++) {
const notLikePaths = linq
.from(pathsParts)
.select((pp) => ({ part: pp.parts[i], path: pp.path }))
.where((p) => p.part.includes("^"))
.toArray();
if (notLikePaths.length > 0 && notLikePaths.length < pathsParts.length) {
notLikePaths.forEach((mlp) => {
const ppToRemove = linq
.from(pathsParts)
.where((p) => p.path == mlp.path)
.firstOrDefault();
if (ppToRemove) {
pathsParts.splice(pathsParts.indexOf(ppToRemove), 1);
}
});
}

if (pathsParts.length == 1) return pathsParts[0].path;
}

const mostLikePathParts = linq
.from(pathsParts)
.orderBy((pp) => pp.parts.length)
.firstOrDefault();
if (!mostLikePathParts) return;
return mostLikePathParts.path;
}

private get cfPath(): string {
return path.join(process.cwd(), this.cFolder);
}
Expand Down
11 changes: 11 additions & 0 deletions test/controllers/restful/method/^query/miss/post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Action, HttpResult } from "../../../../../../src";

export default class extends Action {
async do(): Promise<HttpResult> {
return this.ok({
method: "POST",
action: "query/miss",
realPath: this.realPath,
});
}
}
11 changes: 11 additions & 0 deletions test/controllers/restful/method/^query/post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Action, HttpResult } from "../../../../../src";

export default class extends Action {
async do(): Promise<HttpResult> {
return this.ok({
method: "POST",
action: "query",
realPath: this.realPath,
});
}
}
11 changes: 11 additions & 0 deletions test/controllers/restful/method/^query2/^nextQuery/post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Action, HttpResult } from "../../../../../../src";

export default class extends Action {
async do(): Promise<HttpResult> {
return this.ok({
method: "POST",
action: "query2/nextQuery",
realPath: this.realPath,
});
}
}
11 changes: 11 additions & 0 deletions test/controllers/restful/method/miss/^query/post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Action, HttpResult } from "../../../../../../src";

export default class extends Action {
async do(): Promise<HttpResult> {
return this.ok({
method: "POST",
action: "miss/query",
realPath: this.realPath,
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export default class extends Action {
async do(): Promise<HttpResult> {
return this.ok({
method: "POST",
action: "miss",
realPath: this.realPath,
});
}
}
2 changes: 2 additions & 0 deletions test/controllers/restful/method/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { Action, HttpResult } from "../../../../src";
export default class extends Action {
async do(): Promise<HttpResult> {
return this.ok({
method: "ANY",
action: "simple",
realPath: this.realPath,
});
}
}
109 changes: 0 additions & 109 deletions test/restful.test.ts

This file was deleted.

12 changes: 12 additions & 0 deletions test/restful/err.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { HttpMethod, Router } from "../../src/index";

test(`action name error`, async function () {
const event = {
body: {},
path: "/err",
httpMethod: HttpMethod.post,
};
const router = new Router(event, {}, undefined, "test/controllers");
const result = (await router.do()).result;
expect(result.statusCode).toBe(404);
});
87 changes: 87 additions & 0 deletions test/restful/match.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { HttpMethod, Router } from "../../src/index";

test(`find next`, async function () {
const event = {
body: {},
path: "/restful/method",
httpMethod: HttpMethod.post,
};
const router = new Router(event, {}, undefined, "test/controllers");
const result = (await router.do()).result;
expect(result.statusCode).toBe(200);
});

test(`find simple`, async function () {
const event = {
body: {},
path: "/restful/method/simple",
httpMethod: HttpMethod.post,
};
const router = new Router(event, {}, undefined, "test/controllers");
const result = (await router.do()).result;
expect(result.statusCode).toBe(200);
expect((result.body as Record<string, string>).action).toBe("simple");
});

test(`find simple next`, async function () {
const event = {
body: {},
path: "/restful/method/any",
httpMethod: HttpMethod.post,
};
const router = new Router(event, {}, undefined, "test/controllers");
const result = (await router.do()).result;
expect(result.statusCode).toBe(200);
expect((result.body as Record<string, string>).action).toBe("query");
});

test(`find miss next`, async function () {
const event = {
body: {},
path: "/restful/method/miss",
httpMethod: HttpMethod.post,
};
const router = new Router(event, {}, undefined, "test/controllers");
const result = (await router.do()).result;
expect(result.statusCode).toBe(200);
expect((result.body as Record<string, string>).action).toBe("miss");
expect((result.body as Record<string, string>).action).not.toBe("query");
});

test(`find miss next 2`, async function () {
const event = {
body: {},
path: "/restful/method/miss/any",
httpMethod: HttpMethod.post,
};
const router = new Router(event, {}, undefined, "test/controllers");
const result = (await router.do()).result;
expect(result.statusCode).toBe(200);
expect((result.body as Record<string, string>).action).toBe("miss/query");
});

test(`find miss next 3`, async function () {
const event = {
body: {},
path: "/restful/method/any/miss",
httpMethod: HttpMethod.post,
};
const router = new Router(event, {}, undefined, "test/controllers");
const result = (await router.do()).result;
expect(result.statusCode).toBe(200);
expect((result.body as Record<string, string>).action).toBe("query/miss");
});

test(`find miss next 4`, async function () {
const event = {
body: {},
path: "/restful/method/any/any",
httpMethod: HttpMethod.post,
};
const router = new Router(event, {}, undefined, "test/controllers");
const result = (await router.do()).result;
expect(result.statusCode).toBe(200);
expect((result.body as Record<string, string>).action).toBe(
"query2/nextQuery"
);
});
12 changes: 12 additions & 0 deletions test/restful/notAllowed.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Router } from "../../src/index";

test(`method not allowed`, async function () {
const event = {
body: {},
path: "/restful",
httpMethod: "NO",
};
const router = new Router(event, {}, undefined, "test/controllers");
const result = (await router.do()).result;
expect(result.statusCode).toBe(405);
});
Loading

0 comments on commit c5c50f1

Please sign in to comment.