@lucaapp/service-utils 1.30.0 → 1.31.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/dist/index.d.ts CHANGED
@@ -4,6 +4,7 @@ export * from './lib/serviceIdentity';
4
4
  export * from './lib/urlEncoded';
5
5
  export * from './lib/metrics';
6
6
  export * from './lib/middleware';
7
+ export * from './lib/money';
7
8
  export * from './lib/negotiator';
8
9
  export * from './lib/requestTracer';
9
10
  export * from './lib/wsEvent';
package/dist/index.js CHANGED
@@ -20,6 +20,7 @@ __exportStar(require("./lib/serviceIdentity"), exports);
20
20
  __exportStar(require("./lib/urlEncoded"), exports);
21
21
  __exportStar(require("./lib/metrics"), exports);
22
22
  __exportStar(require("./lib/middleware"), exports);
23
+ __exportStar(require("./lib/money"), exports);
23
24
  __exportStar(require("./lib/negotiator"), exports);
24
25
  __exportStar(require("./lib/requestTracer"), exports);
25
26
  __exportStar(require("./lib/wsEvent"), exports);
@@ -23,23 +23,23 @@ class Api {
23
23
  }
24
24
  get(path, summary, options, handler) {
25
25
  (0, endpoint_1.mountEndpoint)(this.router, 'get', path, options, handler, this.debug);
26
- (0, endpoint_1.registerEndpoint)(this.registry, 'get', this.prefixPath, path, summary, options);
26
+ (0, endpoint_1.registerEndpoint)(this.registry, 'get', this.prefixPath, path, summary, options, this.debug);
27
27
  }
28
28
  post(path, summary, options, handler) {
29
29
  (0, endpoint_1.mountEndpoint)(this.router, 'post', path, options, handler, this.debug);
30
- (0, endpoint_1.registerEndpoint)(this.registry, 'post', this.prefixPath, path, summary, options);
30
+ (0, endpoint_1.registerEndpoint)(this.registry, 'post', this.prefixPath, path, summary, options, this.debug);
31
31
  }
32
32
  patch(path, summary, options, handler) {
33
33
  (0, endpoint_1.mountEndpoint)(this.router, 'patch', path, options, handler, this.debug);
34
- (0, endpoint_1.registerEndpoint)(this.registry, 'patch', this.prefixPath, path, summary, options);
34
+ (0, endpoint_1.registerEndpoint)(this.registry, 'patch', this.prefixPath, path, summary, options, this.debug);
35
35
  }
36
36
  put(path, summary, options, handler) {
37
37
  (0, endpoint_1.mountEndpoint)(this.router, 'put', path, options, handler, this.debug);
38
- (0, endpoint_1.registerEndpoint)(this.registry, 'put', this.prefixPath, path, summary, options);
38
+ (0, endpoint_1.registerEndpoint)(this.registry, 'put', this.prefixPath, path, summary, options, this.debug);
39
39
  }
40
40
  delete(path, summary, options, handler) {
41
41
  (0, endpoint_1.mountEndpoint)(this.router, 'delete', path, options, handler, this.debug);
42
- (0, endpoint_1.registerEndpoint)(this.registry, 'delete', this.prefixPath, path, summary, options);
42
+ (0, endpoint_1.registerEndpoint)(this.registry, 'delete', this.prefixPath, path, summary, options, this.debug);
43
43
  }
