bun-crumb 0.1.0 → 0.3.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 CHANGED
@@ -1,12 +1,9 @@
1
- # <div align='center'> <a> **Crumb** </a> </div>
1
+ # <div align='center'> <a> **Bun Crumb** </a> </div>
2
2
 
3
3
  <div align='center'>
4
-
5
- [![CI](https://github.com/a-marigold/crumb/actions/workflows/ci.yaml/badge.svg)](https://github.com/a-marigold/crumb/actions) ![bun](https://img.shields.io/badge/Bun-000?logo=bun&logoColor=fff) [![npm](https://img.shields.io/npm/v/bun-crumb)](https://npmjs.com/package/bun-crumb)
4
+ [![CI](https://github.com/a-marigold/crumb/actions/workflows/ci.yaml/badge.svg)](https://github.com/a-marigold/crumb/actions) [![bun](https://img.shields.io/badge/Bun-000?logo=bun&logoColor=fff)](https://bun.com) [![npm](https://img.shields.io/npm/v/bun-crumb)](https://npmjs.com/package/bun-crumb) [![Status](https://img.shields.io/badge/BETA-darkgreen?style=for-the-badge)](https://npmjs.com/package/bun-crumb)
6
5
 
7
6
  </div>
8
-
9
-
10
7
 
11
8
  ### Features
12
9
 
@@ -19,7 +16,7 @@
19
16
  ### Installation
20
17
 
21
18
  ```bash
22
- bun add crumb
19
+ bun add bun-crumb
23
20
  ```
24
21
 
25
22
  ### Usage
@@ -27,7 +24,7 @@ bun add crumb
27
24
  Handling Requests
28
25
 
29
26
  ```typescript
30
- import { createRoute, type RouteResponse } from 'crumb-bun';
27
+ import { createRoute, type RouteResponse } from 'bun-crumb';
31
28
 
32
29
  type Product = { title: string; price: number; id: number };
33
30
 
@@ -53,7 +50,7 @@ createRoute({
53
50
  Middleware / Pre-handlers
54
51
 
55
52
  ```typescript
56
- import { createRoute, type RouteResponse } from 'crumb-bun';
53
+ import { createRoute, type RouteResponse } from 'bun-crumb';
57
54
 
58
55
  type Product = { title: string; price: number; id: number };
59
56
 
@@ -83,7 +80,7 @@ createRoute({
83
80
  Setting Headers and Status
84
81
 
85
82
  ```typescript
86
- import { createRoute } from 'crumb-bun';
83
+ import { createRoute } from 'bun-crumb';
87
84
 
88
85
  createRoute({
89
86
  url: '/auth',
package/dist/index.d.ts CHANGED
@@ -61,22 +61,23 @@ type Headers = ResponseInit['headers'];
61
61
  * Type of route handler `request`
62
62
  */
63
63
  interface RouteRequest<T extends {
64
- parsedBody: unknown;
64
+ body: unknown;
65
65
  } = {
66
- parsedBody: unknown;
66
+ body: unknown;
67
67
  }> extends Omit<BunRequest, 'body'> {
68
68
  /**
69
69
  * Parsed, validated from schema body of reqeust
70
70
  */
71
- parsedBody: T extends {
72
- parsedBody: unknown;
73
- } ? T['parsedBody'] : unknown;
71
+ handleBody: () => Promise<T extends {
72
+ body: unknown;
73
+ } ? T['body'] : unknown>;
74
74
  }
75
75
  interface ResponseOptions {
76
76
  status: number;
77
77
  statusText?: string;
78
78
  }
79
79
  /**
80
+ *
80
81
  * Type of route handler `response`
81
82
  */
82
83
  interface RouteResponse<T extends {
@@ -88,7 +89,7 @@ interface RouteResponse<T extends {
88
89
  send: (data: T['body'], options?: ResponseOptions) => void;
89
90
  }
90
91
  type Route = Partial<Record<HttpMethod, RouteOptions>>;
91
- type RouteHandler = (request: RouteRequest, response: RouteResponse) => void;
92
+ type RouteHandler = (request: RouteRequest, response: RouteResponse) => Promise<void> | void;
92
93
  type RouteOptions = {
93
94
  url: string;
94
95
  method: HttpMethod;
@@ -168,6 +169,7 @@ declare const wrapRouteCallback: (routeOptions: RouteOptions, schemaValidator?:
168
169
  * @returns {PreparedRoute} Route object with `GET` or other http method keys with wrapped route callbacks.
169
170
  *
170
171
  * @example
172
+ *
171
173
  * ```typescript
172
174
  * prepareRoute({
173
175
  * GET: {
package/dist/index.js CHANGED
@@ -1 +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};
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,o)=>{const r={"application/json":t=>t.json().catch(t=>{throw new e(400,t)}).then(t=>{if(s&&o&&!o(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&&o&&!o(t,s))throw new e(400,"Request does not match schema");return t})};return n in r?r[n](t):Promise.reject(new e(415,"Unsupported media type"))},o=(t,n)=>o=>{const r=o.headers.get("Content-Type")??"text/plain",a=o;return a.handleBody=()=>s(o,r,t.schema,n).then(t=>t),Promise.resolve(((t,e)=>{let n,s,o=null;const r={},a={setHeader:(t,e)=>{r[t]=e},send:(t,e)=>{"object"==typeof t?r["Content-Type"]="application/json":"string"==typeof t&&(r["Content-Type"]="text/plain"),o=t,n=e?.status,s=e?.statusText}};return Promise.all([e.onRequest?.(t,a),e.preHandler?.(t,a),e.handler(t,a)]).then(()=>new Response(null==o?null:JSON.stringify(o),{headers:r,status:n,statusText:s}))})(a,t)).then(t=>t).catch(t=>t instanceof e?new Response(t.message,{status:t.status}):new Response("Internal server error",{status:500}))},r=(t,e)=>{const n={};for(const s in t)Object.hasOwn(t,s)&&(n[s]=o(t[s],e));return n},a=(t,e)=>{const n={};for(const s of t)n[s[0]]=r(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)})},h=t=>{const e=n.get(t.url);e?e[t.method]=t:n.set(t.url,{[t.method]:t})};export{n as _routes,h as createRoute,s as handleBody,c as listen,r as prepareRoute,a as prepareRoutes,o as wrapRouteCallback};
package/dist/server.d.ts CHANGED
@@ -48,6 +48,7 @@ export declare const wrapRouteCallback: (routeOptions: RouteOptions, schemaValid
48
48
  * @returns {PreparedRoute} Route object with `GET` or other http method keys with wrapped route callbacks.
49
49
  *
50
50
  * @example
51
+ *
51
52
  * ```typescript
52
53
  * prepareRoute({
53
54
  * GET: {
@@ -21,22 +21,23 @@ export type Headers = ResponseInit['headers'];
21
21
  * Type of route handler `request`
22
22
  */
23
23
  export interface RouteRequest<T extends {
24
- parsedBody: unknown;
24
+ body: unknown;
25
25
  } = {
26
- parsedBody: unknown;
26
+ body: unknown;
27
27
  }> extends Omit<BunRequest, 'body'> {
28
28
  /**
29
29
  * Parsed, validated from schema body of reqeust
30
30
  */
31
- parsedBody: T extends {
32
- parsedBody: unknown;
33
- } ? T['parsedBody'] : unknown;
31
+ handleBody: () => Promise<T extends {
32
+ body: unknown;
33
+ } ? T['body'] : unknown>;
34
34
  }
35
35
  export interface ResponseOptions {
36
36
  status: number;
37
37
  statusText?: string;
38
38
  }
39
39
  /**
40
+ *
40
41
  * Type of route handler `response`
41
42
  */
42
43
  export interface RouteResponse<T extends {
@@ -48,7 +49,7 @@ export interface RouteResponse<T extends {
48
49
  send: (data: T['body'], options?: ResponseOptions) => void;
49
50
  }
50
51
  export type Route = Partial<Record<HttpMethod, RouteOptions>>;
51
- export type RouteHandler = (request: RouteRequest, response: RouteResponse) => void;
52
+ export type RouteHandler = (request: RouteRequest, response: RouteResponse) => Promise<void> | void;
52
53
  export type RouteOptions = {
53
54
  url: string;
54
55
  method: HttpMethod;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bun-crumb",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "author": "marigold",
5
5
  "module": "dist/index.js",
6
6
  "license": "UNLICENSED",