@amityco/ts-sdk 7.20.1-c9feba2c.0 → 7.21.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/dist/index.esm.js CHANGED
@@ -242,8 +242,8 @@ var AnalyticsSourceTypeEnum;
242
242
 
243
243
  function getVersion() {
244
244
  try {
245
- // the string ''v7.20.0-esm'' should be replaced by actual value by @rollup/plugin-replace
246
- return 'v7.20.0-esm';
245
+ // the string ''v7.21.0-esm'' should be replaced by actual value by @rollup/plugin-replace
246
+ return 'v7.21.0-esm';
247
247
  }
248
248
  catch (error) {
249
249
  return '__dev__';
@@ -1862,7 +1862,7 @@ var RequestCancelation;
1862
1862
  * More Info:
1863
1863
  * https://axios-http.com/docs/cancellation
1864
1864
  */
1865
- let controller = new AbortController();
1865
+ const controller = new AbortController();
1866
1866
  /**
1867
1867
  * Handle Request Cancellation
1868
1868
  *
@@ -1895,18 +1895,6 @@ const handleRequestCancelation = (cancel) => {
1895
1895
  throw new ASCError('Request Aborted', 800000 /* Amity.ClientError.UNKNOWN_ERROR */, "error" /* Amity.ErrorLevel.ERROR */);
1896
1896
  }
1897
1897
  };
1898
- // --- Visitor usage limit deduplication ---
1899
- let lastVisitorLimitEmitTime = 0;
1900
- // 2 s window — prevents duplicate emissions when multiple simultaneous requests all fail with 400323
1901
- const VISITOR_LIMIT_THROTTLE_MS = 2000;
1902
- function throttledFireVisitorUsageLimit() {
1903
- const now = Date.now();
1904
- if (now - lastVisitorLimitEmitTime < VISITOR_LIMIT_THROTTLE_MS)
1905
- return;
1906
- lastVisitorLimitEmitTime = now;
1907
- fireEvent('visitorUsageLimitReached', undefined);
1908
- }
1909
- // -----------------------------------------
1910
1898
  /**
1911
1899
  * Creates a pre-configured axios instance
1912
1900
  *
@@ -1982,20 +1970,15 @@ const createHttpTransport = (endpoint) => {
1982
1970
  });
1983
1971
  return response;
1984
1972
  }, error => {
1985
- var _a, _b, _c;
1973
+ var _a, _b;
1986
1974
  const { response } = error;
1987
1975
  // handle unauthorized request
1988
1976
  if ((response === null || response === void 0 ? void 0 : response.data.code) === 400100 /* Amity.ServerError.UNAUTHORIZED */) {
1989
1977
  fireEvent('tokenTerminated', "terminated" /* Amity.SessionStates.TERMINATED */);
1990
1978
  }
