@amityco/ts-sdk 0.0.1-beta.17 → 0.0.1-beta.18

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.
Files changed (55) hide show
  1. package/dist/@types/core/model.d.ts +2 -0
  2. package/dist/@types/core/model.d.ts.map +1 -1
  3. package/dist/@types/core/payload.d.ts +5 -0
  4. package/dist/@types/core/payload.d.ts.map +1 -1
  5. package/dist/@types/domains/poll.d.ts +27 -0
  6. package/dist/@types/domains/poll.d.ts.map +1 -0
  7. package/dist/@types/domains/post.d.ts +1 -1
  8. package/dist/@types/domains/post.d.ts.map +1 -1
  9. package/dist/@types/index.d.ts +1 -0
  10. package/dist/@types/index.d.ts.map +1 -1
  11. package/dist/channel/api/deleteChannel.d.ts.map +1 -1
  12. package/dist/core/model/idResolvers.d.ts.map +1 -1
  13. package/dist/core/model/identifyModel.d.ts.map +1 -1
  14. package/dist/core/model/index.d.ts.map +1 -1
  15. package/dist/core/subscription.d.ts.map +1 -1
  16. package/dist/file/api/createFile.d.ts +1 -1
  17. package/dist/file/api/createFile.d.ts.map +1 -1
  18. package/dist/index.cjs.js +187 -9
  19. package/dist/index.d.ts +1 -0
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.esm.js +183 -10
  22. package/dist/index.umd.js +5 -5
  23. package/dist/poll/api/closePoll.d.ts +16 -0
  24. package/dist/poll/api/closePoll.d.ts.map +1 -0
  25. package/dist/poll/api/createPoll.d.ts +23 -0
  26. package/dist/poll/api/createPoll.d.ts.map +1 -0
  27. package/dist/poll/api/deletePoll.d.ts +16 -0
  28. package/dist/poll/api/deletePoll.d.ts.map +1 -0
  29. package/dist/poll/api/getPoll.d.ts +32 -0
  30. package/dist/poll/api/getPoll.d.ts.map +1 -0
  31. package/dist/poll/api/index.d.ts +6 -0
  32. package/dist/poll/api/index.d.ts.map +1 -0
  33. package/dist/poll/api/votePoll.d.ts +17 -0
  34. package/dist/poll/api/votePoll.d.ts.map +1 -0
  35. package/dist/utils/env.d.ts.map +1 -1
  36. package/package.json +1 -1
  37. package/src/@types/core/model.ts +2 -0
  38. package/src/@types/core/payload.ts +6 -0
  39. package/src/@types/domains/poll.ts +32 -0
  40. package/src/@types/domains/post.ts +1 -1
  41. package/src/@types/index.ts +2 -0
  42. package/src/channel/api/deleteChannel.ts +0 -1
  43. package/src/core/model/idResolvers.ts +1 -0
  44. package/src/core/model/identifyModel.ts +1 -0
  45. package/src/core/model/index.ts +1 -0
  46. package/src/core/subscription.ts +1 -3
  47. package/src/file/api/createFile.ts +5 -5
  48. package/src/index.ts +2 -0
  49. package/src/poll/api/closePoll.ts +39 -0
  50. package/src/poll/api/createPoll.ts +43 -0
  51. package/src/poll/api/deletePoll.ts +36 -0
  52. package/src/poll/api/getPoll.ts +64 -0
  53. package/src/poll/api/index.ts +7 -0
  54. package/src/poll/api/votePoll.ts +41 -0
  55. package/src/utils/env.ts +3 -1
