@devmoods/express-extras 0.55.0 → 0.56.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/channels.d.ts +4 -3
- package/dist/channels.js +1 -0
- package/dist/router.d.ts +25 -4
- package/dist/router.js +10 -11
- package/package.json +1 -1
package/dist/channels.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { type Request
|
|
1
|
+
import { type Request } from 'express';
|
|
2
2
|
import { type RedisClient } from './redis.js';
|
|
3
|
+
import { type ExtractParams, type TypedRequestHandler } from './router.js';
|
|
3
4
|
export declare function createEventStream<Channels extends string>(redis: RedisClient): {
|
|
4
|
-
sse: ({ channels, }: {
|
|
5
|
+
sse: <Path extends string>({ channels, }: {
|
|
5
6
|
channels: (req: Request) => string[] | Promise<string[]>;
|
|
6
|
-
}) =>
|
|
7
|
+
}) => TypedRequestHandler<ExtractParams<Path>, any, any, any>;
|
|
7
8
|
publish: (channel: Channels, message: unknown) => Promise<number>;
|
|
8
9
|
};
|
package/dist/channels.js
CHANGED
|
@@ -3,6 +3,7 @@ import { config } from './config.js';
|
|
|
3
3
|
import { requestContext } from './context.js';
|
|
4
4
|
import { getLogger } from './logger.js';
|
|
5
5
|
import {} from './redis.js';
|
|
6
|
+
import {} from './router.js';
|
|
6
7
|
const logger = getLogger();
|
|
7
8
|
export function createEventStream(redis) {
|
|
8
9
|
const keyPrefix = `${config.value.REDIS_KEY_PREFIX}events:`;
|
package/dist/router.d.ts
CHANGED
|
@@ -4,13 +4,34 @@ import { type ValidationSpec } from './validate.js';
|
|
|
4
4
|
type ResponseHeaders = Record<string, string>;
|
|
5
5
|
type HttpResponseCode = number;
|
|
6
6
|
type ResponseWrapper<TResponseBody> = TResponseBody | TResponseBody[] | HttpResponseCode | [HttpResponseCode, TResponseBody] | [HttpResponseCode, TResponseBody, ResponseHeaders];
|
|
7
|
+
type Prettify<T> = {
|
|
8
|
+
[K in keyof T]: T[K];
|
|
9
|
+
} & {};
|
|
10
|
+
export type ExtractParams<Path extends string> = Prettify<Path extends `${string}:${infer Param}/${infer Rest}` ? {
|
|
11
|
+
[K in Param extends `${infer P}?` ? P : Param]: string;
|
|
12
|
+
} & ExtractParams<`/${Rest}`> : Path extends `${string}:${infer Param}` ? {
|
|
13
|
+
[K in Param extends `${infer P}?` ? P : Param]: string;
|
|
14
|
+
} : {}>;
|
|
15
|
+
export type TypedRequestHandler<Params, ResponseBodySchema extends StandardSchemaV1 | undefined, RequestBodySchema extends StandardSchemaV1 | undefined, RequestQuery extends StandardSchemaV1 | undefined> = (req: Request<Params, never, RequestBodySchema extends StandardSchemaV1<any> ? StandardSchemaV1.InferOutput<RequestBodySchema> : unknown, RequestQuery extends StandardSchemaV1<any> ? StandardSchemaV1.InferOutput<RequestQuery> : Record<string, string | string[]>>, res: Response<ResponseBodySchema extends StandardSchemaV1<any> ? StandardSchemaV1.InferOutput<ResponseBodySchema> : unknown>, next: NextFunction) => ResponseWrapper<ResponseBodySchema extends StandardSchemaV1<any> ? StandardSchemaV1.InferOutput<ResponseBodySchema> : unknown> | Promise<ResponseWrapper<ResponseBodySchema extends StandardSchemaV1<any> ? StandardSchemaV1.InferOutput<ResponseBodySchema> : unknown>>;
|
|
7
16
|
export declare class TypedRouter {
|
|
8
17
|
#private;
|
|
9
18
|
router: import("express-serve-static-core").Router;
|
|
10
|
-
get<Path extends string, RequestBodySchema extends StandardSchemaV1<any> | undefined = undefined, ResponseBodySchema extends StandardSchemaV1<any> | undefined = undefined, RequestQuerySchema extends StandardSchemaV1<any> | undefined = undefined>(path: Path, validation
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
19
|
+
get<Path extends string, RequestBodySchema extends StandardSchemaV1<any> | undefined = undefined, ResponseBodySchema extends StandardSchemaV1<any> | undefined = undefined, RequestQuerySchema extends StandardSchemaV1<any> | undefined = undefined>(path: Path, validation: ValidationSpec<RequestBodySchema, ResponseBodySchema, RequestQuerySchema>, ...middleware: [
|
|
20
|
+
...RequestHandler[],
|
|
21
|
+
TypedRequestHandler<ExtractParams<Path>, ResponseBodySchema, RequestBodySchema, RequestQuerySchema>
|
|
22
|
+
]): void;
|
|
23
|
+
post<Path extends string, RequestBodySchema extends StandardSchemaV1<any> | undefined = undefined, ResponseBodySchema extends StandardSchemaV1<any> | undefined = undefined, RequestQuerySchema extends StandardSchemaV1<any> | undefined = undefined>(path: Path, validation: ValidationSpec<RequestBodySchema, ResponseBodySchema, RequestQuerySchema>, ...middleware: [
|
|
24
|
+
...RequestHandler[],
|
|
25
|
+
TypedRequestHandler<ExtractParams<Path>, ResponseBodySchema, RequestBodySchema, RequestQuerySchema>
|
|
26
|
+
]): void;
|
|
27
|
+
put<Path extends string, RequestBodySchema extends StandardSchemaV1<any> | undefined = undefined, ResponseBodySchema extends StandardSchemaV1<any> | undefined = undefined, RequestQuerySchema extends StandardSchemaV1<any> | undefined = undefined>(path: Path, validation: ValidationSpec<RequestBodySchema, ResponseBodySchema, RequestQuerySchema>, ...middleware: [
|
|
28
|
+
...RequestHandler[],
|
|
29
|
+
TypedRequestHandler<ExtractParams<Path>, ResponseBodySchema, RequestBodySchema, RequestQuerySchema>
|
|
30
|
+
]): void;
|
|
31
|
+
delete<Path extends string, RequestBodySchema extends StandardSchemaV1<any> | undefined = undefined, ResponseBodySchema extends StandardSchemaV1<any> | undefined = undefined, RequestQuerySchema extends StandardSchemaV1<any> | undefined = undefined>(path: Path, validation: ValidationSpec<RequestBodySchema, ResponseBodySchema, RequestQuerySchema>, ...middleware: [
|
|
32
|
+
...RequestHandler[],
|
|
33
|
+
TypedRequestHandler<ExtractParams<Path>, ResponseBodySchema, RequestBodySchema, RequestQuerySchema>
|
|
34
|
+
]): void;
|
|
14
35
|
use(...middleware: RequestHandler[]): this;
|
|
15
36
|
use(...middleware: [string, ...RequestHandler[]]): this;
|
|
16
37
|
getRouter(): import("express-serve-static-core").Router;
|
package/dist/router.js
CHANGED
|
@@ -4,26 +4,25 @@ import { asyncMiddleware } from './middleware.js';
|
|
|
4
4
|
import { validateStandardSchema } from './validate.js';
|
|
5
5
|
export class TypedRouter {
|
|
6
6
|
router = Router();
|
|
7
|
-
get(path, validation
|
|
7
|
+
get(path, validation, ...middleware) {
|
|
8
8
|
return this.#handle('get', path, validation, ...middleware);
|
|
9
9
|
}
|
|
10
|
-
post(path, validation
|
|
10
|
+
post(path, validation, ...middleware) {
|
|
11
11
|
return this.#handle('post', path, validation, ...middleware);
|
|
12
12
|
}
|
|
13
|
-
put(path, validation
|
|
13
|
+
put(path, validation, ...middleware) {
|
|
14
14
|
return this.#handle('put', path, validation, ...middleware);
|
|
15
15
|
}
|
|
16
|
-
delete(path, validation
|
|
16
|
+
delete(path, validation, ...middleware) {
|
|
17
17
|
return this.#handle('delete', path, validation, ...middleware);
|
|
18
18
|
}
|
|
19
19
|
#handle(method, path, validation, ...middleware) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
};
|
|
20
|
+
const handler = middleware.pop();
|
|
21
|
+
this.router[method](path, validateStandardSchema({
|
|
22
|
+
body: validation.request,
|
|
23
|
+
query: validation.query,
|
|
24
|
+
response: validation.response,
|
|
25
|
+
}), ...middleware, asyncMiddleware(handler));
|
|
27
26
|
}
|
|
28
27
|
use(...middleware) {
|
|
29
28
|
this.router.use(...middleware);
|