Skip to content

Commit

Permalink
chore(notifications): enable eslint and remove telint (#13091)
Browse files Browse the repository at this point in the history
* chore(notifications): enable eslint and remove telint

* chore(notifications): run yarn lint:fix

* chore(notifications): manual fix of linter reported errors

* apply suggestions

* fix lint regressions caused by rebasing
  • Loading branch information
HuiSF authored Mar 20, 2024
1 parent f6055cb commit 1a6d358
Show file tree
Hide file tree
Showing 61 changed files with 208 additions and 155 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ module.exports = {
'packages/datastore-storage-adapter',
// 'packages/geo',
// 'packages/interactions',
'packages/notifications',
// 'packages/notifications',
// 'packages/predictions',
// 'packages/pubsub',
// 'packages/react-native',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const mockGetConstants = jest.fn();
const mockRegisterHeadlessTask = jest.fn();

describe('initializePushNotifications (native)', () => {
let initializePushNotifications;
let initializePushNotifications: () => void;
const { NativeEvent } = pushModuleConstants;
// create mocks
const mockEventListenerRemover = { remove: jest.fn() };
Expand Down
2 changes: 1 addition & 1 deletion packages/notifications/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = {
coverageThreshold: {
global: {
branches: 73,
functions: 90,
functions: 89,
lines: 91,
statements: 92,
},
Expand Down
3 changes: 2 additions & 1 deletion packages/notifications/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"clean": "npm run clean:size && rimraf dist lib lib-esm",
"clean:size": "rimraf dual-publish-tmp tmp*",
"format": "echo \"Not implemented\"",
"lint": "tslint 'src/**/*.ts' && npm run ts-coverage",
"lint": "eslint '**/*.{ts,tsx}' && npm run ts-coverage",
"lint:fix": "eslint '**/*.{ts,tsx}' --fix",
"ts-coverage": "typescript-coverage-report -p ./tsconfig.build.json -t 88.21"
},
"typesVersions": {
Expand Down
25 changes: 16 additions & 9 deletions packages/notifications/src/eventListeners/eventListeners.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { EventListener, EventListenerRemover, EventType } from './types';
import {
EventListener,
EventListenerHandler,
EventListenerRemover,
EventType,
} from './types';

const eventListeners: Record<string, Set<EventListener<Function>>> = {};
const eventListeners: Record<
string,
Set<EventListener<EventListenerHandler>>
> = {};

export const notifyEventListeners = (type: EventType, ...args: any[]): void => {
eventListeners[type]?.forEach(listener => {
Expand All @@ -17,15 +25,11 @@ export const notifyEventListenersAndAwaitHandlers = (
): Promise<void[]> =>
Promise.all<void>(
Array.from(eventListeners[type] ?? []).map(async listener => {
try {
await listener.handleEvent(...args);
} catch (err) {
throw err;
}
await listener.handleEvent(...args);
}),
);

export const addEventListener = <EventHandler extends Function>(
export const addEventListener = <EventHandler extends EventListenerHandler>(
type: EventType,
handler: EventHandler,
): EventListenerRemover => {
Expand All @@ -40,7 +44,10 @@ export const addEventListener = <EventHandler extends Function>(
},
};
eventListeners[type].add(listener);

return {
remove: () => listener.remove(),
remove: () => {
listener.remove();
},
};
};
12 changes: 7 additions & 5 deletions packages/notifications/src/eventListeners/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import { InAppMessageInteractionEvent } from '../inAppMessaging/types';
import { PushNotificationEvent } from '../pushNotifications/types';

export interface EventListener<EventHandler extends Function> {
export type EventListenerHandler = (...args: any[]) => unknown;

export interface EventListener<EventHandler extends EventListenerHandler> {
handleEvent: EventHandler;
remove: () => void;
remove(): void;
}

export type EventType = InAppMessageInteractionEvent | PushNotificationEvent;

export type EventListenerRemover = {
remove: () => void;
};
export interface EventListenerRemover {
remove(): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
AmplifyErrorCode,
ServiceError,
} from '@aws-amplify/core/internals/utils';

import { InAppMessagingError } from './InAppMessagingError';

export function assertServiceError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// SPDX-License-Identifier: Apache-2.0

import { defaultStorage } from '@aws-amplify/core';
import { STORAGE_KEY_SUFFIX, PINPOINT_KEY_PREFIX } from '../utils';

import { PINPOINT_KEY_PREFIX, STORAGE_KEY_SUFFIX } from '../utils';
import { InAppMessagingValidationErrorCode } from '../../../errors';
import { assertIsInitialized } from '../../../utils';

Expand All @@ -22,5 +22,6 @@ import { assertIsInitialized } from '../../../utils';
export async function clearMessages(): Promise<void> {
assertIsInitialized();
const key = `${PINPOINT_KEY_PREFIX}${STORAGE_KEY_SUFFIX}`;
return await defaultStorage.removeItem(key);

await defaultStorage.removeItem(key);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import flatten from 'lodash/flatten.js';
import { defaultStorage } from '@aws-amplify/core';

import { notifyEventListeners } from '../../../../eventListeners';
import { assertServiceError } from '../../../errors';
import {
InAppMessagingValidationErrorCode,
assertServiceError,
} from '../../../errors';
import { InAppMessage } from '../../../types';
import { assertIsInitialized } from '../../../utils';
import { DispatchEventInput } from '../types';
Expand All @@ -15,6 +18,7 @@ import {
getConflictHandler,
processInAppMessages,
} from '../utils';

import { syncMessages } from './syncMessages';
import { setConflictHandler } from './setConflictHandler';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

import { InAppMessagingAction } from '@aws-amplify/core/internals/utils';
import {
updateEndpoint,
UpdateEndpointException,
updateEndpoint,
} from '@aws-amplify/core/internals/providers/pinpoint';

import { InAppMessagingValidationErrorCode } from '../../../errors';
import {
CATEGORY,
Expand All @@ -22,7 +23,7 @@ import { assertIsInitialized } from '../../../utils';
* profile and activities or actions in your application. Activity can be tracked across devices & platforms by using
* the same `userId`.
*
* @param {IdentifyUserParameters} params The input object used to construct requests sent to Pinpoint's UpdateEndpoint
* @param input The input object that conforms to {@link IdentifyUserInput} used to construct requests sent to Pinpoint's UpdateEndpoint
* API.
* @throws service: {@link UpdateEndpointException} - Thrown when the underlying Pinpoint service returns an error.
* @throws validation: {@link InAppMessagingValidationErrorCode} - Thrown when the provided parameters or library
Expand Down Expand Up @@ -66,11 +67,8 @@ import { assertIsInitialized } from '../../../utils';
* },
* });
*/
export const identifyUser = async ({
userId,
userProfile,
options,
}: IdentifyUserInput): Promise<void> => {
export const identifyUser = async (input: IdentifyUserInput): Promise<void> => {
const { userId, userProfile, options } = input;
assertIsInitialized();
const { credentials, identityId } = await resolveCredentials();
const { appId, region } = resolveConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
// SPDX-License-Identifier: Apache-2.0

import { sessionListener } from '@aws-amplify/core/internals/utils';
import { Hub, HubCapsule } from '@aws-amplify/core';

import { InAppMessage, InAppMessagingEvent } from '../../../types';
import { addEventListener } from '../../../../eventListeners';
import { recordAnalyticsEvent } from '../utils/helpers';
import { PinpointMessageEvent } from '../types';
import { Hub, HubCapsule } from '@aws-amplify/core';
import { dispatchEvent } from './dispatchEvent';
import { incrementMessageCounts, sessionStateChangeHandler } from '../utils';
import { isInitialized, initialize } from '../../../utils';
import { initialize, isInitialized } from '../../../utils';

import { dispatchEvent } from './dispatchEvent';

/**
* Initialize and set up in-app messaging category. This API needs to be called to enable other InAppMessaging APIs.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import { notifyEventListeners } from '../../../../eventListeners';
import { InAppMessagingValidationErrorCode } from '../../../errors';
import { assertIsInitialized } from '../../../utils';
import { NotifyMessageInteractionInput } from '../types/inputs';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import { addEventListener } from '../../../../eventListeners';
import { InAppMessagingValidationErrorCode } from '../../../errors';
import { assertIsInitialized } from '../../../utils';
import { OnMessageActionTakenInput } from '../types/inputs';
import { OnMessageActionTakenOutput } from '../types/outputs';
Expand All @@ -25,5 +26,6 @@ export function onMessageActionTaken(
input: OnMessageActionTakenInput,
): OnMessageActionTakenOutput {
assertIsInitialized();

return addEventListener('messageActionTaken', input);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { addEventListener } from '../../../../eventListeners';
import { OnMessageDismissedOutput } from '../types/outputs';
import { OnMessageDismissedInput } from '../types/inputs';
import { assertIsInitialized } from '../../../utils';
import { InAppMessagingValidationErrorCode } from '../../../errors';

/**
* Registers a callback that will be invoked on `messageDismissed` events.
Expand All @@ -25,5 +26,6 @@ export function onMessageDismissed(
input: OnMessageDismissedInput,
): OnMessageDismissedOutput {
assertIsInitialized();

return addEventListener('messageDismissed', input);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import { addEventListener } from '../../../../eventListeners';
import { OnMessageDisplayedOutput } from '../types/outputs';
import { OnMessageDisplayedInput } from '../types/inputs';
import { assertIsInitialized } from '../../../utils';
import { InAppMessagingValidationErrorCode } from '../../../errors';

/**
* Registers a callback that will be invoked on `messageDisplayed` events.
*
* @param {OnMessageDisplayedInput} input - The input object that holds the callback handler.
* @throws validation: {@link InAppMessagingValidationErrorCode} - Thrown when the provided parameters or library
* configuration is incorrect, or if In App messaging hasn't been initialized.
* @returns {OnMessageDismissedOutput} - An object that holds a remove method to stop listening to events.
* @returns {OnMessageDisplayedOutput} - An object that holds a remove method to stop listening to events.
* @example
* ```ts
* onMessageDisplayed((message) => {
Expand All @@ -25,5 +26,6 @@ export function onMessageDisplayed(
input: OnMessageDisplayedInput,
): OnMessageDisplayedOutput {
assertIsInitialized();

return addEventListener('messageDisplayed', input);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import { addEventListener } from '../../../../eventListeners';
import { InAppMessagingValidationErrorCode } from '../../../errors';
import { assertIsInitialized } from '../../../utils';
import { OnMessageReceivedInput } from '../types/inputs';
import { OnMessageReceivedOutput } from '../types/outputs';
Expand All @@ -25,5 +26,6 @@ export function onMessageReceived(
input: OnMessageReceivedInput,
): OnMessageReceivedOutput {
assertIsInitialized();

return addEventListener('messageReceived', input);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { InAppMessagingValidationErrorCode } from '../../../errors';
import { assertIsInitialized } from '../../../utils';
import { SetConflictHandlerInput } from '../types';
import { setConflictHandler as setConflictHandlerInteral } from '../utils';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ import { InAppMessagingAction } from '@aws-amplify/core/internals/utils';
import { resolveEndpointId } from '@aws-amplify/core/internals/providers/pinpoint';
import { defaultStorage } from '@aws-amplify/core';
import {
resolveConfig,
resolveCredentials,
getInAppMessagingUserAgentString,
STORAGE_KEY_SUFFIX,
PINPOINT_KEY_PREFIX,
CATEGORY,
CHANNEL_TYPE,
} from '../utils';
import {
getInAppMessages,
GetInAppMessagesInput,
GetInAppMessagesOutput,
getInAppMessages,
} from '@aws-amplify/core/internals/aws-clients/pinpoint';

import {
CATEGORY,
CHANNEL_TYPE,
PINPOINT_KEY_PREFIX,
STORAGE_KEY_SUFFIX,
getInAppMessagingUserAgentString,
resolveConfig,
resolveCredentials,
} from '../utils';
import {
InAppMessagingValidationErrorCode,
assertServiceError,
Expand Down Expand Up @@ -80,6 +81,7 @@ async function fetchInAppMessages() {
);
const { InAppMessageCampaigns: messages } =
response.InAppMessagesResponse ?? {};

return messages;
} catch (error) {
assertServiceError(error);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { IdentifyUserOptions } from './options';
import {
InAppMessageConflictHandler,
OnMessageInteractionEventHandler,
} from './types';
import {
InAppMessage,
InAppMessageInteractionEvent,
InAppMessagingEvent,
InAppMessagingIdentifyUserInput,
} from '../../../types';

import { IdentifyUserOptions } from './options';
import {
InAppMessageConflictHandler,
OnMessageInteractionEventHandler,
} from './types';

/**
* Input type for Pinpoint identifyUser API.
*/
Expand Down Expand Up @@ -52,7 +53,7 @@ export type OnMessageActionTakenInput = OnMessageInteractionEventHandler;
/**
* Input type for NotifyMessageInteraction API.
*/
export type NotifyMessageInteractionInput = {
export interface NotifyMessageInteractionInput {
message: InAppMessage;
type: InAppMessageInteractionEvent;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import { InAppMessage } from '../../../types';

export type InAppMessageCountMap = Record<string, number>;

export type DailyInAppMessageCounter = {
export interface DailyInAppMessageCounter {
count: number;
lastCountTimestamp: string;
};
}

export type InAppMessageCounts = {
export interface InAppMessageCounts {
sessionCount: number;
dailyCount: number;
totalCount: number;
};
}

export type MetricsComparator = (
metricsVal: number,
Expand Down
Loading

0 comments on commit 1a6d358

Please sign in to comment.