monapi 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/LICENSE +21 -0
- package/README.md +385 -0
- package/dist/index.d.mts +315 -0
- package/dist/index.d.ts +315 -0
- package/dist/index.js +1015 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +998 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +79 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
import { Request, Response, NextFunction, RequestHandler, Router } from 'express';
|
|
2
|
+
import { FilterQuery, SortOrder, Model, Schema, Connection } from 'mongoose';
|
|
3
|
+
|
|
4
|
+
type FilterOperator = 'eq' | 'ne' | 'gt' | 'gte' | 'lt' | 'lte' | 'in' | 'nin' | 'like' | 'exists';
|
|
5
|
+
interface FilterCondition {
|
|
6
|
+
operator: FilterOperator;
|
|
7
|
+
value: any;
|
|
8
|
+
}
|
|
9
|
+
interface ParsedFilters {
|
|
10
|
+
[field: string]: FilterCondition;
|
|
11
|
+
}
|
|
12
|
+
interface QueryOptions {
|
|
13
|
+
page?: number;
|
|
14
|
+
limit?: number;
|
|
15
|
+
sort?: string | string[];
|
|
16
|
+
fields?: string | string[];
|
|
17
|
+
[key: string]: any;
|
|
18
|
+
}
|
|
19
|
+
interface MongoQuery {
|
|
20
|
+
filter: FilterQuery<any>;
|
|
21
|
+
sort?: {
|
|
22
|
+
[key: string]: SortOrder;
|
|
23
|
+
};
|
|
24
|
+
skip?: number;
|
|
25
|
+
limit?: number;
|
|
26
|
+
projection?: {
|
|
27
|
+
[key: string]: 0 | 1;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
interface PaginationMeta {
|
|
31
|
+
page: number;
|
|
32
|
+
limit: number;
|
|
33
|
+
total: number;
|
|
34
|
+
totalPages?: number;
|
|
35
|
+
}
|
|
36
|
+
interface QueryConfig {
|
|
37
|
+
allowedFilters?: string[];
|
|
38
|
+
allowedSorts?: string[];
|
|
39
|
+
defaultSort?: string;
|
|
40
|
+
defaultLimit?: number;
|
|
41
|
+
maxLimit?: number;
|
|
42
|
+
}
|
|
43
|
+
declare enum FieldType {
|
|
44
|
+
String = "string",
|
|
45
|
+
Number = "number",
|
|
46
|
+
Boolean = "boolean",
|
|
47
|
+
Date = "date",
|
|
48
|
+
ObjectId = "objectid",
|
|
49
|
+
Array = "array",
|
|
50
|
+
Object = "object",
|
|
51
|
+
Mixed = "mixed"
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
type CRUDOperation = 'find' | 'create' | 'update' | 'patch' | 'delete';
|
|
55
|
+
interface User {
|
|
56
|
+
id: string;
|
|
57
|
+
roles?: string[];
|
|
58
|
+
[key: string]: any;
|
|
59
|
+
}
|
|
60
|
+
interface HookContext {
|
|
61
|
+
collection: string;
|
|
62
|
+
operation: CRUDOperation;
|
|
63
|
+
user?: User;
|
|
64
|
+
query?: MongoQuery;
|
|
65
|
+
data?: any;
|
|
66
|
+
result?: any;
|
|
67
|
+
id?: string;
|
|
68
|
+
req: Request;
|
|
69
|
+
res: Response;
|
|
70
|
+
meta?: Record<string, any>;
|
|
71
|
+
preventDefault?: boolean;
|
|
72
|
+
}
|
|
73
|
+
type HookFunction = (ctx: HookContext) => Promise<void> | void;
|
|
74
|
+
interface LifecycleHooks {
|
|
75
|
+
beforeFind?: HookFunction;
|
|
76
|
+
afterFind?: HookFunction;
|
|
77
|
+
beforeCreate?: HookFunction;
|
|
78
|
+
afterCreate?: HookFunction;
|
|
79
|
+
beforeUpdate?: HookFunction;
|
|
80
|
+
afterUpdate?: HookFunction;
|
|
81
|
+
beforeDelete?: HookFunction;
|
|
82
|
+
afterDelete?: HookFunction;
|
|
83
|
+
}
|
|
84
|
+
interface HookEntry {
|
|
85
|
+
collection: string;
|
|
86
|
+
operation: keyof LifecycleHooks;
|
|
87
|
+
handler: HookFunction;
|
|
88
|
+
priority?: number;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
interface PermissionContext {
|
|
92
|
+
user: User;
|
|
93
|
+
collection: string;
|
|
94
|
+
operation: CRUDOperation;
|
|
95
|
+
data?: any;
|
|
96
|
+
id?: string;
|
|
97
|
+
req: Request;
|
|
98
|
+
}
|
|
99
|
+
type PermissionFunction = (ctx: PermissionContext) => boolean | Promise<boolean>;
|
|
100
|
+
type Permission = string[] | PermissionFunction;
|
|
101
|
+
interface PermissionConfig {
|
|
102
|
+
list?: Permission;
|
|
103
|
+
get?: Permission;
|
|
104
|
+
create?: Permission;
|
|
105
|
+
update?: Permission;
|
|
106
|
+
patch?: Permission;
|
|
107
|
+
delete?: Permission;
|
|
108
|
+
}
|
|
109
|
+
interface FieldPermission {
|
|
110
|
+
read?: string[];
|
|
111
|
+
write?: string[];
|
|
112
|
+
}
|
|
113
|
+
interface FieldPermissions {
|
|
114
|
+
[fieldName: string]: FieldPermission;
|
|
115
|
+
}
|
|
116
|
+
type AuthMiddleware = (req: Request, res: Response, next: NextFunction) => void | Promise<void>;
|
|
117
|
+
interface AuthConfig {
|
|
118
|
+
middleware?: AuthMiddleware;
|
|
119
|
+
jwtSecret?: string;
|
|
120
|
+
jwtOptions?: {
|
|
121
|
+
algorithm?: string;
|
|
122
|
+
expiresIn?: string;
|
|
123
|
+
};
|
|
124
|
+
sessionSecret?: string;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
interface ValidationResult {
|
|
128
|
+
valid: boolean;
|
|
129
|
+
errors?: ValidationError$1[];
|
|
130
|
+
data?: any;
|
|
131
|
+
}
|
|
132
|
+
interface ValidationError$1 {
|
|
133
|
+
field: string;
|
|
134
|
+
message: string;
|
|
135
|
+
code?: string;
|
|
136
|
+
}
|
|
137
|
+
interface FieldMetadata {
|
|
138
|
+
name: string;
|
|
139
|
+
type: FieldType;
|
|
140
|
+
required?: boolean;
|
|
141
|
+
default?: any;
|
|
142
|
+
enum?: any[];
|
|
143
|
+
}
|
|
144
|
+
interface SchemaAdapter {
|
|
145
|
+
getFields(): string[];
|
|
146
|
+
getFieldType(field: string): FieldType;
|
|
147
|
+
getFieldMetadata(field: string): FieldMetadata | undefined;
|
|
148
|
+
getAllFieldsMetadata(): FieldMetadata[];
|
|
149
|
+
validate(data: unknown): Promise<ValidationResult> | ValidationResult;
|
|
150
|
+
getMongooseModel?(): Model<any> | undefined;
|
|
151
|
+
getMongooseSchema?(): Schema | undefined;
|
|
152
|
+
}
|
|
153
|
+
declare enum SchemaType {
|
|
154
|
+
Mongoose = "mongoose",
|
|
155
|
+
Typegoose = "typegoose",
|
|
156
|
+
Zod = "zod",
|
|
157
|
+
Joi = "joi",
|
|
158
|
+
Yup = "yup",
|
|
159
|
+
Unknown = "unknown"
|
|
160
|
+
}
|
|
161
|
+
interface SchemaOptions {
|
|
162
|
+
strict?: boolean;
|
|
163
|
+
timestamps?: boolean;
|
|
164
|
+
validateBeforeSave?: boolean;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
type Handler = (req: Request, res: Response, next: NextFunction) => void | Promise<void>;
|
|
168
|
+
interface CRUDHandlers {
|
|
169
|
+
list: Handler;
|
|
170
|
+
get: Handler;
|
|
171
|
+
create: Handler;
|
|
172
|
+
update: Handler;
|
|
173
|
+
patch: Handler;
|
|
174
|
+
delete: Handler;
|
|
175
|
+
}
|
|
176
|
+
interface MiddlewareConfig {
|
|
177
|
+
all?: RequestHandler[];
|
|
178
|
+
list?: RequestHandler[];
|
|
179
|
+
get?: RequestHandler[];
|
|
180
|
+
create?: RequestHandler[];
|
|
181
|
+
update?: RequestHandler[];
|
|
182
|
+
patch?: RequestHandler[];
|
|
183
|
+
delete?: RequestHandler[];
|
|
184
|
+
}
|
|
185
|
+
interface CollectionConfig {
|
|
186
|
+
schema: Schema | Model<any> | any;
|
|
187
|
+
validator?: any;
|
|
188
|
+
model?: Model<any>;
|
|
189
|
+
handlers?: Partial<CRUDHandlers>;
|
|
190
|
+
hooks?: Partial<LifecycleHooks>;
|
|
191
|
+
middleware?: MiddlewareConfig;
|
|
192
|
+
permissions?: PermissionConfig;
|
|
193
|
+
fields?: FieldPermissions;
|
|
194
|
+
query?: QueryConfig;
|
|
195
|
+
adapter?: SchemaAdapter;
|
|
196
|
+
}
|
|
197
|
+
interface DefaultConfig {
|
|
198
|
+
pagination?: {
|
|
199
|
+
limit?: number;
|
|
200
|
+
maxLimit?: number;
|
|
201
|
+
};
|
|
202
|
+
security?: {
|
|
203
|
+
maxRegexLength?: number;
|
|
204
|
+
maxRegexComplexity?: number;
|
|
205
|
+
maxQueryCost?: number;
|
|
206
|
+
};
|
|
207
|
+
query?: QueryConfig;
|
|
208
|
+
}
|
|
209
|
+
interface MonapiConfig {
|
|
210
|
+
connection: Connection;
|
|
211
|
+
basePath?: string;
|
|
212
|
+
framework?: 'express' | 'fastify';
|
|
213
|
+
defaults?: DefaultConfig;
|
|
214
|
+
auth?: AuthConfig;
|
|
215
|
+
logger?: Logger;
|
|
216
|
+
}
|
|
217
|
+
interface Logger {
|
|
218
|
+
info(message: string, meta?: any): void;
|
|
219
|
+
warn(message: string, meta?: any): void;
|
|
220
|
+
error(message: string, meta?: any): void;
|
|
221
|
+
debug(message: string, meta?: any): void;
|
|
222
|
+
}
|
|
223
|
+
interface ListResponse<T = any> {
|
|
224
|
+
data: T[];
|
|
225
|
+
meta: {
|
|
226
|
+
page: number;
|
|
227
|
+
limit: number;
|
|
228
|
+
total: number;
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
interface SingleResponse<T = any> {
|
|
232
|
+
data: T;
|
|
233
|
+
}
|
|
234
|
+
interface ErrorResponse {
|
|
235
|
+
error: {
|
|
236
|
+
code: string;
|
|
237
|
+
message: string;
|
|
238
|
+
details?: any;
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
declare class Monapi {
|
|
243
|
+
private config;
|
|
244
|
+
private logger;
|
|
245
|
+
private collections;
|
|
246
|
+
private authMiddleware?;
|
|
247
|
+
constructor(config: MonapiConfig);
|
|
248
|
+
resource(name: string, collectionConfig: CollectionConfig): this;
|
|
249
|
+
router(): Router;
|
|
250
|
+
getModel(name: string): Model<any> | undefined;
|
|
251
|
+
getAdapter(name: string): SchemaAdapter | undefined;
|
|
252
|
+
private resolveModel;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
declare class MongooseAdapter implements SchemaAdapter {
|
|
256
|
+
private schema;
|
|
257
|
+
private model?;
|
|
258
|
+
constructor(schemaOrModel: Schema | Model<any>);
|
|
259
|
+
private isModel;
|
|
260
|
+
getFields(): string[];
|
|
261
|
+
getFieldType(field: string): FieldType;
|
|
262
|
+
getFieldMetadata(field: string): FieldMetadata | undefined;
|
|
263
|
+
getAllFieldsMetadata(): FieldMetadata[];
|
|
264
|
+
validate(data: unknown): Promise<ValidationResult>;
|
|
265
|
+
private basicValidation;
|
|
266
|
+
getMongooseModel(): Model<any> | undefined;
|
|
267
|
+
getMongooseSchema(): Schema;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
declare function detectSchemaType(schema: any): SchemaType;
|
|
271
|
+
declare function createSchemaAdapter(schema: Schema | Model<any> | any): SchemaAdapter;
|
|
272
|
+
|
|
273
|
+
declare class MonapiError extends Error {
|
|
274
|
+
readonly statusCode: number;
|
|
275
|
+
readonly code: string;
|
|
276
|
+
readonly details?: any;
|
|
277
|
+
constructor(message: string, statusCode: number, code: string, details?: any);
|
|
278
|
+
}
|
|
279
|
+
declare class NotFoundError extends MonapiError {
|
|
280
|
+
constructor(resource: string, id?: string);
|
|
281
|
+
}
|
|
282
|
+
declare class ValidationError extends MonapiError {
|
|
283
|
+
constructor(message: string, details?: any);
|
|
284
|
+
}
|
|
285
|
+
declare class ForbiddenError extends MonapiError {
|
|
286
|
+
constructor(message?: string);
|
|
287
|
+
}
|
|
288
|
+
declare class UnauthorizedError extends MonapiError {
|
|
289
|
+
constructor(message?: string);
|
|
290
|
+
}
|
|
291
|
+
declare class BadRequestError extends MonapiError {
|
|
292
|
+
constructor(message: string, details?: any);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
interface FilterParserOptions {
|
|
296
|
+
adapter?: SchemaAdapter;
|
|
297
|
+
queryConfig?: QueryConfig;
|
|
298
|
+
maxRegexLength?: number;
|
|
299
|
+
maxFilters?: number;
|
|
300
|
+
}
|
|
301
|
+
declare function parseFilters(query: Record<string, any>, options?: FilterParserOptions): FilterQuery<any>;
|
|
302
|
+
|
|
303
|
+
interface QueryBuilderOptions {
|
|
304
|
+
adapter?: SchemaAdapter;
|
|
305
|
+
queryConfig?: QueryConfig;
|
|
306
|
+
defaultLimit?: number;
|
|
307
|
+
maxLimit?: number;
|
|
308
|
+
maxRegexLength?: number;
|
|
309
|
+
}
|
|
310
|
+
declare function buildQuery(queryParams: Record<string, any>, options?: QueryBuilderOptions): MongoQuery;
|
|
311
|
+
declare function buildPaginationMeta(total: number, page: number, limit: number): PaginationMeta;
|
|
312
|
+
|
|
313
|
+
declare function createErrorHandler(logger?: Logger): (err: Error, _req: Request, res: Response, _next: NextFunction) => void;
|
|
314
|
+
|
|
315
|
+
export { type AuthConfig, type AuthMiddleware, BadRequestError, type CRUDHandlers, type CRUDOperation, type CollectionConfig, type DefaultConfig, type ErrorResponse, type FieldMetadata, type FieldPermission, type FieldPermissions, FieldType, type FilterCondition, type FilterOperator, ForbiddenError, type Handler, type HookContext, type HookEntry, type HookFunction, type LifecycleHooks, type ListResponse, type Logger, type MiddlewareConfig, Monapi, type MonapiConfig, MonapiError, type MongoQuery, MongooseAdapter, NotFoundError, type PaginationMeta, type ParsedFilters, type Permission, type PermissionConfig, type PermissionContext, type PermissionFunction, type QueryConfig, type QueryOptions, type SchemaAdapter, type SchemaOptions, SchemaType, type ValidationError$1 as SchemaValidationError, type SingleResponse, UnauthorizedError, type User, ValidationError, type ValidationResult, buildPaginationMeta, buildQuery, createErrorHandler, createSchemaAdapter, detectSchemaType, parseFilters };
|