bun-crumb 0.9.0 → 0.11.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 CHANGED
@@ -58,7 +58,6 @@ type Header = {
58
58
  name: string;
59
59
  value: string;
60
60
  };
61
- type Headers = ResponseInit['headers'];
62
61
 
63
62
  /**
64
63
  *
@@ -84,6 +83,7 @@ interface RouteRequestGeneric {
84
83
  * Type of route handler `request` parameter
85
84
  */
86
85
  interface RouteRequest<T extends RouteRequestGeneric = RouteRequestGeneric> extends Omit<BunRequest, 'params'> {
86
+ query: URLSearchParams;
87
87
  params: RouteRequestParams<T['params']>;
88
88
  /**
89
89
  * #### Parses and validates body of request.
@@ -164,6 +164,14 @@ interface RouteResponse<T extends {
164
164
  * ```
165
165
  */
166
166
  setHeader: (name: Header['name'], value: Header['value']) => void;
167
+ /**
168
+ *
169
+ * @param options `Bun.Cookie` options parametr
170
+ *
171
+ *
172
+ *
173
+ *
174
+ */
167
175
  setCookie: (options: CookieInit) => void;
168
176
  }
169
177
  type Route = Partial<Record<HttpMethod, RouteOptions>>;
@@ -280,4 +288,4 @@ declare const listen: (options?: ListenOptions) => void;
280
288
  declare const createRoute: (routeOptions: RouteOptions) => void;
281
289
 
282
290
  export { createRoute, listen };
283
- export type { Header, Headers, HttpMethod, ListenOptions, RedirectStatusCode, ResponseOptions, Route, RouteHandler, RouteOptions, RouteRequest, RouteRequestGeneric, RouteRequestParams, RouteResponse, Schema, SchemaData, Validate };
291
+ export type { Header, HttpMethod, ListenOptions, RedirectStatusCode, ResponseOptions, Route, RouteHandler, RouteOptions, RouteRequest, RouteRequestGeneric, RouteRequestParams, RouteResponse, Schema, SchemaData, Validate };
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import{serve as e,Cookie as t}from"bun";class s extends Error{status;constructor(e,t){super(t),this.status=e,this.name="HttpError"}}const n=new Map,o=(e,n)=>o=>{const r=o.headers.get("Content-Type")??"text/plain",a=o;return a.handleBody=()=>((e,t,n,o)=>{const r={"application/json":e=>e.json().catch(()=>{throw new s(400,"Bad Request")}).then(e=>{if(n&&o&&!o(e,n))throw new s(400,"Request does not match schema");return e}),"text/plain":e=>e.text().catch(e=>{throw new s(400,e)}).then(e=>{if(n&&o&&!o(e,n))throw new s(400,"Request does not match schema");return e})};return t in r?r[t](e):Promise.reject(new s(415,"Unsupported media type"))})(o,r,e.schema,n),Promise.resolve(((e,s)=>{let n=200,o="",r="";const a=new Headers,c={send:(e,t)=>{"object"==typeof e?(a.has("Content-Type")||a.set("Content-Type","application/json"),r=JSON.stringify(e)):"string"==typeof e?(a.has("Content-Type")||a.set("Content-Type","text/plain"),r=e):r=e,t&&(n=t.status,o=t.statusText)},redirect:(e,t)=>{r="",n=t||302,a.set("Location",e)},setHeader:(e,t)=>{a.set(e,t)},setCookie:e=>{a.append("Set-Cookie",new t(e).toString())}};return Promise.resolve(s.handler(e,c)).then(()=>new Response(r,{headers:a,status:n,statusText:o}))})(a,e)).then(e=>e).catch(e=>e instanceof s?new Response(e.message,{status:e.status}):new Response("Internal server error",{status:500}))},r=(e,t)=>{const s={};for(const n in e)Object.hasOwn(e,n)&&(s[n]=o(e[n],t));return s},a=(e,t)=>{const s={};for(const n of e)s[n[0]]=r(n[1],t);return e.clear(),s},c=t=>{e({port:t?.port,hostname:t?.hostname,development:t?.development??!1,routes:a(n,t?.schemaValidator)})},p=e=>{const t=n.get(e.url);t?t[e.method]=e:n.set(e.url,{[e.method]:e})};export{p as createRoute,c as listen};
1
+ import{serve as e,Cookie as t}from"bun";class s extends Error{status;constructor(e,t){super(t),this.status=e,this.name="HttpError"}}const n=new Map,o=(e,n)=>o=>{const r=o.headers.get("Content-Type")??"text/plain",a=o;return a.handleBody=()=>((e,t,n,o)=>{const r={"application/json":e=>e.json().catch(()=>{throw new s(400,"Bad Request")}).then(e=>{if(n&&o&&!o(e,n))throw new s(400,"Request does not match schema");return e}),"text/plain":e=>e.text().catch(e=>{throw new s(400,e)}).then(e=>{if(n&&o&&!o(e,n))throw new s(400,"Request does not match schema");return e})};return t in r?r[t](e):Promise.reject(new s(415,"Unsupported media type"))})(o,r,e.schema,n),a.query=new URLSearchParams(o.url.split("?")[1]||""),Promise.resolve(((e,s)=>{let n=200,o="",r="";const a=new Headers,c={send:(e,t)=>{"object"==typeof e?(a.has("Content-Type")||a.set("Content-Type","application/json"),r=JSON.stringify(e)):"string"==typeof e?(a.has("Content-Type")||a.set("Content-Type","text/plain"),r=e):r=e,t&&(n=t.status,o=t.statusText)},redirect:(e,t)=>{r="",n=t||302,a.set("Location",e)},setHeader:(e,t)=>{a.set(e,t)},setCookie:e=>{a.append("Set-Cookie",new t(e).toString())}};return Promise.resolve(s.handler(e,c)).then(()=>new Response(r,{headers:a,status:n,statusText:o}))})(a,e)).then(e=>e).catch(e=>e instanceof s?new Response(e.message,{status:e.status}):new Response("Internal server error",{status:500}))},r=(e,t)=>{const s={};for(const n in e)Object.hasOwn(e,n)&&(s[n]=o(e[n],t));return s},a=(e,t)=>{const s={};for(const n of e)s[n[0]]=r(n[1],t);return e.clear(),s},c=t=>{e({port:t?.port,hostname:t?.hostname,development:t?.development??!1,routes:a(n,t?.schemaValidator)})},p=e=>{const t=n.get(e.url);t?t[e.method]=e:n.set(e.url,{[e.method]:e})};export{p as createRoute,c as listen};
@@ -0,0 +1 @@
1
+ export { _routes, prepareRoutes, handleBody } from '../server';
package/dist/server.d.ts CHANGED
@@ -15,7 +15,8 @@ export type Routes = Map<RouteOptions['url'], Route>;
15
15
  export declare const _routes: Routes;
