Skip to content

Commit

Permalink
Merge pull request #29 from sellersindustry/async-endpoints
Browse files Browse the repository at this point in the history
Fixed Async Issues
  • Loading branch information
SellersEvan authored Mar 12, 2024
2 parents 04d4a02 + b84cd42 commit e8eb40c
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 29 deletions.
File renamed without changes.
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -546,8 +546,6 @@ Any help is very much appreciated. Build some useful modules and [submit them to


### TODO
- build static flag module
- Add Documentation to static flag and list
- deploy example server to vercel...
- Update Website
- Inform People
Expand All @@ -557,13 +555,10 @@ Any help is very much appreciated. Build some useful modules and [submit them to


### Maintenance
- Allow capitizal letters in dynamic routes
- Remove the utilities folder, and place the function in the appropriate locations.
- Clean up the CLI system.
- Start command does not have to build the server, just starts a local server. Will keep track to ensure that ExpressJS was the last build. Also allow point controls from arguments on the build file.
- Remove the ExpressJS bundler as it's too large, use a different smaller system, and just call it the "local" bundler.
- Console Development Server, Live Logs
- Allow Null Response Body
- Make a document website with [Mintlify](https://mintlify.com/preview).
- Better linting and cleaned linting system.
- Better method for building JS instead of large multiple-line strings. Can we build a module that allows text files with inline JavaScript, similar to JSX?
Expand Down
2 changes: 1 addition & 1 deletion lib/builder/generator/bundler/expressjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class BundlerExpressJS extends Bundler {
`import configModule_${id} from "${module.config.path}";`,
`app.all("/${route}", async (request, resolve) => {`,
`\tlet endpoint = ${JSON.stringify(endpoint)}`,
`\tlet response = Handler(request, functions_${id}, endpoint, configModule_${id}, config, "${BundlerType.ExpressJS.toString()}");`,
`\tlet response = await Handler(request, functions_${id}, endpoint, configModule_${id}, config, "${BundlerType.ExpressJS.toString()}");`,
`\tawait ExpressJSResponse(response, resolve);`,
`});`,
].join("\n");
Expand Down
2 changes: 1 addition & 1 deletion lib/builder/generator/bundler/vercel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class BundlerVercel extends Bundler {
`import * as functions from "./index";`,
`export default async function index(request, event) {`,
`\tlet endpoint = ${JSON.stringify(endpoint)}`,
`\treturn Handler(request, functions, endpoint, configModule, configServer, "${BundlerType.Vercel.toString()}")`,
`\treturn await Handler(request, functions, endpoint, configModule, configServer, "${BundlerType.Vercel.toString()}")`,
`}`
].join("\n");
}
Expand Down
6 changes: 3 additions & 3 deletions lib/builder/generator/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function getSubroute(subroute:string[]):Route[] {
return subroute.map((name) => {
return {
name: name.toLowerCase(),
orginal: name,
original: name,
isDynamic: false,
isSubroute: true
}
Expand All @@ -69,7 +69,7 @@ function getRoute(filepath:string, path:string):Route[] {
return {
name: getRouteName(name),
isDynamic: isDynamic(name),
orginal: name
original: name
}
});
}
Expand All @@ -81,7 +81,7 @@ function isDynamic(name:string):boolean {


function getRouteName(name:string):string {
return (isDynamic(name) ? name.slice(1, -1) : name).toLowerCase()
return (isDynamic(name) ? name.slice(1, -1) : name.toLowerCase())
}


Expand Down
29 changes: 15 additions & 14 deletions lib/builder/generator/handler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,24 @@ import { SherpaRequest, Environment } from "../../../environment/index.js";
import { ConfigModule, ConfigServer, Endpoint, REQUEST_METHODS, BundlerType } from "../../models/index.js";


type MethodFunctions = { [key:string]:(request:SherpaRequest, environment:Environment) => Response };
type MethodFunctions = { [key:string]:(request:SherpaRequest, environment:Environment) => Response|Promise<Response> };


export function Handler(
export async function Handler(
request:Request, functions:MethodFunctions,
endpoint:Endpoint, module:ConfigModule, server:ConfigServer,
type:BundlerType
):Response {
):Promise<Response> {
let method = request.method.toUpperCase();
if (REQUEST_METHODS.includes(method) && functions[method]) {
try {
let sherpaRequest = prepareRequest(request, type);
let environment = new Environment(server, module, endpoint);
return functions[method](sherpaRequest, environment);
} catch (error) {
return new Response(`SherpaJS: ${error.message}`, { status: 405 });
}
} else {
return new Response(`Unsupported method "${request.method}".`, { status: 405 });
if (!REQUEST_METHODS.includes(method) || !functions[method]) {
return new Response(`Unsupported method "${method}".`, { status: 405 });
}
try {
let sherpaRequest = prepareRequest(request, type);
let environment = new Environment(server, module, endpoint);
return await functions[method](sherpaRequest, environment);
} catch (error) {
return new Response(`SherpaJS: ${error.message}`, { status: 405 });
}
}

Expand Down Expand Up @@ -74,8 +73,10 @@ export async function ExpressJSResponse(sherpa:Response, express:ExpressResponse
express.send(await sherpa.text());
} else if (type && type.includes("application/json")) {
express.send(await sherpa.json());
} else if (!type) {
express.send(await sherpa.text());
} else {
express.status(500).send("Error: SherpaJS ExpressJS only currently support JSON and Text, please contact support.");
express.status(500).send(`Error: SherpaJS ExpressJS only currently support JSON and Text, your trying to use "${type}", please make a new issue.`);
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/builder/linter/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function filename(endpoint:Endpoint):Log[] {


function routes(endpoint:Endpoint):Log[] {
let fullRoute = endpoint.route.map(r => r.orginal).join("/");
let fullRoute = endpoint.route.map(r => r.original).join("/");
for (let subroute of endpoint.route) {
if (!Validate.AlphaNumericDash(subroute.name)) {
return [{
Expand All @@ -62,7 +62,7 @@ function routes(endpoint:Endpoint):Log[] {
path: endpoint.filepath
}];
}
if (subroute.orginal.toLowerCase() != subroute.orginal) {
if (subroute.original.toLowerCase() != subroute.original && !subroute.isDynamic) {
return [{
level: LogLevel.WARN,
message: `Routes should be lowercase "${fullRoute}".`,
Expand Down
2 changes: 1 addition & 1 deletion lib/builder/models/structure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export type Endpoint = {
export type Route = {
name:string;
isDynamic:boolean;
orginal?:string;
original?:string;
isSubroute?:boolean;
};

Expand Down
2 changes: 1 addition & 1 deletion lib/environment/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function NewResponse(body?:unknown, options?:ResponseInit):Respon
return Response.json(body, options);
}
}
return new Response();
return new Response("", options);
}


Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"build": "tsc --build --force tsconfig.json",
"prepare": "npm run build",
"lint": "eslint . --ext .ts",
"cli": "node dist/lib/cli/index.js",
"test": "echo \"no test specified\""
},
"devDependencies": {
Expand Down

0 comments on commit e8eb40c

Please sign in to comment.