You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					33 lines
				
				716 B
			
		
		
			
		
	
	
					33 lines
				
				716 B
			| 
											3 years ago
										 | 'use strict' | ||
|  | 
 | ||
|  | const fp = require('fastify-plugin') | ||
|  | const { parse } = require('querystring') | ||
|  | 
 | ||
|  | function defaultParser (str) { | ||
|  |   return parse(str) | ||
|  | } | ||
|  | 
 | ||
|  | function formBodyPlugin (fastify, options, next) { | ||
|  |   const opts = Object.assign({ parser: defaultParser }, options) | ||
|  |   if (typeof opts.parser !== 'function') { | ||
|  |     next(new Error('parser must be a function')) | ||
|  |     return | ||
|  |   } | ||
|  | 
 | ||
|  |   function contentParser (req, body, done) { | ||
|  |     done(null, opts.parser(body.toString())) | ||
|  |   } | ||
|  | 
 | ||
|  |   fastify.addContentTypeParser( | ||
|  |     'application/x-www-form-urlencoded', | ||
|  |     { parseAs: 'buffer', bodyLimit: opts.bodyLimit }, | ||
|  |     contentParser | ||
|  |   ) | ||
|  |   next() | ||
|  | } | ||
|  | 
 | ||
|  | module.exports = fp(formBodyPlugin, { | ||
|  |   fastify: '^3.0.0', | ||
|  |   name: 'fastify-formbody' | ||
|  | }) |