refactor(sw): fix type errors (#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
This commit is contained in:
zyoshoka 2024-08-30 15:53:04 +09:00 committed by GitHub
parent 3fe7e37f10
commit 8be624aa44
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 31 additions and 21 deletions

View file

@ -78,6 +78,7 @@ jobs:
matrix: matrix:
workspace: workspace:
- backend - backend
- sw
- misskey-js - misskey-js
steps: steps:
- uses: actions/checkout@v4.1.1 - uses: actions/checkout@v4.1.1
@ -92,7 +93,7 @@ jobs:
- run: corepack enable - run: corepack enable
- run: pnpm i --frozen-lockfile - run: pnpm i --frozen-lockfile
- run: pnpm --filter misskey-js run build - run: pnpm --filter misskey-js run build
if: ${{ matrix.workspace == 'backend' }} if: ${{ matrix.workspace == 'backend' || matrix.workspace == 'sw' }}
- run: pnpm --filter misskey-reversi run build - run: pnpm --filter misskey-reversi run build
if: ${{ matrix.workspace == 'backend' }} if: ${{ matrix.workspace == 'backend' }}
- run: pnpm --filter ${{ matrix.workspace }} run typecheck - run: pnpm --filter ${{ matrix.workspace }} run typecheck

View file

@ -8,10 +8,10 @@
import { fileURLToPath } from 'node:url'; import { fileURLToPath } from 'node:url';
import * as esbuild from 'esbuild'; import * as esbuild from 'esbuild';
import locales from '../../locales/index.js'; import locales from '../../locales/index.js';
import meta from '../../package.json' with { type: "json" }; import meta from '../../package.json' with { type: 'json' };
const watch = process.argv[2]?.includes('watch'); const watch = process.argv[2]?.includes('watch');
const __dirname = fileURLToPath(new URL('.', import.meta.url)) const __dirname = fileURLToPath(new URL('.', import.meta.url));
console.log('Starting SW building...'); console.log('Starting SW building...');

View file

@ -59,7 +59,7 @@ async function composeNotification(data: PushNotificationDataMap[keyof PushNotif
const userDetail = await cli.request('users/show', { userId: data.body.userId }, account.token); const userDetail = await cli.request('users/show', { userId: data.body.userId }, account.token);
return [i18n.ts._notification.youWereFollowed, { return [i18n.ts._notification.youWereFollowed, {
body: getUserName(data.body.user), body: getUserName(data.body.user),
icon: data.body.user.avatarUrl, icon: data.body.user.avatarUrl ?? undefined,
badge: iconUrl('user-plus'), badge: iconUrl('user-plus'),
data, data,
actions: userDetail.isFollowing ? [] : [ actions: userDetail.isFollowing ? [] : [
@ -74,7 +74,7 @@ async function composeNotification(data: PushNotificationDataMap[keyof PushNotif
case 'mention': case 'mention':
return [i18n.tsx._notification.youGotMention({ name: getUserName(data.body.user) }), { return [i18n.tsx._notification.youGotMention({ name: getUserName(data.body.user) }), {
body: data.body.note.text ?? '', body: data.body.note.text ?? '',
icon: data.body.user.avatarUrl, icon: data.body.user.avatarUrl ?? undefined,
badge: iconUrl('at'), badge: iconUrl('at'),
data, data,
actions: [ actions: [
@ -88,7 +88,7 @@ async function composeNotification(data: PushNotificationDataMap[keyof PushNotif
case 'reply': case 'reply':
return [i18n.tsx._notification.youGotReply({ name: getUserName(data.body.user) }), { return [i18n.tsx._notification.youGotReply({ name: getUserName(data.body.user) }), {
body: data.body.note.text ?? '', body: data.body.note.text ?? '',
icon: data.body.user.avatarUrl, icon: data.body.user.avatarUrl ?? undefined,
badge: iconUrl('arrow-back-up'), badge: iconUrl('arrow-back-up'),
data, data,
actions: [ actions: [
@ -102,7 +102,7 @@ async function composeNotification(data: PushNotificationDataMap[keyof PushNotif
case 'renote': case 'renote':
return [i18n.tsx._notification.youRenoted({ name: getUserName(data.body.user) }), { return [i18n.tsx._notification.youRenoted({ name: getUserName(data.body.user) }), {
body: data.body.note.text ?? '', body: data.body.note.text ?? '',
icon: data.body.user.avatarUrl, icon: data.body.user.avatarUrl ?? undefined,
badge: iconUrl('repeat'), badge: iconUrl('repeat'),
data, data,
actions: [ actions: [
@ -116,7 +116,7 @@ async function composeNotification(data: PushNotificationDataMap[keyof PushNotif
case 'quote': case 'quote':
return [i18n.tsx._notification.youGotQuote({ name: getUserName(data.body.user) }), { return [i18n.tsx._notification.youGotQuote({ name: getUserName(data.body.user) }), {
body: data.body.note.text ?? '', body: data.body.note.text ?? '',
icon: data.body.user.avatarUrl, icon: data.body.user.avatarUrl ?? undefined,
badge: iconUrl('quote'), badge: iconUrl('quote'),
data, data,
actions: [ actions: [
@ -136,7 +136,7 @@ async function composeNotification(data: PushNotificationDataMap[keyof PushNotif
case 'note': case 'note':
return [i18n.ts._notification.newNote + ': ' + getUserName(data.body.user), { return [i18n.ts._notification.newNote + ': ' + getUserName(data.body.user), {
body: data.body.note.text ?? '', body: data.body.note.text ?? '',
icon: data.body.user.avatarUrl, icon: data.body.user.avatarUrl ?? undefined,
data, data,
}]; }];
@ -163,7 +163,7 @@ async function composeNotification(data: PushNotificationDataMap[keyof PushNotif
const tag = `reaction:${data.body.note.id}`; const tag = `reaction:${data.body.note.id}`;
return [`${reaction} ${getUserName(data.body.user)}`, { return [`${reaction} ${getUserName(data.body.user)}`, {
body: data.body.note.text ?? '', body: data.body.note.text ?? '',
icon: data.body.user.avatarUrl, icon: data.body.user.avatarUrl ?? undefined,
tag, tag,
badge, badge,
data, data,
@ -179,7 +179,7 @@ async function composeNotification(data: PushNotificationDataMap[keyof PushNotif
case 'receiveFollowRequest': case 'receiveFollowRequest':
return [i18n.ts._notification.youReceivedFollowRequest, { return [i18n.ts._notification.youReceivedFollowRequest, {
body: getUserName(data.body.user), body: getUserName(data.body.user),
icon: data.body.user.avatarUrl, icon: data.body.user.avatarUrl ?? undefined,
badge: iconUrl('user-plus'), badge: iconUrl('user-plus'),
data, data,
actions: [ actions: [
@ -197,7 +197,7 @@ async function composeNotification(data: PushNotificationDataMap[keyof PushNotif
case 'followRequestAccepted': case 'followRequestAccepted':
return [i18n.ts._notification.yourFollowRequestAccepted, { return [i18n.ts._notification.yourFollowRequestAccepted, {
body: getUserName(data.body.user), body: getUserName(data.body.user),
icon: data.body.user.avatarUrl, icon: data.body.user.avatarUrl ?? undefined,
badge: iconUrl('circle-check'), badge: iconUrl('circle-check'),
data, data,
}]; }];
@ -237,7 +237,7 @@ async function composeNotification(data: PushNotificationDataMap[keyof PushNotif
case 'unreadAntennaNote': case 'unreadAntennaNote':
return [i18n.tsx._notification.unreadAntennaNote({ name: data.body.antenna.name }), { return [i18n.tsx._notification.unreadAntennaNote({ name: data.body.antenna.name }), {
body: `${getUserName(data.body.note.user)}: ${data.body.note.text ?? ''}`, body: `${getUserName(data.body.note.user)}: ${data.body.note.text ?? ''}`,
icon: data.body.note.user.avatarUrl, icon: data.body.note.user.avatarUrl ?? undefined,
badge: iconUrl('antenna'), badge: iconUrl('antenna'),
tag: `antenna:${data.body.antenna.id}`, tag: `antenna:${data.body.antenna.id}`,
data, data,

View file

@ -4,9 +4,10 @@
*/ */
import { get } from 'idb-keyval'; import { get } from 'idb-keyval';
import * as Misskey from 'misskey-js';
export async function getAccountFromId(id: string): Promise<{ token: string; id: string } | void> { export async function getAccountFromId(id: string): Promise<Pick<Misskey.entities.SignupResponse, 'id' | 'token'> | undefined> {
const accounts = await get<{ token: string; id: string }[]>('accounts'); const accounts = await get<Pick<Misskey.entities.SignupResponse, 'id' | 'token'>[]>('accounts');
if (!accounts) { if (!accounts) {
console.log('Accounts are not recorded'); console.log('Accounts are not recorded');
return; return;

View file

@ -14,15 +14,22 @@ import { getUrlWithLoginId } from '@/scripts/login-id.js';
export const cli = new Misskey.api.APIClient({ origin, fetch: (...args): Promise<Response> => fetch(...args) }); export const cli = new Misskey.api.APIClient({ origin, fetch: (...args): Promise<Response> => fetch(...args) });
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>>> { export async function api<
let account: { token: string; id: string } | void = undefined; E extends keyof Misskey.Endpoints,
P extends Misskey.Endpoints[E]['req']
>(endpoint: E, userId?: string, params?: P): Promise<Misskey.api.SwitchCaseResponseType<E, P> | undefined> {
let account: Pick<Misskey.entities.SignupResponse, 'id' | 'token'> | undefined;
if (userId) { if (userId) {
account = await getAccountFromId(userId); account = await getAccountFromId(userId);
if (!account) return; if (!account) return;
} }
return cli.request(endpoint, options, account?.token); return (cli.request as <E extends keyof Misskey.Endpoints, P extends Misskey.Endpoints[E]['req']>(
endpoint: E,
params: P,
credential?: string | null,
) => Promise<Misskey.api.SwitchCaseResponseType<E, P>>)(endpoint, params, account?.token);
} }
// mark-all-as-read送出を1秒間隔に制限する // mark-all-as-read送出を1秒間隔に制限する
@ -33,7 +40,7 @@ export function sendMarkAllAsRead(userId: string): Promise<null | undefined | vo
return new Promise(resolve => { return new Promise(resolve => {
setTimeout(() => { setTimeout(() => {
readBlockingStatus.set(userId, false); readBlockingStatus.set(userId, false);
api('notifications/mark-all-as-read', userId).then(resolve, resolve); (api('notifications/mark-all-as-read', userId) as Promise<void>).then(resolve, resolve);
}, 1000); }, 1000);
}); });
} }

View file

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

View file

@ -2,6 +2,7 @@
"compilerOptions": { "compilerOptions": {
"allowJs": true, "allowJs": true,
"noEmitOnError": false, "noEmitOnError": false,
"noImplicitAny": false,
"noImplicitReturns": true, "noImplicitReturns": true,
"noUnusedParameters": false, "noUnusedParameters": false,
"noUnusedLocals": true, "noUnusedLocals": true,