@mapl/web 0.1.7 → 0.1.8

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
@@ -3,18 +3,22 @@ A compiled web framework for all runtimes.
3
3
  ```ts
4
4
  import { router, handle, layer, compile } from '@mapl/web';
5
5
 
6
- const api = router.init(
7
- [],
8
- [ handle.get('/', () => 'Hi') ]
9
- );
6
+ const api = router.init([], [
7
+ handle.get('/', () => 'Hi')
8
+ ]);
10
9
 
11
10
  const app = router.init(
11
+ // Middlewares
12
12
  [ layer.attach('id', () => performance.now()) ],
13
+
14
+ // Routes
13
15
  [ handle.get('/path', (c) => c.id) ],
16
+
17
+ // Subrouters
14
18
  { '/api': api }
15
19
  );
16
20
 
17
21
  export default {
18
- fetch: router.compile(app)
22
+ fetch: compile(app)
19
23
  };
20
24
  ```
package/core/compile.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  import type { RouterTag } from "./index.js";
2
+ export declare const compileToString: (router: RouterTag) => string;
2
3
  declare const _default: (router: RouterTag) => ((req: Request) => any);
3
4
  export default _default;
package/core/compile.js CHANGED
@@ -1 +1 @@
1
- import{compileGroup,createArgSet,AsyncFunction,compilerState as state}from"@mapl/framework";import compile from"@mapl/router/method/compiler";import{countParams}from"@mapl/router/path";import{isErr}from"safe-throw";import createContext from"./context.js";let paramArgs=createArgSet(new Array(16).fill(0).map((_1,i)=>`q`+i));let compileReturn=(dat,fnAsync,scopeAsync,contextCreated,result)=>{let typ=dat.type;if(typ===`raw`)return`return `+result;if(fnAsync)result=`await `+result;let str=typ==null?`return new Response(`+result+(contextCreated?`,c)`:`)`):contextCreated?`mh.push(`+(typ===`json`?`mwj`:`mwh`)+`);return new Response(`+(typ===`json`?`JSON.stringify(`+result+`)`:result)+`,c)`:`return new Response(`+(typ===`json`?`JSON.stringify(`+result+`),mwoj`:result+`,mwoh`)+`)`;return scopeAsync?str+`})()`:fnAsync?`return (async()=>{`+str+`})()`:str};let compileHandler=(fn,dat,path,scope)=>{let call=`f`+state[1].push(fn)+`(`;let paramCount=countParams(path);if(paramCount>0)call+=paramArgs[paramCount];if(fn.length>paramCount){call+=paramCount===0?`c`:`,c`;if(!scope[1])return state[2]+compileReturn(dat,fn instanceof AsyncFunction,scope[0],true,call+`)`)}return compileReturn(dat,fn instanceof AsyncFunction,scope[0],scope[1],call+`)`)};let compileErrorHandler=(fn,dat,scope)=>{let call=`f`+state[1].push(fn)+`(t`;if(fn.length>1){call+=`,c`;if(!scope[1])return state[2]+compileReturn(dat,fn instanceof AsyncFunction,scope[0],true,call+`)`)}return compileReturn(dat,fn instanceof AsyncFunction,scope[0],scope[1],call+`)`)};export default router=>{state[0]={};state[1]=[];state[2]=`let mh=[],c=mwc(r,mh);`;state[3]=compileHandler;state[4]=compileErrorHandler;compileGroup(router,[false,false,null,`return mwb`],``,``);return Function(`me`,`mwc`,...state[1].map((_,i)=>`f`+(i+1)),`"use strict";var t=["text/html","application/json"].map(c=>["Content-Type",c]),[mwh,mwj]=t,[mwoh,mwoj]=t.map(c=>({headers:[c]})),[mwn,mwb]=[404,400].map(s=>new Response(null,{status:s}));return(r)=>{`+compile(state[0],`r.method`,`let u=r.url,s=u.indexOf("/",12)+1,e=u.indexOf("?",s),p=e===-1?u.slice(s):u.slice(s,e);`,1)+`return mwn}`)(isErr,createContext,...state[1])};
1
+ import{compileGroup,createArgSet,AsyncFunction,compilerState as state}from"@mapl/framework";import compile from"@mapl/router/method/compiler";import{countParams}from"@mapl/router/path";import{isErr}from"@safe-std/error";import createContext from"./context.js";let paramArgs=createArgSet(new Array(16).fill(0).map((_1,i)=>`q`+i));let compileReturn=(dat,fnAsync,scopeAsync,contextCreated,result)=>{let typ=dat.type;if(typ===`raw`)return`return `+result;if(fnAsync)result=`await `+result;let str=typ==null?`return new Response(`+result+(contextCreated?`,c)`:`)`):contextCreated?`mh.push(`+(typ===`json`?`mwj`:`mwh`)+`);return new Response(`+(typ===`json`?`JSON.stringify(`+result+`)`:result)+`,c)`:`return new Response(`+(typ===`json`?`JSON.stringify(`+result+`),mwoj`:result+`,mwoh`)+`)`;return fnAsync&&!scopeAsync?`return (async()=>{`+str+`})()`:str};let compileHandler=(fn,dat,path,scope)=>{let call=`f`+state[1].push(fn)+`(`;let paramCount=countParams(path);if(paramCount>0)call+=paramArgs[paramCount];if(fn.length>paramCount){call+=paramCount===0?`c`:`,c`;if(!scope[1])return state[2]+compileReturn(dat,fn instanceof AsyncFunction,scope[0],true,call+`)`)}return compileReturn(dat,fn instanceof AsyncFunction,scope[0],scope[1],call+`)`)};let compileErrorHandler=(fn,dat,scope)=>{let call=`f`+state[1].push(fn)+`(t`;if(fn.length>1){call+=`,c`;if(!scope[1])return state[2]+compileReturn(dat,fn instanceof AsyncFunction,scope[0],true,call+`)`)}return compileReturn(dat,fn instanceof AsyncFunction,scope[0],scope[1],call+`)`)};export let compileToString=router=>{state[0]={};state[1]=[];state[2]=`let mh=[],c=mwc(r,mh);`;state[3]=compileHandler;state[4]=compileErrorHandler;compileGroup(router,[false,false,null,`return mwb`,false],``,``);return`"use strict";var t=["text/html","application/json"].map(c=>["Content-Type",c]),[mwh,mwj]=t,[mwoh,mwoj]=t.map(c=>({headers:[c]})),[mwn,mwb]=[404,400].map(s=>new Response(null,{status:s}));return(r)=>{`+compile(state[0],`r.method`,`let u=r.url,s=u.indexOf("/",12)+1,e=u.indexOf("?",s),p=e===-1?u.slice(s):u.slice(s,e);`,1)+`return mwn}`};export default router=>{let debug=compileToString(router);return Function(`me`,`mwc`,...state[1].map((_,i)=>`f`+(i+1)),debug)(isErr,createContext,...state[1])};
package/core/handler.d.ts CHANGED
@@ -1,6 +1,5 @@
1
- import type { Err } from "safe-throw";
1
+ import type { Err } from "@safe-std/error";
2
2
  import type { Context } from "./context.js";
