-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
15 changed files
with
475 additions
and
459 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 @@ | ||
package-lock=false |
This file was deleted.
Oops, something went wrong.
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,2 @@ | ||
files: | ||
- test/**/*.test.js |
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
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,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' | ||
}) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.