Gathering detailed insights and metrics for @bsnext/fastify-payload
Gathering detailed insights and metrics for @bsnext/fastify-payload
Plugin for adding raw-body data as 'request.payload' field into Fastify Requests.
npm install @bsnext/fastify-payload
Typescript
Module System
Node Version
NPM Version
TypeScript (100%)
Total Downloads
1,047
Last Day
4
Last Week
83
Last Month
378
Last Year
1,047
4 Commits
1 Watching
1 Branches
1 Contributors
Minified
Minified + Gzipped
Latest Version
1.0.5
Package Id
@bsnext/fastify-payload@1.0.5
Unpacked Size
13.84 kB
Size
4.91 kB
File Count
6
NPM Version
10.8.2
Node Version
22.8.0
Publised On
16 Oct 2024
Cumulative downloads
Total Downloads
Last day
300%
4
Compared to previous day
Last week
3.8%
83
Compared to previous week
Last month
-32.7%
378
Compared to previous month
Last year
0%
1,047
Compared to previous year
1
4
Plugin for adding raw-body data as 'request.payload' field into Fastify Requests.
Tested on Fastify v4.14+ and Node.JS v19+!
https://github.com/bsnext/fastify-payload/actions/workflows/test.yml
This parser do not have external dependencies, only "@fastify/plugin". Also, it use default secure JSON parser fron Fastify, with defined by server rules about prototype/constructor poisoning.
This plugin Supported adding extra "Content-Type's" and custom parsers.
And that only one reason, why i not use that.
1npm install @bsnext/fastify-payload
1import FastifyPayload from '@bsnext/fastify-payload'; // TS 2import { default as FastifyPayload } from "@bsnext/fastify-payload"; // MJS 3const { default: FastifyPayload } = require(`@bsnext/fastify-payload`); // CJS 4 5const server = Fastify(); 6await server.register(FastifyPayload, { 7 // Use plugin for all paths 8 // Do not use "true" in production 9 // Every request with existed payload - will eat memory 10 global: boolean = false; 11 12 // Specific paths for save payload 13 paths: string[] = []; 14 15 // Payload format 16 parse: `string`|`buffer` = `buffer`; 17 18 // Content-Type parsers. Possible add and use more parsers (XML, JSON5, ...). 19 // "default" option only for "application/json", "text/plain" or "*". 20 // Enabled for "application/json" only by default. 21 // Default parser for "*" will accept any Content-Type on Fastify Server! 22 contentTypes?: { 23 // Use "default" if wanna use default parser. 24 // Or write custom parser by function. 25 [key: string]: 'default' | function(request, payload, callback: (err, result)); 26 [`application/json`]: 'default'; 27 }; 28 29 // Plugin will output warnings when overwritten content parsers 30 // It 100% will output warning about "application/json" overwrite 31 // In 99.9% you need set "false" by hands. 32 // If make "false" by default - it can be a head-pain reason for somebody. 33 overwriteWarning?: boolean = true; 34}); 35 36server.post(url, { 37 config: { 38 // Save payload in this route? 39 // Default "false" for everything. 40 // Of course if not use "global: true" in plugin. 41 payload: boolean = false 42 } 43}, function() { 44 ... 45}) 46
1// Important note about modify default "contentParsers" object. 2// You should send all object if want enable "text/plain" or add new parser 3await server.register(FastifyPayload, 4 { 5 contentParsers: { 6 [`application/json`]: 'default'; 7 [`application/xml`]: function(...) {... your xml parse code}; 8 } 9 } 10); 11 12// If object not will have a "application/json" - it not will parse that. 13// This code will disable plugin for all, and left only "application/xml". 14await server.register(FastifyPayload, 15 { 16 contentParsers: { 17 [`application/xml`]: function(...) {...}; 18 } 19 } 20);
More information about Fastify Content-Type Parsing: https://fastify.dev/docs/v5.0.x/Reference/Server/#addcontenttypeparser
1import Fastify from 'fastify'; 2import FastifyPayload from '@bsnext/fastify-payload'; 3 4const server = Fastify(...); 5await server.register(FastifyPayload); 6 7server.post(`/request_with_payload`, { 8 config: { payload: true } 9}, function(request, response) { 10 console.log(`request.payload ->`, request.payload); 11 response.send(`ok`); 12}) 13 14server.listen({ port: 8080 }); 15
1import Fastify from 'fastify'; 2import FastifyPayload from '@bsnext/fastify-payload'; 3 4const server = Fastify(...); 5await server.register(FastifyPayload, { 6 paths: [`/request_with_payload`, `/one_more_request_with_payload`] 7}); 8 9server.post(`/request_with_payload`, function(request, response) { 10 console.log(`request.payload ->`, request.payload); 11 response.send(`ok`); 12}) 13 14server.patch(`/one_more_request_with_payload`, function(request, response) { 15 console.log(`request.payload ->`, request.payload); 16 response.send(`ok`); 17}) 18 19// Options for save payload can be shuffled: "paths" + "configs" 20server.put(`/last_request_with_payload`, { 21 config: { payload: true } 22}, function(request, response) { 23 console.log(`request.payload ->`, request.payload); 24 response.send(`ok`); 25}) 26 27server.listen({ port: 8080 }); 28
1import Fastify from 'fastify'; 2import FastifyPayload from '@bsnext/fastify-payload'; 3 4import { xml2js } from 'xml-js'; 5 6const server = Fastify(); 7await server.register(FastifyPayload, { 8 contentTypes: { 9 [`application/json`]: 'default', 10 [`application/xml`]: function (request, payload, done) { 11 try { 12 done(null, xml2js(payload)); 13 } catch (error) { 14 done(error); 15 } 16 } 17 } 18}); 19 20server.post(`/convert_xml_to_json`, { 21 config: { payload: true } 22}, function (request, response) { 23 console.log(`request.payload ->`, request.payload); 24 /* 25 request.payload -> <hi> 26 <text>How are you?</text> 27 </hi> 28 */ 29 response.send(request.body); 30 /* 31 { 32 "elements": [ 33 { "type": "element", "name": "hi", "elements": [...] } 34 ] 35 } 36 */ 37}); 38 39server.listen({ port: 8080 }); 40
No vulnerabilities found.
No security vulnerabilities found.