@odatnurd/cf-requests 0.1.4 → 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 +57 -5
- package/package.json +1 -1
package/aegis/index.js
CHANGED
|
@@ -68,26 +68,78 @@ 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.
|
|
74
90
|
const ctx = {
|
|
75
91
|
req: {
|
|
76
92
|
// These methods are used by the validator to pull the parsed data out of
|
|
77
|
-
// the request in order to validate it
|
|
93
|
+
// the request in order to validate it, except for when the data type is
|
|
94
|
+
// header, in which case it invokes the header() function with no name.
|
|
78
95
|
param: () => data,
|
|
79
96
|
json: async () => data,
|
|
80
|
-
query: () => data,
|
|
81
|
-
|
|
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
|
-
|
|
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
|
+
|
|
123
|
+
|
|
124
|
+
// We need to populate an actual cookie header in headers for it the
|
|
125
|
+
// validator to be able to pull cookie data because it wants to parse it
|
|
126
|
+
// itself.
|
|
127
|
+
headers: new Headers(dataType === 'cookie'
|
|
128
|
+
? { 'Cookie': Object.entries(data).map(([k,v]) => `${k}=${v}`).join('; ') }
|
|
129
|
+
: {}),
|
|
84
130
|
|
|
85
131
|
// The validator invokes this to get headers out of the request when the
|
|
86
132
|
// data type is JSON.
|
|
87
133
|
header: (name) => {
|
|
134
|
+
// If there is no name, return the data back directly; this call pattern
|
|
135
|
+
// happens when the data type is header.
|
|
136
|
+
if (name === undefined) {
|
|
137
|
+
return data;
|
|
138
|
+
}
|
|
139
|
+
|
|
88
140
|
return name.toLowerCase() !== 'content-type' ? undefined : {
|
|
89
141
|
json: 'application/json',
|
|
90
|
-
form: '
|
|
142
|
+
form: tempRequest?.headers.get('Content-Type'),
|
|
91
143
|
}[dataType];
|
|
92
144
|
},
|
|
93
145
|
|
package/package.json
CHANGED