Skip to content

Commit

Permalink
Refactor project, tap etc. (#129)
Browse files Browse the repository at this point in the history
* move funkyPlugin.js to index.js

* move types into types folder

* disable package-lock generation

* replace jest with tap

* use standard instead of eslint/prettier

* remove jest artifact

* use strict

* make fastify v4.0.0 a peerDependency

* Remove eslint and prettier

* Apply suggestions from code review

Co-authored-by: Manuel Spigolon <behemoth89@gmail.com>

Co-authored-by: Manuel Spigolon <behemoth89@gmail.com>
  • Loading branch information
Uzlopak and Eomm committed Oct 8, 2022
1 parent 6e2b87b commit a1daf33
Show file tree
Hide file tree
Showing 15 changed files with 475 additions and 459 deletions.
22 changes: 0 additions & 22 deletions .eslintrc.json

This file was deleted.

1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
7 changes: 0 additions & 7 deletions .prettierrc.json

This file was deleted.

2 changes: 2 additions & 0 deletions .taprc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
files:
- test/**/*.test.js
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![NPM Version][npm-image]][npm-url]
[![Build Status](https://github.com/fastify/fastify-funky/workflows/CI/badge.svg)](https://github.com/fastify/fastify-funky/actions)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat)](https://github.com/prettier/prettier)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)

Support for Fastify routes returning functional structures, such as `fp-ts` Either, Task, TaskEither, or plain JavaScript parameterless functions.
Let's go funky, let's go functional!
Expand Down
77 changes: 62 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,62 @@
const { fastifyFunky } = require('./lib/funkyPlugin')

/**
* These export configurations enable JS and TS developers
* to consume fastify-funky in whatever way best suits their needs.
* Some examples of supported import syntax includes:
* - `const fastifyFunky = require('fastify-funky')`
* - `const { fastifyFunky } = require('fastify-funky')`
* - `import * as fastifyFunky from 'fastify-funky'`
* - `import { fastifyFunky } from 'fastify-funky'`
* - `import fastifyFunky from 'fastify-funky'`
*/
fastifyFunky.fastifyFunky = fastifyFunky
fastifyFunky.default = fastifyFunky
module.exports = fastifyFunky
'use strict'

const utilTypes = require('util').types
const fp = require('fastify-plugin')

function resolvePayload (done, err, result, reply) {
if (typeof result === 'string') {
reply.type('text/plain; charset=utf-8').serializer(String)
}
return done(err, result)
}

function plugin (fastify, opts, next) {
fastify.addHook('preSerialization', (req, res, payload, done) => {
// Handle Either
if (isEither(payload)) {
return resolvePayload(done, payload.left, payload.right, res)
}

// Handle Task
if (isTask(payload)) {
const result = payload()
if (isPromise(result)) {
result
.then((taskResult) => {
if (isEither(taskResult)) {
return resolvePayload(done, taskResult.left, taskResult.right, res)
}

return resolvePayload(done, null, taskResult, res)
})
.catch(done)
return
}
if (isEither(result)) {
return resolvePayload(done, result.left, result.right, res)
}
return resolvePayload(done, null, result, this)
}

done(null, payload)
})

next()
}

function isEither (payload) {
return payload.left || payload.right
}

function isPromise (value) {
return utilTypes.isPromise(value)
}

function isTask (value) {
return typeof value === 'function' && value.length === 0
}

module.exports = fp(plugin, {
fastify: '4.x',
name: '@fastify/funky'
})
19 changes: 0 additions & 19 deletions jest.config.json

This file was deleted.

66 changes: 0 additions & 66 deletions lib/funkyPlugin.js

This file was deleted.

18 changes: 7 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,26 @@
}
],
"main": "index.js",
"types": "index.d.ts",
"types": "types/index.d.ts",
"scripts": {
"test": "npm run test:unit && npm run test:typescript",
"test:unit": "jest --config=jest.config.json --coverage",
"test:unit": "tap",
"test:typescript": "tsd",
"lint": "eslint \"lib/**/*.js\" \"test/**/*.js\" index.js",
"prettier": "prettier --write \"{lib,test}/**/*.js\" index.js index.d.ts"
"lint": "standard"
},
"dependencies": {
"fastify-plugin": "^4.0.0"
},
"peerDependencies": {
"fastify": "^3.0.0"
"fastify": "^4.0.0"
},
"devDependencies": {
"@fastify/pre-commit": "2.0.2",
"@types/node": "^18.0.0",
"eslint": "^8.16.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"fastify": "^4.2.0",
"fp-ts": "^2.11.2",
"jest": "^29.0.1",
"prettier": "^2.4.1",
"standard": "^17.0.0",
"tap": "^16.3.0",
"tsd": "^0.20.0"
},
"homepage": "https://github.com/fastify/fastify-funky",
Expand All @@ -57,7 +53,7 @@
"LICENSE",
"lib/*",
"index.js",
"index.d.ts"
"types/index.d.ts"
],
"publishConfig": {
"access": "public"
Expand Down
Loading

0 comments on commit a1daf33

Please sign in to comment.