@momsfriendlydevco/cowboy 1.0.18 → 1.0.19

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/lib/response.js +24 -1
  2. package/package.json +2 -2
package/lib/response.js CHANGED
@@ -25,6 +25,24 @@ export default class CowboyResponse {
25
25
  }
26
26
 
27
27
 
28
+ /**
29
+ * ExpressJS-like type setter and shortcut function
30
+ * Recognises various shorthand types or defaults to setting a MIME type
31
+ * @param {String} type The type string to set, can be a shorthand string or a mime type
32
+ * @returns {CowboyResponse} This chainable instance
33
+ */
34
+ type(type) {
35
+ switch (type) {
36
+ case 'html': return this.set('Content-Type', 'text/html');
37
+ case 'json': return this.set('Content-Type', 'application/json');
38
+ case 'text': return this.set('Content-Type', 'text/plain');
39
+ default:
40
+ if (!/\//.test(type)) throw new Error(`Shorthand type "${type}" is not recognised and does not look like a valid mime type`);
41
+ return this.set('Content-Type', type);
42
+ }
43
+ }
44
+
45
+
28
46
  /**
29
47
  * Send data and (optionally) mark the response as complete
30
48
  * @param {*} data The data to transmit
@@ -34,7 +52,12 @@ export default class CowboyResponse {
34
52
  send(data, end = true) {
35
53
  if (this.code === null) this.code = 200; // Assume OK if not told otherwise
36
54
 
37
- if (typeof data == 'string') {
55
+ if (
56
+ typeof data == 'string'
57
+ || data instanceof FormData
58
+ || data instanceof ReadableStream
59
+ || data instanceof URLSearchParams
60
+ ) {
38
61
  this.body = data;
39
62
  } else {
40
63
  this.body = JSON.stringify(data);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@momsfriendlydevco/cowboy",
3
- "version": "1.0.18",
3
+ "version": "1.0.19",
4
4
  "description": "Wrapper around Cloudflare Wrangler to provide a more Express-like experience",
5
5
  "scripts": {
6
6
  "lint": "eslint ."
@@ -36,7 +36,7 @@
36
36
  },
37
37
  "dependencies": {
38
38
  "@momsfriendlydevco/joyful": "^1.0.1",
39
- "@momsfriendlydevco/path-match": "^1.0.0",
39
+ "@momsfriendlydevco/path-match": "^1.1.0",
40
40
  "toml": "^3.0.0"
41
41
  },
42
42
  "devDependencies": {