44
44
  generateOpenAPISpec() {
45
45
  const generator = new zod_to_openapi_1.OpenAPIGenerator(this.registry.definitions, this.openApiVersion);
@@ -4,5 +4,5 @@ import { Middleware, EndpointResponseSchema } from './types/middleware';
4
4
  import { EndpointOptions, EndpointHandler } from './types/endpoint';
5
5
  type HttpMethod = 'get' | 'post' | 'put' | 'delete' | 'patch';
6
6
  export declare const mountEndpoint: <TResponseSchemas extends readonly EndpointResponseSchema[], TRequestBody = undefined, TRequestParams = undefined, TRequestQuery = undefined, TRequestHeaders = undefined, TMiddlewares extends readonly Middleware<any, any, any, any, any, any>[] | undefined = undefined>(router: Router, method: HttpMethod, path: string, options: EndpointOptions<TResponseSchemas, TRequestBody, TRequestParams, TRequestQuery, TRequestHeaders, TMiddlewares>, handler: EndpointHandler<TResponseSchemas, TRequestBody, TRequestParams, TRequestQuery, TRequestHeaders, TMiddlewares>, debug: boolean) => void;
7
- export declare const registerEndpoint: (registry: OpenAPIRegistry, method: HttpMethod, prefixPath: string, path: string, summary: string, options: EndpointOptions<any, any, any, any, any, any>) => void;
7
+ export declare const registerEndpoint: (registry: OpenAPIRegistry, method: HttpMethod, prefixPath: string, path: string, summary: string, options: EndpointOptions<any, any, any, any, any, any>, debug: boolean) => void;
8
8
  export {};
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.registerEndpoint = exports.mountEndpoint = void 0;
7
7
  const zod_1 = require("zod");
8
8
  const assert_1 = __importDefault(require("assert"));
9
- const boom_1 = require("@hapi/boom");
9
+ const http_1 = require("./types/http");
10
10
  const buildResponseConfig = (responseOptions) => {
11
11
  const responses = {};
12
12
  responseOptions.forEach(response => {
@@ -66,6 +66,27 @@ const validateAndSendResponse = async (expressResponse, response, validResponses
66
66
  }
67
67
  expressResponse.status(response.status).send(validatedResponseBody);
68
68
  };
69
+ const sendBadResponseError = (response, error) => {
70
+ return response.status(500).send({
71
+ error: http_1.HTTPStatus.INTERNAL_SERVER_ERROR,
72
+ message: 'Invalid Response',
73
+ issues: error.issues,
74
+ });
75
+ };
76
+ const sendKnownErrorResponse = (response, statusCode, error, debug) => {
77
+ response.status(statusCode).send({
78
+ error: error.type,
79
+ message: error.message,
80
+ ...(debug && { stack: error.stack }),
81
+ });
82
+ };
83
+ const sendBadRequestError = (response, error) => {
84
+ return response.status(400).send({
85
+ error: http_1.HTTPStatus.BAD_REQUEST,
86
+ message: error.message,
87
+ issues: error.issues,
88
+ });
89
+ };
69
90
  const handleMiddleware = async (middleware, context, request, response, debug) => new Promise(async (resolve, reject) => {
70
91
  try {
71
92
  const handlerRequestBody = ((await middleware.options.schemas?.body?.parseAsync(request.body)) ||
@@ -91,10 +112,9 @@ const handleMiddleware = async (middleware, context, request, response, debug) =
91
112
  resolve(false);
92
113
  }
93
114
  catch (error) {
94
- if (error instanceof Error) {
95
- (0, boom_1.boomify)(error, {
96
- statusCode: 500,
97
- });
115
+ if (error instanceof zod_1.ZodError) {
116
+ sendBadResponseError(response, error);
117
+ return resolve(false);
98
118
  }
99
119
  reject(error);
100
120
  }
@@ -105,27 +125,27 @@ const handleMiddleware = async (middleware, context, request, response, debug) =
105
125
  resolve(true);
106
126
  }
107
127
  catch (error) {
108
- if (error instanceof Error) {
109
- (0, boom_1.boomify)(error, {
110
- statusCode: 500,
111
- });
128
+ if (error instanceof zod_1.ZodError) {
129
+ sendBadResponseError(response, error);
130
+ return resolve(false);
112
131
  }
113
132
  reject(error);
114
133
  }
115
134
  });
116
135
  }
117
136
  catch (error) {
137
+ if (error instanceof zod_1.ZodError) {
138
+ sendBadRequestError(response, error);
139
+ return resolve(false);
140
+ }
118
141
  if (isTypedError(error) &&
119
142
  middleware.options.errors &&
120
143
  middleware.options.errors[error.type]) {
121
144
  const statusCode = middleware.options.errors[error.type];
122
- return response.status(statusCode).send({
123
- statusCode,
124
- error: error.type,
125
- message: error.message,
126
- ...(debug && { stack: error.stack }),
127
- });
145
+ sendKnownErrorResponse(response, statusCode, error, debug);
146
+ return resolve(false);
128
147
  }
148
+ // bubble up unknown errors
129
149
  reject(error);
130
150
  }
131
151
  });
@@ -159,38 +179,41 @@ const mountEndpoint = (router, method, path, options, handler, debug) => {
159
179
  await validateAndSendResponse(response, controllerResponse, options.responses);
160
180
  }
161
181
  catch (error) {
162
- if (error instanceof Error) {
163
- (0, boom_1.boomify)(error, {
164
- statusCode: 500,
165
- });
182
+ if (error instanceof zod_1.ZodError) {
183
+ return sendBadResponseError(response, error);
166
184
  }
167
185
  next(error);
168
186
  }
169
187
  });
170
188
  }
