-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Matteo Collina <[email protected]>
- Loading branch information
Showing
7 changed files
with
220 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
name: ci | ||
|
||
on: | ||
push: | ||
paths-ignore: | ||
- 'docs/**' | ||
- '*.md' | ||
pull_request: | ||
paths-ignore: | ||
- 'docs/**' | ||
- '*.md' | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
|
||
strategy: | ||
matrix: | ||
node-version: [18.x, 20.x] | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Use Node.js | ||
uses: actions/setup-node@v2 | ||
with: | ||
node-version: ${{ matrix.node-version }} | ||
|
||
- name: Install | ||
run: | | ||
npm install | ||
- name: Run tests | ||
run: | | ||
npm run test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,52 @@ | ||
# fastify-asyncforge | ||
# fastify-asyncforge | ||
|
||
## Install | ||
|
||
```sh | ||
npm i fastify fastify-asyncforge | ||
``` | ||
|
||
## Usage | ||
|
||
```js | ||
// App.js | ||
import fastify from 'fastify' | ||
import doWork from './do-work.mjs' | ||
|
||
const app = fastify({ | ||
logger: true | ||
}) | ||
app.register(import('fastify-asyncforge')) | ||
|
||
app.decorate('foo', 'bar') | ||
app.decorateRequest('a') | ||
app.decorateReply('b') | ||
|
||
app.addHook('onRequest', async function (req, reply) { | ||
req.a = 'a' | ||
reply.b = 'b' | ||
}) | ||
|
||
app.get('/', async function (request, reply) { | ||
doWork() | ||
return { hello: 'world' } | ||
}) | ||
|
||
app.listen({ port: 3000 }) | ||
|
||
// do-work.mjs | ||
import { logger, app, request, reply } from 'fastify-asyncforge' | ||
|
||
export default function doWork () { | ||
const log = logger() | ||
log.info({ | ||
foo: app().foo, | ||
a: request().a, | ||
b: reply().b | ||
}, 'doing work') | ||
} | ||
``` | ||
|
||
## License | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
'use strict' | ||
|
||
const fastify = require('fastify') | ||
const doWork = require('./do-work') | ||
|
||
const app = fastify({ | ||
logger: true | ||
}) | ||
app.register(require('../index')) | ||
|
||
app.decorate('foo', 'bar') | ||
app.decorateRequest('a') | ||
app.decorateReply('b') | ||
|
||
app.addHook('onRequest', async function (req, reply) { | ||
req.a = 'a' | ||
reply.b = 'b' | ||
}) | ||
|
||
app.get('/', async function (request, reply) { | ||
doWork() | ||
return { hello: 'world' } | ||
}) | ||
|
||
app.listen({ port: 3000 }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict' | ||
|
||
const { logger, app, request, reply } = require('../') | ||
|
||
module.exports = function doWork () { | ||
const log = logger() | ||
log.info({ | ||
foo: app().foo, | ||
a: request().a, | ||
b: reply().b | ||
}, 'doing work') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use strict' | ||
|
||
const fp = require('fastify-plugin') | ||
const { memo, setAll } = require('asyncforge') | ||
|
||
const app = memo() | ||
const request = memo() | ||
const reply = memo() | ||
const logger = memo() | ||
|
||
module.exports = fp(async function (fastify, opts) { | ||
app.set(fastify) | ||
logger.set(fastify.log) | ||
|
||
fastify.addHook('onRequest', async function (req, res) { | ||
setAll({ | ||
[app.key]: this, | ||
[request.key]: req, | ||
[reply.key]: res, | ||
[logger.key]: req.log | ||
}) | ||
}) | ||
}) | ||
|
||
module.exports.app = app | ||
module.exports.request = request | ||
module.exports.reply = reply | ||
module.exports.logger = logger |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"name": "fastify-asyncforge", | ||
"version": "0.0.1", | ||
"description": "helpers to get fastify request/reply and app", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "standard && borp" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/mcollina/fastify-asyncforge.git" | ||
}, | ||
"keywords": [ | ||
"fastify", | ||
"asynclocalstorage", | ||
"request", | ||
"reply" | ||
], | ||
"author": "Matteo Collina <[email protected]>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/mcollina/fastify-asyncforge/issues" | ||
}, | ||
"homepage": "https://github.com/mcollina/fastify-asyncforge#readme", | ||
"devDependencies": { | ||
"@matteo.collina/tspl": "^0.1.1", | ||
"borp": "^0.11.0", | ||
"fastify": "^4.26.2", | ||
"standard": "^17.1.0" | ||
}, | ||
"dependencies": { | ||
"asyncforge": "^0.1.2", | ||
"fastify-plugin": "^4.5.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict' | ||
|
||
const { test } = require('node:test') | ||
const Fastify = require('fastify') | ||
const tspl = require('@matteo.collina/tspl') | ||
const fastifyAsyncForge = require('../') | ||
|
||
const { app, request, reply, logger } = fastifyAsyncForge | ||
|
||
test('basic helpers', async (t) => { | ||
const p = tspl(t, { plan: 6 }) | ||
const fastify = Fastify() | ||
|
||
await fastify.register(fastifyAsyncForge) | ||
|
||
p.strictEqual(logger(), app.log) | ||
|
||
fastify.get('/', async function (_request, _reply) { | ||
p.strictEqual(app(), this) | ||
p.strictEqual(request(), _request) | ||
p.strictEqual(reply(), _reply) | ||
p.strictEqual(logger(), _request.log) | ||
return { hello: 'world' } | ||
}) | ||
|
||
const res = await fastify.inject({ | ||
method: 'GET', | ||
url: '/' | ||
}) | ||
|
||
p.strictEqual(res.statusCode, 200) | ||
|
||
await p.complete | ||
}) |