mirror of
https://github.com/186526/handlers.js
synced 2024-10-13 00:29:43 +00:00
Add Deno test supported & RegExp construction at route creation time
This commit is contained in:
@ -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,
|
||||
|
||||
31
src/route.ts
31
src/route.ts
@ -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],
|
||||
|
||||
Reference in New Issue
Block a user