@mapl/web 0.0.6 → 0.0.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.
@@ -0,0 +1,6 @@
1
+ import { type Err } from 'safe-throw';
2
+ import type { Context } from '../router/context';
3
+ export type TokenParser = (token: string, c: Context) => Promise<any>;
4
+ export type MalformedErr = Err<'Malformed bearer token'>;
5
+ declare const _default: <const T extends TokenParser>(verifier: T) => (c: Parameters<T>[1]) => ReturnType<T> | Promise<MalformedErr>;
6
+ export default _default;
@@ -0,0 +1 @@
1
+ export default (verifier)=>async(c)=>{let tok=c.req.headers.get("Authorization");return typeof tok==="string"&&tok.startsWith("Bearer ")&&tok.length>7?verifier(tok,c):malformedErr};import{err}from"safe-throw";let malformedErr=Promise.resolve(err("Malformed bearer token"));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mapl/web",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "description": "A compiled web standard framework",
5
5
  "keywords": [
6
6
  "fast",
@@ -34,7 +34,7 @@
34
34
  "typescript-eslint": "latest"
35
35
  },
36
36
  "dependencies": {
37
- "@mapl/framework": "^0.0.26",
37
+ "@mapl/framework": "^0.1.0",
38
38
  "@mapl/router": "^0.4.13",
39
39
  "safe-throw": "^0.0.9"
40
40
  }
@@ -1 +1 @@
1
- export default (router)=>{let baseRouter={};let dependencies=[];compileGroup(router.group,[baseRouter,dependencies,"let mh=[],c=mwc(r,mh);",compileHandler,compileErrorHandler,transformRoute],[false,false,null,"return mwb"],"","");return Function("me","mwc",...dependencies.map((_,i)=>"f"+(i+1)),'let [mwh,mwj]=["text/html","application/json"].map(c=>["content-type",c]),[mwn,mwb]=[404,400].map(s=>new Response(null,{status:s}));return(r)=>{'+compile(baseRouter,o2,"r.method",'let u=r.url,s=u.indexOf("/",12)+1,e=u.indexOf("?",s),p=e===-1?u.slice(s):u.substring(s,e);',1)+"return mwn}")(isErr,createContext,...dependencies)};import{compileGroup,createArgSet,isFuncAsync}from"@mapl/framework";import{o2}from"@mapl/router/tree/compiler";import compile from"@mapl/router/method/compiler";import{transformRoute}from"@mapl/router/transform";import{isErr}from"safe-throw";import{createContext}from"./context";let paramArgs=createArgSet(new Array(16).fill(0).map((_1,i)=>"q"+i));let compileReturn=(state,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?"":state[3])+"mh.push("+(typ==="json"?"mwj":"mwh")+");return new Response("+(typ==="json"?"JSON.stringify("+result+")":result)+",c)";return fnAsync&&!scopeAsync?"return (async()=>{"+str+"})":str};let compileHandler=(fn,dat,path,state,scope)=>{let call="f"+state[1].push(fn)+"(";let paramCount=path[0].length;if(paramCount>0)call+=paramArgs[paramCount];if(fn.length>paramCount){call+=paramCount===0?"c":",c";if(!scope[1])return state[3]+compileReturn(state,dat,isFuncAsync(fn),scope[0],true,call+")")}return compileReturn(state,dat,isFuncAsync(fn),scope[0],scope[1],call+")")};let compileErrorHandler=(fn,dat,state,scope)=>{let call="f"+state[1].push(fn)+"(t";if(fn.length>1){call+=",c";if(!scope[1])return state[3]+compileReturn(state,dat,isFuncAsync(fn),scope[0],true,call+")")}return compileReturn(state,dat,isFuncAsync(fn),scope[0],scope[1],call+")")};
1
+ export default (router)=>{let baseRouter={};let dependencies=[];compileGroup(router._,[baseRouter,dependencies,"let mh=[],c=mwc(r,mh);",(fn,dat,path,state,scope)=>{let call="f"+state[1].push(fn)+"(";let paramCount=path[0].length;if(paramCount>0)call+=paramArgs[paramCount];if(fn.length>paramCount){call+=paramCount===0?"c":",c";if(!scope[1])return state[2]+compileReturn(state,dat,isFuncAsync(fn),scope[0],true,call+")")}return compileReturn(state,dat,isFuncAsync(fn),scope[0],scope[1],call+")")},(fn,dat,state,scope)=>{let call="f"+state[1].push(fn)+"(t";if(fn.length>1){call+=",c";if(!scope[1])return state[2]+compileReturn(state,dat,isFuncAsync(fn),scope[0],true,call+")")}return compileReturn(state,dat,isFuncAsync(fn),scope[0],scope[1],call+")")},transformRoute],[false,false,null,"return mwb"],"","");return Function("me","mwc",...dependencies.map((_,i)=>"f"+(i+1)),'let [mwh,mwj]=["text/html","application/json"].map(c=>["content-type",c]),[mwn,mwb]=[404,400].map(s=>new Response(null,{status:s}));return(r)=>{'+compile(baseRouter,o2,"r.method",'let u=r.url,s=u.indexOf("/",12)+1,e=u.indexOf("?",s),p=e===-1?u.slice(s):u.substring(s,e);',1)+"return mwn}")(isErr,createContext,...dependencies)};import{compileGroup,createArgSet,isFuncAsync}from"@mapl/framework";import{o2}from"@mapl/router/tree/compiler";import compile from"@mapl/router/method/compiler";import{transformRoute}from"@mapl/router/transform";import{isErr}from"safe-throw";import{createContext}from"./context";let paramArgs=createArgSet(new Array(16).fill(0).map((_1,i)=>"q"+i));let compileReturn=(state,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?"":state[2])+"mh.push("+(typ==="json"?"mwj":"mwh")+");return new Response("+(typ==="json"?"JSON.stringify("+result+")":result)+",c)";return fnAsync&&!scopeAsync?"return (async()=>{"+str+"})()":scopeAsync?str+"})()":str};
@@ -2,8 +2,8 @@ import type { Group } from '@mapl/framework';
2
2
  import type { Err } from 'safe-throw';
