@molecule/api-resource-follow 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 +10 -0
- package/dist/authorizers/index.d.ts.map +1 -0
- package/dist/authorizers/index.js +9 -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/handlers/create.d.ts +14 -0
- package/dist/handlers/create.d.ts.map +1 -0
- package/dist/handlers/create.js +45 -0
- package/dist/handlers/del.d.ts +14 -0
- package/dist/handlers/del.d.ts.map +1 -0
- package/dist/handlers/del.js +45 -0
- package/dist/handlers/index.d.ts +6 -0
- package/dist/handlers/index.d.ts.map +1 -0
- package/dist/handlers/index.js +5 -0
- package/dist/handlers/list.d.ts +14 -0
- package/dist/handlers/list.d.ts.map +1 -0
- package/dist/handlers/list.js +41 -0
- package/dist/handlers/read.d.ts +14 -0
- package/dist/handlers/read.d.ts.map +1 -0
- package/dist/handlers/read.js +39 -0
- package/dist/handlers/update.d.ts +14 -0
- package/dist/handlers/update.d.ts.map +1 -0
- package/dist/handlers/update.js +47 -0
- package/dist/index.d.ts +73 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +72 -0
- package/dist/requestHandlerMap.d.ts +21 -0
- package/dist/requestHandlerMap.d.ts.map +1 -0
- package/dist/requestHandlerMap.js +20 -0
- package/dist/routes.d.ts +36 -0
- package/dist/routes.d.ts.map +1 -0
- package/dist/routes.js +32 -0
- package/dist/service.d.ts +65 -0
- package/dist/service.d.ts.map +1 -0
- package/dist/service.js +126 -0
- package/dist/types.d.ts +47 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +8 -0
- package/package.json +64 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work.
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship.
|
|
43
|
+
|
|
44
|
+
"Contribution" shall mean any work of authorship, including the
|
|
45
|
+
original version of the Work and any modifications or additions
|
|
46
|
+
to that Work, that is intentionally submitted to the Licensor for
|
|
47
|
+
inclusion in the Work by the copyright owner or by an individual or
|
|
48
|
+
Legal Entity authorized to submit on behalf of the copyright owner.
|
|
49
|
+
|
|
50
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
51
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
52
|
+
subsequently incorporated within the Work.
|
|
53
|
+
|
|
54
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
55
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
56
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
57
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
58
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
59
|
+
Work and such Derivative Works in Source or Object form.
|
|
60
|
+
|
|
61
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
62
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
63
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
64
|
+
patent license to make, have made, use, offer to sell, sell, import,
|
|
65
|
+
and otherwise transfer the Work.
|
|
66
|
+
|
|
67
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
68
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
69
|
+
modifications, and in Source or Object form, provided that You
|
|
70
|
+
meet the following conditions:
|
|
71
|
+
|
|
72
|
+
(a) You must give any other recipients of the Work or
|
|
73
|
+
Derivative Works a copy of this License; and
|
|
74
|
+
|
|
75
|
+
(b) You must cause any modified files to carry prominent notices
|
|
76
|
+
stating that You changed the files; and
|
|
77
|
+
|
|
78
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
79
|
+
that You distribute, all copyright, patent, trademark, and
|
|
80
|
+
attribution notices from the Source form of the Work,
|
|
81
|
+
excluding those notices that do not pertain to any part of
|
|
82
|
+
the Derivative Works; and
|
|
83
|
+
|
|
84
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
85
|
+
distribution, then any Derivative Works that You distribute must
|
|
86
|
+
include a readable copy of the attribution notices contained
|
|
87
|
+
within such NOTICE file.
|
|
88
|
+
|
|
89
|
+
5. Submission of Contributions.
|
|
90
|
+
|
|
91
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
92
|
+
names, trademarks, service marks, or product names of the Licensor.
|
|
93
|
+
|
|
94
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
95
|
+
agreed to in writing, Licensor provides the Work on an "AS IS" BASIS,
|
|
96
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND.
|
|
97
|
+
|
|
98
|
+
8. Limitation of Liability. In no event and under no legal theory shall
|
|
99
|
+
any Contributor be liable to You for damages.
|
|
100
|
+
|
|
101
|
+
9. Accepting Warranty or Additional Liability.
|
|
102
|
+
|
|
103
|
+
Copyright 2026 Molecule Dev, Inc.
|
|
104
|
+
|
|
105
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
106
|
+
you may not use this file except in compliance with the License.
|
|
107
|
+
You may obtain a copy of the License at
|
|
108
|
+
|
|
109
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
110
|
+
|
|
111
|
+
Unless required by applicable law or agreed to in writing, software
|
|
112
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
113
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
114
|
+
See the License for the specific language governing permissions and
|
|
115
|
+
limitations under the License.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/authorizers/index.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser-guard.d.ts","sourceRoot":"","sources":["../src/browser-guard.ts"],"names":[],"mappings":"AAuBA,OAAO,EAAE,CAAA"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser guard — `@molecule/api-resource-follow` is SERVER-ONLY.
|
|
3
|
+
*
|
|
4
|
+
* Generated by scripts/gen-browser-guards.mjs (workspace root) — edit THAT, not this.
|
|
5
|
+
* Evaluating a server package in a browser bundle is always an import-graph mistake
|
|
6
|
+
* (node APIs, secrets); without this guard it surfaces as a cryptic downstream crash
|
|
7
|
+
* ("Buffer is not defined") far from the culprit. Throwing here names the package and
|
|
8
|
+
* the fix at the exact moment the client bundle evaluates it. jsdom tests and SSR are
|
|
9
|
+
* unaffected: the throw requires browser globals AND the absence of a node runtime.
|
|
10
|
+
*/
|
|
11
|
+
const g = globalThis;
|
|
12
|
+
if (g.window !== undefined && g.document !== undefined && !g.process?.versions?.node) {
|
|
13
|
+
throw new Error('@molecule/api-resource-follow is SERVER-ONLY: it was bundled into browser/client code. Import it only ' +
|
|
14
|
+
'from server code (a server route/function or your API), or dynamic-import it inside ' +
|
|
15
|
+
'the server handler — never from components or shared client modules, and never ' +
|
|
16
|
+
'polyfill Buffer/process to silence this.');
|
|
17
|
+
}
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Follow handler.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type { MoleculeRequest, MoleculeResponse } from '@molecule/api-resource';
|
|
7
|
+
/**
|
|
8
|
+
* Creates a follow relationship. Idempotent.
|
|
9
|
+
*
|
|
10
|
+
* @param req - The request with `targetType` and `targetId` params.
|
|
11
|
+
* @param res - The response object.
|
|
12
|
+
*/
|
|
13
|
+
export declare function create(req: MoleculeRequest, res: MoleculeResponse): Promise<void>;
|
|
14
|
+
//# sourceMappingURL=create.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create.d.ts","sourceRoot":"","sources":["../../src/handlers/create.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AAI/E;;;;;GAKG;AACH,wBAAsB,MAAM,CAAC,GAAG,EAAE,eAAe,EAAE,GAAG,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,CA+BvF"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Follow handler.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { t } from '@molecule/api-i18n';
|
|
7
|
+
import { logger } from '@molecule/api-logger';
|
|
8
|
+
import { follow } from '../service.js';
|
|
9
|
+
/**
|
|
10
|
+
* Creates a follow relationship. Idempotent.
|
|
11
|
+
*
|
|
12
|
+
* @param req - The request with `targetType` and `targetId` params.
|
|
13
|
+
* @param res - The response object.
|
|
14
|
+
*/
|
|
15
|
+
export async function create(req, res) {
|
|
16
|
+
const userId = res.locals.session?.userId;
|
|
17
|
+
if (!userId) {
|
|
18
|
+
res.status(401).json({
|
|
19
|
+
error: t('resource.error.unauthorized', undefined, { defaultValue: 'Unauthorized' }),
|
|
20
|
+
errorKey: 'resource.error.unauthorized',
|
|
21
|
+
});
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const { targetType, targetId } = req.params;
|
|
25
|
+
if (!targetType || !targetId) {
|
|
26
|
+
res.status(400).json({
|
|
27
|
+
error: t('follow.error.missingTarget', undefined, {
|
|
28
|
+
defaultValue: 'Target type and ID are required',
|
|
29
|
+
}),
|
|
30
|
+
errorKey: 'follow.error.missingTarget',
|
|
31
|
+
});
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
try {
|
|
35
|
+
const result = await follow(userId, targetType, targetId);
|
|
36
|
+
res.status(201).json(result);
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
logger.error('Failed to follow', { userId, targetType, targetId, error });
|
|
40
|
+
res.status(500).json({
|
|
41
|
+
error: t('follow.error.createFailed', undefined, { defaultValue: 'Failed to follow' }),
|
|
42
|
+
errorKey: 'follow.error.createFailed',
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unfollow handler.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type { MoleculeRequest, MoleculeResponse } from '@molecule/api-resource';
|
|
7
|
+
/**
|
|
8
|
+
* Removes a follow relationship.
|
|
9
|
+
*
|
|
10
|
+
* @param req - The request with `targetType` and `targetId` params.
|
|
11
|
+
* @param res - The response object.
|
|
12
|
+
*/
|
|
13
|
+
export declare function del(req: MoleculeRequest, res: MoleculeResponse): Promise<void>;
|
|
14
|
+
//# sourceMappingURL=del.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"del.d.ts","sourceRoot":"","sources":["../../src/handlers/del.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AAI/E;;;;;GAKG;AACH,wBAAsB,GAAG,CAAC,GAAG,EAAE,eAAe,EAAE,GAAG,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,CA+BpF"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unfollow handler.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { t } from '@molecule/api-i18n';
|
|
7
|
+
import { logger } from '@molecule/api-logger';
|
|
8
|
+
import { unfollow } from '../service.js';
|
|
9
|
+
/**
|
|
10
|
+
* Removes a follow relationship.
|
|
11
|
+
*
|
|
12
|
+
* @param req - The request with `targetType` and `targetId` params.
|
|
13
|
+
* @param res - The response object.
|
|
14
|
+
*/
|
|
15
|
+
export async function del(req, res) {
|
|
16
|
+
const userId = res.locals.session?.userId;
|
|
17
|
+
if (!userId) {
|
|
18
|
+
res.status(401).json({
|
|
19
|
+
error: t('resource.error.unauthorized', undefined, { defaultValue: 'Unauthorized' }),
|
|
20
|
+
errorKey: 'resource.error.unauthorized',
|
|
21
|
+
});
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const { targetType, targetId } = req.params;
|
|
25
|
+
if (!targetType || !targetId) {
|
|
26
|
+
res.status(400).json({
|
|
27
|
+
error: t('follow.error.missingTarget', undefined, {
|
|
28
|
+
defaultValue: 'Target type and ID are required',
|
|
29
|
+
}),
|
|
30
|
+
errorKey: 'follow.error.missingTarget',
|
|
31
|
+
});
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
try {
|
|
35
|
+
await unfollow(userId, targetType, targetId);
|
|
36
|
+
res.status(204).end();
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
logger.error('Failed to unfollow', { userId, targetType, targetId, error });
|
|
40
|
+
res.status(500).json({
|
|
41
|
+
error: t('follow.error.deleteFailed', undefined, { defaultValue: 'Failed to unfollow' }),
|
|
42
|
+
errorKey: 'follow.error.deleteFailed',
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/handlers/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA;AAC3B,cAAc,UAAU,CAAA;AACxB,cAAc,WAAW,CAAA;AACzB,cAAc,WAAW,CAAA;AACzB,cAAc,aAAa,CAAA"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* List followers handler.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type { MoleculeRequest, MoleculeResponse } from '@molecule/api-resource';
|
|
7
|
+
/**
|
|
8
|
+
* Lists paginated followers of a target.
|
|
9
|
+
*
|
|
10
|
+
* @param req - The request with `targetType` and `targetId` params.
|
|
11
|
+
* @param res - The response object.
|
|
12
|
+
*/
|
|
13
|
+
export declare function list(req: MoleculeRequest, res: MoleculeResponse): Promise<void>;
|
|
14
|
+
//# 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;AAI/E;;;;;GAKG;AACH,wBAAsB,IAAI,CAAC,GAAG,EAAE,eAAe,EAAE,GAAG,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,CA2BrF"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* List followers handler.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { t } from '@molecule/api-i18n';
|
|
7
|
+
import { logger } from '@molecule/api-logger';
|
|
8
|
+
import { getFollowers } from '../service.js';
|
|
9
|
+
/**
|
|
10
|
+
* Lists paginated followers of a target.
|
|
11
|
+
*
|
|
12
|
+
* @param req - The request with `targetType` and `targetId` params.
|
|
13
|
+
* @param res - The response object.
|
|
14
|
+
*/
|
|
15
|
+
export async function list(req, res) {
|
|
16
|
+
const { targetType, targetId } = req.params;
|
|
17
|
+
if (!targetType || !targetId) {
|
|
18
|
+
res.status(400).json({
|
|
19
|
+
error: t('follow.error.missingTarget', undefined, {
|
|
20
|
+
defaultValue: 'Target type and ID are required',
|
|
21
|
+
}),
|
|
22
|
+
errorKey: 'follow.error.missingTarget',
|
|
23
|
+
});
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const limit = parseInt(req.query.limit, 10) || 20;
|
|
27
|
+
const offset = parseInt(req.query.offset, 10) || 0;
|
|
28
|
+
try {
|
|
29
|
+
const result = await getFollowers(targetType, targetId, { limit, offset });
|
|
30
|
+
res.json(result);
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
logger.error('Failed to list followers', { targetType, targetId, error });
|
|
34
|
+
res.status(500).json({
|
|
35
|
+
error: t('follow.error.listFailed', undefined, {
|
|
36
|
+
defaultValue: 'Failed to list followers',
|
|
37
|
+
}),
|
|
38
|
+
errorKey: 'follow.error.listFailed',
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get following list handler.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type { MoleculeRequest, MoleculeResponse } from '@molecule/api-resource';
|
|
7
|
+
/**
|
|
8
|
+
* Lists targets the current user is following.
|
|
9
|
+
*
|
|
10
|
+
* @param req - The request with pagination query params.
|
|
11
|
+
* @param res - The response object.
|
|
12
|
+
*/
|
|
13
|
+
export declare function following(req: MoleculeRequest, res: MoleculeResponse): Promise<void>;
|
|
14
|
+
//# 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;AAI/E;;;;;GAKG;AACH,wBAAsB,SAAS,CAAC,GAAG,EAAE,eAAe,EAAE,GAAG,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,CAyB1F"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get following list handler.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { t } from '@molecule/api-i18n';
|
|
7
|
+
import { logger } from '@molecule/api-logger';
|
|
8
|
+
import { getFollowing } from '../service.js';
|
|
9
|
+
/**
|
|
10
|
+
* Lists targets the current user is following.
|
|
11
|
+
*
|
|
12
|
+
* @param req - The request with pagination query params.
|
|
13
|
+
* @param res - The response object.
|
|
14
|
+
*/
|
|
15
|
+
export async function following(req, res) {
|
|
16
|
+
const userId = res.locals.session?.userId;
|
|
17
|
+
if (!userId) {
|
|
18
|
+
res.status(401).json({
|
|
19
|
+
error: t('resource.error.unauthorized', undefined, { defaultValue: 'Unauthorized' }),
|
|
20
|
+
errorKey: 'resource.error.unauthorized',
|
|
21
|
+
});
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const limit = parseInt(req.query.limit, 10) || 20;
|
|
25
|
+
const offset = parseInt(req.query.offset, 10) || 0;
|
|
26
|
+
try {
|
|
27
|
+
const result = await getFollowing(userId, { limit, offset });
|
|
28
|
+
res.json(result);
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
logger.error('Failed to list following', { userId, error });
|
|
32
|
+
res.status(500).json({
|
|
33
|
+
error: t('follow.error.followingFailed', undefined, {
|
|
34
|
+
defaultValue: 'Failed to list following',
|
|
35
|
+
}),
|
|
36
|
+
errorKey: 'follow.error.followingFailed',
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Check follow status handler.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type { MoleculeRequest, MoleculeResponse } from '@molecule/api-resource';
|
|
7
|
+
/**
|
|
8
|
+
* Checks if the current user is following a target.
|
|
9
|
+
*
|
|
10
|
+
* @param req - The request with `targetType` and `targetId` params.
|
|
11
|
+
* @param res - The response object.
|
|
12
|
+
*/
|
|
13
|
+
export declare function checkFollowing(req: MoleculeRequest, res: MoleculeResponse): Promise<void>;
|
|
14
|
+
//# sourceMappingURL=update.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"update.d.ts","sourceRoot":"","sources":["../../src/handlers/update.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AAI/E;;;;;GAKG;AACH,wBAAsB,cAAc,CAAC,GAAG,EAAE,eAAe,EAAE,GAAG,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,CAiC/F"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Check follow status handler.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { t } from '@molecule/api-i18n';
|
|
7
|
+
import { logger } from '@molecule/api-logger';
|
|
8
|
+
import { isFollowing } from '../service.js';
|
|
9
|
+
/**
|
|
10
|
+
* Checks if the current user is following a target.
|
|
11
|
+
*
|
|
12
|
+
* @param req - The request with `targetType` and `targetId` params.
|
|
13
|
+
* @param res - The response object.
|
|
14
|
+
*/
|
|
15
|
+
export async function checkFollowing(req, res) {
|
|
16
|
+
const userId = res.locals.session?.userId;
|
|
17
|
+
if (!userId) {
|
|
18
|
+
res.status(401).json({
|
|
19
|
+
error: t('resource.error.unauthorized', undefined, { defaultValue: 'Unauthorized' }),
|
|
20
|
+
errorKey: 'resource.error.unauthorized',
|
|
21
|
+
});
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const { targetType, targetId } = req.params;
|
|
25
|
+
if (!targetType || !targetId) {
|
|
26
|
+
res.status(400).json({
|
|
27
|
+
error: t('follow.error.missingTarget', undefined, {
|
|
28
|
+
defaultValue: 'Target type and ID are required',
|
|
29
|
+
}),
|
|
30
|
+
errorKey: 'follow.error.missingTarget',
|
|
31
|
+
});
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
try {
|
|
35
|
+
const result = await isFollowing(userId, targetType, targetId);
|
|
36
|
+
res.json({ following: result });
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
logger.error('Failed to check follow status', { userId, targetType, targetId, error });
|
|
40
|
+
res.status(500).json({
|
|
41
|
+
error: t('follow.error.checkFailed', undefined, {
|
|
42
|
+
defaultValue: 'Failed to check follow status',
|
|
43
|
+
}),
|
|
44
|
+
errorKey: 'follow.error.checkFailed',
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Follow/unfollow resource for molecule.dev.
|
|
3
|
+
*
|
|
4
|
+
* Polymorphic follow system for users or any resource type. Supports
|
|
5
|
+
* followers list, following list, and follow status checks.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { routes, requestHandlerMap } from '@molecule/api-resource-follow'
|
|
11
|
+
*
|
|
12
|
+
* // Wire routes into your Express app via mlcl inject
|
|
13
|
+
* // POST /follow/:targetType/:targetId
|
|
14
|
+
* // DELETE /follow/:targetType/:targetId
|
|
15
|
+
* // GET /:targetType/:targetId/followers
|
|
16
|
+
* // GET /following
|
|
17
|
+
* // GET /follow/check/:targetType/:targetId
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @remarks
|
|
21
|
+
* - **List endpoints return a PAGINATED envelope** `{ data, total, limit, offset }`, not a
|
|
22
|
+
* bare array — read the rows off `result.data` (server). On the client, `unwrapList(res)`
|
|
23
|
+
* from `@molecule/app-http` normalizes this envelope (pass it the whole HttpResponse), so
|
|
24
|
+
* the rows come back; reading the response as a bare array — or `res.data` alone (which is
|
|
25
|
+
* the envelope) — yields an EMPTY list.
|
|
26
|
+
* Table: `src/__setup__/follows.sql` creates the single `follows` table. An
|
|
27
|
+
* mlcl-scaffolded API replays `__setup__/*.sql` automatically on migrate;
|
|
28
|
+
* anywhere else run it once — nothing at runtime creates it.
|
|
29
|
+
*
|
|
30
|
+
* The follower is ALWAYS the authenticated user: handlers read
|
|
31
|
+
* `res.locals.session` (populated by your global auth middleware) and 401
|
|
32
|
+
* without it — never accept a follower userId from the body or params.
|
|
33
|
+
* `GET /:targetType/:targetId/followers` is deliberately PUBLIC; gate it
|
|
34
|
+
* yourself if follower lists are private in your app.
|
|
35
|
+
*
|
|
36
|
+
* `targetType` is a free-form string (`user`, `post`, …) — the package does
|
|
37
|
+
* not validate it against your schema, so constrain accepted values in your
|
|
38
|
+
* app if arbitrary types would be a problem.
|
|
39
|
+
*
|
|
40
|
+
* @e2e
|
|
41
|
+
* Integration checklist — drive the real UI (live preview, no mocks), adapt
|
|
42
|
+
* each item to this app's actual screens/flows, and check every box off one
|
|
43
|
+
* by one. A box you can't check is an integration bug to fix — not a skip:
|
|
44
|
+
* - [ ] User A follows user B (`POST /follow/user/:B`): B's follower count and
|
|
45
|
+
* A's following count each increment by exactly one, B appears in A's
|
|
46
|
+
* following list (`GET /following`) and A appears in B's followers list
|
|
47
|
+
* (`GET /user/:B/followers`). Reload — the edge and both counts persist (it's
|
|
48
|
+
* a real `follows` row, not local UI state).
|
|
49
|
+
* - [ ] Following is IDEMPOTENT: A following B a second time (double-tap Follow
|
|
50
|
+
* or replay the POST) creates NO duplicate edge and does NOT double-count —
|
|
51
|
+
* exactly one `follows` row exists for (A → B) and both counts are unchanged.
|
|
52
|
+
* - [ ] Unfollow (`DELETE /follow/user/:B`) removes the edge: A's following
|
|
53
|
+
* count and B's follower count each decrement back, B leaves A's following
|
|
54
|
+
* list, A leaves B's followers, and `GET /follow/check/user/:B` now returns
|
|
55
|
+
* `{ following: false }`.
|
|
56
|
+
* - [ ] You cannot follow yourself: the UI never offers Follow on your own
|
|
57
|
+
* profile, and following your own id never inflates your own counts. This
|
|
58
|
+
* package's `follow()` does not reject `followerId === targetId`, so the app
|
|
59
|
+
* must guard it — verify the guard exists, don't assume it.
|
|
60
|
+
* - [ ] AUTHORIZATION — the follower is ALWAYS the session user: handlers read
|
|
61
|
+
* `res.locals.session` and 401 without it, so no UI or endpoint lets you
|
|
62
|
+
* follow/unfollow on behalf of another user by passing their id, and
|
|
63
|
+
* follow/unfollow act only on your own edges. Signed in as A you can never
|
|
64
|
+
* make B follow or unfollow anyone.
|
|
65
|
+
*/
|
|
66
|
+
export * from './authorizers/index.js';
|
|
67
|
+
export * from './browser-guard.js';
|
|
68
|
+
export * from './handlers/index.js';
|
|
69
|
+
export * from './requestHandlerMap.js';
|
|
70
|
+
export * from './routes.js';
|
|
71
|
+
export * from './service.js';
|
|
72
|
+
export * from './types.js';
|
|
73
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgEG;AAEH,cAAc,wBAAwB,CAAA;AACtC,cAAc,oBAAoB,CAAA;AAClC,cAAc,qBAAqB,CAAA;AACnC,cAAc,wBAAwB,CAAA;AACtC,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,cAAc,YAAY,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Follow/unfollow resource for molecule.dev.
|
|
3
|
+
*
|
|
4
|
+
* Polymorphic follow system for users or any resource type. Supports
|
|
5
|
+
* followers list, following list, and follow status checks.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { routes, requestHandlerMap } from '@molecule/api-resource-follow'
|
|
11
|
+
*
|
|
12
|
+
* // Wire routes into your Express app via mlcl inject
|
|
13
|
+
* // POST /follow/:targetType/:targetId
|
|
14
|
+
* // DELETE /follow/:targetType/:targetId
|
|
15
|
+
* // GET /:targetType/:targetId/followers
|
|
16
|
+
* // GET /following
|
|
17
|
+
* // GET /follow/check/:targetType/:targetId
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @remarks
|
|
21
|
+
* - **List endpoints return a PAGINATED envelope** `{ data, total, limit, offset }`, not a
|
|
22
|
+
* bare array — read the rows off `result.data` (server). On the client, `unwrapList(res)`
|
|
23
|
+
* from `@molecule/app-http` normalizes this envelope (pass it the whole HttpResponse), so
|
|
24
|
+
* the rows come back; reading the response as a bare array — or `res.data` alone (which is
|
|
25
|
+
* the envelope) — yields an EMPTY list.
|
|
26
|
+
* Table: `src/__setup__/follows.sql` creates the single `follows` table. An
|
|
27
|
+
* mlcl-scaffolded API replays `__setup__/*.sql` automatically on migrate;
|
|
28
|
+
* anywhere else run it once — nothing at runtime creates it.
|
|
29
|
+
*
|
|
30
|
+
* The follower is ALWAYS the authenticated user: handlers read
|
|
31
|
+
* `res.locals.session` (populated by your global auth middleware) and 401
|
|
32
|
+
* without it — never accept a follower userId from the body or params.
|
|
33
|
+
* `GET /:targetType/:targetId/followers` is deliberately PUBLIC; gate it
|
|
34
|
+
* yourself if follower lists are private in your app.
|
|
35
|
+
*
|
|
36
|
+
* `targetType` is a free-form string (`user`, `post`, …) — the package does
|
|
37
|
+
* not validate it against your schema, so constrain accepted values in your
|
|
38
|
+
* app if arbitrary types would be a problem.
|
|
39
|
+
*
|
|
40
|
+
* @e2e
|
|
41
|
+
* Integration checklist — drive the real UI (live preview, no mocks), adapt
|
|
42
|
+
* each item to this app's actual screens/flows, and check every box off one
|
|
43
|
+
* by one. A box you can't check is an integration bug to fix — not a skip:
|
|
44
|
+
* - [ ] User A follows user B (`POST /follow/user/:B`): B's follower count and
|
|
45
|
+
* A's following count each increment by exactly one, B appears in A's
|
|
46
|
+
* following list (`GET /following`) and A appears in B's followers list
|
|
47
|
+
* (`GET /user/:B/followers`). Reload — the edge and both counts persist (it's
|
|
48
|
+
* a real `follows` row, not local UI state).
|
|
49
|
+
* - [ ] Following is IDEMPOTENT: A following B a second time (double-tap Follow
|
|
50
|
+
* or replay the POST) creates NO duplicate edge and does NOT double-count —
|
|
51
|
+
* exactly one `follows` row exists for (A → B) and both counts are unchanged.
|
|
52
|
+
* - [ ] Unfollow (`DELETE /follow/user/:B`) removes the edge: A's following
|
|
53
|
+
* count and B's follower count each decrement back, B leaves A's following
|
|
54
|
+
* list, A leaves B's followers, and `GET /follow/check/user/:B` now returns
|
|
55
|
+
* `{ following: false }`.
|
|
56
|
+
* - [ ] You cannot follow yourself: the UI never offers Follow on your own
|
|
57
|
+
* profile, and following your own id never inflates your own counts. This
|
|
58
|
+
* package's `follow()` does not reject `followerId === targetId`, so the app
|
|
59
|
+
* must guard it — verify the guard exists, don't assume it.
|
|
60
|
+
* - [ ] AUTHORIZATION — the follower is ALWAYS the session user: handlers read
|
|
61
|
+
* `res.locals.session` and 401 without it, so no UI or endpoint lets you
|
|
62
|
+
* follow/unfollow on behalf of another user by passing their id, and
|
|
63
|
+
* follow/unfollow act only on your own edges. Signed in as A you can never
|
|
64
|
+
* make B follow or unfollow anyone.
|
|
65
|
+
*/
|
|
66
|
+
export * from './authorizers/index.js';
|
|
67
|
+
export * from './browser-guard.js';
|
|
68
|
+
export * from './handlers/index.js';
|
|
69
|
+
export * from './requestHandlerMap.js';
|
|
70
|
+
export * from './routes.js';
|
|
71
|
+
export * from './service.js';
|
|
72
|
+
export * from './types.js';
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Maps route handler names to their implementations.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { create } from './handlers/create.js';
|
|
7
|
+
import { del } from './handlers/del.js';
|
|
8
|
+
import { list } from './handlers/list.js';
|
|
9
|
+
import { following } from './handlers/read.js';
|
|
10
|
+
import { checkFollowing } from './handlers/update.js';
|
|
11
|
+
/**
|
|
12
|
+
* Handler map for follow routes.
|
|
13
|
+
*/
|
|
14
|
+
export declare const requestHandlerMap: {
|
|
15
|
+
readonly create: typeof create;
|
|
16
|
+
readonly del: typeof del;
|
|
17
|
+
readonly list: typeof list;
|
|
18
|
+
readonly following: typeof following;
|
|
19
|
+
readonly checkFollowing: typeof checkFollowing;
|
|
20
|
+
};
|
|
21
|
+
//# sourceMappingURL=requestHandlerMap.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"requestHandlerMap.d.ts","sourceRoot":"","sources":["../src/requestHandlerMap.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAA;AAC7C,OAAO,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAA;AACvC,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAA;AACzC,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAA;AAC9C,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AAErD;;GAEG;AACH,eAAO,MAAM,iBAAiB;;;;;;CAMpB,CAAA"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Maps route handler names to their implementations.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { create } from './handlers/create.js';
|
|
7
|
+
import { del } from './handlers/del.js';
|
|
8
|
+
import { list } from './handlers/list.js';
|
|
9
|
+
import { following } from './handlers/read.js';
|
|
10
|
+
import { checkFollowing } from './handlers/update.js';
|
|
11
|
+
/**
|
|
12
|
+
* Handler map for follow routes.
|
|
13
|
+
*/
|
|
14
|
+
export const requestHandlerMap = {
|
|
15
|
+
create,
|
|
16
|
+
del,
|
|
17
|
+
list,
|
|
18
|
+
following,
|
|
19
|
+
checkFollowing,
|
|
20
|
+
};
|
package/dist/routes.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Follow route definitions.
|
|
3
|
+
*
|
|
4
|
+
* Declarative routing — mlcl inject reads these to generate the Express router.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Routes for follow/unfollow, followers, following, and status check.
|
|
10
|
+
*/
|
|
11
|
+
export declare const routes: readonly [{
|
|
12
|
+
readonly method: "post";
|
|
13
|
+
readonly path: "/follow/:targetType/:targetId";
|
|
14
|
+
readonly handler: "create";
|
|
15
|
+
readonly middlewares: readonly ["authenticate"];
|
|
16
|
+
}, {
|
|
17
|
+
readonly method: "delete";
|
|
18
|
+
readonly path: "/follow/:targetType/:targetId";
|
|
19
|
+
readonly handler: "del";
|
|
20
|
+
readonly middlewares: readonly ["authenticate"];
|
|
21
|
+
}, {
|
|
22
|
+
readonly method: "get";
|
|
23
|
+
readonly path: "/:targetType/:targetId/followers";
|
|
24
|
+
readonly handler: "list";
|
|
25
|
+
}, {
|
|
26
|
+
readonly method: "get";
|
|
27
|
+
readonly path: "/following";
|
|
28
|
+
readonly handler: "following";
|
|
29
|
+
readonly middlewares: readonly ["authenticate"];
|
|
30
|
+
}, {
|
|
31
|
+
readonly method: "get";
|
|
32
|
+
readonly path: "/follow/check/:targetType/:targetId";
|
|
33
|
+
readonly handler: "checkFollowing";
|
|
34
|
+
readonly middlewares: readonly ["authenticate"];
|
|
35
|
+
}];
|
|
36
|
+
//# sourceMappingURL=routes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"routes.d.ts","sourceRoot":"","sources":["../src/routes.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH;;GAEG;AACH,eAAO,MAAM,MAAM;;;;;;;;;;;;;;;;;;;;;;;;EAqBT,CAAA"}
|
package/dist/routes.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Follow route definitions.
|
|
3
|
+
*
|
|
4
|
+
* Declarative routing — mlcl inject reads these to generate the Express router.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Routes for follow/unfollow, followers, following, and status check.
|
|
10
|
+
*/
|
|
11
|
+
export const routes = [
|
|
12
|
+
{
|
|
13
|
+
method: 'post',
|
|
14
|
+
path: '/follow/:targetType/:targetId',
|
|
15
|
+
handler: 'create',
|
|
16
|
+
middlewares: ['authenticate'],
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
method: 'delete',
|
|
20
|
+
path: '/follow/:targetType/:targetId',
|
|
21
|
+
handler: 'del',
|
|
22
|
+
middlewares: ['authenticate'],
|
|
23
|
+
},
|
|
24
|
+
{ method: 'get', path: '/:targetType/:targetId/followers', handler: 'list' },
|
|
25
|
+
{ method: 'get', path: '/following', handler: 'following', middlewares: ['authenticate'] },
|
|
26
|
+
{
|
|
27
|
+
method: 'get',
|
|
28
|
+
path: '/follow/check/:targetType/:targetId',
|
|
29
|
+
handler: 'checkFollowing',
|
|
30
|
+
middlewares: ['authenticate'],
|
|
31
|
+
},
|
|
32
|
+
];
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Follow business logic service.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type { Follow, PaginatedResult, PaginationOptions } from './types.js';
|
|
7
|
+
/**
|
|
8
|
+
* Creates a follow relationship. Idempotent — returns existing follow if already following.
|
|
9
|
+
*
|
|
10
|
+
* @param followerId - The ID of the follower.
|
|
11
|
+
* @param targetType - The type of target being followed.
|
|
12
|
+
* @param targetId - The ID of the target being followed.
|
|
13
|
+
* @returns The created or existing follow.
|
|
14
|
+
*/
|
|
15
|
+
export declare function follow(followerId: string, targetType: string, targetId: string): Promise<Follow>;
|
|
16
|
+
/**
|
|
17
|
+
* Removes a follow relationship.
|
|
18
|
+
*
|
|
19
|
+
* @param followerId - The ID of the follower.
|
|
20
|
+
* @param targetType - The type of target.
|
|
21
|
+
* @param targetId - The ID of the target.
|
|
22
|
+
*/
|
|
23
|
+
export declare function unfollow(followerId: string, targetType: string, targetId: string): Promise<void>;
|
|
24
|
+
/**
|
|
25
|
+
* Gets paginated followers of a target.
|
|
26
|
+
*
|
|
27
|
+
* @param targetType - The type of target.
|
|
28
|
+
* @param targetId - The ID of the target.
|
|
29
|
+
* @param options - Pagination options.
|
|
30
|
+
* @returns Paginated followers.
|
|
31
|
+
*/
|
|
32
|
+
export declare function getFollowers(targetType: string, targetId: string, options?: PaginationOptions): Promise<PaginatedResult<Follow>>;
|
|
33
|
+
/**
|
|
34
|
+
* Gets paginated targets a user is following.
|
|
35
|
+
*
|
|
36
|
+
* @param userId - The follower's user ID.
|
|
37
|
+
* @param options - Pagination options.
|
|
38
|
+
* @returns Paginated follows.
|
|
39
|
+
*/
|
|
40
|
+
export declare function getFollowing(userId: string, options?: PaginationOptions): Promise<PaginatedResult<Follow>>;
|
|
41
|
+
/**
|
|
42
|
+
* Checks if a user is following a target.
|
|
43
|
+
*
|
|
44
|
+
* @param followerId - The follower's user ID.
|
|
45
|
+
* @param targetType - The type of target.
|
|
46
|
+
* @param targetId - The ID of the target.
|
|
47
|
+
* @returns `true` if following.
|
|
48
|
+
*/
|
|
49
|
+
export declare function isFollowing(followerId: string, targetType: string, targetId: string): Promise<boolean>;
|
|
50
|
+
/**
|
|
51
|
+
* Gets the follower count for a target.
|
|
52
|
+
*
|
|
53
|
+
* @param targetType - The type of target.
|
|
54
|
+
* @param targetId - The ID of the target.
|
|
55
|
+
* @returns The number of followers.
|
|
56
|
+
*/
|
|
57
|
+
export declare function getFollowerCount(targetType: string, targetId: string): Promise<number>;
|
|
58
|
+
/**
|
|
59
|
+
* Gets the following count for a user.
|
|
60
|
+
*
|
|
61
|
+
* @param userId - The user ID.
|
|
62
|
+
* @returns The number of targets the user is following.
|
|
63
|
+
*/
|
|
64
|
+
export declare function getFollowingCount(userId: string): Promise<number>;
|
|
65
|
+
//# sourceMappingURL=service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../src/service.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,KAAK,EAAE,MAAM,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAI5E;;;;;;;GAOG;AACH,wBAAsB,MAAM,CAC1B,UAAU,EAAE,MAAM,EAClB,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,MAAM,CAAC,CAWjB;AAED;;;;;;GAMG;AACH,wBAAsB,QAAQ,CAC5B,UAAU,EAAE,MAAM,EAClB,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,IAAI,CAAC,CAMf;AAED;;;;;;;GAOG;AACH,wBAAsB,YAAY,CAChC,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE,MAAM,EAChB,OAAO,GAAE,iBAAsB,GAC9B,OAAO,CAAC,eAAe,CAAC,MAAM,CAAC,CAAC,CAoBlC;AAED;;;;;;GAMG;AACH,wBAAsB,YAAY,CAChC,MAAM,EAAE,MAAM,EACd,OAAO,GAAE,iBAAsB,GAC9B,OAAO,CAAC,eAAe,CAAC,MAAM,CAAC,CAAC,CAiBlC;AAED;;;;;;;GAOG;AACH,wBAAsB,WAAW,CAC/B,UAAU,EAAE,MAAM,EAClB,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,OAAO,CAAC,CAOlB;AAED;;;;;;GAMG;AACH,wBAAsB,gBAAgB,CAAC,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAK5F;AAED;;;;;GAKG;AACH,wBAAsB,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAEvE"}
|
package/dist/service.js
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Follow business logic service.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { count, create as dbCreate, deleteMany, findMany, findOne } from '@molecule/api-database';
|
|
7
|
+
const TABLE = 'follows';
|
|
8
|
+
/**
|
|
9
|
+
* Creates a follow relationship. Idempotent — returns existing follow if already following.
|
|
10
|
+
*
|
|
11
|
+
* @param followerId - The ID of the follower.
|
|
12
|
+
* @param targetType - The type of target being followed.
|
|
13
|
+
* @param targetId - The ID of the target being followed.
|
|
14
|
+
* @returns The created or existing follow.
|
|
15
|
+
*/
|
|
16
|
+
export async function follow(followerId, targetType, targetId) {
|
|
17
|
+
const existing = await findOne(TABLE, [
|
|
18
|
+
{ field: 'followerId', operator: '=', value: followerId },
|
|
19
|
+
{ field: 'targetType', operator: '=', value: targetType },
|
|
20
|
+
{ field: 'targetId', operator: '=', value: targetId },
|
|
21
|
+
]);
|
|
22
|
+
if (existing)
|
|
23
|
+
return existing;
|
|
24
|
+
const result = await dbCreate(TABLE, { followerId, targetType, targetId });
|
|
25
|
+
return result.data;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Removes a follow relationship.
|
|
29
|
+
*
|
|
30
|
+
* @param followerId - The ID of the follower.
|
|
31
|
+
* @param targetType - The type of target.
|
|
32
|
+
* @param targetId - The ID of the target.
|
|
33
|
+
*/
|
|
34
|
+
export async function unfollow(followerId, targetType, targetId) {
|
|
35
|
+
await deleteMany(TABLE, [
|
|
36
|
+
{ field: 'followerId', operator: '=', value: followerId },
|
|
37
|
+
{ field: 'targetType', operator: '=', value: targetType },
|
|
38
|
+
{ field: 'targetId', operator: '=', value: targetId },
|
|
39
|
+
]);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Gets paginated followers of a target.
|
|
43
|
+
*
|
|
44
|
+
* @param targetType - The type of target.
|
|
45
|
+
* @param targetId - The ID of the target.
|
|
46
|
+
* @param options - Pagination options.
|
|
47
|
+
* @returns Paginated followers.
|
|
48
|
+
*/
|
|
49
|
+
export async function getFollowers(targetType, targetId, options = {}) {
|
|
50
|
+
const limit = options.limit ?? 20;
|
|
51
|
+
const offset = options.offset ?? 0;
|
|
52
|
+
const where = [
|
|
53
|
+
{ field: 'targetType', operator: '=', value: targetType },
|
|
54
|
+
{ field: 'targetId', operator: '=', value: targetId },
|
|
55
|
+
];
|
|
56
|
+
const [data, total] = await Promise.all([
|
|
57
|
+
findMany(TABLE, {
|
|
58
|
+
where,
|
|
59
|
+
orderBy: [{ field: 'createdAt', direction: 'desc' }],
|
|
60
|
+
limit,
|
|
61
|
+
offset,
|
|
62
|
+
}),
|
|
63
|
+
count(TABLE, where),
|
|
64
|
+
]);
|
|
65
|
+
return { data, total, limit, offset };
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Gets paginated targets a user is following.
|
|
69
|
+
*
|
|
70
|
+
* @param userId - The follower's user ID.
|
|
71
|
+
* @param options - Pagination options.
|
|
72
|
+
* @returns Paginated follows.
|
|
73
|
+
*/
|
|
74
|
+
export async function getFollowing(userId, options = {}) {
|
|
75
|
+
const limit = options.limit ?? 20;
|
|
76
|
+
const offset = options.offset ?? 0;
|
|
77
|
+
const where = [{ field: 'followerId', operator: '=', value: userId }];
|
|
78
|
+
const [data, total] = await Promise.all([
|
|
79
|
+
findMany(TABLE, {
|
|
80
|
+
where,
|
|
81
|
+
orderBy: [{ field: 'createdAt', direction: 'desc' }],
|
|
82
|
+
limit,
|
|
83
|
+
offset,
|
|
84
|
+
}),
|
|
85
|
+
count(TABLE, where),
|
|
86
|
+
]);
|
|
87
|
+
return { data, total, limit, offset };
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Checks if a user is following a target.
|
|
91
|
+
*
|
|
92
|
+
* @param followerId - The follower's user ID.
|
|
93
|
+
* @param targetType - The type of target.
|
|
94
|
+
* @param targetId - The ID of the target.
|
|
95
|
+
* @returns `true` if following.
|
|
96
|
+
*/
|
|
97
|
+
export async function isFollowing(followerId, targetType, targetId) {
|
|
98
|
+
const existing = await findOne(TABLE, [
|
|
99
|
+
{ field: 'followerId', operator: '=', value: followerId },
|
|
100
|
+
{ field: 'targetType', operator: '=', value: targetType },
|
|
101
|
+
{ field: 'targetId', operator: '=', value: targetId },
|
|
102
|
+
]);
|
|
103
|
+
return existing !== null;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Gets the follower count for a target.
|
|
107
|
+
*
|
|
108
|
+
* @param targetType - The type of target.
|
|
109
|
+
* @param targetId - The ID of the target.
|
|
110
|
+
* @returns The number of followers.
|
|
111
|
+
*/
|
|
112
|
+
export async function getFollowerCount(targetType, targetId) {
|
|
113
|
+
return count(TABLE, [
|
|
114
|
+
{ field: 'targetType', operator: '=', value: targetType },
|
|
115
|
+
{ field: 'targetId', operator: '=', value: targetId },
|
|
116
|
+
]);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Gets the following count for a user.
|
|
120
|
+
*
|
|
121
|
+
* @param userId - The user ID.
|
|
122
|
+
* @returns The number of targets the user is following.
|
|
123
|
+
*/
|
|
124
|
+
export async function getFollowingCount(userId) {
|
|
125
|
+
return count(TABLE, [{ field: 'followerId', operator: '=', value: userId }]);
|
|
126
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Follow resource type definitions.
|
|
3
|
+
*
|
|
4
|
+
* Polymorphic follow/unfollow system for users or any resource type.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* A follow relationship between a user and a target resource.
|
|
10
|
+
*/
|
|
11
|
+
export interface Follow {
|
|
12
|
+
/** Unique follow identifier. */
|
|
13
|
+
id: string;
|
|
14
|
+
/** The ID of the user who is following. */
|
|
15
|
+
followerId: string;
|
|
16
|
+
/** The type of target being followed (e.g. 'user', 'project'). */
|
|
17
|
+
targetType: string;
|
|
18
|
+
/** The ID of the target being followed. */
|
|
19
|
+
targetId: string;
|
|
20
|
+
/** When the follow was created (ISO 8601). */
|
|
21
|
+
createdAt: string;
|
|
22
|
+
/** When the follow was last updated (ISO 8601). */
|
|
23
|
+
updatedAt: string;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Options for paginated queries.
|
|
27
|
+
*/
|
|
28
|
+
export interface PaginationOptions {
|
|
29
|
+
/** Maximum number of results to return. */
|
|
30
|
+
limit?: number;
|
|
31
|
+
/** Number of results to skip. */
|
|
32
|
+
offset?: number;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* A paginated result set.
|
|
36
|
+
*/
|
|
37
|
+
export interface PaginatedResult<T> {
|
|
38
|
+
/** The result items for the current page. */
|
|
39
|
+
data: T[];
|
|
40
|
+
/** Total number of matching items across all pages. */
|
|
41
|
+
total: number;
|
|
42
|
+
/** Maximum number of results per page. */
|
|
43
|
+
limit: number;
|
|
44
|
+
/** Number of results skipped. */
|
|
45
|
+
offset: number;
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH;;GAEG;AACH,MAAM,WAAW,MAAM;IACrB,gCAAgC;IAChC,EAAE,EAAE,MAAM,CAAA;IACV,2CAA2C;IAC3C,UAAU,EAAE,MAAM,CAAA;IAClB,kEAAkE;IAClE,UAAU,EAAE,MAAM,CAAA;IAClB,2CAA2C;IAC3C,QAAQ,EAAE,MAAM,CAAA;IAChB,8CAA8C;IAC9C,SAAS,EAAE,MAAM,CAAA;IACjB,mDAAmD;IACnD,SAAS,EAAE,MAAM,CAAA;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,2CAA2C;IAC3C,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,iCAAiC;IACjC,MAAM,CAAC,EAAE,MAAM,CAAA;CAChB;AAED;;GAEG;AACH,MAAM,WAAW,eAAe,CAAC,CAAC;IAChC,6CAA6C;IAC7C,IAAI,EAAE,CAAC,EAAE,CAAA;IACT,uDAAuD;IACvD,KAAK,EAAE,MAAM,CAAA;IACb,0CAA0C;IAC1C,KAAK,EAAE,MAAM,CAAA;IACb,iCAAiC;IACjC,MAAM,EAAE,MAAM,CAAA;CACf"}
|
package/dist/types.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@molecule/api-resource-follow",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Follow/unfollow users or any resource with polymorphic targeting",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc",
|
|
10
|
+
"test": "vitest run",
|
|
11
|
+
"test:watch": "vitest"
|
|
12
|
+
},
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"keywords": [
|
|
23
|
+
"molecule",
|
|
24
|
+
"follow",
|
|
25
|
+
"unfollow",
|
|
26
|
+
"resource"
|
|
27
|
+
],
|
|
28
|
+
"license": "Apache-2.0",
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/node": "26.1.2",
|
|
31
|
+
"typescript": "6.0.3",
|
|
32
|
+
"vitest": "4.1.10"
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"@molecule/api-database": "^1.0.0",
|
|
36
|
+
"@molecule/api-i18n": "^1.0.0",
|
|
37
|
+
"@molecule/api-logger": "^1.0.0",
|
|
38
|
+
"@molecule/api-resource": "^1.0.0"
|
|
39
|
+
},
|
|
40
|
+
"peerDependenciesMeta": {
|
|
41
|
+
"@molecule/api-database": {
|
|
42
|
+
"optional": true
|
|
43
|
+
},
|
|
44
|
+
"@molecule/api-i18n": {
|
|
45
|
+
"optional": true
|
|
46
|
+
},
|
|
47
|
+
"@molecule/api-logger": {
|
|
48
|
+
"optional": true
|
|
49
|
+
},
|
|
50
|
+
"@molecule/api-resource": {
|
|
51
|
+
"optional": true
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
"repository": {
|
|
55
|
+
"type": "git",
|
|
56
|
+
"url": "https://github.com/molecule-dev/molecule.git",
|
|
57
|
+
"directory": "packages/api/resources/follow"
|
|
58
|
+
},
|
|
59
|
+
"homepage": "https://github.com/molecule-dev/molecule/tree/main/packages/api/resources/follow",
|
|
60
|
+
"bugs": "https://github.com/molecule-dev/molecule/issues",
|
|
61
|
+
"publishConfig": {
|
|
62
|
+
"access": "public"
|
|
63
|
+
}
|
|
64
|
+
}
|