@amityco/ts-sdk 0.0.1-beta.31 → 0.0.1-beta.32

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 (74) hide show
  1. package/dist/cache/api/backupCache.d.ts.map +1 -1
  2. package/dist/cache/api/tests/pushToCache.test.d.ts +2 -0
  3. package/dist/cache/api/tests/pushToCache.test.d.ts.map +1 -0
  4. package/dist/channel/api/addChannelMembers.d.ts.map +1 -1
  5. package/dist/channel/api/getChannel.d.ts.map +1 -1
  6. package/dist/channel/api/tests/addChannelMembers.test.d.ts +2 -0
  7. package/dist/channel/api/tests/addChannelMembers.test.d.ts.map +1 -0
  8. package/dist/channel/api/tests/getChannel.test.d.ts +2 -0
  9. package/dist/channel/api/tests/getChannel.test.d.ts.map +1 -0
  10. package/dist/channel/events/onChannelMemberAdded.d.ts.map +1 -1
  11. package/dist/core/events.d.ts +1 -1
  12. package/dist/core/events.d.ts.map +1 -1
  13. package/dist/core/transports/http.d.ts +5 -0
  14. package/dist/core/transports/http.d.ts.map +1 -1
  15. package/dist/core/transports/test/http.test.d.ts +2 -0
  16. package/dist/core/transports/test/http.test.d.ts.map +1 -0
  17. package/dist/index.cjs.js +35 -45
  18. package/dist/index.esm.js +34 -45
  19. package/dist/index.umd.js +4 -4
  20. package/dist/message/api/deleteMessage.d.ts.map +1 -1
  21. package/dist/message/api/tests/deleteMessage.test.d.ts +2 -0
  22. package/dist/message/api/tests/deleteMessage.test.d.ts.map +1 -0
  23. package/dist/message/{tests/api → api/tests}/getMessage.test.d.ts +0 -0
  24. package/dist/message/api/tests/getMessage.test.d.ts.map +1 -0
  25. package/dist/message/api/tests/getMessages.test.d.ts +2 -0
  26. package/dist/message/api/tests/getMessages.test.d.ts.map +1 -0
  27. package/dist/message/api/tests/queryMessages.test.d.ts +2 -0
  28. package/dist/message/api/tests/queryMessages.test.d.ts.map +1 -0
  29. package/dist/message/api/tests/updateMessage.test.d.ts +2 -0
  30. package/dist/message/api/tests/updateMessage.test.d.ts.map +1 -0
  31. package/dist/reaction/api/addReaction.d.ts.map +1 -1
  32. package/dist/reaction/api/removeReaction.d.ts.map +1 -1
  33. package/dist/reaction/api/tests/addReaction.test.d.ts +2 -0
  34. package/dist/reaction/api/tests/addReaction.test.d.ts.map +1 -0
  35. package/dist/reaction/api/tests/removeReaction.test.d.ts +2 -0
  36. package/dist/reaction/api/tests/removeReaction.test.d.ts.map +1 -0
  37. package/dist/user/api/getUser.d.ts.map +1 -1
  38. package/dist/user/api/queryUsers.d.ts.map +1 -1
  39. package/dist/user/api/tests/queryUsers.test.d.ts +2 -0
  40. package/dist/user/api/tests/queryUsers.test.d.ts.map +1 -0
  41. package/dist/utils/tests/dummy/post.d.ts +12 -1
  42. package/dist/utils/tests/dummy/post.d.ts.map +1 -1
  43. package/package.json +2 -1
  44. package/src/cache/api/backupCache.ts +0 -1
  45. package/src/cache/api/tests/pushToCache.test.ts +57 -0
  46. package/src/channel/api/addChannelMembers.ts +9 -1
  47. package/src/channel/api/getChannel.ts +20 -5
  48. package/src/channel/api/tests/addChannelMembers.test.ts +95 -0
  49. package/src/channel/api/tests/getChannel.test.ts +108 -0
  50. package/src/channel/api/tests/queryChannels.test.ts +20 -0
  51. package/src/channel/events/onChannelMemberAdded.ts +1 -1
  52. package/src/channel/observers/tests/liveChannels.test.ts +0 -1
  53. package/src/core/events.ts +1 -1
  54. package/src/core/transports/http.ts +3 -17
  55. package/src/core/transports/test/http.test.ts +83 -0
  56. package/src/message/api/deleteMessage.ts +2 -3
  57. package/src/message/api/tests/createMessage.test.ts +74 -51
  58. package/src/message/api/tests/deleteMessage.test.ts +110 -0
  59. package/src/message/api/tests/getMessage.test.ts +121 -0
  60. package/src/message/api/tests/getMessages.test.ts +108 -0
  61. package/src/message/api/tests/queryMessages.test.ts +115 -0
  62. package/src/message/api/tests/updateMessage.test.ts +100 -0
  63. package/src/post/tests/api/queryPosts.test.ts +117 -18
  64. package/src/reaction/api/addReaction.ts +1 -4
  65. package/src/reaction/api/removeReaction.ts +1 -4
  66. package/src/reaction/api/tests/addReaction.test.ts +37 -0
  67. package/src/reaction/api/tests/removeReaction.test.ts +43 -0
  68. package/src/user/api/getUser.ts +1 -2
  69. package/src/user/api/queryUsers.ts +24 -5
  70. package/src/user/api/tests/queryUsers.test.ts +124 -0
  71. package/src/utils/tests/dummy/post.ts +26 -3
  72. package/src/utils/tests/dummy/user.ts +1 -1
  73. package/dist/message/tests/api/getMessage.test.d.ts.map +0 -1
  74. package/src/message/tests/api/getMessage.test.ts +0 -96
