@odatnurd/cf-requests 0.1.5 → 0.1.6
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/aegis/index.js +42 -3
- package/package.json +1 -1
package/aegis/index.js
CHANGED
|
@@ -68,6 +68,22 @@ export async function schemaTest(dataType, schema, data, validator) {
|
|
|
68
68
|
// not throw an error.
|
|
69
69
|
const next = () => {};
|
|
70
70
|
|
|
71
|
+
// For form data, we need to create a single, shared request object *before*
|
|
72
|
+
// we build the context, so that both the header and the body can be derived
|
|
73
|
+
// from the same source, ensuring the multipart boundary matches.
|
|
74
|
+
let tempRequest = null;
|
|
75
|
+
if (dataType === 'form') {
|
|
76
|
+
const formData = new FormData();
|
|
77
|
+
for (const key in data) {
|
|
78
|
+
formData.append(key, data[key]);
|
|
79
|
+
}
|
|
80
|
+
tempRequest = new Request('http://localhost', {
|
|
81
|
+
method: 'POST',
|
|
82
|
+
body: formData,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
|
|
71
87
|
// In order to run the test we need to create a fake Hono context object to
|
|
72
88
|
// pass to the middleware; this mimics the smallest possible footprint of
|
|
73
89
|
// Hono context for our purposes.
|
|
@@ -78,9 +94,32 @@ export async function schemaTest(dataType, schema, data, validator) {
|
|
|
78
94
|
// header, in which case it invokes the header() function with no name.
|
|
79
95
|
param: () => data,
|
|
80
96
|
json: async () => data,
|
|
81
|
-
query: () => data,
|
|
97
|
+
query: (key) => data[key],
|
|
98
|
+
queries: (key) => {
|
|
99
|
+
const result = {};
|
|
100
|
+
for(const [k, v] of Object.entries(data)) {
|
|
101
|
+
result[k] = Array.isArray(v) ? v : [v];
|
|
102
|
+
}
|
|
103
|
+
return key ? result[key] : result;
|
|
104
|
+
},
|
|
82
105
|
cookie: () => data,
|
|
83
|
-
formData: async () =>
|
|
106
|
+
formData: async () => {
|
|
107
|
+
if (dataType === 'form') {
|
|
108
|
+
return tempRequest.formData();
|
|
109
|
+
}
|
|
110
|
+
// Fallback for other types, though not strictly needed by the validator
|
|
111
|
+
const formData = new FormData();
|
|
112
|
+
for (const key in data) {
|
|
113
|
+
formData.append(key, data[key]);
|
|
114
|
+
}
|
|
115
|
+
return formData;
|
|
116
|
+
},
|
|
117
|
+
// For form data, the validator expects to be able to get the raw body
|
|
118
|
+
// as an ArrayBuffer. We can simulate this by URL-encoding the data.
|
|
119
|
+
arrayBuffer: async () => tempRequest ? tempRequest.arrayBuffer() : new ArrayBuffer(0),
|
|
120
|
+
// The validator also uses a bodyCache property to store parsed bodies.
|
|
121
|
+
bodyCache: {},
|
|
122
|
+
|
|
84
123
|
|
|
85
124
|
// We need to populate an actual cookie header in headers for it the
|
|
86
125
|
// validator to be able to pull cookie data because it wants to parse it
|
|
@@ -100,7 +139,7 @@ export async function schemaTest(dataType, schema, data, validator) {
|
|
|
100
139
|
|
|
101
140
|
return name.toLowerCase() !== 'content-type' ? undefined : {
|
|
102
141
|
json: 'application/json',
|
|
103
|
-
form: '
|
|
142
|
+
form: tempRequest?.headers.get('Content-Type'),
|
|
104
143
|
}[dataType];
|
|
105
144
|
},
|
|
106
145
|
|
package/package.json
CHANGED