171
189
  catch (error) {
172
- if (error instanceof zod_1.z.ZodError && !(0, boom_1.isBoom)(error)) {
173
- return response.status(400).send({ issues: error.issues });
190
+ if (error instanceof zod_1.ZodError) {
191
+ return sendBadRequestError(response, error);
174
192
  }
175
193
  if (isTypedError(error) &&
176
194
  options.errors &&
177
195
  options.errors[error.type]) {
178
196
  const statusCode = options.errors[error.type];
179
- return response.status(statusCode).send({
180
- statusCode,
181
- error: error.type,
197
+ return sendKnownErrorResponse(response, statusCode, error, debug);
198
+ }
199
+ if (error instanceof Error) {
200
+ return response.status(500).send({
201
+ error: http_1.HTTPStatus.INTERNAL_SERVER_ERROR,
182
202
  message: error.message,
183
203
  ...(debug && { stack: error.stack }),
184
204
  });
185
205
  }
186
- next(error);
206
+ return response.status(500).send({
207
+ error: http_1.HTTPStatus.INTERNAL_SERVER_ERROR,
208
+ message: 'Unknown Error',
209
+ });
187
210
  }
188
211
  });
189
212
  };
190
213
  exports.mountEndpoint = mountEndpoint;
191
214
  const registerEndpoint = (registry, method, prefixPath, path, summary,
192
215
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
193
- options) => {
216
+ options, debug) => {
194
217
  let bodySchema = options.schemas?.body || undefined;
195
218
  let paramsSchema = options.schemas?.params || undefined;
196
219
  let querySchema = options.schemas?.query || undefined;
@@ -199,17 +222,14 @@ options) => {
199
222
  if (options.errors) {
200
223
  for (const errorType of Object.keys(options.errors)) {
201
224
  const statusCode = options.errors[errorType];
202
- responseSchemas = responseSchemas.concat([
203
- {
204
- status: statusCode,
205
- description: errorType,
206
- schema: zod_1.z.object({
207
- statusCode: zod_1.z.literal(statusCode),
208
- error: zod_1.z.literal(errorType),
209
- message: zod_1.z.string(),
210
- }),
211
- },
212
- ]);
225
+ responseSchemas.push({
226
+ status: statusCode,
227
+ description: errorType,
228
+ schema: zod_1.z.object({
229
+ error: zod_1.z.literal(errorType),
230
+ message: zod_1.z.string(),
231
+ }),
232
+ });
213
233
  }
214
234
  }
215
235
  for (const middleware of options.middlewares || []) {
@@ -239,20 +259,41 @@ options) => {
239
259
  if (middleware.options.errors) {
240
260
  for (const errorType of Object.keys(middleware.options.errors)) {
241
261
  const statusCode = middleware.options.errors[errorType];
242
- responseSchemas = responseSchemas.concat([
243
- {
244
- status: statusCode,
245
- description: errorType,
246
- schema: zod_1.z.object({
247
- statusCode: zod_1.z.literal(statusCode),
248
- error: zod_1.z.literal(errorType),
249
- message: zod_1.z.string(),
250
- }),
251
- },
252
- ]);
262
+ responseSchemas.push({
263
+ status: statusCode,
264
+ description: errorType,
265
+ schema: zod_1.z.object({
266
+ error: zod_1.z.literal(errorType),
267
+ message: zod_1.z.string(),
268
+ }),
269
+ });
253
270
  }
254
271
  }
255
272
  }
273
+ // Add ZodError response
274
+ responseSchemas.push({
275
+ status: 400,
276
+ description: 'Bad Request',
277
+ schema: zod_1.z.object({
278
+ error: zod_1.z.literal(http_1.HTTPStatus.BAD_REQUEST),
279
+ message: zod_1.z.string(),
280
+ issues: zod_1.z.array(zod_1.z
281
+ .object({
282
+ message: zod_1.z.string(),
283
+ })
284
+ .openapi({ additionalProperties: true })),
285
+ }),
286
+ });
287
+ // Add UnknownError response
288
+ responseSchemas.push({
289
+ status: 500,
290
+ description: 'Internal Server Error',
291
+ schema: zod_1.z.object({
292
+ error: zod_1.z.literal(http_1.HTTPStatus.INTERNAL_SERVER_ERROR),
293
+ message: zod_1.z.string(),
294
+ ...(debug && { stack: zod_1.z.string().optional() }),
295
+ }),
296
+ });
256
297
  const responseMap = buildResponseConfig(responseSchemas);
257
298
  registry.registerPath({
258
299
  method,
@@ -1,56 +1,45 @@
1
1
  import { z } from 'zod';
2
+ import { HTTPStatus } from './types/http';
2
3
  declare const noContentSchema: z.ZodVoid;
3
4
  declare const badRequestErrorSchema: z.ZodObject<{
4
- statusCode: z.ZodLiteral<400>;
5
- error: z.ZodString;
5
+ error: z.ZodLiteral<HTTPStatus.BAD_REQUEST>;
6
6
  message: z.ZodString;
7
7
  }, "strip", z.ZodTypeAny, {
8
8
  message: string;
9
- error: string;
10
- statusCode: 400;
9
+ error: HTTPStatus.BAD_REQUEST;
11
10
  }, {
12
11
  message: string;
13
- error: string;
14
- statusCode: 400;
12
+ error: HTTPStatus.BAD_REQUEST;
15
13
  }>;
16
14
  declare const unauthorizedErrorSchema: z.ZodObject<{
17
- statusCode: z.ZodLiteral<401>;
18
- error: z.ZodString;
15
+ error: z.ZodLiteral<HTTPStatus.UNAUTHORIZED>;
19
16
  message: z.ZodString;
20
17
  }, "strip", z.ZodTypeAny, {
21
18
  message: string;
22
- error: string;
23
- statusCode: 401;
19
+ error: HTTPStatus.UNAUTHORIZED;
24
20
  }, {
25
21
  message: string;
26
- error: string;
27
- statusCode: 401;
22
+ error: HTTPStatus.UNAUTHORIZED;
28
23
  }>;
29
24
  declare const forbiddenErrorSchema: z.ZodObject<{
30
- statusCode: z.ZodLiteral<403>;
31
- error: z.ZodString;
25
+ error: z.ZodLiteral<HTTPStatus.FORBIDDEN>;
32
26
  message: z.ZodString;
33
27
  }, "strip", z.ZodTypeAny, {
34
28
  message: string;
35
- error: string;
36
- statusCode: 403;
29
+ error: HTTPStatus.FORBIDDEN;
37
30
  }, {
38
31
  message: string;
39
- error: string;
40
- statusCode: 403;
32
+ error: HTTPStatus.FORBIDDEN;
41
33
  }>;
42
34
  declare const notFoundErrorSchema: z.ZodObject<{
43
- statusCode: z.ZodLiteral<404>;
44
- error: z.ZodString;
35
+ error: z.ZodLiteral<HTTPStatus.NOT_FOUND>;
45
36
  message: z.ZodString;
46
37
  }, "strip", z.ZodTypeAny, {
47
38
  message: string;
48
- error: string;
49
- statusCode: 404;
39
+ error: HTTPStatus.NOT_FOUND;
50
40
  }, {
51
41
  message: string;
52
- error: string;
53
- statusCode: 404;
42
+ error: HTTPStatus.NOT_FOUND;
54
43
  }>;
55
44
  export declare const okResponse: <T>(schema: z.ZodType<T, z.ZodTypeDef, T>) => {
56
45
  status: 200;
@@ -3,27 +3,24 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.notFoundResponse = exports.forbiddenResponse = exports.unauthorizedResponse = exports.badRequestResponse = exports.noContentResponse = exports.okResponse = void 0;
4
4
  const zod_to_openapi_1 = require("@asteasolutions/zod-to-openapi");
5
5
  const zod_1 = require("zod");
6
+ const http_1 = require("./types/http");
6
7
  (0, zod_to_openapi_1.extendZodWithOpenApi)(zod_1.z);
7
8
  // response schemas
8
9
  const noContentSchema = zod_1.z.void();
9
10
  const badRequestErrorSchema = zod_1.z.object({
10
- statusCode: zod_1.z.literal(400),
11
- error: zod_1.z.string(),
11
+ error: zod_1.z.literal(http_1.HTTPStatus.BAD_REQUEST),
12
12
  message: zod_1.z.string(),
13
13
  });
14
14
  const unauthorizedErrorSchema = zod_1.z.object({
15
- statusCode: zod_1.z.literal(401),
16
- error: zod_1.z.string(),
15
+ error: zod_1.z.literal(http_1.HTTPStatus.UNAUTHORIZED),
17
16
  message: zod_1.z.string(),
18
17
  });
19
18
  const forbiddenErrorSchema = zod_1.z.object({
20
- statusCode: zod_1.z.literal(403),
21
- error: zod_1.z.string(),
19
+ error: zod_1.z.literal(http_1.HTTPStatus.FORBIDDEN),
22
20
  message: zod_1.z.string(),
23
21
  });
24
22
  const notFoundErrorSchema = zod_1.z.object({
25
- statusCode: zod_1.z.literal(404),
26
- error: zod_1.z.string(),
23
+ error: zod_1.z.literal(http_1.HTTPStatus.NOT_FOUND),
27
24
  message: zod_1.z.string(),
28
25
  });
29
26
  // response functions
@@ -1,3 +1,4 @@
1
+ import { HTTPStatus } from './types/http';
1
2
  export declare const ok: <T>(body: T) => {
2
3
  status: 200;
3
4
  body: T;
@@ -9,32 +10,28 @@ export declare const noContent: () => {
9
10
  export declare const badRequest: (message?: string) => {
10
11
  status: 400;
11
12
  body: {
12
- statusCode: 400;
13
- error: string;
13
+ error: HTTPStatus.BAD_REQUEST;
14
14
  message: string;
15
15
  };
16
16
  };
17
17
  export declare const unauthorized: (message?: string) => {
18
18
  status: 401;
19
19
  body: {
20
- statusCode: 401;
21
- error: string;
20
+ error: HTTPStatus.UNAUTHORIZED;
22
21
  message: string;
23
22
  };
24
23
  };
25
24
  export declare const forbidden: (message?: string) => {
26
25
  status: 403;
27
26
  body: {
28
- statusCode: 403;
29
- error: string;
27
+ error: HTTPStatus.FORBIDDEN;
30
28
  message: string;
31
29
  };
32
30
  };
33
31
  export declare const notFound: (message?: string) => {
34
32
  status: 404;
35
33
  body: {
36
- statusCode: 404;
37
- error: string;
34
+ error: HTTPStatus.NOT_FOUND;
38
35
  message: string;
39
36
  };
40
37
  };
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.notFound = exports.forbidden = exports.unauthorized = exports.badRequest = exports.noContent = exports.ok = void 0;
4
+ const http_1 = require("./types/http");
4
5
  const ok = (body) => ({
5
6
  status: 200,
6
7
  body,
@@ -14,8 +15,7 @@ exports.noContent = noContent;
14
15
  const badRequest = (message) => ({
15
16
  status: 400,
16
17
  body: {
17
- statusCode: 400,
18
- error: 'Bad request',
18
+ error: http_1.HTTPStatus.BAD_REQUEST,
19
19
  message: message || 'Bad request',
20
20
  },
21
21
  });
@@ -23,8 +23,7 @@ exports.badRequest = badRequest;
23
23
  const unauthorized = (message) => ({
24
24
  status: 401,
25
25
  body: {
26
- statusCode: 401,
27
- error: 'Unauthorized',
26
+ error: http_1.HTTPStatus.UNAUTHORIZED,
28
27
  message: message || 'Unauthorized',
29
28
  },
30
29
  });
@@ -32,8 +31,7 @@ exports.unauthorized = unauthorized;
32
31
  const forbidden = (message) => ({
33
32
  status: 403,
34
33
  body: {
35
- statusCode: 403,
36
- error: 'Forbidden',
34
+ error: http_1.HTTPStatus.FORBIDDEN,
37
35
  message: message || 'Forbidden',
38
36
  },
39
37
  });
@@ -41,8 +39,7 @@ exports.forbidden = forbidden;
41
39
  const notFound = (message) => ({
42
40
  status: 404,
43
41
  body: {
44
- statusCode: 404,
45
- error: 'Not found',
42
+ error: http_1.HTTPStatus.NOT_FOUND,
46
43
  message: message || 'Not found',
47
44
  },
48
45
  });
@@ -0,0 +1,17 @@
1
+ export declare enum HTTPStatus {
2
+ 'OK' = "OK",
3
+ 'CREATED' = "CREATED",
4
+ 'ACCEPTED' = "ACCEPTED",
5
+ 'NO_CONTENT' = "NO_CONTENT",
6
+ 'BAD_REQUEST' = "BAD_REQUEST",
7
+ 'UNAUTHORIZED' = "UNAUTHORIZED",
8
+ 'FORBIDDEN' = "FORBIDDEN",
9
+ 'NOT_FOUND' = "NOT_FOUND",
10
+ 'CONFLICT' = "CONFLICT",
11
+ 'GONE' = "GONE",
12
+ 'TOO_MANY_REQUESTS' = "TOO_MANY_REQUESTS",
13
+ 'INTERNAL_SERVER_ERROR' = "INTERNAL_SERVER_ERROR",
14
+ 'NOT_IMPLEMENTED' = "NOT_IMPLEMENTED",
15
+ 'BAD_GATEWAY' = "BAD_GATEWAY",
16
+ 'SERVICE_UNAVAILABLE' = "SERVICE_UNAVAILABLE"
17
+ }
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HTTPStatus = void 0;
4
+ var HTTPStatus;
5
+ (function (HTTPStatus) {
6
+ HTTPStatus["OK"] = "OK";
7
+ HTTPStatus["CREATED"] = "CREATED";
8
+ HTTPStatus["ACCEPTED"] = "ACCEPTED";
9
+ HTTPStatus["NO_CONTENT"] = "NO_CONTENT";
10
+ HTTPStatus["BAD_REQUEST"] = "BAD_REQUEST";
11
+ HTTPStatus["UNAUTHORIZED"] = "UNAUTHORIZED";
12
+ HTTPStatus["FORBIDDEN"] = "FORBIDDEN";
13
+ HTTPStatus["NOT_FOUND"] = "NOT_FOUND";
14
+ HTTPStatus["CONFLICT"] = "CONFLICT";
15
+ HTTPStatus["GONE"] = "GONE";
16
+ HTTPStatus["TOO_MANY_REQUESTS"] = "TOO_MANY_REQUESTS";
17
+ HTTPStatus["INTERNAL_SERVER_ERROR"] = "INTERNAL_SERVER_ERROR";
18
+ HTTPStatus["NOT_IMPLEMENTED"] = "NOT_IMPLEMENTED";
19
+ HTTPStatus["BAD_GATEWAY"] = "BAD_GATEWAY";
20
+ HTTPStatus["SERVICE_UNAVAILABLE"] = "SERVICE_UNAVAILABLE";
21
+ })(HTTPStatus = exports.HTTPStatus || (exports.HTTPStatus = {}));
@@ -1,3 +1,4 @@
1
+ import { Model } from 'sequelize';
1
2
  interface Money {
2
3
  getAmount(): number;
3
4
  add(addend?: Money): Money;
@@ -26,4 +27,6 @@ declare class Money implements Money {
26
27
  min(comparator: Money): Money;
27
28
  max(comparator: Money): Money;
28
29
  }
29
- export { Money };
30
+ declare const moneyfiedGet: (model: Model, key: string) => Money | null;
31
+ declare const moneyfiedSet: (model: Model, key: string, value: Money | undefined) => void;
32
+ export { moneyfiedGet, moneyfiedSet, Money };
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Money = void 0;
3
+ exports.Money = exports.moneyfiedSet = exports.moneyfiedGet = void 0;
4
4
  const CENTS_PER_EURO = 100;
5
5
  const EPSILON = 0.00000000001;
6
6
  const roundHalfUp = (value) => {
@@ -87,3 +87,14 @@ class Money {
87
87
  }
88
88
  }
89
89
  exports.Money = Money;
90
+ const moneyfiedGet = (model, key) => {
91
+ const currentValue = model.getDataValue(key);
92
+ return currentValue === null || currentValue === undefined
93
+ ? null
94
+ : Money.fromAmount(currentValue);
95
+ };
96
+ exports.moneyfiedGet = moneyfiedGet;
97
+ const moneyfiedSet = (model, key, value) => {
98
+ model.setDataValue(key, value?.getAmount());
99
+ };
100
+ exports.moneyfiedSet = moneyfiedSet;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lucaapp/service-utils",
3
- "version": "1.30.0",
3
+ "version": "1.31.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [
@@ -57,6 +57,7 @@
57
57
  "pino-http": "8.2.1",
58
58
  "prom-client": "14.1.0",
59
59
  "response-time": "^2.3.2",
60
+ "sequelize": "^6.29.0",
60
61
  "swagger-ui-express": "4.5.0",
61
62
  "url-value-parser": "^2.2.0",
62
63
  "uuid": "^9.0.0",