@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.
- package/dist/@types/core/model.d.ts +2 -0
- package/dist/@types/core/model.d.ts.map +1 -1
- package/dist/@types/core/payload.d.ts +5 -0
- package/dist/@types/core/payload.d.ts.map +1 -1
- package/dist/@types/domains/poll.d.ts +27 -0
- package/dist/@types/domains/poll.d.ts.map +1 -0
- package/dist/@types/domains/post.d.ts +1 -1
- package/dist/@types/domains/post.d.ts.map +1 -1
- package/dist/@types/index.d.ts +1 -0
- package/dist/@types/index.d.ts.map +1 -1
- package/dist/channel/api/deleteChannel.d.ts.map +1 -1
- package/dist/core/model/idResolvers.d.ts.map +1 -1
- package/dist/core/model/identifyModel.d.ts.map +1 -1
- package/dist/core/model/index.d.ts.map +1 -1
- package/dist/core/subscription.d.ts.map +1 -1
- package/dist/file/api/createFile.d.ts +1 -1
- package/dist/file/api/createFile.d.ts.map +1 -1
- package/dist/index.cjs.js +187 -9
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.esm.js +183 -10
- package/dist/index.umd.js +5 -5
- package/dist/poll/api/closePoll.d.ts +16 -0
- package/dist/poll/api/closePoll.d.ts.map +1 -0
- package/dist/poll/api/createPoll.d.ts +23 -0
- package/dist/poll/api/createPoll.d.ts.map +1 -0
- package/dist/poll/api/deletePoll.d.ts +16 -0
- package/dist/poll/api/deletePoll.d.ts.map +1 -0
- package/dist/poll/api/getPoll.d.ts +32 -0
- package/dist/poll/api/getPoll.d.ts.map +1 -0
- package/dist/poll/api/index.d.ts +6 -0
- package/dist/poll/api/index.d.ts.map +1 -0
- package/dist/poll/api/votePoll.d.ts +17 -0
- package/dist/poll/api/votePoll.d.ts.map +1 -0
- package/dist/utils/env.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/@types/core/model.ts +2 -0
- package/src/@types/core/payload.ts +6 -0
- package/src/@types/domains/poll.ts +32 -0
- package/src/@types/domains/post.ts +1 -1
- package/src/@types/index.ts +2 -0
- package/src/channel/api/deleteChannel.ts +0 -1
- package/src/core/model/idResolvers.ts +1 -0
- package/src/core/model/identifyModel.ts +1 -0
- package/src/core/model/index.ts +1 -0
- package/src/core/subscription.ts +1 -3
- package/src/file/api/createFile.ts +5 -5
- package/src/index.ts +2 -0
- package/src/poll/api/closePoll.ts +39 -0
- package/src/poll/api/createPoll.ts +43 -0
- package/src/poll/api/deletePoll.ts +36 -0
- package/src/poll/api/getPoll.ts +64 -0
- package/src/poll/api/index.ts +7 -0
- package/src/poll/api/votePoll.ts +41 -0
- 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 @@
|
|
|
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"}
|
package/dist/utils/env.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"env.d.ts","sourceRoot":"","sources":["../../src/utils/env.ts"],"names":[],"mappings":";
|
|
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
package/src/@types/core/model.ts
CHANGED
|
@@ -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;
|
package/src/@types/index.ts
CHANGED
|
@@ -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
|
);
|
|
@@ -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'],
|
package/src/core/model/index.ts
CHANGED
package/src/core/subscription.ts
CHANGED
|
@@ -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('
|
|
28
|
-
throw new Error('The formData object must have a `
|
|
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/
|
|
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
|
|
49
|
+
data,
|
|
50
50
|
cachedAt,
|
|
51
51
|
};
|
|
52
52
|
};
|
package/src/index.ts
CHANGED
|
@@ -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,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 =
|
|
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
|
|