Skip to content

Commit

Permalink
initial import
Browse files Browse the repository at this point in the history
Signed-off-by: Matteo Collina <[email protected]>
  • Loading branch information
mcollina committed Apr 19, 2024
1 parent bcc4fb8 commit eb4489c
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 1 deletion.
34 changes: 34 additions & 0 deletions .github/workflows/ci.yml
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
53 changes: 52 additions & 1 deletion README.md
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
25 changes: 25 additions & 0 deletions example/app.js
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 })
12 changes: 12 additions & 0 deletions example/do-work.js
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')
}
28 changes: 28 additions & 0 deletions index.js
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
35 changes: 35 additions & 0 deletions package.json
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"
}
}
34 changes: 34 additions & 0 deletions test/basic.test.js
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
})

0 comments on commit eb4489c

Please sign in to comment.