glasswork 1.4.1 → 1.6.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/CHANGELOG.md +75 -1
- package/README.md +6 -2
- package/dist/audit/audit-logger.d.ts +6 -1
- package/dist/audit/audit-logger.d.ts.map +1 -1
- package/dist/audit/audit-logger.js +6 -1
- package/dist/audit/audit-logger.js.map +1 -1
- package/dist/auth/assert.d.ts +4 -0
- package/dist/auth/assert.d.ts.map +1 -1
- package/dist/auth/assert.js +7 -3
- package/dist/auth/assert.js.map +1 -1
- package/dist/auth/better-auth-provider.js +13 -4
- package/dist/auth/better-auth-provider.js.map +1 -1
- package/dist/auth/errors.d.ts +17 -0
- package/dist/auth/errors.d.ts.map +1 -0
- package/dist/auth/errors.js +23 -0
- package/dist/auth/errors.js.map +1 -0
- package/dist/auth/index.d.ts +2 -1
- package/dist/auth/index.d.ts.map +1 -1
- package/dist/auth/index.js +1 -0
- package/dist/auth/index.js.map +1 -1
- package/dist/auth/middleware.d.ts +13 -0
- package/dist/auth/middleware.d.ts.map +1 -1
- package/dist/auth/middleware.js +19 -8
- package/dist/auth/middleware.js.map +1 -1
- package/dist/auth/types.d.ts +4 -0
- package/dist/auth/types.d.ts.map +1 -1
- package/dist/config/providers.d.ts.map +1 -1
- package/dist/config/providers.js +5 -4
- package/dist/config/providers.js.map +1 -1
- package/dist/core/bootstrap.d.ts +2 -2
- package/dist/core/bootstrap.d.ts.map +1 -1
- package/dist/core/bootstrap.js +162 -83
- package/dist/core/bootstrap.js.map +1 -1
- package/dist/core/contract.d.ts +9 -2
- package/dist/core/contract.d.ts.map +1 -1
- package/dist/core/contract.js +2 -1
- package/dist/core/contract.js.map +1 -1
- package/dist/core/errors.d.ts +16 -0
- package/dist/core/errors.d.ts.map +1 -0
- package/dist/core/errors.js +31 -0
- package/dist/core/errors.js.map +1 -0
- package/dist/core/glasswork-config.d.ts +42 -0
- package/dist/core/glasswork-config.d.ts.map +1 -0
- package/dist/core/glasswork-config.js +2 -0
- package/dist/core/glasswork-config.js.map +1 -0
- package/dist/core/index.d.ts +7 -4
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +5 -3
- package/dist/core/index.js.map +1 -1
- package/dist/core/job-services.d.ts +16 -0
- package/dist/core/job-services.d.ts.map +1 -0
- package/dist/core/job-services.js +19 -0
- package/dist/core/job-services.js.map +1 -0
- package/dist/core/lifecycle.d.ts +30 -2
- package/dist/core/lifecycle.d.ts.map +1 -1
- package/dist/core/lifecycle.js +100 -13
- package/dist/core/lifecycle.js.map +1 -1
- package/dist/core/module-graph.d.ts +7 -1
- package/dist/core/module-graph.d.ts.map +1 -1
- package/dist/core/module-graph.js +90 -21
- package/dist/core/module-graph.js.map +1 -1
- package/dist/core/module-inspection.d.ts +44 -0
- package/dist/core/module-inspection.d.ts.map +1 -0
- package/dist/core/module-inspection.js +114 -0
- package/dist/core/module-inspection.js.map +1 -0
- package/dist/core/module-runtime.d.ts +22 -0
- package/dist/core/module-runtime.d.ts.map +1 -0
- package/dist/core/module-runtime.js +252 -0
- package/dist/core/module-runtime.js.map +1 -0
- package/dist/core/module.d.ts +5 -1
- package/dist/core/module.d.ts.map +1 -1
- package/dist/core/module.js +15 -3
- package/dist/core/module.js.map +1 -1
- package/dist/core/request-scope.d.ts +1 -1
- package/dist/core/request-scope.d.ts.map +1 -1
- package/dist/core/request-scope.js +10 -8
- package/dist/core/request-scope.js.map +1 -1
- package/dist/core/types.d.ts +106 -10
- package/dist/core/types.d.ts.map +1 -1
- package/dist/email/compiler/compiler.d.ts.map +1 -1
- package/dist/email/compiler/compiler.js +24 -13
- package/dist/email/compiler/compiler.js.map +1 -1
- package/dist/email/email-service.d.ts +0 -4
- package/dist/email/email-service.d.ts.map +1 -1
- package/dist/email/email-service.js +2 -29
- package/dist/email/email-service.js.map +1 -1
- package/dist/email/html-to-text.d.ts +14 -0
- package/dist/email/html-to-text.d.ts.map +1 -0
- package/dist/email/html-to-text.js +47 -0
- package/dist/email/html-to-text.js.map +1 -0
- package/dist/email/index.d.ts +1 -0
- package/dist/email/index.d.ts.map +1 -1
- package/dist/email/index.js +1 -0
- package/dist/email/index.js.map +1 -1
- package/dist/email/ses.d.ts +7 -0
- package/dist/email/ses.d.ts.map +1 -0
- package/dist/email/ses.js +6 -0
- package/dist/email/ses.js.map +1 -0
- package/dist/email/smtp.d.ts +7 -0
- package/dist/email/smtp.d.ts.map +1 -0
- package/dist/email/smtp.js +6 -0
- package/dist/email/smtp.js.map +1 -0
- package/dist/email/transports/ses-transport.d.ts.map +1 -1
- package/dist/email/transports/ses-transport.js +6 -4
- package/dist/email/transports/ses-transport.js.map +1 -1
- package/dist/email/transports/smtp-transport.d.ts +40 -0
- package/dist/email/transports/smtp-transport.d.ts.map +1 -0
- package/dist/email/transports/smtp-transport.js +67 -0
- package/dist/email/transports/smtp-transport.js.map +1 -0
- package/dist/http/error-handler.d.ts +7 -1
- package/dist/http/error-handler.d.ts.map +1 -1
- package/dist/http/error-handler.js +18 -6
- package/dist/http/error-handler.js.map +1 -1
- package/dist/http/error-response.dto.d.ts +6 -0
- package/dist/http/error-response.dto.d.ts.map +1 -1
- package/dist/http/error-response.dto.js +6 -0
- package/dist/http/error-response.dto.js.map +1 -1
- package/dist/http/errors.d.ts +12 -0
- package/dist/http/errors.d.ts.map +1 -1
- package/dist/http/errors.js +35 -23
- package/dist/http/errors.js.map +1 -1
- package/dist/http/index.d.ts +1 -1
- package/dist/http/index.d.ts.map +1 -1
- package/dist/http/index.js +1 -1
- package/dist/http/index.js.map +1 -1
- package/dist/http/route-authorization.d.ts.map +1 -1
- package/dist/http/route-authorization.js +3 -3
- package/dist/http/route-authorization.js.map +1 -1
- package/dist/http/route-helpers.d.ts +2 -1
- package/dist/http/route-helpers.d.ts.map +1 -1
- package/dist/http/route-helpers.js +29 -2
- package/dist/http/route-helpers.js.map +1 -1
- package/dist/http/route-openapi-context.d.ts +15 -1
- package/dist/http/route-openapi-context.d.ts.map +1 -1
- package/dist/http/route-openapi-context.js.map +1 -1
- package/dist/http/route-response.d.ts +6 -2
- package/dist/http/route-response.d.ts.map +1 -1
- package/dist/http/route-response.js +36 -16
- package/dist/http/route-response.js.map +1 -1
- package/dist/http/route-types.d.ts +12 -4
- package/dist/http/route-types.d.ts.map +1 -1
- package/dist/http/route-types.js.map +1 -1
- package/dist/http/route-validation.d.ts.map +1 -1
- package/dist/http/route-validation.js +21 -1
- package/dist/http/route-validation.js.map +1 -1
- package/dist/jobs/drivers/eventbridge-scheduler-driver.d.ts +1 -0
- package/dist/jobs/drivers/eventbridge-scheduler-driver.d.ts.map +1 -1
- package/dist/jobs/drivers/eventbridge-scheduler-driver.js +12 -2
- package/dist/jobs/drivers/eventbridge-scheduler-driver.js.map +1 -1
- package/dist/jobs/drivers/mock-driver.d.ts +4 -0
- package/dist/jobs/drivers/mock-driver.d.ts.map +1 -1
- package/dist/jobs/drivers/mock-driver.js +10 -3
- package/dist/jobs/drivers/mock-driver.js.map +1 -1
- package/dist/jobs/drivers/sqs-driver.d.ts +1 -1
- package/dist/jobs/drivers/sqs-driver.d.ts.map +1 -1
- package/dist/jobs/drivers/sqs-driver.js +8 -2
- package/dist/jobs/drivers/sqs-driver.js.map +1 -1
- package/dist/jobs/errors.d.ts +14 -1
- package/dist/jobs/errors.d.ts.map +1 -1
- package/dist/jobs/errors.js +19 -2
- package/dist/jobs/errors.js.map +1 -1
- package/dist/jobs/index.d.ts +3 -3
- package/dist/jobs/index.d.ts.map +1 -1
- package/dist/jobs/index.js +1 -1
- package/dist/jobs/index.js.map +1 -1
- package/dist/jobs/job-service.d.ts +46 -7
- package/dist/jobs/job-service.d.ts.map +1 -1
- package/dist/jobs/job-service.js +63 -9
- package/dist/jobs/job-service.js.map +1 -1
- package/dist/jobs/scheduler.d.ts +6 -0
- package/dist/jobs/scheduler.d.ts.map +1 -0
- package/dist/jobs/scheduler.js +6 -0
- package/dist/jobs/scheduler.js.map +1 -0
- package/dist/jobs/sqs.d.ts +7 -0
- package/dist/jobs/sqs.d.ts.map +1 -0
- package/dist/jobs/sqs.js +7 -0
- package/dist/jobs/sqs.js.map +1 -0
- package/dist/jobs/types.d.ts +6 -1
- package/dist/jobs/types.d.ts.map +1 -1
- package/dist/jobs/utils.d.ts +6 -0
- package/dist/jobs/utils.d.ts.map +1 -1
- package/dist/jobs/utils.js +8 -0
- package/dist/jobs/utils.js.map +1 -1
- package/dist/jobs/worker.d.ts.map +1 -1
- package/dist/jobs/worker.js +28 -23
- package/dist/jobs/worker.js.map +1 -1
- package/dist/middleware/rate-limit.d.ts.map +1 -1
- package/dist/middleware/rate-limit.js +5 -4
- package/dist/middleware/rate-limit.js.map +1 -1
- package/dist/observability/exception-tracking.d.ts +23 -0
- package/dist/observability/exception-tracking.d.ts.map +1 -1
- package/dist/observability/exception-tracking.js +101 -6
- package/dist/observability/exception-tracking.js.map +1 -1
- package/dist/observability/index.d.ts +1 -1
- package/dist/observability/index.d.ts.map +1 -1
- package/dist/observability/index.js +1 -1
- package/dist/observability/index.js.map +1 -1
- package/dist/observability/sanitizers.d.ts.map +1 -1
- package/dist/observability/sanitizers.js +2 -0
- package/dist/observability/sanitizers.js.map +1 -1
- package/dist/openapi/drift.d.ts +17 -2
- package/dist/openapi/drift.d.ts.map +1 -1
- package/dist/openapi/drift.js +79 -5
- package/dist/openapi/drift.js.map +1 -1
- package/dist/openapi/openapi.d.ts.map +1 -1
- package/dist/openapi/openapi.js +2 -1
- package/dist/openapi/openapi.js.map +1 -1
- package/dist/utils/environment.d.ts +10 -2
- package/dist/utils/environment.d.ts.map +1 -1
- package/dist/utils/environment.js +20 -3
- package/dist/utils/environment.js.map +1 -1
- package/dist/utils/module-interop.d.ts +9 -0
- package/dist/utils/module-interop.d.ts.map +1 -0
- package/dist/utils/module-interop.js +16 -0
- package/dist/utils/module-interop.js.map +1 -0
- package/dist/utils/optional-dependency.d.ts +6 -0
- package/dist/utils/optional-dependency.d.ts.map +1 -0
- package/dist/utils/optional-dependency.js +16 -0
- package/dist/utils/optional-dependency.js.map +1 -0
- package/dist/utils/serialize-prisma-types.d.ts +51 -15
- package/dist/utils/serialize-prisma-types.d.ts.map +1 -1
- package/dist/utils/serialize-prisma-types.js +47 -16
- package/dist/utils/serialize-prisma-types.js.map +1 -1
- package/package.json +56 -12
package/dist/openapi/drift.js
CHANGED
|
@@ -1,18 +1,92 @@
|
|
|
1
|
-
import { readFile } from 'node:fs/promises';
|
|
1
|
+
import { readFile, writeFile } from 'node:fs/promises';
|
|
2
2
|
import { isDeepStrictEqual } from 'node:util';
|
|
3
|
+
const HTTP_METHODS = new Set(['get', 'put', 'post', 'delete', 'options', 'head', 'patch', 'trace']);
|
|
4
|
+
const MAX_LISTED = 20;
|
|
3
5
|
export class OpenAPIDriftError extends Error {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
+
differences;
|
|
7
|
+
constructor(filePath, differences = []) {
|
|
8
|
+
super(formatDrift(filePath, differences));
|
|
6
9
|
this.name = 'OpenAPIDriftError';
|
|
10
|
+
this.differences = differences;
|
|
7
11
|
}
|
|
8
12
|
}
|
|
9
|
-
/**
|
|
13
|
+
/**
|
|
14
|
+
* Compares JSON documents without changing the committed contract file. On a mismatch the
|
|
15
|
+
* error lists the changed operations and schemas.
|
|
16
|
+
*/
|
|
10
17
|
export async function assertOpenAPIMatches(document, filePath) {
|
|
11
18
|
const expected = JSON.parse(await readFile(filePath, 'utf8'));
|
|
12
19
|
// Normalize to JSON so omitted undefined properties match the written artifact.
|
|
13
20
|
const actual = JSON.parse(JSON.stringify(document));
|
|
14
21
|
if (!isDeepStrictEqual(expected, actual)) {
|
|
15
|
-
throw new OpenAPIDriftError(filePath);
|
|
22
|
+
throw new OpenAPIDriftError(filePath, diffOpenAPI(expected, actual));
|
|
16
23
|
}
|
|
17
24
|
}
|
|
25
|
+
/** Writes a document in the form `assertOpenAPIMatches()` reads: indented JSON. */
|
|
26
|
+
export async function writeOpenAPIDocument(document, filePath) {
|
|
27
|
+
await writeFile(filePath, `${JSON.stringify(document, null, 2)}\n`, 'utf8');
|
|
28
|
+
}
|
|
29
|
+
/** Lists where two JSON documents differ, at the deepest object key that differs. */
|
|
30
|
+
export function diffOpenAPI(expected, actual) {
|
|
31
|
+
const differences = [];
|
|
32
|
+
collect(expected, actual, '', differences);
|
|
33
|
+
return differences;
|
|
34
|
+
}
|
|
35
|
+
function isObject(value) {
|
|
36
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
37
|
+
}
|
|
38
|
+
function collect(expected, actual, pointer, differences) {
|
|
39
|
+
if (isDeepStrictEqual(expected, actual))
|
|
40
|
+
return;
|
|
41
|
+
if (!isObject(expected) || !isObject(actual)) {
|
|
42
|
+
differences.push({ kind: 'changed', pointer });
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
for (const key of new Set([...Object.keys(expected), ...Object.keys(actual)])) {
|
|
46
|
+
const child = `${pointer}/${key.replaceAll('~', '~0').replaceAll('/', '~1')}`;
|
|
47
|
+
if (!(key in actual))
|
|
48
|
+
differences.push({ kind: 'removed', pointer: child });
|
|
49
|
+
else if (!(key in expected))
|
|
50
|
+
differences.push({ kind: 'added', pointer: child });
|
|
51
|
+
else
|
|
52
|
+
collect(expected[key], actual[key], child, differences);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function unescapePointerToken(token) {
|
|
56
|
+
return token.replaceAll('~1', '/').replaceAll('~0', '~');
|
|
57
|
+
}
|
|
58
|
+
/** `GET /api/projects` or `schema ProjectDto` for a pointer, with the rest of the pointer. */
|
|
59
|
+
function describe(pointer) {
|
|
60
|
+
const tokens = pointer.split('/').slice(1);
|
|
61
|
+
if (tokens[0] === 'paths' && tokens[1] !== undefined) {
|
|
62
|
+
const path = unescapePointerToken(tokens[1]);
|
|
63
|
+
if (tokens[2] !== undefined && HTTP_METHODS.has(tokens[2])) {
|
|
64
|
+
return { subject: `${tokens[2].toUpperCase()} ${path}`, detail: tokens.slice(3).join('/') };
|
|
65
|
+
}
|
|
66
|
+
return { subject: path, detail: tokens.slice(2).join('/') };
|
|
67
|
+
}
|
|
68
|
+
if (tokens[0] === 'components' && tokens[1] === 'schemas' && tokens[2] !== undefined) {
|
|
69
|
+
return {
|
|
70
|
+
subject: `schema ${unescapePointerToken(tokens[2])}`,
|
|
71
|
+
detail: tokens.slice(3).join('/'),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
return { subject: pointer || '(document)', detail: '' };
|
|
75
|
+
}
|
|
76
|
+
function formatDrift(filePath, differences) {
|
|
77
|
+
const symbol = { added: '+', removed: '-', changed: '~' };
|
|
78
|
+
const lines = differences.slice(0, MAX_LISTED).map(({ kind, pointer }) => {
|
|
79
|
+
const { subject, detail } = describe(pointer);
|
|
80
|
+
const kindOfWhole = detail === '' ? kind : 'changed';
|
|
81
|
+
return ` ${symbol[kindOfWhole]} ${subject}${detail ? `: ${detail} ${kind}` : ''}`;
|
|
82
|
+
});
|
|
83
|
+
if (differences.length > MAX_LISTED) {
|
|
84
|
+
lines.push(` … and ${differences.length - MAX_LISTED} more`);
|
|
85
|
+
}
|
|
86
|
+
return [
|
|
87
|
+
`OpenAPI contract drift in ${filePath}${lines.length ? ':' : ''}`,
|
|
88
|
+
...lines,
|
|
89
|
+
`Regenerate and commit ${filePath} if the change is intended.`,
|
|
90
|
+
].join('\n');
|
|
91
|
+
}
|
|
18
92
|
//# sourceMappingURL=drift.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"drift.js","sourceRoot":"","sources":["../../src/openapi/drift.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"drift.js","sourceRoot":"","sources":["../../src/openapi/drift.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACvD,OAAO,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAC;AAU9C,MAAM,YAAY,GA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6BAA6B;KAC/D,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACf,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openapi.d.ts","sourceRoot":"","sources":["../../src/openapi/openapi.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,IAAI,EAAqB,MAAM,MAAM,CAAC;AACpD,OAAO,EAAE,aAAa,EAAuB,MAAM,cAAc,CAAC;AAElE,OAAO,KAAK,EACV,WAAW,EACX,iBAAiB,EACjB,cAAc,EACd,gBAAgB,EACjB,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAgB,KAAK,MAAM,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"openapi.d.ts","sourceRoot":"","sources":["../../src/openapi/openapi.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,IAAI,EAAqB,MAAM,MAAM,CAAC;AACpD,OAAO,EAAE,aAAa,EAAuB,MAAM,cAAc,CAAC;AAElE,OAAO,KAAK,EACV,WAAW,EACX,iBAAiB,EACjB,cAAc,EACd,gBAAgB,EACjB,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAgB,KAAK,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAI/D,MAAM,WAAW,uBAAuB;IACtC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,GAAG,EAAE,IAAI,CAAC;IACV,WAAW,EAAE,WAAW,CAAC;IACzB,OAAO,EAAE,cAAc,CAAC;IACxB,SAAS,CAAC,EAAE,gBAAgB,CAAC;IAC7B,UAAU,CAAC,EAAE,iBAAiB,CAAC;CAChC;AAED;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACrC,uEAAuE;IACvE,YAAY,CAAC,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,UAAU,CAAC,OAAO,aAAa,CAAC,CAAC,CAAC,CAAC;IACxE;;;;;;;;;;;;;;OAcG;IACH,SAAS,CAAC,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CACjC;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,uBAAuB,GAAG,sBAAsB,CAiEzF"}
|
package/dist/openapi/openapi.js
CHANGED
|
@@ -2,6 +2,7 @@ import { writeFile } from 'node:fs/promises';
|
|
|
2
2
|
import { generateSpecs, openAPIRouteHandler } from 'hono-openapi';
|
|
3
3
|
import { deepMerge } from '../utils/deep-merge.js';
|
|
4
4
|
import { createLogger } from '../utils/logger.js';
|
|
5
|
+
import { namedExports } from '../utils/module-interop.js';
|
|
5
6
|
import { defaultOpenAPIComponents } from './defaults.js';
|
|
6
7
|
/**
|
|
7
8
|
* Configure OpenAPI documentation for the application.
|
|
@@ -51,7 +52,7 @@ export function configureOpenAPI(options) {
|
|
|
51
52
|
let handlerPromise;
|
|
52
53
|
app.get('/api', async (c, next) => {
|
|
53
54
|
handlerPromise ??= import('@hono/swagger-ui')
|
|
54
|
-
.then((
|
|
55
|
+
.then((module) => namedExports(module).swaggerUI({ url: '/api/openapi.json' }))
|
|
55
56
|
.catch((cause) => {
|
|
56
57
|
throw new Error('Unable to load Swagger UI. Install the optional peer with: npm install @hono/swagger-ui', { cause });
|
|
57
58
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openapi.js","sourceRoot":"","sources":["../../src/openapi/openapi.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAE7C,OAAO,EAAE,aAAa,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAQlE,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACnD,OAAO,EAAE,YAAY,EAAe,MAAM,oBAAoB,CAAC;AAC/D,OAAO,EAAE,wBAAwB,EAAE,MAAM,eAAe,CAAC;AAmCzD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,UAAU,gBAAgB,CAAC,OAAgC;IAC/D,MAAM,E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* `NODE_ENV=production` and `NODE_ENV=test` are used as-is. Otherwise AWS Lambda
|
|
12
|
+
* counts as production and everything else as development. This is the default
|
|
13
|
+
* for the `environment` option of `bootstrap()`.
|
|
14
|
+
*/
|
|
15
|
+
export declare function detectEnvironment(): 'development' | 'production' | 'test';
|
|
16
|
+
/**
|
|
17
|
+
* Check if the detected environment is production
|
|
18
|
+
* (`NODE_ENV=production`, or AWS Lambda unless `NODE_ENV=test`).
|
|
11
19
|
*/
|
|
12
20
|
export declare function isProduction(): boolean;
|
|
13
21
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"environment.d.ts","sourceRoot":"","sources":["../../src/utils/environment.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;GAEG;AACH,wBAAgB,QAAQ,IAAI,OAAO,CAElC;AAED;;;GAGG;AACH,wBAAgB,YAAY,IAAI,OAAO,CAEtC;AAED;;;GAGG;AACH,wBAAgB,MAAM,IAAI,OAAO,CAUhC;AAED;;GAEG;AACH,wBAAgB,aAAa,IAAI,OAAO,CAEvC"}
|
|
1
|
+
{"version":3,"file":"environment.d.ts","sourceRoot":"","sources":["../../src/utils/environment.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;GAEG;AACH,wBAAgB,QAAQ,IAAI,OAAO,CAElC;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,IAAI,aAAa,GAAG,YAAY,GAAG,MAAM,CAMzE;AAED;;;GAGG;AACH,wBAAgB,YAAY,IAAI,OAAO,CAEtC;AAED;;;GAGG;AACH,wBAAgB,MAAM,IAAI,OAAO,CAUhC;AAED;;GAEG;AACH,wBAAgB,aAAa,IAAI,OAAO,CAEvC"}
|
|
@@ -8,11 +8,28 @@ export function isLambda() {
|
|
|
8
8
|
return !!(process.env.AWS_LAMBDA_FUNCTION_NAME || process.env.LAMBDA_TASK_ROOT);
|
|
9
9
|
}
|
|
10
10
|
/**
|
|
11
|
-
*
|
|
12
|
-
*
|
|
11
|
+
* Detect the runtime environment.
|
|
12
|
+
*
|
|
13
|
+
* `NODE_ENV=production` and `NODE_ENV=test` are used as-is. Otherwise AWS Lambda
|
|
14
|
+
* counts as production and everything else as development. This is the default
|
|
15
|
+
* for the `environment` option of `bootstrap()`.
|
|
16
|
+
*/
|
|
17
|
+
export function detectEnvironment() {
|
|
18
|
+
const nodeEnv = process.env.NODE_ENV;
|
|
19
|
+
if (nodeEnv === 'production')
|
|
20
|
+
return 'production';
|
|
21
|
+
if (nodeEnv === 'test')
|
|
22
|
+
return 'test';
|
|
23
|
+
if (isLambda())
|
|
24
|
+
return 'production';
|
|
25
|
+
return 'development';
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Check if the detected environment is production
|
|
29
|
+
* (`NODE_ENV=production`, or AWS Lambda unless `NODE_ENV=test`).
|
|
13
30
|
*/
|
|
14
31
|
export function isProduction() {
|
|
15
|
-
return
|
|
32
|
+
return detectEnvironment() === 'production';
|
|
16
33
|
}
|
|
17
34
|
/**
|
|
18
35
|
* Check if running in test environment.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"environment.js","sourceRoot":"","sources":["../../src/utils/environment.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;GAEG;AACH,MAAM,UAAU,QAAQ;IACtB,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,wBAAwB,IAAI,OAAO,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC;AAClF,CAAC;AAED
|
|
1
|
+
{"version":3,"file":"environment.js","sourceRoot":"","sources":["../../src/utils/environment.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;GAEG;AACH,MAAM,UAAU,QAAQ;IACtB,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,wBAAwB,IAAI,OAAO,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC;AAClF,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,iBAAiB;IAC/B,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC;IACrC,IAAI,OAAO,KAAK,YAAY;QAAE,OAAO,YAAY,CAAC;IAClD,IAAI,OAAO,KAAK,MAAM;QAAE,OAAO,MAAM,CAAC;IACtC,IAAI,QAAQ,EAAE;QAAE,OAAO,YAAY,CAAC;IACpC,OAAO,aAAa,CAAC;AACvB,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,YAAY;IAC1B,OAAO,iBAAiB,EAAE,KAAK,YAAY,CAAC;AAC9C,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,MAAM;IACpB,uBAAuB;IACvB,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,MAAM,EAAE,CAAC;QACpC,OAAO,IAAI,CAAC;IACd,CAAC;IACD,wEAAwE;IACxE,IAAI,OAAO,CAAC,GAAG,CAAC,MAAM,KAAK,MAAM,IAAI,OAAO,CAAC,GAAG,CAAC,cAAc,EAAE,CAAC;QAChE,OAAO,IAAI,CAAC;IACd,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,aAAa;IAC3B,OAAO,CAAC,YAAY,EAAE,IAAI,CAAC,MAAM,EAAE,CAAC;AACtC,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Named exports of a dynamically imported module.
|
|
3
|
+
*
|
|
4
|
+
* Node exposes the named exports of a CommonJS package through `import()`, but a bundler that
|
|
5
|
+
* turns the package into an ESM chunk may expose them only as `default`. Optional peers such
|
|
6
|
+
* as the AWS SDK clients are CommonJS packages, so read their exports from either place.
|
|
7
|
+
*/
|
|
8
|
+
export declare function namedExports<T extends object>(module: T): T;
|
|
9
|
+
//# sourceMappingURL=module-interop.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"module-interop.d.ts","sourceRoot":"","sources":["../../src/utils/module-interop.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,CAAC,SAAS,MAAM,EAAE,MAAM,EAAE,CAAC,GAAG,CAAC,CAO3D"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Named exports of a dynamically imported module.
|
|
3
|
+
*
|
|
4
|
+
* Node exposes the named exports of a CommonJS package through `import()`, but a bundler that
|
|
5
|
+
* turns the package into an ESM chunk may expose them only as `default`. Optional peers such
|
|
6
|
+
* as the AWS SDK clients are CommonJS packages, so read their exports from either place.
|
|
7
|
+
*/
|
|
8
|
+
export function namedExports(module) {
|
|
9
|
+
// Check first: module mocks may throw for an export they do not define.
|
|
10
|
+
const fallback = 'default' in module ? module.default : undefined;
|
|
11
|
+
if (fallback === null || (typeof fallback !== 'object' && typeof fallback !== 'function')) {
|
|
12
|
+
return module;
|
|
13
|
+
}
|
|
14
|
+
return { ...fallback, ...module };
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=module-interop.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"module-interop.js","sourceRoot":"","sources":["../../src/utils/module-interop.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,UAAU,YAAY,CAAmB,MAAS;IACtD,wEAAwE;IACxE,MAAM,QAAQ,GAAG,SAAS,IAAI,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC;IAClE,IAAI,QAAQ,KAAK,IAAI,IAAI,CAAC,OAAO,QAAQ,KAAK,QAAQ,IAAI,OAAO,QAAQ,KAAK,UAAU,CAAC,EAAE,CAAC;QAC1F,OAAO,MAAM,CAAC;IAChB,CAAC;IACD,OAAO,EAAE,GAAG,QAAQ,EAAE,GAAG,MAAM,EAAO,CAAC;AACzC,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Loads an optional peer the first time an adapter needs it, so importing the adapter's entry
|
|
3
|
+
* point works without the peer installed. A missing peer fails with an install hint.
|
|
4
|
+
*/
|
|
5
|
+
export declare function optionalDependency<T extends object>(name: string, user: string, load: () => Promise<T>): () => Promise<T>;
|
|
6
|
+
//# sourceMappingURL=optional-dependency.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"optional-dependency.d.ts","sourceRoot":"","sources":["../../src/utils/optional-dependency.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,SAAS,MAAM,EACjD,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GACrB,MAAM,OAAO,CAAC,CAAC,CAAC,CASlB"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { namedExports } from './module-interop.js';
|
|
2
|
+
/**
|
|
3
|
+
* Loads an optional peer the first time an adapter needs it, so importing the adapter's entry
|
|
4
|
+
* point works without the peer installed. A missing peer fails with an install hint.
|
|
5
|
+
*/
|
|
6
|
+
export function optionalDependency(name, user, load) {
|
|
7
|
+
let loading;
|
|
8
|
+
return () => {
|
|
9
|
+
loading ??= load().then(namedExports, (cause) => {
|
|
10
|
+
loading = undefined;
|
|
11
|
+
throw new Error(`${user} requires ${name}. Install it with: npm install ${name}`, { cause });
|
|
12
|
+
});
|
|
13
|
+
return loading;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=optional-dependency.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"optional-dependency.js","sourceRoot":"","sources":["../../src/utils/optional-dependency.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEnD;;;GAGG;AACH,MAAM,UAAU,kBAAkB,CAChC,IAAY,EACZ,IAAY,EACZ,IAAsB;IAEtB,IAAI,OAA+B,CAAC;IACpC,OAAO,GAAG,EAAE;QACV,OAAO,KAAK,IAAI,EAAE,CAAC,IAAI,CAAC,YAAY,EAAE,CAAC,KAAc,EAAE,EAAE;YACvD,OAAO,GAAG,SAAS,CAAC;YACpB,MAAM,IAAI,KAAK,CAAC,GAAG,IAAI,aAAa,IAAI,kCAAkC,IAAI,EAAE,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;QAC/F,CAAC,CAAC,CAAC;QACH,OAAO,OAAO,CAAC;IACjB,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -1,34 +1,62 @@
|
|
|
1
|
+
import type { ConfiguredDecimalSerialization, DecimalSerialization } from '../core/glasswork-config.js';
|
|
1
2
|
/**
|
|
2
3
|
* Type helper to recursively convert Date and Decimal fields in an object type
|
|
3
4
|
* - Date → string (ISO 8601)
|
|
4
|
-
* - Decimal → number
|
|
5
|
+
* - Decimal → number, or string when `TDecimals` is `'string'`
|
|
5
6
|
*
|
|
6
7
|
* Note: Decimal is from Prisma's Decimal.js, but we don't import the type
|
|
7
|
-
* to avoid hard dependency on @prisma/client
|
|
8
|
+
* to avoid hard dependency on @prisma/client. It is matched structurally through
|
|
9
|
+
* {@link PrismaDecimalLike}.
|
|
8
10
|
*/
|
|
9
|
-
export type SerializedTypes<T> = T extends Date ? string : T extends {
|
|
11
|
+
export type SerializedTypes<T, TDecimals extends DecimalSerialization = 'number'> = T extends Date ? string : T extends PrismaDecimalLike | {
|
|
10
12
|
constructor: {
|
|
11
13
|
name: 'Decimal';
|
|
12
14
|
};
|
|
13
|
-
} ? number : T extends Array<infer U> ? Array<SerializedTypes<U>> : T extends object ? {
|
|
14
|
-
[K in keyof T]: SerializedTypes<T[K]>;
|
|
15
|
+
} ? TDecimals extends 'string' ? string : number : T extends Array<infer U> ? Array<SerializedTypes<U, TDecimals>> : T extends object ? {
|
|
16
|
+
[K in keyof T]: SerializedTypes<T[K], TDecimals>;
|
|
15
17
|
} : T;
|
|
16
18
|
/**
|
|
17
19
|
* Type for a transformer function that converts a value to a serializable type
|
|
18
20
|
*/
|
|
19
21
|
export type TypeTransformer = (value: unknown) => unknown;
|
|
20
22
|
/**
|
|
21
|
-
* Configuration for type serialization
|
|
23
|
+
* Configuration for type serialization. Build one with {@link createSerializationConfig}, or
|
|
24
|
+
* list the transformers directly.
|
|
22
25
|
*/
|
|
23
|
-
export interface SerializationConfig {
|
|
26
|
+
export interface SerializationConfig<TDecimals extends DecimalSerialization = 'number'> {
|
|
24
27
|
/**
|
|
25
28
|
* Custom transformers to apply to specific types
|
|
26
29
|
* The function should return the transformed value or undefined if not applicable
|
|
27
30
|
*/
|
|
28
31
|
transformers: TypeTransformer[];
|
|
32
|
+
/**
|
|
33
|
+
* How the built-in transformer serializes Decimal values; `'number'` when absent. Set by
|
|
34
|
+
* {@link createSerializationConfig}, which picks the matching transformer.
|
|
35
|
+
*/
|
|
36
|
+
decimals?: TDecimals;
|
|
29
37
|
}
|
|
30
38
|
/**
|
|
31
|
-
*
|
|
39
|
+
* Build a serialization configuration: your transformers first, then the built-in Date and
|
|
40
|
+
* Decimal handling. `bootstrap()` builds the application-wide configuration this way from its
|
|
41
|
+
* `serialization` option; use this for calls to `serializePrismaTypes()` outside routes.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* const config = createSerializationConfig({
|
|
46
|
+
* decimals: 'string',
|
|
47
|
+
* transformers: [moneyTransformer],
|
|
48
|
+
* });
|
|
49
|
+
*
|
|
50
|
+
* serializePrismaTypes({ price: new Decimal('10.50') }, config);
|
|
51
|
+
* // { price: '10.50' }
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export declare function createSerializationConfig<TDecimals extends DecimalSerialization = 'number'>(options?: {
|
|
55
|
+
decimals?: TDecimals;
|
|
56
|
+
transformers?: TypeTransformer[];
|
|
57
|
+
}): SerializationConfig<TDecimals>;
|
|
58
|
+
/**
|
|
59
|
+
* Default configuration with built-in transformers for Date and Decimal types (as numbers).
|
|
32
60
|
* Export this if you want to extend it with custom transformers.
|
|
33
61
|
*
|
|
34
62
|
* @example
|
|
@@ -51,7 +79,7 @@ export declare const defaultConfig: SerializationConfig;
|
|
|
51
79
|
*
|
|
52
80
|
* This utility handles the mismatch between Prisma's data types and API response DTOs:
|
|
53
81
|
* - Date objects → ISO 8601 strings
|
|
54
|
-
* - Decimal objects → numbers
|
|
82
|
+
* - Decimal objects → numbers, or strings with a `decimals: 'string'` configuration
|
|
55
83
|
*
|
|
56
84
|
* It preserves null values and handles nested objects and arrays.
|
|
57
85
|
* You can extend it with custom transformers for additional types.
|
|
@@ -62,8 +90,9 @@ export declare const defaultConfig: SerializationConfig;
|
|
|
62
90
|
* you'll need to use type assertions or `strictTypes: true` in your route handlers.
|
|
63
91
|
*
|
|
64
92
|
* @template T - The type of data to transform
|
|
93
|
+
* @template TDecimals - The Decimal serialization of `config`, which decides the result type
|
|
65
94
|
* @param data - The data to transform (object, array, or primitive)
|
|
66
|
-
* @param config - Optional configuration
|
|
95
|
+
* @param config - Optional configuration, see {@link createSerializationConfig}
|
|
67
96
|
* @returns The same structure with Prisma types converted to JSON-serializable types
|
|
68
97
|
*
|
|
69
98
|
* @example
|
|
@@ -103,7 +132,7 @@ export declare const defaultConfig: SerializationConfig;
|
|
|
103
132
|
* }
|
|
104
133
|
* ```
|
|
105
134
|
*/
|
|
106
|
-
export declare function serializePrismaTypes<T>(data: T, config?: SerializationConfig
|
|
135
|
+
export declare function serializePrismaTypes<T, TDecimals extends DecimalSerialization = 'number'>(data: T, config?: SerializationConfig<TDecimals>, _depth?: number, _ancestors?: WeakSet<object>): SerializedTypes<T, TDecimals>;
|
|
107
136
|
/**
|
|
108
137
|
* Type representing Prisma's Decimal type structure.
|
|
109
138
|
* We use a structural type to avoid importing from @prisma/client.
|
|
@@ -136,6 +165,8 @@ type DateFieldPattern = `${string}At` | `${string}Date` | `${string}Time` | `${s
|
|
|
136
165
|
* - For Date fields: Only string fields with conventional date names (ending in `At`, `Date`, `Time`, etc.)
|
|
137
166
|
* accept Date objects. Other string fields only accept strings.
|
|
138
167
|
* - For Decimal fields: All number fields accept Decimal-like objects (see rationale below).
|
|
168
|
+
* When `GlassworkConfig` declares `decimals: 'string'`, the non-date string fields accept
|
|
169
|
+
* them instead, because Decimals then serialize to strings.
|
|
139
170
|
* - Recursively applies to objects and arrays.
|
|
140
171
|
*
|
|
141
172
|
* **Date Field Detection:**
|
|
@@ -208,12 +239,17 @@ type DateFieldPattern = `${string}At` | `${string}Date` | `${string}Time` | `${s
|
|
|
208
239
|
*/
|
|
209
240
|
type WithDateSupport<T> = T extends string | infer Rest ? string | Date | Rest : T;
|
|
210
241
|
/**
|
|
211
|
-
*
|
|
212
|
-
|
|
242
|
+
* The type a Decimal serializes into, as declared in `GlassworkConfig` (`number` by default).
|
|
243
|
+
*/
|
|
244
|
+
type DecimalField = ConfiguredDecimalSerialization extends 'string' ? string : number;
|
|
245
|
+
/**
|
|
246
|
+
* Helper type to add Decimal support to the fields Decimals serialize into (including nullable
|
|
247
|
+
* ones). Preserves null and undefined in unions. Literal unions such as `'open' | 'closed'`
|
|
248
|
+
* and `1 | 2` stay as they are, because a Decimal never serializes into one of them.
|
|
213
249
|
*/
|
|
214
|
-
type WithDecimalSupport<T> =
|
|
250
|
+
type WithDecimalSupport<T> = DecimalField extends T ? T | PrismaDecimalLike : T;
|
|
215
251
|
export type AcceptPrismaTypes<T, AdditionalDatePatterns extends string = never> = T extends object ? T extends Array<infer U> ? Array<AcceptPrismaTypes<U, AdditionalDatePatterns>> : {
|
|
216
|
-
[K in keyof T]: K extends string ? K extends DateFieldPattern | AdditionalDatePatterns ? T[K] extends string | null | undefined ? WithDateSupport<T[K]> : AcceptPrismaTypes<T[K], AdditionalDatePatterns> : T[K] extends
|
|
252
|
+
[K in keyof T]: K extends string ? K extends DateFieldPattern | AdditionalDatePatterns ? T[K] extends string | null | undefined ? WithDateSupport<T[K]> : AcceptPrismaTypes<T[K], AdditionalDatePatterns> : T[K] extends DecimalField | null | undefined ? WithDecimalSupport<T[K]> : AcceptPrismaTypes<T[K], AdditionalDatePatterns> : AcceptPrismaTypes<T[K], AdditionalDatePatterns>;
|
|
217
253
|
} : T;
|
|
218
254
|
export {};
|
|
219
255
|
//# sourceMappingURL=serialize-prisma-types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"serialize-prisma-types.d.ts","sourceRoot":"","sources":["../../src/utils/serialize-prisma-types.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"serialize-prisma-types.d.ts","sourceRoot":"","sources":["../../src/utils/serialize-prisma-types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,8BAA8B,EAC9B,oBAAoB,EACrB,MAAM,6BAA6B,CAAC;AAErC;;;;;;;;GAQG;AACH,MAAM,MAAM,eAAe,CAAC,CAAC,EAAE,SAAS,SAAS,oBAAoB,GAAG,QAAQ,IAAI,CAAC,SAAS,IAAI,GAC9F,MAAM,GACN,CAAC,SAAS,iBAAiB,GAAG;IAAE,WAAW,EAAE;QAAE,IAAI,EAAE,SAAS,CAAA;KAAE,CAAA;CAAE,GAChE,SAAS,SAAS,QAAQ,GACxB,MAAM,GACN,MAAM,GACR,CAAC,SAAS,KAAK,CAAC,MAAM,CAAC,CAAC,GACtB,KAAK,CAAC,eAAe,CAAC,CAAC,EAAE,SAAS,CAAC,CAAC,GACpC,CAAC,SAAS,MAAM,GACd;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,SAAS,CAAC;CAAE,GACpD,CAAC,CAAC;AAEZ;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG,CAAC,KAAK,EAAE,OAAO,KAAK,OAAO,CAAC;AAE1D;;;GAGG;AACH,MAAM,WAAW,mBAAmB,CAAC,SAAS,SAAS,oBAAoB,GAAG,QAAQ;IACpF;;;OAGG;IACH,YAAY,EAAE,eAAe,EAAE,CAAC;IAChC;;;OAGG;IACH,QAAQ,CAAC,EAAE,SAAS,CAAC;CACt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A8B,SAAS,QAAQ,GAAG,MAAM,GAAG,MAAM,CAAC;AAEtF;;;;GAIG;AACH,KAAK,kBAAkB,CAAC,CAAC,IAAI,YAAY,SAAS,CAAC,GAAG,CAAC,GAAG,iBAAiB,GAAG,CAAC,CAAC;AAEhF,MAAM,MAAM,iBAAiB,CAAC,CAAC,EAAE,sBAAsB,SAAS,MAAM,GAAG,KAAK,IAAI,CAAC,SAAS,MAAM,GAC9F,CAAC,SAAS,KAAK,CAAC,MAAM,CAAC,CAAC,GACtB,KAAK,CAAC,iBAAiB,CAAC,CAAC,EAAE,sBAAsB,CAAC,CAAC,GACnD;KACG,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,SAAS,MAAM,GAC5B,CAAC,SAAS,gBAAgB,GAAG,sBAAsB,GACjD,CAAC,CAAC,CAAC,CAAC,SAAS,MAAM,GAAG,IAAI,GAAG,SAAS,GACpC,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GACrB,iBAAiB,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,sBAAsB,CAAC,GACjD,CAAC,CAAC,CAAC,CAAC,SAAS,YAAY,GAAG,IAAI,GAAG,SAAS,GAC1C,kBAAkB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GACxB,iBAAiB,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,sBAAsB,CAAC,GACnD,iBAAiB,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,sBAAsB,CAAC;CACpD,GACH,CAAC,CAAC"}
|
|
@@ -38,19 +38,51 @@ function isDecimal(value) {
|
|
|
38
38
|
return hasDecimalStructure || hasDecimalConstructor;
|
|
39
39
|
}
|
|
40
40
|
/**
|
|
41
|
-
*
|
|
41
|
+
* Serialize a Decimal in plain notation with every digit and the stored scale. `toString()`
|
|
42
|
+
* and `toJSON()` switch to exponential notation for large and small values.
|
|
42
43
|
*/
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
return
|
|
51
|
-
|
|
44
|
+
function decimalToString(value) {
|
|
45
|
+
return typeof value.toFixed === 'function' ? value.toFixed() : String(value);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Build the transformer that handles Date and Decimal types
|
|
49
|
+
*/
|
|
50
|
+
function createDefaultTransformer(decimals) {
|
|
51
|
+
return (value) => {
|
|
52
|
+
if (value instanceof Date) {
|
|
53
|
+
return value.toISOString();
|
|
54
|
+
}
|
|
55
|
+
if (isDecimal(value)) {
|
|
56
|
+
return decimals === 'string' ? decimalToString(value) : value.toNumber();
|
|
57
|
+
}
|
|
58
|
+
return undefined; // Not handled by this transformer
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Build a serialization configuration: your transformers first, then the built-in Date and
|
|
63
|
+
* Decimal handling. `bootstrap()` builds the application-wide configuration this way from its
|
|
64
|
+
* `serialization` option; use this for calls to `serializePrismaTypes()` outside routes.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```typescript
|
|
68
|
+
* const config = createSerializationConfig({
|
|
69
|
+
* decimals: 'string',
|
|
70
|
+
* transformers: [moneyTransformer],
|
|
71
|
+
* });
|
|
72
|
+
*
|
|
73
|
+
* serializePrismaTypes({ price: new Decimal('10.50') }, config);
|
|
74
|
+
* // { price: '10.50' }
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
export function createSerializationConfig(options = {}) {
|
|
78
|
+
const decimals = (options.decimals ?? 'number');
|
|
79
|
+
return {
|
|
80
|
+
decimals,
|
|
81
|
+
transformers: [...(options.transformers ?? []), createDefaultTransformer(decimals)],
|
|
82
|
+
};
|
|
83
|
+
}
|
|
52
84
|
/**
|
|
53
|
-
* Default configuration with built-in transformers for Date and Decimal types.
|
|
85
|
+
* Default configuration with built-in transformers for Date and Decimal types (as numbers).
|
|
54
86
|
* Export this if you want to extend it with custom transformers.
|
|
55
87
|
*
|
|
56
88
|
* @example
|
|
@@ -67,15 +99,13 @@ const defaultTransformer = (value) => {
|
|
|
67
99
|
* serializePrismaTypes(data, customConfig);
|
|
68
100
|
* ```
|
|
69
101
|
*/
|
|
70
|
-
export const defaultConfig =
|
|
71
|
-
transformers: [defaultTransformer],
|
|
72
|
-
};
|
|
102
|
+
export const defaultConfig = createSerializationConfig();
|
|
73
103
|
/**
|
|
74
104
|
* Recursively converts Prisma types to JSON-serializable equivalents.
|
|
75
105
|
*
|
|
76
106
|
* This utility handles the mismatch between Prisma's data types and API response DTOs:
|
|
77
107
|
* - Date objects → ISO 8601 strings
|
|
78
|
-
* - Decimal objects → numbers
|
|
108
|
+
* - Decimal objects → numbers, or strings with a `decimals: 'string'` configuration
|
|
79
109
|
*
|
|
80
110
|
* It preserves null values and handles nested objects and arrays.
|
|
81
111
|
* You can extend it with custom transformers for additional types.
|
|
@@ -86,8 +116,9 @@ export const defaultConfig = {
|
|
|
86
116
|
* you'll need to use type assertions or `strictTypes: true` in your route handlers.
|
|
87
117
|
*
|
|
88
118
|
* @template T - The type of data to transform
|
|
119
|
+
* @template TDecimals - The Decimal serialization of `config`, which decides the result type
|
|
89
120
|
* @param data - The data to transform (object, array, or primitive)
|
|
90
|
-
* @param config - Optional configuration
|
|
121
|
+
* @param config - Optional configuration, see {@link createSerializationConfig}
|
|
91
122
|
* @returns The same structure with Prisma types converted to JSON-serializable types
|
|
92
123
|
*
|
|
93
124
|
* @example
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"serialize-prisma-types.js","sourceRoot":"","sources":["../../src/utils/serialize-prisma-types.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"serialize-prisma-types.js","sourceRoot":"","sources":["../../src/utils/serialize-prisma-types.ts"],"names":[],"mappings":"AA8DA;;;GAGG;AACH,MAAM,uBAAuB,GAAG,EAAE,CAAC;AAEnC;;;;;;;;;GASG;AACH,SAAS,SAAS,CAAC,KAAc;IAC/B,IAAI,KAAK,KA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