Added NodePlatformAdapter and fixed bug in platform independent functions and added Demo.

This commit is contained in:
2022-06-29 15:00:01 +00:00
committed by GitHub
parent f3c0d646e3
commit bdbb5deda9
18 changed files with 432 additions and 105 deletions

66
demo/index.ts Normal file
View File

@ -0,0 +1,66 @@
import * as handlerJS from "..";
import errorHandler from "./errorHandler";
interface requestType {
hood: boolean;
id: number;
}
const App = new handlerJS.rootRouter<requestType, any>();
App.binding(
"/(.*)",
new handlerJS.handler("ANY", [
async (request, response) => {
console.log(request);
return undefined;
},
])
);
App.binding(
"/",
App.create(
"ANY",
(): Promise<string> =>
new Promise((resolve) => {
console.log("Hello World!");
resolve("Hello World!");
throw handlerJS.ChainInterrupted;
})
)
);
App.route("/v1/(.*)")
.add(
new handlerJS.route(
["/echo", "/echo/(.*)"],
[
new handlerJS.handler(handlerJS.method["GET"], [
async (request, response) => {
response = response ?? new handlerJS.response("");
response?.headers.set("Hello", "World");
response.body = request.url.pathname;
return response;
},
]),
new handlerJS.handler(handlerJS.method["POST"], [
async (request, response) => {
response = response ?? new handlerJS.response("");
response.body = request.body;
return response;
},
]),
]
)
)
.binding(
"/error",
App.create(handlerJS.method["ANY"], async () => {
throw new Error("Nothing will happen here.");
})
)
.useErrorResponder(errorHandler);
App.useAdapater(handlerJS.platformAdapater.NodePlatformAdapter);
App.adapater.listen(8080);