1991
- // handle visitor daily usage limit exceeded
1992
- if (((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.code) === 400323 /* Amity.ServerError.VISITOR_USAGE_LIMIT_EXCEEDED */) {
1993
- throttledFireVisitorUsageLimit();
1994
- throw new ASCError('Visitor usage limit exceeded', 400323 /* Amity.ServerError.VISITOR_USAGE_LIMIT_EXCEEDED */, "error" /* Amity.ErrorLevel.ERROR */);
1995
- }
1996
1979
  // if it's an error with status in the response payload,
1997
1980
  // then it's an expected error.
1998
- if ((_b = response === null || response === void 0 ? void 0 : response.data) === null || _b === void 0 ? void 0 : _b.status) {
1981
+ if ((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.status) {
1999
1982
  unwrapPayload(response === null || response === void 0 ? void 0 : response.data);
2000
1983
  }
2001
1984
  // as on request cancellation error is returned
@@ -2003,7 +1986,7 @@ const createHttpTransport = (endpoint) => {
2003
1986
  throw error;
2004
1987
  }
2005
1988
  // unexpected error.
2006
- throw new Error((_c = response === null || response === void 0 ? void 0 : response.data) !== null && _c !== void 0 ? _c : error);
1989
+ throw new Error((_b = response === null || response === void 0 ? void 0 : response.data) !== null && _b !== void 0 ? _b : error);
2007
1990
  });
2008
1991
  instance.defaults.paramsSerializer = {
2009
1992
  encode: params => encodeURIComponent(params),
@@ -25282,30 +25265,6 @@ const onClientBanned = (callback) => {
25282
25265
  */
25283
25266
  const onNetworkActivities = (callback) => NetworkActivitiesWatcher$1.getInstance().onNetworkActivities(callback);
25284
25267
 
25285
- /**
25286
- * Fired when a visitor or bot user has exceeded their daily API request quota (100 req/day).
25287
- * The backend returns error code `400323` (`VISITOR_USAGE_LIMIT_EXCEEDED`) on any subsequent
25288
- * read request after the limit is hit.
25289
- *
25290
- * Subscribe to this event to show a full-page error state or prompt the user to sign in.
25291
- *
25292
- * ```js
25293
- * import { onVisitorUsageLimitReached } from '@amityco/ts-sdk'
25294
- * const dispose = onVisitorUsageLimitReached(() => {
25295
- * // show usage limit UI
25296
- * })
25297
- * ```
25298
- *
25299
- * @param callback The function to call when the event is fired
25300
- * @returns an {@link Amity.Unsubscriber} function to stop listening
25301
- *
25302
- * @category Client Events
25303
- */
25304
- const onVisitorUsageLimitReached = (callback) => {
25305
- const client = getActiveClient();
25306
- return createEventSubscriber(client, 'client/onVisitorUsageLimitReached', 'visitorUsageLimitReached', callback);
25307
- };
25308
-
25309
25268
  /**
25310
25269
  * ```js
25311
25270
  * import { onTokenExpired } from '@amityco/ts-sdk'
@@ -27335,7 +27294,6 @@ var index$s = /*#__PURE__*/Object.freeze({
27335
27294
  onClientBanned: onClientBanned,
27336
27295
  onSessionStateChange: onSessionStateChange,
27337
27296
  onNetworkActivities: onNetworkActivities,
27338
- onVisitorUsageLimitReached: onVisitorUsageLimitReached,
27339
27297
  getUserUnread: getUserUnread,
27340
27298
  getMarkerSyncEvents: getMarkerSyncEvents,
27341
27299
  setMarkerSyncEvents: setMarkerSyncEvents,
@@ -29366,7 +29324,7 @@ const getWatchSessionStorage = () => {
29366
29324
  return storageInstance;
29367
29325
  };
29368
29326
 
29369
- const privateKey = "MIIEpQIBAAKCAQEAwAEc/oZgYIvKSUG/C3mONYLR4ZPgAjMEX4bJ+xqqakUDRtqlNO+eZs2blQ1Ko0DBkqPExyQezvjibH5W2UZBV5RaBTlTcNVKTToMBEGesAfaEcM3qUyQHxdbFYZv6P4sb14dcwxTQ8usmaV8ooiR1Fcaso5ZWYcZ8Hb46FbQ7OoVumsBtPWwfZ4f003o5VCl6AIM6lcLv9UDLlFVYhE+PeXpRHtfWlGqxMvqC9oinlwhL6nWv6VjQXW4nhcib72dPBzfHT7k/PMKto2SxALYdb68ENiAGuJLWi3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dZoI8RdmJD1al\n3Q7N9hofooSy4rj6E3txzWZCHJjHad2cnCp/O26HiReGAl7wTcfTmNdiFHhZQzm5\nJBkvWAiwuvQMNfEbnXxw6/vIDwKBgQDH7fX8gsc77JLvAWgp1MaQN/sbqVb6JeT1\nFQfR8E/WFCSmzQBtNzd5KgYuCeelwr/8DyYytvN2BzCYZXp73gI1jF3YlW5jVn74\nOY6TwQ095digwo6Z0yuxopdIOApKgAkL9PRKgNrqAf3NAyMua6lOGifzjDojC3KU\nfylQmxMn4wKBgHp2B9O/H0dEBw5JQ8W0+JX6yWQz7mEjGiR2/1W+XXb8hQ1zr709\nw1r6Gb+EghRpnZ3fBpYGGbYOMFx8wKHM+N6qW3F0ReX8v2juFGE8aRSa5oYBrWzt\nU16Idjbv8hj84cZ1PJmdyvDtpYn9rpWHOZl4rxEbPvbqkIsOMyNVqdT5AoGAOSge\nmwIIU2le2FVeohbibXiToWTYKMuMmURZ5/r72AgKMmWJKbAPe+Q3wBG01/7FRBpQ\noU8Ma0HC8s6QJbliiEyIx9JwrJWd1vkdecBHONrtA4ibm/5zD2WcOllLF+FitLhi\n3qnX6+6F0IaFGFBPJrTzlv0P4dTz/OAdv52V7GECgYEA2TttOKBAqWllgOaZOkql\nLVMJVmgR7s6tLi1+cEP8ZcapV9aRbRzTAKXm4f8AEhtlG9F9kCOvHYCYGi6JaiWJ\nZkHjeex3T+eE6Di6y5Bm/Ift5jtVhJ4jCVwHOKTMej79NPUFTJfv8hCo29haBDv6\nRXFrv+T21KCcw8k3sJeJWWQ=\n-----END PRIVATE KEY-----";
29370
29328
  /*
29371
29329
  * The crypto algorithm used for importing key and signing string
29372
29330
  */
@@ -42114,9 +42072,55 @@ getCommentByIds.locally = (commentIds) => {
42114
42072
  };
42115
42073
  };
42116
42074
 
42117
- /* begin_public_function
42118
- id: comment.create
42119
- */
42075
+ const getLocalCommentId = () => uuid();
42076
+ const createCommentOptimistic = (bundle) => {
42077
+ const client = getActiveClient();
42078
+ if (!client.cache)
42079
+ return;
42080
+ const commentId = getLocalCommentId();
42081
+ client.log('comment/createComment.optimistically', Object.assign({ commentId }, bundle));
42082
+ const createdTime = new Date().toISOString();
42083
+ const comment = {
42084
+ commentId,
42085
+ userId: client.userId,
42086
+ parentId: bundle.parentId,
42087
+ rootId: bundle.parentId || commentId,
42088
+ childrenNumber: 0,
42089
+ children: [],
42090
+ segmentNumber: 0,
42091
+ editedAt: createdTime,
42092
+ referenceId: bundle.referenceId,
42093
+ referenceType: bundle.referenceType,
42094
+ data: bundle.data,
42095
+ metadata: bundle.metadata,
42096
+ mentionees: bundle.mentionees,
42097
+ attachments: bundle.attachments,
42098
+ links: bundle.links,
42099
+ createdAt: createdTime,
42100
+ updatedAt: createdTime,
42101
+ isDeleted: false,
42102
+ flagCount: 0,
42103
+ hashFlag: null,
42104
+ reactions: {},
42105
+ reactionsCount: 0,
42106
+ myReactions: [],
42107
+ path: '',
42108
+ targetId: '',
42109
+ targetType: 'community',
42110
+ dataType: 'text',
42111
+ syncState: "syncing" /* Amity.SyncState.Syncing */,
42112
+ };
42113
+ const cachedAt = UNSYNCED_OBJECT_CACHED_AT_VALUE;
42114
+ pushToCache(['comment', 'get', comment.commentId], comment, { cachedAt });
42115
+ fireEvent('local.comment.created', {
42116
+ comments: [comment],
42117
+ commentChildren: [],
42118
+ users: [],
42119
+ files: [],
42120
+ communityUsers: [],
42121
+ });
42122
+ return comment;
42123
+ };
42120
42124
  /**
42121
42125
  * ```js
42122
42126
  * import { CommentRepository } from '@amityco/ts-sdk'
@@ -42134,48 +42138,70 @@ getCommentByIds.locally = (commentIds) => {
42134
42138
  const createComment = async (bundle) => {
42135
42139
  const client = getActiveClient();
42136
42140
  client.log('comment/createComment', bundle);
42137
- const { data } = await client.http.post('/api/v3/comments', bundle);
42138
- const { comments } = data;
42139
- // BE always returns an array of comments If it got record 0 from BE it might have a problem on creation logic
42140
- if (comments.length === 0)
42141
- throw new Error('Comment not created');
42142
- const cachedAt = client.cache && Date.now();
42143
- if (client.cache)
42144
- ingestInCache(data, { cachedAt });
42145
- if (['post', 'content'].includes(bundle.referenceType)) {
42146
- PostCommentCountEngine$1.getInstance().queueCommentChangeTask(new CreateTask(bundle.referenceId, comments[0].commentId, data.comments[0].createdAt));
42147
- }
42148
- else if (bundle.referenceType === 'story') {
42149
- const storyIndex = pullFromCache([
42150
- "story-reference" /* STORY_KEY_CACHE.STORY_ID_TO_REFERENCE_ID */,
42151
- bundle.referenceId,
42152
- ]);
42153
- if (storyIndex === null || storyIndex === void 0 ? void 0 : storyIndex.data) {
42154
- const cacheStory = pullFromCache([
42155
- "story" /* STORY_KEY_CACHE.STORY */,
42156
- 'get',
42157
- storyIndex.data,
42141
+ const optimisticData = createCommentOptimistic(bundle);
42142
+ const commentId = optimisticData === null || optimisticData === void 0 ? void 0 : optimisticData.commentId;
42143
+ try {
42144
+ const { data } = await client.http.post('/api/v3/comments', Object.assign(Object.assign({}, bundle), { commentId }));
42145
+ const { comments } = data;
42146
+ // BE always returns an array of comments If it got record 0 from BE it might have a problem on creation logic
42147
+ if (comments.length === 0)
42148
+ throw new Error('Comment not created');
42149
+ const cachedAt = client.cache && Date.now();
42150
+ if (client.cache)
42151
+ ingestInCache(data, { cachedAt });
42152
+ if (['post', 'content'].includes(bundle.referenceType)) {
42153
+ PostCommentCountEngine$1.getInstance().queueCommentChangeTask(new CreateTask(bundle.referenceId, comments[0].commentId, data.comments[0].createdAt));
42154
+ }
42155
+ else if (bundle.referenceType === 'story') {
42156
+ const storyIndex = pullFromCache([
42157
+ "story-reference" /* STORY_KEY_CACHE.STORY_ID_TO_REFERENCE_ID */,
42158
+ bundle.referenceId,
42158
42159
  ]);
42159
- if (cacheStory === null || cacheStory === void 0 ? void 0 : cacheStory.data) {
42160
- fireEvent('story.updated', {
42161
- stories: [
42162
- Object.assign(Object.assign({}, cacheStory.data), { commentsCount: cacheStory.data.commentsCount + 1, comments: [...new Set([...cacheStory.data.comments, comments[0].commentId])] }),
42163
- ],
42164
- categories: [],
42165
- comments,
42166
- communities: [],
42167
- communityUsers: data.communityUsers,
42168
- files: data.files,
42169
- users: data.users,
42170
- });
42160
+ if (storyIndex === null || storyIndex === void 0 ? void 0 : storyIndex.data) {
42161
+ const cacheStory = pullFromCache([
42162
+ "story" /* STORY_KEY_CACHE.STORY */,
42163
+ 'get',
42164
+ storyIndex.data,
42165
+ ]);
42166
+ if (cacheStory === null || cacheStory === void 0 ? void 0 : cacheStory.data) {
42167
+ fireEvent('story.updated', {
42168
+ stories: [
42169
+ Object.assign(Object.assign({}, cacheStory.data), { commentsCount: cacheStory.data.commentsCount + 1, comments: [...new Set([...cacheStory.data.comments, comments[0].commentId])] }),
42170
+ ],
42171
+ categories: [],
42172
+ comments,
42173
+ communities: [],
42174
+ communityUsers: data.communityUsers,
42175
+ files: data.files,
42176
+ users: data.users,
42177
+ });
42178
+ }
42171
42179
  }
42172
42180
  }
42181
+ fireEvent('local.comment.created', {
42182
+ comments: [Object.assign(Object.assign({}, comments[0]), { syncState: "synced" /* Amity.SyncState.Synced */ })],
42183
+ commentChildren: data.commentChildren,
42184
+ users: data.users,
42185
+ files: data.files,
42186
+ communityUsers: data.communityUsers,
42187
+ });
42188
+ return {
42189
+ data: LinkedObject.comment(comments[0]),
42190
+ cachedAt,
42191
+ };
42192
+ }
42193
+ catch (e) {
42194
+ if (optimisticData) {
42195
+ fireEvent('local.comment.created', {
42196
+ comments: [Object.assign(Object.assign({}, optimisticData), { syncState: "error" /* Amity.SyncState.Error */ })],
42197
+ commentChildren: [],
42198
+ users: [],
42199
+ files: [],
42200
+ communityUsers: [],
42201
+ });
42202
+ }
42203
+ throw e;
42173
42204
  }
42174
- fireEvent('local.comment.created', data);
42175
- return {
42176
- data: LinkedObject.comment(comments[0]),
42177
- cachedAt,
42178
- };
42179
42205
  };
42180
42206
  /* end_public_function */
42181
42207
 
@@ -42302,8 +42328,21 @@ getStoryByStoryId$1.locally = (storyId) => {
42302
42328
  * @async
42303
42329
  */
42304
42330
  const deleteComment = async (commentId, permanent = false) => {
42305
- var _a, _b;
42331
+ var _a, _b, _c;
42306
42332
  const client = getActiveClient();
42333
+ const cached = (_a = pullFromCache(['comment', 'get', commentId])) === null || _a === void 0 ? void 0 : _a.data;
42334
+ if ((cached === null || cached === void 0 ? void 0 : cached.syncState) === "error" /* Amity.SyncState.Error */) {
42335
+ const deletedComment = Object.assign(Object.assign({}, cached), { isDeleted: true });
42336
+ dropFromCache(['comment', 'get', commentId]);
42337
+ fireEvent('local.comment.deleted', {
42338
+ comments: [deletedComment],
42339
+ commentChildren: [],
42340
+ files: [],
42341
+ users: [],
42342
+ communityUsers: [],
42343
+ });
42344
+ return deletedComment;
42345
+ }
42307
42346
  const comment = await getComment$2(commentId);
42308
42347
  // API-FIX: This endpoint has not been implemented yet.
42309
42348
  await client.http.delete(`/api/v4/comments/${encodeURIComponent(commentId)}`, {
@@ -42333,11 +42372,11 @@ const deleteComment = async (commentId, permanent = false) => {
42333
42372
  });
42334
42373
  }
42335
42374
  else {
42336
- const post = (_a = pullFromCache(['post', 'get', comment.data.referenceId])) === null || _a === void 0 ? void 0 : _a.data;
42375
+ const post = (_b = pullFromCache(['post', 'get', comment.data.referenceId])) === null || _b === void 0 ? void 0 : _b.data;
42337
42376
  if (post) {
42338
42377
  const engine = PostCommentCountEngine$1.getInstance();
42339
42378
  engine.queueCommentChangeTask(new DeleteTask(post.postId, commentId));
42340
- if (!deleted.parentId && ((_b = deleted.children /* c8 ignore next */) === null || _b === void 0 ? void 0 : _b.length) > 0) {
42379
+ if (!deleted.parentId && ((_c = deleted.children /* c8 ignore next */) === null || _c === void 0 ? void 0 : _c.length) > 0) {
42341
42380
  // NOTE: delete the parent comment will also remove all children comments
42342
42381
  deleted.children.forEach((childCommentId) => {
42343
42382
  engine.queueCommentChangeTask(new DeleteTask(post.postId, childCommentId));