From 27160526074dbe1cd26834085e02671760fe2e80 Mon Sep 17 00:00:00 2001 From: Bilta John Date: Wed, 12 Feb 2025 14:26:14 +0530 Subject: [PATCH 1/8] fix: Remove verify token call --- src/Siren.ts | 92 ---------------------------------------------------- src/types.ts | 1 - 2 files changed, 93 deletions(-) diff --git a/src/Siren.ts b/src/Siren.ts index db3fcd6..a385e6a 100644 --- a/src/Siren.ts +++ b/src/Siren.ts @@ -1,6 +1,5 @@ import SirenError from './error'; import { - verifyToken, fetchUnviewedNotificationsCount, fetchAllNotifications, markAsReadById, @@ -10,22 +9,18 @@ import { } from './api'; import { BulkUpdateType, - VerificationStatus, ErrorMap, SirenErrorTypes, DATA_FETCH_INTERVAL, - ErrorLevelType, EventType } from './constants/generic'; import { getStartDate, sanitizeSession, emitSirenError } from './utils'; import type { - ErrorCallbackType, FetchNotificationsParamsType, InitConfigType, ActionCallbackType, NotificationsApiResponse, - VerifyTokenResponse, NotificationDataType, SirenErrorType, NotificationDataResponse, @@ -39,19 +34,16 @@ import type { export class Siren { private token: string; private recipientId: string; - private onError: ErrorCallbackType; private notificationFetchIntervalId: number | undefined; private unViewedCountFetchIntervalId: number | undefined; private latestNotification: NotificationDataType | null = null; private actionCallbacks: ActionCallbackType | undefined; - private tokenValidationStatus: VerificationStatus = VerificationStatus.PENDING; /** * Binds specific class methods to ensure the correct `this` context when called. * This is necessary for methods that are used asynchronously or passed as callbacks. * @private */ private bindMethods() { - this.verifyToken = this.verifyToken.bind(this); this.fetchUnviewedNotificationsCount = this.fetchUnviewedNotificationsCount.bind(this); this.fetchAllNotifications = this.fetchAllNotifications.bind(this); this.startRealTimeFetch = this.startRealTimeFetch.bind(this); @@ -81,35 +73,11 @@ export class Siren { this.token = session.data?.token || ''; this.recipientId = session.data?.recipientId || ''; - this.onError = session.data?.onError; this.actionCallbacks = actionCallbacks; - if (session.data) this.verifyToken(); this.bindMethods(); } - /** - * Verifies the validity of the user's recipient and token. - * - * @returns {Promise} - * - `VerifyTokenResponse` object containing status as SUCCESS. - * - `null` if the API fails to return a valid response. - */ - async verifyToken(): Promise { - this.tokenValidationStatus = VerificationStatus.PENDING; - const res = await verifyToken(this.token, this.recipientId, this.onError); - - if (res) - this.tokenValidationStatus = - res && res.data?.status === VerificationStatus.SUCCESS - ? VerificationStatus.SUCCESS - : VerificationStatus.FAILED; // api returns null in failed scenarios - if(this.actionCallbacks?.onStatusChange) - this.actionCallbacks.onStatusChange(this.tokenValidationStatus); - - return res; - } - /** * Retrieves the number of unviewed notifications count for the user. * @@ -118,9 +86,6 @@ export class Siren { * */ async fetchUnviewedNotificationsCount(): Promise { - const hasPermission = this.authorizeUserAction(); - - if (!hasPermission) return this.getErrorForUnauthorizedAction(); const response = await fetchUnviewedNotificationsCount(this.token, this.recipientId); @@ -146,9 +111,6 @@ export class Siren { async fetchAllNotifications( params: FetchNotificationsParamsType ): Promise { - const hasPermission = this.authorizeUserAction(); - - if (!hasPermission) return this.getErrorForUnauthorizedAction(); const response = await fetchAllNotifications(this.token, this.recipientId, params); if (response && response?.data?.length && response?.data?.length > 0) @@ -170,7 +132,6 @@ export class Siren { eventType: EventType; params?: FetchNotificationsParamsType; }): void { - this.authorizeUserAction(); switch (eventType) { case EventType.NOTIFICATION: this.startRealTimeNotificationFetch(params ?? {}); @@ -208,9 +169,7 @@ export class Siren { */ async markAsReadById(id: string): Promise { - const hasPermission = this.authorizeUserAction(); - if (!hasPermission) return this.getErrorForUnauthorizedAction(); let response; if (id) response = await markAsReadById(this.token, this.recipientId, id); @@ -230,9 +189,6 @@ export class Siren { async markAsReadByDate(params: BulkUpdateParamsType): Promise { const { startDate, category } = params || {}; - const hasPermission = this.authorizeUserAction(); - - if (!hasPermission) return this.getErrorForUnauthorizedAction(); let response; if (startDate) { @@ -265,9 +221,6 @@ export class Siren { */ async deleteById(id: string): Promise { - const hasPermission = this.authorizeUserAction(); - - if (!hasPermission) return this.getErrorForUnauthorizedAction(); let response; if (id) response = await deleteNotificationById(this.token, this.recipientId, id); @@ -286,9 +239,7 @@ export class Siren { */ async deleteByDate(params: BulkUpdateParamsType): Promise { const { startDate, isRead, category } = params || {}; - const hasPermission = this.authorizeUserAction(); - if (!hasPermission) return this.getErrorForUnauthorizedAction(); let response; if (startDate) { @@ -322,9 +273,6 @@ export class Siren { * */ async markAllAsViewed(startDate: string): Promise { - const hasPermission = this.authorizeUserAction(); - - if (!hasPermission) return this.getErrorForUnauthorizedAction(); let response; if (startDate) @@ -349,7 +297,6 @@ export class Siren { params.start ?? this.latestNotification?.createdAt ?? new Date().toISOString(); this.notificationFetchIntervalId = window.setInterval(async () => { - if (this.tokenValidationStatus !== VerificationStatus.SUCCESS) return null; const response = await fetchAllNotifications(this.token, this.recipientId, { ...params, ...(lastFetchedCreatedAt && { @@ -380,7 +327,6 @@ export class Siren { */ private startRealTimeUnviewedCountFetch(): void { this.unViewedCountFetchIntervalId = window.setInterval(async () => { - if (this.tokenValidationStatus !== VerificationStatus.SUCCESS) return; const response = await fetchUnviewedNotificationsCount(this.token, this.recipientId); if (this.actionCallbacks?.onEventReceive) @@ -404,44 +350,6 @@ export class Siren { return { data: null, error }; } - - private authorizeUserAction(): boolean { - if (this?.tokenValidationStatus === VerificationStatus.FAILED) { - const errorObj = new SirenError( - SirenErrorTypes.ERROR, - ErrorMap.UNAUTHORIZED_OPERATION.message, - ErrorMap.UNAUTHORIZED_OPERATION.code - ); - - if (this.onError) this.onError(errorObj.getError()); - - return false; - } - if (this?.tokenValidationStatus === VerificationStatus.PENDING) return false; - - return true; - } - - private getErrorForUnauthorizedAction() { - if (this?.tokenValidationStatus === VerificationStatus.PENDING) - return { - data: null, - error: { - Type: ErrorLevelType.ERROR, - Message: ErrorMap.AUTHENTICATION_PENDING.message, - Code: ErrorMap.AUTHENTICATION_PENDING.code - } - }; - - return { - data: null, - error: { - Type: ErrorLevelType.ERROR, - Message: ErrorMap.UNAUTHORIZED_OPERATION.message, - Code: ErrorMap.UNAUTHORIZED_OPERATION.code - } - }; - } } if (window) window.Siren = Siren; diff --git a/src/types.ts b/src/types.ts index 9cd6563..ed1c887 100644 --- a/src/types.ts +++ b/src/types.ts @@ -158,7 +158,6 @@ export type ActionCallbackType = { response: NotificationsApiResponse | UnviewedCountApiResponse, eventType: EventType ) => void; - onStatusChange?: (status: VerificationStatus) => void; }; /** From 1244f4eaf8a92e276b50bdf505772563249df6a7 Mon Sep 17 00:00:00 2001 From: Bilta John Date: Wed, 12 Feb 2025 14:28:42 +0530 Subject: [PATCH 2/8] fix: Eslint error --- src/types.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/types.ts b/src/types.ts index ed1c887..19459a3 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,6 +1,5 @@ import "promise-polyfill/src/polyfill"; import type { - VerificationStatus, EventType, ApiOperationType, BulkUpdateType From 7203031cc6b82547617e31831b5b17cafa7486d3 Mon Sep 17 00:00:00 2001 From: Bilta John Date: Wed, 12 Feb 2025 14:33:30 +0530 Subject: [PATCH 3/8] fix: Eslint error --- src/utils.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 85bd1be..939f37e 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -56,8 +56,7 @@ export function sanitizeSession( code: ErrorMap.INVALID_CREDENTIALS.code }; } else if ( - (actionCallbacks?.onEventReceive && typeof actionCallbacks.onEventReceive !== 'function') || - (actionCallbacks?.onStatusChange && typeof actionCallbacks.onStatusChange !== 'function') + (actionCallbacks?.onEventReceive && typeof actionCallbacks.onEventReceive !== 'function') ) { emitSirenError( SirenErrorTypes.CONFIG_ERROR, From ae4c74d6ecb8e8aa8d81a7d3465c8c0a761fcfb1 Mon Sep 17 00:00:00 2001 From: Bilta John Date: Thu, 13 Feb 2025 10:23:23 +0530 Subject: [PATCH 4/8] 1.2.3-rc.0 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index fa47e4c..9f2799c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@sirenapp/js-sdk", - "version": "1.2.2", + "version": "1.2.3-rc.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@sirenapp/js-sdk", - "version": "1.2.2", + "version": "1.2.3-rc.0", "license": "MIT", "dependencies": { "promise-polyfill": "^8.3.0", diff --git a/package.json b/package.json index 8420d1a..27cefb4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@sirenapp/js-sdk", - "version": "1.2.2", + "version": "1.2.3-rc.0", "description": "JavaScript middleware designed to streamline interaction for managing and displaying in-app notifications seamlessly", "main": "dist/umd/siren-js-umd-sdk.js", "module": "dist/esm/siren-js-esm-sdk.js", From 686c6c01ac9e3ec2c125ada16a49eabe46f81594 Mon Sep 17 00:00:00 2001 From: Bilta John Date: Thu, 13 Feb 2025 11:35:27 +0530 Subject: [PATCH 5/8] docs: Update change log --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 468a49c..ce59d15 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ All notable changes to this project will be documented in this file. +### Fixed + +## [1.2.4] - 2025-02-13 + +- Remove verify token api call + ## [1.2.1] - 2024-06-24 ### Fixed From 86c5831e79b630125a822fca0b758783b5ba7616 Mon Sep 17 00:00:00 2001 From: Bilta John Date: Thu, 13 Feb 2025 11:37:36 +0530 Subject: [PATCH 6/8] docs: Update read me --- README.md | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 94753fd..d5d3cae 100644 --- a/README.md +++ b/README.md @@ -39,9 +39,6 @@ const sirenInstance = new Siren({ onEventReceive?: (response: NotificationsApiResponse, eventType: 'NOTIFICATIONS'| 'UNVIEWED_COUNT') => { # callback function to receive data }; - onStatusChange?: (status: 'SUCCESS' | 'FAILED' | 'PENDING') =>{ - # callback function triggered when token verification status changes - } } ``` @@ -93,15 +90,7 @@ Siren constructor accepts the following arguments ## Siren Methods -### 1. verifyToken - -This method verifies the validity of the given tokens (recipientId and userToken).This method is called automatically while creating the instance . Once the verification is successful, the remaining exposed methods can be accessed. - -```bash -await sirenInstance.verifyToken(); -``` - -### 2. fetchUnviewedNotificationsCount +### 1. fetchUnviewedNotificationsCount This method retrieves the count of unviewed notifications. @@ -109,7 +98,7 @@ This method retrieves the count of unviewed notifications. const { unviewedCount } = await sirenInstance.fetchUnviewedNotificationsCount() ``` -### 3. fetchAllNotifications +### 2. fetchAllNotifications This method retrieves list of notifications in a paginated manner. @@ -192,7 +181,7 @@ Below are the accepted optional filters }[] ``` -### 4. startRealTimeFetch +### 3. startRealTimeFetch By specifying the parameter eventType as either `NOTIFICATIONS` or `UNVIEWED_COUNT`, this method triggers the real-time retrieval of notifications or the count of unviewed notifications. If `NOTIFICATIONS` is selected, further parameters (`params`) can be provided for additional customization or filtering @@ -253,7 +242,7 @@ Below are the accepted optional filters for `NOTIFICATIONS` event type -### 5. stopRealTimeFetch +### 4. stopRealTimeFetch By specifying the parameter eventType as either `NOTIFICATIONS` or `UNVIEWED_COUNT`, this method stops the real-time retrieval of notifications or the count of unviewed notifications. @@ -264,7 +253,7 @@ sirenInstance.stopRealTimeFetch(NOTIFICATIONS); sirenInstance.stopRealTimeFetch(UNVIEWED_COUNT); ``` -### 6. markAsReadById +### 5. markAsReadById This method marks the notification as read. It accepts a notification id as an argument. @@ -272,7 +261,7 @@ This method marks the notification as read. It accepts a notification id as an a await sirenInstance.markAsReadById("your-notification-id"); ``` -### 7. markAsReadByDate +### 6. markAsReadByDate This method marks the notifications as read till the given date.
It accepts a param object as an argument with keys startDate (ISO date string) and category(string). @@ -281,7 +270,7 @@ It accepts a param object as an argument with keys startDate (ISO date string) a await sirenInstance.markAsReadByDate({startDate: "2011-10-05T14:48:00.000Z"}); ``` -### 8. deleteById +### 7. deleteById This method deletes a notification. It accepts a notification id as an argument. @@ -289,7 +278,7 @@ This method deletes a notification. It accepts a notification id as an argument. await sirenInstance.deleteById("your-notification-id"); ``` -### 9. deleteByDate +### 8. deleteByDate This method deletes the notifications till the given date.
It accepts a param object as an argument with keys startDate (ISO date string), isRead(boolean) and category(string). @@ -298,7 +287,7 @@ It accepts a param object as an argument with keys startDate (ISO date string), await sirenInstance.deleteByDate({startDate: "2011-10-05T14:48:00.000Z"}); ``` -### 10. markAllAsViewed +### 9. markAllAsViewed This method marks the notifications as viewed till the given date. This sets the unviewed count as 0
It accepts an ISO date string as an argument From b0ad47284b1c3f5b1f386853b842813a622df8b0 Mon Sep 17 00:00:00 2001 From: Bilta John Date: Thu, 13 Feb 2025 11:53:05 +0530 Subject: [PATCH 7/8] tests: Update test cases --- tests/siren.spec.ts | 50 --------------------------------------------- 1 file changed, 50 deletions(-) diff --git a/tests/siren.spec.ts b/tests/siren.spec.ts index c167d2e..c333462 100644 --- a/tests/siren.spec.ts +++ b/tests/siren.spec.ts @@ -19,56 +19,6 @@ describe('Siren Class', () => { Code: 'MISSING_PARAMETER' }; - beforeAll(() => { - const myPrivateFunc = jest.spyOn(Siren.prototype as any, 'authorizeUserAction'); - - myPrivateFunc.mockImplementation(() => true); - }); - - describe('verifyToken', () => { - let mockApi: jest.SpyInstance; - - const sirenInstance: Siren = new Siren(constructorProps); - - afterEach(() => { - mockApi.mockReset(); // Reset mock between tests - }); - - it('should call api with correct arguments and return success', async () => { - mockApi = jest - .spyOn(Apis, 'verifyToken') - .mockImplementation(() => Promise.resolve({ data: { status: 'SUCCESS' }, error: null })); - const res = await sirenInstance.verifyToken(); - - expect(res).toEqual({ data: { status: 'SUCCESS' }, error: null }); - expect(mockApi).toHaveBeenCalledTimes(1); - expect(mockApi).toHaveBeenCalledWith( - constructorProps.token, - constructorProps.recipientId, - constructorProps.onError - ); - }); - it('should call api with incorrect arguments and return error', async () => { - mockApi = jest.spyOn(Apis, 'verifyToken').mockImplementation(() => - Promise.resolve({ - data: null, - error: ApiError - }) - ); - const res = await sirenInstance.verifyToken(); - - expect(mockApi).toHaveBeenCalledTimes(1); - expect(mockApi).toHaveBeenCalledWith( - constructorProps.token, - constructorProps.recipientId, - constructorProps.onError - ); - expect(res).toEqual({ - data: null, - error: ApiError - }); - }); - }); describe('fetchUnviewedNotificationsCount', () => { const ApiResponse = { id: '1', From 8cb98db7491cc9312274aa5c5dbdc8cdd603c83c Mon Sep 17 00:00:00 2001 From: Bilta John Date: Thu, 13 Feb 2025 14:08:02 +0530 Subject: [PATCH 8/8] build: Revert rc version --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 9f2799c..fa47e4c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@sirenapp/js-sdk", - "version": "1.2.3-rc.0", + "version": "1.2.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@sirenapp/js-sdk", - "version": "1.2.3-rc.0", + "version": "1.2.2", "license": "MIT", "dependencies": { "promise-polyfill": "^8.3.0", diff --git a/package.json b/package.json index 27cefb4..8420d1a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@sirenapp/js-sdk", - "version": "1.2.3-rc.0", + "version": "1.2.2", "description": "JavaScript middleware designed to streamline interaction for managing and displaying in-app notifications seamlessly", "main": "dist/umd/siren-js-umd-sdk.js", "module": "dist/esm/siren-js-esm-sdk.js",