@momsfriendlydevco/cowboy 1.1.0 → 1.2.0
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 +20 -0
- package/lib/cowboy.js +45 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -63,6 +63,21 @@ export default cowboy()
|
|
|
63
63
|
req => widgetStore.delete(req.params.id)
|
|
64
64
|
)
|
|
65
65
|
};
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
Cron schedule handling
|
|
69
|
+
----------------------
|
|
70
|
+
Cron scheduling is a little basic at the moment but likely to improve in the future.
|
|
71
|
+
To set up a Cron handler simply install it by calling `.schedule(callback)`:
|
|
72
|
+
|
|
73
|
+
```javascript
|
|
74
|
+
import cowboy from '@momsfriendlydevco/cowboy';
|
|
75
|
+
|
|
76
|
+
export default cowboy()
|
|
77
|
+
.schedule(async (event, env, ctx) => {
|
|
78
|
+
// Handle cron code here
|
|
79
|
+
})
|
|
80
|
+
```
|
|
66
81
|
```
|
|
67
82
|
|
|
68
83
|
Debugging
|
|
@@ -141,6 +156,11 @@ Cowboy.proxy(path, request, env)
|
|
|
141
156
|
Forward from one route to another as if the second route was called first.
|
|
142
157
|
|
|
143
158
|
|
|
159
|
+
Cowboy.schedule(callback)
|
|
160
|
+
-------------------------
|
|
161
|
+
Install a scheduled Cron handler function.
|
|
162
|
+
|
|
163
|
+
|
|
144
164
|
CowboyRequest
|
|
145
165
|
-------------
|
|
146
166
|
```javascript
|
package/lib/cowboy.js
CHANGED
|
@@ -166,6 +166,20 @@ export class Cowboy {
|
|
|
166
166
|
}
|
|
167
167
|
|
|
168
168
|
|
|
169
|
+
/**
|
|
170
|
+
* Set up Cloudflare response to "scheduled" call
|
|
171
|
+
* This is really just a map to the last handler we installed to .schedule(cb) - for now
|
|
172
|
+
*
|
|
173
|
+
* @param {CloudflareEvent} event The Cloudflare event context passed
|
|
174
|
+
* @param {Object} env Environment variables
|
|
175
|
+
* @param {CloudflareContext} ctx The Cloudflare context to respond to
|
|
176
|
+
*/
|
|
177
|
+
scheduled(event, env, ctx) {
|
|
178
|
+
if (!this.schedule.handler) throw new Error('Attemped to access Cowboy.scheduled without first calling .schedule() to set something up!');
|
|
179
|
+
return this.schedule.handler.call(this, event, env, ctx);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
|
|
169
183
|
/**
|
|
170
184
|
* Call a router function as if it were invoked directly
|
|
171
185
|
* This function exists as an easier way to remap body contents without
|
|
@@ -226,16 +240,31 @@ export class Cowboy {
|
|
|
226
240
|
response = res.end(response);
|
|
227
241
|
}
|
|
228
242
|
} catch (e) {
|
|
229
|
-
let errorText =
|
|
243
|
+
let errorText =
|
|
244
|
+
!e ? 'An unknown error has occured'
|
|
245
|
+
: typeof e == 'string' ? e
|
|
246
|
+
: e instanceof Error ? e.toString().replace(/^Error: /, '')
|
|
247
|
+
: e.error && typeof e.error == 'string' ? e.error
|
|
248
|
+
: e.err && typeof e.err == 'string' ? e.err
|
|
249
|
+
: e?.data && typeof e.data == 'string' ? e.data
|
|
250
|
+
: e?.data?.errmsg && typeof e.data.errmsg == 'string' ? e.data.errmsg
|
|
251
|
+
: e?.data?.error && typeof e.data.error == 'string' ? e.data.error
|
|
252
|
+
: e?.data?.err && typeof e.data.err == 'string' ? e.data.err
|
|
253
|
+
: e?.data?.statusText && typeof e.data.statusText == 'string' ? e.data.statusText
|
|
254
|
+
: e?.status === -1 ? 'Server connection failed'
|
|
255
|
+
: typeof e == 'function' && e.toString() !== '[object Object]' ? e.toString()
|
|
256
|
+
: e?.code && e?.message && typeof e.code == 'string' && typeof e.message == 'string' ? `${e.code}: ${e.message}` // Supabase error objects
|
|
257
|
+
: 'An unknown error has occured';
|
|
230
258
|
|
|
231
259
|
debug('Error thrown', e);
|
|
260
|
+
debug('Extracted error text digest', {errorText});
|
|
232
261
|
|
|
233
262
|
// Form: '404: Not found'
|
|
234
263
|
if (/^(\d{3}):/.test(errorText)) {
|
|
235
264
|
let errorBits = /^(?<status>\d{3}):?(?<text>.*)$/.exec(errorText).groups;
|
|
236
265
|
res.status(errorBits.status).send(errorBits.text);
|
|
237
|
-
} else { // Generic error - assume 400
|
|
238
|
-
res.status(400).send(
|
|
266
|
+
} else { // Generic error code - assume 400
|
|
267
|
+
res.status(400).send(errorText);
|
|
239
268
|
}
|
|
240
269
|
|
|
241
270
|
response = res;
|
|
@@ -255,6 +284,19 @@ export class Cowboy {
|
|
|
255
284
|
options(path, ...middleware) { return this.route('OPTIONS', path, ...middleware) }
|
|
256
285
|
|
|
257
286
|
|
|
287
|
+
/**
|
|
288
|
+
* Handle cron job scheduling
|
|
289
|
+
*
|
|
290
|
+
* @param {Function} cb The callback to install for all scheduled events. Called as `(event:CloudflareEvent, env:Object, ctx:CloudflareContext)`
|
|
291
|
+
* @returns {Cowboy} This chainable Cowboy router instance
|
|
292
|
+
*/
|
|
293
|
+
schedule(handler) {
|
|
294
|
+
console.info('Installed schedule event handler. Access via http://localhost:8787/__scheduled');
|
|
295
|
+
this.schedule.handler = handler;
|
|
296
|
+
return this;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
|
|
258
300
|
/**
|
|
259
301
|
* Generial Init() sequence
|
|
260
302
|
* This will be run automatically on setup or the first fetch()
|