Skip to content

Commit b3089e3

Browse files
committed
Refactor: Clean up ESLint suppressions and improve code quality
- Add explicit return types to functions in feature-announcements.ts and perp-notifications.test.ts - Apply nullish coalescing operator (??) where appropriate - Rename short variable names for better readability (r -> response, i -> entry/asset, n -> item/rawNotification) - Rename private field #TTL to #ttl in OnChainNotificationsCache class - Add eslint-disable comments with explanations for Contentful API property names - Remove corresponding suppressions from eslint-suppressions.json - Update CHANGELOG.md
1 parent 2170179 commit b3089e3

File tree

4 files changed

+43
-45
lines changed

4 files changed

+43
-45
lines changed

eslint-suppressions.json

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1840,30 +1840,6 @@
18401840
"count": 2
18411841
}
18421842
},
1843-
"packages/notification-services-controller/src/NotificationServicesController/services/feature-announcements.ts": {
1844-
"@typescript-eslint/explicit-function-return-type": {
1845-
"count": 2
1846-
},
1847-
"@typescript-eslint/naming-convention": {
1848-
"count": 2
1849-
},
1850-
"@typescript-eslint/prefer-nullish-coalescing": {
1851-
"count": 2
1852-
},
1853-
"id-length": {
1854-
"count": 3
1855-
}
1856-
},
1857-
"packages/notification-services-controller/src/NotificationServicesController/services/notification-config-cache.ts": {
1858-
"@typescript-eslint/naming-convention": {
1859-
"count": 1
1860-
}
1861-
},
1862-
"packages/notification-services-controller/src/NotificationServicesController/services/perp-notifications.test.ts": {
1863-
"@typescript-eslint/explicit-function-return-type": {
1864-
"count": 1
1865-
}
1866-
},
18671843
"packages/notification-services-controller/src/NotificationServicesController/services/perp-notifications.ts": {
18681844
"@typescript-eslint/explicit-function-return-type": {
18691845
"count": 1

packages/notification-services-controller/src/NotificationServicesController/services/feature-announcements.ts

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,25 @@ type Env = {
3737
*/
3838
export type ContentfulResult = {
3939
includes?: {
40+
// Property names match Contentful API response structure
41+
// eslint-disable-next-line @typescript-eslint/naming-convention
4042
Entry?: Entry[];
43+
// eslint-disable-next-line @typescript-eslint/naming-convention
4144
Asset?: Asset[];
4245
};
4346
items?: TypeFeatureAnnouncement[];
4447
};
4548

46-
export const getFeatureAnnouncementUrl = (env: Env, previewToken?: string) => {
49+
export const getFeatureAnnouncementUrl = (
50+
env: Env,
51+
previewToken?: string,
52+
): string => {
4753
const domain = previewToken ? PREVIEW_DOMAIN : DEFAULT_DOMAIN;
4854
const replacedUrl = FEATURE_ANNOUNCEMENT_URL.replace(
4955
DEFAULT_SPACE_ID,
5056
env.spaceId,
5157
)
52-
.replace(DEFAULT_ACCESS_TOKEN, previewToken || env.accessToken)
58+
.replace(DEFAULT_ACCESS_TOKEN, previewToken ?? env.accessToken)
5359
.replace(DEFAULT_CLIENT_ID, env.platform)
5460
.replace(DEFAULT_DOMAIN, domain);
5561
return encodeURI(replacedUrl);
@@ -62,14 +68,22 @@ const fetchFeatureAnnouncementNotifications = async (
6268
const url = getFeatureAnnouncementUrl(env, previewToken);
6369

6470
const data = await fetch(url)
65-
.then((r) => r.json())
71+
.then((response) => response.json())
6672
.catch(() => null);
6773

6874
if (!data) {
6975
return [];
7076
}
7177

72-
const findIncludedItem = (sysId: string) => {
78+
const findIncludedItem = (
79+
sysId: string,
80+
):
81+
| ImageFields['fields']
82+
| TypeExtensionLinkFields['fields']
83+
| TypePortfolioLinkFields['fields']
84+
| TypeMobileLinkFields['fields']
85+
| TypeExternalLinkFields['fields']
86+
| null => {
7387
const typedData: EntryCollection<
7488
| ImageFields
7589
| TypeExtensionLinkFields
@@ -78,15 +92,19 @@ const fetchFeatureAnnouncementNotifications = async (
7892
| TypeExternalLinkFields
7993
> = data;
8094
const item =
81-
typedData?.includes?.Entry?.find((i: Entry) => i?.sys?.id === sysId) ||
82-
typedData?.includes?.Asset?.find((i: Asset) => i?.sys?.id === sysId);
95+
typedData?.includes?.Entry?.find(
96+
(entry: Entry) => entry?.sys?.id === sysId,
97+
) ??
98+
typedData?.includes?.Asset?.find(
99+
(asset: Asset) => asset?.sys?.id === sysId,
100+
);
83101
return item ? item?.fields : null;
84102
};
85103

86104
const contentfulNotifications = data?.items ?? [];
87105
const rawNotifications: FeatureAnnouncementRawNotification[] =
88-
contentfulNotifications.map((n: TypeFeatureAnnouncement) => {
89-
const { fields } = n;
106+
contentfulNotifications.map((item: TypeFeatureAnnouncement) => {
107+
const { fields } = item;
90108
const imageFields = fields.image
91109
? (findIncludedItem(fields.image.sys.id) as ImageFields['fields'])
92110
: undefined;
@@ -114,7 +132,7 @@ const fetchFeatureAnnouncementNotifications = async (
114132

115133
const notification: FeatureAnnouncementRawNotification = {
116134
type: TRIGGER_TYPES.FEATURES_ANNOUNCEMENT,
117-
createdAt: new Date(n.sys.createdAt).toString(),
135+
createdAt: new Date(item.sys.createdAt).toString(),
118136
data: {
119137
id: fields.id,
120138
category: fields.category,
@@ -163,15 +181,17 @@ const fetchFeatureAnnouncementNotifications = async (
163181
},
164182
} as const;
165183

166-
const filteredRawNotifications = rawNotifications.filter((n) => {
167-
const minVersion = n.data?.[versionKeys[env.platform].min];
168-
const maxVersion = n.data?.[versionKeys[env.platform].max];
169-
return isVersionInBounds({
170-
currentVersion: env.platformVersion,
171-
minVersion,
172-
maxVersion,
173-
});
174-
});
184+
const filteredRawNotifications = rawNotifications.filter(
185+
(rawNotification) => {
186+
const minVersion = rawNotification.data?.[versionKeys[env.platform].min];
187+
const maxVersion = rawNotification.data?.[versionKeys[env.platform].max];
188+
return isVersionInBounds({
189+
currentVersion: env.platformVersion,
190+
minVersion,
191+
maxVersion,
192+
});
193+
},
194+
);
175195

176196
return filteredRawNotifications;
177197
};

packages/notification-services-controller/src/NotificationServicesController/services/notification-config-cache.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ export const NotificationConfigCacheTTL = 1000 * 60; // 60 seconds
88
export class OnChainNotificationsCache {
99
#cache: NotificationConfigCache | null = null;
1010

11-
readonly #TTL = NotificationConfigCacheTTL;
11+
readonly #ttl = NotificationConfigCacheTTL;
1212

1313
#isExpired(): boolean {
14-
return !this.#cache || Date.now() - this.#cache.timestamp > this.#TTL;
14+
return !this.#cache || Date.now() - this.#cache.timestamp > this.#ttl;
1515
}
1616

1717
#hasAllAddresses(addresses: string[]): boolean {

packages/notification-services-controller/src/NotificationServicesController/services/perp-notifications.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ describe('Perps Service - createPerpOrderNotification', () => {
1414
jest.clearAllMocks();
1515
});
1616

17-
const arrangeMocks = () => {
17+
const arrangeMocks = (): {
18+
consoleErrorSpy: jest.SpyInstance<void, Parameters<typeof console.error>>;
19+
} => {
1820
const consoleErrorSpy = jest
1921
.spyOn(console, 'error')
2022
.mockImplementation(jest.fn());

0 commit comments

Comments
 (0)