Skip to content

Commit

Permalink
chore: support for the fastify and structures
Browse files Browse the repository at this point in the history
Removes some of the orion boiler plate code.
  • Loading branch information
joamag committed Mar 3, 2024
1 parent 9d52c57 commit 03db078
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

*
* Support for fastify handlers and structures

### Changed

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@
"typings": "index.ts",
"peerDependencies": {
"dotenv": "^16.4.5",
"fastify": "^4.26.1",
"hive-js-util": "^0.5.3",
"yonius": "^0.13.11"
},
"devDependencies": {
"@types/chai": "^4.3.12",
"@types/mocha": "^10.0.6",
"@types/node": "^20.11.21",
"@types/node": "^20.11.24",
"@typescript-eslint/eslint-plugin": "^7.1.0",
"@typescript-eslint/parser": "^7.1.0",
"chai": "^5.1.0",
"dotenv": "^16.4.5",
"eslint": "^8.57.0",
"fastify": "^4.26.1",
"hive-js-util": "^0.5.3",
"mocha": "^10.3.0",
"mocha-cli": "^1.0.1",
Expand Down
30 changes: 30 additions & 0 deletions ts/fastify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { FastifyRequest, FastifyReply } from "fastify";
import { Error } from "./structs";

export const errorHandlerFastify = (
errG: globalThis.Error,
req: FastifyRequest,
res: FastifyReply
) => {
const err = errG as unknown as Error;
const code =
err.code && Number(err.code) >= 100 && Number(err.code) < 600
? Number(err.code)
: 500;
const result: {
error: string;
code: number;
stack?: string[];
} = { error: err.message, code: code };
if (process.env.NODE_ENV !== "production") {
result.stack = err.stack ? err.stack.split("\n") : [];
}
res.code(result.code).send(result);
};

export const notFoundHandlerFastify = (
req: FastifyRequest,
res: FastifyReply
) => {
res.code(404).send({ error: "Route not found", code: 404 });
};
4 changes: 3 additions & 1 deletion ts/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from "./fs";
export * from "./boot";
export * from "./fastify";
export * from "./fs";
export * from "./logging";
export * from "./structs";
5 changes: 5 additions & 0 deletions ts/structs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type Error = {
message: string;
code: number;
stack?: string;
};

0 comments on commit 03db078

Please sign in to comment.