3
3
  import type { Context } from './context';
4
4
  export type ErrorFunc<E extends Err = Err, T extends {} = Record<string, any>, Args extends any[] = any[]> = (err: E, c: Context & T, ...args: Args) => any;
5
- export type HandlerFunc<Params extends string[] = string[], T extends {} = Record<string, any>, Args extends any[] = any[]> = (...args: [...params: Params, c: Context & T, ...args: Args]) => any;
6
- export type MiddlewareFunc<T extends {} = Record<string, any>, Args extends any[] = any[]> = (c: Context & T, ...args: Args) => any;
5
+ export type HandlerFunc<Params extends string[] = string[], T extends {} = Record<string, any>> = (...args: [...params: Params, c: Context & T]) => any;
6
+ export type MiddlewareFunc<T extends {} = Record<string, any>> = (c: Context & T) => any;
7
7
  /**
8
8
  * Basic information to compile
9
9
  */
package/router/index.d.ts CHANGED
@@ -1,21 +1,22 @@
1
1
  import type { Err, InferErr, InferResult } from 'safe-throw';
2
2
  import type { InferRoute } from '@mapl/router/transform';
3
+ import { type AwaitedReturn } from './utils';
3
4
  import type { ErrorFunc, HandlerData, HandlerFunc, HandlerGroup, MiddlewareFunc } from './handler';
4
5
  import { type Methods } from './method';
