Comparing version 0.0.1-beta.8 to 0.0.1-beta.9
@@ -0,22 +1,10 @@ | ||
import { CoreValidator, TypeOf, ValidateFunction } from 'suretype'; | ||
export { v } from 'suretype'; | ||
import { Router, HTTPMethod } from '@ampt/tree-router'; | ||
export { HTTPMethod } from '@ampt/tree-router'; | ||
import { CoreValidator, TypeOf, ValidateFunction } from 'suretype'; | ||
export { v } from 'suretype'; | ||
type ExtractRouteParams<T extends string> = string extends T ? Record<string, string> : T extends `${infer Start}:${infer Param}*` ? { | ||
[k in Param]: string; | ||
} : T extends `${infer Start}:${infer Param}/${infer Rest}` ? { | ||
[k in Param | keyof ExtractRouteParams<Rest>]: string; | ||
} : T extends `${infer Start}:${infer Param}` ? { | ||
[k in Param]: string; | ||
} : {}; | ||
type Params<A, B> = { | ||
[K in keyof A]: K extends keyof B ? B[K] : A[K]; | ||
}; | ||
type ObjectSchema = { | ||
[key: string]: CoreValidator<any>; | ||
}; | ||
type SyncOrAsync<T> = T | Promise<T>; | ||
type ApiHandler<Route extends string, RouterParams> = (event: HttpEvent<Params<ExtractRouteParams<Route>, RouterParams>>) => SyncOrAsync<HttpEvent<Params<ExtractRouteParams<Route>, RouterParams>>>; | ||
type MiddlewareHandler<ParamsType> = (event: HttpEvent<ParamsType>) => SyncOrAsync<HttpEvent<ParamsType> | undefined> | void; | ||
declare class ApiRequest { | ||
@@ -31,2 +19,3 @@ private _request; | ||
} | ||
declare class ApiResponse { | ||
@@ -38,2 +27,3 @@ status: number; | ||
} | ||
declare class HttpEvent<ParamsType = {}> { | ||
@@ -49,2 +39,18 @@ params: ParamsType; | ||
} | ||
type SyncOrAsync<T> = T | Promise<T>; | ||
type MiddlewareHandler<ParamsType> = (event: HttpEvent<ParamsType>) => SyncOrAsync<HttpEvent<ParamsType> | undefined> | void; | ||
type ExtractRouteParams<T extends string> = string extends T ? Record<string, string> : T extends `${infer Start}:${infer Param}*` ? { | ||
[k in Param]: string; | ||
} : T extends `${infer Start}:${infer Param}/${infer Rest}` ? { | ||
[k in Param | keyof ExtractRouteParams<Rest>]: string; | ||
} : T extends `${infer Start}:${infer Param}` ? { | ||
[k in Param]: string; | ||
} : {}; | ||
type Params<A, B> = { | ||
[K in keyof A]: K extends keyof B ? B[K] : A[K]; | ||
}; | ||
type ApiHandler<Route extends string, RouterParams> = (event: HttpEvent<Params<ExtractRouteParams<Route>, RouterParams>>) => SyncOrAsync<HttpEvent<Params<ExtractRouteParams<Route>, RouterParams>>>; | ||
declare class ApiRouter<ParamsSchema extends ObjectSchema> { | ||
@@ -89,2 +95,3 @@ prefix: string; | ||
} | ||
declare class Group { | ||
@@ -96,4 +103,5 @@ routes: Map<string, any>; | ||
} | ||
declare function api(name?: string): Group; | ||
export { ApiHandler, ApiRequest, ApiResponse, ExtractRouteParams, HttpEvent, MiddlewareHandler, ObjectSchema, Params, SyncOrAsync, api }; | ||
export { api }; |
import { createRequire as topLevelCreateRequire } from 'module' | ||
const require = topLevelCreateRequire(import.meta.url) | ||
// index.ts | ||
import { v as v2 } from "suretype"; | ||
// Group.ts | ||
import { http } from "@ampt/sdk"; | ||
// ../../lib/tree-router/util.ts | ||
@@ -452,8 +458,55 @@ function findWildcard(path) { | ||
// index.ts | ||
import { http } from "@ampt/sdk"; | ||
// ApiRouter.ts | ||
import { v, compile } from "suretype"; | ||
// normalize.ts | ||
function normalize(path) { | ||
return path.replace(/^\/*([^\/]+)?\/*$/, "$1").replace(/\/+/g, "/"); | ||
return path.replace(/^\/*|\/*$/g, "").replace(/\/+/g, "/"); | ||
} | ||
// ApiRouter.ts | ||
var ApiRouter = class { | ||
prefix; | ||
router; | ||
middleware; | ||
validate; | ||
constructor({ | ||
prefix, | ||
schema, | ||
middleware | ||
}) { | ||
this.prefix = `/${normalize(prefix)}`; | ||
this.middleware = middleware; | ||
this.router = new Router({ ignoreTrailingSlash: true }); | ||
this.validate = schema ? compile(v.object(schema), { ajvOptions: { coerceTypes: true } }) : () => ({ ok: true }); | ||
} | ||
on(method, _route, handler) { | ||
const normalized = normalize(_route); | ||
const route = this.prefix === "/" ? `/${normalized}` : normalized === "" ? this.prefix : `${this.prefix}/${normalized}`; | ||
return this.router.on(method, route, handler); | ||
} | ||
get(route, handler) { | ||
return this.on("GET", route, handler); | ||
} | ||
post(route, handler) { | ||
return this.on("POST", route, handler); | ||
} | ||
put(route, handler) { | ||
return this.on("PUT", route, handler); | ||
} | ||
patch(route, handler) { | ||
return this.on("PATCH", route, handler); | ||
} | ||
delete(route, handler) { | ||
return this.on("DELETE", route, handler); | ||
} | ||
options(route, handler) { | ||
return this.on("OPTIONS", route, handler); | ||
} | ||
head(route, handler) { | ||
return this.on("HEAD", route, handler); | ||
} | ||
}; | ||
// ApiRequest.ts | ||
async function readStream(stream) { | ||
@@ -490,2 +543,4 @@ let result = ""; | ||
}; | ||
// ApiResponse.ts | ||
var ApiResponse = class { | ||
@@ -504,2 +559,4 @@ status = 200; | ||
}; | ||
// HttpEvent.ts | ||
var HttpEvent = class { | ||
@@ -530,44 +587,4 @@ params; | ||
}; | ||
var ApiRouter = class { | ||
prefix; | ||
router; | ||
middleware; | ||
validate; | ||
constructor({ | ||
prefix, | ||
schema, | ||
middleware | ||
}) { | ||
this.prefix = `/${normalize(prefix)}`; | ||
this.middleware = middleware; | ||
this.router = new Router(); | ||
this.validate = schema ? compile(v.object(schema), { ajvOptions: { coerceTypes: true } }) : () => ({ ok: true }); | ||
} | ||
on(method, _route, handler) { | ||
const normalized = normalize(_route); | ||
const route = normalized === "" ? "" : `/${normalized}`; | ||
return this.router.on(method, `${this.prefix}${route}`, handler); | ||
} | ||
get(route, handler) { | ||
return this.on("GET", route, handler); | ||
} | ||
post(route, handler) { | ||
return this.on("POST", route, handler); | ||
} | ||
put(route, handler) { | ||
return this.on("PUT", route, handler); | ||
} | ||
patch(route, handler) { | ||
return this.on("PATCH", route, handler); | ||
} | ||
delete(route, handler) { | ||
return this.on("DELETE", route, handler); | ||
} | ||
options(route, handler) { | ||
return this.on("OPTIONS", route, handler); | ||
} | ||
head(route, handler) { | ||
return this.on("HEAD", route, handler); | ||
} | ||
}; | ||
// Group.ts | ||
var Group = class { | ||
@@ -611,2 +628,4 @@ routes = /* @__PURE__ */ new Map(); | ||
}; | ||
// Api.ts | ||
var Api = class { | ||
@@ -625,2 +644,4 @@ groups; | ||
}; | ||
// index.ts | ||
var globalApi = new Api(); | ||
@@ -631,7 +652,4 @@ function api(name) { | ||
export { | ||
ApiRequest, | ||
ApiResponse, | ||
HttpEvent, | ||
api, | ||
v | ||
v2 as v | ||
}; |
{ | ||
"name": "@ampt/api", | ||
"version": "0.0.1-beta.8", | ||
"version": "0.0.1-beta.9", | ||
"type": "module", | ||
"types": "dist/index.d.ts", | ||
"scripts": { | ||
"test": "NODE_OPTIONS=\"--no-warnings --experimental-vm-modules\" jest --coverage", | ||
"prepublishOnly": "npm run build", | ||
@@ -17,7 +18,5 @@ "build": "tsup" | ||
"dependencies": { | ||
"suretype": "^3.2.0" | ||
}, | ||
"peerDependencies": { | ||
"suretype": "^3.2.0", | ||
"@ampt/sdk": "^0.0.1-beta.45" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
39406
1381
+ Added@ampt/sdk@^0.0.1-beta.45