galbe 0.13.0 → 0.13.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/package.json +1 -1
- package/src/parser.ts +19 -1
- package/src/types.ts +2 -1
package/package.json
CHANGED
package/src/parser.ts
CHANGED
|
@@ -12,6 +12,7 @@ import type {
|
|
|
12
12
|
STNull,
|
|
13
13
|
STPropsValue,
|
|
14
14
|
STUnion,
|
|
15
|
+
STIntersection,
|
|
15
16
|
} from './schema'
|
|
16
17
|
|
|
17
18
|
import { readableStreamToArrayBuffer } from 'bun'
|
|
@@ -112,13 +113,20 @@ export const requestBodyParser = async (
|
|
|
112
113
|
}
|
|
113
114
|
return await streamToString(body, schema as STBodyValue)
|
|
114
115
|
} else if (contentType === 'json') {
|
|
115
|
-
if (
|
|
116
|
+
if (
|
|
117
|
+
!['object', 'json', 'boolean', 'number', 'integer', 'string', 'array', 'union', 'intersection'].includes(kind)
|
|
118
|
+
)
|
|
116
119
|
throw new RequestError({ status: 400, payload: { body: `Not a valid body` } })
|
|
117
120
|
if (kind === 'union') {
|
|
118
121
|
let str = body === null ? 'null' : await streamToString(body)
|
|
119
122
|
let json = JSON.parse(str)
|
|
120
123
|
return unionize(json, schema)
|
|
121
124
|
}
|
|
125
|
+
if (kind === 'intersection') {
|
|
126
|
+
let str = body === null ? 'null' : await streamToString(body)
|
|
127
|
+
let json = JSON.parse(str)
|
|
128
|
+
return intersectionize(json, schema)
|
|
129
|
+
}
|
|
122
130
|
const str = body === null ? 'null' : await streamToString(body)
|
|
123
131
|
let json
|
|
124
132
|
try {
|
|
@@ -753,3 +761,13 @@ const unionize = (b: any, schema: STUnion) => {
|
|
|
753
761
|
else if (error) throw new RequestError({ status: 400, payload: { body: error } })
|
|
754
762
|
else throw new RequestError({ status: 400, payload: { body: `No matching body schema found` } })
|
|
755
763
|
}
|
|
764
|
+
|
|
765
|
+
const intersectionize = (b: any, schema: STIntersection) => {
|
|
766
|
+
let res
|
|
767
|
+
try {
|
|
768
|
+
for (let s of schema.allOf) res = validate(b, s, { parse: true })
|
|
769
|
+
return res
|
|
770
|
+
} catch (e) {
|
|
771
|
+
throw new RequestError({ status: 400, payload: { body: `No matching body schema found` } })
|
|
772
|
+
}
|
|
773
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -5,6 +5,7 @@ import type {
|
|
|
5
5
|
STBoolean,
|
|
6
6
|
STByteArray,
|
|
7
7
|
STInteger,
|
|
8
|
+
STIntersection,
|
|
8
9
|
STJson,
|
|
9
10
|
STLiteral,
|
|
10
11
|
STMultipartForm,
|
|
@@ -40,7 +41,7 @@ export type STBody =
|
|
|
40
41
|
| Partial<{
|
|
41
42
|
byteArray?: STByteArray | STStream
|
|
42
43
|
text?: STString | STLiteral | STBoolean | STNumber | STInteger | STUnion | STStream
|
|
43
|
-
json?: STJson | STObject | STBoolean | STInteger | STNumber | STString | STArray | STUnion
|
|
44
|
+
json?: STJson | STObject | STBoolean | STInteger | STNumber | STString | STArray | STUnion | STIntersection
|
|
44
45
|
urlForm?: STObject | STStream | STUnion
|
|
45
46
|
multipart?: STMultipartForm | STStream | STUnion
|
|
46
47
|
default?: STString | STByteArray | STStream | STAny
|