5
- export type RouteRegister<in out Args extends any[], in out State extends {}, in out E extends Err> = {
6
- [K in Methods | 'any']: <Path extends string>(path: Path, handler: HandlerFunc<InferRoute<Path>, State, Args>, ...data: HandlerData[]) => Router<Args, State, E>;
6
+ export type RouteRegister<in out State extends {}, in out E extends Err> = {
7
+ [K in Methods | 'any']: <Path extends string>(path: Path, handler: HandlerFunc<InferRoute<Path>, State>, ...data: HandlerData[]) => Router<State, E>;
7
8
  };
8
- export interface Router<in out Args extends any[], in out State extends {} = {}, in out E extends Err = never> extends RouteRegister<Args, State, E> {
9
+ export interface Router<in out State extends {} = {}, in out E extends Err = never> extends RouteRegister<State, E> {
9
10
  _state: State;
10
11
  _err: E;
11
- group: HandlerGroup;
12
- apply: (fn: MiddlewareFunc<State, Args>) => this;
13
- check: <const T extends MiddlewareFunc<State, Args>>(fn: T) => Router<Args, State, E | InferErr<ReturnType<T>>>;
14
- set: <Prop extends string, const T extends MiddlewareFunc<State, Args>>(prop: Prop, fn: T) => Router<Args, State & Record<Prop, ReturnType<T>>, E>;
15
- parse: <Prop extends string, const T extends MiddlewareFunc<State, Args>>(prop: Prop, fn: T) => Router<Args, State & Record<Prop, InferResult<ReturnType<T>>>, E | InferErr<ReturnType<T>>>;
16
- route: <Prefix extends string, const App extends Router<Args, any, any>>(prefix: Prefix, app: App) => this;
17
- err: (fn: ErrorFunc<E, State, Args>, ...data: HandlerData[]) => this;
12
+ _: HandlerGroup;
13
+ apply: (fn: MiddlewareFunc<State>) => this;
14
+ check: <const T extends MiddlewareFunc<State>>(fn: T) => Router<State, E | InferErr<AwaitedReturn<T>>>;
15
+ set: <Prop extends string, const T extends MiddlewareFunc<State>>(prop: Prop, fn: T) => Router<State & Record<Prop, AwaitedReturn<T>>, E>;
16
+ parse: <Prop extends string, const T extends MiddlewareFunc<State>>(prop: Prop, fn: T) => Router<State & Record<Prop, InferResult<AwaitedReturn<T>>>, E | InferErr<AwaitedReturn<T>>>;
17
+ route: <Prefix extends string, const App extends Router<any, any>>(prefix: Prefix, app: App) => this;
18
+ err: (fn: ErrorFunc<E, State>, ...data: HandlerData[]) => this;
18
19
  }
19
- export type AnyRouter = Router<any, any, any> | Router<any, any>;
20
+ export type AnyRouter = Router<any, any>;
20
21
  declare const _default: () => Router<[]>;
21
22
  export default _default;
package/router/index.js CHANGED
@@ -1 +1 @@
1
- export default ()=>{let obj=Object.create(routerProto);obj.group=[[],[],null,[]];return obj};import{proto}from"./utils";import{ALL,METHODS}from"./method";let createMethodRegister=(method)=>function(path,handler,...data){this.group[1].push([method,path,handler,proto(...data)]);return this};let routerProto=proto({group:null,apply(f){this.group[0].push([0,f]);return this},check(f){this.group[0].push([1,f]);return this},set(prop,f){this.group[0].push([2,f,prop]);return this},parse(prop,f){this.group[0].push([3,f,prop]);return this},route(prefix,app){this.group[3].push([prefix,app.group]);return this},any:createMethodRegister(ALL),err(f,...data){this.group[2]=[f,proto(...data)];return this}},Object.fromEntries(METHODS.map((method)=>[method,createMethodRegister(method.toUpperCase())])));
1
+ export default ()=>{let obj=Object.create(routerProto);obj._=[[],[],null,[]];return obj};import{proto}from"./utils";import{ALL,METHODS}from"./method";let createMethodRegister=(method)=>function(path,handler,...data){this._[1].push([method,path,handler,proto(...data)]);return this};let routerProto=proto({_:null,apply(f){this._[0].push([0,f]);return this},check(f){this._[0].push([2,f]);return this},set(prop,f){this._[0].push([1,f,prop]);return this},parse(prop,f){this._[0].push([3,f,prop]);return this},route(prefix,app){this._[3].push([prefix,app._]);return this},any:createMethodRegister(ALL),err(f,...data){this._[2]=[f,proto(...data)];return this}},Object.fromEntries(METHODS.map((method)=>[method,createMethodRegister(method.toUpperCase())])));
package/router/utils.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export type UnionToIntersection<U> = (U extends any ? (x: U) => void : never) extends ((x: infer I) => void) ? I : never;
2
+ export type AwaitedReturn<U extends (...a: any[]) => any> = Awaited<ReturnType<U>>;
2
3
  export declare const proto: <T extends any[]>(...f: T) => UnionToIntersection<T[number]>;