@@ -0,0 +1,16 @@
1
+ /**
2
+ * ```js
3
+ * import { closePoll } from '@amityco/ts-sdk'
4
+ * const updated = await closePoll(pollId)
5
+ * ```
6
+ *
7
+ * Updates an {@link Amity.Poll}
8
+ *
9
+ * @param pollId The ID of the {@link Amity.Poll} to close
10
+ * @returns the updated {@link Amity.Poll} object
11
+ *
12
+ * @category Poll API
13
+ * @async
14
+ */
15
+ export declare const closePoll: (pollId: Amity.Poll['pollId']) => Promise<Amity.Cached<Amity.Poll>>;
16
+ //# sourceMappingURL=closePoll.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"closePoll.d.ts","sourceRoot":"","sources":["../../../src/poll/api/closePoll.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,SAAS,WACZ,MAAM,IAAI,CAAC,QAAQ,CAAC,KAC3B,QAAQ,MAAM,MAAM,CAAC,MAAM,IAAI,CAAC,CAkBlC,CAAC"}
@@ -0,0 +1,23 @@
1
+ /**
2
+ * ```js
3
+ * import { createPoll } from '@amityco/ts-sdk'
4
+ * const created = await createPoll({
5
+ * question: 'question',
6
+ * answers: [
7
+ * { dataType: 'text', data: 'answer1' },
8
+ * { dataType: 'text', data: 'answer2' },
9
+ * ],
10
+ * closedIn: 1649136484
11
+ * }))
12
+ * ```
13
+ *
14
+ * Creates an {@link Amity.Poll}
15
+ *
16
+ * @param bundle The data necessary to create a new {@link Amity.Poll}
17
+ * @returns The newly created {@link Amity.Poll}
18
+ *
19
+ * @category Poll API
20
+ * @async
21
+ */
22
+ export declare const createPoll: (bundle: Pick<Amity.Poll, 'question' | 'answers' | 'answerType' | 'closedIn'>) => Promise<Amity.Cached<Amity.Poll>>;
23
+ //# sourceMappingURL=createPoll.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createPoll.d.ts","sourceRoot":"","sources":["../../../src/poll/api/createPoll.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,UAAU,WACb,KAAK,MAAM,IAAI,EAAE,UAAU,GAAG,SAAS,GAAG,YAAY,GAAG,UAAU,CAAC,KAC3E,QAAQ,MAAM,MAAM,CAAC,MAAM,IAAI,CAAC,CAelC,CAAC"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * ```js
3
+ * import { deletePoll } from '@amityco/ts-sdk'
4
+ * const success = await deletePoll(pollId)
5
+ * ```
6
+ *
7
+ * Deletes a {@link Amity.Poll}
8
+ *
9
+ * @param pollId The {@link Amity.Poll} ID to delete
10
+ * @return A success boolean if the {@link Amity.Poll} was deleted
11
+ *
12
+ * @category Poll API
13
+ * @async
14
+ */
15
+ export declare const deletePoll: (pollId: Amity.Poll['pollId']) => Promise<boolean>;
16
+ //# sourceMappingURL=deletePoll.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deletePoll.d.ts","sourceRoot":"","sources":["../../../src/poll/api/deletePoll.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,UAAU,WAAkB,MAAM,IAAI,CAAC,QAAQ,CAAC,KAAG,QAAQ,OAAO,CAe9E,CAAC"}
@@ -0,0 +1,32 @@
1
+ /**
2
+ * ```js
3
+ * import { getPoll } from '@amityco/ts-sdk'
4
+ * const poll = await getPoll('foobar')
5
+ * ```
6
+ *
7
+ * Fetches a {@link Amity.Poll} object
8
+ *
9
+ * @param pollId the ID of the {@link Amity.Poll} to fetch
10
+ * @returns the associated {@link Amity.Poll} object
11
+ *
12
+ * @category Poll API
13
+ * @async
14
+ */
15
+ export declare const getPoll: {
16
+ (pollId: Amity.Poll['pollId']): Promise<Amity.Cached<Amity.Poll>>;
17
+ /**
18
+ * ```js
19
+ * import { getPoll } from '@amityco/ts-sdk'
20
+ * const poll = getPoll.locally('foobar')
21
+ * ```
22
+ *
23
+ * Fetches a {@link Amity.Poll} object
24
+ *
25
+ * @param pollId the ID of the {@link Amity.Poll} to fetch
26
+ * @returns the associated {@link Amity.Poll} object
27
+ *
28
+ * @category Poll API
29
+ */
30
+ locally(pollId: Amity.Poll['pollId']): Amity.Cached<Amity.Poll> | undefined;
31
+ };
32
+ //# sourceMappingURL=getPoll.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getPoll.d.ts","sourceRoot":"","sources":["../../../src/poll/api/getPoll.ts"],"names":[],"mappings":"AAKA;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,OAAO;aAAkB,MAAM,IAAI,CAAC,QAAQ,CAAC,GAAG,QAAQ,MAAM,MAAM,CAAC,MAAM,IAAI,CAAC,CAAC;IAiB9F;;;;;;;;;;;;OAYG;oBACwB,MAAM,IAAI,CAAC,QAAQ,CAAC,GAAG,MAAM,MAAM,CAAC,MAAM,IAAI,CAAC,GAAG,SAAS;CAfrF,CAAC"}
@@ -0,0 +1,6 @@
1
+ export * from './getPoll';
2
+ export * from './createPoll';
3
+ export * from './closePoll';
4
+ export * from './deletePoll';
5
+ export * from './votePoll';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/poll/api/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAE1B,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAE7B,cAAc,YAAY,CAAC"}
@@ -0,0 +1,17 @@
1
+ /**
2
+ * ```js
3
+ * import { votePoll } from '@amityco/ts-sdk'
4
+ * const voted = await votePoll(pollId)
5
+ * ```
6
+ *
7
+ * Votes for an {@link Amity.Poll}
8
+ *
9
+ * @param pollId The ID of the {@link Amity.Poll} to vote
10
+ * @param answerIds The IDs of the {@link Amity.Poll} answers to vote {@link Amity.Poll}
11
+ * @returns the updated {@link Amity.Poll} object
12
+ *
13
+ * @category Poll API
14
+ * @async
15
+ */
16
+ export declare const votePoll: (pollId: Amity.Poll['pollId'], answerIds: string[]) => Promise<Amity.Cached<Amity.Poll>>;
17
+ //# sourceMappingURL=votePoll.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"votePoll.d.ts","sourceRoot":"","sources":["../../../src/poll/api/votePoll.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,QAAQ,WACX,MAAM,IAAI,CAAC,QAAQ,CAAC,aACjB,MAAM,EAAE,KAClB,QAAQ,MAAM,MAAM,CAAC,MAAM,IAAI,CAAC,CAkBlC,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"env.d.ts","sourceRoot":"","sources":["../../src/utils/env.ts"],"names":[],"mappings":";AAGA,eAAO,MAAiB,WAAW,yBAAyD,CAAC;AAE7F,wBAAgB,MAAM,IAAI,MAAM,CAAC,UAAU,GAAG,SAAS,CAEtD;AAED,eAAO,MAAM,YAAY,SAA6C,CAAC"}
1
+ {"version":3,"file":"env.d.ts","sourceRoot":"","sources":["../../src/utils/env.ts"],"names":[],"mappings":";AAKA,eAAO,MAAiB,WAAW,yBAAyD,CAAC;AAE7F,wBAAgB,MAAM,IAAI,MAAM,CAAC,UAAU,GAAG,SAAS,CAEtD;AAED,eAAO,MAAM,YAAY,SAA6C,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amityco/ts-sdk",
3
- "version": "0.0.1-beta.17",
3
+ "version": "0.0.1-beta.18",
4
4
  "license": "CC-BY-ND-4.0",
5
5
  "author": "amity.co <developers@amity.co> (https://amity.co)",
6
6
  "description": "Amity Social Cloud Typescript SDK",
@@ -20,6 +20,7 @@ declare global {
20
20
  communityUsers: Amity.Membership<'community'>;
21
21
  post: Amity.Post;
22
22
  comment: Amity.Comment;
23
+ poll: Amity.Poll;
23
24
 
24
25
  stream: Amity.Stream;
25
26
 
@@ -48,6 +49,7 @@ declare global {
48
49
  communityUsers: Pick<Amity.Membership<'community'>, 'communityId' | 'userId'>;
49
50
  post: Pick<Amity.Post, 'postId'>;
50
51
  comment: Pick<Amity.Comment, 'commentId'>;
52
+ poll: Pick<Amity.Poll, 'pollId'>;
51
53
 
52
54
  stream: Pick<Amity.Stream, 'streamId'>;
53
55
 
@@ -14,6 +14,7 @@ declare global {
14
14
  communityUser: Amity.CommunityMembershipPayload;
15
15
  post: Amity.PostPayload;
16
16
  comment: Amity.CommentPayload;
17
+ poll: Amity.PollPayload;
17
18
  stream: Amity.StreamPayload;
18
19
  };
19
20
 
@@ -98,6 +99,11 @@ declare global {
98
99
  files?: Amity.File[];
99
100
  };
100
101
 
102
+ type PollPayload = {
103
+ users: Amity.User[];
104
+ polls: Amity.Poll[];
105
+ };
106
+
101
107
  type GlobalFeedPayload = Amity.PostPayload;
102
108
 
103
109
  type FollowersPayload = {
@@ -0,0 +1,32 @@
1
+ export {};
2
+
3
+ declare global {
4
+ namespace Amity {
5
+ type PollDataType = 'text';
6
+
7
+ type PollAnswerType = 'single' | 'multiple';
8
+
9
+ type PollAnswer = {
10
+ id: string;
11
+ dataType: PollDataType;
12
+ data: string;
13
+ voteCount: number;
14
+ isVotedByUser: boolean;
15
+ };
16
+
17
+ type PollStatus = 'closed' | 'open';
18
+
19
+ type Poll = {
20
+ pollId: string;
21
+ question: string;
22
+ answers: PollAnswer[];
23
+ answerType: PollAnswerType;
24
+ closedAt?: Amity.timestamp;
25
+ closedIn?: number;
26
+ isVoted?: boolean;
27
+ status: PollStatus;
28
+ userId: Amity.User['userId'];
29
+ } & Amity.Timestamps &
30
+ Amity.SoftDelete;
31
+ }
32
+ }
@@ -5,7 +5,7 @@ declare global {
5
5
  // bad hack until we get proper feed objects in backend
6
6
  type PostTargetType = Amity.FeedType | 'content';
7
7
 
8
- type PostContentType = 'text' | 'image' | 'file' | 'video';
8
+ type PostContentType = 'text' | 'image' | 'file' | 'video' | 'liveStream' | 'poll';
9
9
 
10
10
  type Post<T extends PostContentType = any> = {
11
11
  postId: string;
@@ -32,3 +32,5 @@ import './domains/comment';
32
32
  import './domains/stream';
33
33
 
34
34
  import './domains/mention';
35
+
36
+ import './domains/poll';
@@ -22,7 +22,6 @@ export const deleteChannel = async (
22
22
  const client = getActiveClient();
23
23
  client.log('channel/deleteChannel', channelId);
24
24
 
25
- // API-FIX: This endpoint has not been implemented yet.
26
25
  await client.http.delete<{ success: boolean }>(
27
26
  `/api/v3/channels/${encodeURIComponent(channelId)}`,
28
27
  );
@@ -23,6 +23,7 @@ const idResolvers: Resolvers = {
23
23
  communityUsers: ({ communityId, userId }) => `${communityId}#${userId}`,