3
- import { type Tag } from "./utils.js";
4
3
  import type { RouterTag } from "./index.js";
5
4
  export type ErrorHandler<E extends Err = Err> = (err: E, c: Context) => any;
6
5
  export type Handler<
@@ -16,7 +15,9 @@ export interface HandlerData extends Record<symbol, any> {
16
15
  }
17
16
  export type InferPath<T extends string> = T extends `${string}*${infer Next}` ? Next extends "*" ? [string] : [string, ...InferPath<Next>] : [];
18
17
  declare const handlerTag: unique symbol;
19
- export type HandlerTag<T> = Tag<T, typeof handlerTag>;
18
+ export interface HandlerTag<T> {
19
+ [handlerTag]: T;
20
+ }
20
21
  /**
21
22
  * Return JSON
22
23
  */
package/core/handler.js CHANGED
@@ -1 +1 @@
1
- import{proto}from"./utils.js";export let json={type:`json`};export let html={type:`html`};export let raw={type:`raw`};export let error=(r,f,...dat)=>{r[2]=[f,proto(...dat)];return r};export let route=(method,path,handler,...dat)=>[method,path,handler,proto(...dat)];export let any=(...a)=>route(``,...a);export let get=(...a)=>route(`GET`,...a);export let post=(...a)=>route(`POST`,...a);export let put=(...a)=>route(`PUT`,...a);export let del=(...a)=>route(`DELETE`,...a);export let patch=(...a)=>route(`PATCH`,...a);export let options=(...a)=>route(`OPTIONS`,...a);export let trace=(...a)=>route(`TRACE`,...a);
1
+ import{proto}from"./utils.js";export let json={type:`json`};export let html={type:`html`};export let raw={type:`raw`};export let error=(r,f,...dat)=>{r[2]=[f,proto(...dat)];return r};export let route=(method,path,handler,...dat)=>[method,path,handler,dat.length===0?{}:proto(...dat)];export let any=(...a)=>route(``,...a);export let get=(...a)=>route(`GET`,...a);export let post=(...a)=>route(`POST`,...a);export let put=(...a)=>route(`PUT`,...a);export let del=(...a)=>route(`DELETE`,...a);export let patch=(...a)=>route(`PATCH`,...a);export let options=(...a)=>route(`OPTIONS`,...a);export let trace=(...a)=>route(`TRACE`,...a);
package/core/index.d.ts CHANGED
@@ -1,12 +1,14 @@
1
1
  import type { HandlerTag } from "./handler.js";
2
2
  import type { AnyMiddlewareTypes } from "./middleware.js";
3
- import type { Tag, ToNever, UnionToIntersection } from "./utils.js";
3
+ import type { UnionToIntersection } from "./utils.js";
4
4
  declare const routerTag: unique symbol;
5
- export type RouterTag<E = any> = Tag<E, typeof routerTag>;
5
+ export interface RouterTag<E = any> {
6
+ [routerTag]: E;
7
+ }
6
8
  export type InferError<
7
9
  T extends AnyMiddlewareTypes[],