@@ -1 +1 @@
1
- {"version":3,"file":"deleteMessage.d.ts","sourceRoot":"","sources":["../../../src/message/api/deleteMessage.ts"],"names":[],"mappings":"AAOA;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,aAAa;gBACb,MAAM,OAAO,CAAC,WAAW,CAAC,GACpC,QAAQ,MAAM,OAAO,CAAC;IAgBzB;;;;;;;;;;;;OAYG;8BAEU,MAAM,OAAO,CAAC,WAAW,CAAC,GACpC,MAAM,MAAM,CAAC,MAAM,OAAO,CAAC,GAAG,SAAS;CAjBzC,CAAC"}
1
+ {"version":3,"file":"deleteMessage.d.ts","sourceRoot":"","sources":["../../../src/message/api/deleteMessage.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,aAAa;gBACb,MAAM,OAAO,CAAC,WAAW,CAAC,GACpC,QAAQ,MAAM,OAAO,CAAC;IAgBzB;;;;;;;;;;;;OAYG;8BAEU,MAAM,OAAO,CAAC,WAAW,CAAC,GACpC,MAAM,MAAM,CAAC,MAAM,OAAO,CAAC,GAAG,SAAS;CAjBzC,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=deleteMessage.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deleteMessage.test.d.ts","sourceRoot":"","sources":["../../../../src/message/api/tests/deleteMessage.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getMessage.test.d.ts","sourceRoot":"","sources":["../../../../src/message/api/tests/getMessage.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=getMessages.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getMessages.test.d.ts","sourceRoot":"","sources":["../../../../src/message/api/tests/getMessages.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=queryMessages.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"queryMessages.test.d.ts","sourceRoot":"","sources":["../../../../src/message/api/tests/queryMessages.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=updateMessage.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"updateMessage.test.d.ts","sourceRoot":"","sources":["../../../../src/message/api/tests/updateMessage.test.ts"],"names":[],"mappings":""}
@@ -1 +1 @@
1
- {"version":3,"file":"addReaction.d.ts","sourceRoot":"","sources":["../../../src/reaction/api/addReaction.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;;;;;;KAeK;AACL,eAAO,MAAM,WAAW;oBACP,MAAM,QAAQ,CAAC,eAAe,CAAC,eACjC,MAAM,QAAQ,CAAC,aAAa,CAAC,gBAC5B,MAAM,QAAQ,CAAC,cAAc,CAAC,GAC3C,QAAQ,OAAO,CAAC;IAoBnB;;;;;;;;;;;;;;SAcK;kCAEY,MAAM,aAAa,eACrB,MAAM,QAAQ,CAAC,aAAa,CAAC,gBAC5B,MAAM,QAAQ,CAAC,cAAc,CAAC,GAC3C,OAAO,GAAG,SAAS;CArBrB,CAAC"}
1
+ {"version":3,"file":"addReaction.d.ts","sourceRoot":"","sources":["../../../src/reaction/api/addReaction.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;;;;;;KAeK;AACL,eAAO,MAAM,WAAW;oBACP,MAAM,QAAQ,CAAC,eAAe,CAAC,eACjC,MAAM,QAAQ,CAAC,aAAa,CAAC,gBAC5B,MAAM,QAAQ,CAAC,cAAc,CAAC,GAC3C,QAAQ,OAAO,CAAC;IAiBnB;;;;;;;;;;;;;;SAcK;kCAEY,MAAM,aAAa,eACrB,MAAM,QAAQ,CAAC,aAAa,CAAC,gBAC5B,MAAM,QAAQ,CAAC,cAAc,CAAC,GAC3C,OAAO,GAAG,SAAS;CArBrB,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"removeReaction.d.ts","sourceRoot":"","sources":["../../../src/reaction/api/removeReaction.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;;;;;;KAeK;AACL,eAAO,MAAM,cAAc;oBACV,MAAM,QAAQ,CAAC,eAAe,CAAC,eACjC,MAAM,QAAQ,CAAC,aAAa,CAAC,gBAC5B,MAAM,QAAQ,CAAC,cAAc,CAAC,GAC3C,QAAQ,OAAO,CAAC;IAkBnB;;;;;;;;;;;;;;SAcK;kCAEY,MAAM,aAAa,eACrB,MAAM,QAAQ,CAAC,aAAa,CAAC,gBAC5B,MAAM,QAAQ,CAAC,cAAc,CAAC,GAC3C,OAAO,GAAG,SAAS;CArBrB,CAAC"}
1
+ {"version":3,"file":"removeReaction.d.ts","sourceRoot":"","sources":["../../../src/reaction/api/removeReaction.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;;;;;;KAeK;AACL,eAAO,MAAM,cAAc;oBACV,MAAM,QAAQ,CAAC,eAAe,CAAC,eACjC,MAAM,QAAQ,CAAC,aAAa,CAAC,gBAC5B,MAAM,QAAQ,CAAC,cAAc,CAAC,GAC3C,QAAQ,OAAO,CAAC;IAenB;;;;;;;;;;;;;;SAcK;kCAEY,MAAM,aAAa,eACrB,MAAM,QAAQ,CAAC,aAAa,CAAC,gBAC5B,MAAM,QAAQ,CAAC,cAAc,CAAC,GAC3C,OAAO,GAAG,SAAS;CArBrB,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=addReaction.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"addReaction.test.d.ts","sourceRoot":"","sources":["../../../../src/reaction/api/tests/addReaction.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=removeReaction.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"removeReaction.test.d.ts","sourceRoot":"","sources":["../../../../src/reaction/api/tests/removeReaction.test.ts"],"names":[],"mappings":""}
@@ -1 +1 @@
1
- {"version":3,"file":"getUser.d.ts","sourceRoot":"","sources":["../../../src/user/api/getUser.ts"],"names":[],"mappings":"AAUA;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,OAAO;aACV,MAAM,IAAI,CAAC,QAAQ,CAAC,qBAE3B,QAAQ,MAAM,MAAM,CAAC,MAAM,IAAI,CAAC,CAAC;IAwCpC;;;;;;;;;;;;OAYG;oBACwB,MAAM,IAAI,CAAC,QAAQ,CAAC,GAAG,MAAM,MAAM,CAAC,MAAM,IAAI,CAAC,GAAG,SAAS;CAfrF,CAAC"}
1
+ {"version":3,"file":"getUser.d.ts","sourceRoot":"","sources":["../../../src/user/api/getUser.ts"],"names":[],"mappings":"AAUA;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,OAAO;aACV,MAAM,IAAI,CAAC,QAAQ,CAAC,qBAE3B,QAAQ,MAAM,MAAM,CAAC,MAAM,IAAI,CAAC,CAAC;IAuCpC;;;;;;;;;;;;OAYG;oBACwB,MAAM,IAAI,CAAC,QAAQ,CAAC,GAAG,MAAM,MAAM,CAAC,MAAM,IAAI,CAAC,GAAG,SAAS;CAfrF,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"queryUsers.d.ts","sourceRoot":"","sources":["../../../src/user/api/queryUsers.ts"],"names":[],"mappings":"AASA;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,UAAU;aACd;QACL,WAAW,CAAC,EAAE,MAAM,IAAI,CAAC,aAAa,CAAC,CAAC;QACxC,MAAM,CAAC,EAAE,KAAK,GAAG,SAAS,CAAC;QAC3B,MAAM,CAAC,EAAE,aAAa,GAAG,cAAc,GAAG,aAAa,CAAC;QACxD,IAAI,CAAC,EAAE,MAAM,IAAI,CAAC;KACnB,GACA,QAAQ,MAAM,MAAM,CAAC,MAAM,KAAK,CAAC,MAAM,IAAI,CAAC,CAAC,CAAC;IAwCjD;;;;;;;;;;;;;OAaG;oBAEM,WAAW,iBAAiB,CAAC,CAAC,CAAC,CAAC,GACtC,MAAM,MAAM,CAAC,MAAM,KAAK,CAAC,MAAM,IAAI,CAAC,CAAC,GAAG,SAAS;CAlBnD,CAAC"}
1
+ {"version":3,"file":"queryUsers.d.ts","sourceRoot":"","sources":["../../../src/user/api/queryUsers.ts"],"names":[],"mappings":"AASA;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,UAAU;aACd;QACL,WAAW,CAAC,EAAE,MAAM,IAAI,CAAC,aAAa,CAAC,CAAC;QACxC,MAAM,CAAC,EAAE,KAAK,GAAG,SAAS,CAAC;QAC3B,MAAM,CAAC,EAAE,aAAa,GAAG,cAAc,GAAG,aAAa,CAAC;QACxD,IAAI,CAAC,EAAE,MAAM,IAAI,CAAC;KACnB,GACA,QAAQ,MAAM,MAAM,CAAC,MAAM,KAAK,CAAC,MAAM,IAAI,CAAC,CAAC,CAAC;IAiDjD;;;;;;;;;;;;;OAaG;oBAEM,WAAW,iBAAiB,CAAC,CAAC,CAAC,CAAC,GACtC,MAAM,MAAM,CAAC,MAAM,KAAK,CAAC,MAAM,IAAI,CAAC,CAAC,GAAG,SAAS;CAlBnD,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=queryUsers.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"queryUsers.test.d.ts","sourceRoot":"","sources":["../../../../src/user/api/tests/queryUsers.test.ts"],"names":[],"mappings":""}
@@ -1,4 +1,6 @@
1
- export declare const post1: Amity.Post;
1
+ export declare const post11: Amity.Post;
2
+ export declare const post12: Amity.Post;
3
+ export declare const post13: Amity.Post;
2
4
  export declare const posts: {
3
5
  targetId: string;
4
6
  targetType: Amity.PostTargetType;
@@ -6,4 +8,13 @@ export declare const posts: {
6
8
  page2: string[];
7
9
  page3: string[];
8
10
  };
11
+ export declare const postQueryResponse: {
12
+ data: {
13
+ posts: Amity.Post<any>[];
14
+ files: never[];
15
+ paging: {
16
+ next: string;
17
+ };
18
+ };
19
+ };
9
20
  //# sourceMappingURL=post.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"post.d.ts","sourceRoot":"","sources":["../../../../src/utils/tests/dummy/post.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,KAAK,EAAE,KAAK,CAAC,IAoBzB,CAAC;AAEF,eAAO,MAAM,KAAK;;;;;;CAMjB,CAAC"}
1
+ {"version":3,"file":"post.d.ts","sourceRoot":"","sources":["../../../../src/utils/tests/dummy/post.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,MAAM,EAAE,KAAK,CAAC,IAoB1B,CAAC;AAEF,eAAO,MAAM,MAAM,EAAE,KAAK,CAAC,IAG1B,CAAC;AAEF,eAAO,MAAM,MAAM,EAAE,KAAK,CAAC,IAG1B,CAAC;AAEF,eAAO,MAAM,KAAK;;;;;;CAMjB,CAAC;AAIF,eAAO,MAAM,iBAAiB;;;;;;;;CAQ7B,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amityco/ts-sdk",
3
- "version": "0.0.1-beta.31",
3
+ "version": "0.0.1-beta.32",
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",
@@ -59,6 +59,7 @@
59
59
  "js-base64": "^3.7.2",
60
60
  "mitt": "^3.0.0",
61
61
  "mqtt": "^4.3.7",
62
+ "qs": "^6.11.0",
62
63
  "react-native-uuid": "^2.0.1",
63
64
  "socket.io-client": "2.2.0",
64
65
  "ts-jest": "^28.0.3"
@@ -1,4 +1,3 @@
1
- /* eslint-disable @typescript-eslint/no-unused-vars */
2
1
  import AsyncStorage from '@react-native-async-storage/async-storage';
3
2
 
4
3
  import { getActiveClient } from '~/client/api/activeClient';
@@ -0,0 +1,57 @@
1
+ import { getActiveClient } from '~/client/api';
2
+ import { connectClient, disconnectClient, user11 } from '~/utils/tests';
3
+
4
+ import { pushToCache, enableCache, disableCache, pullFromCache } from '..';
5
+ import { encodeKey } from '../../utils';
6
+
7
+ describe('pushToCache', () => {
8
+ beforeAll(connectClient);
9
+ afterAll(disconnectClient);
10
+
11
+ describe('- cache enabled', () => {
12
+ beforeEach(enableCache);
13
+ afterEach(disableCache);
14
+
15
+ const cacheKey = ['user', 'get', user11.userId];
16
+
17
+ test('it should return true if data added to cache', () => {
18
+ const recieved = pushToCache(cacheKey, user11);
19
+
20
+ expect(recieved).toBe(true);
21
+ });
22
+
23
+ test('it should add data to cache', () => {
24
+ const returned = pushToCache(cacheKey, user11);
25
+ const recieved = pullFromCache(cacheKey)?.data;
26
+
27
+ expect(returned).toBe(true);
28
+ expect(recieved).toBe(user11);
29
+ });
30
+
31
+ test('it should persist data offline', () => {
32
+ const { cache } = getActiveClient();
33
+ const encodedKey = encodeKey(cacheKey);
34
+ const options = { offline: true, cachedAt: Date.now() };
35
+
36
+ const addedUser = pushToCache(cacheKey, user11, options);
37
+
38
+ const expected = {
39
+ key: cacheKey,
40
+ data: user11,
41
+ ...options,
42
+ };
43
+ const recieved = cache?.data[encodedKey];
44
+
45
+ expect(addedUser).toBe(true);
46
+ expect(recieved).toStrictEqual(expected);
47
+ });
48
+ });
49
+
50
+ describe('- no cache', () => {
51
+ test('it should return false if no cache', () => {
52
+ const recieved = pushToCache(['model', 'key', 'modleId'], {});
53
+
54
+ expect(recieved).toBe(false);
55
+ });
56
+ });
57
+ });
@@ -36,7 +36,15 @@ export const addChannelMembers = async (
36
36
 
37
37
  if (client.cache) ingestInCache(data);
38
38
 
39
- fireEvent('v3.channel.didAddUsers', data);
39
+ /*
40
+ * should fire event for each user that was added
41
+ */
42
+ userIds.forEach(id =>
43
+ fireEvent('v3.channel.didAddUsers', {
44
+ ...data,
45
+ channelUsers: data.channelUsers.filter(u => u.userId === id),
46
+ }),
47
+ );
40
48
 
41
49
  const { channelUsers } = data;
42
50
  return !!channelUsers.find(
@@ -3,6 +3,10 @@ import { getActiveClient } from '~/client/api';
3
3
  import { pullFromCache } from '~/cache/api';
4
4
  import { ingestInCache } from '~/cache/api/ingestInCache';
5
5
 
6
+ import { isInTombstone } from '~/cache/api/isInTombstone';
7
+ import { checkIfShouldGoesToTombstone } from '~/cache/utils';
8
+ import { pushToTombstone } from '~/cache/api/pushToTombstone';
9
+
6
10
  import { prepareChannelPayload } from '../utils';
7
11
 
8
12
  /**
@@ -25,12 +29,23 @@ export const getChannel = async (
25
29
  const client = getActiveClient();
26
30
  client.log('channel/getChannel', channelId);
27
31
 
28
- // API-FIX: endpoint should not be /list, parameters should be querystring.
29
- const { data: payload } = await client.http.get<Amity.ChannelPayload>(
30
- `/api/v3/channels/${encodeURIComponent(channelId)}`,
31
- );
32
+ isInTombstone('channel', channelId);
33
+
34
+ let data: Amity.ChannelPayload<any, Amity.Channel>;
35
+
36
+ try {
37
+ const { data: payload } = await client.http.get<Amity.ChannelPayload>(
38
+ `/api/v3/channels/${encodeURIComponent(channelId)}`,
39
+ );
40
+
41
+ data = prepareChannelPayload(payload);
42
+ } catch (error) {
43
+ if (checkIfShouldGoesToTombstone((error as Amity.ErrorResponse)?.code)) {
44
+ pushToTombstone('channel', channelId);
45
+ }
32
46
 
33
- const data = prepareChannelPayload(payload);
47
+ throw error;
48
+ }
34
49
 
35
50
  const cachedAt = client.cache && Date.now();
36
51
  if (client.cache) ingestInCache(data, { cachedAt });
@@ -0,0 +1,95 @@
1
+ import { getResolver } from '~/core/model';
2
+ import { pullFromCache, enableCache, disableCache } from '~/cache/api';
3
+
4
+ import {
5
+ client,
6
+ pause,
7
+ channelUserQueryResponse,
8
+ channelUser1,
9
+ channelUser3,
10
+ channel11,
11
+ } from '~/utils/tests';
12
+
13
+ import { onChannelMemberAdded } from '~/channel/events';
14
+ import { addChannelMembers } from '../addChannelMembers';
15
+
16
+ describe('addChannelMembers', () => {
17
+ const { channelId } = channelUserQueryResponse.data.channels[0];
18
+ const { userId } = channelUser1;
19
+
20
+ test('it should add channel members in channel', async () => {
21
+ client.http.post = jest.fn().mockResolvedValueOnce(channelUserQueryResponse);
22
+
23
+ const recieved = await addChannelMembers(channelId, [userId]);
24
+
25
+ expect(recieved).toBe(true);
26
+ });
27
+
28
+ test('it should update cache with channel members', async () => {
29
+ enableCache();
30
+
31
+ client.http.post = jest.fn().mockResolvedValueOnce(channelUserQueryResponse);
32
+
33
+ const channelUserCacheKey = getResolver('channelUsers')({
34
+ channelId,
35
+ userId,
36
+ });
37
+
38
+ const recieved = await addChannelMembers(channelId, [userId]);
39
+ const channelUser = pullFromCache<Amity.Membership<'channel'>>([
40
+ 'channelUsers',
41
+ 'get',
42
+ channelUserCacheKey,
43
+ ])?.data;
44
+
45
+ expect(recieved).toBe(true);
46
+ expect(channelUser).toBeDefined();
47
+ expect(channelUser).toBe(channelUser1);
48
+
49
+ disableCache();
50
+ });
51
+
52
+ test('it should fire event when new members added', async () => {
53
+ expect.assertions(1);
54
+
55
+ client.http.post = jest.fn().mockResolvedValueOnce(channelUserQueryResponse);
56
+ let dispose;
57
+
58
+ const callback = new Promise(resolve => {
59
+ dispose = onChannelMemberAdded(resolve);
60
+ }).finally(dispose);
61
+
62
+ await addChannelMembers(channelId, [userId]);
63
+
64
+ return expect(callback).resolves.not.toThrow();
65
+ });
66
+
67
+ test('it should fire event with all new members', async () => {
68
+ client.http.post = jest.fn().mockResolvedValueOnce(channelUserQueryResponse);
69
+
70
+ const callback = jest.fn();
71
+ const { userId: userId2 } = channelUser3;
72
+ const unsub = onChannelMemberAdded(callback);
73
+
74
+ const recieved = await addChannelMembers(channelId, [userId, userId2]);
75
+
76
+ expect(recieved).toBe(true);
77
+
78
+ await pause();
79
+
80
+ /*
81
+ * Assertion below is required to check if the event handler is called with the right
82
+ * params. It's not enough to check if the handler does not throw
83
+ */
84
+ expect(callback).toHaveBeenNthCalledWith(1, { ...channel11 }, { ...channelUser1 });
85
+ expect(callback).toHaveBeenNthCalledWith(2, { ...channel11 }, { ...channelUser3 });
86
+
87
+ unsub();
88
+ });
89
+
90
+ test('it should return an error', async () => {
91
+ client.http.post = jest.fn().mockRejectedValueOnce(new Error('error'));
92
+
93
+ await expect(addChannelMembers('channelId', [])).rejects.toThrow('error');
94
+ });
95
+ });
@@ -0,0 +1,108 @@
1
+ import { ASCApiError } from '~/core/errors';
2
+ import { disableCache, enableCache, pushToCache } from '~/cache/api';
3
+ import { client, channelQueryResponse } from '~/utils/tests';
4
+
5
+ import { pushToTombstone } from '~/cache/api/pushToTombstone';
6
+ import { isInTombstone } from '~/cache/api/isInTombstone';
7
+
8
+ import { getChannel } from '../getChannel';
9
+
10
+ const { channelId } = channelQueryResponse.data.channels[0];
11
+ const channel = channelQueryResponse.data.channels[0];
12
+
13
+ describe('getChannel', () => {
14
+ test('it should return a channel', async () => {
15
+ client.http.get = jest.fn().mockResolvedValueOnce(channelQueryResponse);
16
+
17
+ const expected = channel;
18
+ const { data: received } = await getChannel(channelId);
19
+
20
+ expect(received).toStrictEqual(expected);
21
+ });
22
+
23
+ test('it should update cache after fetch from server', async () => {
24
+ enableCache();
25
+ client.http.get = jest.fn().mockResolvedValue(channelQueryResponse);
26
+ await getChannel(channelId);
27
+
28
+ expect(getChannel.locally(channelId)).toBeDefined();
29
+
30
+ disableCache();
31
+ });
32
+
33
+ test('it should return an error', async () => {
34
+ client.http.get = jest.fn().mockRejectedValueOnce(new Error('error'));
35
+
36
+ await expect(getChannel(channelId)).rejects.toThrow('error');
37
+ });
38
+
39
+ test('it should return an error if channel in cache but api throws not found', async () => {
40
+ enableCache();
41
+ pushToCache(['channel', 'get', channelId], channel, { cachedAt: Date.now() });
42
+
43
+ client.http.get = jest
44
+ .fn()
45
+ .mockRejectedValueOnce(
46
+ new ASCApiError('not found!', Amity.ServerError.ITEM_NOT_FOUND, Amity.ErrorLevel.ERROR),
47
+ );
48
+
49
+ await expect(getChannel(channelId)).rejects.toThrow();
50
+ expect(getChannel.locally(channelId)).toBeUndefined();
51
+
52
+ disableCache();
53
+ });
54
+ });
55
+
56
+ describe('getChannel locally', () => {
57
+ test('it should return data from cache', () => {
58
+ enableCache();
59
+
60
+ const cachedAt = Date.now();
61
+
62
+ pushToCache(['channel', 'get', channelId], channel, { cachedAt });
63
+
64
+ const data = getChannel.locally(channelId);
65
+
66
+ expect(data).toBeDefined();
67
+ expect(data?.data).toEqual(channel);
68
+ expect(data?.cachedAt).toEqual(cachedAt);
69
+
70
+ disableCache();
71
+ });
72
+
73
+ test('should return undefined if channel not in cache', () => {
74
+ expect(getChannel.locally('non-existent-channel')).toBeUndefined();
75
+ });
76
+
77
+ test('should return undefined if cache not enabled', () => {
78
+ expect(getChannel.locally(channelId)).toBeUndefined();
79
+ });
80
+ });
81
+
82
+ describe('getChannel tombostone', () => {
83
+ test('it should add channel to tombostone if 404', async () => {
84
+ enableCache();
85
+
86
+ client.http.get = jest
87
+ .fn()
88
+ .mockRejectedValueOnce(
89
+ new ASCApiError('not found!', Amity.ServerError.ITEM_NOT_FOUND, Amity.ErrorLevel.ERROR),
90
+ );
91
+
92
+ await expect(getChannel(channelId)).rejects.toThrow();
93
+ expect(() => {
94
+ isInTombstone('channel', channelId);
95
+ }).toThrow();
96
+
97
+ disableCache();
98
+ });
99
+
100
+ test('it should throw error if channel not found in tombostone', async () => {
101
+ enableCache();
102
+
103
+ pushToTombstone('channel', channelId);
104
+ await expect(getChannel(channelId)).rejects.toThrow();
105
+
106
+ disableCache();
107
+ });
108
+ });
@@ -85,6 +85,26 @@ describe('queryChannels.locally', () => {
85
85
  disableCache();
86
86
  });
87
87
 
88
+ test('it should return undefined if params do not match', () => {
89
+ enableCache();
90
+
91
+ const { data } = channelQueryResponse;
92
+ const { channels: expected, paging } = data;
93
+
94
+ ingestInCache(data as Amity.ChannelPayload);
95
+
96
+ pushToCache(cacheKey, { channels: expected.map(getResolver('channel')), paging });
97
+
98
+ const { data: received } = queryChannels.locally()!;
99
+ const received2 = queryChannels.locally({ displayName: 'test' })!;
100
+
101
+ expect(received).toBeDefined();
102
+ expect(received).toEqual(expected);
103
+ expect(received2).toBeUndefined();
104
+
105
+ disableCache();
106
+ });
107
+
88
108
  test('it should return undefined if data not in cache', () => {
89
109
  enableCache();
90
110
 
@@ -22,7 +22,7 @@ import { prepareChannelPayload } from '../utils';
22
22
  */
23
23
  export const onChannelMemberAdded = (
24
24
  callback: (channel: Amity.Channel, member: Amity.Membership<'channel'>) => void,
25
- ) => {
25
+ ): Amity.Unsubscriber => {
26
26
  const client = getActiveClient();
27
27
 
28
28
  const filter = (payload: Amity.ChannelMembershipPayload) => {
@@ -2,7 +2,6 @@ import { disableCache, enableCache, pushToCache } from '~/cache/api';
2
2
  import { getResolver } from '~/core/model';
3
3
 
4
4
  import { liveChannels } from '../liveChannels';
5
- import { queryChannels } from '~/channel/api';
6
5
  import { prepareChannelPayload } from '../../utils/prepareChannelPayload';
7
6
 
8
7
  import {
@@ -117,7 +117,7 @@ export const createEventSubscriber = <T extends keyof Amity.Events>(
117
117
  export const createMqttMessageSubscriber = <T extends keyof Amity.Events>(
118
118
  client: Amity.Client,
119
119
  namespace: string,
120
- event: string,
120
+ event: T,
121
121
  fn: (params: Record<string, unknown>) => void,
122
122
  ): Amity.Unsubscriber => {
123
123
  return createEventSubscriber(client, namespace, 'message', ([topic, message]) => {
@@ -1,4 +1,5 @@
1
1
  import axios from 'axios';
2
+ import qs from 'qs';
2
3
  import HttpAgent, { HttpsAgent } from 'agentkeepalive';
3
4
 
4
5
  import { unwrapPayload } from './utils';
@@ -7,23 +8,8 @@ import { unwrapPayload } from './utils';
7
8
  * Axios param serializer (for querystring)
8
9
  * @hidden
9
10
  */
10
- const stringify = (params: Record<string, unknown>, prefix?: string): string => {
11
- return Object.entries(params)
12
- .filter(([, value]) => value !== undefined)
13
- .map(([name, value]) => {
14
- const key = prefix ? `${prefix}[${name}]` : name;
15
-
16
- if (Array.isArray(value)) {
17
- return value.map(item => `${key}[]=${item}`).join('&');
18
- }
19
- if (typeof value !== 'object') {
20
- return `${key}=${value}`;
21
- }
22
-
23
- // @ts-ignore
24
- return stringify(value, prefix ?? name);
25
- })
26
- .join('&');
11
+ export const stringify = (params: Record<string, unknown>): string => {
12
+ return qs.stringify(params, { arrayFormat: 'brackets' });
27
13
  };
28
14
 
29
15
  /**
@@ -0,0 +1,83 @@
1
+ import { createHttpTransport, stringify } from '../http';
2
+
3
+ describe('core/transports', () => {
4
+ describe('stringify', () => {
5
+ test('should serializer simple string', () => {
6
+ const params = { a: 'c' };
7
+ const url = stringify(params);
8
+ expect(url).toBe('a=c');
9
+ });
10
+
11
+ test('should serializer empty string', () => {
12
+ const params = { a: '' };
13
+ const url = stringify(params);
14
+ expect(url).toBe('a=');
15
+ });
16
+
17
+ test('should serializer special character', () => {
18
+ const params = { a: '✓', b: 'ø' };
19
+ const url = stringify(params);
20
+ expect(url).toBe('a=%E2%9C%93&b=%C3%B8');
21
+ });
22
+
23
+ test('should serializer boolean', () => {
24
+ const params = { a: true, b: false };
25
+ const url = stringify(params);
26
+ expect(url).toBe('a=true&b=false');
27
+ });
28
+
29
+ test('should serializer null ', () => {
30
+ const params = { a: null };
31
+ const url = stringify(params);
32
+ expect(url).toBe('a=');
33
+ });
34
+
35
+ test('should serializer undefined', () => {
36
+ const params = { a: undefined };
37
+ const url = stringify(params);
38
+ expect(url).toBe('');
39
+ });
40
+
41
+ test('should serializer NaN', () => {
42
+ const params = { a: NaN };
43
+ const url = stringify(params);
44
+ expect(url).toBe('a=NaN');
45
+ });
46
+
47
+ test('should serializer simple array', () => {
48
+ const params = { a: ['b', 'c', 'd'] };
49
+ const url = stringify(params);
50
+ expect(url).toBe('a%5B%5D=b&a%5B%5D=c&a%5B%5D=d');
51
+ });
52
+
53
+ test('should serializer complex array', () => {
54
+ const params = { a: ['b', { c: 'd' }] };
55
+ const url = stringify(params);
56
+ expect(url).toBe('a%5B%5D=b&a%5B%5D%5Bc%5D=d');
57
+ });
58
+
59
+ test('should serializer nested object', () => {
60
+ const params = { a: { b: 'c' } };
61
+ const url = stringify(params);
62
+ expect(url).toBe('a%5Bb%5D=c');
63
+ });
64
+
65
+ test('should serializer deep nested object', () => {
66
+ const params = { a: { b: { c: { d: { e: { f: { g: { h: { i: 'j' } } } } } } } } };
67
+ const url = stringify(params);
68
+ expect(url).toBe('a%5Bb%5D%5Bc%5D%5Bd%5D%5Be%5D%5Bf%5D%5Bg%5D%5Bh%5D%5Bi%5D=j');
69
+ });
70
+
71
+ test('should serializer empty object', () => {
72
+ const params = { a: { b: [] } };
73
+ const url = stringify(params);
74
+ expect(url).toBe('');
75
+ });
76
+ });
77
+
78
+ describe('createHttpTransport', () => {
79
+ test('should create axios instance with out error', () => {
80
+ expect(createHttpTransport('/')).not.toThrow();
81
+ });
82
+ });
83
+ });
@@ -1,10 +1,9 @@
1
1
  import { getActiveClient } from '~/client/api';
2
-
3
- import { getMessage } from './getMessage';
4
-
5
2
  import { pullFromCache, upsertInCache } from '~/cache/api';
6
3
  import { fireEvent } from '~/core/events';
7
4
 
5
+ import { getMessage } from './getMessage';
6
+
8
7
  /**
9
8
  * ```js
10
9
  * import { deleteMessage } from '@amityco/ts-sdk'