@taserjs/router 0.0.4 → 0.0.5
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/cjs/builder/app.cjs +22 -0
- package/dist/cjs/builder/app.cjs.map +1 -0
- package/dist/cjs/builder/app.d.cts +11 -0
- package/dist/cjs/{constants.cjs → builder/constants.cjs} +1 -1
- package/dist/cjs/builder/constants.cjs.map +1 -0
- package/dist/cjs/{factories.cjs → builder/factories.cjs} +1 -1
- package/dist/cjs/builder/factories.cjs.map +1 -0
- package/dist/cjs/{factories.d.cts → builder/factories.d.cts} +2 -2
- package/dist/cjs/{middleware.cjs → builder/middleware.cjs} +1 -1
- package/dist/cjs/builder/middleware.cjs.map +1 -0
- package/dist/cjs/{middleware.d.cts → builder/middleware.d.cts} +1 -1
- package/dist/cjs/{route.cjs → builder/route.cjs} +19 -33
- package/dist/cjs/builder/route.cjs.map +1 -0
- package/dist/cjs/{route.d.cts → builder/route.d.cts} +3 -3
- package/dist/cjs/{taser-router.cjs → builder/router.cjs} +38 -11
- package/dist/cjs/builder/router.cjs.map +1 -0
- package/dist/cjs/builder/router.d.cts +48 -0
- package/dist/cjs/{create-context.cjs → context/create-context.cjs} +1 -1
- package/dist/cjs/context/create-context.cjs.map +1 -0
- package/dist/cjs/{create-context.d.cts → context/create-context.d.cts} +2 -2
- package/dist/cjs/define/validators.cjs +1 -1
- package/dist/cjs/define/validators.cjs.map +1 -1
- package/dist/cjs/define/validators.d.cts +1 -1
- package/dist/cjs/index.cjs +6 -16
- package/dist/cjs/index.d.cts +6 -7
- package/dist/cjs/middleware/body-limit.cjs +2 -2
- package/dist/cjs/middleware/body-limit.cjs.map +1 -1
- package/dist/cjs/middleware/compress.cjs +2 -2
- package/dist/cjs/middleware/compress.cjs.map +1 -1
- package/dist/cjs/middleware/cors.cjs +2 -2
- package/dist/cjs/middleware/cors.cjs.map +1 -1
- package/dist/cjs/middleware/csrf.cjs +2 -2
- package/dist/cjs/middleware/csrf.cjs.map +1 -1
- package/dist/cjs/middleware/etag.cjs +2 -2
- package/dist/cjs/middleware/etag.cjs.map +1 -1
- package/dist/cjs/middleware/jwk.cjs +2 -8
- package/dist/cjs/middleware/jwk.cjs.map +1 -1
- package/dist/cjs/middleware/jwt.cjs +2 -8
- package/dist/cjs/middleware/jwt.cjs.map +1 -1
- package/dist/cjs/middleware/secure-headers.cjs +2 -2
- package/dist/cjs/middleware/secure-headers.cjs.map +1 -1
- package/dist/cjs/middleware/timing.cjs +2 -2
- package/dist/cjs/middleware/timing.cjs.map +1 -1
- package/dist/cjs/middleware/wrap-hono.cjs +10 -0
- package/dist/cjs/middleware/wrap-hono.cjs.map +1 -1
- package/dist/cjs/middleware/wrap-hono.d.cts +4 -0
- package/dist/cjs/types/app.d.cts +7 -7
- package/dist/cjs/types/index.d.cts +32 -13
- package/dist/cjs/types/returns.d.cts +2 -2
- package/dist/cjs/types/type-utils.d.cts +1 -2
- package/dist/cjs/types/units.cjs.map +1 -1
- package/dist/cjs/types/units.d.cts +4 -9
- package/dist/esm/builder/app.d.ts +11 -0
- package/dist/esm/builder/app.js +22 -0
- package/dist/esm/builder/app.js.map +1 -0
- package/dist/esm/{constants.js → builder/constants.js} +1 -1
- package/dist/esm/builder/constants.js.map +1 -0
- package/dist/esm/{factories.d.ts → builder/factories.d.ts} +2 -2
- package/dist/esm/{factories.js → builder/factories.js} +1 -1
- package/dist/esm/builder/factories.js.map +1 -0
- package/dist/esm/{middleware.d.ts → builder/middleware.d.ts} +1 -1
- package/dist/esm/{middleware.js → builder/middleware.js} +1 -1
- package/dist/esm/builder/middleware.js.map +1 -0
- package/dist/esm/{route.d.ts → builder/route.d.ts} +3 -3
- package/dist/esm/{route.js → builder/route.js} +19 -33
- package/dist/esm/builder/route.js.map +1 -0
- package/dist/esm/builder/router.d.ts +48 -0
- package/dist/esm/{taser-router.js → builder/router.js} +37 -10
- package/dist/esm/builder/router.js.map +1 -0
- package/dist/esm/{create-context.d.ts → context/create-context.d.ts} +2 -2
- package/dist/esm/{create-context.js → context/create-context.js} +1 -1
- package/dist/esm/context/create-context.js.map +1 -0
- package/dist/esm/define/validators.d.ts +1 -1
- package/dist/esm/define/validators.js +1 -1
- package/dist/esm/define/validators.js.map +1 -1
- package/dist/esm/index.d.ts +6 -7
- package/dist/esm/index.js +5 -6
- package/dist/esm/middleware/body-limit.js +2 -2
- package/dist/esm/middleware/body-limit.js.map +1 -1
- package/dist/esm/middleware/compress.js +2 -2
- package/dist/esm/middleware/compress.js.map +1 -1
- package/dist/esm/middleware/cors.js +2 -2
- package/dist/esm/middleware/cors.js.map +1 -1
- package/dist/esm/middleware/csrf.js +2 -2
- package/dist/esm/middleware/csrf.js.map +1 -1
- package/dist/esm/middleware/etag.js +2 -2
- package/dist/esm/middleware/etag.js.map +1 -1
- package/dist/esm/middleware/jwk.js +2 -8
- package/dist/esm/middleware/jwk.js.map +1 -1
- package/dist/esm/middleware/jwt.js +2 -8
- package/dist/esm/middleware/jwt.js.map +1 -1
- package/dist/esm/middleware/secure-headers.js +2 -2
- package/dist/esm/middleware/secure-headers.js.map +1 -1
- package/dist/esm/middleware/timing.js +2 -2
- package/dist/esm/middleware/timing.js.map +1 -1
- package/dist/esm/middleware/wrap-hono.d.ts +4 -0
- package/dist/esm/middleware/wrap-hono.js +10 -1
- package/dist/esm/middleware/wrap-hono.js.map +1 -1
- package/dist/esm/types/app.d.ts +7 -7
- package/dist/esm/types/index.d.ts +32 -13
- package/dist/esm/types/returns.d.ts +2 -2
- package/dist/esm/types/type-utils.d.ts +1 -2
- package/dist/esm/types/units.d.ts +4 -9
- package/dist/esm/types/units.js.map +1 -1
- package/package.json +3 -3
- package/src/builder/app.ts +28 -0
- package/src/{factories.ts → builder/factories.ts} +2 -2
- package/src/{middleware.ts → builder/middleware.ts} +2 -2
- package/src/{route.ts → builder/route.ts} +30 -34
- package/src/builder/router.ts +347 -0
- package/src/{create-context.ts → context/create-context.ts} +2 -2
- package/src/define/validators.ts +1 -1
- package/src/index.ts +9 -12
- package/src/middleware/body-limit.ts +2 -2
- package/src/middleware/compress.ts +2 -2
- package/src/middleware/cors.ts +2 -2
- package/src/middleware/csrf.ts +2 -2
- package/src/middleware/etag.ts +2 -2
- package/src/middleware/jwk.ts +2 -7
- package/src/middleware/jwt.ts +2 -7
- package/src/middleware/secure-headers.ts +2 -2
- package/src/middleware/timing.ts +2 -2
- package/src/middleware/wrap-hono.ts +13 -0
- package/src/types/app.ts +12 -8
- package/src/types/index.ts +44 -10
- package/src/types/returns.ts +2 -2
- package/src/types/type-utils.ts +1 -2
- package/src/types/units.ts +4 -8
- package/dist/cjs/constants.cjs.map +0 -1
- package/dist/cjs/create-context.cjs.map +0 -1
- package/dist/cjs/factories.cjs.map +0 -1
- package/dist/cjs/middleware.cjs.map +0 -1
- package/dist/cjs/route.cjs.map +0 -1
- package/dist/cjs/taser-app.cjs +0 -36
- package/dist/cjs/taser-app.cjs.map +0 -1
- package/dist/cjs/taser-app.d.cts +0 -18
- package/dist/cjs/taser-router.cjs.map +0 -1
- package/dist/cjs/taser-router.d.cts +0 -42
- package/dist/esm/constants.js.map +0 -1
- package/dist/esm/create-context.js.map +0 -1
- package/dist/esm/factories.js.map +0 -1
- package/dist/esm/middleware.js.map +0 -1
- package/dist/esm/route.js.map +0 -1
- package/dist/esm/taser-app.d.ts +0 -18
- package/dist/esm/taser-app.js +0 -35
- package/dist/esm/taser-app.js.map +0 -1
- package/dist/esm/taser-router.d.ts +0 -42
- package/dist/esm/taser-router.js.map +0 -1
- package/src/taser-app.ts +0 -38
- package/src/taser-router.ts +0 -165
- /package/dist/cjs/{constants.d.cts → builder/constants.d.cts} +0 -0
- /package/dist/esm/{constants.d.ts → builder/constants.d.ts} +0 -0
- /package/src/{constants.ts → builder/constants.ts} +0 -0
package/src/taser-router.ts
DELETED
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createTaserRuntime,
|
|
3
|
-
type NotFoundHandler,
|
|
4
|
-
type OnErrorHandler,
|
|
5
|
-
type RouteManifestShape,
|
|
6
|
-
} from "@taserjs/router-core";
|
|
7
|
-
import { normalizeOnError } from "@taserjs/router-utils";
|
|
8
|
-
|
|
9
|
-
import {
|
|
10
|
-
createAllRoute,
|
|
11
|
-
createAnyRoute,
|
|
12
|
-
createDeleteRoute,
|
|
13
|
-
createGetRoute,
|
|
14
|
-
createHeadRoute,
|
|
15
|
-
createOptionsRoute,
|
|
16
|
-
createPatchRoute,
|
|
17
|
-
createPostRoute,
|
|
18
|
-
createPutRoute,
|
|
19
|
-
} from "./factories.js";
|
|
20
|
-
import { defineHandler } from "./define/handler.js";
|
|
21
|
-
import { createMiddleware } from "./middleware.js";
|
|
22
|
-
import { TaserApp } from "./taser-app.js";
|
|
23
|
-
import type { ContextDefinition, CreateTaserAppOptions, OnErrorOptions } from "./types/app.js";
|
|
24
|
-
import type { AppContext } from "./types/units.js";
|
|
25
|
-
import type { HandlerBuilder, LayoutId, MiddlewareBuilder } from "./types/index.js";
|
|
26
|
-
import type { Schema } from "./types/schema.js";
|
|
27
|
-
import type {
|
|
28
|
-
CreateAllRoute,
|
|
29
|
-
CreateAnyRoute,
|
|
30
|
-
CreateWithBodyRoute,
|
|
31
|
-
CreateWithoutBodyRoute,
|
|
32
|
-
} from "./factories.js";
|
|
33
|
-
|
|
34
|
-
function toOnErrorHandler(
|
|
35
|
-
onErrorOptions: OnErrorOptions | OnErrorOptions["handle"],
|
|
36
|
-
): OnErrorHandler {
|
|
37
|
-
return normalizeOnError(onErrorOptions);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
type RouterState = {
|
|
41
|
-
options: CreateTaserAppOptions;
|
|
42
|
-
contextDef: ContextDefinition<Record<string, unknown>, Record<string, unknown>>;
|
|
43
|
-
onError?: OnErrorHandler;
|
|
44
|
-
notFound?: NotFoundHandler;
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
const emptyContext: ContextDefinition<Record<string, unknown>, Record<string, unknown>> = {};
|
|
48
|
-
|
|
49
|
-
export class TaserRouter<TAppContext extends Record<string, unknown> = AppContext> {
|
|
50
|
-
private readonly state: RouterState;
|
|
51
|
-
|
|
52
|
-
constructor(options: CreateTaserAppOptions = {}, state?: Partial<Omit<RouterState, "options">>) {
|
|
53
|
-
this.state = {
|
|
54
|
-
options,
|
|
55
|
-
contextDef: state?.contextDef ?? emptyContext,
|
|
56
|
-
...(state?.onError !== undefined ? { onError: state.onError } : {}),
|
|
57
|
-
...(state?.notFound !== undefined ? { notFound: state.notFound } : {}),
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
context<TBoot extends Record<string, unknown>, TReq extends Record<string, unknown>>(
|
|
62
|
-
definition: ContextDefinition<TBoot, TReq>,
|
|
63
|
-
): TaserRouter<TBoot & TReq> {
|
|
64
|
-
this.state.contextDef = definition;
|
|
65
|
-
return this as unknown as TaserRouter<TBoot & TReq>;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
onError<
|
|
69
|
-
TResponses extends import("./types/returns.js").ReturnsMap =
|
|
70
|
-
import("./types/returns.js").ReturnsMap,
|
|
71
|
-
>(options: OnErrorOptions<TResponses> | OnErrorOptions<TResponses>["handle"]): this {
|
|
72
|
-
this.state.onError = toOnErrorHandler(options);
|
|
73
|
-
return this;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
notFound(handler: (ctx: unknown) => Response | Promise<Response> | unknown): this {
|
|
77
|
-
this.state.notFound = (ctx) => handler(ctx);
|
|
78
|
-
return this;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
get: CreateWithoutBodyRoute<"GET", TAppContext> = createGetRoute as CreateWithoutBodyRoute<
|
|
82
|
-
"GET",
|
|
83
|
-
TAppContext
|
|
84
|
-
>;
|
|
85
|
-
post: CreateWithBodyRoute<"POST", TAppContext> = createPostRoute as CreateWithBodyRoute<
|
|
86
|
-
"POST",
|
|
87
|
-
TAppContext
|
|
88
|
-
>;
|
|
89
|
-
put: CreateWithBodyRoute<"PUT", TAppContext> = createPutRoute as CreateWithBodyRoute<
|
|
90
|
-
"PUT",
|
|
91
|
-
TAppContext
|
|
92
|
-
>;
|
|
93
|
-
patch: CreateWithBodyRoute<"PATCH", TAppContext> = createPatchRoute as CreateWithBodyRoute<
|
|
94
|
-
"PATCH",
|
|
95
|
-
TAppContext
|
|
96
|
-
>;
|
|
97
|
-
delete: CreateWithoutBodyRoute<"DELETE", TAppContext> =
|
|
98
|
-
createDeleteRoute as CreateWithoutBodyRoute<"DELETE", TAppContext>;
|
|
99
|
-
options: CreateWithoutBodyRoute<"OPTIONS", TAppContext> =
|
|
100
|
-
createOptionsRoute as CreateWithoutBodyRoute<"OPTIONS", TAppContext>;
|
|
101
|
-
head: CreateWithoutBodyRoute<"HEAD", TAppContext> = createHeadRoute as CreateWithoutBodyRoute<
|
|
102
|
-
"HEAD",
|
|
103
|
-
TAppContext
|
|
104
|
-
>;
|
|
105
|
-
any: CreateAnyRoute<TAppContext> = createAnyRoute as CreateAnyRoute<TAppContext>;
|
|
106
|
-
all: CreateAllRoute<TAppContext> = createAllRoute as CreateAllRoute<TAppContext>;
|
|
107
|
-
|
|
108
|
-
middleware<const Layout extends LayoutId>(
|
|
109
|
-
layout: Layout,
|
|
110
|
-
): MiddlewareBuilder<Layout, readonly [], TAppContext> {
|
|
111
|
-
return createMiddleware(layout) as MiddlewareBuilder<Layout, readonly [], TAppContext>;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
defineHandler<TQuery = unknown, TParams = unknown, TBody = unknown>(options?: {
|
|
115
|
-
query?: Schema<TQuery>;
|
|
116
|
-
params?: Schema<TParams>;
|
|
117
|
-
body?: Schema<TBody>;
|
|
118
|
-
}): HandlerBuilder<
|
|
119
|
-
readonly [],
|
|
120
|
-
{ query: TQuery; params: TParams; body: TBody },
|
|
121
|
-
{},
|
|
122
|
-
TAppContext
|
|
123
|
-
> {
|
|
124
|
-
return options === undefined
|
|
125
|
-
? defineHandler<TAppContext>()
|
|
126
|
-
: defineHandler<TAppContext>(options);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
create<const TManifest extends RouteManifestShape>(manifest: TManifest): TaserApp<TManifest> {
|
|
130
|
-
const definition = this.state.contextDef;
|
|
131
|
-
const validateResponse = this.state.options.response?.validate ?? true;
|
|
132
|
-
const onValidationFailure = this.state.options.response?.onValidationFailure;
|
|
133
|
-
const basePath = this.state.options.basePath;
|
|
134
|
-
|
|
135
|
-
const hasCustomContext = definition.boot !== undefined || definition.request !== undefined;
|
|
136
|
-
const bootPromise = hasCustomContext
|
|
137
|
-
? Promise.resolve(definition.boot?.() ?? ({} as Record<string, unknown>))
|
|
138
|
-
: undefined;
|
|
139
|
-
|
|
140
|
-
const contextFactory = hasCustomContext
|
|
141
|
-
? async ({ native }: { native?: unknown } = {}) => {
|
|
142
|
-
const bootContext = (await bootPromise) ?? {};
|
|
143
|
-
const requestContext = (await definition.request?.({ native: native as never })) ?? {};
|
|
144
|
-
return { ...bootContext, ...requestContext };
|
|
145
|
-
}
|
|
146
|
-
: () => ({});
|
|
147
|
-
|
|
148
|
-
const runtime = createTaserRuntime(manifest, contextFactory, {
|
|
149
|
-
...(basePath !== undefined ? { basePath } : {}),
|
|
150
|
-
response: {
|
|
151
|
-
validate: validateResponse,
|
|
152
|
-
...(onValidationFailure !== undefined ? { onValidationFailure } : {}),
|
|
153
|
-
},
|
|
154
|
-
...(this.state.onError !== undefined ? { onError: this.state.onError } : {}),
|
|
155
|
-
...(this.state.notFound !== undefined ? { notFound: this.state.notFound } : {}),
|
|
156
|
-
...(this.state.options.cookies !== undefined ? { cookies: this.state.options.cookies } : {}),
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
return new TaserApp(runtime, manifest);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
export function createTaserApp(options: CreateTaserAppOptions = {}): TaserRouter {
|
|
164
|
-
return new TaserRouter(options);
|
|
165
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|