@decaf-ts/for-nest 0.1.5 → 0.1.7
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/for-nest.cjs +1 -1
- package/dist/for-nest.cjs.map +1 -1
- package/dist/for-nest.js +1 -1
- package/dist/for-nest.js.map +1 -1
- package/lib/constants.cjs +4 -1
- package/lib/constants.d.ts +3 -0
- package/lib/constants.js.map +1 -1
- package/lib/decaf-model/DecafModelModule.cjs +8 -0
- package/lib/decaf-model/DecafModelModule.d.ts +1 -0
- package/lib/decaf-model/DecafModelModule.js.map +1 -1
- package/lib/decaf-model/FromModelController.cjs +42 -30
- package/lib/decaf-model/FromModelController.d.ts +2 -10
- package/lib/decaf-model/FromModelController.js.map +1 -1
- package/lib/decaf-model/decorators/ApiParamsFromModel.d.ts +2 -2
- package/lib/decaf-model/decorators/ApiParamsFromModel.js.map +1 -1
- package/lib/decaf-model/decorators/DecafParams.d.ts +2 -2
- package/lib/decaf-model/decorators/DecafParams.js.map +1 -1
- package/lib/decaf-model/decorators/decorators.cjs +60 -0
- package/lib/decaf-model/decorators/decorators.d.ts +46 -0
- package/lib/decaf-model/decorators/decorators.js.map +1 -0
- package/lib/decaf-model/decorators/types.d.ts +7 -1
- package/lib/esm/constants.d.ts +3 -0
- package/lib/esm/constants.js +3 -0
- package/lib/esm/constants.js.map +1 -1
- package/lib/esm/decaf-model/DecafModelModule.d.ts +1 -0
- package/lib/esm/decaf-model/DecafModelModule.js +9 -1
- package/lib/esm/decaf-model/DecafModelModule.js.map +1 -1
- package/lib/esm/decaf-model/FromModelController.d.ts +2 -10
- package/lib/esm/decaf-model/FromModelController.js +43 -31
- package/lib/esm/decaf-model/FromModelController.js.map +1 -1
- package/lib/esm/decaf-model/decorators/ApiParamsFromModel.d.ts +2 -2
- package/lib/esm/decaf-model/decorators/ApiParamsFromModel.js.map +1 -1
- package/lib/esm/decaf-model/decorators/DecafParams.d.ts +2 -2
- package/lib/esm/decaf-model/decorators/DecafParams.js.map +1 -1
- package/lib/esm/decaf-model/decorators/decorators.d.ts +46 -0
- package/lib/esm/decaf-model/decorators/decorators.js +55 -0
- package/lib/esm/decaf-model/decorators/decorators.js.map +1 -0
- package/lib/esm/decaf-model/decorators/types.d.ts +7 -1
- package/lib/esm/index.d.ts +1 -1
- package/lib/esm/index.js +1 -1
- package/lib/esm/interceptors/AuthInterceptor.d.ts +10 -0
- package/lib/esm/interceptors/AuthInterceptor.js +37 -0
- package/lib/esm/interceptors/AuthInterceptor.js.map +1 -0
- package/lib/esm/interceptors/DecafRequestHandlerInterceptor.js +2 -1
- package/lib/esm/interceptors/DecafRequestHandlerInterceptor.js.map +1 -1
- package/lib/esm/request/DecafHandlerExecutor.d.ts +1 -1
- package/lib/esm/request/DecafHandlerExecutor.js +2 -2
- package/lib/esm/request/DecafHandlerExecutor.js.map +1 -1
- package/lib/esm/types.d.ts +46 -2
- package/lib/index.cjs +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/interceptors/AuthInterceptor.cjs +40 -0
- package/lib/interceptors/AuthInterceptor.d.ts +10 -0
- package/lib/interceptors/AuthInterceptor.js.map +1 -0
- package/lib/interceptors/DecafRequestHandlerInterceptor.cjs +2 -1
- package/lib/interceptors/DecafRequestHandlerInterceptor.js.map +1 -1
- package/lib/request/DecafHandlerExecutor.cjs +2 -2
- package/lib/request/DecafHandlerExecutor.d.ts +1 -1
- package/lib/request/DecafHandlerExecutor.js.map +1 -1
- package/lib/types.d.ts +46 -2
- package/package.json +5 -2
|
@@ -12,7 +12,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
|
12
12
|
};
|
|
13
13
|
import { Body, Controller, Param } from "@nestjs/common";
|
|
14
14
|
import { ApiBadRequestResponse, ApiBody, ApiCreatedResponse, ApiExtraModels, ApiNotFoundResponse, ApiOkResponse, ApiOperation, ApiParam, ApiTags, ApiUnprocessableEntityResponse, getSchemaPath, } from "@nestjs/swagger";
|
|
15
|
-
import { Repository } from "@decaf-ts/core";
|
|
15
|
+
import { ModelService, Repository } from "@decaf-ts/core";
|
|
16
16
|
import { Model } from "@decaf-ts/decorator-validation";
|
|
17
17
|
import { LoggedClass, Logging, toKebabCase } from "@decaf-ts/logging";
|
|
18
18
|
import { DBKeys, ValidationError } from "@decaf-ts/db-decorators";
|
|
@@ -20,6 +20,7 @@ import { Metadata } from "@decaf-ts/decoration";
|
|
|
20
20
|
import { ApiOperationFromModel, ApiParamsFromModel, DecafParams, } from "./decorators/index.js";
|
|
21
21
|
import { DecafRequestContext } from "./../request/index.js";
|
|
22
22
|
import { DECAF_ADAPTER_OPTIONS } from "./../constants.js";
|
|
23
|
+
import { Auth } from "./decorators/decorators.js";
|
|
23
24
|
/**
|
|
24
25
|
* @description
|
|
25
26
|
* Factory and utilities for generating dynamic NestJS controllers from Decaf {@link Model} definitions.
|
|
@@ -77,7 +78,7 @@ export class FromModelController {
|
|
|
77
78
|
const routePath = toKebabCase(tableName);
|
|
78
79
|
const modelClazzName = ModelClazz.name;
|
|
79
80
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
80
|
-
const { description, apiProperties, path } = FromModelController.getRouteParametersFromModel(ModelClazz);
|
|
81
|
+
const { description, getPK, apiProperties, path } = FromModelController.getRouteParametersFromModel(ModelClazz);
|
|
81
82
|
log.debug(`Creating controller for model: ${modelClazzName}`);
|
|
82
83
|
let DynamicModelController = class DynamicModelController extends LoggedClass {
|
|
83
84
|
constructor(clientContext) {
|
|
@@ -86,20 +87,22 @@ export class FromModelController {
|
|
|
86
87
|
this.pk = Model.pk(ModelClazz);
|
|
87
88
|
log.info(`Registering dynamic controller for model: ${modelClazzName} route: /${routePath}`);
|
|
88
89
|
}
|
|
89
|
-
get
|
|
90
|
-
if (!this.
|
|
91
|
-
this.
|
|
90
|
+
get persistence() {
|
|
91
|
+
if (!this._persistence)
|
|
92
|
+
this._persistence =
|
|
93
|
+
ModelService.getService(ModelClazz) ||
|
|
94
|
+
Repository.forModel(ModelClazz);
|
|
92
95
|
const adapterOptions = this.clientContext.get(DECAF_ADAPTER_OPTIONS);
|
|
93
96
|
if (adapterOptions)
|
|
94
|
-
return this.
|
|
95
|
-
return this.
|
|
97
|
+
return this._persistence.for(adapterOptions);
|
|
98
|
+
return this._persistence;
|
|
96
99
|
}
|
|
97
100
|
async create(data) {
|
|
98
101
|
const log = this.log.for(this.create);
|
|
99
102
|
log.verbose(`creating new ${modelClazzName}`);
|
|
100
103
|
let created;
|
|
101
104
|
try {
|
|
102
|
-
created = await this.
|
|
105
|
+
created = await this.persistence.create(data);
|
|
103
106
|
}
|
|
104
107
|
catch (e) {
|
|
105
108
|
log.error(`Failed to create new ${modelClazzName}`, e);
|
|
@@ -108,15 +111,15 @@ export class FromModelController {
|
|
|
108
111
|
log.info(`created new ${modelClazzName} with id ${created[this.pk]}`);
|
|
109
112
|
return created;
|
|
110
113
|
}
|
|
111
|
-
async read(
|
|
112
|
-
const id =
|
|
114
|
+
async read(routeParams) {
|
|
115
|
+
const id = getPK(...routeParams.ordered);
|
|
113
116
|
if (typeof id === "undefined")
|
|
114
117
|
throw new ValidationError(`No ${this.pk} provided`);
|
|
115
118
|
const log = this.log.for(this.read);
|
|
116
119
|
let read;
|
|
117
120
|
try {
|
|
118
121
|
log.debug(`reading ${modelClazzName} with ${this.pk} ${id}`);
|
|
119
|
-
read = await this.
|
|
122
|
+
read = await this.persistence.read(id);
|
|
120
123
|
}
|
|
121
124
|
catch (e) {
|
|
122
125
|
log.error(`Failed to read ${modelClazzName} with id ${id}`, e);
|
|
@@ -130,7 +133,8 @@ export class FromModelController {
|
|
|
130
133
|
let results;
|
|
131
134
|
try {
|
|
132
135
|
log.debug(`Querying ${modelClazzName} using method "${method}"`);
|
|
133
|
-
|
|
136
|
+
const args = [method];
|
|
137
|
+
results = await this.persistence.query(...args);
|
|
134
138
|
}
|
|
135
139
|
catch (e) {
|
|
136
140
|
log.error(`Failed to query ${modelClazzName} using method "${method}"`, e);
|
|
@@ -141,10 +145,16 @@ export class FromModelController {
|
|
|
141
145
|
}
|
|
142
146
|
async update(routeParams, body) {
|
|
143
147
|
const log = this.log.for(this.update);
|
|
148
|
+
const id = getPK(...routeParams.ordered);
|
|
149
|
+
if (typeof id === "undefined")
|
|
150
|
+
throw new ValidationError(`No ${this.pk} provided`);
|
|
144
151
|
let updated;
|
|
145
152
|
try {
|
|
146
|
-
log.info(`updating ${modelClazzName} with ${this.pk} ${
|
|
147
|
-
updated = await this.
|
|
153
|
+
log.info(`updating ${modelClazzName} with ${this.pk} ${id}`);
|
|
154
|
+
updated = await this.persistence.update(new ModelClazz({
|
|
155
|
+
...body,
|
|
156
|
+
[this.pk]: id,
|
|
157
|
+
}));
|
|
148
158
|
}
|
|
149
159
|
catch (e) {
|
|
150
160
|
log.error(e);
|
|
@@ -152,24 +162,22 @@ export class FromModelController {
|
|
|
152
162
|
}
|
|
153
163
|
return updated;
|
|
154
164
|
}
|
|
155
|
-
async delete(
|
|
156
|
-
const id = pathParams[this.pk];
|
|
157
|
-
if (typeof id === "undefined")
|
|
158
|
-
throw new ValidationError(`No ${this.pk} provided`);
|
|
165
|
+
async delete(routeParams) {
|
|
159
166
|
const log = this.log.for(this.delete);
|
|
167
|
+
const id = getPK(...routeParams.ordered);
|
|
160
168
|
if (typeof id === "undefined")
|
|
161
169
|
throw new ValidationError(`No ${this.pk} provided`);
|
|
162
|
-
let
|
|
170
|
+
let del;
|
|
163
171
|
try {
|
|
164
|
-
log.debug(`deleting ${modelClazzName} with ${this.pk} ${
|
|
165
|
-
|
|
172
|
+
log.debug(`deleting ${modelClazzName} with ${this.pk} ${id}`);
|
|
173
|
+
del = await this.persistence.delete(id);
|
|
166
174
|
}
|
|
167
175
|
catch (e) {
|
|
168
|
-
log.error(`Failed to delete ${modelClazzName} with id ${
|
|
176
|
+
log.error(`Failed to delete ${modelClazzName} with id ${id}`, e);
|
|
169
177
|
throw e;
|
|
170
178
|
}
|
|
171
|
-
log.info(`deleted ${modelClazzName} with id ${
|
|
172
|
-
return
|
|
179
|
+
log.info(`deleted ${modelClazzName} with id ${id}`);
|
|
180
|
+
return del;
|
|
173
181
|
}
|
|
174
182
|
};
|
|
175
183
|
__decorate([
|
|
@@ -201,7 +209,7 @@ export class FromModelController {
|
|
|
201
209
|
ApiNotFoundResponse({
|
|
202
210
|
description: `No ${modelClazzName} record matches the provided identifier.`,
|
|
203
211
|
}),
|
|
204
|
-
__param(0,
|
|
212
|
+
__param(0, DecafParams(apiProperties)),
|
|
205
213
|
__metadata("design:type", Function),
|
|
206
214
|
__metadata("design:paramtypes", [Object]),
|
|
207
215
|
__metadata("design:returntype", Promise)
|
|
@@ -254,7 +262,7 @@ export class FromModelController {
|
|
|
254
262
|
ApiNotFoundResponse({
|
|
255
263
|
description: `No ${modelClazzName} record matches the provided identifier.`,
|
|
256
264
|
}),
|
|
257
|
-
__param(0,
|
|
265
|
+
__param(0, DecafParams(apiProperties)),
|
|
258
266
|
__metadata("design:type", Function),
|
|
259
267
|
__metadata("design:paramtypes", [Object]),
|
|
260
268
|
__metadata("design:returntype", Promise)
|
|
@@ -263,16 +271,15 @@ export class FromModelController {
|
|
|
263
271
|
Controller(routePath),
|
|
264
272
|
ApiTags(modelClazzName),
|
|
265
273
|
ApiExtraModels(ModelClazz),
|
|
274
|
+
Auth(ModelClazz),
|
|
266
275
|
__metadata("design:paramtypes", [DecafRequestContext])
|
|
267
276
|
], DynamicModelController);
|
|
268
277
|
return DynamicModelController;
|
|
269
278
|
}
|
|
270
279
|
static getRouteParametersFromModel(ModelClazz) {
|
|
271
|
-
const instance = new ModelClazz({});
|
|
272
280
|
const pk = Model.pk(ModelClazz);
|
|
273
|
-
const
|
|
274
|
-
const composedKeys =
|
|
275
|
-
[];
|
|
281
|
+
const composed = Metadata.get(ModelClazz, Metadata.key(DBKeys.COMPOSED, pk));
|
|
282
|
+
const composedKeys = composed?.args ?? [];
|
|
276
283
|
const keysToReturn = Array.isArray(composedKeys) && composedKeys.length > 0
|
|
277
284
|
? [...composedKeys]
|
|
278
285
|
: [pk];
|
|
@@ -288,7 +295,12 @@ export class FromModelController {
|
|
|
288
295
|
type: String,
|
|
289
296
|
};
|
|
290
297
|
});
|
|
291
|
-
return {
|
|
298
|
+
return {
|
|
299
|
+
path,
|
|
300
|
+
description,
|
|
301
|
+
apiProperties,
|
|
302
|
+
getPK: (...params) => composed?.separator ? params.join(composed.separator) : params.join(""),
|
|
303
|
+
};
|
|
292
304
|
}
|
|
293
305
|
}
|
|
294
306
|
//# sourceMappingURL=FromModelController.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FromModelController.js","sourceRoot":"","sources":["../../../src/decaf-model/FromModelController.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACzD,OAAO,EACL,qBAAqB,EACrB,OAAO,EACP,kBAAkB,EAClB,cAAc,EACd,mBAAmB,EACnB,aAAa,EACb,YAAY,EACZ,QAAQ,EACR,OAAO,EACP,8BAA8B,EAC9B,aAAa,GACd,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAQ,UAAU,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"FromModelController.js","sourceRoot":"","sources":["../../../src/decaf-model/FromModelController.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACzD,OAAO,EACL,qBAAqB,EACrB,OAAO,EACP,kBAAkB,EAClB,cAAc,EACd,mBAAmB,EACnB,aAAa,EACb,YAAY,EACZ,QAAQ,EACR,OAAO,EACP,8BAA8B,EAC9B,aAAa,GACd,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,YAAY,EAAQ,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAChE,OAAO,EAAE,KAAK,EAAoB,MAAM,gCAAgC,CAAC;AACzE,OAAO,E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G,UAAU,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,GAAG,EAAE,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAC1D,MAAM,aAAa,GAAyB,UAAU,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE;YACjE,OAAO;gBACL,IAAI,EAAE,GAAG;gBACT,WAAW,EAAE,QAAQ,CAAC,WAAW,CAAC,UAAU,EAAE,GAAG,CAAC;gBAClD,QAAQ,EAAE,IAAI;gBACd,IAAI,EAAE,MAAM;aACb,CAAC;QACJ,CAAC,CAAC,CAAC;QAEH,OAAO;YACL,IAAI;YACJ,WAAW;YACX,aAAa;YACb,KAAK,EAAE,CAAC,GAAG,MAA8B,EAAE,EAAE,CAC3C,QAAQ,EAAE,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;SAC1E,CAAC;IACJ,CAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { DecafApiProperties } from "./types";
|
|
2
2
|
/**
|
|
3
3
|
* @description Applies a set of Swagger `@ApiParam` decorators generated from a typed specification array.
|
|
4
4
|
* @summary Transforms each entry of the provided `ApiParam[]` into a corresponding `@ApiParam()` decorator (defaulting `description`, `required`, and `type` when omitted) and composes them into a single decorator via Nest's `applyDecorators`. Useful for synchronizing runtime route params with OpenAPI documentation in a concise, declarative way.
|
|
@@ -10,4 +10,4 @@ import { ApiParam } from "./types";
|
|
|
10
10
|
* @return {any} A composed decorator applying all generated `@ApiParam` decorators to the target method or controller.
|
|
11
11
|
* @function ApiParamsFromModel
|
|
12
12
|
*/
|
|
13
|
-
export declare function ApiParamsFromModel(props?:
|
|
13
|
+
export declare function ApiParamsFromModel(props?: DecafApiProperties[]): MethodDecorator & ClassDecorator;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ApiParamsFromModel.js","sourceRoot":"","sources":["../../../../src/decaf-model/decorators/ApiParamsFromModel.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACjD,OAAO,EAAE,QAAQ,IAAI,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAG1D;;;;;;;;;;GAUG;AACH,MAAM,UAAU,kBAAkB,CAChC,
|
|
1
|
+
{"version":3,"file":"ApiParamsFromModel.js","sourceRoot":"","sources":["../../../../src/decaf-model/decorators/ApiParamsFromModel.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACjD,OAAO,EAAE,QAAQ,IAAI,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAG1D;;;;;;;;;;GAUG;AACH,MAAM,UAAU,kBAAkB,CAChC,QAA8B,EAAE;IAEhC,MAAM,UAAU,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CACjC,WAAW,CAAC;QACV,IAAI,EAAE,CAAC,CAAC,IAAI;QACZ,WAAW,EAAE,CAAC,CAAC,WAAW,IAAI,mBAAmB,CAAC,CAAC,IAAI,EAAE;QACzD,QAAQ,EAAE,CAAC,CAAC,QAAQ,IAAI,IAAI;QAC5B,8BAA8B;QAC9B,IAAI,EAAE,CAAC,CAAC,IAAI,IAAI,MAAM;KACvB,CAAC,CACH,CAAC;IACF,OAAO,eAAe,CAAC,GAAG,UAAU,CAAC,CAAC;AACxC,CAAC"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DecafApiProperties } from "./types";
|
|
2
2
|
/**
|
|
3
3
|
* @description A higher-level decorator factory that leverages `OrderedParams` to extract route parameters in a specific order derived from a list of `ApiParam` definitions.
|
|
4
4
|
* @summary `DecafParams` computes the order of route parameters based on the provided `ApiParam[]` specification (using each element’s `name`), then applies `OrderedParams(order)` as a parameter decorator. This enables parameter-level binding that remains consistent with the documented API parameter metadata.
|
|
5
5
|
* @param {ApiParam[]} [props=[]] Array of `ApiParam` definitions whose `name` fields determine the parameter extraction order.
|
|
6
6
|
* @return {ParameterDecorator} A NestJS parameter decorator that injects an ordered list of parameters and metadata into the controller method argument.
|
|
7
7
|
*/
|
|
8
|
-
export declare function DecafParams(props?:
|
|
8
|
+
export declare function DecafParams(props?: DecafApiProperties[]): ParameterDecorator;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DecafParams.js","sourceRoot":"","sources":["../../../../src/decaf-model/decorators/DecafParams.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAoB,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"DecafParams.js","sourceRoot":"","sources":["../../../../src/decaf-model/decorators/DecafParams.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAoB,MAAM,gBAAgB,CAAC;AAExE;;;;;;;;;;;GAWG;AACH,MAAM,aAAa,GAAG,oBAAoB,CACxC,CAAC,KAA2B,EAAE,GAAqB,EAAmB,EAAE;IACtE,MAAM,GAAG,GAAG,GAAG,CAAC,YAAY,EAAE,CAAC,UAAU,EAAE,CAAC;IAC5C,MAAM,QAAQ,GAAG,CAAC,GAAG,EAAE,MAAM,IAAI,EAAE,CAAwB,CAAC;IAC5D,MAAM,SAAS,GAAG,KAAK,IAAI,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IACjD,MAAM,OAAO,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC;IAClD,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC;AACjD,CAAC,CACF,CAAC;AAEF;;;;;GAKG;AACH,MAAM,UAAU,WAAW,CACzB,QAA8B,EAAE;IAEhC,MAAM,KAAK,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;IACvC,OAAO,aAAa,CAAC,KAAK,CAAC,CAAC;AAC9B,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Constructor } from "@decaf-ts/decoration";
|
|
2
|
+
/**
|
|
3
|
+
* A decorator function that applies authentication and authorization metadata to a NestJS controller or method.
|
|
4
|
+
*
|
|
5
|
+
* @param model - The model name or constructor function for the resource being accessed.
|
|
6
|
+
* If a string is provided, it is used as the resource name.
|
|
7
|
+
* If a constructor function is provided, its name is used as the resource name.
|
|
8
|
+
*
|
|
9
|
+
* @returns - A function that applies the authentication and authorization decorators to the target.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* @Controller('users')
|
|
14
|
+
* @Auth('User')
|
|
15
|
+
* export class UsersController {
|
|
16
|
+
* // ...
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* @Controller('users')
|
|
23
|
+
* @Auth(User)
|
|
24
|
+
* export class UsersController {
|
|
25
|
+
* // ...
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare function Auth(model: string | Constructor): <TFunction extends Function, Y>(target: TFunction | object, propertyKey?: string | symbol, descriptor?: TypedPropertyDescriptor<Y>) => void;
|
|
30
|
+
/**
|
|
31
|
+
* A decorator function that sets the roles required for authentication and authorization to the model in NestJS.
|
|
32
|
+
*
|
|
33
|
+
* @param roles - An array of role names required for access.
|
|
34
|
+
*
|
|
35
|
+
* @returns - A function that applies the role decorators to the target.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```typescript
|
|
39
|
+
* @model('users')
|
|
40
|
+
* @Roles(['admin'])
|
|
41
|
+
* export class UserModel {
|
|
42
|
+
* //...
|
|
43
|
+
* }
|
|
44
|
+
*
|
|
45
|
+
*/
|
|
46
|
+
export declare const Roles: (roles: string[]) => (model: any, prop?: any, descriptor?: PropertyDescriptor | number) => void;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { applyDecorators, SetMetadata, UseInterceptors } from "@nestjs/common";
|
|
2
|
+
import { AUTH_META_KEY, AuthRole } from "./../../constants.js";
|
|
3
|
+
import { AuthInterceptor } from "./../../interceptors/AuthInterceptor.js";
|
|
4
|
+
import { metadata } from "@decaf-ts/decoration";
|
|
5
|
+
/**
|
|
6
|
+
* A decorator function that applies authentication and authorization metadata to a NestJS controller or method.
|
|
7
|
+
*
|
|
8
|
+
* @param model - The model name or constructor function for the resource being accessed.
|
|
9
|
+
* If a string is provided, it is used as the resource name.
|
|
10
|
+
* If a constructor function is provided, its name is used as the resource name.
|
|
11
|
+
*
|
|
12
|
+
* @returns - A function that applies the authentication and authorization decorators to the target.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* @Controller('users')
|
|
17
|
+
* @Auth('User')
|
|
18
|
+
* export class UsersController {
|
|
19
|
+
* // ...
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* @Controller('users')
|
|
26
|
+
* @Auth(User)
|
|
27
|
+
* export class UsersController {
|
|
28
|
+
* // ...
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export function Auth(model) {
|
|
33
|
+
const resource = typeof model === "string" ? model : model.name;
|
|
34
|
+
return applyDecorators(SetMetadata(AUTH_META_KEY, resource), UseInterceptors(AuthInterceptor));
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* A decorator function that sets the roles required for authentication and authorization to the model in NestJS.
|
|
38
|
+
*
|
|
39
|
+
* @param roles - An array of role names required for access.
|
|
40
|
+
*
|
|
41
|
+
* @returns - A function that applies the role decorators to the target.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* @model('users')
|
|
46
|
+
* @Roles(['admin'])
|
|
47
|
+
* export class UserModel {
|
|
48
|
+
* //...
|
|
49
|
+
* }
|
|
50
|
+
*
|
|
51
|
+
*/
|
|
52
|
+
export const Roles = (roles) => {
|
|
53
|
+
return metadata(AuthRole, roles);
|
|
54
|
+
};
|
|
55
|
+
//# sourceMappingURL=decorators.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decorators.js","sourceRoot":"","sources":["../../../../src/decaf-model/decorators/decorators.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAE/E,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAE,6BAAwB;AAC1D,OAAO,EAAE,eAAe,EAAE,gDAA2C;AACrE,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAEhD;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,UAAU,IAAI,CAAC,KAA2B;IAC9C,MAAM,QAAQ,GAAG,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC;IAEhE,OAAO,eAAe,CACpB,WAAW,CAAC,aAAa,EAAE,QAAQ,CAAC,EACpC,eAAe,CAAC,eAAe,CAAC,CACjC,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;;;GAeG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG,CAAC,KAAe,EAAE,EAAE;IACvC,OAAO,QAAQ,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;AACnC,CAAC,CAAC"}
|
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
export type HttpVerbs = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
2
|
-
export type
|
|
2
|
+
export type DecafApiProperties = {
|
|
3
3
|
name: string;
|
|
4
4
|
description?: string;
|
|
5
5
|
required?: boolean;
|
|
6
6
|
type?: any;
|
|
7
7
|
};
|
|
8
|
+
export type DecafModelRoute = {
|
|
9
|
+
path: string;
|
|
10
|
+
description?: string;
|
|
11
|
+
apiProperties: DecafApiProperties[];
|
|
12
|
+
getPK: (...args: Array<string | number>) => string;
|
|
13
|
+
};
|
|
8
14
|
export type DecafParamProps = {
|
|
9
15
|
original: Record<string, any>;
|
|
10
16
|
ordered: any[];
|
package/lib/esm/index.d.ts
CHANGED
package/lib/esm/index.js
CHANGED
|
@@ -36,7 +36,7 @@ export * from "./request/index.js";
|
|
|
36
36
|
* @constant
|
|
37
37
|
* @type {string}
|
|
38
38
|
*/
|
|
39
|
-
export const VERSION = "0.1.
|
|
39
|
+
export const VERSION = "0.1.7";
|
|
40
40
|
export const PACKAGE_NAME = "@decaf-ts/for-nest";
|
|
41
41
|
Metadata.registerLibrary(PACKAGE_NAME, VERSION);
|
|
42
42
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { NestInterceptor, ExecutionContext, CallHandler } from "@nestjs/common";
|
|
2
|
+
import { Reflector } from "@nestjs/core";
|
|
3
|
+
import { Observable } from "rxjs";
|
|
4
|
+
import type { AuthHandler } from "../types";
|
|
5
|
+
export declare class AuthInterceptor implements NestInterceptor {
|
|
6
|
+
private readonly reflector;
|
|
7
|
+
private readonly authHandler?;
|
|
8
|
+
constructor(reflector: Reflector, authHandler?: AuthHandler | undefined);
|
|
9
|
+
intercept(ctx: ExecutionContext, next: CallHandler): Promise<Observable<any>>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
11
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
12
|
+
};
|
|
13
|
+
import { Injectable, Inject, Optional, } from "@nestjs/common";
|
|
14
|
+
import { Reflector } from "@nestjs/core";
|
|
15
|
+
import { AUTH_HANDLER, AUTH_META_KEY } from "./../constants.js";
|
|
16
|
+
let AuthInterceptor = class AuthInterceptor {
|
|
17
|
+
constructor(reflector, authHandler) {
|
|
18
|
+
this.reflector = reflector;
|
|
19
|
+
this.authHandler = authHandler;
|
|
20
|
+
}
|
|
21
|
+
async intercept(ctx, next) {
|
|
22
|
+
const modelName = this.reflector.get(AUTH_META_KEY, ctx.getHandler()) ??
|
|
23
|
+
this.reflector.get(AUTH_META_KEY, ctx.getClass());
|
|
24
|
+
if (modelName && this.authHandler) {
|
|
25
|
+
await this.authHandler.authorize(ctx, modelName);
|
|
26
|
+
}
|
|
27
|
+
return next.handle();
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
AuthInterceptor = __decorate([
|
|
31
|
+
Injectable(),
|
|
32
|
+
__param(1, Optional()),
|
|
33
|
+
__param(1, Inject(AUTH_HANDLER)),
|
|
34
|
+
__metadata("design:paramtypes", [Reflector, Object])
|
|
35
|
+
], AuthInterceptor);
|
|
36
|
+
export { AuthInterceptor };
|
|
37
|
+
//# sourceMappingURL=AuthInterceptor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AuthInterceptor.js","sourceRoot":"","sources":["../../../src/interceptors/AuthInterceptor.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EACL,UAAU,EAIV,MAAM,EACN,QAAQ,GACT,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAIzC,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,0BAAqB;AAIpD,IAAM,eAAe,GAArB,MAAM,eAAe;IAC1B,YACmB,SAAoB,EACc,WAAyB;QAD3D,cAAS,GAAT,SAAS,CAAW;QACc,gBAAW,GAAX,WAAW,CAAc;IAC3E,CAAC;IAEJ,KAAK,CAAC,SAAS,CACb,GAAqB,EACrB,IAAiB;QAEjB,MAAM,SAAS,GACb,IAAI,CAAC,SAAS,CAAC,GAAG,CAChB,aAAa,EACb,GAAG,CAAC,UAAU,EAAE,CACjB;YACD,IAAI,CAAC,SAAS,CAAC,GAAG,CAAuB,aAAa,EAAE,GAAG,CAAC,QAAQ,EAAE,CAAC,CAAC;QAE1E,IAAI,SAAS,IAAI,IAAI,CAAC,WAAW,EAAE,CAAC;YAClC,MAAM,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;QACnD,CAAC;QAED,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC;IACvB,CAAC;CACF,CAAA;AAvBY,eAAe;IAD3B,UAAU,EAAE;IAIR,WAAA,QAAQ,EAAE,CAAA;IAAE,WAAA,MAAM,CAAC,YAAY,CAAC,CAAA;qCADL,SAAS;GAF5B,eAAe,CAuB3B"}
|
|
@@ -61,7 +61,8 @@ let DecafRequestHandlerInterceptor = class DecafRequestHandlerInterceptor {
|
|
|
61
61
|
}
|
|
62
62
|
async intercept(ctx, next) {
|
|
63
63
|
const req = ctx.switchToHttp().getRequest();
|
|
64
|
-
|
|
64
|
+
const res = ctx.switchToHttp().getResponse();
|
|
65
|
+
await this.executor.exec(req, res);
|
|
65
66
|
return next.handle();
|
|
66
67
|
}
|
|
67
68
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DecafRequestHandlerInterceptor.js","sourceRoot":"","sources":["../../../src/interceptors/DecafRequestHandlerInterceptor.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAGL,UAAU,EAEV,KAAK,GACN,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,oBAAoB,EAAE,8BAAmB;AAElD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AAEI,IAAM,8BAA8B,GAApC,MAAM,8BAA8B;IACzC,YAA6B,QAA8B;QAA9B,aAAQ,GAAR,QAAQ,CAAsB;IAAG,CAAC;IAE/D,KAAK,CAAC,SAAS,CAAC,GAAqB,EAAE,IAAiB;QACtD,MAAM,GAAG,GAAG,GAAG,CAAC,YAAY,EAAE,CAAC,UAAU,EAAE,CAAC;QAC5C,MAAM,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"DecafRequestHandlerInterceptor.js","sourceRoot":"","sources":["../../../src/interceptors/DecafRequestHandlerInterceptor.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAGL,UAAU,EAEV,KAAK,GACN,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,oBAAoB,EAAE,8BAAmB;AAElD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AAEI,IAAM,8BAA8B,GAApC,MAAM,8BAA8B;IACzC,YAA6B,QAA8B;QAA9B,aAAQ,GAAR,QAAQ,CAAsB;IAAG,CAAC;IAE/D,KAAK,CAAC,SAAS,CAAC,GAAqB,EAAE,IAAiB;QACtD,MAAM,GAAG,GAAG,GAAG,CAAC,YAAY,EAAE,CAAC,UAAU,EAAE,CAAC;QAC5C,MAAM,GAAG,GAAG,GAAG,CAAC,YAAY,EAAE,CAAC,WAAW,EAAE,CAAC;QAC7C,MAAM,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;QACnC,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC;IACvB,CAAC;CACF,CAAA;AATY,8BAA8B;IAD1C,UAAU,CAAC,EAAE,KAAK,EAAE,KAAK,CAAC,OAAO,EAAE,CAAC;qCAEI,oBAAoB;GADhD,8BAA8B,CAS1C"}
|
|
@@ -48,5 +48,5 @@ export declare class DecafHandlerExecutor {
|
|
|
48
48
|
private readonly handlers;
|
|
49
49
|
private readonly context;
|
|
50
50
|
constructor(handlers: DecafRequestHandler[], context: DecafRequestContext);
|
|
51
|
-
exec(req: Request): Promise<void>;
|
|
51
|
+
exec(req: Request, res: Response): Promise<void>;
|
|
52
52
|
}
|
|
@@ -62,9 +62,9 @@ let DecafHandlerExecutor = class DecafHandlerExecutor {
|
|
|
62
62
|
this.handlers = handlers;
|
|
63
63
|
this.context = context;
|
|
64
64
|
}
|
|
65
|
-
async exec(req) {
|
|
65
|
+
async exec(req, res) {
|
|
66
66
|
for (const handler of this.handlers) {
|
|
67
|
-
await handler.handle(this.context, req);
|
|
67
|
+
await handler.handle(this.context, req, res);
|
|
68
68
|
}
|
|
69
69
|
}
|
|
70
70
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DecafHandlerExecutor.js","sourceRoot":"","sources":["../../../src/request/DecafHandlerExecutor.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAC3D,OAAO,EAAE,mBAAmB,EAAE,iCAA8B;AAE5D,OAAO,EAAE,cAAc,EAAE,0BAAqB;AAE9C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2CG;AAEI,IAAM,oBAAoB,GAA1B,MAAM,oBAAoB;IAC/B,YAC2C,QAA+B,EACvD,OAA4B;QADJ,aAAQ,GAAR,QAAQ,CAAuB;QACvD,YAAO,GAAP,OAAO,CAAqB;IAC5C,CAAC;IAEJ,KAAK,CAAC,IAAI,CAAC,GAAY;
|
|
1
|
+
{"version":3,"file":"DecafHandlerExecutor.js","sourceRoot":"","sources":["../../../src/request/DecafHandlerExecutor.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAC3D,OAAO,EAAE,mBAAmB,EAAE,iCAA8B;AAE5D,OAAO,EAAE,cAAc,EAAE,0BAAqB;AAE9C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2CG;AAEI,IAAM,oBAAoB,GAA1B,MAAM,oBAAoB;IAC/B,YAC2C,QAA+B,EACvD,OAA4B;QADJ,aAAQ,GAAR,QAAQ,CAAuB;QACvD,YAAO,GAAP,OAAO,CAAqB;IAC5C,CAAC;IAEJ,KAAK,CAAC,IAAI,CAAC,GAAY,EAAE,GAAa;QACpC,KAAK,MAAM,OAAO,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;YACpC,MAAM,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC;QAC/C,CAAC;IACH,CAAC;CACF,CAAA;AAXY,oBAAoB;IADhC,UAAU,CAAC,EAAE,KAAK,EAAE,KAAK,CAAC,OAAO,EAAE,CAAC;IAGhC,WAAA,MAAM,CAAC,cAAc,CAAC,CAAA;4CACG,mBAAmB;GAHpC,oBAAoB,CAWhC"}
|
package/lib/esm/types.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { Adapter } from "@decaf-ts/core";
|
|
2
2
|
import { Constructor } from "@decaf-ts/decoration";
|
|
3
|
-
import { Type } from "@nestjs/common";
|
|
3
|
+
import { ExecutionContext, Type } from "@nestjs/common";
|
|
4
4
|
export interface RequestContextAccessor {
|
|
5
5
|
set(key: string | symbol, value: any): void;
|
|
6
6
|
get<T = any>(key: string | symbol): T | undefined;
|
|
7
7
|
}
|
|
8
8
|
export interface DecafRequestHandler {
|
|
9
|
-
handle(context: RequestContextAccessor, req: Request): Promise<void>;
|
|
9
|
+
handle(context: RequestContextAccessor, req: Request, res: Response): Promise<void>;
|
|
10
10
|
}
|
|
11
11
|
/**
|
|
12
12
|
* @publicApi
|
|
@@ -18,3 +18,47 @@ export type DecafModuleOptions<CONF = any, A extends Adapter<CONF, any, any, any
|
|
|
18
18
|
autoControllers: boolean;
|
|
19
19
|
handlers?: Type<DecafRequestHandler>[];
|
|
20
20
|
};
|
|
21
|
+
/**
|
|
22
|
+
* Abstraction used by the {@link AuthInterceptor} to authorize decaf models.
|
|
23
|
+
*
|
|
24
|
+
* // costumauthHandler.ts
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* export class CustomAuthHandler implements AuthHandler {
|
|
28
|
+
* async authorize(ctx: ExecutionContext, resource: string) {
|
|
29
|
+
* const req = ctx.switchToHttp().getRequest();
|
|
30
|
+
* const userRole = req.headers.authorization?.split(" ")[1] as string;
|
|
31
|
+
* if (!userRole) throw new AuthorizationError("Unauthenticated");
|
|
32
|
+
* const roles = Metadata.get(Model.get(resource)!, AuthRole);
|
|
33
|
+
* if (!roles.includes(userRole)) {
|
|
34
|
+
* throw new AuthorizationError("Unauthorized");
|
|
35
|
+
* }
|
|
36
|
+
* }
|
|
37
|
+
* }
|
|
38
|
+
*
|
|
39
|
+
* // auth.module.ts
|
|
40
|
+
*
|
|
41
|
+
* @Global()
|
|
42
|
+
* @Module({
|
|
43
|
+
* providers: [
|
|
44
|
+
* AuthInterceptor,
|
|
45
|
+
* CustomAuthHandler,
|
|
46
|
+
* {
|
|
47
|
+
* provide: AUTH_HANDLER,
|
|
48
|
+
* useClass: CustomAuthHandler, //swap this to use another provider
|
|
49
|
+
* },
|
|
50
|
+
* ],
|
|
51
|
+
* exports: [AUTH_HANDLER, AuthInterceptor],
|
|
52
|
+
* })
|
|
53
|
+
* export class AuthModule {}
|
|
54
|
+
*/
|
|
55
|
+
export interface AuthHandler {
|
|
56
|
+
/**
|
|
57
|
+
* Inspect the request context and ensure the caller can access the model.
|
|
58
|
+
* Implementations should throw an {@link AuthorizationError} on denial.
|
|
59
|
+
*
|
|
60
|
+
* @param ctx - Nest execution context that exposes the request/response.
|
|
61
|
+
* @param model - Model name or constructor being accessed.
|
|
62
|
+
*/
|
|
63
|
+
authorize(ctx: ExecutionContext, model: string | Constructor): Promise<void> | void;
|
|
64
|
+
}
|
package/lib/index.cjs
CHANGED
|
@@ -53,7 +53,7 @@ __exportStar(require("./request/index.cjs"), exports);
|
|
|
53
53
|
* @constant
|
|
54
54
|
* @type {string}
|
|
55
55
|
*/
|
|
56
|
-
exports.VERSION = "0.1.
|
|
56
|
+
exports.VERSION = "0.1.7";
|
|
57
57
|
exports.PACKAGE_NAME = "@decaf-ts/for-nest";
|
|
58
58
|
decoration_1.Metadata.registerLibrary(exports.PACKAGE_NAME, exports.VERSION);
|
|
59
59
|
//# sourceMappingURL=index.js.map
|
package/lib/index.d.ts
CHANGED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.AuthInterceptor = void 0;
|
|
16
|
+
const common_1 = require("@nestjs/common");
|
|
17
|
+
const core_1 = require("@nestjs/core");
|
|
18
|
+
const constants_1 = require("./../constants.cjs");
|
|
19
|
+
let AuthInterceptor = class AuthInterceptor {
|
|
20
|
+
constructor(reflector, authHandler) {
|
|
21
|
+
this.reflector = reflector;
|
|
22
|
+
this.authHandler = authHandler;
|
|
23
|
+
}
|
|
24
|
+
async intercept(ctx, next) {
|
|
25
|
+
const modelName = this.reflector.get(constants_1.AUTH_META_KEY, ctx.getHandler()) ??
|
|
26
|
+
this.reflector.get(constants_1.AUTH_META_KEY, ctx.getClass());
|
|
27
|
+
if (modelName && this.authHandler) {
|
|
28
|
+
await this.authHandler.authorize(ctx, modelName);
|
|
29
|
+
}
|
|
30
|
+
return next.handle();
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
exports.AuthInterceptor = AuthInterceptor;
|
|
34
|
+
exports.AuthInterceptor = AuthInterceptor = __decorate([
|
|
35
|
+
(0, common_1.Injectable)(),
|
|
36
|
+
__param(1, (0, common_1.Optional)()),
|
|
37
|
+
__param(1, (0, common_1.Inject)(constants_1.AUTH_HANDLER)),
|
|
38
|
+
__metadata("design:paramtypes", [core_1.Reflector, Object])
|
|
39
|
+
], AuthInterceptor);
|
|
40
|
+
//# sourceMappingURL=AuthInterceptor.js.map
|