@odatnurd/cf-requests 0.1.3 → 0.1.5

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.
Files changed (2) hide show
  1. package/aegis/index.js +21 -5
  2. package/package.json +1 -1
package/aegis/index.js CHANGED
@@ -26,7 +26,7 @@ export function initializeRequestChecks() {
26
26
  );
27
27
 
28
28
  addCheck.value.isResponseWithStatus(
29
- (source, status) => source instanceof Response && source.status == status
29
+ (source, status) => source instanceof Response && source.status === status
30
30
  );
31
31
  }
32
32
 
@@ -74,18 +74,34 @@ export async function schemaTest(dataType, schema, data, validator) {
74
74
  const ctx = {
75
75
  req: {
76
76
  // These methods are used by the validator to pull the parsed data out of
77
- // the request in order to validate it.
77
+ // the request in order to validate it, except for when the data type is
78
+ // header, in which case it invokes the header() function with no name.
78
79
  param: () => data,
79
80
  json: async () => data,
80
81
  query: () => data,
82
+ cookie: () => data,
83
+ formData: async () => data,
84
+
85
+ // We need to populate an actual cookie header in headers for it the
86
+ // validator to be able to pull cookie data because it wants to parse it
87
+ // itself.
88
+ headers: new Headers(dataType === 'cookie'
89
+ ? { 'Cookie': Object.entries(data).map(([k,v]) => `${k}=${v}`).join('; ') }
90
+ : {}),
81
91
 
82
92
  // The validator invokes this to get headers out of the request when the
83
93
  // data type is JSON.
84
94
  header: (name) => {
85
- if (name.toLowerCase() === 'content-type' && dataType === 'json') {
86
- return 'application/json';
95
+ // If there is no name, return the data back directly; this call pattern
96
+ // happens when the data type is header.
97
+ if (name === undefined) {
98
+ return data;
87
99
  }
88
- return undefined;
100
+
101
+ return name.toLowerCase() !== 'content-type' ? undefined : {
102
+ json: 'application/json',
103
+ form: 'multipart/form-data',
104
+ }[dataType];
89
105
  },
90
106
 
91
107
  // When validation succeeds, it invokes this to store the data back into
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@odatnurd/cf-requests",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "Simple Cloudflare Hono request wrapper",
5
5
  "author": "OdatNurd (https://odatnurd.net)",
6
6
  "homepage": "https://github.com/OdatNurd/cf-requests",