@uidu/client 3.22.0 → 3.23.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/src/votes.ts ADDED
@@ -0,0 +1,38 @@
1
+ import type { UiduClient } from './client';
2
+ import {
3
+ CreateVoteDocument,
4
+ DeleteVoteDocument,
5
+ type VoteFieldsFragment,
6
+ } from './generated';
7
+
8
+ // Votes and reactions on a record — for a custom app, one of its ModelItems
9
+ // (Votes::Vote::Create, which covers taking one back too). The voter is always
10
+ // the contact of the token's user, and only one's own vote can be taken back.
11
+
12
+ export type Vote = VoteFieldsFragment;
13
+
14
+ /**
15
+ * Vote on a record as the token's user: an upvote by default, `voteFlag:
16
+ * false` for a downvote, or a reaction with `emoji`. Returns the payload: the
17
+ * vote at `vote.node`, the record's new `votable.votesCount`, and `errors`.
18
+ */
19
+ export async function vote(
20
+ client: UiduClient,
21
+ variables: { votableId: string; voteFlag?: boolean; emoji?: string },
22
+ ) {
23
+ const { votableId, voteFlag = true, emoji } = variables;
24
+ const result = await client.mutate(CreateVoteDocument, {
25
+ input: { votableId, attributes: { voteFlag, emoji } },
26
+ });
27
+ return result.createVote ?? null;
28
+ }
29
+
30
+ /**
31
+ * Take back one's own vote (`id` of a vote whose `mine` is true). Someone
32
+ * else's comes back as an error, not thrown. Returns the payload with the
33
+ * record's new `votable.votesCount`.
34
+ */
35
+ export async function unvote(client: UiduClient, variables: { id: string }) {
36
+ const result = await client.mutate(DeleteVoteDocument, { input: variables });
37
+ return result.deleteVote ?? null;
38
+ }