Skip to content

Commit

Permalink
feat(fastify): support moment locals methods
Browse files Browse the repository at this point in the history
  • Loading branch information
wibus-wee committed Aug 14, 2022
1 parent 1f73193 commit fd98ec5
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@
"class-validator-jsonschema": "^3.1.1",
"consola": "*",
"cron": "*",
"dayjs": "1.11.4",
"ejs": "3.1.8",
"dayjs": "1.11.5",
"ejs": "3.1.8",
"entities": "^4.3.1",
"handlebars": "^4.7.7",
"i": "^0.3.7",
Expand All @@ -89,6 +88,7 @@
"marked": "^4.0.18",
"mime": "^3.0.0",
"mkdirp": "^1.0.4",
"moment": "^2.29.4",
"mongoose": "6.5.0",
"mongoose-aggregate-paginate-v2": "1.0.6",
"mongoose-lean-id": "0.3.0",
Expand Down
12 changes: 7 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion src/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export async function bootstrap() {
"art-template": require("art-template"),
},
templates: join(THEME_DIR), // 模板目录,模板名字应在 Render 中指定
viewExt: "ejs", // 模板文件的后缀名
defaultContext: {
dev: process.env.NODE_ENV === "development",
},
Expand Down
8 changes: 8 additions & 0 deletions src/common/adapt/fastify.adapt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ app.register(FastifyMultipart, {
},
});

// set locals
app.getInstance().decorateReply("locals", null);
app.getInstance().addHook("preHandler", (req, res: any, next) => {
res.locals = {};
res.locals.moment = require("moment");
next();
});

app.getInstance().addHook("onRequest", (request, reply, done) => {
// set undefined origin
const origin = request.headers.origin;
Expand Down

0 comments on commit fd98ec5

Please sign in to comment.