@ts-kizuna/fastify 1.49.7 → 1.51.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.cjs +10 -41
- package/dist/index.d.cts +12 -121
- package/dist/index.d.mts +12 -121
- package/dist/index.mjs +11 -42
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -127,21 +127,15 @@ const fastifyKizuna = (0, fastify_plugin.default)(async (app, options) => {
|
|
|
127
127
|
for (const [routeKey, route] of Object.entries(routes)) mountRoute(routeKey, route, routes, router);
|
|
128
128
|
}
|
|
129
129
|
}, { name: "@ts-kizuna/fastify" });
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
const api =
|
|
139
|
-
jobs: contract.jobs,
|
|
140
|
-
handlers: jobs ?? {},
|
|
141
|
-
config: contract.jobsConfig,
|
|
142
|
-
transport: options?.jobTransport,
|
|
143
|
-
onError: options?.onJobError
|
|
144
|
-
} : void 0 });
|
|
130
|
+
/**
|
|
131
|
+
* Turn a contract into a server handle: the serving counterpart to `Kizuna`.
|
|
132
|
+
* Keep the instance and use `server.guard` to define guards, `server.router`
|
|
133
|
+
* to write typed handlers, and `server.api` to assemble them.
|
|
134
|
+
*/
|
|
135
|
+
var KizunaServer = class {
|
|
136
|
+
constructor(contract, options) {
|
|
137
|
+
Object.assign(this, (0, _ts_kizuna_core_adapter.createServerSurface)(contract, options, (assembled) => {
|
|
138
|
+
const api = assembled;
|
|
145
139
|
const plugin = (0, fastify_plugin.default)(async (app, pluginOptions) => {
|
|
146
140
|
await fastifyKizuna(app, {
|
|
147
141
|
...pluginOptions,
|
|
@@ -154,32 +148,7 @@ const createServerSurface = (contract, options) => {
|
|
|
154
148
|
await app.register(plugin, mountOptions ?? {});
|
|
155
149
|
}
|
|
156
150
|
});
|
|
157
|
-
}
|
|
158
|
-
};
|
|
159
|
-
};
|
|
160
|
-
/**
|
|
161
|
-
* Turn a contract into a server handle: the serving counterpart to `Kizuna`.
|
|
162
|
-
* Keep the instance and use `server.guard` to define guards, `server.router`
|
|
163
|
-
* to write typed handlers, and `server.api` to assemble them.
|
|
164
|
-
*
|
|
165
|
-
* @example
|
|
166
|
-
* const server = new KizunaServer(contract);
|
|
167
|
-
*
|
|
168
|
-
* const requireUser = server.guard('user', ({ bearer, deny }) => {
|
|
169
|
-
* const session = bearer && sessions.get(bearer.token);
|
|
170
|
-
* return session ? { userId: session.userId } : deny(401, 'Unauthorized');
|
|
171
|
-
* });
|
|
172
|
-
*
|
|
173
|
-
* export const api = server.api({
|
|
174
|
-
* router,
|
|
175
|
-
* guards: {
|
|
176
|
-
* user: requireUser,
|
|
177
|
-
* },
|
|
178
|
-
* });
|
|
179
|
-
*/
|
|
180
|
-
var KizunaServer = class {
|
|
181
|
-
constructor(contract, options) {
|
|
182
|
-
Object.assign(this, createServerSurface(contract, options));
|
|
151
|
+
}));
|
|
183
152
|
}
|
|
184
153
|
};
|
|
185
154
|
//#endregion
|
package/dist/index.d.cts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { FastifyInstance, FastifyPluginAsync, FastifyReply, FastifyRequest } from "fastify";
|
|
2
|
-
import { ApiWithRouter,
|
|
3
|
-
import {
|
|
4
|
-
import { Contract, CredentialOf, GuardSuccess, JobHandlers, JobsArg, RequestContextHeaderValues, RequestContextSchema, SecurityScheme, TagOptions } from "@ts-kizuna/core";
|
|
2
|
+
import { ApiWithRouter, ContractJobsRouter, ContractRouter, ErrorFormatter, GUARDS_META, JOBS_META, REQUEST_CONTEXT_META, RouteDefinition, RouteHandler as RouteHandler$1, Routes, SCHEMES_META, Server as Server$1, ServerOptions } from "@ts-kizuna/core/adapter";
|
|
3
|
+
import { Contract, RoutesOf } from "@ts-kizuna/core";
|
|
5
4
|
|
|
6
5
|
//#region src/server.d.ts
|
|
7
6
|
type FastifyApi<R extends Routes = Routes> = ApiWithRouter<R> & {
|
|
@@ -33,18 +32,12 @@ type RouteHandler<R extends RouteDefinition> = RouteHandler$1<R, FastifyHandlerC
|
|
|
33
32
|
* secured by the contract's `auth` map additionally receive each required
|
|
34
33
|
* identity's context in their handler args, under `auth`, keyed by the identity's name.
|
|
35
34
|
*/
|
|
36
|
-
type Router<C> =
|
|
35
|
+
type Router<C> = ContractRouter<C, FastifyHandlerContext>;
|
|
37
36
|
/**
|
|
38
37
|
* The handler for each of a contract's scheduled jobs, typed against it. Each
|
|
39
38
|
* receives only the job's `input`, so the same handler can be run in process.
|
|
40
39
|
*/
|
|
41
|
-
type JobsRouter<C> = C
|
|
42
|
-
/**
|
|
43
|
-
* The handlers for a group named on the contract, or for a bare route group.
|
|
44
|
-
* Both forms resolve through one signature: a second candidate of the same
|
|
45
|
-
* arity costs zero-argument handlers their contextual type.
|
|
46
|
-
*/
|
|
47
|
-
type GroupRouter<Source, GroupOrRoutes> = GroupOrRoutes extends string ? Router<Source>[Extract<GroupOrRoutes, keyof Router<Source>>] : Router<GroupOrRoutes>;
|
|
40
|
+
type JobsRouter<C> = ContractJobsRouter<C>;
|
|
48
41
|
declare module 'fastify' {
|
|
49
42
|
interface FastifyRequest {
|
|
50
43
|
kizunaRoute?: RouteDefinition;
|
|
@@ -66,32 +59,6 @@ interface FastifyOptions {
|
|
|
66
59
|
*/
|
|
67
60
|
formatError?: ErrorFormatter<FastifyRequest>;
|
|
68
61
|
}
|
|
69
|
-
/**
|
|
70
|
-
* A guard per identity, keyed by name. Each receives the handler context, a
|
|
71
|
-
* `deny` helper, and the matched route's required scopes, and returns that
|
|
72
|
-
* identity's {@link GuardSuccess} (its context and access fields) or a `deny(...)`
|
|
73
|
-
* result. Keying by name lets each guard's return be typed against its own
|
|
74
|
-
* identity, so access values narrow without an annotation. An
|
|
75
|
-
* authentication-only identity (no context, no access) returns nothing on
|
|
76
|
-
* success, or `deny(...)`.
|
|
77
|
-
*/
|
|
78
|
-
type GuardFns<Schemes extends Record<string, SecurityScheme>, Params> = { [Name in keyof Schemes]: (args: FastifyHandlerContext & CredentialOf<Schemes[Name]> & {
|
|
79
|
-
params: Params;
|
|
80
|
-
deny: GuardDeny;
|
|
81
|
-
scopes: string[];
|
|
82
|
-
}) => [keyof GuardSuccess<Schemes[Name]>] extends [never] ? void | GuardDenial | Promise<void | GuardDenial> : GuardSuccess<Schemes[Name]> | GuardDenial | Promise<GuardSuccess<Schemes[Name]> | GuardDenial> };
|
|
83
|
-
/**
|
|
84
|
-
* One guard per identity declared on the contract.
|
|
85
|
-
*/
|
|
86
|
-
type GuardsForSchemes<Schemes extends Record<string, SecurityScheme>> = { [Name in keyof Schemes]: GuardRun<FastifyHandlerContext> };
|
|
87
|
-
/**
|
|
88
|
-
* The resolver functions for the request context schemas declared on `kizuna`,
|
|
89
|
-
* keyed by name. Each runs on every route and returns its schema's value.
|
|
90
|
-
*/
|
|
91
|
-
type RequestResolverFns<RequestContext extends Record<string, RequestContextSchema>> = { [Name in keyof RequestContext]: (args: FastifyHandlerContext & {
|
|
92
|
-
params: Record<string, string>;
|
|
93
|
-
headers: RequestContextHeaderValues<RequestContext[Name]>;
|
|
94
|
-
}) => z.output<RequestContext[Name]['context']> | Promise<z.output<RequestContext[Name]['context']>> };
|
|
95
62
|
interface KizunaPluginOptions extends FastifyOptions {
|
|
96
63
|
/**
|
|
97
64
|
* The API object built by `server.api`.
|
|
@@ -106,95 +73,19 @@ interface KizunaPluginOptions extends FastifyOptions {
|
|
|
106
73
|
* await api.mount(app);
|
|
107
74
|
*/
|
|
108
75
|
declare const fastifyKizuna: (app: FastifyInstance, options: KizunaPluginOptions) => Promise<void>;
|
|
109
|
-
|
|
110
|
-
interface Server<R extends Routes, Schemes extends Record<string, SecurityScheme>, Auth, RequestContext extends Record<string, RequestContextSchema>, Plugins extends ContractPlugins, J extends Jobs = Jobs> {
|
|
111
|
-
/**
|
|
112
|
-
* Define a guard for one of the contract's identities. It runs before the
|
|
113
|
-
* handlers of every route whose `auth` entry requires the identity, and
|
|
114
|
-
* receives the credential its method extracted (`bearer`, `apiKey`, or
|
|
115
|
-
* `basic`, `null` when absent). Return the identity's context and access
|
|
116
|
-
* fields to allow the request, or call `deny(status, detail)`.
|
|
117
|
-
*/
|
|
118
|
-
guard<const Name extends Extract<keyof Schemes, string>>(name: Name, run: GuardFns<Schemes, GuardParams<R, Auth, Name>>[Name]): GuardRun<FastifyHandlerContext>;
|
|
119
|
-
/**
|
|
120
|
-
* Define a request context resolver declared on the contract. It runs on
|
|
121
|
-
* every route, public ones included, and never denies.
|
|
122
|
-
*/
|
|
123
|
-
requestContext<const Name extends Extract<keyof RequestContext, string>>(name: Name, run: RequestResolverFns<RequestContext>[Name]): RequestContextRun<FastifyHandlerContext>;
|
|
124
|
-
/**
|
|
125
|
-
* Write typed handlers for the contract or one of its route groups.
|
|
126
|
-
*/
|
|
127
|
-
router: {
|
|
128
|
-
<const GroupOrRoutes extends Extract<keyof Router<ServerContract<R, Schemes, Auth, RequestContext, Plugins, J>>, string> | Routes>(group: GroupOrRoutes, router: GroupRouter<ServerContract<R, Schemes, Auth, RequestContext, Plugins, J>, GroupOrRoutes>): GroupRouter<ServerContract<R, Schemes, Auth, RequestContext, Plugins, J>, GroupOrRoutes>;
|
|
129
|
-
(router: Router<ServerContract<R, Schemes, Auth, RequestContext, Plugins, J>>): Router<ServerContract<R, Schemes, Auth, RequestContext, Plugins, J>>;
|
|
130
|
-
};
|
|
131
|
-
/**
|
|
132
|
-
* Write a handler for each of the contract's jobs.
|
|
133
|
-
*
|
|
134
|
-
* Pass a `transport` to say where a queued job goes. Without one, `queue`
|
|
135
|
-
* runs the job in this process and it is lost on a crash.
|
|
136
|
-
*
|
|
137
|
-
* @example
|
|
138
|
-
* export const jobs = server.jobs({
|
|
139
|
-
* sendDigests: async () => ({
|
|
140
|
-
* status: 200,
|
|
141
|
-
* body: {
|
|
142
|
-
* sent: await sendPendingDigests(),
|
|
143
|
-
* },
|
|
144
|
-
* }),
|
|
145
|
-
* });
|
|
146
|
-
*/
|
|
147
|
-
jobs(handlers: JobsRouter<ServerContract<R, Schemes, Auth, RequestContext, Plugins, J>>): JobsRouter<ServerContract<R, Schemes, Auth, RequestContext, Plugins, J>>;
|
|
148
|
-
/**
|
|
149
|
-
* Assemble the router, guards, and job handlers into the api object.
|
|
150
|
-
*/
|
|
151
|
-
api(options: {
|
|
152
|
-
router: Router<ServerContract<R, Schemes, Auth, RequestContext, Plugins, J>>;
|
|
153
|
-
} & (string extends keyof Schemes ? {
|
|
154
|
-
guards?: undefined;
|
|
155
|
-
} : {
|
|
156
|
-
guards: NoInfer<GuardsForSchemes<Schemes>>;
|
|
157
|
-
}) & (string extends keyof J ? {
|
|
158
|
-
jobs?: undefined;
|
|
159
|
-
} : {
|
|
160
|
-
jobs: NoInfer<JobsRouter<ServerContract<R, Schemes, Auth, RequestContext, Plugins, J>>>;
|
|
161
|
-
}) & (string extends keyof RequestContext ? {
|
|
162
|
-
requestContext?: undefined;
|
|
163
|
-
} : {
|
|
164
|
-
requestContext: NoInfer<{ [Name in keyof RequestContext]: RequestContextRun<FastifyHandlerContext> }>;
|
|
165
|
-
}) & (string extends keyof Plugins ? {
|
|
166
|
-
plugins?: undefined;
|
|
167
|
-
} : {
|
|
168
|
-
plugins: PluginImplementations<Plugins, FastifyHandlerContext>;
|
|
169
|
-
})): FastifyApi<R>;
|
|
170
|
-
}
|
|
76
|
+
interface Server<C extends Contract> extends Server$1<C, FastifyHandlerContext, FastifyApi<RoutesOf<C>>> {}
|
|
171
77
|
/**
|
|
172
78
|
* Turn a contract into a server handle: the serving counterpart to `Kizuna`.
|
|
173
79
|
* Keep the instance and use `server.guard` to define guards, `server.router`
|
|
174
80
|
* to write typed handlers, and `server.api` to assemble them.
|
|
175
|
-
*
|
|
176
|
-
* @example
|
|
177
|
-
* const server = new KizunaServer(contract);
|
|
178
|
-
*
|
|
179
|
-
* const requireUser = server.guard('user', ({ bearer, deny }) => {
|
|
180
|
-
* const session = bearer && sessions.get(bearer.token);
|
|
181
|
-
* return session ? { userId: session.userId } : deny(401, 'Unauthorized');
|
|
182
|
-
* });
|
|
183
|
-
*
|
|
184
|
-
* export const api = server.api({
|
|
185
|
-
* router,
|
|
186
|
-
* guards: {
|
|
187
|
-
* user: requireUser,
|
|
188
|
-
* },
|
|
189
|
-
* });
|
|
190
81
|
*/
|
|
191
|
-
declare class KizunaServer<
|
|
192
|
-
readonly guard: Server<
|
|
193
|
-
readonly requestContext: Server<
|
|
194
|
-
readonly router: Server<
|
|
195
|
-
readonly jobs: Server<
|
|
196
|
-
readonly api: Server<
|
|
197
|
-
constructor(contract:
|
|
82
|
+
declare class KizunaServer<C extends Contract> implements Server<C> {
|
|
83
|
+
readonly guard: Server<C>['guard'];
|
|
84
|
+
readonly requestContext: Server<C>['requestContext'];
|
|
85
|
+
readonly router: Server<C>['router'];
|
|
86
|
+
readonly jobs: Server<C>['jobs'];
|
|
87
|
+
readonly api: Server<C>['api'];
|
|
88
|
+
constructor(contract: C, options?: ServerOptions);
|
|
198
89
|
}
|
|
199
90
|
//#endregion
|
|
200
91
|
export { FastifyApi, FastifyHandlerContext, FastifyOptions, FastifyPreHandler, JobsRouter, KizunaPluginOptions, KizunaServer, RouteHandler, Router, Server, fastifyKizuna };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { ApiWithRouter,
|
|
1
|
+
import { ApiWithRouter, ContractJobsRouter, ContractRouter, ErrorFormatter, GUARDS_META, JOBS_META, REQUEST_CONTEXT_META, RouteDefinition, RouteHandler as RouteHandler$1, Routes, SCHEMES_META, Server as Server$1, ServerOptions } from "@ts-kizuna/core/adapter";
|
|
2
2
|
import { FastifyInstance, FastifyPluginAsync, FastifyReply, FastifyRequest } from "fastify";
|
|
3
|
-
import {
|
|
4
|
-
import { Contract, CredentialOf, GuardSuccess, JobHandlers, JobsArg, RequestContextHeaderValues, RequestContextSchema, SecurityScheme, TagOptions } from "@ts-kizuna/core";
|
|
3
|
+
import { Contract, RoutesOf } from "@ts-kizuna/core";
|
|
5
4
|
|
|
6
5
|
//#region src/server.d.ts
|
|
7
6
|
type FastifyApi<R extends Routes = Routes> = ApiWithRouter<R> & {
|
|
@@ -33,18 +32,12 @@ type RouteHandler<R extends RouteDefinition> = RouteHandler$1<R, FastifyHandlerC
|
|
|
33
32
|
* secured by the contract's `auth` map additionally receive each required
|
|
34
33
|
* identity's context in their handler args, under `auth`, keyed by the identity's name.
|
|
35
34
|
*/
|
|
36
|
-
type Router<C> =
|
|
35
|
+
type Router<C> = ContractRouter<C, FastifyHandlerContext>;
|
|
37
36
|
/**
|
|
38
37
|
* The handler for each of a contract's scheduled jobs, typed against it. Each
|
|
39
38
|
* receives only the job's `input`, so the same handler can be run in process.
|
|
40
39
|
*/
|
|
41
|
-
type JobsRouter<C> = C
|
|
42
|
-
/**
|
|
43
|
-
* The handlers for a group named on the contract, or for a bare route group.
|
|
44
|
-
* Both forms resolve through one signature: a second candidate of the same
|
|
45
|
-
* arity costs zero-argument handlers their contextual type.
|
|
46
|
-
*/
|
|
47
|
-
type GroupRouter<Source, GroupOrRoutes> = GroupOrRoutes extends string ? Router<Source>[Extract<GroupOrRoutes, keyof Router<Source>>] : Router<GroupOrRoutes>;
|
|
40
|
+
type JobsRouter<C> = ContractJobsRouter<C>;
|
|
48
41
|
declare module 'fastify' {
|
|
49
42
|
interface FastifyRequest {
|
|
50
43
|
kizunaRoute?: RouteDefinition;
|
|
@@ -66,32 +59,6 @@ interface FastifyOptions {
|
|
|
66
59
|
*/
|
|
67
60
|
formatError?: ErrorFormatter<FastifyRequest>;
|
|
68
61
|
}
|
|
69
|
-
/**
|
|
70
|
-
* A guard per identity, keyed by name. Each receives the handler context, a
|
|
71
|
-
* `deny` helper, and the matched route's required scopes, and returns that
|
|
72
|
-
* identity's {@link GuardSuccess} (its context and access fields) or a `deny(...)`
|
|
73
|
-
* result. Keying by name lets each guard's return be typed against its own
|
|
74
|
-
* identity, so access values narrow without an annotation. An
|
|
75
|
-
* authentication-only identity (no context, no access) returns nothing on
|
|
76
|
-
* success, or `deny(...)`.
|
|
77
|
-
*/
|
|
78
|
-
type GuardFns<Schemes extends Record<string, SecurityScheme>, Params> = { [Name in keyof Schemes]: (args: FastifyHandlerContext & CredentialOf<Schemes[Name]> & {
|
|
79
|
-
params: Params;
|
|
80
|
-
deny: GuardDeny;
|
|
81
|
-
scopes: string[];
|
|
82
|
-
}) => [keyof GuardSuccess<Schemes[Name]>] extends [never] ? void | GuardDenial | Promise<void | GuardDenial> : GuardSuccess<Schemes[Name]> | GuardDenial | Promise<GuardSuccess<Schemes[Name]> | GuardDenial> };
|
|
83
|
-
/**
|
|
84
|
-
* One guard per identity declared on the contract.
|
|
85
|
-
*/
|
|
86
|
-
type GuardsForSchemes<Schemes extends Record<string, SecurityScheme>> = { [Name in keyof Schemes]: GuardRun<FastifyHandlerContext> };
|
|
87
|
-
/**
|
|
88
|
-
* The resolver functions for the request context schemas declared on `kizuna`,
|
|
89
|
-
* keyed by name. Each runs on every route and returns its schema's value.
|
|
90
|
-
*/
|
|
91
|
-
type RequestResolverFns<RequestContext extends Record<string, RequestContextSchema>> = { [Name in keyof RequestContext]: (args: FastifyHandlerContext & {
|
|
92
|
-
params: Record<string, string>;
|
|
93
|
-
headers: RequestContextHeaderValues<RequestContext[Name]>;
|
|
94
|
-
}) => z.output<RequestContext[Name]['context']> | Promise<z.output<RequestContext[Name]['context']>> };
|
|
95
62
|
interface KizunaPluginOptions extends FastifyOptions {
|
|
96
63
|
/**
|
|
97
64
|
* The API object built by `server.api`.
|
|
@@ -106,95 +73,19 @@ interface KizunaPluginOptions extends FastifyOptions {
|
|
|
106
73
|
* await api.mount(app);
|
|
107
74
|
*/
|
|
108
75
|
declare const fastifyKizuna: (app: FastifyInstance, options: KizunaPluginOptions) => Promise<void>;
|
|
109
|
-
|
|
110
|
-
interface Server<R extends Routes, Schemes extends Record<string, SecurityScheme>, Auth, RequestContext extends Record<string, RequestContextSchema>, Plugins extends ContractPlugins, J extends Jobs = Jobs> {
|
|
111
|
-
/**
|
|
112
|
-
* Define a guard for one of the contract's identities. It runs before the
|
|
113
|
-
* handlers of every route whose `auth` entry requires the identity, and
|
|
114
|
-
* receives the credential its method extracted (`bearer`, `apiKey`, or
|
|
115
|
-
* `basic`, `null` when absent). Return the identity's context and access
|
|
116
|
-
* fields to allow the request, or call `deny(status, detail)`.
|
|
117
|
-
*/
|
|
118
|
-
guard<const Name extends Extract<keyof Schemes, string>>(name: Name, run: GuardFns<Schemes, GuardParams<R, Auth, Name>>[Name]): GuardRun<FastifyHandlerContext>;
|
|
119
|
-
/**
|
|
120
|
-
* Define a request context resolver declared on the contract. It runs on
|
|
121
|
-
* every route, public ones included, and never denies.
|
|
122
|
-
*/
|
|
123
|
-
requestContext<const Name extends Extract<keyof RequestContext, string>>(name: Name, run: RequestResolverFns<RequestContext>[Name]): RequestContextRun<FastifyHandlerContext>;
|
|
124
|
-
/**
|
|
125
|
-
* Write typed handlers for the contract or one of its route groups.
|
|
126
|
-
*/
|
|
127
|
-
router: {
|
|
128
|
-
<const GroupOrRoutes extends Extract<keyof Router<ServerContract<R, Schemes, Auth, RequestContext, Plugins, J>>, string> | Routes>(group: GroupOrRoutes, router: GroupRouter<ServerContract<R, Schemes, Auth, RequestContext, Plugins, J>, GroupOrRoutes>): GroupRouter<ServerContract<R, Schemes, Auth, RequestContext, Plugins, J>, GroupOrRoutes>;
|
|
129
|
-
(router: Router<ServerContract<R, Schemes, Auth, RequestContext, Plugins, J>>): Router<ServerContract<R, Schemes, Auth, RequestContext, Plugins, J>>;
|
|
130
|
-
};
|
|
131
|
-
/**
|
|
132
|
-
* Write a handler for each of the contract's jobs.
|
|
133
|
-
*
|
|
134
|
-
* Pass a `transport` to say where a queued job goes. Without one, `queue`
|
|
135
|
-
* runs the job in this process and it is lost on a crash.
|
|
136
|
-
*
|
|
137
|
-
* @example
|
|
138
|
-
* export const jobs = server.jobs({
|
|
139
|
-
* sendDigests: async () => ({
|
|
140
|
-
* status: 200,
|
|
141
|
-
* body: {
|
|
142
|
-
* sent: await sendPendingDigests(),
|
|
143
|
-
* },
|
|
144
|
-
* }),
|
|
145
|
-
* });
|
|
146
|
-
*/
|
|
147
|
-
jobs(handlers: JobsRouter<ServerContract<R, Schemes, Auth, RequestContext, Plugins, J>>): JobsRouter<ServerContract<R, Schemes, Auth, RequestContext, Plugins, J>>;
|
|
148
|
-
/**
|
|
149
|
-
* Assemble the router, guards, and job handlers into the api object.
|
|
150
|
-
*/
|
|
151
|
-
api(options: {
|
|
152
|
-
router: Router<ServerContract<R, Schemes, Auth, RequestContext, Plugins, J>>;
|
|
153
|
-
} & (string extends keyof Schemes ? {
|
|
154
|
-
guards?: undefined;
|
|
155
|
-
} : {
|
|
156
|
-
guards: NoInfer<GuardsForSchemes<Schemes>>;
|
|
157
|
-
}) & (string extends keyof J ? {
|
|
158
|
-
jobs?: undefined;
|
|
159
|
-
} : {
|
|
160
|
-
jobs: NoInfer<JobsRouter<ServerContract<R, Schemes, Auth, RequestContext, Plugins, J>>>;
|
|
161
|
-
}) & (string extends keyof RequestContext ? {
|
|
162
|
-
requestContext?: undefined;
|
|
163
|
-
} : {
|
|
164
|
-
requestContext: NoInfer<{ [Name in keyof RequestContext]: RequestContextRun<FastifyHandlerContext> }>;
|
|
165
|
-
}) & (string extends keyof Plugins ? {
|
|
166
|
-
plugins?: undefined;
|
|
167
|
-
} : {
|
|
168
|
-
plugins: PluginImplementations<Plugins, FastifyHandlerContext>;
|
|
169
|
-
})): FastifyApi<R>;
|
|
170
|
-
}
|
|
76
|
+
interface Server<C extends Contract> extends Server$1<C, FastifyHandlerContext, FastifyApi<RoutesOf<C>>> {}
|
|
171
77
|
/**
|
|
172
78
|
* Turn a contract into a server handle: the serving counterpart to `Kizuna`.
|
|
173
79
|
* Keep the instance and use `server.guard` to define guards, `server.router`
|
|
174
80
|
* to write typed handlers, and `server.api` to assemble them.
|
|
175
|
-
*
|
|
176
|
-
* @example
|
|
177
|
-
* const server = new KizunaServer(contract);
|
|
178
|
-
*
|
|
179
|
-
* const requireUser = server.guard('user', ({ bearer, deny }) => {
|
|
180
|
-
* const session = bearer && sessions.get(bearer.token);
|
|
181
|
-
* return session ? { userId: session.userId } : deny(401, 'Unauthorized');
|
|
182
|
-
* });
|
|
183
|
-
*
|
|
184
|
-
* export const api = server.api({
|
|
185
|
-
* router,
|
|
186
|
-
* guards: {
|
|
187
|
-
* user: requireUser,
|
|
188
|
-
* },
|
|
189
|
-
* });
|
|
190
81
|
*/
|
|
191
|
-
declare class KizunaServer<
|
|
192
|
-
readonly guard: Server<
|
|
193
|
-
readonly requestContext: Server<
|
|
194
|
-
readonly router: Server<
|
|
195
|
-
readonly jobs: Server<
|
|
196
|
-
readonly api: Server<
|
|
197
|
-
constructor(contract:
|
|
82
|
+
declare class KizunaServer<C extends Contract> implements Server<C> {
|
|
83
|
+
readonly guard: Server<C>['guard'];
|
|
84
|
+
readonly requestContext: Server<C>['requestContext'];
|
|
85
|
+
readonly router: Server<C>['router'];
|
|
86
|
+
readonly jobs: Server<C>['jobs'];
|
|
87
|
+
readonly api: Server<C>['api'];
|
|
88
|
+
constructor(contract: C, options?: ServerOptions);
|
|
198
89
|
}
|
|
199
90
|
//#endregion
|
|
200
91
|
export { FastifyApi, FastifyHandlerContext, FastifyOptions, FastifyPreHandler, JobsRouter, KizunaPluginOptions, KizunaServer, RouteHandler, Router, Server, fastifyKizuna };
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import fastifyPlugin from "fastify-plugin";
|
|
2
2
|
import { Readable } from "node:stream";
|
|
3
|
-
import { GUARDS_META, JOBS_META, REQUEST_CONTEXT_META, ROUTER_META, SCHEMES_META,
|
|
3
|
+
import { GUARDS_META, JOBS_META, REQUEST_CONTEXT_META, ROUTER_META, SCHEMES_META, createAdapter, createServerSurface, jobRouter, jobRoutes, jobRunnerFrom, pluginExportsOf, pluginRouterOf, pluginRoutesOf, renderJsonResult } from "@ts-kizuna/core/adapter";
|
|
4
4
|
//#region src/server.ts
|
|
5
5
|
/**
|
|
6
6
|
* Write a web `Response` to a Fastify reply. Plugins answer in web terms to stay
|
|
@@ -103,21 +103,15 @@ const fastifyKizuna = fastifyPlugin(async (app, options) => {
|
|
|
103
103
|
for (const [routeKey, route] of Object.entries(routes)) mountRoute(routeKey, route, routes, router);
|
|
104
104
|
}
|
|
105
105
|
}, { name: "@ts-kizuna/fastify" });
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
const api =
|
|
115
|
-
jobs: contract.jobs,
|
|
116
|
-
handlers: jobs ?? {},
|
|
117
|
-
config: contract.jobsConfig,
|
|
118
|
-
transport: options?.jobTransport,
|
|
119
|
-
onError: options?.onJobError
|
|
120
|
-
} : void 0 });
|
|
106
|
+
/**
|
|
107
|
+
* Turn a contract into a server handle: the serving counterpart to `Kizuna`.
|
|
108
|
+
* Keep the instance and use `server.guard` to define guards, `server.router`
|
|
109
|
+
* to write typed handlers, and `server.api` to assemble them.
|
|
110
|
+
*/
|
|
111
|
+
var KizunaServer = class {
|
|
112
|
+
constructor(contract, options) {
|
|
113
|
+
Object.assign(this, createServerSurface(contract, options, (assembled) => {
|
|
114
|
+
const api = assembled;
|
|
121
115
|
const plugin = fastifyPlugin(async (app, pluginOptions) => {
|
|
122
116
|
await fastifyKizuna(app, {
|
|
123
117
|
...pluginOptions,
|
|
@@ -130,32 +124,7 @@ const createServerSurface = (contract, options) => {
|
|
|
130
124
|
await app.register(plugin, mountOptions ?? {});
|
|
131
125
|
}
|
|
132
126
|
});
|
|
133
|
-
}
|
|
134
|
-
};
|
|
135
|
-
};
|
|
136
|
-
/**
|
|
137
|
-
* Turn a contract into a server handle: the serving counterpart to `Kizuna`.
|
|
138
|
-
* Keep the instance and use `server.guard` to define guards, `server.router`
|
|
139
|
-
* to write typed handlers, and `server.api` to assemble them.
|
|
140
|
-
*
|
|
141
|
-
* @example
|
|
142
|
-
* const server = new KizunaServer(contract);
|
|
143
|
-
*
|
|
144
|
-
* const requireUser = server.guard('user', ({ bearer, deny }) => {
|
|
145
|
-
* const session = bearer && sessions.get(bearer.token);
|
|
146
|
-
* return session ? { userId: session.userId } : deny(401, 'Unauthorized');
|
|
147
|
-
* });
|
|
148
|
-
*
|
|
149
|
-
* export const api = server.api({
|
|
150
|
-
* router,
|
|
151
|
-
* guards: {
|
|
152
|
-
* user: requireUser,
|
|
153
|
-
* },
|
|
154
|
-
* });
|
|
155
|
-
*/
|
|
156
|
-
var KizunaServer = class {
|
|
157
|
-
constructor(contract, options) {
|
|
158
|
-
Object.assign(this, createServerSurface(contract, options));
|
|
127
|
+
}));
|
|
159
128
|
}
|
|
160
129
|
};
|
|
161
130
|
//#endregion
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ts-kizuna/fastify",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.51.0",
|
|
4
4
|
"description": "Fastify adapter for ts-kizuna",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ts-kizuna",
|
|
@@ -60,14 +60,14 @@
|
|
|
60
60
|
"peerDependencies": {
|
|
61
61
|
"fastify": "^5.0.0",
|
|
62
62
|
"zod": "^4.0.0",
|
|
63
|
-
"@ts-kizuna/core": "1.
|
|
63
|
+
"@ts-kizuna/core": "1.51.0"
|
|
64
64
|
},
|
|
65
65
|
"devDependencies": {
|
|
66
66
|
"fastify": "^5.0.0",
|
|
67
67
|
"tsdown": "^0.21.0",
|
|
68
|
-
"typescript": "^
|
|
68
|
+
"typescript": "^6.0.3",
|
|
69
69
|
"zod": "^4.0.0",
|
|
70
|
-
"@ts-kizuna/core": "1.
|
|
70
|
+
"@ts-kizuna/core": "1.51.0"
|
|
71
71
|
},
|
|
72
72
|
"scripts": {
|
|
73
73
|
"build": "tsdown src/index.ts --format esm,cjs --dts --clean --external @ts-kizuna/core --external fastify --external fastify-plugin --external zod",
|