Add Deno test supported & RegExp construction at route creation time

This commit is contained in:
2022-07-03 19:15:42 +00:00
committed by GitHub
parent 6559a3f134
commit 87744a2b25
10 changed files with 153 additions and 40 deletions

View File

@ -35,6 +35,7 @@ export class SWPlatformAdapter<T = any, K = any> implements platformAdapater {
}
async handleResponse(response: response<K>): Promise<Response> {
if (response.status === 204) { response.body = null; }
const nativResponse = new Response(response.body, {
status: response.status,
headers: response.headers.headers,

View File

@ -10,26 +10,33 @@ interface matchedStatus {
}[];
}
interface regExpKey {
name: string;
prefix: string;
suffix: string;
pattern: string;
modifier: string;
};
export class route {
public paths: path[];
private paths: path[];
public handlers: handler<any, any>[];
private regExps: { regExp: RegExp, keys: regExpKey[] }[] = [];
constructor(paths: path[], handlers: handler<any, any>[]) {
this.paths = paths;
this.handlers = handlers;
this.paths.forEach(path => {
const keys: regExpKey[] = [];
this.regExps.push({ regExp: pathToRegexp(path, keys), keys });
})
}
async exec(path: string): Promise<matchedStatus> {
let Answer = await Promise.all<Promise<matchedStatus>>(
this.paths.map(async (it) => {
const keys: {
name: string;
prefix: string;
suffix: string;
pattern: string;
modifier: string;
}[] = [];
const regExp = pathToRegexp(it, keys);
const answer = regExp.exec(path);
this.regExps.map(async (it) => {
const answer = it.regExp.exec(path);
if (answer === null)
return {
matched: false,
@ -38,7 +45,7 @@ export class route {
let attributes: matchedStatus["attributes"] = [];
keys.forEach((key, index) => {
it.keys.forEach((key, index) => {
attributes.push({
name: key.name,
value: answer[index + 1],