bun-crumb 0.12.1 → 0.12.2

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/dist/index.d.ts CHANGED
@@ -121,6 +121,45 @@ interface RouteResponse<T extends {
121
121
  } = {
122
122
  body: unknown;
123
123
  }> {
124
+ /**
125
+ * Sets Response body to provided `data`.
126
+ * Automatically sets `Content-Type` to `application/json` if an object was provided and transforms `data` to JSON.
127
+ *
128
+ * ### **IMPORTANT NOTES**:
129
+ * - #### If `Content-Type` header is already set, this function will not change this header.
130
+ *
131
+ *
132
+ *
133
+ *
134
+ * - #### If you want to complete request and send response, you must necessarily `return` the call of this function. Otherwise, code of your handler will continue executing and `Response` body could be changed.
135
+ *
136
+ * @param data value that will be sent to client
137
+ * @param options standard `Response` options from web API
138
+ *
139
+ * @example
140
+ * ```typescript
141
+ * response.setHeader('content-type', 'x-www-form-urlencoded'); // Set `Content-Type` header
142
+ *
143
+ * return response.send('Hello'); // This does not change `Content-Type` header, because this header is set above
144
+ * ```
145
+ *
146
+ * @example
147
+ * ```typescript
148
+ * return response.send('Hello'); // This sets `Content-Type` header to `text/plain`, because this header is not set above.
149
+ * ```
150
+ *
151
+ * @example
152
+ * ```typescript
153
+ * return response.send({ key: 'value' }); // This will set `Content-Type` header to `application/json`, because this header is not set above. Object is transformed to JSON automatically
154
+ * ```
155
+ *
156
+ * @example
157
+ * ```typescript
158
+ * response.send({key: 'value'}); // ❌ Without return, the code below changes the body of response
159
+ * response.send('Hello'); // ❌ This changes the body of response, but `Content-Type` header will not change
160
+ * ```
161
+ *
162
+ */
124
163
  send: (data?: T['body'], options?: ResponseOptions) => void;
125
164
  /**
126
165
  * Sets `Location` header to provided `url` and `response.status` to provided `status`
@@ -129,7 +168,7 @@ interface RouteResponse<T extends {
129
168
  *
130
169
  * @param url `Location` to redirect
131
170
  *
132
- * @param status redirect http code (`3xx`)
171
+ * @param status redirect http code (`3xx`). Equals to 302 by default
133
172
  *
134
173
  * @example
135
174
  *
@@ -140,7 +179,7 @@ interface RouteResponse<T extends {
140
179
  * The same behaviour is
141
180
  * ```typescript
142
181
  * response.setHeader('Location', 'https://bun-crumb.vercel.app');
143
- * return response.send('', {status: 302});
182
+ * return response.send('', { status: 302 });
144
183
  * ```
145
184
  */
146
185
  redirect: (url: string, status?: RedirectStatusCode | (number & {})) => void;
@@ -237,7 +276,8 @@ interface ListenOptions {
237
276
  declare const listen: (options?: ListenOptions) => void;
238
277
 
239
278
  /**
240
- * Creates a route with `url`, `method` and `handler`.
279
+ * #### Creates a route with `url`, `method` and `handler`.
280
+ *
241
281
  * Should be called before `listen` function is called.
242
282
  *
243
283
  *
@@ -261,8 +301,8 @@ declare const listen: (options?: ListenOptions) => void;
261
301
  *
262
302
  * @example
263
303
  * ```typescript
264
- * const deleteProduct = (
265
- * request: RouteRequest,
304
+ * const deleteProduct: RouteHandler = (
305
+ * request,
266
306
  * response: RouteResponse<{ body: { error: string } | { product: Product } }>
267
307
  * ) => {
268
308
  * const id = request.params.id;
package/dist/route.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import type { RouteOptions } from './types';
2
2
  /**
3
- * Creates a route with `url`, `method` and `handler`.
3
+ * #### Creates a route with `url`, `method` and `handler`.
4
+ *
4
5
  * Should be called before `listen` function is called.
5
6
  *
6
7
  *
@@ -24,8 +25,8 @@ import type { RouteOptions } from './types';
24
25
  *
25
26
  * @example
26
27
  * ```typescript
27
- * const deleteProduct = (
28
- * request: RouteRequest,
28
+ * const deleteProduct: RouteHandler = (
29
+ * request,
29
30
  * response: RouteResponse<{ body: { error: string } | { product: Product } }>
30
31
  * ) => {
31
32
  * const id = request.params.id;
@@ -0,0 +1 @@
1
+ export {};
@@ -60,6 +60,45 @@ export interface RouteResponse<T extends {
60
60
  } = {
61
61
  body: unknown;
62
62
  }> {
63
+ /**
64
+ * Sets Response body to provided `data`.
65
+ * Automatically sets `Content-Type` to `application/json` if an object was provided and transforms `data` to JSON.
66
+ *
67
+ * ### **IMPORTANT NOTES**:
68
+ * - #### If `Content-Type` header is already set, this function will not change this header.
69
+ *
70
+ *
71
+ *
72
+ *
73
+ * - #### If you want to complete request and send response, you must necessarily `return` the call of this function. Otherwise, code of your handler will continue executing and `Response` body could be changed.
74
+ *
75
+ * @param data value that will be sent to client
76
+ * @param options standard `Response` options from web API
77
+ *
78
+ * @example
79
+ * ```typescript
80
+ * response.setHeader('content-type', 'x-www-form-urlencoded'); // Set `Content-Type` header
81
+ *
82
+ * return response.send('Hello'); // This does not change `Content-Type` header, because this header is set above
83
+ * ```
84
+ *
85
+ * @example
86
+ * ```typescript
87
+ * return response.send('Hello'); // This sets `Content-Type` header to `text/plain`, because this header is not set above.
88
+ * ```
89
+ *
90
+ * @example
91
+ * ```typescript
92
+ * return response.send({ key: 'value' }); // This will set `Content-Type` header to `application/json`, because this header is not set above. Object is transformed to JSON automatically
93
+ * ```
94
+ *
95
+ * @example
96
+ * ```typescript
97
+ * response.send({key: 'value'}); // ❌ Without return, the code below changes the body of response
98
+ * response.send('Hello'); // ❌ This changes the body of response, but `Content-Type` header will not change
99
+ * ```
100
+ *
101
+ */
63
102
  send: (data?: T['body'], options?: ResponseOptions) => void;
64
103
  /**
65
104
  * Sets `Location` header to provided `url` and `response.status` to provided `status`
@@ -68,7 +107,7 @@ export interface RouteResponse<T extends {
68
107
  *
69
108
  * @param url `Location` to redirect
70
109
  *
71
- * @param status redirect http code (`3xx`)
110
+ * @param status redirect http code (`3xx`). Equals to 302 by default
72
111
  *
73
112
  * @example
74
113
  *
@@ -79,7 +118,7 @@ export interface RouteResponse<T extends {
79
118
  * The same behaviour is
80
119
  * ```typescript
81
120
  * response.setHeader('Location', 'https://bun-crumb.vercel.app');
82
- * return response.send('', {status: 302});
121
+ * return response.send('', { status: 302 });
83
122
  * ```
84
123
  */
85
124
  redirect: (url: string, status?: RedirectStatusCode | (number & {})) => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bun-crumb",
3
- "version": "0.12.1",
3
+ "version": "0.12.2",
4
4
  "author": "marigold",
5
5
  "module": "dist/index.js",
6
6
  "license": "UNLICENSED",
@@ -17,6 +17,7 @@
17
17
  "rollup-plugin-dts": "^6.3.0",
18
18
  "tslib": "^2.8.1"
19
19
  },
20
+ "homepage": "https://bun-crumb.vercel.app",
20
21
  "files": [
21
22
  "dist"
22
23
  ],