8
10
  S extends Record<string, RouterTag>
9
- > = ToNever<S[keyof S][typeof routerTag] | T[number][0]>;
11
+ > = S[keyof S][typeof routerTag] | T[number][0];
10
12
  export type InferHandler<T extends AnyMiddlewareTypes[]> = HandlerTag<T extends [] ? {} : UnionToIntersection<T[number][1]>>;
11
13
  declare const _default: <
12
14
  const T extends AnyMiddlewareTypes[],
@@ -1,4 +1,4 @@
1
- import type { InferErr } from "safe-throw";
1
+ import type { InferErr } from "@safe-std/error";
2
2
  import type { AwaitedReturn } from "./utils.js";
3
3
  import type { Context } from "./context.js";
4
4
  export type MiddlewareHandler = (c: Context) => any;
package/core/utils.d.ts CHANGED
@@ -1,8 +1,6 @@
1
1
  export type UnionToIntersection<U> = (U extends any ? (x: U) => void : never) extends (x: infer I) => void ? I : never;
2
2
  export type AwaitedReturn<U extends (...a: any[]) => any> = Awaited<ReturnType<U>>;
3
- export type ToNever<T> = unknown extends T ? never : T;
4
- export type Tag<
5
- S,
6
- T extends symbol
7
- > = Record<T, S>;
8
- export declare const proto: <T extends any[]>(...f: T) => UnionToIntersection<T[number]>;
3
+ export declare const proto: <
4
+ A extends {},
5
+ T extends any[]
6
+ >(a: A, ...f: T) => A & UnionToIntersection<T[number]>;
package/core/utils.js CHANGED
@@ -1 +1 @@
1
- export let proto=(...f)=>Object.assign(Object.create(null),...f);
1
+ export let proto=(a,...f)=>Object.assign({...a},...f);
package/index.d.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  export { default as router } from "./core/index.js";
2
+ export type * from "./core/index.js";
2
3
  export * as handle from "./core/handler.js";
3
4
  export * as layer from "./core/middleware.js";
4
5
  export { default as compile } from "./core/compile.js";
5
6
  export * as cors from "./utils/cors.js";
6
- export * as bodyParser from "./utils/body.js";
7
- export * as st from "safe-throw/error";
package/index.js CHANGED
@@ -1 +1 @@
1
- export{default as router}from"./core/index.js";export*as handle from"./core/handler.js";export*as layer from"./core/middleware.js";export{default as compile}from"./core/compile.js";export*as cors from"./utils/cors.js";export*as bodyParser from"./utils/body.js";export*as st from"safe-throw/error";
1
+ export{default as router}from"./core/index.js";export*as handle from"./core/handler.js";export*as layer from"./core/middleware.js";export{default as compile}from"./core/compile.js";export*as cors from"./utils/cors.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mapl/web",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "description": "A compiled web framework for all runtimes",
5
5
  "keywords": [],
6
6
  "repository": {
@@ -12,20 +12,19 @@
12
12
  "main": "./index.js",
13
13
  "types": "./index.d.ts",
14
14
  "dependencies": {
15
- "@mapl/framework": "^0.1.7",
16
- "@mapl/router": "^0.5.7",
17
- "safe-throw": "^0.5.2"
15
+ "@mapl/framework": "^0.2.4",
16
+ "@mapl/router": "^0.6.0",
17
+ "@safe-std/error": "^1.0.0"
18
18
  },
19
19
  "exports": {
20
20
  "./constants": "./constants.js",
21
21
  ".": "./index.js",
22
- "./utils/body": "./utils/body.js",
23
- "./core/compile": "./core/compile.js",
24
- "./utils/cors": "./utils/cors.js",
25
22
  "./core": "./core/index.js",
26
- "./core/handler": "./core/handler.js",
23
+ "./core/compile": "./core/compile.js",
24
+ "./core/utils": "./core/utils.js",
27
25
  "./core/middleware": "./core/middleware.js",
28
26
  "./core/context": "./core/context.js",
29
- "./core/utils": "./core/utils.js"
27
+ "./core/handler": "./core/handler.js",
28
+ "./utils/cors": "./utils/cors.js"
30
29
  }
31
30
  }
package/utils/body.d.ts DELETED
@@ -1,17 +0,0 @@
1
- import { type MiddlewareTypes } from "../core/middleware.js";
2
- interface TBody<T> {
3
- body: T;
4
- }
5
- /**
6
- * Parse body to text
7
- */
8
- export declare const text: MiddlewareTypes<never, TBody<string>>;
9
- /**
10
- * Parse body to a blob
11
- */
12
- export declare const blob: MiddlewareTypes<never, TBody<Blob>>;
13
- /**
14
- * Parse body to a byte array
15
- */
16
- export declare const bytes: MiddlewareTypes<never, TBody<Uint8Array>>;
17
- export {};
package/utils/body.js DELETED
@@ -1 +0,0 @@
1
- import{attach}from"../core/middleware.js";export let text=attach(`body`,async c=>c.req.text());export let blob=attach(`body`,async c=>c.req.blob());export let bytes=attach(`body`,async c=>c.req.bytes());