@miguelmorales13/nestkit 0.11.0 → 0.16.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 +469 -9
- package/dist/audit/audit.module.d.ts +23 -0
- package/dist/audit/audit.options.d.ts +16 -0
- package/dist/audit/audit.port.d.ts +37 -0
- package/dist/audit/audit.service.d.ts +29 -0
- package/dist/audit/index.cjs +72 -0
- package/dist/audit/index.d.ts +7 -0
- package/dist/audit/index.js +72 -0
- package/dist/audit/sinks.d.ts +18 -0
- package/dist/auth/index.cjs +4 -3
- package/dist/auth/index.js +2 -1
- package/dist/auth/oauth/index.cjs +6 -5
- package/dist/auth/oauth/index.js +2 -1
- package/dist/bootstrap/index.cjs +6 -5
- package/dist/bootstrap/index.js +5 -4
- package/dist/chunk-47FXU74G.cjs +205 -0
- package/dist/{chunk-54ZXIB5T.cjs → chunk-4GT66KQS.cjs} +4 -4
- package/dist/{chunk-J7TURDAL.cjs → chunk-67B7IYAV.cjs} +2 -2
- package/dist/{chunk-NVCI3CQI.cjs → chunk-C5DV4RAX.cjs} +6 -1
- package/dist/{chunk-KDAA6GFF.js → chunk-DABTRWA6.js} +12 -7
- package/dist/{chunk-ZA56XBCK.cjs → chunk-EBPEMVZU.cjs} +15 -0
- package/dist/{chunk-EYURGACO.js → chunk-JV4EAOF2.js} +1 -1
- package/dist/chunk-KP2ZHJSR.js +35 -0
- package/dist/{chunk-7I2Y7V52.js → chunk-KRC47OX6.js} +1 -1
- package/dist/chunk-LK3OINS7.js +205 -0
- package/dist/{chunk-A3B2EY4V.js → chunk-MLOQDUBN.js} +6 -1
- package/dist/chunk-TCAMLNUZ.js +27 -0
- package/dist/{chunk-V2M75FN3.cjs → chunk-U7QK24HL.cjs} +2 -2
- package/dist/{chunk-AOCF5QCZ.js → chunk-UIVBPNH4.js} +4 -4
- package/dist/{chunk-TJHRABML.cjs → chunk-UWDHAZVE.cjs} +12 -7
- package/dist/chunk-XHW4CTBS.js +57 -0
- package/dist/chunk-XX5ROVSC.cjs +57 -0
- package/dist/chunk-Y72D5L6I.cjs +27 -0
- package/dist/crud/crud.controller.d.ts +3 -1
- package/dist/crud/crud.service.d.ts +63 -4
- package/dist/crud/index.cjs +7 -2
- package/dist/crud/index.d.ts +2 -1
- package/dist/crud/index.js +6 -1
- package/dist/crud/list-query.dto.d.ts +23 -0
- package/dist/crud/repository.port.d.ts +33 -1
- package/dist/errors/index.cjs +4 -4
- package/dist/errors/index.js +4 -4
- package/dist/idempotency/idempotency.interceptor.d.ts +35 -0
- package/dist/idempotency/idempotency.module.d.ts +27 -0
- package/dist/idempotency/idempotency.options.d.ts +29 -0
- package/dist/idempotency/idempotency.port.d.ts +43 -0
- package/dist/idempotency/index.cjs +206 -0
- package/dist/idempotency/index.d.ts +7 -0
- package/dist/idempotency/index.js +206 -0
- package/dist/idempotency/memory.store.d.ts +24 -0
- package/dist/index.cjs +26 -15
- package/dist/index.js +27 -16
- package/dist/invoicing/adapters/facturama.invoicing.d.ts +50 -0
- package/dist/invoicing/adapters/facturapi.invoicing.d.ts +35 -0
- package/dist/invoicing/amounts.d.ts +52 -0
- package/dist/invoicing/index.cjs +577 -0
- package/dist/invoicing/index.d.ts +11 -0
- package/dist/invoicing/index.js +577 -0
- package/dist/invoicing/invoicing.module.d.ts +42 -0
- package/dist/invoicing/invoicing.options.d.ts +51 -0
- package/dist/invoicing/invoicing.port.d.ts +156 -0
- package/dist/invoicing/lines.d.ts +21 -0
- package/dist/invoicing/provider-http.d.ts +21 -0
- package/dist/payments/adapters/clip.terminal.d.ts +101 -0
- package/dist/payments/adapters/mercadopago.terminal.d.ts +74 -0
- package/dist/payments/index.cjs +795 -0
- package/dist/payments/index.d.ts +12 -0
- package/dist/payments/index.js +795 -0
- package/dist/payments/money.d.ts +20 -0
- package/dist/payments/payments-webhook.controller.d.ts +30 -0
- package/dist/payments/payments.module.d.ts +45 -0
- package/dist/payments/payments.options.d.ts +84 -0
- package/dist/payments/payments.port.d.ts +174 -0
- package/dist/payments/provider-http.d.ts +26 -0
- package/dist/response/api-response.d.ts +7 -0
- package/dist/response/index.cjs +12 -4
- package/dist/response/index.d.ts +2 -0
- package/dist/response/index.js +12 -4
- package/dist/response/paginated.d.ts +25 -0
- package/dist/response/response.interceptor.d.ts +4 -0
- package/dist/tracking/index.cjs +3 -3
- package/dist/tracking/index.d.ts +1 -1
- package/dist/tracking/index.js +2 -2
- package/dist/tracking/request-context.d.ts +22 -0
- package/package.json +25 -3
- package/dist/chunk-3CLYZC3T.cjs +0 -104
- package/dist/chunk-EBO6UKHL.js +0 -20
- package/dist/chunk-JOVBJDJ2.js +0 -104
- /package/dist/{chunk-MR2IFCZE.cjs → chunk-H2EHN366.cjs} +0 -0
- /package/dist/{chunk-NAK4WDKS.js → chunk-ODPSIFOB.js} +0 -0
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import {
|
|
2
|
+
withRequestContext
|
|
3
|
+
} from "./chunk-XHW4CTBS.js";
|
|
4
|
+
import {
|
|
5
|
+
buildPageMeta,
|
|
6
|
+
paginated
|
|
7
|
+
} from "./chunk-TCAMLNUZ.js";
|
|
8
|
+
import {
|
|
9
|
+
__decorateClass,
|
|
10
|
+
__decorateParam
|
|
11
|
+
} from "./chunk-4MGIQFAJ.js";
|
|
12
|
+
|
|
13
|
+
// src/crud/list-query.dto.ts
|
|
14
|
+
import { Type } from "class-transformer";
|
|
15
|
+
import { IsIn, IsInt, IsOptional, IsString, Max, Min } from "class-validator";
|
|
16
|
+
var ListQuery = class {
|
|
17
|
+
};
|
|
18
|
+
__decorateClass([
|
|
19
|
+
IsOptional(),
|
|
20
|
+
Type(() => Number),
|
|
21
|
+
IsInt(),
|
|
22
|
+
Min(1)
|
|
23
|
+
], ListQuery.prototype, "page", 2);
|
|
24
|
+
__decorateClass([
|
|
25
|
+
IsOptional(),
|
|
26
|
+
Type(() => Number),
|
|
27
|
+
IsInt(),
|
|
28
|
+
Min(1),
|
|
29
|
+
Max(200)
|
|
30
|
+
], ListQuery.prototype, "limit", 2);
|
|
31
|
+
__decorateClass([
|
|
32
|
+
IsOptional(),
|
|
33
|
+
IsString()
|
|
34
|
+
], ListQuery.prototype, "sortBy", 2);
|
|
35
|
+
__decorateClass([
|
|
36
|
+
IsOptional(),
|
|
37
|
+
IsIn(["asc", "desc"])
|
|
38
|
+
], ListQuery.prototype, "sortOrder", 2);
|
|
39
|
+
|
|
40
|
+
// src/crud/crud.service.ts
|
|
41
|
+
var BaseCrudService = class {
|
|
42
|
+
constructor(repository, options = {}) {
|
|
43
|
+
this.repository = repository;
|
|
44
|
+
this.options = options;
|
|
45
|
+
}
|
|
46
|
+
findById(id, tenantId) {
|
|
47
|
+
return this.repository.findById(id, tenantId);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* One page of rows.
|
|
51
|
+
*
|
|
52
|
+
* Everything a caller can influence is bounded here rather than in the
|
|
53
|
+
* controller, so calling the service directly is as safe as going through
|
|
54
|
+
* HTTP: the limit is capped, and filter and sort fields are checked against
|
|
55
|
+
* the allowlists before they reach the adapter.
|
|
56
|
+
*/
|
|
57
|
+
async findAll(query = {}, tenantId) {
|
|
58
|
+
const maxLimit = this.options.maxLimit ?? 100;
|
|
59
|
+
const limit = Math.min(Math.max(query.limit ?? this.options.defaultLimit ?? 20, 1), maxLimit);
|
|
60
|
+
const page = Math.max(query.page ?? 1, 1);
|
|
61
|
+
const sortBy = query.sortBy && (this.options.sortable ?? []).includes(query.sortBy) ? query.sortBy : void 0;
|
|
62
|
+
const { items, total } = await this.repository.findAll(
|
|
63
|
+
{
|
|
64
|
+
filter: this.allowedFilter(query),
|
|
65
|
+
limit,
|
|
66
|
+
offset: (page - 1) * limit,
|
|
67
|
+
sortBy,
|
|
68
|
+
sortOrder: sortBy ? query.sortOrder ?? "desc" : void 0
|
|
69
|
+
},
|
|
70
|
+
tenantId
|
|
71
|
+
);
|
|
72
|
+
return paginated(items, buildPageMeta(total, page, limit));
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Keeps only the query fields this service declares filterable. Anything
|
|
76
|
+
* else is dropped silently — a rejection would tell a caller which columns
|
|
77
|
+
* exist, which is the question they were asking.
|
|
78
|
+
*/
|
|
79
|
+
allowedFilter(query) {
|
|
80
|
+
const filterable = this.options.filterable ?? [];
|
|
81
|
+
if (filterable.length === 0) return void 0;
|
|
82
|
+
const filter = {};
|
|
83
|
+
for (const field of filterable) {
|
|
84
|
+
if (query[field] !== void 0) filter[field] = query[field];
|
|
85
|
+
}
|
|
86
|
+
return Object.keys(filter).length > 0 ? filter : void 0;
|
|
87
|
+
}
|
|
88
|
+
async create(data, tenantId) {
|
|
89
|
+
const created = await this.repository.create(data, tenantId);
|
|
90
|
+
await this.audit("created", created.id, { after: created, tenantId });
|
|
91
|
+
return created;
|
|
92
|
+
}
|
|
93
|
+
async update(id, data, tenantId) {
|
|
94
|
+
const before = await this.readBefore(id, tenantId);
|
|
95
|
+
const updated = await this.repository.update(id, data, tenantId);
|
|
96
|
+
await this.audit("updated", id, { before, after: updated, tenantId });
|
|
97
|
+
return updated;
|
|
98
|
+
}
|
|
99
|
+
async delete(id, tenantId) {
|
|
100
|
+
const before = await this.readBefore(id, tenantId);
|
|
101
|
+
if (this.options.deleteStrategy === "soft") {
|
|
102
|
+
await this.repository.update(id, { deletedAt: /* @__PURE__ */ new Date() }, tenantId);
|
|
103
|
+
} else {
|
|
104
|
+
await this.repository.delete(id, tenantId);
|
|
105
|
+
}
|
|
106
|
+
await this.audit(this.options.deleteStrategy === "soft" ? "soft-deleted" : "deleted", id, {
|
|
107
|
+
before,
|
|
108
|
+
tenantId
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
/** Only when the trail is configured to want it — otherwise it is a query for nothing. */
|
|
112
|
+
async readBefore(id, tenantId) {
|
|
113
|
+
if (!this.options.audit?.includeBefore) return void 0;
|
|
114
|
+
return await this.repository.findById(id, tenantId) ?? void 0;
|
|
115
|
+
}
|
|
116
|
+
async audit(action, resourceId, details) {
|
|
117
|
+
const audit = this.options.audit;
|
|
118
|
+
if (!audit) return;
|
|
119
|
+
await audit.recorder.record(
|
|
120
|
+
withRequestContext({
|
|
121
|
+
action,
|
|
122
|
+
resource: audit.resource,
|
|
123
|
+
resourceId,
|
|
124
|
+
before: details.before,
|
|
125
|
+
after: details.after,
|
|
126
|
+
tenantId: details.tenantId
|
|
127
|
+
})
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
// src/crud/crud.controller.ts
|
|
133
|
+
import {
|
|
134
|
+
Body,
|
|
135
|
+
Controller,
|
|
136
|
+
Delete,
|
|
137
|
+
Get,
|
|
138
|
+
Inject,
|
|
139
|
+
Param,
|
|
140
|
+
Patch,
|
|
141
|
+
Post,
|
|
142
|
+
Query,
|
|
143
|
+
Req
|
|
144
|
+
} from "@nestjs/common";
|
|
145
|
+
function createCrudController(serviceToken, options = {}) {
|
|
146
|
+
const tenantIdOf = (req) => options.tenantIdExtractor?.(req);
|
|
147
|
+
let CrudControllerHost = class {
|
|
148
|
+
constructor(service) {
|
|
149
|
+
this.service = service;
|
|
150
|
+
}
|
|
151
|
+
findAll(query, req) {
|
|
152
|
+
const raw = req.query ?? {};
|
|
153
|
+
return this.service.findAll({ ...raw, ...query }, tenantIdOf(req));
|
|
154
|
+
}
|
|
155
|
+
findById(id, req) {
|
|
156
|
+
return this.service.findById(id, tenantIdOf(req));
|
|
157
|
+
}
|
|
158
|
+
create(data, req) {
|
|
159
|
+
return this.service.create(data, tenantIdOf(req));
|
|
160
|
+
}
|
|
161
|
+
update(id, data, req) {
|
|
162
|
+
return this.service.update(id, data, tenantIdOf(req));
|
|
163
|
+
}
|
|
164
|
+
remove(id, req) {
|
|
165
|
+
return this.service.delete(id, tenantIdOf(req));
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
__decorateClass([
|
|
169
|
+
Get(),
|
|
170
|
+
__decorateParam(0, Query()),
|
|
171
|
+
__decorateParam(1, Req())
|
|
172
|
+
], CrudControllerHost.prototype, "findAll", 1);
|
|
173
|
+
__decorateClass([
|
|
174
|
+
Get(":id"),
|
|
175
|
+
__decorateParam(0, Param("id")),
|
|
176
|
+
__decorateParam(1, Req())
|
|
177
|
+
], CrudControllerHost.prototype, "findById", 1);
|
|
178
|
+
__decorateClass([
|
|
179
|
+
Post(),
|
|
180
|
+
__decorateParam(0, Body()),
|
|
181
|
+
__decorateParam(1, Req())
|
|
182
|
+
], CrudControllerHost.prototype, "create", 1);
|
|
183
|
+
__decorateClass([
|
|
184
|
+
Patch(":id"),
|
|
185
|
+
__decorateParam(0, Param("id")),
|
|
186
|
+
__decorateParam(1, Body()),
|
|
187
|
+
__decorateParam(2, Req())
|
|
188
|
+
], CrudControllerHost.prototype, "update", 1);
|
|
189
|
+
__decorateClass([
|
|
190
|
+
Delete(":id"),
|
|
191
|
+
__decorateParam(0, Param("id")),
|
|
192
|
+
__decorateParam(1, Req())
|
|
193
|
+
], CrudControllerHost.prototype, "remove", 1);
|
|
194
|
+
CrudControllerHost = __decorateClass([
|
|
195
|
+
Controller(options.path ?? ""),
|
|
196
|
+
__decorateParam(0, Inject(serviceToken))
|
|
197
|
+
], CrudControllerHost);
|
|
198
|
+
return CrudControllerHost;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export {
|
|
202
|
+
ListQuery,
|
|
203
|
+
BaseCrudService,
|
|
204
|
+
createCrudController
|
|
205
|
+
};
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import {
|
|
2
|
+
RequestContext
|
|
3
|
+
} from "./chunk-KP2ZHJSR.js";
|
|
1
4
|
import {
|
|
2
5
|
UnauthorizedAppException
|
|
3
6
|
} from "./chunk-ORWJ7LES.js";
|
|
@@ -54,7 +57,9 @@ var JwtAuthGuard = class {
|
|
|
54
57
|
if (!token) {
|
|
55
58
|
throw new UnauthorizedAppException("Missing Authorization bearer token");
|
|
56
59
|
}
|
|
57
|
-
|
|
60
|
+
const user = this.tokens.verifyAccessToken(token);
|
|
61
|
+
req.user = user;
|
|
62
|
+
RequestContext.setActor({ id: user.sub, tenantId: user.tenantId, roles: user.roles });
|
|
58
63
|
return true;
|
|
59
64
|
}
|
|
60
65
|
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// src/response/paginated.ts
|
|
2
|
+
var PAGINATED = /* @__PURE__ */ Symbol.for("nestkit.paginated");
|
|
3
|
+
function buildPageMeta(total, page, limit) {
|
|
4
|
+
const pages = limit > 0 ? Math.ceil(total / limit) : 0;
|
|
5
|
+
return {
|
|
6
|
+
page,
|
|
7
|
+
limit,
|
|
8
|
+
total,
|
|
9
|
+
pages,
|
|
10
|
+
hasNext: page < pages,
|
|
11
|
+
hasPrevious: page > 1 && total > 0
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
function paginated(items, meta) {
|
|
15
|
+
const page = { items, meta };
|
|
16
|
+
Object.defineProperty(page, PAGINATED, { value: true, enumerable: false });
|
|
17
|
+
return page;
|
|
18
|
+
}
|
|
19
|
+
function isPaginated(value) {
|
|
20
|
+
return typeof value === "object" && value !== null && value[PAGINATED] === true;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export {
|
|
24
|
+
buildPageMeta,
|
|
25
|
+
paginated,
|
|
26
|
+
isPaginated
|
|
27
|
+
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var _chunkEBPEMVZUcjs = require('./chunk-EBPEMVZU.cjs');
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
var _chunk2REOCMUDcjs = require('./chunk-2REOCMUD.cjs');
|
|
@@ -15,7 +15,7 @@ var RequestIdMiddleware = class {
|
|
|
15
15
|
const requestId = _nullishCoalesce((Array.isArray(incoming) ? incoming[0] : incoming), () => ( _crypto.randomUUID.call(void 0, )));
|
|
16
16
|
req.requestId = requestId;
|
|
17
17
|
res.setHeader(REQUEST_ID_HEADER, requestId);
|
|
18
|
-
|
|
18
|
+
_chunkEBPEMVZUcjs.RequestContext.run({ requestId }, () => next());
|
|
19
19
|
}
|
|
20
20
|
};
|
|
21
21
|
RequestIdMiddleware = exports.RequestIdMiddleware = _chunk2REOCMUDcjs.__decorateClass.call(void 0, [
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ResponseInterceptor
|
|
3
|
-
} from "./chunk-KDAA6GFF.js";
|
|
4
1
|
import {
|
|
5
2
|
GlobalExceptionFilter
|
|
6
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-KRC47OX6.js";
|
|
4
|
+
import {
|
|
5
|
+
ResponseInterceptor
|
|
6
|
+
} from "./chunk-DABTRWA6.js";
|
|
7
7
|
|
|
8
8
|
// src/bootstrap/apply-defaults.ts
|
|
9
9
|
import {
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var _chunkY72D5L6Icjs = require('./chunk-Y72D5L6I.cjs');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
var _chunkEBPEMVZUcjs = require('./chunk-EBPEMVZU.cjs');
|
|
4
7
|
|
|
5
8
|
|
|
6
9
|
var _chunk2REOCMUDcjs = require('./chunk-2REOCMUD.cjs');
|
|
@@ -11,12 +14,14 @@ var _operators = require('rxjs/operators');
|
|
|
11
14
|
var ResponseInterceptor = class {
|
|
12
15
|
intercept(_context, next) {
|
|
13
16
|
return next.handle().pipe(
|
|
14
|
-
_operators.map.call(void 0, (data) =>
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
_operators.map.call(void 0, (data) => {
|
|
18
|
+
const envelope = {
|
|
19
|
+
success: true,
|
|
20
|
+
requestId: _nullishCoalesce(_chunkEBPEMVZUcjs.RequestContext.getRequestId(), () => ( "unknown")),
|
|
21
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
22
|
+
};
|
|
23
|
+
return _chunkY72D5L6Icjs.isPaginated.call(void 0, data) ? { ...envelope, data: data.items, meta: data.meta } : { ...envelope, data };
|
|
24
|
+
})
|
|
20
25
|
);
|
|
21
26
|
}
|
|
22
27
|
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import {
|
|
2
|
+
RequestContext
|
|
3
|
+
} from "./chunk-KP2ZHJSR.js";
|
|
4
|
+
import {
|
|
5
|
+
__decorateClass,
|
|
6
|
+
__decorateParam
|
|
7
|
+
} from "./chunk-4MGIQFAJ.js";
|
|
8
|
+
|
|
9
|
+
// src/audit/audit.port.ts
|
|
10
|
+
var AUDIT_SINK = /* @__PURE__ */ Symbol("AUDIT_SINK");
|
|
11
|
+
|
|
12
|
+
// src/audit/audit.options.ts
|
|
13
|
+
var AUDIT_OPTIONS = /* @__PURE__ */ Symbol("AUDIT_OPTIONS");
|
|
14
|
+
|
|
15
|
+
// src/audit/audit.service.ts
|
|
16
|
+
import { Inject, Injectable, Logger, Optional } from "@nestjs/common";
|
|
17
|
+
function withRequestContext(event) {
|
|
18
|
+
const actor = RequestContext.getActor();
|
|
19
|
+
return {
|
|
20
|
+
...event,
|
|
21
|
+
actorId: event.actorId ?? actor?.id,
|
|
22
|
+
tenantId: event.tenantId ?? actor?.tenantId,
|
|
23
|
+
requestId: event.requestId ?? RequestContext.getRequestId(),
|
|
24
|
+
at: event.at ?? /* @__PURE__ */ new Date()
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
var AuditService = class {
|
|
28
|
+
constructor(sink, options = {}) {
|
|
29
|
+
this.sink = sink;
|
|
30
|
+
this.options = options;
|
|
31
|
+
this.logger = new Logger(AuditService.name);
|
|
32
|
+
}
|
|
33
|
+
async record(event) {
|
|
34
|
+
const complete = withRequestContext(event);
|
|
35
|
+
try {
|
|
36
|
+
await this.sink.record(complete);
|
|
37
|
+
} catch (error) {
|
|
38
|
+
if (this.options.onError === "fail") throw error;
|
|
39
|
+
this.logger.error(
|
|
40
|
+
`Audit sink failed for ${complete.action} ${complete.resource}: ${error instanceof Error ? error.message : String(error)}`
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
AuditService = __decorateClass([
|
|
46
|
+
Injectable(),
|
|
47
|
+
__decorateParam(0, Inject(AUDIT_SINK)),
|
|
48
|
+
__decorateParam(1, Optional()),
|
|
49
|
+
__decorateParam(1, Inject(AUDIT_OPTIONS))
|
|
50
|
+
], AuditService);
|
|
51
|
+
|
|
52
|
+
export {
|
|
53
|
+
AUDIT_SINK,
|
|
54
|
+
AUDIT_OPTIONS,
|
|
55
|
+
withRequestContext,
|
|
56
|
+
AuditService
|
|
57
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
2
|
+
|
|
3
|
+
var _chunkEBPEMVZUcjs = require('./chunk-EBPEMVZU.cjs');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
var _chunk2REOCMUDcjs = require('./chunk-2REOCMUD.cjs');
|
|
8
|
+
|
|
9
|
+
// src/audit/audit.port.ts
|
|
10
|
+
var AUDIT_SINK = /* @__PURE__ */ Symbol("AUDIT_SINK");
|
|
11
|
+
|
|
12
|
+
// src/audit/audit.options.ts
|
|
13
|
+
var AUDIT_OPTIONS = /* @__PURE__ */ Symbol("AUDIT_OPTIONS");
|
|
14
|
+
|
|
15
|
+
// src/audit/audit.service.ts
|
|
16
|
+
var _common = require('@nestjs/common');
|
|
17
|
+
function withRequestContext(event) {
|
|
18
|
+
const actor = _chunkEBPEMVZUcjs.RequestContext.getActor();
|
|
19
|
+
return {
|
|
20
|
+
...event,
|
|
21
|
+
actorId: _nullishCoalesce(event.actorId, () => ( _optionalChain([actor, 'optionalAccess', _ => _.id]))),
|
|
22
|
+
tenantId: _nullishCoalesce(event.tenantId, () => ( _optionalChain([actor, 'optionalAccess', _2 => _2.tenantId]))),
|
|
23
|
+
requestId: _nullishCoalesce(event.requestId, () => ( _chunkEBPEMVZUcjs.RequestContext.getRequestId())),
|
|
24
|
+
at: _nullishCoalesce(event.at, () => ( /* @__PURE__ */ new Date()))
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
var AuditService = class {
|
|
28
|
+
constructor(sink, options = {}) {
|
|
29
|
+
this.sink = sink;
|
|
30
|
+
this.options = options;
|
|
31
|
+
this.logger = new (0, _common.Logger)(AuditService.name);
|
|
32
|
+
}
|
|
33
|
+
async record(event) {
|
|
34
|
+
const complete = withRequestContext(event);
|
|
35
|
+
try {
|
|
36
|
+
await this.sink.record(complete);
|
|
37
|
+
} catch (error) {
|
|
38
|
+
if (this.options.onError === "fail") throw error;
|
|
39
|
+
this.logger.error(
|
|
40
|
+
`Audit sink failed for ${complete.action} ${complete.resource}: ${error instanceof Error ? error.message : String(error)}`
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
AuditService = exports.AuditService = _chunk2REOCMUDcjs.__decorateClass.call(void 0, [
|
|
46
|
+
_common.Injectable.call(void 0, ),
|
|
47
|
+
_chunk2REOCMUDcjs.__decorateParam.call(void 0, 0, _common.Inject.call(void 0, AUDIT_SINK)),
|
|
48
|
+
_chunk2REOCMUDcjs.__decorateParam.call(void 0, 1, _common.Optional.call(void 0, )),
|
|
49
|
+
_chunk2REOCMUDcjs.__decorateParam.call(void 0, 1, _common.Inject.call(void 0, AUDIT_OPTIONS))
|
|
50
|
+
], AuditService);
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
exports.AUDIT_SINK = AUDIT_SINK; exports.AUDIT_OPTIONS = AUDIT_OPTIONS; exports.withRequestContext = withRequestContext; exports.AuditService = AuditService;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/response/paginated.ts
|
|
2
|
+
var PAGINATED = /* @__PURE__ */ Symbol.for("nestkit.paginated");
|
|
3
|
+
function buildPageMeta(total, page, limit) {
|
|
4
|
+
const pages = limit > 0 ? Math.ceil(total / limit) : 0;
|
|
5
|
+
return {
|
|
6
|
+
page,
|
|
7
|
+
limit,
|
|
8
|
+
total,
|
|
9
|
+
pages,
|
|
10
|
+
hasNext: page < pages,
|
|
11
|
+
hasPrevious: page > 1 && total > 0
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
function paginated(items, meta) {
|
|
15
|
+
const page = { items, meta };
|
|
16
|
+
Object.defineProperty(page, PAGINATED, { value: true, enumerable: false });
|
|
17
|
+
return page;
|
|
18
|
+
}
|
|
19
|
+
function isPaginated(value) {
|
|
20
|
+
return typeof value === "object" && value !== null && value[PAGINATED] === true;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
exports.buildPageMeta = buildPageMeta; exports.paginated = paginated; exports.isPaginated = isPaginated;
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { type Type } from '@nestjs/common';
|
|
2
|
+
import type { Paginated } from '../response/paginated.js';
|
|
2
3
|
import type { BaseEntity } from '../entities/base.entity.js';
|
|
3
4
|
import type { BaseCrudService } from './crud.service.js';
|
|
5
|
+
import { ListQuery } from './list-query.dto.js';
|
|
4
6
|
export interface CreateCrudControllerOptions {
|
|
5
7
|
/** Route prefix for the generated controller. Defaults to no prefix. */
|
|
6
8
|
path?: string;
|
|
@@ -31,7 +33,7 @@ export interface CreateCrudControllerOptions {
|
|
|
31
33
|
* }) {}
|
|
32
34
|
*/
|
|
33
35
|
export declare function createCrudController<T extends BaseEntity>(serviceToken: Type<BaseCrudService<T>>, options?: CreateCrudControllerOptions): Type<{
|
|
34
|
-
findAll(
|
|
36
|
+
findAll(query: ListQuery, req: unknown): Promise<Paginated<T>>;
|
|
35
37
|
findById(id: string, req: unknown): Promise<T | null>;
|
|
36
38
|
create(data: Omit<T, 'id' | 'createdAt' | 'updatedAt'>, req: unknown): Promise<T>;
|
|
37
39
|
update(id: string, data: Partial<T>, req: unknown): Promise<T>;
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
import type { AuditSinkPort } from '../audit/audit.port.js';
|
|
2
|
+
import { type Paginated } from '../response/paginated.js';
|
|
1
3
|
import type { BaseEntity } from '../entities/base.entity.js';
|
|
4
|
+
import type { ListQuery } from './list-query.dto.js';
|
|
2
5
|
import type { Repository } from './repository.port.js';
|
|
3
|
-
export interface BaseCrudServiceOptions {
|
|
6
|
+
export interface BaseCrudServiceOptions<T extends BaseEntity = BaseEntity> {
|
|
4
7
|
/**
|
|
5
8
|
* 'hard' (default): delete() calls repository.delete().
|
|
6
9
|
* 'soft': delete() calls repository.update(id, { deletedAt: new Date() })
|
|
@@ -11,6 +14,45 @@ export interface BaseCrudServiceOptions {
|
|
|
11
14
|
* if you always soft-delete.
|
|
12
15
|
*/
|
|
13
16
|
deleteStrategy?: 'soft' | 'hard';
|
|
17
|
+
/** Page size when the caller does not ask for one. Defaults to 20. */
|
|
18
|
+
defaultLimit?: number;
|
|
19
|
+
/**
|
|
20
|
+
* Largest page this service will serve, whatever the query string says.
|
|
21
|
+
* Defaults to 100.
|
|
22
|
+
*/
|
|
23
|
+
maxLimit?: number;
|
|
24
|
+
/**
|
|
25
|
+
* Fields a caller may filter by. **Empty by default, and that is the
|
|
26
|
+
* point**: the previous behaviour forwarded the whole query string to the
|
|
27
|
+
* repository, so any column — a password hash, an internal flag, another
|
|
28
|
+
* tenant's marker — could be used to probe rows one guess at a time.
|
|
29
|
+
* Opening a field is one line; leaving it open by accident is a leak.
|
|
30
|
+
*/
|
|
31
|
+
filterable?: (keyof T & string)[];
|
|
32
|
+
/**
|
|
33
|
+
* Fields a caller may sort by. Empty by default for the same reason plus a
|
|
34
|
+
* sharper one: adapters interpolate this name into SQL.
|
|
35
|
+
*/
|
|
36
|
+
sortable?: (keyof T & string)[];
|
|
37
|
+
/**
|
|
38
|
+
* Records every create, update and delete this service performs.
|
|
39
|
+
*
|
|
40
|
+
* `recorder` is anything with `record(event)`: the raw `AuditSinkPort`, or
|
|
41
|
+
* `AuditService` — pass the service to inherit its policy for a failing
|
|
42
|
+
* sink, because this class does not catch. One place decides whether a lost
|
|
43
|
+
* entry stops the operation, and it is not this one.
|
|
44
|
+
*/
|
|
45
|
+
audit?: {
|
|
46
|
+
recorder: AuditSinkPort;
|
|
47
|
+
/** Name of the thing being changed, as the trail should read: `invoice`, `widget`. */
|
|
48
|
+
resource: string;
|
|
49
|
+
/**
|
|
50
|
+
* Read the row before updating or deleting it, so the entry carries what
|
|
51
|
+
* it used to say. Off by default: it is an extra query per write, and a
|
|
52
|
+
* trail without it still answers who and when.
|
|
53
|
+
*/
|
|
54
|
+
includeBefore?: boolean;
|
|
55
|
+
};
|
|
14
56
|
}
|
|
15
57
|
/**
|
|
16
58
|
* Generic CRUD orchestration on top of a Repository<T> port. Optional to
|
|
@@ -19,11 +61,28 @@ export interface BaseCrudServiceOptions {
|
|
|
19
61
|
*/
|
|
20
62
|
export declare class BaseCrudService<T extends BaseEntity> {
|
|
21
63
|
protected readonly repository: Repository<T>;
|
|
22
|
-
protected readonly options: BaseCrudServiceOptions
|
|
23
|
-
constructor(repository: Repository<T>, options?: BaseCrudServiceOptions);
|
|
64
|
+
protected readonly options: BaseCrudServiceOptions<T>;
|
|
65
|
+
constructor(repository: Repository<T>, options?: BaseCrudServiceOptions<T>);
|
|
24
66
|
findById(id: string, tenantId?: string): Promise<T | null>;
|
|
25
|
-
|
|
67
|
+
/**
|
|
68
|
+
* One page of rows.
|
|
69
|
+
*
|
|
70
|
+
* Everything a caller can influence is bounded here rather than in the
|
|
71
|
+
* controller, so calling the service directly is as safe as going through
|
|
72
|
+
* HTTP: the limit is capped, and filter and sort fields are checked against
|
|
73
|
+
* the allowlists before they reach the adapter.
|
|
74
|
+
*/
|
|
75
|
+
findAll(query?: ListQuery, tenantId?: string): Promise<Paginated<T>>;
|
|
76
|
+
/**
|
|
77
|
+
* Keeps only the query fields this service declares filterable. Anything
|
|
78
|
+
* else is dropped silently — a rejection would tell a caller which columns
|
|
79
|
+
* exist, which is the question they were asking.
|
|
80
|
+
*/
|
|
81
|
+
protected allowedFilter(query: Record<string, unknown>): Partial<T> | undefined;
|
|
26
82
|
create(data: Omit<T, 'id' | 'createdAt' | 'updatedAt'>, tenantId?: string): Promise<T>;
|
|
27
83
|
update(id: string, data: Partial<T>, tenantId?: string): Promise<T>;
|
|
28
84
|
delete(id: string, tenantId?: string): Promise<void>;
|
|
85
|
+
/** Only when the trail is configured to want it — otherwise it is a query for nothing. */
|
|
86
|
+
private readBefore;
|
|
87
|
+
private audit;
|
|
29
88
|
}
|
package/dist/crud/index.cjs
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
2
|
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
|
|
5
|
+
var _chunk47FXU74Gcjs = require('../chunk-47FXU74G.cjs');
|
|
6
|
+
require('../chunk-XX5ROVSC.cjs');
|
|
7
|
+
require('../chunk-Y72D5L6I.cjs');
|
|
8
|
+
require('../chunk-EBPEMVZU.cjs');
|
|
5
9
|
require('../chunk-2REOCMUD.cjs');
|
|
6
10
|
|
|
7
11
|
|
|
8
12
|
|
|
9
|
-
|
|
13
|
+
|
|
14
|
+
exports.BaseCrudService = _chunk47FXU74Gcjs.BaseCrudService; exports.ListQuery = _chunk47FXU74Gcjs.ListQuery; exports.createCrudController = _chunk47FXU74Gcjs.createCrudController;
|
package/dist/crud/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export type { Repository } from './repository.port.js';
|
|
1
|
+
export type { Repository, FindAllOptions, PageResult } from './repository.port.js';
|
|
2
|
+
export { ListQuery } from './list-query.dto.js';
|
|
2
3
|
export { BaseCrudService } from './crud.service.js';
|
|
3
4
|
export type { BaseCrudServiceOptions } from './crud.service.js';
|
|
4
5
|
export { createCrudController } from './crud.controller.js';
|
package/dist/crud/index.js
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
import {
|
|
2
2
|
BaseCrudService,
|
|
3
|
+
ListQuery,
|
|
3
4
|
createCrudController
|
|
4
|
-
} from "../chunk-
|
|
5
|
+
} from "../chunk-LK3OINS7.js";
|
|
6
|
+
import "../chunk-XHW4CTBS.js";
|
|
7
|
+
import "../chunk-TCAMLNUZ.js";
|
|
8
|
+
import "../chunk-KP2ZHJSR.js";
|
|
5
9
|
import "../chunk-4MGIQFAJ.js";
|
|
6
10
|
export {
|
|
7
11
|
BaseCrudService,
|
|
12
|
+
ListQuery,
|
|
8
13
|
createCrudController
|
|
9
14
|
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Query string of a list endpoint, as a real DTO.
|
|
3
|
+
*
|
|
4
|
+
* Being a class with decorators is the point: the global `ValidationPipe` runs
|
|
5
|
+
* with `whitelist: true`, which only strips unknown properties when there is a
|
|
6
|
+
* class to compare against. The previous signature typed the query as
|
|
7
|
+
* `Partial<T>`, and a bare type carries no metadata — so nothing was stripped
|
|
8
|
+
* and every column of the entity was filterable from the URL.
|
|
9
|
+
*
|
|
10
|
+
* `@Type(() => Number)` is not decoration: query values arrive as strings, and
|
|
11
|
+
* `?limit=20` without it is the string `"20"`, which `@IsInt()` rejects.
|
|
12
|
+
*/
|
|
13
|
+
export declare class ListQuery {
|
|
14
|
+
page?: number;
|
|
15
|
+
/**
|
|
16
|
+
* Capped at 200 here and again, per service, by `maxLimit`. The hard cap
|
|
17
|
+
* exists so a forgotten configuration cannot turn into `?limit=1000000`.
|
|
18
|
+
*/
|
|
19
|
+
limit?: number;
|
|
20
|
+
/** Ignored unless the service lists the field in `sortable`. */
|
|
21
|
+
sortBy?: string;
|
|
22
|
+
sortOrder?: 'asc' | 'desc';
|
|
23
|
+
}
|