diff --git a/.sonarcloud.properties b/.sonarcloud.properties new file mode 100644 index 0000000..bf42636 --- /dev/null +++ b/.sonarcloud.properties @@ -0,0 +1,12 @@ +# Path to sources +sonar.sources=src +sonar.exclusions=src/__tests__/** +#sonar.inclusions= +# Path to tests +sonar.tests=src/__tests__ +#sonar.test.exclusions= +#sonar.test.inclusions= +# Source encoding +sonar.sourceEncoding=UTF-8 +# Exclusions for copy-paste detection +#sonar.cpd.exclusions= diff --git a/src/providers/push/apns/apns.provider.ts b/src/providers/push/apns/apns.provider.ts index d27f581..b7bcd8b 100644 --- a/src/providers/push/apns/apns.provider.ts +++ b/src/providers/push/apns/apns.provider.ts @@ -1,12 +1,11 @@ import {inject, Provider} from '@loopback/core'; +import {AnyObject} from '@loopback/repository'; import {HttpErrors} from '@loopback/rest'; import apns from '@parse/node-apn'; import {ApnsBinding} from './keys'; import {ApnsConfigType, ApnsMessage, ApnsSubscriberType} from './types'; -// sonarignore:start -// eslint-disable-next-line @typescript-eslint/no-explicit-any -export class ApnsProvider implements Provider { - // sonarignore:end + +export class ApnsProvider implements Provider { constructor( @inject(ApnsBinding.Config, { optional: true, @@ -37,7 +36,8 @@ export class ApnsProvider implements Provider { ); } - if (message.receiver.to.length > 500) { + const maxReceivers = 500; + if (message.receiver.to.length > maxReceivers) { throw new HttpErrors.BadRequest( 'Message receiver count cannot exceed 500 !', ); @@ -47,9 +47,12 @@ export class ApnsProvider implements Provider { } } getMainNote(message: ApnsMessage) { + const expiresIn = 3600; // seconds + const floor = 1000; + const defaultBadgeCount = 3; const note = new apns.Notification(); - note.expiry = Math.floor(Date.now() / 1000) + 3600; // Expires 1 hour from now. - note.badge = this.apnsConfig?.options.badge ?? 3; + note.expiry = Math.floor(Date.now() / floor) + expiresIn; // Expires 1 hour from now. + note.badge = this.apnsConfig?.options.badge ?? defaultBadgeCount; note.alert = message.body; note.payload = {messageFrom: message.options.messageFrom}; // The topic is usually the bundle identifier of your application. diff --git a/src/providers/push/apns/types.ts b/src/providers/push/apns/types.ts index f4b58d2..b779c8b 100644 --- a/src/providers/push/apns/types.ts +++ b/src/providers/push/apns/types.ts @@ -1,3 +1,4 @@ +import {AnyObject} from '@loopback/repository'; import {ProviderOptions} from '@parse/node-apn'; import { PushMessage, @@ -20,12 +21,7 @@ export interface ApnsConfigType { export interface ApnsMessage extends PushMessage { receiver: ApnsReceiver; - // sonarignore:start - options: { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - [key: string]: any; - }; - // sonarignore:end + options: AnyObject; } export interface ApnsReceiver extends PushReceiver { to: ApnsSubscriber[]; diff --git a/src/providers/push/fcm/fcm.provider.ts b/src/providers/push/fcm/fcm.provider.ts index f6c2b0c..692058a 100644 --- a/src/providers/push/fcm/fcm.provider.ts +++ b/src/providers/push/fcm/fcm.provider.ts @@ -22,6 +22,7 @@ export class FcmProvider implements Provider { fcmService: admin.app.App; initialValidations(message: FcmMessage) { + const maxReceivers = 500; if ( message.receiver.to.length === 0 && !message.options.topic && @@ -31,8 +32,7 @@ export class FcmProvider implements Provider { 'Message receiver, topic or condition not found in request !', ); } - - if (message.receiver.to.length > 500) { + if (message.receiver.to.length > maxReceivers) { throw new HttpErrors.BadRequest( 'Message receiver count cannot exceed 500 !', ); @@ -69,11 +69,12 @@ export class FcmProvider implements Provider { ...generalMessageObj, data: {...message.options.data}, }; - promises.push( - this.fcmService - .messaging() - .sendMulticast(msgToTransfer, (message.options.dryRun = false)), - ); + + const dryRun = message.options.dryRun ?? false; + const sendPromise = this.fcmService + .messaging() + .sendMulticast(msgToTransfer, dryRun); + promises.push(sendPromise); } return promises; } @@ -94,11 +95,11 @@ export class FcmProvider implements Provider { data: {...message.options.data}, }; - promises.push( - this.fcmService - .messaging() - .send(msgToTransfer, (message.options.dryRun = false)), - ); + const dryRun = message.options.dryRun ?? false; + const sendPromise = this.fcmService + .messaging() + .send(msgToTransfer, dryRun); + promises.push(sendPromise); }); } @@ -123,11 +124,11 @@ export class FcmProvider implements Provider { ...generalMessageObj, data: {...message.options.data}, }; - promises.push( - this.fcmService - .messaging() - .send(msgToTransfer, (message.options.dryRun = false)), - ); + const dryRun = message.options.dryRun ?? false; + const sendPromise = this.fcmService + .messaging() + .send(msgToTransfer, dryRun); + promises.push(sendPromise); }); } diff --git a/src/providers/push/fcm/types.ts b/src/providers/push/fcm/types.ts index 327412a..a04e05f 100644 --- a/src/providers/push/fcm/types.ts +++ b/src/providers/push/fcm/types.ts @@ -36,10 +36,8 @@ export interface FcmMessage extends PushMessage { webpush?: admin.messaging.WebpushConfig; apns?: admin.messaging.ApnsConfig; fcmOptions?: admin.messaging.FcmOptions; - // sonarignore:start - // eslint-disable-next-line @typescript-eslint/no-explicit-any - [key: string]: any; - // sonarignore:end + /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ + [key: string]: any; //NOSONAR }; } diff --git a/src/providers/push/pubnub/pubnub.provider.ts b/src/providers/push/pubnub/pubnub.provider.ts index f3aa599..91dd4bf 100644 --- a/src/providers/push/pubnub/pubnub.provider.ts +++ b/src/providers/push/pubnub/pubnub.provider.ts @@ -2,10 +2,14 @@ import {inject, Provider} from '@loopback/core'; import {HttpErrors} from '@loopback/rest'; import Pubnub from 'pubnub'; import {Aps, MessageConfig, PnApns, TargetsType} from '.'; -import {PayloadType} from './types'; import {Config} from '../../../types'; import {PubnubBindings} from './keys'; -import {PubNubMessage, PubNubNotification, PubNubSubscriberType} from './types'; +import { + PayloadType, + PubNubMessage, + PubNubNotification, + PubNubSubscriberType, +} from './types'; export class PubNubProvider implements Provider { constructor( @@ -56,10 +60,6 @@ export class PubNubProvider implements Provider { aps: apsData, pnPush: targetTypeData, }; - // const generalMessageObj = { - // pn_gcm: pn_gcm, - // pn_apns: Object.assign(pn_apns, message.options), - // }; return {pnGcm: pnGcm, pnApns: Object.assign(pnApns, message.options)}; } getPublishConfig(message: PubNubMessage) { diff --git a/src/providers/push/socketio/types.ts b/src/providers/push/socketio/types.ts index 7987689..4a080a3 100644 --- a/src/providers/push/socketio/types.ts +++ b/src/providers/push/socketio/types.ts @@ -1,3 +1,4 @@ +import {AnyObject} from '@loopback/repository'; import { PushMessage, PushNotification, @@ -32,10 +33,5 @@ export interface SocketConfig { * Path represents the default socket server endpoint */ defaultPath: string; - options: { - // sonarignore:start - // eslint-disable-next-line @typescript-eslint/no-explicit-any - [key: string]: any; - // sonarignore:end - }; + options: AnyObject; } diff --git a/src/types.ts b/src/types.ts index 14c0810..001a56e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,3 +1,4 @@ +import {AnyObject} from '@loopback/repository'; export interface INotification { publish(message: Message): Promise; } @@ -22,20 +23,13 @@ export interface Config { options?: MessageOptions; } -export interface MessageOptions { - // sonarignore:start - // eslint-disable-next-line @typescript-eslint/no-explicit-any - [key: string]: any; - // sonarignore:end -} +export type MessageOptions = AnyObject; export interface Subscriber { id: string; name?: string; - // sonarignore:start // eslint-disable-next-line @typescript-eslint/no-explicit-any - [key: string]: any; - // sonarignore:end + [key: string]: any; //NOSONAR } export interface Receiver {