bun-crumb 0.6.0 → 0.7.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/dist/index.d.ts +4 -2
- package/dist/index.js +1 -1
- package/dist/server.d.ts +1 -1
- package/dist/types/route.d.ts +2 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -49,6 +49,7 @@ type Validate = (data: unknown, schema: SchemaData) => boolean;
|
|
|
49
49
|
* ```
|
|
50
50
|
*/
|
|
51
51
|
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS';
|
|
52
|
+
type RedirectStatusCode = 300 | 301 | 302 | 303 | 304 | 307 | 308;
|
|
52
53
|
/**
|
|
53
54
|
* HTTP Header struct.
|
|
54
55
|
*/
|
|
@@ -87,6 +88,7 @@ interface RouteResponse<T extends {
|
|
|
87
88
|
}> {
|
|
88
89
|
setHeader: (name: Header['name'], value: Header['value']) => void;
|
|
89
90
|
send: (data: T['body'], options?: ResponseOptions) => void;
|
|
91
|
+
redirect: (url: string, status: RedirectStatusCode | (number & {})) => void;
|
|
90
92
|
}
|
|
91
93
|
type Route = Partial<Record<HttpMethod, RouteOptions>>;
|
|
92
94
|
type RouteHandler = (request: RouteRequest, response: RouteResponse) => Promise<void> | void;
|
|
@@ -143,7 +145,7 @@ interface ListenOptions {
|
|
|
143
145
|
* listen(PORT, 'localhost');
|
|
144
146
|
* ```
|
|
145
147
|
*/
|
|
146
|
-
declare const listen: (options
|
|
148
|
+
declare const listen: (options?: ListenOptions) => void;
|
|
147
149
|
|
|
148
150
|
/**
|
|
149
151
|
* Creates a route with `url`, `method` and `handler`.
|
|
@@ -201,4 +203,4 @@ declare const listen: (options: ListenOptions) => void;
|
|
|
201
203
|
declare const createRoute: (routeOptions: RouteOptions) => void;
|
|
202
204
|
|
|
203
205
|
export { createRoute, listen };
|
|
204
|
-
export type { Header, Headers, HttpMethod, ListenOptions, ResponseOptions, Route, RouteHandler, RouteOptions, RouteRequest, RouteResponse, Schema, SchemaData, Validate };
|
|
206
|
+
export type { Header, Headers, HttpMethod, ListenOptions, RedirectStatusCode, ResponseOptions, Route, RouteHandler, RouteOptions, RouteRequest, RouteResponse, Schema, SchemaData, Validate };
|
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 s=new Map,n=(t,s)=>n=>{const o=n.headers.get("Content-Type")??"text/plain",r=n;return r.handleBody=()=>((t,s,n,o)=>{const r={"application/json":t=>t.json().catch(()=>{throw new e(400,"Bad Request")}).then(t=>{if(n&&o&&!o(t,n))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(n&&o&&!o(t,n))throw new e(400,"Request does not match schema");return t})};return s in r?r[s](t):Promise.reject(new e(415,"Unsupported media type"))})(n,o,t.schema,s),Promise.resolve(((t,e)=>{let s=200,n="",o="";const r={},a={setHeader:(t,e)=>{r[t]=e},send:(t,e)=>{"object"==typeof t?(r["Content-Type"]="application/json",o=JSON.stringify(t)):"string"==typeof t&&(r["Content-Type"]="text/plain",o=t),e&&(s=e.status,n=e.statusText)}};return Promise.resolve(e.handler(t,a)).then(()=>new Response(o,{headers:r,status:s,statusText:n}))})(r,t)).then(t=>t).catch(t=>t instanceof e?new Response(t.message,{status:t.status}):new Response("Internal server error",{status:500}))},o=(t,e)=>{const s={};for(const o in t)Object.hasOwn(t,o)&&(s[o]=n(t[o],e));return s},r=(t,e)=>{const s={};for(const n of t)s[n[0]]=o(n[1],e);return t.clear(),s},a=e=>{t({port:e
|
|
1
|
+
import{serve as t}from"bun";class e extends Error{status;constructor(t,e){super(e),this.status=t,this.name="HttpError"}}const s=new Map,n=(t,s)=>n=>{const o=n.headers.get("Content-Type")??"text/plain",r=n;return r.handleBody=()=>((t,s,n,o)=>{const r={"application/json":t=>t.json().catch(()=>{throw new e(400,"Bad Request")}).then(t=>{if(n&&o&&!o(t,n))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(n&&o&&!o(t,n))throw new e(400,"Request does not match schema");return t})};return s in r?r[s](t):Promise.reject(new e(415,"Unsupported media type"))})(n,o,t.schema,s),Promise.resolve(((t,e)=>{let s=200,n="",o="";const r={},a={setHeader:(t,e)=>{r[t]=e},send:(t,e)=>{"object"==typeof t?(r["Content-Type"]="application/json",o=JSON.stringify(t)):"string"==typeof t&&(r["Content-Type"]="text/plain",o=t),e&&(s=e.status,n=e.statusText)},redirect:(t,e)=>{o="",s=e,r.Location=t}};return Promise.resolve(e.handler(t,a)).then(()=>new Response(o,{headers:r,status:s,statusText:n}))})(r,t)).then(t=>t).catch(t=>t instanceof e?new Response(t.message,{status:t.status}):new Response("Internal server error",{status:500}))},o=(t,e)=>{const s={};for(const o in t)Object.hasOwn(t,o)&&(s[o]=n(t[o],e));return s},r=(t,e)=>{const s={};for(const n of t)s[n[0]]=o(n[1],e);return t.clear(),s},a=e=>{t({port:e?.port,hostname:e?.hostname,development:e?.development??!1,routes:r(s,e?.schemaValidator)})},c=t=>{const e=s.get(t.url);e?e[t.method]=t:s.set(t.url,{[t.method]:t})};export{c as createRoute,a as listen};
|
package/dist/server.d.ts
CHANGED
|
@@ -104,5 +104,5 @@ export declare const prepareRoutes: (routes: Routes, schemaValidator?: Validate)
|
|
|
104
104
|
* listen(PORT, 'localhost');
|
|
105
105
|
* ```
|
|
106
106
|
*/
|
|
107
|
-
export declare const listen: (options
|
|
107
|
+
export declare const listen: (options?: ListenOptions) => void;
|
|
108
108
|
export {};
|
package/dist/types/route.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ import type { SchemaData } from './schema';
|
|
|
9
9
|
* ```
|
|
10
10
|
*/
|
|
11
11
|
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS';
|
|
12
|
+
export type RedirectStatusCode = 300 | 301 | 302 | 303 | 304 | 307 | 308;
|
|
12
13
|
/**
|
|
13
14
|
* HTTP Header struct.
|
|
14
15
|
*/
|
|
@@ -47,6 +48,7 @@ export interface RouteResponse<T extends {
|
|
|
47
48
|
}> {
|
|
48
49
|
setHeader: (name: Header['name'], value: Header['value']) => void;
|
|
49
50
|
send: (data: T['body'], options?: ResponseOptions) => void;
|
|
51
|
+
redirect: (url: string, status: RedirectStatusCode | (number & {})) => void;
|
|
50
52
|
}
|
|
51
53
|
export type Route = Partial<Record<HttpMethod, RouteOptions>>;
|
|
52
54
|
export type RouteHandler = (request: RouteRequest, response: RouteResponse) => Promise<void> | void;
|