16
16
  /**
17
17
  *
18
- * Runtime function that used in request.
18
+ *
19
+ *
19
20
  * Parses body to supported content type (json, plain text) and validates it with route schema.
20
21
  *
21
22
  * @param {BunRequest} request incoming bun request.
@@ -25,6 +25,7 @@ export interface RouteRequestGeneric {
25
25
  * Type of route handler `request` parameter
26
26
  */
27
27
  export interface RouteRequest<T extends RouteRequestGeneric = RouteRequestGeneric> extends Omit<BunRequest, 'params'> {
28
+ query: URLSearchParams;
28
29
  params: RouteRequestParams<T['params']>;
29
30
  /**
30
31
  * #### Parses and validates body of request.
@@ -105,6 +106,14 @@ export interface RouteResponse<T extends {
105
106
  * ```
106
107
  */
107
108
  setHeader: (name: Header['name'], value: Header['value']) => void;
109
+ /**
110
+ *
111
+ * @param options `Bun.Cookie` options parametr
112
+ *
113
+ *
114
+ *
115
+ *
116
+ */
108
117
  setCookie: (options: CookieInit) => void;
109
118
  }
110
119
  export type Route = Partial<Record<HttpMethod, RouteOptions>>;
@@ -16,4 +16,3 @@ export type Header = {
16
16
  name: string;
17
17
  value: string;
18
18
  };
19
- export type Headers = ResponseInit['headers'];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bun-crumb",
3
- "version": "0.9.0",
3
+ "version": "0.11.0",
4
4
  "author": "marigold",
5
5
  "module": "dist/index.js",
6
6
  "license": "UNLICENSED",