@koa-stack/body 0.16.0 → 0.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/package.json +22 -11
- package/src/index.ts +54 -6
- package/build/index.d.ts +0 -65
- package/build/index.d.ts.map +0 -1
- package/build/index.js +0 -193
- package/build/index.js.map +0 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Bogdan Stefanescu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
CHANGED
|
@@ -1,16 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@koa-stack/body",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.18.0",
|
|
4
4
|
"description": "Expose the request body as a promise in koa Context.body",
|
|
5
|
-
"main": "./
|
|
6
|
-
"
|
|
7
|
-
"build": "npm run clean && tsc --build",
|
|
8
|
-
"clean": "rimraf ./build ./tsconfig.tsbuildinfo",
|
|
9
|
-
"prepublish": "npm run build",
|
|
10
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
11
|
-
},
|
|
5
|
+
"main": "./lib/cjs/index.js",
|
|
6
|
+
"type": "module",
|
|
12
7
|
"files": [
|
|
13
|
-
"
|
|
8
|
+
"lib",
|
|
14
9
|
"src"
|
|
15
10
|
],
|
|
16
11
|
"keywords": [
|
|
@@ -30,6 +25,22 @@
|
|
|
30
25
|
"devDependencies": {
|
|
31
26
|
"@types/formidable": "^3.4.5",
|
|
32
27
|
"@types/inflation": "^2.0.4",
|
|
33
|
-
"@types/qs": "^6.9.15"
|
|
28
|
+
"@types/qs": "^6.9.15",
|
|
29
|
+
"fast-xml-parser": "^5.2.5",
|
|
30
|
+
"ts-dual-module": "^0.6.3"
|
|
31
|
+
},
|
|
32
|
+
"ts_dual_module": {
|
|
33
|
+
"outDir": "lib"
|
|
34
|
+
},
|
|
35
|
+
"exports": {
|
|
36
|
+
"types": "./lib/types/index.d.ts",
|
|
37
|
+
"import": "./lib/esm/index.js",
|
|
38
|
+
"require": "./lib/cjs/index.js"
|
|
39
|
+
},
|
|
40
|
+
"types": "./lib/types/index.d.ts",
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "pnpm exec tsmod build",
|
|
43
|
+
"clean": "rimraf ./lib",
|
|
44
|
+
"test": "echo \"Error: no test specified\""
|
|
34
45
|
}
|
|
35
|
-
}
|
|
46
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -3,10 +3,12 @@ import inflate from 'inflation';
|
|
|
3
3
|
import qs from 'qs';
|
|
4
4
|
import formidable from 'formidable';
|
|
5
5
|
import Koa, { Context, Request } from 'koa';
|
|
6
|
+
import type { XMLParser } from 'fast-xml-parser';
|
|
6
7
|
|
|
7
8
|
declare module 'koa' {
|
|
8
9
|
interface BaseContext {
|
|
9
|
-
payload: Promise<LazyBody
|
|
10
|
+
payload: Promise<LazyBody>,
|
|
11
|
+
hasPayload: boolean,
|
|
10
12
|
}
|
|
11
13
|
}
|
|
12
14
|
|
|
@@ -70,6 +72,10 @@ export class LazyBody {
|
|
|
70
72
|
this.files = files;
|
|
71
73
|
}
|
|
72
74
|
|
|
75
|
+
get isEmpty() {
|
|
76
|
+
return this.raw === '';
|
|
77
|
+
}
|
|
78
|
+
|
|
73
79
|
assertJSON(statusCode: number, message: string) {
|
|
74
80
|
if (this.type !== FormType.json) {
|
|
75
81
|
this.ctx.throw(statusCode || 415, message);
|
|
@@ -181,6 +187,13 @@ export class LazyBody {
|
|
|
181
187
|
this.request.body = body;
|
|
182
188
|
}
|
|
183
189
|
});
|
|
190
|
+
// payload is an alias to request.hasBody in v3
|
|
191
|
+
Object.defineProperty(koa.context, 'hasPayload', {
|
|
192
|
+
get() {
|
|
193
|
+
return this.request.length > 0 ||
|
|
194
|
+
this.request.headers['transfer-encoding'] != null;
|
|
195
|
+
},
|
|
196
|
+
});
|
|
184
197
|
}
|
|
185
198
|
|
|
186
199
|
}
|
|
@@ -203,20 +216,55 @@ async function createBody(koaRequest: Request, opts: LazyBodyOpts) {
|
|
|
203
216
|
type = FormType.form;
|
|
204
217
|
} else if (koaRequest.is('json', '+json')) {
|
|
205
218
|
// by default we use JSON.parse. You can replace the json parser using opts.json
|
|
206
|
-
|
|
207
|
-
|
|
219
|
+
if (koaRequest.ctx.hasPayload) {
|
|
220
|
+
raw = await getRawBodyText(koaRequest, opts);
|
|
221
|
+
} else {
|
|
222
|
+
raw = '';
|
|
223
|
+
}
|
|
224
|
+
if (raw === '') { // for JSON we support no content posted
|
|
225
|
+
data = undefined;
|
|
226
|
+
} else {
|
|
227
|
+
data = opts.json ? opts.json(raw) : JSON.parse(raw);
|
|
228
|
+
}
|
|
208
229
|
type = FormType.json;
|
|
209
230
|
} else if (koaRequest.is('xml', '+xml')) {
|
|
210
231
|
// by default fast-xml-parser is used - to change the parser you should provde an xml parser through opts.xml
|
|
211
232
|
raw = await getRawBodyText(koaRequest, opts);
|
|
212
|
-
|
|
233
|
+
if (opts.xml) {
|
|
234
|
+
data = opts.xml(raw);
|
|
235
|
+
} else {
|
|
236
|
+
let parser = await tryGetXmlParser();
|
|
237
|
+
if (parser) {
|
|
238
|
+
data = parser.parse(raw);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
213
241
|
type = FormType.xml;
|
|
214
242
|
} else if (koaRequest.is('text/*')) {
|
|
215
243
|
raw = await getRawBodyText(koaRequest, opts);
|
|
216
244
|
type = FormType.text;
|
|
217
245
|
} else {
|
|
218
|
-
|
|
219
|
-
|
|
246
|
+
type = FormType.text;
|
|
247
|
+
// if has body
|
|
248
|
+
console.log();
|
|
249
|
+
if (koaRequest.ctx.hasPayload) {
|
|
250
|
+
raw = await getRawBodyText(koaRequest, opts);
|
|
251
|
+
} else {
|
|
252
|
+
raw = '';
|
|
253
|
+
}
|
|
220
254
|
}
|
|
221
255
|
return new LazyBody(koaRequest.ctx, type, data, raw, files);
|
|
222
256
|
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Use fast-xml-parser if available
|
|
260
|
+
* @returns
|
|
261
|
+
*/
|
|
262
|
+
async function tryGetXmlParser(): Promise<XMLParser | undefined> {
|
|
263
|
+
try {
|
|
264
|
+
const mod = await import("fast-xml-parser") as typeof import("fast-xml-parser");
|
|
265
|
+
return new mod.XMLParser();
|
|
266
|
+
} catch (err) {
|
|
267
|
+
console.warn("Could not find fast-xml-parser. You need to pass xml option for a custom parser", err);
|
|
268
|
+
return undefined;
|
|
269
|
+
}
|
|
270
|
+
}
|
package/build/index.d.ts
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import readRawBody from 'raw-body';
|
|
2
|
-
import inflate from 'inflation';
|
|
3
|
-
import formidable from 'formidable';
|
|
4
|
-
import Koa, { Context } from 'koa';
|
|
5
|
-
declare module 'koa' {
|
|
6
|
-
interface BaseContext {
|
|
7
|
-
payload: Promise<LazyBody>;
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
export interface OwnOpts {
|
|
11
|
-
formidable?: formidable.Options;
|
|
12
|
-
inflate?: inflate.Options;
|
|
13
|
-
form?: (data: string) => any;
|
|
14
|
-
json?: (data: string) => any;
|
|
15
|
-
xml?: (data: string) => any;
|
|
16
|
-
}
|
|
17
|
-
export type LazyBodyOpts = readRawBody.Options & OwnOpts;
|
|
18
|
-
/**
|
|
19
|
-
* Request body class
|
|
20
|
-
* json
|
|
21
|
-
*
|
|
22
|
-
*/
|
|
23
|
-
export declare class LazyBody {
|
|
24
|
-
ctx: Context;
|
|
25
|
-
type: FormType;
|
|
26
|
-
data: any;
|
|
27
|
-
raw: string;
|
|
28
|
-
files: formidable.Files | null | undefined;
|
|
29
|
-
constructor(ctx: Context, type: FormType, data: any, raw: string, files: formidable.Files | null | undefined);
|
|
30
|
-
assertJSON(statusCode: number, message: string): void;
|
|
31
|
-
assertXML(statusCode: number, message: string): void;
|
|
32
|
-
/**
|
|
33
|
-
* Assert body type is urlencoded
|
|
34
|
-
* @param {*} statusCode
|
|
35
|
-
* @param {*} message
|
|
36
|
-
*/
|
|
37
|
-
assertForm(statusCode: number, message: string): void;
|
|
38
|
-
assertMultipart(statusCode: number, message: string): void;
|
|
39
|
-
assertText(statusCode: number, message: string): void;
|
|
40
|
-
/**
|
|
41
|
-
* Assert that body type is either multipart or urlencoded
|
|
42
|
-
* @param {*} statusCode
|
|
43
|
-
* @param {*} message
|
|
44
|
-
*/
|
|
45
|
-
assertParams(statusCode: number, message: string): void;
|
|
46
|
-
get text(): string;
|
|
47
|
-
get json(): any;
|
|
48
|
-
get xml(): any;
|
|
49
|
-
get params(): any;
|
|
50
|
-
get isForm(): boolean;
|
|
51
|
-
get isJSON(): boolean;
|
|
52
|
-
get isXML(): boolean;
|
|
53
|
-
get isMultipart(): boolean;
|
|
54
|
-
get isText(): boolean;
|
|
55
|
-
static install(koa: Koa, opts?: LazyBodyOpts): void;
|
|
56
|
-
}
|
|
57
|
-
declare enum FormType {
|
|
58
|
-
'multipart' = 0,
|
|
59
|
-
'form' = 1,
|
|
60
|
-
'json' = 2,
|
|
61
|
-
'xml' = 3,
|
|
62
|
-
'text' = 4
|
|
63
|
-
}
|
|
64
|
-
export {};
|
|
65
|
-
//# sourceMappingURL=index.d.ts.map
|
package/build/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,WAAW,MAAM,UAAU,CAAC;AACnC,OAAO,OAAO,MAAM,WAAW,CAAC;AAEhC,OAAO,UAAU,MAAM,YAAY,CAAC;AACpC,OAAO,GAAG,EAAE,EAAE,OAAO,EAAW,MAAM,KAAK,CAAC;AAE5C,OAAO,QAAQ,KAAK,CAAC;IACjB,UAAU,WAAW;QACjB,OAAO,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAA;KAC7B;CACJ;AAED,MAAM,WAAW,OAAO;IACpB,UAAU,CAAC,EAAE,UAAU,CAAC,OAAO,CAAC;IAChC,OAAO,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC;IAC1B,IAAI,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,GAAG,CAAC;IAC7B,IAAI,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,GAAG,CAAC;IAC7B,GAAG,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,GAAG,CAAC;CAC/B;AACD,MAAM,MAAM,YAAY,GAAG,WAAW,CAAC,OAAO,GAAG,OAAO,CAAC;AAgCzD;;;;GAIG;AACH,qBAAa,QAAQ;IAEjB,GAAG,EAAE,OAAO,CAAC;IACb,IAAI,EAAE,QAAQ,CAAC;IACf,IAAI,EAAE,GAAG,CAAC;IACV,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,UAAU,CAAC,KAAK,GAAG,IAAI,GAAG,SAAS,CAAC;gBAE/B,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU,CAAC,KAAK,GAAG,IAAI,GAAG,SAAS;IAQ5G,UAAU,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;IAM9C,SAAS,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;IAM7C;;;;OAIG;IACH,UAAU,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;IAM9C,eAAe,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;IAMnD,UAAU,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;IAM9C;;;;OAIG;IACH,YAAY,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;IAMhD,IAAI,IAAI,WAEP;IAED,IAAI,IAAI,QAEP;IAED,IAAI,GAAG,QAEN;IAED,IAAI,MAAM,QAOT;IAED,IAAI,MAAM,YAET;IAED,IAAI,MAAM,YAET;IAED,IAAI,KAAK,YAER;IAED,IAAI,WAAW,YAEd;IAED,IAAI,MAAM,YAET;IAED,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,GAAE,YAAiB,GAAG,IAAI;CA0B1D;AACD,aAAK,QAAQ;IACT,WAAW,IAAA;IAAE,MAAM,IAAA;IAAE,MAAM,IAAA;IAAE,KAAK,IAAA;IAAE,MAAM,IAAA;CAC7C"}
|
package/build/index.js
DELETED
|
@@ -1,193 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.LazyBody = void 0;
|
|
7
|
-
const raw_body_1 = __importDefault(require("raw-body"));
|
|
8
|
-
const inflation_1 = __importDefault(require("inflation"));
|
|
9
|
-
const qs_1 = __importDefault(require("qs"));
|
|
10
|
-
const formidable_1 = __importDefault(require("formidable"));
|
|
11
|
-
function parseMultipartBody(koaRequest, opts) {
|
|
12
|
-
const form = (0, formidable_1.default)(opts.formidable);
|
|
13
|
-
return new Promise((resolve, reject) => {
|
|
14
|
-
form.parse(koaRequest.req, (err, fields, files) => {
|
|
15
|
-
if (err) {
|
|
16
|
-
reject(err);
|
|
17
|
-
}
|
|
18
|
-
else {
|
|
19
|
-
resolve({
|
|
20
|
-
params: fields,
|
|
21
|
-
files: files
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
});
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
async function getRawBodyText(koaRequest, opts) {
|
|
28
|
-
const len = koaRequest.length;
|
|
29
|
-
let encoding = koaRequest.headers['content-encoding'];
|
|
30
|
-
if (len && !encoding) {
|
|
31
|
-
opts.length = len;
|
|
32
|
-
}
|
|
33
|
-
// TODO how to detect custom encoding on content type to ser encoding for readRawBody
|
|
34
|
-
return (await (0, raw_body_1.default)((0, inflation_1.default)(koaRequest.req, opts.inflate), opts)).toString( /*which charset?*/);
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Request body class
|
|
38
|
-
* json
|
|
39
|
-
*
|
|
40
|
-
*/
|
|
41
|
-
class LazyBody {
|
|
42
|
-
constructor(ctx, type, data, raw, files) {
|
|
43
|
-
this.ctx = ctx;
|
|
44
|
-
this.type = type;
|
|
45
|
-
this.data = data;
|
|
46
|
-
this.raw = raw;
|
|
47
|
-
this.files = files;
|
|
48
|
-
}
|
|
49
|
-
assertJSON(statusCode, message) {
|
|
50
|
-
if (this.type !== FormType.json) {
|
|
51
|
-
this.ctx.throw(statusCode || 415, message);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
assertXML(statusCode, message) {
|
|
55
|
-
if (this.type !== FormType.xml) {
|
|
56
|
-
this.ctx.throw(statusCode || 415, message);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Assert body type is urlencoded
|
|
61
|
-
* @param {*} statusCode
|
|
62
|
-
* @param {*} message
|
|
63
|
-
*/
|
|
64
|
-
assertForm(statusCode, message) {
|
|
65
|
-
if (this.type !== FormType.form) {
|
|
66
|
-
this.ctx.throw(statusCode || 415, message);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
assertMultipart(statusCode, message) {
|
|
70
|
-
if (this.type !== FormType.multipart) {
|
|
71
|
-
this.ctx.throw(statusCode || 415, message);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
assertText(statusCode, message) {
|
|
75
|
-
if (this.type !== FormType.text) {
|
|
76
|
-
this.ctx.throw(statusCode || 415, message);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* Assert that body type is either multipart or urlencoded
|
|
81
|
-
* @param {*} statusCode
|
|
82
|
-
* @param {*} message
|
|
83
|
-
*/
|
|
84
|
-
assertParams(statusCode, message) {
|
|
85
|
-
if (this.type !== FormType.form && this.type !== FormType.multipart) {
|
|
86
|
-
this.ctx.throw(statusCode || 415, message);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
get text() {
|
|
90
|
-
return this.raw;
|
|
91
|
-
}
|
|
92
|
-
get json() {
|
|
93
|
-
return this.isJSON ? this.data : undefined;
|
|
94
|
-
}
|
|
95
|
-
get xml() {
|
|
96
|
-
return this.isXML ? this.data : undefined;
|
|
97
|
-
}
|
|
98
|
-
get params() {
|
|
99
|
-
switch (this.type) {
|
|
100
|
-
case FormType.form:
|
|
101
|
-
case FormType.multipart:
|
|
102
|
-
return this.data;
|
|
103
|
-
default: return undefined;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
get isForm() {
|
|
107
|
-
return this.type === FormType.form;
|
|
108
|
-
}
|
|
109
|
-
get isJSON() {
|
|
110
|
-
return this.type === FormType.json;
|
|
111
|
-
}
|
|
112
|
-
get isXML() {
|
|
113
|
-
return this.type === FormType.xml;
|
|
114
|
-
}
|
|
115
|
-
get isMultipart() {
|
|
116
|
-
return this.type === FormType.multipart;
|
|
117
|
-
}
|
|
118
|
-
get isText() {
|
|
119
|
-
return this.type === FormType.text;
|
|
120
|
-
}
|
|
121
|
-
static install(koa, opts = {}) {
|
|
122
|
-
opts.encoding = opts.encoding || 'utf8';
|
|
123
|
-
opts.limit = opts.limit || '1mb';
|
|
124
|
-
Object.defineProperty(koa.request, 'body', {
|
|
125
|
-
get() {
|
|
126
|
-
if (!this._body) {
|
|
127
|
-
this._body = createBody(this, opts);
|
|
128
|
-
}
|
|
129
|
-
return this._body;
|
|
130
|
-
},
|
|
131
|
-
// be able to work along code using koa2-formidable (wich sets the body property pf the request)
|
|
132
|
-
set(body) {
|
|
133
|
-
this._body = body;
|
|
134
|
-
}
|
|
135
|
-
});
|
|
136
|
-
// payload is an alias to request.body
|
|
137
|
-
Object.defineProperty(koa.context, 'payload', {
|
|
138
|
-
get() {
|
|
139
|
-
return this.request.body;
|
|
140
|
-
},
|
|
141
|
-
set(body) {
|
|
142
|
-
this.request.body = body;
|
|
143
|
-
}
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
exports.LazyBody = LazyBody;
|
|
148
|
-
var FormType;
|
|
149
|
-
(function (FormType) {
|
|
150
|
-
FormType[FormType["multipart"] = 0] = "multipart";
|
|
151
|
-
FormType[FormType["form"] = 1] = "form";
|
|
152
|
-
FormType[FormType["json"] = 2] = "json";
|
|
153
|
-
FormType[FormType["xml"] = 3] = "xml";
|
|
154
|
-
FormType[FormType["text"] = 4] = "text";
|
|
155
|
-
})(FormType || (FormType = {}));
|
|
156
|
-
async function createBody(koaRequest, opts) {
|
|
157
|
-
let type, data, raw, files = null;
|
|
158
|
-
if (koaRequest.is('multipart')) {
|
|
159
|
-
raw = '';
|
|
160
|
-
const result = await parseMultipartBody(koaRequest, opts);
|
|
161
|
-
data = result.params || {};
|
|
162
|
-
files = result.files || {};
|
|
163
|
-
type = FormType.multipart;
|
|
164
|
-
}
|
|
165
|
-
else if (koaRequest.is('urlencoded')) {
|
|
166
|
-
// by default we use qs. You can replace the querystring parser using opts.form
|
|
167
|
-
raw = await getRawBodyText(koaRequest, opts);
|
|
168
|
-
data = opts.form ? opts.form(raw) : qs_1.default.parse(raw);
|
|
169
|
-
type = FormType.form;
|
|
170
|
-
}
|
|
171
|
-
else if (koaRequest.is('json', '+json')) {
|
|
172
|
-
// by default we use JSON.parse. You can replace the json parser using opts.json
|
|
173
|
-
raw = await getRawBodyText(koaRequest, opts);
|
|
174
|
-
data = opts.json ? opts.json(raw) : JSON.parse(raw);
|
|
175
|
-
type = FormType.json;
|
|
176
|
-
}
|
|
177
|
-
else if (koaRequest.is('xml', '+xml')) {
|
|
178
|
-
// by default fast-xml-parser is used - to change the parser you should provde an xml parser through opts.xml
|
|
179
|
-
raw = await getRawBodyText(koaRequest, opts);
|
|
180
|
-
data = opts.xml ? opts.xml(raw) : require('fast-xml-parser').parse(raw);
|
|
181
|
-
type = FormType.xml;
|
|
182
|
-
}
|
|
183
|
-
else if (koaRequest.is('text/*')) {
|
|
184
|
-
raw = await getRawBodyText(koaRequest, opts);
|
|
185
|
-
type = FormType.text;
|
|
186
|
-
}
|
|
187
|
-
else {
|
|
188
|
-
koaRequest.ctx.throw(500, 'Attempting to read text body from an usupported request content type: ' + koaRequest.headers['content-type']);
|
|
189
|
-
throw '';
|
|
190
|
-
}
|
|
191
|
-
return new LazyBody(koaRequest.ctx, type, data, raw, files);
|
|
192
|
-
}
|
|
193
|
-
//# sourceMappingURL=index.js.map
|
package/build/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.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