@odatnurd/cf-requests 0.1.14 → 0.1.16
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/README.md +40 -25
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -97,39 +97,45 @@ schema are present if all goes well.
|
|
|
97
97
|
|
|
98
98
|
|
|
99
99
|
```js
|
|
100
|
-
// Bring in the validation generator, the success response generator,
|
|
101
|
-
// route handler generator.
|
|
102
|
-
import {
|
|
100
|
+
// Bring in the validation generator, the success response generator,
|
|
101
|
+
// and the route handler generator.
|
|
102
|
+
import {
|
|
103
|
+
validate,
|
|
104
|
+
success,
|
|
105
|
+
routeHandler
|
|
106
|
+
} from '@odatnurd/cf-requests';
|
|
103
107
|
|
|
104
108
|
// Using the Joker rollup plugin, this will result in a objects with a
|
|
105
|
-
// `validate()` and `mask()` function within them, which verify that
|
|
106
|
-
// is correct and mask away any fields not defined by the
|
|
109
|
+
// `validate()` and `mask()` function within them, which verify that
|
|
110
|
+
// the result is correct and mask away any fields not defined by the
|
|
111
|
+
// schema, respectively.
|
|
107
112
|
import * as inputSchema from '#schemas/test_input';
|
|
108
113
|
import * as outputSchema from '#schemas/test_output';
|
|
109
114
|
|
|
110
|
-
// The hono-file-routes package defines routes in a file by exporting
|
|
111
|
-
// as routes. Here we are using the routeHandler() generator,
|
|
112
|
-
// an appropriate route array based on its arguments.
|
|
115
|
+
// The hono-file-routes package defines routes in a file by exporting
|
|
116
|
+
// `$verb` as routes. Here we are using the routeHandler() generator,
|
|
117
|
+
// which constructs an appropriate route array based on its arguments.
|
|
113
118
|
//
|
|
114
119
|
// This value could also be used in a standard Hono app.
|
|
115
120
|
export const $post = routeHandler(
|
|
116
|
-
// Generate a validator that verifies the body of the request is
|
|
117
|
-
// matches the schema; results in a 422 error otherwise.
|
|
121
|
+
// Generate a validator that verifies the body of the request is
|
|
122
|
+
// json that matches the schema; results in a 422 error otherwise.
|
|
118
123
|
validate('json', inputSchema),
|
|
119
124
|
|
|
120
|
-
// Generator a validator that verifies that the result being sent
|
|
121
|
-
// client matches the schema; results in a 500 error
|
|
125
|
+
// Generator a validator that verifies that the result being sent
|
|
126
|
+
// back to the client matches the schema; results in a 500 error
|
|
127
|
+
// otherwise.
|
|
122
128
|
validate('result', outputSchema),
|
|
123
129
|
|
|
124
|
-
// Async functions that take a single argument are route handlers;
|
|
125
|
-
// be automatically guarded with a try/catch block
|
|
130
|
+
// Async functions that take a single argument are route handlers;
|
|
131
|
+
// they will be automatically guarded with a try/catch block
|
|
126
132
|
async (ctx) => {
|
|
127
133
|
// PUll out the validated JSON body.
|
|
128
134
|
const body = ctx.req.valid('json');
|
|
129
135
|
|
|
130
|
-
// Thrown exceptions inside the handler cause a fail() call to
|
|
131
|
-
// status is 500 for generic errors, but you can throw
|
|
132
|
-
// to get a specific result as desired.
|
|
136
|
+
// Thrown exceptions inside the handler cause a fail() call to
|
|
137
|
+
// occur; the status is 500 for generic errors, but you can throw
|
|
138
|
+
// HTTPError instances to get a specific result as desired.
|
|
133
139
|
if (body.key1 != 69) {
|
|
134
140
|
throw new Error('key is not nice');
|
|
135
141
|
}
|
|
@@ -170,7 +176,8 @@ called once at the top of your `aegis.config.js` file.
|
|
|
170
176
|
---
|
|
171
177
|
|
|
172
178
|
```javascript
|
|
173
|
-
export async function schemaTest(dataType, schema, data,
|
|
179
|
+
export async function schemaTest(dataType, schema, data,
|
|
180
|
+
validator = undefined) {}
|
|
174
181
|
```
|
|
175
182
|
Takes a `dataType` and `schema` as would be provided to the `validate` function
|
|
176
183
|
and runs the validation against `data` to see what the result is. The function
|
|
@@ -198,7 +205,11 @@ the database first in order to set up testing.
|
|
|
198
205
|
**Example `aegis.config.js`:**
|
|
199
206
|
|
|
200
207
|
```js
|
|
201
|
-
import {
|
|
208
|
+
import {
|
|
209
|
+
initializeCustomChecks,
|
|
210
|
+
aegisSetup,
|
|
211
|
+
aegisTeardown
|
|
212
|
+
} from '@odatnurd/cf-aegis';
|
|
202
213
|
import { initializeRequestChecks } from '@odatnurd/cf-requests/aegis';
|
|
203
214
|
|
|
204
215
|
initializeCustomChecks();
|
|
@@ -405,7 +416,9 @@ export const $post = [
|
|
|
405
416
|
---
|
|
406
417
|
|
|
407
418
|
```js
|
|
408
|
-
export class HttpError extends Error {
|
|
419
|
+
export class HttpError extends Error {
|
|
420
|
+
constructor(message, status=500) {}
|
|
421
|
+
}
|
|
409
422
|
```
|
|
410
423
|
|
|
411
424
|
This is a simple exception class that wraps a textual message and a status code.
|
|
@@ -419,7 +432,9 @@ error with the same layout as any other exception class.
|
|
|
419
432
|
---
|
|
420
433
|
|
|
421
434
|
```js
|
|
422
|
-
export class SchemaError extends HttpError {
|
|
435
|
+
export class SchemaError extends HttpError {
|
|
436
|
+
constructor(message, status=500, result=undefined) {}
|
|
437
|
+
}
|
|
423
438
|
```
|
|
424
439
|
|
|
425
440
|
This is a simple extension to HttpError and is thrown in cases where a schema
|
|
@@ -444,15 +459,15 @@ cleaner looking, while still allowing for arbitrary middleware
|
|
|
444
459
|
export const $post = routeHandler(
|
|
445
460
|
validate('json', testSchema),
|
|
446
461
|
|
|
447
|
-
// More than one argument, so function is directly returned; no
|
|
448
|
-
// wrapper here.
|
|
462
|
+
// More than one argument, so function is directly returned; no
|
|
463
|
+
// body() call wrapper here.
|
|
449
464
|
async (ctx, next) => {
|
|
450
465
|
console.log('Async middleware is running!');
|
|
451
466
|
await next();
|
|
452
467
|
},
|
|
453
468
|
|
|
454
|
-
// Single argument async functions are wrapped in body(), so
|
|
455
|
-
// are handled consistently.
|
|
469
|
+
// Single argument async functions are wrapped in body(), so
|
|
470
|
+
// exceptions raised are handled consistently.
|
|
456
471
|
async (ctx) => {
|
|
457
472
|
const body = ctx.req.valid('json');
|
|
458
473
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@odatnurd/cf-requests",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.16",
|
|
4
4
|
"description": "Simple Cloudflare Hono request wrapper",
|
|
5
5
|
"author": "OdatNurd (https://odatnurd.net)",
|
|
6
6
|
"homepage": "https://github.com/OdatNurd/cf-requests",
|
|
@@ -28,11 +28,11 @@
|
|
|
28
28
|
],
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@axel669/aegis": "^0.3.1",
|
|
31
|
-
"@axel669/joker": "^0.3.
|
|
32
|
-
"@odatnurd/cf-aegis": "^0.1.
|
|
31
|
+
"@axel669/joker": "^0.3.6",
|
|
32
|
+
"@odatnurd/cf-aegis": "^0.1.9",
|
|
33
33
|
"json5": "^2.2.3",
|
|
34
|
-
"miniflare": "^4.
|
|
35
|
-
"smol-toml": "^1.
|
|
34
|
+
"miniflare": "^4.20260424.0",
|
|
35
|
+
"smol-toml": "^1.6.1"
|
|
36
36
|
},
|
|
37
37
|
"peerDependencies": {
|
|
38
38
|
"@axel669/aegis": "^0.3.1",
|