@momsfriendlydevco/cowboy 1.1.0 → 1.1.1

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 (3) hide show
  1. package/README.md +20 -0
  2. package/lib/cowboy.js +27 -0
  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
@@ -255,6 +269,19 @@ export class Cowboy {
255
269
  options(path, ...middleware) { return this.route('OPTIONS', path, ...middleware) }
256
270
 
257
271
 
272
+ /**
273
+ * Handle cron job scheduling
274
+ *
275
+ * @param {Function} cb The callback to install for all scheduled events. Called as `(event:CloudflareEvent, env:Object, ctx:CloudflareContext)`
276
+ * @returns {Cowboy} This chainable Cowboy router instance
277
+ */
278
+ schedule(handler) {
279
+ console.info('Installed schedule event handler. Access via http://localhost:8787/__scheduled');
280
+ this.schedule.handler = handler;
281
+ return this;
282
+ }
283
+
284
+
258
285
  /**
259
286
  * Generial Init() sequence
260
287
  * This will be run automatically on setup or the first fetch()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@momsfriendlydevco/cowboy",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "Wrapper around Cloudflare Wrangler to provide a more Express-like experience",
5
5
  "scripts": {
6
6
  "lint": "eslint ."