@molecule/api-resource-version-history 1.0.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/LICENSE +115 -0
- package/dist/authorizers/index.d.ts +24 -0
- package/dist/authorizers/index.d.ts.map +1 -0
- package/dist/authorizers/index.js +23 -0
- package/dist/authorizers/ownership.d.ts +77 -0
- package/dist/authorizers/ownership.d.ts.map +1 -0
- package/dist/authorizers/ownership.js +112 -0
- package/dist/browser-guard.d.ts +2 -0
- package/dist/browser-guard.d.ts.map +1 -0
- package/dist/browser-guard.js +18 -0
- package/dist/diff.d.ts +33 -0
- package/dist/diff.d.ts.map +1 -0
- package/dist/diff.js +110 -0
- package/dist/handlers/count.d.ts +17 -0
- package/dist/handlers/count.d.ts.map +1 -0
- package/dist/handlers/count.js +58 -0
- package/dist/handlers/create.d.ts +19 -0
- package/dist/handlers/create.d.ts.map +1 -0
- package/dist/handlers/create.js +73 -0
- package/dist/handlers/diff.d.ts +19 -0
- package/dist/handlers/diff.d.ts.map +1 -0
- package/dist/handlers/diff.js +70 -0
- package/dist/handlers/index.d.ts +8 -0
- package/dist/handlers/index.d.ts.map +1 -0
- package/dist/handlers/index.js +7 -0
- package/dist/handlers/list.d.ts +18 -0
- package/dist/handlers/list.d.ts.map +1 -0
- package/dist/handlers/list.js +61 -0
- package/dist/handlers/read.d.ts +20 -0
- package/dist/handlers/read.d.ts.map +1 -0
- package/dist/handlers/read.js +68 -0
- package/dist/handlers/readByNumber.d.ts +17 -0
- package/dist/handlers/readByNumber.d.ts.map +1 -0
- package/dist/handlers/readByNumber.js +80 -0
- package/dist/handlers/restore.d.ts +20 -0
- package/dist/handlers/restore.d.ts.map +1 -0
- package/dist/handlers/restore.js +84 -0
- package/dist/i18n.d.ts +10 -0
- package/dist/i18n.d.ts.map +1 -0
- package/dist/i18n.js +17 -0
- package/dist/index.d.ts +130 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +129 -0
- package/dist/registry.d.ts +79 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +65 -0
- package/dist/requestHandlerMap.d.ts +25 -0
- package/dist/requestHandlerMap.d.ts.map +1 -0
- package/dist/requestHandlerMap.js +24 -0
- package/dist/routes.d.ts +67 -0
- package/dist/routes.d.ts.map +1 -0
- package/dist/routes.js +69 -0
- package/dist/service.d.ts +107 -0
- package/dist/service.d.ts.map +1 -0
- package/dist/service.js +193 -0
- package/dist/types.d.ts +106 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +11 -0
- package/dist/validation.d.ts +21 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +34 -0
- package/package.json +70 -0
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create version handler.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { t } from '@molecule/api-i18n';
|
|
7
|
+
import { logger } from '@molecule/api-logger';
|
|
8
|
+
import { isVersionAuthorized } from '../authorizers/ownership.js';
|
|
9
|
+
import { createVersion } from '../service.js';
|
|
10
|
+
import { createVersionSchema } from '../validation.js';
|
|
11
|
+
/**
|
|
12
|
+
* Captures a new version of a resource.
|
|
13
|
+
*
|
|
14
|
+
* Secure by default: returns 401 with no session, and 404 (no existence leak)
|
|
15
|
+
* when the caller is not authorized for the parent resource — a caller can only
|
|
16
|
+
* capture versions of a resource they own, never inject snapshots into another
|
|
17
|
+
* tenant's resource.
|
|
18
|
+
*
|
|
19
|
+
* @param req - The request with `resourceType` / `resourceId` params and a snapshot body.
|
|
20
|
+
* @param res - The response object (reads `locals.session`/`locals.versionHistoryAdmin`).
|
|
21
|
+
*/
|
|
22
|
+
export async function create(req, res) {
|
|
23
|
+
const userId = res.locals.session?.userId;
|
|
24
|
+
if (!userId) {
|
|
25
|
+
res.status(401).json({
|
|
26
|
+
error: t('resource.error.unauthorized', undefined, { defaultValue: 'Unauthorized' }),
|
|
27
|
+
errorKey: 'resource.error.unauthorized',
|
|
28
|
+
});
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
const { resourceType, resourceId } = req.params;
|
|
32
|
+
if (!resourceType || !resourceId) {
|
|
33
|
+
res.status(400).json({
|
|
34
|
+
error: t('versionHistory.error.missingResource', undefined, {
|
|
35
|
+
defaultValue: 'Resource type and ID are required',
|
|
36
|
+
}),
|
|
37
|
+
errorKey: 'versionHistory.error.missingResource',
|
|
38
|
+
});
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
if (!(await isVersionAuthorized(res, { resourceType, resourceId, userId }))) {
|
|
42
|
+
res.status(404).json({
|
|
43
|
+
error: t('versionHistory.error.notFound', undefined, { defaultValue: 'Version not found' }),
|
|
44
|
+
errorKey: 'versionHistory.error.notFound',
|
|
45
|
+
});
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
const parsed = createVersionSchema.safeParse(req.body);
|
|
49
|
+
if (!parsed.success) {
|
|
50
|
+
const errors = parsed.error.issues.map((e) => `${e.path.join('.')}: ${e.message}`).join(', ');
|
|
51
|
+
res.status(400).json({ error: errors, errorKey: 'versionHistory.error.validationFailed' });
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
try {
|
|
55
|
+
const version = await createVersion({
|
|
56
|
+
resourceType,
|
|
57
|
+
resourceId,
|
|
58
|
+
userId,
|
|
59
|
+
snapshot: parsed.data.snapshot,
|
|
60
|
+
reason: parsed.data.reason ?? null,
|
|
61
|
+
});
|
|
62
|
+
res.status(201).json(version);
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
logger.error('Failed to create version', { resourceType, resourceId, userId, error });
|
|
66
|
+
res.status(500).json({
|
|
67
|
+
error: t('versionHistory.error.createFailed', undefined, {
|
|
68
|
+
defaultValue: 'Failed to create version',
|
|
69
|
+
}),
|
|
70
|
+
errorKey: 'versionHistory.error.createFailed',
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Diff versions handler.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type { MoleculeRequest, MoleculeResponse } from '@molecule/api-resource';
|
|
7
|
+
/**
|
|
8
|
+
* Returns the shallow diff between two versions of the same resource.
|
|
9
|
+
*
|
|
10
|
+
* Secure by default: returns 401 with no session, and 404 (no existence leak)
|
|
11
|
+
* when either version is missing OR the caller is not authorized for the parent
|
|
12
|
+
* resource the two versions share. An opt-in {@link versionHistoryAdmin} may
|
|
13
|
+
* diff any versions.
|
|
14
|
+
*
|
|
15
|
+
* @param req - The request with `fromVersionId` and `toVersionId` params.
|
|
16
|
+
* @param res - The response object (reads `locals.session`/`locals.versionHistoryAdmin`).
|
|
17
|
+
*/
|
|
18
|
+
export declare function diff(req: MoleculeRequest, res: MoleculeResponse): Promise<void>;
|
|
19
|
+
//# sourceMappingURL=diff.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"diff.d.ts","sourceRoot":"","sources":["../../src/handlers/diff.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AAK/E;;;;;;;;;;GAUG;AACH,wBAAsB,IAAI,CAAC,GAAG,EAAE,eAAe,EAAE,GAAG,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,CAoDrF"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Diff versions handler.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { t } from '@molecule/api-i18n';
|
|
7
|
+
import { logger } from '@molecule/api-logger';
|
|
8
|
+
import { isVersionAuthorized } from '../authorizers/ownership.js';
|
|
9
|
+
import { diffVersions } from '../service.js';
|
|
10
|
+
/**
|
|
11
|
+
* Returns the shallow diff between two versions of the same resource.
|
|
12
|
+
*
|
|
13
|
+
* Secure by default: returns 401 with no session, and 404 (no existence leak)
|
|
14
|
+
* when either version is missing OR the caller is not authorized for the parent
|
|
15
|
+
* resource the two versions share. An opt-in {@link versionHistoryAdmin} may
|
|
16
|
+
* diff any versions.
|
|
17
|
+
*
|
|
18
|
+
* @param req - The request with `fromVersionId` and `toVersionId` params.
|
|
19
|
+
* @param res - The response object (reads `locals.session`/`locals.versionHistoryAdmin`).
|
|
20
|
+
*/
|
|
21
|
+
export async function diff(req, res) {
|
|
22
|
+
const userId = res.locals.session?.userId;
|
|
23
|
+
if (!userId) {
|
|
24
|
+
res.status(401).json({
|
|
25
|
+
error: t('resource.error.unauthorized', undefined, { defaultValue: 'Unauthorized' }),
|
|
26
|
+
errorKey: 'resource.error.unauthorized',
|
|
27
|
+
});
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
const { fromVersionId, toVersionId } = req.params;
|
|
31
|
+
if (!fromVersionId || !toVersionId) {
|
|
32
|
+
res.status(400).json({
|
|
33
|
+
error: t('versionHistory.error.missingId', undefined, {
|
|
34
|
+
defaultValue: 'Version ID is required',
|
|
35
|
+
}),
|
|
36
|
+
errorKey: 'versionHistory.error.missingId',
|
|
37
|
+
});
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
try {
|
|
41
|
+
const result = await diffVersions(fromVersionId, toVersionId);
|
|
42
|
+
// `diffVersions` already guarantees both versions share one resource; treat
|
|
43
|
+
// "not found" and "not authorized for that parent resource" identically so
|
|
44
|
+
// another tenant's snapshot/diff is never disclosed.
|
|
45
|
+
if (!result ||
|
|
46
|
+
!(await isVersionAuthorized(res, {
|
|
47
|
+
resourceType: result.from.resourceType,
|
|
48
|
+
resourceId: result.from.resourceId,
|
|
49
|
+
userId,
|
|
50
|
+
}))) {
|
|
51
|
+
res.status(404).json({
|
|
52
|
+
error: t('versionHistory.error.diffNotFound', undefined, {
|
|
53
|
+
defaultValue: 'One or both versions not found, or they belong to different resources',
|
|
54
|
+
}),
|
|
55
|
+
errorKey: 'versionHistory.error.diffNotFound',
|
|
56
|
+
});
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
res.json(result);
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
logger.error('Failed to diff versions', { fromVersionId, toVersionId, error });
|
|
63
|
+
res.status(500).json({
|
|
64
|
+
error: t('versionHistory.error.diffFailed', undefined, {
|
|
65
|
+
defaultValue: 'Failed to diff versions',
|
|
66
|
+
}),
|
|
67
|
+
errorKey: 'versionHistory.error.diffFailed',
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/handlers/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAC1B,cAAc,aAAa,CAAA;AAC3B,cAAc,WAAW,CAAA;AACzB,cAAc,WAAW,CAAA;AACzB,cAAc,WAAW,CAAA;AACzB,cAAc,mBAAmB,CAAA;AACjC,cAAc,cAAc,CAAA"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* List versions handler.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type { MoleculeRequest, MoleculeResponse } from '@molecule/api-resource';
|
|
7
|
+
/**
|
|
8
|
+
* Lists paginated versions for a resource, newest version first.
|
|
9
|
+
*
|
|
10
|
+
* Secure by default: returns 401 with no session, and 404 (no existence leak)
|
|
11
|
+
* when the caller is not authorized for the parent resource — only the parent
|
|
12
|
+
* resource's owner (or an opt-in {@link versionHistoryAdmin}) sees its versions.
|
|
13
|
+
*
|
|
14
|
+
* @param req - The request with `resourceType` and `resourceId` params.
|
|
15
|
+
* @param res - The response object (reads `locals.session`/`locals.versionHistoryAdmin`).
|
|
16
|
+
*/
|
|
17
|
+
export declare function list(req: MoleculeRequest, res: MoleculeResponse): Promise<void>;
|
|
18
|
+
//# sourceMappingURL=list.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"list.d.ts","sourceRoot":"","sources":["../../src/handlers/list.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AAK/E;;;;;;;;;GASG;AACH,wBAAsB,IAAI,CAAC,GAAG,EAAE,eAAe,EAAE,GAAG,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,CA4CrF"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* List versions handler.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { t } from '@molecule/api-i18n';
|
|
7
|
+
import { logger } from '@molecule/api-logger';
|
|
8
|
+
import { isVersionAuthorized } from '../authorizers/ownership.js';
|
|
9
|
+
import { getVersionsForResource } from '../service.js';
|
|
10
|
+
/**
|
|
11
|
+
* Lists paginated versions for a resource, newest version first.
|
|
12
|
+
*
|
|
13
|
+
* Secure by default: returns 401 with no session, and 404 (no existence leak)
|
|
14
|
+
* when the caller is not authorized for the parent resource — only the parent
|
|
15
|
+
* resource's owner (or an opt-in {@link versionHistoryAdmin}) sees its versions.
|
|
16
|
+
*
|
|
17
|
+
* @param req - The request with `resourceType` and `resourceId` params.
|
|
18
|
+
* @param res - The response object (reads `locals.session`/`locals.versionHistoryAdmin`).
|
|
19
|
+
*/
|
|
20
|
+
export async function list(req, res) {
|
|
21
|
+
const userId = res.locals.session?.userId;
|
|
22
|
+
if (!userId) {
|
|
23
|
+
res.status(401).json({
|
|
24
|
+
error: t('resource.error.unauthorized', undefined, { defaultValue: 'Unauthorized' }),
|
|
25
|
+
errorKey: 'resource.error.unauthorized',
|
|
26
|
+
});
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
const { resourceType, resourceId } = req.params;
|
|
30
|
+
if (!resourceType || !resourceId) {
|
|
31
|
+
res.status(400).json({
|
|
32
|
+
error: t('versionHistory.error.missingResource', undefined, {
|
|
33
|
+
defaultValue: 'Resource type and ID are required',
|
|
34
|
+
}),
|
|
35
|
+
errorKey: 'versionHistory.error.missingResource',
|
|
36
|
+
});
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (!(await isVersionAuthorized(res, { resourceType, resourceId, userId }))) {
|
|
40
|
+
res.status(404).json({
|
|
41
|
+
error: t('versionHistory.error.notFound', undefined, { defaultValue: 'Version not found' }),
|
|
42
|
+
errorKey: 'versionHistory.error.notFound',
|
|
43
|
+
});
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
const limit = parseInt(req.query.limit, 10) || 20;
|
|
47
|
+
const offset = parseInt(req.query.offset, 10) || 0;
|
|
48
|
+
try {
|
|
49
|
+
const result = await getVersionsForResource(resourceType, resourceId, { limit, offset });
|
|
50
|
+
res.json(result);
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
logger.error('Failed to list versions', { resourceType, resourceId, error });
|
|
54
|
+
res.status(500).json({
|
|
55
|
+
error: t('versionHistory.error.listFailed', undefined, {
|
|
56
|
+
defaultValue: 'Failed to list versions',
|
|
57
|
+
}),
|
|
58
|
+
errorKey: 'versionHistory.error.listFailed',
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Read version handler (by version ID).
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type { MoleculeRequest, MoleculeResponse } from '@molecule/api-resource';
|
|
7
|
+
/**
|
|
8
|
+
* Reads a single version by ID.
|
|
9
|
+
*
|
|
10
|
+
* Secure by default: returns 401 with no session, and 404 (no existence leak)
|
|
11
|
+
* when the version is missing OR the caller is not authorized for its parent
|
|
12
|
+
* resource — a non-owner cannot tell the two apart, so another tenant's
|
|
13
|
+
* snapshot is never disclosed. An opt-in {@link versionHistoryAdmin} may read
|
|
14
|
+
* any version.
|
|
15
|
+
*
|
|
16
|
+
* @param req - The request with `versionId` param.
|
|
17
|
+
* @param res - The response object (reads `locals.session`/`locals.versionHistoryAdmin`).
|
|
18
|
+
*/
|
|
19
|
+
export declare function read(req: MoleculeRequest, res: MoleculeResponse): Promise<void>;
|
|
20
|
+
//# sourceMappingURL=read.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"read.d.ts","sourceRoot":"","sources":["../../src/handlers/read.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AAK/E;;;;;;;;;;;GAWG;AACH,wBAAsB,IAAI,CAAC,GAAG,EAAE,eAAe,EAAE,GAAG,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,CAiDrF"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Read version handler (by version ID).
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { t } from '@molecule/api-i18n';
|
|
7
|
+
import { logger } from '@molecule/api-logger';
|
|
8
|
+
import { isVersionAuthorized } from '../authorizers/ownership.js';
|
|
9
|
+
import { getVersionById } from '../service.js';
|
|
10
|
+
/**
|
|
11
|
+
* Reads a single version by ID.
|
|
12
|
+
*
|
|
13
|
+
* Secure by default: returns 401 with no session, and 404 (no existence leak)
|
|
14
|
+
* when the version is missing OR the caller is not authorized for its parent
|
|
15
|
+
* resource — a non-owner cannot tell the two apart, so another tenant's
|
|
16
|
+
* snapshot is never disclosed. An opt-in {@link versionHistoryAdmin} may read
|
|
17
|
+
* any version.
|
|
18
|
+
*
|
|
19
|
+
* @param req - The request with `versionId` param.
|
|
20
|
+
* @param res - The response object (reads `locals.session`/`locals.versionHistoryAdmin`).
|
|
21
|
+
*/
|
|
22
|
+
export async function read(req, res) {
|
|
23
|
+
const userId = res.locals.session?.userId;
|
|
24
|
+
if (!userId) {
|
|
25
|
+
res.status(401).json({
|
|
26
|
+
error: t('resource.error.unauthorized', undefined, { defaultValue: 'Unauthorized' }),
|
|
27
|
+
errorKey: 'resource.error.unauthorized',
|
|
28
|
+
});
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
const { versionId } = req.params;
|
|
32
|
+
if (!versionId) {
|
|
33
|
+
res.status(400).json({
|
|
34
|
+
error: t('versionHistory.error.missingId', undefined, {
|
|
35
|
+
defaultValue: 'Version ID is required',
|
|
36
|
+
}),
|
|
37
|
+
errorKey: 'versionHistory.error.missingId',
|
|
38
|
+
});
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
try {
|
|
42
|
+
const version = await getVersionById(versionId);
|
|
43
|
+
// Treat "not found" and "not authorized for the parent resource"
|
|
44
|
+
// identically so the existence of another tenant's snapshot is not leaked.
|
|
45
|
+
if (!version ||
|
|
46
|
+
!(await isVersionAuthorized(res, {
|
|
47
|
+
resourceType: version.resourceType,
|
|
48
|
+
resourceId: version.resourceId,
|
|
49
|
+
userId,
|
|
50
|
+
}))) {
|
|
51
|
+
res.status(404).json({
|
|
52
|
+
error: t('versionHistory.error.notFound', undefined, { defaultValue: 'Version not found' }),
|
|
53
|
+
errorKey: 'versionHistory.error.notFound',
|
|
54
|
+
});
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
res.json(version);
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
logger.error('Failed to read version', { versionId, error });
|
|
61
|
+
res.status(500).json({
|
|
62
|
+
error: t('versionHistory.error.readFailed', undefined, {
|
|
63
|
+
defaultValue: 'Failed to read version',
|
|
64
|
+
}),
|
|
65
|
+
errorKey: 'versionHistory.error.readFailed',
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Read version handler (by version number on a resource).
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type { MoleculeRequest, MoleculeResponse } from '@molecule/api-resource';
|
|
7
|
+
/**
|
|
8
|
+
* Reads a single version of a resource by 1-based version number.
|
|
9
|
+
*
|
|
10
|
+
* Secure by default: returns 401 with no session, and 404 (no existence leak)
|
|
11
|
+
* when the caller is not authorized for the parent resource.
|
|
12
|
+
*
|
|
13
|
+
* @param req - The request with `resourceType`, `resourceId`, and `version` params.
|
|
14
|
+
* @param res - The response object (reads `locals.session`/`locals.versionHistoryAdmin`).
|
|
15
|
+
*/
|
|
16
|
+
export declare function readByNumber(req: MoleculeRequest, res: MoleculeResponse): Promise<void>;
|
|
17
|
+
//# sourceMappingURL=readByNumber.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"readByNumber.d.ts","sourceRoot":"","sources":["../../src/handlers/readByNumber.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AAK/E;;;;;;;;GAQG;AACH,wBAAsB,YAAY,CAAC,GAAG,EAAE,eAAe,EAAE,GAAG,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,CAgE7F"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Read version handler (by version number on a resource).
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { t } from '@molecule/api-i18n';
|
|
7
|
+
import { logger } from '@molecule/api-logger';
|
|
8
|
+
import { isVersionAuthorized } from '../authorizers/ownership.js';
|
|
9
|
+
import { getVersionByNumber } from '../service.js';
|
|
10
|
+
/**
|
|
11
|
+
* Reads a single version of a resource by 1-based version number.
|
|
12
|
+
*
|
|
13
|
+
* Secure by default: returns 401 with no session, and 404 (no existence leak)
|
|
14
|
+
* when the caller is not authorized for the parent resource.
|
|
15
|
+
*
|
|
16
|
+
* @param req - The request with `resourceType`, `resourceId`, and `version` params.
|
|
17
|
+
* @param res - The response object (reads `locals.session`/`locals.versionHistoryAdmin`).
|
|
18
|
+
*/
|
|
19
|
+
export async function readByNumber(req, res) {
|
|
20
|
+
const userId = res.locals.session?.userId;
|
|
21
|
+
if (!userId) {
|
|
22
|
+
res.status(401).json({
|
|
23
|
+
error: t('resource.error.unauthorized', undefined, { defaultValue: 'Unauthorized' }),
|
|
24
|
+
errorKey: 'resource.error.unauthorized',
|
|
25
|
+
});
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
const { resourceType, resourceId, version } = req.params;
|
|
29
|
+
if (!resourceType || !resourceId) {
|
|
30
|
+
res.status(400).json({
|
|
31
|
+
error: t('versionHistory.error.missingResource', undefined, {
|
|
32
|
+
defaultValue: 'Resource type and ID are required',
|
|
33
|
+
}),
|
|
34
|
+
errorKey: 'versionHistory.error.missingResource',
|
|
35
|
+
});
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if (!(await isVersionAuthorized(res, { resourceType, resourceId, userId }))) {
|
|
39
|
+
res.status(404).json({
|
|
40
|
+
error: t('versionHistory.error.notFound', undefined, { defaultValue: 'Version not found' }),
|
|
41
|
+
errorKey: 'versionHistory.error.notFound',
|
|
42
|
+
});
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
const versionNumber = parseInt(version ?? '', 10);
|
|
46
|
+
if (!Number.isInteger(versionNumber) || versionNumber < 1) {
|
|
47
|
+
res.status(400).json({
|
|
48
|
+
error: t('versionHistory.error.invalidVersion', undefined, {
|
|
49
|
+
defaultValue: 'Version number must be a positive integer',
|
|
50
|
+
}),
|
|
51
|
+
errorKey: 'versionHistory.error.invalidVersion',
|
|
52
|
+
});
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
try {
|
|
56
|
+
const result = await getVersionByNumber(resourceType, resourceId, versionNumber);
|
|
57
|
+
if (!result) {
|
|
58
|
+
res.status(404).json({
|
|
59
|
+
error: t('versionHistory.error.notFound', undefined, { defaultValue: 'Version not found' }),
|
|
60
|
+
errorKey: 'versionHistory.error.notFound',
|
|
61
|
+
});
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
res.json(result);
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
logger.error('Failed to read version by number', {
|
|
68
|
+
resourceType,
|
|
69
|
+
resourceId,
|
|
70
|
+
versionNumber,
|
|
71
|
+
error,
|
|
72
|
+
});
|
|
73
|
+
res.status(500).json({
|
|
74
|
+
error: t('versionHistory.error.readFailed', undefined, {
|
|
75
|
+
defaultValue: 'Failed to read version',
|
|
76
|
+
}),
|
|
77
|
+
errorKey: 'versionHistory.error.readFailed',
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Restore version handler.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type { MoleculeRequest, MoleculeResponse } from '@molecule/api-resource';
|
|
7
|
+
/**
|
|
8
|
+
* Restores a prior version by appending a new version whose snapshot
|
|
9
|
+
* matches it. Append-only — the existing rows are never mutated.
|
|
10
|
+
*
|
|
11
|
+
* Secure by default: returns 401 with no session, and 404 (no existence leak)
|
|
12
|
+
* when the version is missing OR the caller is not authorized for its parent
|
|
13
|
+
* resource — a non-owner can neither restore another tenant's version nor learn
|
|
14
|
+
* that it exists. An opt-in {@link versionHistoryAdmin} may restore any version.
|
|
15
|
+
*
|
|
16
|
+
* @param req - The request with `versionId` param.
|
|
17
|
+
* @param res - The response object (reads `locals.session`/`locals.versionHistoryAdmin`).
|
|
18
|
+
*/
|
|
19
|
+
export declare function restore(req: MoleculeRequest, res: MoleculeResponse): Promise<void>;
|
|
20
|
+
//# sourceMappingURL=restore.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"restore.d.ts","sourceRoot":"","sources":["../../src/handlers/restore.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AAM/E;;;;;;;;;;;GAWG;AACH,wBAAsB,OAAO,CAAC,GAAG,EAAE,eAAe,EAAE,GAAG,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,CAkExF"}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Restore version handler.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { t } from '@molecule/api-i18n';
|
|
7
|
+
import { logger } from '@molecule/api-logger';
|
|
8
|
+
import { isVersionAuthorized } from '../authorizers/ownership.js';
|
|
9
|
+
import { getVersionById, restoreVersion } from '../service.js';
|
|
10
|
+
import { restoreVersionSchema } from '../validation.js';
|
|
11
|
+
/**
|
|
12
|
+
* Restores a prior version by appending a new version whose snapshot
|
|
13
|
+
* matches it. Append-only — the existing rows are never mutated.
|
|
14
|
+
*
|
|
15
|
+
* Secure by default: returns 401 with no session, and 404 (no existence leak)
|
|
16
|
+
* when the version is missing OR the caller is not authorized for its parent
|
|
17
|
+
* resource — a non-owner can neither restore another tenant's version nor learn
|
|
18
|
+
* that it exists. An opt-in {@link versionHistoryAdmin} may restore any version.
|
|
19
|
+
*
|
|
20
|
+
* @param req - The request with `versionId` param.
|
|
21
|
+
* @param res - The response object (reads `locals.session`/`locals.versionHistoryAdmin`).
|
|
22
|
+
*/
|
|
23
|
+
export async function restore(req, res) {
|
|
24
|
+
const userId = res.locals.session?.userId;
|
|
25
|
+
if (!userId) {
|
|
26
|
+
res.status(401).json({
|
|
27
|
+
error: t('resource.error.unauthorized', undefined, { defaultValue: 'Unauthorized' }),
|
|
28
|
+
errorKey: 'resource.error.unauthorized',
|
|
29
|
+
});
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
const { versionId } = req.params;
|
|
33
|
+
if (!versionId) {
|
|
34
|
+
res.status(400).json({
|
|
35
|
+
error: t('versionHistory.error.missingId', undefined, {
|
|
36
|
+
defaultValue: 'Version ID is required',
|
|
37
|
+
}),
|
|
38
|
+
errorKey: 'versionHistory.error.missingId',
|
|
39
|
+
});
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const parsed = restoreVersionSchema.safeParse(req.body ?? {});
|
|
43
|
+
if (!parsed.success) {
|
|
44
|
+
const errors = parsed.error.issues.map((e) => `${e.path.join('.')}: ${e.message}`).join(', ');
|
|
45
|
+
res.status(400).json({ error: errors, errorKey: 'versionHistory.error.validationFailed' });
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
// Verify the caller is authorized for the version's PARENT resource before
|
|
49
|
+
// restoring. Treat "not found" and "not authorized" identically so a
|
|
50
|
+
// non-owner can neither restore another tenant's version nor learn it exists.
|
|
51
|
+
const target = await getVersionById(versionId);
|
|
52
|
+
if (!target ||
|
|
53
|
+
!(await isVersionAuthorized(res, {
|
|
54
|
+
resourceType: target.resourceType,
|
|
55
|
+
resourceId: target.resourceId,
|
|
56
|
+
userId,
|
|
57
|
+
}))) {
|
|
58
|
+
res.status(404).json({
|
|
59
|
+
error: t('versionHistory.error.notFound', undefined, { defaultValue: 'Version not found' }),
|
|
60
|
+
errorKey: 'versionHistory.error.notFound',
|
|
61
|
+
});
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
try {
|
|
65
|
+
const restored = await restoreVersion(versionId, userId, parsed.data.reason ?? null);
|
|
66
|
+
if (!restored) {
|
|
67
|
+
res.status(404).json({
|
|
68
|
+
error: t('versionHistory.error.notFound', undefined, { defaultValue: 'Version not found' }),
|
|
69
|
+
errorKey: 'versionHistory.error.notFound',
|
|
70
|
+
});
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
res.status(201).json(restored);
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
logger.error('Failed to restore version', { versionId, userId, error });
|
|
77
|
+
res.status(500).json({
|
|
78
|
+
error: t('versionHistory.error.restoreFailed', undefined, {
|
|
79
|
+
defaultValue: 'Failed to restore version',
|
|
80
|
+
}),
|
|
81
|
+
errorKey: 'versionHistory.error.restoreFailed',
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
package/dist/i18n.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"i18n.d.ts","sourceRoot":"","sources":["../src/i18n.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAWH;;GAEG;AACH,eAAO,MAAM,cAAc,OAAO,CAAA"}
|
package/dist/i18n.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Locale registration for the version-history resource.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { registerLocaleModule } from '@molecule/api-i18n';
|
|
7
|
+
try {
|
|
8
|
+
const locales = await import('@molecule/api-locales-resource-version-history');
|
|
9
|
+
registerLocaleModule(locales);
|
|
10
|
+
}
|
|
11
|
+
catch (_error) {
|
|
12
|
+
// Locale package not available — translations fall back to inline defaults.
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The i18n registered.
|
|
16
|
+
*/
|
|
17
|
+
export const i18nRegistered = true;
|