@wooksjs/http-body 0.7.0 → 0.7.1
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/dist/index.cjs +18 -11
- package/dist/index.d.ts +4 -4
- package/dist/index.mjs +18 -11
- package/package.json +5 -5
package/dist/index.cjs
CHANGED
|
@@ -30,15 +30,22 @@ const ILLEGAL_KEYS = [
|
|
|
30
30
|
"constructor",
|
|
31
31
|
"prototype"
|
|
32
32
|
];
|
|
33
|
-
const illigalKeySet = new Set(ILLEGAL_KEYS);
|
|
34
33
|
function safeJsonParse(src) {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
});
|
|
34
|
+
const parsed = JSON.parse(src);
|
|
35
|
+
assertNoProtoKeys(parsed);
|
|
36
|
+
return parsed;
|
|
39
37
|
}
|
|
40
|
-
function
|
|
41
|
-
if (
|
|
38
|
+
function assertNoProtoKeys(obj) {
|
|
39
|
+
if (obj === null || typeof obj !== "object") return;
|
|
40
|
+
if (Array.isArray(obj)) {
|
|
41
|
+
for (const item of obj) assertNoProtoKeys(item);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const record = obj;
|
|
45
|
+
for (const key of Object.keys(record)) {
|
|
46
|
+
if (key === ILLEGAL_KEYS[0] || key === ILLEGAL_KEYS[1] || key === ILLEGAL_KEYS[2]) throw new __wooksjs_event_http.HttpError(400, `Illegal key name "${key}"`);
|
|
47
|
+
assertNoProtoKeys(record[key]);
|
|
48
|
+
}
|
|
42
49
|
}
|
|
43
50
|
|
|
44
51
|
//#endregion
|
|
@@ -137,20 +144,20 @@ function urlEncodedParser(v) {
|
|
|
137
144
|
* @example
|
|
138
145
|
* ```ts
|
|
139
146
|
* app.post('/api/data', async () => {
|
|
140
|
-
* const {
|
|
141
|
-
* if (
|
|
147
|
+
* const { is, parseBody } = useBody()
|
|
148
|
+
* if (is('json')) {
|
|
142
149
|
* const data = await parseBody<{ name: string }>()
|
|
143
150
|
* return { received: data.name }
|
|
144
151
|
* }
|
|
145
152
|
* })
|
|
146
153
|
* ```
|
|
147
154
|
*
|
|
148
|
-
* @returns Object with `
|
|
155
|
+
* @returns Object with `is(type)` checker, `parseBody` function, and `rawBody` accessor.
|
|
149
156
|
*/
|
|
150
157
|
const useBody = (0, __wooksjs_event_core.defineWook)((ctx) => {
|
|
151
158
|
const { rawBody } = (0, __wooksjs_event_http.useRequest)(ctx);
|
|
152
159
|
return {
|
|
153
|
-
|
|
160
|
+
is: (type) => contentIsSlot(type, ctx),
|
|
154
161
|
parseBody: () => ctx.get(parsedBodySlot),
|
|
155
162
|
rawBody
|
|
156
163
|
};
|
package/dist/index.d.ts
CHANGED
|
@@ -8,18 +8,18 @@ type KnownContentType = 'json' | 'html' | 'xml' | 'text' | 'binary' | 'form-data
|
|
|
8
8
|
* @example
|
|
9
9
|
* ```ts
|
|
10
10
|
* app.post('/api/data', async () => {
|
|
11
|
-
* const {
|
|
12
|
-
* if (
|
|
11
|
+
* const { is, parseBody } = useBody()
|
|
12
|
+
* if (is('json')) {
|
|
13
13
|
* const data = await parseBody<{ name: string }>()
|
|
14
14
|
* return { received: data.name }
|
|
15
15
|
* }
|
|
16
16
|
* })
|
|
17
17
|
* ```
|
|
18
18
|
*
|
|
19
|
-
* @returns Object with `
|
|
19
|
+
* @returns Object with `is(type)` checker, `parseBody` function, and `rawBody` accessor.
|
|
20
20
|
*/
|
|
21
21
|
declare const useBody: (ctx?: EventContext) => {
|
|
22
|
-
|
|
22
|
+
is: (type: KnownContentType | (string & {})) => boolean;
|
|
23
23
|
parseBody: <T>() => Promise<T>;
|
|
24
24
|
rawBody: () => Promise<Buffer<ArrayBufferLike>>;
|
|
25
25
|
};
|
package/dist/index.mjs
CHANGED
|
@@ -7,15 +7,22 @@ const ILLEGAL_KEYS = [
|
|
|
7
7
|
"constructor",
|
|
8
8
|
"prototype"
|
|
9
9
|
];
|
|
10
|
-
const illigalKeySet = new Set(ILLEGAL_KEYS);
|
|
11
10
|
function safeJsonParse(src) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
});
|
|
11
|
+
const parsed = JSON.parse(src);
|
|
12
|
+
assertNoProtoKeys(parsed);
|
|
13
|
+
return parsed;
|
|
16
14
|
}
|
|
17
|
-
function
|
|
18
|
-
if (
|
|
15
|
+
function assertNoProtoKeys(obj) {
|
|
16
|
+
if (obj === null || typeof obj !== "object") return;
|
|
17
|
+
if (Array.isArray(obj)) {
|
|
18
|
+
for (const item of obj) assertNoProtoKeys(item);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
const record = obj;
|
|
22
|
+
for (const key of Object.keys(record)) {
|
|
23
|
+
if (key === ILLEGAL_KEYS[0] || key === ILLEGAL_KEYS[1] || key === ILLEGAL_KEYS[2]) throw new HttpError(400, `Illegal key name "${key}"`);
|
|
24
|
+
assertNoProtoKeys(record[key]);
|
|
25
|
+
}
|
|
19
26
|
}
|
|
20
27
|
|
|
21
28
|
//#endregion
|
|
@@ -114,20 +121,20 @@ function urlEncodedParser(v) {
|
|
|
114
121
|
* @example
|
|
115
122
|
* ```ts
|
|
116
123
|
* app.post('/api/data', async () => {
|
|
117
|
-
* const {
|
|
118
|
-
* if (
|
|
124
|
+
* const { is, parseBody } = useBody()
|
|
125
|
+
* if (is('json')) {
|
|
119
126
|
* const data = await parseBody<{ name: string }>()
|
|
120
127
|
* return { received: data.name }
|
|
121
128
|
* }
|
|
122
129
|
* })
|
|
123
130
|
* ```
|
|
124
131
|
*
|
|
125
|
-
* @returns Object with `
|
|
132
|
+
* @returns Object with `is(type)` checker, `parseBody` function, and `rawBody` accessor.
|
|
126
133
|
*/
|
|
127
134
|
const useBody = defineWook((ctx) => {
|
|
128
135
|
const { rawBody } = useRequest(ctx);
|
|
129
136
|
return {
|
|
130
|
-
|
|
137
|
+
is: (type) => contentIsSlot(type, ctx),
|
|
131
138
|
parseBody: () => ctx.get(parsedBodySlot),
|
|
132
139
|
rawBody
|
|
133
140
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wooksjs/http-body",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"description": "@wooksjs/http-body",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"api",
|
|
@@ -42,12 +42,12 @@
|
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"typescript": "^5.9.3",
|
|
44
44
|
"vitest": "^3.2.4",
|
|
45
|
-
"@wooksjs/event-core": "^0.7.
|
|
46
|
-
"@wooksjs/event-http": "^0.7.
|
|
45
|
+
"@wooksjs/event-core": "^0.7.1",
|
|
46
|
+
"@wooksjs/event-http": "^0.7.1"
|
|
47
47
|
},
|
|
48
48
|
"peerDependencies": {
|
|
49
|
-
"@wooksjs/event-core": "^0.7.
|
|
50
|
-
"@wooksjs/event-http": "^0.7.
|
|
49
|
+
"@wooksjs/event-core": "^0.7.1",
|
|
50
|
+
"@wooksjs/event-http": "^0.7.1"
|
|
51
51
|
},
|
|
52
52
|
"scripts": {
|
|
53
53
|
"build": "rolldown -c ../../rolldown.config.mjs"
|