24
24
  post: ({ postId }) => postId,
25
25
  comment: ({ commentId }) => commentId,
26
+ poll: ({ pollId }) => pollId,
26
27
 
27
28
  stream: ({ streamId }) => streamId,
28
29
 
@@ -31,6 +31,7 @@ const CRITERIAS: Criterias = {
31
31
  communityUsers: ['userId', 'communityId', 'communityMembership'],
32
32
  post: ['postId', 'feedId'],
33
33
  comment: ['commentId', 'referenceId'],
34
+ poll: ['pollId'],
34
35
  stream: ['streamId'],
35
36
  follow: ['from', 'to'],
36
37
  followCount: ['userId', 'followerCount'],
@@ -20,6 +20,7 @@ export const PAYLOAD2MODEL: Record<string, Amity.Domain> = {
20
20
  posts: 'post',
21
21
  postChildren: 'post',
22
22
  comments: 'comment',
23
+ polls: 'poll',
23
24
  videoStreamings: 'stream',
24
25
 
25
26
  follows: 'follow',
@@ -37,9 +37,7 @@ export const getUserTopic = (
37
37
  level: Exclude<SubscriptionLevels, SubscriptionLevels.COMMUNITY> = SubscriptionLevels.USER,
38
38
  ): string =>
39
39
  getCommunityUserTopic(
40
- level === SubscriptionLevels.USER
41
- ? path
42
- : path.replace(/^(?<networkId>\w*)/, '$<networkId>/social'),
40
+ level === SubscriptionLevels.USER ? path : path.replace(/^(\w*)/, '$1/social'),
43
41
  level,
44
42
  );
45
43
 
@@ -20,19 +20,19 @@ import { ingestInCache } from '~/cache/api/ingestInCache';
20
20
  export const createFile = async <T extends Amity.FileType = any>(
21
21
  formData: FormData,
22
22
  onProgress?: (percent: number) => void,
23
- ): Promise<Amity.Cached<Amity.File>> => {
23
+ ): Promise<Amity.Cached<Amity.File[]>> => {
24
24
  const client = getActiveClient();
25
25
  client.log('file/createFile', formData);
26
26
 
27
- if (!formData.getAll('file').length)
28
- throw new Error('The formData object must have a `file` key.');
27
+ if (!formData.getAll('files').length)
28
+ throw new Error('The formData object must have a `files` key.');
29
29
 
30
30
  const headers =
31
31
  'getHeaders' in formData
32
32
  ? (formData as any).getHeaders()
33
33
  : { 'content-type': 'multipart/form-data' };
34
34
 
35
- const { data } = await client.http.post<Amity.CreateFilePayload<T>>('/api/v3/files', formData, {
35
+ const { data } = await client.http.post<Amity.CreateFilePayload<T>>('/api/v4/files', formData, {
36
36
  headers,
37
37
  onUploadProgress({ loaded, total }) {
38
38
  onProgress && onProgress(Math.round((loaded * 100) / total));
@@ -46,7 +46,7 @@ export const createFile = async <T extends Amity.FileType = any>(
46
46
  if (client.cache) ingestInCache({ files: data }, { cachedAt });
47
47
 
48
48
  return {
49
- data: data[0],
49
+ data,
50
50
  cachedAt,
51
51
  };
52
52
  };
package/src/index.ts CHANGED
@@ -58,5 +58,7 @@ export * from './comment/api';
58
58
  export * from './comment/events';
59
59
  export * from './comment/observers';
60
60
 
61
+ export * from './poll/api';
62
+
61
63
  export * from './stream/api';
62
64
  export * from './stream/events';
@@ -0,0 +1,39 @@
1
+ import { getActiveClient } from '~/client/api';
2
+
3
+ import { ingestInCache } from '~/cache/api/ingestInCache';
4
+
5
+ /**
6
+ * ```js
7
+ * import { closePoll } from '@amityco/ts-sdk'
8
+ * const updated = await closePoll(pollId)
9
+ * ```
10
+ *
11
+ * Updates an {@link Amity.Poll}
12
+ *
13
+ * @param pollId The ID of the {@link Amity.Poll} to close
14
+ * @returns the updated {@link Amity.Poll} object
15
+ *
16
+ * @category Poll API
17
+ * @async
18
+ */
19
+ export const closePoll = async (
20
+ pollId: Amity.Poll['pollId'],
21
+ ): Promise<Amity.Cached<Amity.Poll>> => {
22
+ const client = getActiveClient();
23
+ client.log('user/closePoll', pollId);
24
+
25
+ const { data } = await client.http.put<Amity.PollPayload>(
26
+ `/api/v3/polls/${encodeURIComponent(pollId)}`,
27
+ { status: 'closed' },
28
+ );
29
+
30
+ const cachedAt = client.cache && Date.now();
31
+ if (client.cache) ingestInCache(data, { cachedAt });
32
+
33
+ const { polls } = data;
34
+
35
+ return {
36
+ data: polls.find(poll => poll.pollId === pollId)!,
37
+ cachedAt,
38
+ };
39
+ };
@@ -0,0 +1,43 @@
1
+ import { getActiveClient } from '~/client/api';
2
+
3
+ import { ingestInCache } from '~/cache/api/ingestInCache';
4
+
5
+ /**
6
+ * ```js
7
+ * import { createPoll } from '@amityco/ts-sdk'
8
+ * const created = await createPoll({
9
+ * question: 'question',
10
+ * answers: [
11
+ * { dataType: 'text', data: 'answer1' },
12
+ * { dataType: 'text', data: 'answer2' },
13
+ * ],
14
+ * closedIn: 1649136484
15
+ * }))
16
+ * ```
17
+ *
18
+ * Creates an {@link Amity.Poll}
19
+ *
20
+ * @param bundle The data necessary to create a new {@link Amity.Poll}
21
+ * @returns The newly created {@link Amity.Poll}
22
+ *
23
+ * @category Poll API
24
+ * @async
25
+ */
26
+ export const createPoll = async (
27
+ bundle: Pick<Amity.Poll, 'question' | 'answers' | 'answerType' | 'closedIn'>,
28
+ ): Promise<Amity.Cached<Amity.Poll>> => {
29
+ const client = getActiveClient();
30
+ client.log('post/createPoll', bundle);
31
+
32
+ const { data } = await client.http.post<Amity.PollPayload>('/api/v3/polls', bundle);
33
+
34
+ const cachedAt = client.cache && Date.now();
35
+ if (client.cache) ingestInCache(data, { cachedAt });
36
+
37
+ const { polls } = data;
38
+
39
+ return {
40
+ data: polls[0],
41
+ cachedAt,
42
+ };
43
+ };
@@ -0,0 +1,36 @@
1
+ import { getActiveClient } from '~/client/api';
2
+
3
+ import { upsertInCache } from '~/cache/api';
4
+
5
+ import { getPoll } from './getPoll';
6
+
7
+ /**
8
+ * ```js
9
+ * import { deletePoll } from '@amityco/ts-sdk'
10
+ * const success = await deletePoll(pollId)
11
+ * ```
12
+ *
13
+ * Deletes a {@link Amity.Poll}
14
+ *
15
+ * @param pollId The {@link Amity.Poll} ID to delete
16
+ * @return A success boolean if the {@link Amity.Poll} was deleted
17
+ *
18
+ * @category Poll API
19
+ * @async
20
+ */
21
+ export const deletePoll = async (pollId: Amity.Poll['pollId']): Promise<boolean> => {
22
+ const client = getActiveClient();
23
+ client.log('poll/deletePoll', pollId);
24
+
25
+ const poll = await getPoll(pollId);
26
+
27
+ // API-FIX: it returns { success: boolean } but seems it should be Amity.Response<{ success: boolean }
28
+ const { data } = await client.http.delete<{ success: boolean }>(`/api/v3/polls/${pollId}`);
29
+
30
+ const { success } = data; // unwrapPayload(data)
31
+ const deleted = { ...poll.data, isDeleted: true };
32
+
33
+ upsertInCache(['poll', 'get', pollId], deleted);
34
+
35
+ return success;
36
+ };
@@ -0,0 +1,64 @@
1
+ import { getActiveClient } from '~/client/api';
2
+
3
+ import { pullFromCache } from '~/cache/api';
4
+ import { ingestInCache } from '~/cache/api/ingestInCache';
5
+
6
+ /**
7
+ * ```js
8
+ * import { getPoll } from '@amityco/ts-sdk'
9
+ * const poll = await getPoll('foobar')
10
+ * ```
11
+ *
12
+ * Fetches a {@link Amity.Poll} object
13
+ *
14
+ * @param pollId the ID of the {@link Amity.Poll} to fetch
15
+ * @returns the associated {@link Amity.Poll} object
16
+ *
17
+ * @category Poll API
18
+ * @async
19
+ */
20
+ export const getPoll = async (pollId: Amity.Poll['pollId']): Promise<Amity.Cached<Amity.Poll>> => {
21
+ const client = getActiveClient();
22
+ client.log('poll/getPoll', pollId);
23
+
24
+ const { data } = await client.http.get<Amity.PollPayload>(`/api/v3/polls/${pollId}`);
25
+
26
+ const cachedAt = client.cache && Date.now();
27
+ if (client.cache) ingestInCache(data, { cachedAt });
28
+
29
+ const { polls } = data;
30
+
31
+ return {
32
+ data: polls.find(poll => poll.pollId === pollId)!,
33
+ cachedAt,
34
+ };
35
+ };
36
+
37
+ /**
38
+ * ```js
39
+ * import { getPoll } from '@amityco/ts-sdk'
40
+ * const poll = getPoll.locally('foobar')
41
+ * ```
42
+ *
43
+ * Fetches a {@link Amity.Poll} object
44
+ *
45
+ * @param pollId the ID of the {@link Amity.Poll} to fetch
46
+ * @returns the associated {@link Amity.Poll} object
47
+ *
48
+ * @category Poll API
49
+ */
50
+ getPoll.locally = (pollId: Amity.Poll['pollId']): Amity.Cached<Amity.Poll> | undefined => {
51
+ const client = getActiveClient();
52
+ client.log('poll/getPoll', pollId);
53
+
54
+ if (!client.cache) return;
55
+
56
+ const cached = pullFromCache<Amity.Poll>(['poll', 'get', pollId]);
57
+
58
+ if (!cached) return;
59
+
60
+ return {
61
+ data: cached.data,
62
+ cachedAt: cached.cachedAt,
63
+ };
64
+ };
@@ -0,0 +1,7 @@
1
+ export * from './getPoll';
2
+
3
+ export * from './createPoll';
4
+ export * from './closePoll';
5
+ export * from './deletePoll';
6
+
7
+ export * from './votePoll';
@@ -0,0 +1,41 @@
1
+ import { getActiveClient } from '~/client/api';
2
+
3
+ import { ingestInCache } from '~/cache/api/ingestInCache';
4
+
5
+ /**
6
+ * ```js
7
+ * import { votePoll } from '@amityco/ts-sdk'
8
+ * const voted = await votePoll(pollId)
9
+ * ```
10
+ *
11
+ * Votes for an {@link Amity.Poll}
12
+ *
13
+ * @param pollId The ID of the {@link Amity.Poll} to vote
14
+ * @param answerIds The IDs of the {@link Amity.Poll} answers to vote {@link Amity.Poll}
15
+ * @returns the updated {@link Amity.Poll} object
16
+ *
17
+ * @category Poll API
18
+ * @async
19
+ */
20
+ export const votePoll = async (
21
+ pollId: Amity.Poll['pollId'],
22
+ answerIds: string[],
23
+ ): Promise<Amity.Cached<Amity.Poll>> => {
24
+ const client = getActiveClient();
25
+ client.log('user/votePoll', pollId);
26
+
27
+ const { data } = await client.http.post<Amity.PollPayload>(
28
+ `/api/v3/polls/${encodeURIComponent(pollId)}/votes`,
29
+ { pollId, answerIds },
30
+ );
31
+
32
+ const cachedAt = client.cache && Date.now();
33
+ if (client.cache) ingestInCache(data, { cachedAt });
34
+
35
+ const { polls } = data;
36
+
37
+ return {
38
+ data: polls.find(poll => poll.pollId === pollId)!,
39
+ cachedAt,
40
+ };
41
+ };
package/src/utils/env.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  /* eslint-disable no-undef */
2
- const theGlobal = globalThis || global || window;
2
+ const theGlobal =
3
+ // eslint-disable-next-line no-nested-ternary
4
+ typeof globalThis === 'object' ? globalThis : typeof global === 'object' ? global : window;
3
5
 
4
6
  export const { process: safeProcess = {} }: { process: Partial<NodeJS.Process> } = theGlobal;
5
7