@extk/expressive 0.1.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/README.md ADDED
File without changes
@@ -0,0 +1,262 @@
1
+ import winston, { Logger as Logger$1 } from 'winston';
2
+ import * as express from 'express';
3
+ import { RequestHandler, Request } from 'express';
4
+ import * as express_serve_static_core from 'express-serve-static-core';
5
+ import { RouteParameters } from 'express-serve-static-core';
6
+ import * as morgan from 'morgan';
7
+ import * as helmet from 'helmet';
8
+
9
+ type ExpressRoute = string;
10
+ type ExpressLocalsObj = Record<string, any>;
11
+ type ExpressHandler = RequestHandler<RouteParameters<ExpressRoute>, any, any, qs.ParsedQs, ExpressLocalsObj[]>;
12
+ type PaginationQuery = {
13
+ limit?: string | number;
14
+ page?: string | number;
15
+ };
16
+ type Pagination = {
17
+ limit: number;
18
+ offset: number;
19
+ };
20
+ type ReqSnapshot = {
21
+ readonly query: qs.ParsedQs;
22
+ readonly path: string;
23
+ readonly method: string;
24
+ readonly userId: string | number | undefined;
25
+ };
26
+
27
+ type HttpMethod = 'get' | 'post' | 'put' | 'patch' | 'delete' | 'head' | 'options' | 'trace';
28
+ type OtherString = string & {};
29
+ type ContentType = 'application/json' | 'application/xml' | 'text/plain' | 'text/html' | OtherString;
30
+ type AlertHandler = (err: Error & Record<string, any>, reqSnapshot?: ReqSnapshot) => void | Promise<void>;
31
+ type Logger = Logger$1;
32
+ type Container = {
33
+ logger: Logger;
34
+ alertHandler?: AlertHandler;
35
+ };
36
+
37
+ type Schema = Record<string, unknown> | {
38
+ $ref: string;
39
+ };
40
+ type Content = {
41
+ description?: string;
42
+ content?: Partial<Record<ContentType, {
43
+ schema: Schema;
44
+ }>>;
45
+ };
46
+ type Param = {
47
+ in: 'path' | 'query' | 'headers';
48
+ name: string;
49
+ desciption: string;
50
+ required: boolean;
51
+ schema: Schema;
52
+ };
53
+ type Servers = {
54
+ url: string;
55
+ description?: string;
56
+ }[];
57
+ type PathItem = {
58
+ summary?: string;
59
+ requestBody?: Content;
60
+ parameters?: Param[];
61
+ servers?: Servers;
62
+ responses?: Record<string, Content>;
63
+ tags?: string[];
64
+ operationId?: string;
65
+ deprecated?: boolean;
66
+ security?: AuthMethod[];
67
+ };
68
+ type AuthMethod = Record<string, string[]>;
69
+ type SecurityScheme = {
70
+ type: 'http';
71
+ scheme: 'basic' | 'bearer';
72
+ } | {
73
+ type: 'apiKey';
74
+ in: 'header';
75
+ name: string;
76
+ } | {
77
+ type: 'openIdConnect';
78
+ openIdConnectUrl: string;
79
+ } | {
80
+ type: 'oauth2';
81
+ flows: {
82
+ authorizationCode: {
83
+ authorizationUrl: string;
84
+ tokenUrl: string;
85
+ scopes: Record<string, string>;
86
+ };
87
+ };
88
+ };
89
+ type SwaggerConfig = {
90
+ openapi: '3.1.0';
91
+ info?: {
92
+ version?: string;
93
+ title?: string;
94
+ termsOfService?: string;
95
+ };
96
+ externalDocs?: {
97
+ url?: string;
98
+ description?: string;
99
+ };
100
+ servers?: Servers;
101
+ paths: Record<string, {
102
+ summary?: string;
103
+ description?: string;
104
+ servers?: Servers;
105
+ } & Partial<Record<HttpMethod, PathItem>>>;
106
+ components: {
107
+ schemas?: Record<string, Schema>;
108
+ securitySchemes?: Record<string, SecurityScheme>;
109
+ };
110
+ security?: AuthMethod[];
111
+ };
112
+
113
+ declare class ApiError extends Error {
114
+ readonly code: string;
115
+ readonly httpStatusCode: number;
116
+ data?: unknown;
117
+ constructor(message: string, httpStatusCode: number, errorCode: string);
118
+ setData<T>(data: T): this;
119
+ }
120
+ declare class NotFoundError extends ApiError {
121
+ constructor(message?: string);
122
+ }
123
+ declare class DuplicateError extends ApiError {
124
+ constructor(message?: string);
125
+ }
126
+ declare class BadRequestError extends ApiError {
127
+ constructor(message?: string);
128
+ }
129
+ declare class SchemaValidationError extends ApiError {
130
+ constructor(message?: string);
131
+ }
132
+ declare class FileTooBigError extends ApiError {
133
+ constructor();
134
+ }
135
+ declare class InvalidFileTypeError extends ApiError {
136
+ constructor();
137
+ }
138
+ declare class InvalidCredentialsError extends ApiError {
139
+ constructor();
140
+ }
141
+ declare class InternalError extends ApiError {
142
+ constructor();
143
+ }
144
+ declare class TooManyRequestsError extends ApiError {
145
+ constructor(message?: string);
146
+ }
147
+ declare class ForbiddenError extends ApiError {
148
+ constructor(message?: string);
149
+ }
150
+ declare class TokenExpiredError extends ApiError {
151
+ constructor(message?: string);
152
+ }
153
+ declare class UserUnauthorizedError extends ApiError {
154
+ constructor(message?: string);
155
+ }
156
+
157
+ declare function parsePositiveInteger<T>(v: T, defaultValue: number, max?: number): number;
158
+ declare function parseIdOrFail(v: unknown): number;
159
+ declare function slugify(text: string): string;
160
+ declare function getTmpDir(): string;
161
+ declare function getTmpPath(...steps: string[]): string;
162
+ declare function parseDefaultPagination(query: PaginationQuery): Pagination;
163
+ declare function createReqSnapshot(req: Request & {
164
+ user?: {
165
+ id?: string | number;
166
+ };
167
+ }): ReqSnapshot;
168
+
169
+ declare function isDev(): boolean;
170
+ declare function isProd(): boolean;
171
+ declare function getEnvVar(configName: string): string;
172
+ type Env = 'dev' | 'prod' | OtherString;
173
+
174
+ declare const createLogger: (options?: winston.LoggerOptions) => winston.Logger;
175
+ declare const getDefaultFileLogger: (name?: string) => winston.Logger;
176
+ declare const getDefaultConsoleLogger: (name?: string) => winston.Logger;
177
+
178
+ declare function jsonSchema(schema: Schema): Content;
179
+ declare function jsonSchemaRef(name: string): Content;
180
+ declare function param(inP: Param['in'], id: string, schema: Schema, required?: boolean, description?: string, name?: string): Param;
181
+ declare function pathParam(id: string, schema: Schema, required?: boolean, description?: string, name?: string): Param;
182
+ declare function queryParam(id: string, schema: Schema, required?: boolean, description?: string, name?: string): Param;
183
+ declare function headerParam(id: string, schema: Schema, required?: boolean, description?: string, name?: string): Param;
184
+ declare const SWG: {
185
+ param: typeof param;
186
+ pathParam: typeof pathParam;
187
+ queryParam: typeof queryParam;
188
+ headerParam: typeof headerParam;
189
+ jsonSchema: typeof jsonSchema;
190
+ jsonSchemaRef: typeof jsonSchemaRef;
191
+ security: (name: string) => AuthMethod;
192
+ securitySchemes: {
193
+ readonly BasicAuth: () => SecurityScheme;
194
+ readonly BearerAuth: () => SecurityScheme;
195
+ readonly ApiKeyAuth: (headerName: string) => SecurityScheme;
196
+ readonly OpenID: (openIdConnectUrl: string) => SecurityScheme;
197
+ readonly OAuth2: (authorizationUrl: string, tokenUrl: string, scopes: Record<string, string>) => SecurityScheme;
198
+ };
199
+ };
200
+
201
+ declare class ApiErrorResponse<T = undefined> {
202
+ readonly status = "error";
203
+ readonly message: string;
204
+ readonly errorCode: string;
205
+ readonly errors?: T;
206
+ constructor(message: string, errorCode: string, errors?: T);
207
+ }
208
+
209
+ declare class ApiResponse<T = undefined> {
210
+ status: string;
211
+ result?: T;
212
+ constructor(result?: T);
213
+ }
214
+
215
+ declare function bootstrap(container: Container): {
216
+ notFoundMiddleware: (_req: express.Request, res: express.Response, _next: express.NextFunction) => void;
217
+ silently: (fn: () => Promise<void>, reqSnapshot?: ReqSnapshot) => void;
218
+ getErrorHandlerMiddleware: (errorMapper?: (err: Error & Record<string, unknown>) => ApiError | null | undefined) => (err: Error & Record<string, unknown>, req: express.Request, res: express.Response, _next: express.NextFunction) => Promise<void>;
219
+ swaggerBuilder: () => {
220
+ withInfo(info: SwaggerConfig["info"]): /*elided*/ any;
221
+ withServers(servers: Servers): /*elided*/ any;
222
+ withSecuritySchemes(schemes: Record<string, SecurityScheme>): /*elided*/ any;
223
+ withSchemas(schemas: Record<string, Schema>): /*elided*/ any;
224
+ withDefaultSecurity(globalAuthMethods: AuthMethod[]): /*elided*/ any;
225
+ get(): SwaggerConfig;
226
+ };
227
+ expressiveServer(configs: {
228
+ swagger: {
229
+ path: ExpressRoute;
230
+ doc: SwaggerConfig;
231
+ };
232
+ options?: {
233
+ helmet?: Readonly<helmet.HelmetOptions>;
234
+ morgan?: Readonly<{
235
+ format: string;
236
+ options?: Parameters<typeof morgan>[1];
237
+ }>;
238
+ };
239
+ app?: express.Express;
240
+ }): express.Express;
241
+ expressiveRouter(configs: {
242
+ oapi?: {
243
+ tags?: string[];
244
+ servers?: Servers;
245
+ security?: AuthMethod[];
246
+ };
247
+ }): {
248
+ router: express_serve_static_core.Router;
249
+ addRoute(context: {
250
+ method: HttpMethod;
251
+ path: ExpressRoute;
252
+ oapi?: {
253
+ pathOverride?: string;
254
+ pathParameters?: Param[];
255
+ headerParameters?: Param[];
256
+ queryParameters?: Param[];
257
+ } & Omit<PathItem, "parameters">;
258
+ }, ...handlers: ExpressHandler[]): express_serve_static_core.Router;
259
+ };
260
+ };
261
+
262
+ export { type AlertHandler, ApiError, ApiErrorResponse, ApiResponse, type AuthMethod, BadRequestError, type Container, type Content, type ContentType, DuplicateError, type Env, type ExpressHandler, type ExpressLocalsObj, type ExpressRoute, FileTooBigError, ForbiddenError, type HttpMethod, InternalError, InvalidCredentialsError, InvalidFileTypeError, type Logger, NotFoundError, type OtherString, type Pagination, type PaginationQuery, type Param, type PathItem, type ReqSnapshot, SWG, type Schema, SchemaValidationError, type SecurityScheme, type Servers, type SwaggerConfig, TokenExpiredError, TooManyRequestsError, UserUnauthorizedError, bootstrap, createLogger, createReqSnapshot, getDefaultConsoleLogger, getDefaultFileLogger, getEnvVar, getTmpDir, getTmpPath, isDev, isProd, parseDefaultPagination, parseIdOrFail, parsePositiveInteger, slugify };
@@ -0,0 +1,262 @@
1
+ import winston, { Logger as Logger$1 } from 'winston';
2
+ import * as express from 'express';
3
+ import { RequestHandler, Request } from 'express';
4
+ import * as express_serve_static_core from 'express-serve-static-core';
5
+ import { RouteParameters } from 'express-serve-static-core';
6
+ import * as morgan from 'morgan';
7
+ import * as helmet from 'helmet';
8
+
9
+ type ExpressRoute = string;
10
+ type ExpressLocalsObj = Record<string, any>;
11
+ type ExpressHandler = RequestHandler<RouteParameters<ExpressRoute>, any, any, qs.ParsedQs, ExpressLocalsObj[]>;
12
+ type PaginationQuery = {
13
+ limit?: string | number;
14
+ page?: string | number;
15
+ };
16
+ type Pagination = {
17
+ limit: number;
18
+ offset: number;
19
+ };
20
+ type ReqSnapshot = {
21
+ readonly query: qs.ParsedQs;
22
+ readonly path: string;
23
+ readonly method: string;
24
+ readonly userId: string | number | undefined;
25
+ };
26
+
27
+ type HttpMethod = 'get' | 'post' | 'put' | 'patch' | 'delete' | 'head' | 'options' | 'trace';
28
+ type OtherString = string & {};
29
+ type ContentType = 'application/json' | 'application/xml' | 'text/plain' | 'text/html' | OtherString;
30
+ type AlertHandler = (err: Error & Record<string, any>, reqSnapshot?: ReqSnapshot) => void | Promise<void>;
31
+ type Logger = Logger$1;
32
+ type Container = {
33
+ logger: Logger;
34
+ alertHandler?: AlertHandler;
35
+ };
36
+
37
+ type Schema = Record<string, unknown> | {
38
+ $ref: string;
39
+ };
40
+ type Content = {
41
+ description?: string;
42
+ content?: Partial<Record<ContentType, {
43
+ schema: Schema;
44
+ }>>;
45
+ };
46
+ type Param = {
47
+ in: 'path' | 'query' | 'headers';
48
+ name: string;
49
+ desciption: string;
50
+ required: boolean;
51
+ schema: Schema;
52
+ };
53
+ type Servers = {
54
+ url: string;
55
+ description?: string;
56
+ }[];
57
+ type PathItem = {
58
+ summary?: string;
59
+ requestBody?: Content;
60
+ parameters?: Param[];
61
+ servers?: Servers;
62
+ responses?: Record<string, Content>;
63
+ tags?: string[];
64
+ operationId?: string;
65
+ deprecated?: boolean;
66
+ security?: AuthMethod[];
67
+ };
68
+ type AuthMethod = Record<string, string[]>;
69
+ type SecurityScheme = {
70
+ type: 'http';
71
+ scheme: 'basic' | 'bearer';
72
+ } | {
73
+ type: 'apiKey';
74
+ in: 'header';
75
+ name: string;
76
+ } | {
77
+ type: 'openIdConnect';
78
+ openIdConnectUrl: string;
79
+ } | {
80
+ type: 'oauth2';
81
+ flows: {
82
+ authorizationCode: {
83
+ authorizationUrl: string;
84
+ tokenUrl: string;
85
+ scopes: Record<string, string>;
86
+ };
87
+ };
88
+ };
89
+ type SwaggerConfig = {
90
+ openapi: '3.1.0';
91
+ info?: {
92
+ version?: string;
93
+ title?: string;
94
+ termsOfService?: string;
95
+ };
96
+ externalDocs?: {
97
+ url?: string;
98
+ description?: string;
99
+ };
100
+ servers?: Servers;
101
+ paths: Record<string, {
102
+ summary?: string;
103
+ description?: string;
104
+ servers?: Servers;
105
+ } & Partial<Record<HttpMethod, PathItem>>>;
106
+ components: {
107
+ schemas?: Record<string, Schema>;
108
+ securitySchemes?: Record<string, SecurityScheme>;
109
+ };
110
+ security?: AuthMethod[];
111
+ };
112
+
113
+ declare class ApiError extends Error {
114
+ readonly code: string;
115
+ readonly httpStatusCode: number;
116
+ data?: unknown;
117
+ constructor(message: string, httpStatusCode: number, errorCode: string);
118
+ setData<T>(data: T): this;
119
+ }
120
+ declare class NotFoundError extends ApiError {
121
+ constructor(message?: string);
122
+ }
123
+ declare class DuplicateError extends ApiError {
124
+ constructor(message?: string);
125
+ }
126
+ declare class BadRequestError extends ApiError {
127
+ constructor(message?: string);
128
+ }
129
+ declare class SchemaValidationError extends ApiError {
130
+ constructor(message?: string);
131
+ }
132
+ declare class FileTooBigError extends ApiError {
133
+ constructor();
134
+ }
135
+ declare class InvalidFileTypeError extends ApiError {
136
+ constructor();
137
+ }
138
+ declare class InvalidCredentialsError extends ApiError {
139
+ constructor();
140
+ }
141
+ declare class InternalError extends ApiError {
142
+ constructor();
143
+ }
144
+ declare class TooManyRequestsError extends ApiError {
145
+ constructor(message?: string);
146
+ }
147
+ declare class ForbiddenError extends ApiError {
148
+ constructor(message?: string);
149
+ }
150
+ declare class TokenExpiredError extends ApiError {
151
+ constructor(message?: string);
152
+ }
153
+ declare class UserUnauthorizedError extends ApiError {
154
+ constructor(message?: string);
155
+ }
156
+
157
+ declare function parsePositiveInteger<T>(v: T, defaultValue: number, max?: number): number;
158
+ declare function parseIdOrFail(v: unknown): number;
159
+ declare function slugify(text: string): string;
160
+ declare function getTmpDir(): string;
161
+ declare function getTmpPath(...steps: string[]): string;
162
+ declare function parseDefaultPagination(query: PaginationQuery): Pagination;
163
+ declare function createReqSnapshot(req: Request & {
164
+ user?: {
165
+ id?: string | number;
166
+ };
167
+ }): ReqSnapshot;
168
+
169
+ declare function isDev(): boolean;
170
+ declare function isProd(): boolean;
171
+ declare function getEnvVar(configName: string): string;
172
+ type Env = 'dev' | 'prod' | OtherString;
173
+
174
+ declare const createLogger: (options?: winston.LoggerOptions) => winston.Logger;
175
+ declare const getDefaultFileLogger: (name?: string) => winston.Logger;
176
+ declare const getDefaultConsoleLogger: (name?: string) => winston.Logger;
177
+
178
+ declare function jsonSchema(schema: Schema): Content;
179
+ declare function jsonSchemaRef(name: string): Content;
180
+ declare function param(inP: Param['in'], id: string, schema: Schema, required?: boolean, description?: string, name?: string): Param;
181
+ declare function pathParam(id: string, schema: Schema, required?: boolean, description?: string, name?: string): Param;
182
+ declare function queryParam(id: string, schema: Schema, required?: boolean, description?: string, name?: string): Param;
183
+ declare function headerParam(id: string, schema: Schema, required?: boolean, description?: string, name?: string): Param;
184
+ declare const SWG: {
185
+ param: typeof param;
186
+ pathParam: typeof pathParam;
187
+ queryParam: typeof queryParam;
188
+ headerParam: typeof headerParam;
189
+ jsonSchema: typeof jsonSchema;
190
+ jsonSchemaRef: typeof jsonSchemaRef;
191
+ security: (name: string) => AuthMethod;
192
+ securitySchemes: {
193
+ readonly BasicAuth: () => SecurityScheme;
194
+ readonly BearerAuth: () => SecurityScheme;
195
+ readonly ApiKeyAuth: (headerName: string) => SecurityScheme;
196
+ readonly OpenID: (openIdConnectUrl: string) => SecurityScheme;
197
+ readonly OAuth2: (authorizationUrl: string, tokenUrl: string, scopes: Record<string, string>) => SecurityScheme;
198
+ };
199
+ };
200
+
201
+ declare class ApiErrorResponse<T = undefined> {
202
+ readonly status = "error";
203
+ readonly message: string;
204
+ readonly errorCode: string;
205
+ readonly errors?: T;
206
+ constructor(message: string, errorCode: string, errors?: T);
207
+ }
208
+
209
+ declare class ApiResponse<T = undefined> {
210
+ status: string;
211
+ result?: T;
212
+ constructor(result?: T);
213
+ }
214
+
215
+ declare function bootstrap(container: Container): {
216
+ notFoundMiddleware: (_req: express.Request, res: express.Response, _next: express.NextFunction) => void;
217
+ silently: (fn: () => Promise<void>, reqSnapshot?: ReqSnapshot) => void;
218
+ getErrorHandlerMiddleware: (errorMapper?: (err: Error & Record<string, unknown>) => ApiError | null | undefined) => (err: Error & Record<string, unknown>, req: express.Request, res: express.Response, _next: express.NextFunction) => Promise<void>;
219
+ swaggerBuilder: () => {
220
+ withInfo(info: SwaggerConfig["info"]): /*elided*/ any;
221
+ withServers(servers: Servers): /*elided*/ any;
222
+ withSecuritySchemes(schemes: Record<string, SecurityScheme>): /*elided*/ any;
223
+ withSchemas(schemas: Record<string, Schema>): /*elided*/ any;
224
+ withDefaultSecurity(globalAuthMethods: AuthMethod[]): /*elided*/ any;
225
+ get(): SwaggerConfig;
226
+ };
227
+ expressiveServer(configs: {
228
+ swagger: {
229
+ path: ExpressRoute;
230
+ doc: SwaggerConfig;
231
+ };
232
+ options?: {
233
+ helmet?: Readonly<helmet.HelmetOptions>;
234
+ morgan?: Readonly<{
235
+ format: string;
236
+ options?: Parameters<typeof morgan>[1];
237
+ }>;
238
+ };
239
+ app?: express.Express;
240
+ }): express.Express;
241
+ expressiveRouter(configs: {
242
+ oapi?: {
243
+ tags?: string[];
244
+ servers?: Servers;
245
+ security?: AuthMethod[];
246
+ };
247
+ }): {
248
+ router: express_serve_static_core.Router;
249
+ addRoute(context: {
250
+ method: HttpMethod;
251
+ path: ExpressRoute;
252
+ oapi?: {
253
+ pathOverride?: string;
254
+ pathParameters?: Param[];
255
+ headerParameters?: Param[];
256
+ queryParameters?: Param[];
257
+ } & Omit<PathItem, "parameters">;
258
+ }, ...handlers: ExpressHandler[]): express_serve_static_core.Router;
259
+ };
260
+ };
261
+
262
+ export { type AlertHandler, ApiError, ApiErrorResponse, ApiResponse, type AuthMethod, BadRequestError, type Container, type Content, type ContentType, DuplicateError, type Env, type ExpressHandler, type ExpressLocalsObj, type ExpressRoute, FileTooBigError, ForbiddenError, type HttpMethod, InternalError, InvalidCredentialsError, InvalidFileTypeError, type Logger, NotFoundError, type OtherString, type Pagination, type PaginationQuery, type Param, type PathItem, type ReqSnapshot, SWG, type Schema, SchemaValidationError, type SecurityScheme, type Servers, type SwaggerConfig, TokenExpiredError, TooManyRequestsError, UserUnauthorizedError, bootstrap, createLogger, createReqSnapshot, getDefaultConsoleLogger, getDefaultFileLogger, getEnvVar, getTmpDir, getTmpPath, isDev, isProd, parseDefaultPagination, parseIdOrFail, parsePositiveInteger, slugify };