Skip to content

Commit 8be624a

Browse files
authored
refactor(sw): fix type errors (misskey-dev#14478)
* style(sw): lint fixes * refactor(sw): fix type errors * chore(sw): disable `noImplicitAny` * ci(sw): enable typecheck ci * ci(sw): build `misskey-js` before typecheck
1 parent 3fe7e37 commit 8be624a

File tree

7 files changed

+31
-21
lines changed

7 files changed

+31
-21
lines changed

.github/workflows/lint.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ jobs:
7878
matrix:
7979
workspace:
8080
- backend
81+
- sw
8182
- misskey-js
8283
steps:
8384
- uses: actions/[email protected]
@@ -92,7 +93,7 @@ jobs:
9293
- run: corepack enable
9394
- run: pnpm i --frozen-lockfile
9495
- run: pnpm --filter misskey-js run build
95-
if: ${{ matrix.workspace == 'backend' }}
96+
if: ${{ matrix.workspace == 'backend' || matrix.workspace == 'sw' }}
9697
- run: pnpm --filter misskey-reversi run build
9798
if: ${{ matrix.workspace == 'backend' }}
9899
- run: pnpm --filter ${{ matrix.workspace }} run typecheck

packages/sw/build.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
import { fileURLToPath } from 'node:url';
99
import * as esbuild from 'esbuild';
1010
import locales from '../../locales/index.js';
11-
import meta from '../../package.json' with { type: "json" };
11+
import meta from '../../package.json' with { type: 'json' };
1212
const watch = process.argv[2]?.includes('watch');
1313

14-
const __dirname = fileURLToPath(new URL('.', import.meta.url))
14+
const __dirname = fileURLToPath(new URL('.', import.meta.url));
1515

1616
console.log('Starting SW building...');
1717

packages/sw/src/scripts/create-notification.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ async function composeNotification(data: PushNotificationDataMap[keyof PushNotif
5959
const userDetail = await cli.request('users/show', { userId: data.body.userId }, account.token);
6060
return [i18n.ts._notification.youWereFollowed, {
6161
body: getUserName(data.body.user),
62-
icon: data.body.user.avatarUrl,
62+
icon: data.body.user.avatarUrl ?? undefined,
6363
badge: iconUrl('user-plus'),
6464
data,
6565
actions: userDetail.isFollowing ? [] : [
@@ -74,7 +74,7 @@ async function composeNotification(data: PushNotificationDataMap[keyof PushNotif
7474
case 'mention':
7575
return [i18n.tsx._notification.youGotMention({ name: getUserName(data.body.user) }), {
7676
body: data.body.note.text ?? '',
77-
icon: data.body.user.avatarUrl,
77+
icon: data.body.user.avatarUrl ?? undefined,
7878
badge: iconUrl('at'),
7979
data,
8080
actions: [
@@ -88,7 +88,7 @@ async function composeNotification(data: PushNotificationDataMap[keyof PushNotif
8888
case 'reply':
8989
return [i18n.tsx._notification.youGotReply({ name: getUserName(data.body.user) }), {
9090
body: data.body.note.text ?? '',
91-
icon: data.body.user.avatarUrl,
91+
icon: data.body.user.avatarUrl ?? undefined,
9292
badge: iconUrl('arrow-back-up'),
9393
data,
9494
actions: [
@@ -102,7 +102,7 @@ async function composeNotification(data: PushNotificationDataMap[keyof PushNotif
102102
case 'renote':
103103
return [i18n.tsx._notification.youRenoted({ name: getUserName(data.body.user) }), {
104104
body: data.body.note.text ?? '',
105-
icon: data.body.user.avatarUrl,
105+
icon: data.body.user.avatarUrl ?? undefined,
106106
badge: iconUrl('repeat'),
107107
data,
108108
actions: [
@@ -116,7 +116,7 @@ async function composeNotification(data: PushNotificationDataMap[keyof PushNotif
116116
case 'quote':
117117
return [i18n.tsx._notification.youGotQuote({ name: getUserName(data.body.user) }), {
118118
body: data.body.note.text ?? '',
119-
icon: data.body.user.avatarUrl,
119+
icon: data.body.user.avatarUrl ?? undefined,
120120
badge: iconUrl('quote'),
121121
data,
122122
actions: [
@@ -136,7 +136,7 @@ async function composeNotification(data: PushNotificationDataMap[keyof PushNotif
136136
case 'note':
137137
return [i18n.ts._notification.newNote + ': ' + getUserName(data.body.user), {
138138
body: data.body.note.text ?? '',
139-
icon: data.body.user.avatarUrl,
139+
icon: data.body.user.avatarUrl ?? undefined,
140140
data,
141141
}];
142142

@@ -163,7 +163,7 @@ async function composeNotification(data: PushNotificationDataMap[keyof PushNotif
163163
const tag = `reaction:${data.body.note.id}`;
164164
return [`${reaction} ${getUserName(data.body.user)}`, {
165165
body: data.body.note.text ?? '',
166-
icon: data.body.user.avatarUrl,
166+
icon: data.body.user.avatarUrl ?? undefined,
167167
tag,
168168
badge,
169169
data,
@@ -179,7 +179,7 @@ async function composeNotification(data: PushNotificationDataMap[keyof PushNotif
179179
case 'receiveFollowRequest':
180180
return [i18n.ts._notification.youReceivedFollowRequest, {
181181
body: getUserName(data.body.user),
182-
icon: data.body.user.avatarUrl,
182+
icon: data.body.user.avatarUrl ?? undefined,
183183
badge: iconUrl('user-plus'),
184184
data,
185185
actions: [
@@ -197,7 +197,7 @@ async function composeNotification(data: PushNotificationDataMap[keyof PushNotif
197197
case 'followRequestAccepted':
198198
return [i18n.ts._notification.yourFollowRequestAccepted, {
199199
body: getUserName(data.body.user),
200-
icon: data.body.user.avatarUrl,
200+
icon: data.body.user.avatarUrl ?? undefined,
201201
badge: iconUrl('circle-check'),
202202
data,
203203
}];
@@ -237,7 +237,7 @@ async function composeNotification(data: PushNotificationDataMap[keyof PushNotif
237237
case 'unreadAntennaNote':
238238
return [i18n.tsx._notification.unreadAntennaNote({ name: data.body.antenna.name }), {
239239
body: `${getUserName(data.body.note.user)}: ${data.body.note.text ?? ''}`,
240-
icon: data.body.note.user.avatarUrl,
240+
icon: data.body.note.user.avatarUrl ?? undefined,
241241
badge: iconUrl('antenna'),
242242
tag: `antenna:${data.body.antenna.id}`,
243243
data,

packages/sw/src/scripts/get-account-from-id.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
*/
55

66
import { get } from 'idb-keyval';
7+
import * as Misskey from 'misskey-js';
78

8-
export async function getAccountFromId(id: string): Promise<{ token: string; id: string } | void> {
9-
const accounts = await get<{ token: string; id: string }[]>('accounts');
9+
export async function getAccountFromId(id: string): Promise<Pick<Misskey.entities.SignupResponse, 'id' | 'token'> | undefined> {
10+
const accounts = await get<Pick<Misskey.entities.SignupResponse, 'id' | 'token'>[]>('accounts');
1011
if (!accounts) {
1112
console.log('Accounts are not recorded');
1213
return;

packages/sw/src/scripts/operations.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,22 @@ import { getUrlWithLoginId } from '@/scripts/login-id.js';
1414

1515
export const cli = new Misskey.api.APIClient({ origin, fetch: (...args): Promise<Response> => fetch(...args) });
1616

17-
export async function api<E extends keyof Misskey.Endpoints, O extends Misskey.Endpoints[E]['req']>(endpoint: E, userId?: string, options?: O): Promise<void | ReturnType<typeof cli.request<E, O>>> {
18-
let account: { token: string; id: string } | void = undefined;
17+
export async function api<
18+
E extends keyof Misskey.Endpoints,
19+
P extends Misskey.Endpoints[E]['req']
20+
>(endpoint: E, userId?: string, params?: P): Promise<Misskey.api.SwitchCaseResponseType<E, P> | undefined> {
21+
let account: Pick<Misskey.entities.SignupResponse, 'id' | 'token'> | undefined;
1922

2023
if (userId) {
2124
account = await getAccountFromId(userId);
2225
if (!account) return;
2326
}
2427

25-
return cli.request(endpoint, options, account?.token);
28+
return (cli.request as <E extends keyof Misskey.Endpoints, P extends Misskey.Endpoints[E]['req']>(
29+
endpoint: E,
30+
params: P,
31+
credential?: string | null,
32+
) => Promise<Misskey.api.SwitchCaseResponseType<E, P>>)(endpoint, params, account?.token);
2633
}
2734

2835
// mark-all-as-read送出を1秒間隔に制限する
@@ -33,7 +40,7 @@ export function sendMarkAllAsRead(userId: string): Promise<null | undefined | vo
3340
return new Promise(resolve => {
3441
setTimeout(() => {
3542
readBlockingStatus.set(userId, false);
36-
api('notifications/mark-all-as-read', userId).then(resolve, resolve);
43+
(api('notifications/mark-all-as-read', userId) as Promise<void>).then(resolve, resolve);
3744
}, 1000);
3845
});
3946
}

packages/sw/src/sw.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ globalThis.addEventListener('notificationclick', (ev: ServiceWorkerGlobalScopeEv
160160
case 'markAllAsRead':
161161
await globalThis.registration.getNotifications()
162162
.then(notifications => notifications.forEach(n => n.tag !== 'read_notification' && n.close()));
163-
await get('accounts').then(accounts => {
164-
return Promise.all(accounts.map(async account => {
163+
await get<Pick<Misskey.entities.SignupResponse, 'id' | 'token'>[]>('accounts').then(accounts => {
164+
return Promise.all((accounts ?? []).map(async account => {
165165
await swos.sendMarkAllAsRead(account.id);
166166
}));
167167
});

packages/sw/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"compilerOptions": {
33
"allowJs": true,
44
"noEmitOnError": false,
5+
"noImplicitAny": false,
56
"noImplicitReturns": true,
67
"noUnusedParameters": false,
78
"noUnusedLocals": true,

0 commit comments

Comments
 (0)