bun-crumb 0.1.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/LICENSE +24 -0
- package/README.md +93 -0
- package/dist/__tests__/server.test.d.ts +1 -0
- package/dist/errors/HttpError.d.ts +4 -0
- package/dist/index.d.ts +284 -0
- package/dist/index.js +1 -0
- package/dist/route.d.ts +55 -0
- package/dist/server.d.ts +107 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/route.d.ts +59 -0
- package/dist/types/schema.d.ts +39 -0
- package/dist/types/server.d.ts +24 -0
- package/package.json +43 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
This is free and unencumbered software released into the public domain.
|
|
2
|
+
|
|
3
|
+
Anyone is free to copy, modify, publish, use, compile, sell, or
|
|
4
|
+
distribute this software, either in source code form or as a compiled
|
|
5
|
+
binary, for any purpose, commercial or non-commercial, and by any
|
|
6
|
+
means.
|
|
7
|
+
|
|
8
|
+
In jurisdictions that recognize copyright laws, the author or authors
|
|
9
|
+
of this software dedicate any and all copyright interest in the
|
|
10
|
+
software to the public domain. We make this dedication for the benefit
|
|
11
|
+
of the public at large and to the detriment of our heirs and
|
|
12
|
+
successors. We intend this dedication to be an overt act of
|
|
13
|
+
relinquishment in perpetuity of all present and future rights to this
|
|
14
|
+
software under copyright law.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
19
|
+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
20
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
21
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+
|
|
24
|
+
For more information, please refer to <https://unlicense.org>
|
package/README.md
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# <div align='center'> <a> **Crumb** </a> </div>
|
|
2
|
+
|
|
3
|
+
<div align='center'>
|
|
4
|
+
|
|
5
|
+
[](https://github.com/a-marigold/crumb/actions)  [](https://npmjs.com/package/bun-crumb)
|
|
6
|
+
|
|
7
|
+
</div>
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Features
|
|
12
|
+
|
|
13
|
+
- Only Bun is supported
|
|
14
|
+
|
|
15
|
+
- No classes
|
|
16
|
+
|
|
17
|
+
- Uses Bun’s HTTP API
|
|
18
|
+
|
|
19
|
+
### Installation
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
bun add crumb
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### Usage
|
|
26
|
+
|
|
27
|
+
Handling Requests
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
import { createRoute, type RouteResponse } from 'crumb-bun';
|
|
31
|
+
|
|
32
|
+
type Product = { title: string; price: number; id: number };
|
|
33
|
+
|
|
34
|
+
const products: Product[] = [];
|
|
35
|
+
|
|
36
|
+
createRoute({
|
|
37
|
+
url: '/products',
|
|
38
|
+
method: 'GET',
|
|
39
|
+
handler: (request, response: RouteResponse<{ body: Product[] }>) => {
|
|
40
|
+
return response.send(
|
|
41
|
+
[
|
|
42
|
+
{ title: 'cookie', price: 100, id: 1 },
|
|
43
|
+
{ title: 'bread', price: 1600, id: 2 },
|
|
44
|
+
],
|
|
45
|
+
{ status: 200 }
|
|
46
|
+
);
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
Middleware / Pre-handlers
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
import { createRoute, type RouteResponse } from 'crumb-bun';
|
|
57
|
+
|
|
58
|
+
type Product = { title: string; price: number; id: number };
|
|
59
|
+
|
|
60
|
+
const products: Product[] = [];
|
|
61
|
+
|
|
62
|
+
createRoute({
|
|
63
|
+
url: '/products',
|
|
64
|
+
method: 'POST',
|
|
65
|
+
preHandler: (request, response) => {
|
|
66
|
+
if (products.find((product) => product.id === request.body.id)) {
|
|
67
|
+
return response.send(
|
|
68
|
+
{ message: 'Product with this id already exists' },
|
|
69
|
+
{ status: 409 }
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
handler: (request, response: RouteResponse<{ body: Product }>) => {
|
|
74
|
+
products.push(body);
|
|
75
|
+
|
|
76
|
+
return response.send(body, { status: 201 });
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
Setting Headers and Status
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
import { createRoute } from 'crumb-bun';
|
|
87
|
+
|
|
88
|
+
createRoute({
|
|
89
|
+
url: '/auth',
|
|
90
|
+
method: 'POST',
|
|
91
|
+
handler: (request, response) => {},
|
|
92
|
+
});
|
|
93
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import { BunRequest } from 'bun';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The global Schema type that reflects schema type in `Validate` function
|
|
5
|
+
*
|
|
6
|
+
* @default {}
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
*
|
|
10
|
+
* ```typescript
|
|
11
|
+
* // types/global.d.ts
|
|
12
|
+
* import type { ZodType } from 'zod';
|
|
13
|
+
*
|
|
14
|
+
*
|
|
15
|
+
*
|
|
16
|
+
* declare module 'crumb-bun' {
|
|
17
|
+
* interface Schema {
|
|
18
|
+
* zod: ZodType;
|
|
19
|
+
* }
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* ```json
|
|
24
|
+
* // tsconfig.json
|
|
25
|
+
* {
|
|
26
|
+
* "include": ["types"]
|
|
27
|
+
* }
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
interface Schema {
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Straight `Schema` type
|
|
34
|
+
*/
|
|
35
|
+
type SchemaData = Schema[keyof Schema];
|
|
36
|
+
/**
|
|
37
|
+
* The schema validator function type.
|
|
38
|
+
*
|
|
39
|
+
* Supports any schemas like `zod`, `ajv`, `yup`
|
|
40
|
+
*/
|
|
41
|
+
type Validate = (data: unknown, schema: SchemaData) => boolean;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* HTTP Method primitive.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```typescript
|
|
48
|
+
* const method: HttpMethod = 'GET';
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS';
|
|
52
|
+
/**
|
|
53
|
+
* HTTP Header struct.
|
|
54
|
+
*/
|
|
55
|
+
type Header = {
|
|
56
|
+
name: string;
|
|
57
|
+
value: string;
|
|
58
|
+
};
|
|
59
|
+
type Headers = ResponseInit['headers'];
|
|
60
|
+
/**
|
|
61
|
+
* Type of route handler `request`
|
|
62
|
+
*/
|
|
63
|
+
interface RouteRequest<T extends {
|
|
64
|
+
parsedBody: unknown;
|
|
65
|
+
} = {
|
|
66
|
+
parsedBody: unknown;
|
|
67
|
+
}> extends Omit<BunRequest, 'body'> {
|
|
68
|
+
/**
|
|
69
|
+
* Parsed, validated from schema body of reqeust
|
|
70
|
+
*/
|
|
71
|
+
parsedBody: T extends {
|
|
72
|
+
parsedBody: unknown;
|
|
73
|
+
} ? T['parsedBody'] : unknown;
|
|
74
|
+
}
|
|
75
|
+
interface ResponseOptions {
|
|
76
|
+
status: number;
|
|
77
|
+
statusText?: string;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Type of route handler `response`
|
|
81
|
+
*/
|
|
82
|
+
interface RouteResponse<T extends {
|
|
83
|
+
body: unknown;
|
|
84
|
+
} = {
|
|
85
|
+
body: unknown;
|
|
86
|
+
}> {
|
|
87
|
+
setHeader: (name: Header['name'], value: Header['value']) => void;
|
|
88
|
+
send: (data: T['body'], options?: ResponseOptions) => void;
|
|
89
|
+
}
|
|
90
|
+
type Route = Partial<Record<HttpMethod, RouteOptions>>;
|
|
91
|
+
type RouteHandler = (request: RouteRequest, response: RouteResponse) => void;
|
|
92
|
+
type RouteOptions = {
|
|
93
|
+
url: string;
|
|
94
|
+
method: HttpMethod;
|
|
95
|
+
schema?: SchemaData;
|
|
96
|
+
onRequest?: RouteHandler;
|
|
97
|
+
preHandler?: RouteHandler;
|
|
98
|
+
handler: RouteHandler;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
interface ListenOptions {
|
|
102
|
+
/**
|
|
103
|
+
* Server port to listen
|
|
104
|
+
*/
|
|
105
|
+
port?: number | string;
|
|
106
|
+
/**
|
|
107
|
+
* Server hostname to listen
|
|
108
|
+
*
|
|
109
|
+
* @example `localhost`, `0.0.0.0`
|
|
110
|
+
*
|
|
111
|
+
*/
|
|
112
|
+
hostname?: string;
|
|
113
|
+
/**
|
|
114
|
+
* Development flag.
|
|
115
|
+
*
|
|
116
|
+
* @default NODE_ENV enviroment variable value
|
|
117
|
+
*/
|
|
118
|
+
development?: boolean;
|
|
119
|
+
/**
|
|
120
|
+
* Global schema validator function that will be used in every route
|
|
121
|
+
*/
|
|
122
|
+
schemaValidator?: Validate;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
type PreparedRoute = Partial<Record<HttpMethod, WrappedRouteCallback>>;
|
|
126
|
+
type WrappedRouteCallback = (request: BunRequest) => Promise<Response>;
|
|
127
|
+
/**
|
|
128
|
+
* Used straight as Bun.serve `routes` object.
|
|
129
|
+
*/
|
|
130
|
+
type PreparedRoutes = Record<RouteOptions['url'], PreparedRoute>;
|
|
131
|
+
type Routes = Map<RouteOptions['url'], Route>;
|
|
132
|
+
/**
|
|
133
|
+
* An internal Map with routes of app. Do not use it in user code to prevent undefined errors
|
|
134
|
+
*/
|
|
135
|
+
declare const _routes: Routes;
|
|
136
|
+
/**
|
|
137
|
+
* Runtime function that used in request.
|
|
138
|
+
* Parses body to supported content type (json, plain text) and validates it with route schema.
|
|
139
|
+
*
|
|
140
|
+
* @param {BunRequest} request incoming bun request.
|
|
141
|
+
* @param {string} contentType request `Content-Type` header value.
|
|
142
|
+
* @param {Schema} schema json or any schema with declared `Schema` type.
|
|
143
|
+
* @param {Validate} schemaValidator schema validator function that receives `data` and `schema` arguments.
|
|
144
|
+
*
|
|
145
|
+
* @returns {Promise<unknown>} Promise with body
|
|
146
|
+
*/
|
|
147
|
+
declare const handleBody: (request: BunRequest, contentType: string, schema?: SchemaData, schemaValidator?: Validate) => Promise<unknown>;
|
|
148
|
+
/**
|
|
149
|
+
* Internal `server` function.
|
|
150
|
+
* Creates a function with handler and all route hooks.
|
|
151
|
+
*
|
|
152
|
+
* The created function can be used as a callback for route in Bun.serve `routes` object.
|
|
153
|
+
*
|
|
154
|
+
*
|
|
155
|
+
*
|
|
156
|
+
*
|
|
157
|
+
*
|
|
158
|
+
* @param routeOptions options of route
|
|
159
|
+
* @returns {WrappedRouteCallback} Function that is ready to be used in Bun.serve `routes`
|
|
160
|
+
*/
|
|
161
|
+
declare const wrapRouteCallback: (routeOptions: RouteOptions, schemaValidator?: Validate) => WrappedRouteCallback;
|
|
162
|
+
/**
|
|
163
|
+
* Internal `server` function.
|
|
164
|
+
* Prepares a route to be used in Bun.serve `routes` object.
|
|
165
|
+
*
|
|
166
|
+
* @param {Route} route
|
|
167
|
+
*
|
|
168
|
+
* @returns {PreparedRoute} Route object with `GET` or other http method keys with wrapped route callbacks.
|
|
169
|
+
*
|
|
170
|
+
* @example
|
|
171
|
+
* ```typescript
|
|
172
|
+
* prepareRoute({
|
|
173
|
+
* GET: {
|
|
174
|
+
* url: '/products',
|
|
175
|
+
* method: 'GET',
|
|
176
|
+
* handler: (request, response) => {},
|
|
177
|
+
* },
|
|
178
|
+
* POST: {
|
|
179
|
+
* url: '/products/:id',
|
|
180
|
+
* method: 'POST',
|
|
181
|
+
* handler: (request, response) => {},
|
|
182
|
+
* },
|
|
183
|
+
* });
|
|
184
|
+
* // Output will be:
|
|
185
|
+
* ({
|
|
186
|
+
* GET: (request: BunRequest) => {
|
|
187
|
+
* // ...code
|
|
188
|
+
* return new Response();
|
|
189
|
+
* },
|
|
190
|
+
* POST: (request: BunRequest) => {
|
|
191
|
+
* // ...code
|
|
192
|
+
* return new Response();
|
|
193
|
+
* },
|
|
194
|
+
* })
|
|
195
|
+
* ```
|
|
196
|
+
*
|
|
197
|
+
*/
|
|
198
|
+
declare const prepareRoute: (route: Route, schemaValidator?: Validate) => PreparedRoute;
|
|
199
|
+
/**
|
|
200
|
+
* Internal server function.
|
|
201
|
+
* Calls `prepareRoute` for every route of `routes` Map and returns prepared routes to use in Bun.serve `routes`.
|
|
202
|
+
*
|
|
203
|
+
* @param {Routes} routes Map with routes to prepare.
|
|
204
|
+
*
|
|
205
|
+
* @returns {PreparedRoutes} An object that is used straight in Bun.serve `routes` object.
|
|
206
|
+
*/
|
|
207
|
+
declare const prepareRoutes: (routes: Routes, schemaValidator?: Validate) => PreparedRoutes;
|
|
208
|
+
/**
|
|
209
|
+
* Starts serving http server.
|
|
210
|
+
*
|
|
211
|
+
*
|
|
212
|
+
* @param {ListenOption} options - options
|
|
213
|
+
*
|
|
214
|
+
*
|
|
215
|
+
*
|
|
216
|
+
* @example
|
|
217
|
+
*
|
|
218
|
+
* ```typescript
|
|
219
|
+
* import { listen } from 'crumb-bun';
|
|
220
|
+
*
|
|
221
|
+
* const PORT = proccess.env['PORT'] || 1000;
|
|
222
|
+
*
|
|
223
|
+
* listen(PORT, 'localhost');
|
|
224
|
+
* ```
|
|
225
|
+
*/
|
|
226
|
+
declare const listen: (options: ListenOptions) => void;
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Creates a route with `url`, `method` and `handler`.
|
|
230
|
+
* Should be called before `listen` function is called.
|
|
231
|
+
*
|
|
232
|
+
*
|
|
233
|
+
* @param {RouteOptions} routeOptions - route
|
|
234
|
+
*
|
|
235
|
+
*
|
|
236
|
+
*
|
|
237
|
+
* @example
|
|
238
|
+
* ```typescript
|
|
239
|
+
* createRoute({
|
|
240
|
+
* url: '*',
|
|
241
|
+
* method: 'GET',
|
|
242
|
+
* handler: (request, response) => {
|
|
243
|
+
* response.send(
|
|
244
|
+
* { message: 'The resource you are looking for is not found.' },
|
|
245
|
+
* { status: 404, statusText: 'Not Found' }
|
|
246
|
+
* );
|
|
247
|
+
* },
|
|
248
|
+
* });
|
|
249
|
+
* ```
|
|
250
|
+
*
|
|
251
|
+
* @example
|
|
252
|
+
* ```typescript
|
|
253
|
+
* const deleteProduct = (
|
|
254
|
+
* request: RouteRequest,
|
|
255
|
+
* response: RouteResponse<{ body: { error: string } | { product: Product } }>
|
|
256
|
+
* ) => {
|
|
257
|
+
* const id = request.params.id;
|
|
258
|
+
*
|
|
259
|
+
* if (!(id in products)) {
|
|
260
|
+
* return response.send(
|
|
261
|
+
* { error: 'Product with this id is not found' },
|
|
262
|
+
* { status: 404 }
|
|
263
|
+
* );
|
|
264
|
+
* }
|
|
265
|
+
*
|
|
266
|
+
* const product = products[id];
|
|
267
|
+
*
|
|
268
|
+
* products[id] = null;
|
|
269
|
+
*
|
|
270
|
+
* return response.send(product);
|
|
271
|
+
* };
|
|
272
|
+
*
|
|
273
|
+
* createRoute({
|
|
274
|
+
* url: '/products/:id',
|
|
275
|
+
* method: 'DELETE',
|
|
276
|
+
*
|
|
277
|
+
* handler: deleteProduct,
|
|
278
|
+
* });
|
|
279
|
+
* ```
|
|
280
|
+
*/
|
|
281
|
+
declare const createRoute: (routeOptions: RouteOptions) => void;
|
|
282
|
+
|
|
283
|
+
export { _routes, createRoute, handleBody, listen, prepareRoute, prepareRoutes, wrapRouteCallback };
|
|
284
|
+
export type { Header, Headers, HttpMethod, ListenOptions, ResponseOptions, Route, RouteHandler, RouteOptions, RouteRequest, RouteResponse, Routes, Schema, SchemaData, Validate, WrappedRouteCallback };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{serve as t}from"bun";class e extends Error{status;constructor(t,e){super(e),this.status=t,this.name="HttpError"}}const n=new Map,s=(t,n,s,r)=>{const o={"application/json":t=>t.json().catch(t=>{throw new e(400,t)}).then(t=>{if(s&&r&&!r(t,s))throw new e(400,"Request does not match schema");return t}),"text/plain":t=>t.text().catch(t=>{throw new e(400,t)}).then(t=>{if(s&&r&&!r(t,s))throw new e(400,"Request does not match schema");return t})};return n in o?o[n](t):Promise.reject(new e(415,"Unsupported media type"))},r=(t,n)=>r=>{const o=r.headers.get("Content-Type")??"text/plain";return s(r,o,t.schema,n).then(e=>{const n=r;return n.parsedBody=e,((t,e)=>{let n,s,r=null;const o={},a={setHeader:(t,e)=>{o[t]=e},send:(t,e)=>{"object"==typeof t?o["Content-Type"]="application/json":"string"==typeof t&&(o["Content-Type"]="text/plain"),r=t,n=e?.status,s=e?.statusText}};return e.onRequest?.(t,a),e.preHandler?.(t,a),e.handler(t,a),new Response(null==r?null:JSON.stringify(r),{headers:o,status:n,statusText:s})})(n,t)}).catch(t=>t instanceof e?new Response(t.message,{status:t.status}):new Response("Internal server error",{status:500}))},o=(t,e)=>{const n={};for(const s of Object.entries(t))n[s[0]]=r(s[1],e);return n},a=(t,e)=>{const n={};for(const s of t)n[s[0]]=o(s[1],e);return t.clear(),n},c=e=>{t({port:e.port,hostname:e.hostname,development:e.development??!1,routes:a(n,e?.schemaValidator)})},u=t=>{const e=n.get(t.url);e?e[t.method]=t:n.set(t.url,{[t.method]:t})};export{n as _routes,u as createRoute,s as handleBody,c as listen,o as prepareRoute,a as prepareRoutes,r as wrapRouteCallback};
|
package/dist/route.d.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { RouteOptions } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a route with `url`, `method` and `handler`.
|
|
4
|
+
* Should be called before `listen` function is called.
|
|
5
|
+
*
|
|
6
|
+
*
|
|
7
|
+
* @param {RouteOptions} routeOptions - route
|
|
8
|
+
*
|
|
9
|
+
*
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* createRoute({
|
|
14
|
+
* url: '*',
|
|
15
|
+
* method: 'GET',
|
|
16
|
+
* handler: (request, response) => {
|
|
17
|
+
* response.send(
|
|
18
|
+
* { message: 'The resource you are looking for is not found.' },
|
|
19
|
+
* { status: 404, statusText: 'Not Found' }
|
|
20
|
+
* );
|
|
21
|
+
* },
|
|
22
|
+
* });
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* const deleteProduct = (
|
|
28
|
+
* request: RouteRequest,
|
|
29
|
+
* response: RouteResponse<{ body: { error: string } | { product: Product } }>
|
|
30
|
+
* ) => {
|
|
31
|
+
* const id = request.params.id;
|
|
32
|
+
*
|
|
33
|
+
* if (!(id in products)) {
|
|
34
|
+
* return response.send(
|
|
35
|
+
* { error: 'Product with this id is not found' },
|
|
36
|
+
* { status: 404 }
|
|
37
|
+
* );
|
|
38
|
+
* }
|
|
39
|
+
*
|
|
40
|
+
* const product = products[id];
|
|
41
|
+
*
|
|
42
|
+
* products[id] = null;
|
|
43
|
+
*
|
|
44
|
+
* return response.send(product);
|
|
45
|
+
* };
|
|
46
|
+
*
|
|
47
|
+
* createRoute({
|
|
48
|
+
* url: '/products/:id',
|
|
49
|
+
* method: 'DELETE',
|
|
50
|
+
*
|
|
51
|
+
* handler: deleteProduct,
|
|
52
|
+
* });
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export declare const createRoute: (routeOptions: RouteOptions) => void;
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { BunRequest } from 'bun';
|
|
2
|
+
import type { Route, RouteOptions, HttpMethod } from './types/route';
|
|
3
|
+
import type { SchemaData, Validate } from './types';
|
|
4
|
+
import type { ListenOptions } from './types';
|
|
5
|
+
type PreparedRoute = Partial<Record<HttpMethod, WrappedRouteCallback>>;
|
|
6
|
+
export type WrappedRouteCallback = (request: BunRequest) => Promise<Response>;
|
|
7
|
+
/**
|
|
8
|
+
* Used straight as Bun.serve `routes` object.
|
|
9
|
+
*/
|
|
10
|
+
type PreparedRoutes = Record<RouteOptions['url'], PreparedRoute>;
|
|
11
|
+
export type Routes = Map<RouteOptions['url'], Route>;
|
|
12
|
+
/**
|
|
13
|
+
* An internal Map with routes of app. Do not use it in user code to prevent undefined errors
|
|
14
|
+
*/
|
|
15
|
+
export declare const _routes: Routes;
|
|
16
|
+
/**
|
|
17
|
+
* Runtime function that used in request.
|
|
18
|
+
* Parses body to supported content type (json, plain text) and validates it with route schema.
|
|
19
|
+
*
|
|
20
|
+
* @param {BunRequest} request incoming bun request.
|
|
21
|
+
* @param {string} contentType request `Content-Type` header value.
|
|
22
|
+
* @param {Schema} schema json or any schema with declared `Schema` type.
|
|
23
|
+
* @param {Validate} schemaValidator schema validator function that receives `data` and `schema` arguments.
|
|
24
|
+
*
|
|
25
|
+
* @returns {Promise<unknown>} Promise with body
|
|
26
|
+
*/
|
|
27
|
+
export declare const handleBody: (request: BunRequest, contentType: string, schema?: SchemaData, schemaValidator?: Validate) => Promise<unknown>;
|
|
28
|
+
/**
|
|
29
|
+
* Internal `server` function.
|
|
30
|
+
* Creates a function with handler and all route hooks.
|
|
31
|
+
*
|
|
32
|
+
* The created function can be used as a callback for route in Bun.serve `routes` object.
|
|
33
|
+
*
|
|
34
|
+
*
|
|
35
|
+
*
|
|
36
|
+
*
|
|
37
|
+
*
|
|
38
|
+
* @param routeOptions options of route
|
|
39
|
+
* @returns {WrappedRouteCallback} Function that is ready to be used in Bun.serve `routes`
|
|
40
|
+
*/
|
|
41
|
+
export declare const wrapRouteCallback: (routeOptions: RouteOptions, schemaValidator?: Validate) => WrappedRouteCallback;
|
|
42
|
+
/**
|
|
43
|
+
* Internal `server` function.
|
|
44
|
+
* Prepares a route to be used in Bun.serve `routes` object.
|
|
45
|
+
*
|
|
46
|
+
* @param {Route} route
|
|
47
|
+
*
|
|
48
|
+
* @returns {PreparedRoute} Route object with `GET` or other http method keys with wrapped route callbacks.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```typescript
|
|
52
|
+
* prepareRoute({
|
|
53
|
+
* GET: {
|
|
54
|
+
* url: '/products',
|
|
55
|
+
* method: 'GET',
|
|
56
|
+
* handler: (request, response) => {},
|
|
57
|
+
* },
|
|
58
|
+
* POST: {
|
|
59
|
+
* url: '/products/:id',
|
|
60
|
+
* method: 'POST',
|
|
61
|
+
* handler: (request, response) => {},
|
|
62
|
+
* },
|
|
63
|
+
* });
|
|
64
|
+
* // Output will be:
|
|
65
|
+
* ({
|
|
66
|
+
* GET: (request: BunRequest) => {
|
|
67
|
+
* // ...code
|
|
68
|
+
* return new Response();
|
|
69
|
+
* },
|
|
70
|
+
* POST: (request: BunRequest) => {
|
|
71
|
+
* // ...code
|
|
72
|
+
* return new Response();
|
|
73
|
+
* },
|
|
74
|
+
* })
|
|
75
|
+
* ```
|
|
76
|
+
*
|
|
77
|
+
*/
|
|
78
|
+
export declare const prepareRoute: (route: Route, schemaValidator?: Validate) => PreparedRoute;
|
|
79
|
+
/**
|
|
80
|
+
* Internal server function.
|
|
81
|
+
* Calls `prepareRoute` for every route of `routes` Map and returns prepared routes to use in Bun.serve `routes`.
|
|
82
|
+
*
|
|
83
|
+
* @param {Routes} routes Map with routes to prepare.
|
|
84
|
+
*
|
|
85
|
+
* @returns {PreparedRoutes} An object that is used straight in Bun.serve `routes` object.
|
|
86
|
+
*/
|
|
87
|
+
export declare const prepareRoutes: (routes: Routes, schemaValidator?: Validate) => PreparedRoutes;
|
|
88
|
+
/**
|
|
89
|
+
* Starts serving http server.
|
|
90
|
+
*
|
|
91
|
+
*
|
|
92
|
+
* @param {ListenOption} options - options
|
|
93
|
+
*
|
|
94
|
+
*
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
*
|
|
98
|
+
* ```typescript
|
|
99
|
+
* import { listen } from 'crumb-bun';
|
|
100
|
+
*
|
|
101
|
+
* const PORT = proccess.env['PORT'] || 1000;
|
|
102
|
+
*
|
|
103
|
+
* listen(PORT, 'localhost');
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
export declare const listen: (options: ListenOptions) => void;
|
|
107
|
+
export {};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { BunRequest } from 'bun';
|
|
2
|
+
import type { SchemaData } from './schema';
|
|
3
|
+
/**
|
|
4
|
+
* HTTP Method primitive.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* const method: HttpMethod = 'GET';
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS';
|
|
12
|
+
/**
|
|
13
|
+
* HTTP Header struct.
|
|
14
|
+
*/
|
|
15
|
+
export type Header = {
|
|
16
|
+
name: string;
|
|
17
|
+
value: string;
|
|
18
|
+
};
|
|
19
|
+
export type Headers = ResponseInit['headers'];
|
|
20
|
+
/**
|
|
21
|
+
* Type of route handler `request`
|
|
22
|
+
*/
|
|
23
|
+
export interface RouteRequest<T extends {
|
|
24
|
+
parsedBody: unknown;
|
|
25
|
+
} = {
|
|
26
|
+
parsedBody: unknown;
|
|
27
|
+
}> extends Omit<BunRequest, 'body'> {
|
|
28
|
+
/**
|
|
29
|
+
* Parsed, validated from schema body of reqeust
|
|
30
|
+
*/
|
|
31
|
+
parsedBody: T extends {
|
|
32
|
+
parsedBody: unknown;
|
|
33
|
+
} ? T['parsedBody'] : unknown;
|
|
34
|
+
}
|
|
35
|
+
export interface ResponseOptions {
|
|
36
|
+
status: number;
|
|
37
|
+
statusText?: string;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Type of route handler `response`
|
|
41
|
+
*/
|
|
42
|
+
export interface RouteResponse<T extends {
|
|
43
|
+
body: unknown;
|
|
44
|
+
} = {
|
|
45
|
+
body: unknown;
|
|
46
|
+
}> {
|
|
47
|
+
setHeader: (name: Header['name'], value: Header['value']) => void;
|
|
48
|
+
send: (data: T['body'], options?: ResponseOptions) => void;
|
|
49
|
+
}
|
|
50
|
+
export type Route = Partial<Record<HttpMethod, RouteOptions>>;
|
|
51
|
+
export type RouteHandler = (request: RouteRequest, response: RouteResponse) => void;
|
|
52
|
+
export type RouteOptions = {
|
|
53
|
+
url: string;
|
|
54
|
+
method: HttpMethod;
|
|
55
|
+
schema?: SchemaData;
|
|
56
|
+
onRequest?: RouteHandler;
|
|
57
|
+
preHandler?: RouteHandler;
|
|
58
|
+
handler: RouteHandler;
|
|
59
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The global Schema type that reflects schema type in `Validate` function
|
|
3
|
+
*
|
|
4
|
+
* @default {}
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
*
|
|
8
|
+
* ```typescript
|
|
9
|
+
* // types/global.d.ts
|
|
10
|
+
* import type { ZodType } from 'zod';
|
|
11
|
+
*
|
|
12
|
+
*
|
|
13
|
+
*
|
|
14
|
+
* declare module 'crumb-bun' {
|
|
15
|
+
* interface Schema {
|
|
16
|
+
* zod: ZodType;
|
|
17
|
+
* }
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* ```json
|
|
22
|
+
* // tsconfig.json
|
|
23
|
+
* {
|
|
24
|
+
* "include": ["types"]
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export interface Schema {
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Straight `Schema` type
|
|
32
|
+
*/
|
|
33
|
+
export type SchemaData = Schema[keyof Schema];
|
|
34
|
+
/**
|
|
35
|
+
* The schema validator function type.
|
|
36
|
+
*
|
|
37
|
+
* Supports any schemas like `zod`, `ajv`, `yup`
|
|
38
|
+
*/
|
|
39
|
+
export type Validate = (data: unknown, schema: SchemaData) => boolean;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Validate } from './schema';
|
|
2
|
+
export interface ListenOptions {
|
|
3
|
+
/**
|
|
4
|
+
* Server port to listen
|
|
5
|
+
*/
|
|
6
|
+
port?: number | string;
|
|
7
|
+
/**
|
|
8
|
+
* Server hostname to listen
|
|
9
|
+
*
|
|
10
|
+
* @example `localhost`, `0.0.0.0`
|
|
11
|
+
*
|
|
12
|
+
*/
|
|
13
|
+
hostname?: string;
|
|
14
|
+
/**
|
|
15
|
+
* Development flag.
|
|
16
|
+
*
|
|
17
|
+
* @default NODE_ENV enviroment variable value
|
|
18
|
+
*/
|
|
19
|
+
development?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Global schema validator function that will be used in every route
|
|
22
|
+
*/
|
|
23
|
+
schemaValidator?: Validate;
|
|
24
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "bun-crumb",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"author": "marigold",
|
|
5
|
+
"module": "dist/index.js",
|
|
6
|
+
"license": "UNLICENSED",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/a-marigold/crumb-bun"
|
|
10
|
+
},
|
|
11
|
+
"devDependencies": {
|
|
12
|
+
"@biomejs/biome": "^2.3.10",
|
|
13
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
14
|
+
"@rollup/plugin-typescript": "^12.3.0",
|
|
15
|
+
"@types/bun": "latest",
|
|
16
|
+
"rollup": "^4.53.5",
|
|
17
|
+
"rollup-plugin-dts": "^6.3.0",
|
|
18
|
+
"tslib": "^2.8.1"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
23
|
+
"main": "./dist/index.js",
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": "./dist/index.js"
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"typescript": "^5"
|
|
30
|
+
},
|
|
31
|
+
"keywords": [
|
|
32
|
+
"typescript",
|
|
33
|
+
"bun",
|
|
34
|
+
"bun-crumb"
|
|
35
|
+
],
|
|
36
|
+
"private": false,
|
|
37
|
+
"scripts": {
|
|
38
|
+
"lint": "bunx biome check",
|
|
39
|
+
"build": "bunx rollup -c",
|
|
40
|
+
"prepublishOnly": "bun run build",
|
|
41
|
+
"pshPublic": "bun publish --access public"
|
|
42
|
+
}
|
|
43
|
+
}
|