@terreno/api 0.0.3 → 0.0.10
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 +3 -3
- package/dist/api.d.ts +5 -5
- package/dist/expressServer.d.ts +2 -2
- package/dist/openApi.d.ts +7 -7
- package/dist/openApiBuilder.d.ts +3 -3
- package/dist/permissions.d.ts +2 -2
- package/dist/transformers.d.ts +4 -4
- package/package.json +1 -1
- package/src/api.ts +5 -5
- package/src/example.ts +2 -2
- package/src/expressServer.ts +2 -2
- package/src/openApi.test.ts +4 -4
- package/src/openApi.ts +7 -7
- package/src/openApiBuilder.test.ts +2 -2
- package/src/openApiBuilder.ts +4 -4
- package/src/permissions.ts +2 -2
- package/src/transformers.ts +4 -4
package/README.md
CHANGED
|
@@ -5,9 +5,9 @@ Most REST APIs wind up being a lot of boilerplate, so this tries to cut that dow
|
|
|
5
5
|
into a full blown framework of its own. This library is inspired by the
|
|
6
6
|
[Django-REST-Framework](https://www.django-rest-framework.org).
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
These APIs integrate with @terreno/rtk to create consistent types on the frontend
|
|
9
|
+
and backend, and automatically generated React hooks to fetch, query, and modify
|
|
10
|
+
model instances.
|
|
11
11
|
|
|
12
12
|
## Getting started
|
|
13
13
|
|
package/dist/api.d.ts
CHANGED
|
@@ -22,7 +22,7 @@ export type RESTMethod = "list" | "create" | "read" | "update" | "delete";
|
|
|
22
22
|
* This is the main configuration.
|
|
23
23
|
* @param T - the base document type. This should not include Mongoose models, just the types of the object.
|
|
24
24
|
*/
|
|
25
|
-
export interface
|
|
25
|
+
export interface ModelRouterOptions<T> {
|
|
26
26
|
/**
|
|
27
27
|
* A group of method-level (create/read/update/delete/list) permissions.
|
|
28
28
|
* Determine if the user can perform the operation at all, and for read/update/delete methods,
|
|
@@ -180,7 +180,7 @@ export interface modelRouterOptions<T> {
|
|
|
180
180
|
* This is a good spot to remove sensitive information from the object, such as passwords or API
|
|
181
181
|
* keys. Throw an APIError to return a 400 with an error message.
|
|
182
182
|
*/
|
|
183
|
-
responseHandler?: (value: (Document<any, any, any> & T) | (Document<any, any, any> & T)[], method: "list" | "create" | "read" | "update" | "delete", request: express.Request, options:
|
|
183
|
+
responseHandler?: (value: (Document<any, any, any> & T) | (Document<any, any, any> & T)[], method: "list" | "create" | "read" | "update" | "delete", request: express.Request, options: ModelRouterOptions<T>) => Promise<JSONValue>;
|
|
184
184
|
/**
|
|
185
185
|
* The discriminatorKey that you passed when creating the Mongoose models. Defaults to __t. See:
|
|
186
186
|
* https://mongoosejs.com/docs/discriminators.html If this key is provided,
|
|
@@ -214,14 +214,14 @@ export interface modelRouterOptions<T> {
|
|
|
214
214
|
*/
|
|
215
215
|
openApiExtraModelProperties?: any;
|
|
216
216
|
}
|
|
217
|
-
export declare function getModel(baseModel: Model<any>, body?: any, options?:
|
|
217
|
+
export declare function getModel(baseModel: Model<any>, body?: any, options?: ModelRouterOptions<any>): mongoose.Model<any, {}, {}, {}, any, any, any>;
|
|
218
218
|
/**
|
|
219
219
|
* Create a set of CRUD routes given a Mongoose model $baseModel and configuration options.
|
|
220
220
|
*
|
|
221
221
|
* @param baseModel A Mongoose Model
|
|
222
222
|
* @param options Options for configuring the REST API, such as permissions, transformers, and hooks.
|
|
223
223
|
*/
|
|
224
|
-
export declare function modelRouter<T>(baseModel: Model<T>, options:
|
|
224
|
+
export declare function modelRouter<T>(baseModel: Model<T>, options: ModelRouterOptions<T>): express.Router;
|
|
225
225
|
export declare const asyncHandler: (fn: any) => (req: Request, res: Response, next: NextFunction) => Promise<any>;
|
|
226
226
|
export declare const gooseRestRouter: typeof modelRouter;
|
|
227
|
-
export type GooseRESTOptions<T> =
|
|
227
|
+
export type GooseRESTOptions<T> = ModelRouterOptions<T>;
|
package/dist/expressServer.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import * as Sentry from "@sentry/node";
|
|
2
2
|
import express, { type Router } from "express";
|
|
3
3
|
import type jwt from "jsonwebtoken";
|
|
4
|
-
import type {
|
|
4
|
+
import type { ModelRouterOptions } from "./api";
|
|
5
5
|
import { type UserModel as UserMongooseModel } from "./auth";
|
|
6
6
|
import { type LoggingOptions } from "./logger";
|
|
7
7
|
export declare function setupEnvironment(): void;
|
|
8
|
-
export type AddRoutes = (router: Router, options?: Partial<
|
|
8
|
+
export type AddRoutes = (router: Router, options?: Partial<ModelRouterOptions<any>>) => void;
|
|
9
9
|
export declare function logRequests(req: any, res: any, next: any): void;
|
|
10
10
|
export declare function createRouter(rootPath: string, addRoutes: AddRoutes, middleware?: any[]): any[];
|
|
11
11
|
export declare function createRouterWithAuth(rootPath: string, addRoutes: (router: Router) => void, middleware?: any[]): any[];
|
package/dist/openApi.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Model } from "mongoose";
|
|
2
|
-
import type {
|
|
2
|
+
import type { ModelRouterOptions } from "./api";
|
|
3
3
|
export declare const apiErrorContent: {
|
|
4
4
|
"application/json": {
|
|
5
5
|
schema: {
|
|
@@ -52,9 +52,9 @@ export declare const defaultOpenApiErrorResponses: {
|
|
|
52
52
|
description: string;
|
|
53
53
|
};
|
|
54
54
|
};
|
|
55
|
-
export declare function getOpenApiMiddleware<T>(model: Model<T>, options: Partial<
|
|
56
|
-
export declare function listOpenApiMiddleware<T>(model: Model<T>, options: Partial<
|
|
57
|
-
export declare function createOpenApiMiddleware<T>(model: Model<T>, options: Partial<
|
|
58
|
-
export declare function patchOpenApiMiddleware<T>(model: Model<T>, options: Partial<
|
|
59
|
-
export declare function deleteOpenApiMiddleware<T>(model: Model<T>, options: Partial<
|
|
60
|
-
export declare function readOpenApiMiddleware<T>(options: Partial<
|
|
55
|
+
export declare function getOpenApiMiddleware<T>(model: Model<T>, options: Partial<ModelRouterOptions<T>>): any;
|
|
56
|
+
export declare function listOpenApiMiddleware<T>(model: Model<T>, options: Partial<ModelRouterOptions<T>>): any;
|
|
57
|
+
export declare function createOpenApiMiddleware<T>(model: Model<T>, options: Partial<ModelRouterOptions<T>>): any;
|
|
58
|
+
export declare function patchOpenApiMiddleware<T>(model: Model<T>, options: Partial<ModelRouterOptions<T>>): any;
|
|
59
|
+
export declare function deleteOpenApiMiddleware<T>(model: Model<T>, options: Partial<ModelRouterOptions<T>>): any;
|
|
60
|
+
export declare function readOpenApiMiddleware<T>(options: Partial<ModelRouterOptions<T>>, properties: any, required: string[], queryParameters: any): any;
|
package/dist/openApiBuilder.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ModelRouterOptions } from "./api";
|
|
2
2
|
/**
|
|
3
3
|
* Defines a property within an OpenAPI schema.
|
|
4
4
|
*
|
|
@@ -189,7 +189,7 @@ export declare class OpenApiMiddlewareBuilder {
|
|
|
189
189
|
*
|
|
190
190
|
* @param options - Router options containing the OpenAPI path configuration
|
|
191
191
|
*/
|
|
192
|
-
constructor(options: Partial<
|
|
192
|
+
constructor(options: Partial<ModelRouterOptions<any>>);
|
|
193
193
|
/**
|
|
194
194
|
* Sets the tags for the OpenAPI operation.
|
|
195
195
|
*
|
|
@@ -416,4 +416,4 @@ export declare class OpenApiMiddlewareBuilder {
|
|
|
416
416
|
* router.get("/analytics/stats", statsMiddleware, getStatsHandler);
|
|
417
417
|
* ```
|
|
418
418
|
*/
|
|
419
|
-
export declare function createOpenApiBuilder(options: Partial<
|
|
419
|
+
export declare function createOpenApiBuilder(options: Partial<ModelRouterOptions<any>>): OpenApiMiddlewareBuilder;
|
package/dist/permissions.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type express from "express";
|
|
2
2
|
import type { NextFunction } from "express";
|
|
3
3
|
import { type Model } from "mongoose";
|
|
4
|
-
import { type
|
|
4
|
+
import { type ModelRouterOptions, type RESTMethod } from "./api";
|
|
5
5
|
import type { User } from "./auth";
|
|
6
6
|
export type PermissionMethod<T> = (method: RESTMethod, user?: User, obj?: T) => boolean | Promise<boolean>;
|
|
7
7
|
export interface RESTPermissions<T> {
|
|
@@ -23,4 +23,4 @@ export declare const Permissions: {
|
|
|
23
23
|
IsOwnerOrReadOnly: (method: RESTMethod, user?: User, obj?: any) => boolean;
|
|
24
24
|
};
|
|
25
25
|
export declare function checkPermissions<T>(method: RESTMethod, permissions: PermissionMethod<T>[], user?: User, obj?: T): Promise<boolean>;
|
|
26
|
-
export declare function permissionMiddleware<T>(baseModel: Model<T>, options: Pick<
|
|
26
|
+
export declare function permissionMiddleware<T>(baseModel: Model<T>, options: Pick<ModelRouterOptions<T>, "permissions" | "populatePaths" | "discriminatorKey">): (req: express.Request, _res: express.Response, next: NextFunction) => Promise<void>;
|
package/dist/transformers.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type express from "express";
|
|
2
2
|
import type { Document } from "mongoose";
|
|
3
|
-
import type {
|
|
3
|
+
import type { ModelRouterOptions } from "./api";
|
|
4
4
|
import type { User } from "./auth";
|
|
5
5
|
export interface TerrenoTransformer<T> {
|
|
6
6
|
transform?: (obj: Partial<T>, method: "create" | "update", user?: User) => Partial<T> | undefined;
|
|
@@ -16,10 +16,10 @@ export declare function AdminOwnerTransformer<T>(options: {
|
|
|
16
16
|
ownerWriteFields?: string[];
|
|
17
17
|
adminWriteFields?: string[];
|
|
18
18
|
}): TerrenoTransformer<T>;
|
|
19
|
-
export declare function transform<T>(options:
|
|
20
|
-
export declare function serialize<T>(req: express.Request, options:
|
|
19
|
+
export declare function transform<T>(options: ModelRouterOptions<T>, data: Partial<T> | Partial<T>[], method: "create" | "update", user?: User): Partial<T> | (Partial<T> | undefined)[] | undefined;
|
|
20
|
+
export declare function serialize<T>(req: express.Request, options: ModelRouterOptions<T>, data: (Document<any, any, any> & T) | (Document<any, any, any> & T)[]): Partial<T> | (Partial<T> | undefined)[] | undefined;
|
|
21
21
|
/**
|
|
22
22
|
* Default response handler for modelRouter. Calls toObject on each doc and returns the result,
|
|
23
23
|
* using transformers.serializer if provided.
|
|
24
24
|
*/
|
|
25
|
-
export declare function defaultResponseHandler<T>(doc: (Document<any, any, any> & T) | (Document<any, any, any> & T)[] | null, method: "list" | "create" | "read" | "update", request: express.Request, options:
|
|
25
|
+
export declare function defaultResponseHandler<T>(doc: (Document<any, any, any> & T) | (Document<any, any, any> & T)[] | null, method: "list" | "create" | "read" | "update", request: express.Request, options: ModelRouterOptions<T>): Promise<Partial<T> | (Partial<T> | undefined)[] | null | undefined>;
|
package/package.json
CHANGED
package/src/api.ts
CHANGED
|
@@ -70,7 +70,7 @@ export type RESTMethod = "list" | "create" | "read" | "update" | "delete";
|
|
|
70
70
|
* This is the main configuration.
|
|
71
71
|
* @param T - the base document type. This should not include Mongoose models, just the types of the object.
|
|
72
72
|
*/
|
|
73
|
-
export interface
|
|
73
|
+
export interface ModelRouterOptions<T> {
|
|
74
74
|
/**
|
|
75
75
|
* A group of method-level (create/read/update/delete/list) permissions.
|
|
76
76
|
* Determine if the user can perform the operation at all, and for read/update/delete methods,
|
|
@@ -239,7 +239,7 @@ export interface modelRouterOptions<T> {
|
|
|
239
239
|
value: (Document<any, any, any> & T) | (Document<any, any, any> & T)[],
|
|
240
240
|
method: "list" | "create" | "read" | "update" | "delete",
|
|
241
241
|
request: express.Request,
|
|
242
|
-
options:
|
|
242
|
+
options: ModelRouterOptions<T>
|
|
243
243
|
) => Promise<JSONValue>;
|
|
244
244
|
/**
|
|
245
245
|
* The discriminatorKey that you passed when creating the Mongoose models. Defaults to __t. See:
|
|
@@ -277,7 +277,7 @@ export interface modelRouterOptions<T> {
|
|
|
277
277
|
|
|
278
278
|
// A function to decide which model to use. If no discriminators are provided,
|
|
279
279
|
// just returns the base model. If
|
|
280
|
-
export function getModel(baseModel: Model<any>, body?: any, options?:
|
|
280
|
+
export function getModel(baseModel: Model<any>, body?: any, options?: ModelRouterOptions<any>) {
|
|
281
281
|
const discriminatorKey = options?.discriminatorKey ?? "__t";
|
|
282
282
|
const modelName = body?.[discriminatorKey];
|
|
283
283
|
if (!modelName) {
|
|
@@ -344,7 +344,7 @@ function checkQueryParamAllowed(
|
|
|
344
344
|
*/
|
|
345
345
|
export function modelRouter<T>(
|
|
346
346
|
baseModel: Model<T>,
|
|
347
|
-
options:
|
|
347
|
+
options: ModelRouterOptions<T>
|
|
348
348
|
): express.Router {
|
|
349
349
|
const router = express.Router();
|
|
350
350
|
|
|
@@ -1033,4 +1033,4 @@ export const asyncHandler = (fn: any) => (req: Request, res: Response, next: Nex
|
|
|
1033
1033
|
|
|
1034
1034
|
// For backwards compatibility with the old names.
|
|
1035
1035
|
export const gooseRestRouter = modelRouter;
|
|
1036
|
-
export type GooseRESTOptions<T> =
|
|
1036
|
+
export type GooseRESTOptions<T> = ModelRouterOptions<T>;
|
package/src/example.ts
CHANGED
|
@@ -2,7 +2,7 @@ import express from "express";
|
|
|
2
2
|
import mongoose, {model, Schema} from "mongoose";
|
|
3
3
|
import passportLocalMongoose from "passport-local-mongoose";
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import {type ModelRouterOptions, modelRouter} from "./api";
|
|
6
6
|
import {addAuthRoutes, setupAuth} from "./auth";
|
|
7
7
|
import {setupServer} from "./expressServer";
|
|
8
8
|
import {logger} from "./logger";
|
|
@@ -68,7 +68,7 @@ function getBaseServer() {
|
|
|
68
68
|
setupAuth(app, UserModel as any);
|
|
69
69
|
addAuthRoutes(app, UserModel as any);
|
|
70
70
|
|
|
71
|
-
function addRoutes(router: express.Router, options?: Partial<
|
|
71
|
+
function addRoutes(router: express.Router, options?: Partial<ModelRouterOptions<any>>): void {
|
|
72
72
|
router.use(
|
|
73
73
|
"/food",
|
|
74
74
|
modelRouter(FoodModel, {
|
package/src/expressServer.ts
CHANGED
|
@@ -9,7 +9,7 @@ import onFinished from "on-finished";
|
|
|
9
9
|
import passport from "passport";
|
|
10
10
|
import qs from "qs";
|
|
11
11
|
|
|
12
|
-
import type {
|
|
12
|
+
import type {ModelRouterOptions} from "./api";
|
|
13
13
|
import {addAuthRoutes, addMeRoutes, setupAuth, type UserModel as UserMongooseModel} from "./auth";
|
|
14
14
|
import {apiErrorMiddleware, apiUnauthorizedMiddleware} from "./errors";
|
|
15
15
|
import {type LoggingOptions, logger, setupLogging} from "./logger";
|
|
@@ -41,7 +41,7 @@ export function setupEnvironment(): void {
|
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
export type AddRoutes = (router: Router, options?: Partial<
|
|
44
|
+
export type AddRoutes = (router: Router, options?: Partial<ModelRouterOptions<any>>) => void;
|
|
45
45
|
|
|
46
46
|
const logRequestsFinished = (req: any, res: any, startTime: bigint) => {
|
|
47
47
|
const options = (res.locals.loggingOptions ?? {}) as LoggingOptions;
|
package/src/openApi.test.ts
CHANGED
|
@@ -4,13 +4,13 @@ import type {Router} from "express";
|
|
|
4
4
|
import supertest from "supertest";
|
|
5
5
|
import type TestAgent from "supertest/lib/agent";
|
|
6
6
|
|
|
7
|
-
import {
|
|
7
|
+
import {type ModelRouterOptions, modelRouter} from "./api";
|
|
8
8
|
import {addAuthRoutes, setupAuth} from "./auth";
|
|
9
9
|
import {setupServer} from "./expressServer";
|
|
10
10
|
import {Permissions} from "./permissions";
|
|
11
11
|
import {FoodModel, setupDb, UserModel} from "./tests";
|
|
12
12
|
|
|
13
|
-
function getMessageSummaryOpenApiMiddleware(options: Partial<
|
|
13
|
+
function getMessageSummaryOpenApiMiddleware(options: Partial<ModelRouterOptions<any>>): any {
|
|
14
14
|
return options.openApi.path({
|
|
15
15
|
parameters: [
|
|
16
16
|
{
|
|
@@ -42,7 +42,7 @@ function getMessageSummaryOpenApiMiddleware(options: Partial<modelRouterOptions<
|
|
|
42
42
|
});
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
function addRoutes(router: Router, options?: Partial<
|
|
45
|
+
function addRoutes(router: Router, options?: Partial<ModelRouterOptions<any>>): void {
|
|
46
46
|
router.use(
|
|
47
47
|
"/food",
|
|
48
48
|
modelRouter(FoodModel as any, {
|
|
@@ -176,7 +176,7 @@ describe("openApi", () => {
|
|
|
176
176
|
});
|
|
177
177
|
});
|
|
178
178
|
|
|
179
|
-
function addRoutesPopulate(router: Router, options?: Partial<
|
|
179
|
+
function addRoutesPopulate(router: Router, options?: Partial<ModelRouterOptions<any>>): void {
|
|
180
180
|
options?.openApi.component("schemas", "LimitedUser", {
|
|
181
181
|
properties: {
|
|
182
182
|
email: {
|
package/src/openApi.ts
CHANGED
|
@@ -3,7 +3,7 @@ import merge from "lodash/merge";
|
|
|
3
3
|
import type {Model} from "mongoose";
|
|
4
4
|
import m2s from "mongoose-to-swagger";
|
|
5
5
|
|
|
6
|
-
import type {
|
|
6
|
+
import type {ModelRouterOptions} from "./api";
|
|
7
7
|
import {logger} from "./logger";
|
|
8
8
|
import {getOpenApiSpecForModel} from "./populate";
|
|
9
9
|
|
|
@@ -112,7 +112,7 @@ function createAPIErrorComponent(openApi: any) {
|
|
|
112
112
|
});
|
|
113
113
|
}
|
|
114
114
|
|
|
115
|
-
export function getOpenApiMiddleware<T>(model: Model<T>, options: Partial<
|
|
115
|
+
export function getOpenApiMiddleware<T>(model: Model<T>, options: Partial<ModelRouterOptions<T>>) {
|
|
116
116
|
createAPIErrorComponent(options.openApi);
|
|
117
117
|
if (!options.openApi?.path) {
|
|
118
118
|
// Just log this once rather than for each middleware.
|
|
@@ -154,7 +154,7 @@ export function getOpenApiMiddleware<T>(model: Model<T>, options: Partial<modelR
|
|
|
154
154
|
);
|
|
155
155
|
}
|
|
156
156
|
|
|
157
|
-
export function listOpenApiMiddleware<T>(model: Model<T>, options: Partial<
|
|
157
|
+
export function listOpenApiMiddleware<T>(model: Model<T>, options: Partial<ModelRouterOptions<T>>) {
|
|
158
158
|
if (!options.openApi?.path) {
|
|
159
159
|
return noop;
|
|
160
160
|
}
|
|
@@ -319,7 +319,7 @@ export function listOpenApiMiddleware<T>(model: Model<T>, options: Partial<model
|
|
|
319
319
|
|
|
320
320
|
export function createOpenApiMiddleware<T>(
|
|
321
321
|
model: Model<T>,
|
|
322
|
-
options: Partial<
|
|
322
|
+
options: Partial<ModelRouterOptions<T>>
|
|
323
323
|
) {
|
|
324
324
|
if (!options.openApi?.path) {
|
|
325
325
|
return noop;
|
|
@@ -371,7 +371,7 @@ export function createOpenApiMiddleware<T>(
|
|
|
371
371
|
|
|
372
372
|
export function patchOpenApiMiddleware<T>(
|
|
373
373
|
model: Model<T>,
|
|
374
|
-
options: Partial<
|
|
374
|
+
options: Partial<ModelRouterOptions<T>>
|
|
375
375
|
) {
|
|
376
376
|
if (!options.openApi?.path) {
|
|
377
377
|
return noop;
|
|
@@ -423,7 +423,7 @@ export function patchOpenApiMiddleware<T>(
|
|
|
423
423
|
|
|
424
424
|
export function deleteOpenApiMiddleware<T>(
|
|
425
425
|
model: Model<T>,
|
|
426
|
-
options: Partial<
|
|
426
|
+
options: Partial<ModelRouterOptions<T>>
|
|
427
427
|
) {
|
|
428
428
|
if (!options.openApi?.path) {
|
|
429
429
|
return noop;
|
|
@@ -452,7 +452,7 @@ export function deleteOpenApiMiddleware<T>(
|
|
|
452
452
|
// This is a generic OpenAPI wrapper for a read that returns any object described by `properties`.
|
|
453
453
|
// Useful for endpoints that don't directly map to a model.
|
|
454
454
|
export function readOpenApiMiddleware<T>(
|
|
455
|
-
options: Partial<
|
|
455
|
+
options: Partial<ModelRouterOptions<T>>,
|
|
456
456
|
properties: any,
|
|
457
457
|
required: string[],
|
|
458
458
|
queryParameters: any
|
|
@@ -4,14 +4,14 @@ import type {Router} from "express";
|
|
|
4
4
|
import supertest from "supertest";
|
|
5
5
|
import type TestAgent from "supertest/lib/agent";
|
|
6
6
|
|
|
7
|
-
import {
|
|
7
|
+
import {type ModelRouterOptions, modelRouter} from "./api";
|
|
8
8
|
import {addAuthRoutes, setupAuth} from "./auth";
|
|
9
9
|
import {setupServer} from "./expressServer";
|
|
10
10
|
import {createOpenApiBuilder, OpenApiMiddlewareBuilder} from "./openApiBuilder";
|
|
11
11
|
import {Permissions} from "./permissions";
|
|
12
12
|
import {FoodModel, UserModel} from "./tests";
|
|
13
13
|
|
|
14
|
-
function addRoutesWithBuilder(router: Router, options?: Partial<
|
|
14
|
+
function addRoutesWithBuilder(router: Router, options?: Partial<ModelRouterOptions<any>>): void {
|
|
15
15
|
// Add a custom endpoint using the OpenApiMiddlewareBuilder
|
|
16
16
|
const statsMiddleware = createOpenApiBuilder(options ?? {})
|
|
17
17
|
.withTags(["Stats"])
|
package/src/openApiBuilder.ts
CHANGED
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
*/
|
|
32
32
|
import merge from "lodash/merge";
|
|
33
33
|
|
|
34
|
-
import type {
|
|
34
|
+
import type {ModelRouterOptions} from "./api";
|
|
35
35
|
import {logger} from "./logger";
|
|
36
36
|
import {defaultOpenApiErrorResponses} from "./openApi";
|
|
37
37
|
|
|
@@ -250,7 +250,7 @@ interface OpenApiConfig {
|
|
|
250
250
|
*/
|
|
251
251
|
export class OpenApiMiddlewareBuilder {
|
|
252
252
|
/** Router options containing OpenAPI configuration */
|
|
253
|
-
private options: Partial<
|
|
253
|
+
private options: Partial<ModelRouterOptions<any>>;
|
|
254
254
|
|
|
255
255
|
/** Accumulated OpenAPI configuration from builder methods */
|
|
256
256
|
private config: OpenApiConfig;
|
|
@@ -260,7 +260,7 @@ export class OpenApiMiddlewareBuilder {
|
|
|
260
260
|
*
|
|
261
261
|
* @param options - Router options containing the OpenAPI path configuration
|
|
262
262
|
*/
|
|
263
|
-
constructor(options: Partial<
|
|
263
|
+
constructor(options: Partial<ModelRouterOptions<any>>) {
|
|
264
264
|
this.options = options;
|
|
265
265
|
this.config = {
|
|
266
266
|
responses: {},
|
|
@@ -630,7 +630,7 @@ export class OpenApiMiddlewareBuilder {
|
|
|
630
630
|
* ```
|
|
631
631
|
*/
|
|
632
632
|
export function createOpenApiBuilder(
|
|
633
|
-
options: Partial<
|
|
633
|
+
options: Partial<ModelRouterOptions<any>>
|
|
634
634
|
): OpenApiMiddlewareBuilder {
|
|
635
635
|
return new OpenApiMiddlewareBuilder(options);
|
|
636
636
|
}
|
package/src/permissions.ts
CHANGED
|
@@ -4,7 +4,7 @@ import type express from "express";
|
|
|
4
4
|
import type {NextFunction} from "express";
|
|
5
5
|
import mongoose, {type Model} from "mongoose";
|
|
6
6
|
|
|
7
|
-
import {addPopulateToQuery, getModel, type
|
|
7
|
+
import {addPopulateToQuery, getModel, type ModelRouterOptions, type RESTMethod} from "./api";
|
|
8
8
|
import type {User} from "./auth";
|
|
9
9
|
import {APIError} from "./errors";
|
|
10
10
|
import {logger} from "./logger";
|
|
@@ -102,7 +102,7 @@ export async function checkPermissions<T>(
|
|
|
102
102
|
// req.obj.
|
|
103
103
|
export function permissionMiddleware<T>(
|
|
104
104
|
baseModel: Model<T>,
|
|
105
|
-
options: Pick<
|
|
105
|
+
options: Pick<ModelRouterOptions<T>, "permissions" | "populatePaths" | "discriminatorKey">
|
|
106
106
|
) {
|
|
107
107
|
return async (req: express.Request, _res: express.Response, next: NextFunction) => {
|
|
108
108
|
if (req.method === "OPTIONS") {
|
package/src/transformers.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type express from "express";
|
|
2
2
|
import type {Document} from "mongoose";
|
|
3
3
|
|
|
4
|
-
import type {
|
|
4
|
+
import type {ModelRouterOptions} from "./api";
|
|
5
5
|
import type {User} from "./auth";
|
|
6
6
|
import {APIError} from "./errors";
|
|
7
7
|
import {logger} from "./logger";
|
|
@@ -88,7 +88,7 @@ export function AdminOwnerTransformer<T>(options: {
|
|
|
88
88
|
}
|
|
89
89
|
|
|
90
90
|
export function transform<T>(
|
|
91
|
-
options:
|
|
91
|
+
options: ModelRouterOptions<T>,
|
|
92
92
|
data: Partial<T> | Partial<T>[],
|
|
93
93
|
method: "create" | "update",
|
|
94
94
|
user?: User
|
|
@@ -112,7 +112,7 @@ export function transform<T>(
|
|
|
112
112
|
|
|
113
113
|
export function serialize<T>(
|
|
114
114
|
req: express.Request,
|
|
115
|
-
options:
|
|
115
|
+
options: ModelRouterOptions<T>,
|
|
116
116
|
data: (Document<any, any, any> & T) | (Document<any, any, any> & T)[]
|
|
117
117
|
) {
|
|
118
118
|
const serializeFn = (serializeData: Document<any, any, any> & T, serializeUser?: User) => {
|
|
@@ -153,7 +153,7 @@ export async function defaultResponseHandler<T>(
|
|
|
153
153
|
doc: (Document<any, any, any> & T) | (Document<any, any, any> & T)[] | null,
|
|
154
154
|
method: "list" | "create" | "read" | "update",
|
|
155
155
|
request: express.Request,
|
|
156
|
-
options:
|
|
156
|
+
options: ModelRouterOptions<T>
|
|
157
157
|
) {
|
|
158
158
|
if (!doc) {
|
|
159
159
|
return null;
|