body-parsec
Advanced tools
Comparing version 2.1.0 to 3.0.0
@@ -5,5 +5,5 @@ /// <reference types="node" /> | ||
declare type ReqWithBody = EventEmitter & Partial<IncomingMessage> & { | ||
declare type ReqWithBody = IncomingMessage & { | ||
body?: any; | ||
}; | ||
} & EventEmitter; | ||
declare const parsec: <T extends ReqWithBody>(fn: (body: any) => void) => (req: T | ReqWithBody, _res: ServerResponse) => Promise<void>; | ||
@@ -10,0 +10,0 @@ declare const json: () => (req: ReqWithBody, _res?: ServerResponse, next?: () => void) => Promise<void>; |
@@ -1,1 +0,1 @@ | ||
import{json,parsec,raw,text,urlencoded}from"./chunk.VMHT7XW4.js";export{parsec as custom,json,raw,text,urlencoded}; | ||
import{json,parsec,raw,text,urlencoded}from"./chunk.RIRTAYR4.js";export{parsec as custom,json,raw,text,urlencoded}; |
@@ -1,1 +0,1 @@ | ||
import{parsec}from"./chunk.VMHT7XW4.js";import{parse}from"querystring";const custom=(fn=body=>body)=>async(ctx,next)=>{await parsec(fn)(ctx.req,ctx.res);next==null?void 0:next()};const json=()=>async(ctx,next)=>{await custom(x=>JSON.parse(x.toString()))(ctx);next==null?void 0:next()};const raw=()=>async(ctx,next)=>{await custom(x=>x)(ctx);next==null?void 0:next()};const text=()=>async(ctx,next)=>{await custom(x=>x.toString())(ctx);next==null?void 0:next()};const urlencoded=()=>async(ctx,next)=>{await custom(x=>parse(x.toString()))(ctx);next==null?void 0:next()};export{custom,json,raw,text,urlencoded}; | ||
import{parsec}from"./chunk.RIRTAYR4.js";import{parse}from"querystring";const custom=(fn=body=>body)=>async(ctx,next)=>{await parsec(fn)(ctx.req,ctx.res);next==null?void 0:next()};const json=()=>async(ctx,next)=>{await custom(x=>JSON.parse(x.toString()))(ctx);next==null?void 0:next()};const raw=()=>async(ctx,next)=>{await custom(x=>x)(ctx);next==null?void 0:next()};const text=()=>async(ctx,next)=>{await custom(x=>x.toString())(ctx);next==null?void 0:next()};const urlencoded=()=>async(ctx,next)=>{await custom(x=>parse(x.toString()))(ctx);next==null?void 0:next()};export{custom,json,raw,text,urlencoded}; |
{ | ||
"name": "body-parsec", | ||
"version": "2.1.0", | ||
"version": "3.0.0", | ||
"repository": "https://github.com/talentlessguy/parsec.git", | ||
@@ -5,0 +5,0 @@ "author": "talentlessguy <pilll.PL22@gmail.com>", |
@@ -93,5 +93,5 @@ # parsec 🌌 | ||
res.send(` | ||
<urlencoded method="POST" action="/"> | ||
<form method="POST" action="/" enctype="application/x-www-form-urlencoded"> | ||
<input name="name" /> | ||
</urlencoded> | ||
</form> | ||
`) | ||
@@ -98,0 +98,0 @@ }) |
import { ServerResponse as Response, IncomingMessage } from 'http' | ||
import * as qs from 'querystring' | ||
import { once, EventEmitter, on } from 'events' | ||
import { once, EventEmitter } from 'events' | ||
// Extend the request object with body | ||
export type ReqWithBody = EventEmitter & | ||
Partial<IncomingMessage> & { | ||
body?: any | ||
} | ||
export type ReqWithBody = IncomingMessage & { | ||
body?: any | ||
} & EventEmitter | ||
@@ -14,7 +13,9 @@ // Main function | ||
if (['POST', 'PUT', 'PATCH', 'DELETE'].includes(req.method)) { | ||
const body = await once(req, 'data').then((data) => { | ||
return fn(data) | ||
}) | ||
let body = '' | ||
req.body = body | ||
for await (const chunk of req) { | ||
body += chunk | ||
} | ||
req.body = fn(body) | ||
} | ||
@@ -21,0 +22,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
26904
29
219
9