monkeeShark/packages/sw/src/scripts/operations.ts
Kagami Sascha Rosylight cfd7cf0f1e
refactor(sw): Typecheck the service worker (#9314)
* Typecheck the service worker

Currently the service worker build never typechecks, since esbuild and typescript-eslint don't do such job.

esbuild: https://esbuild.github.io/content-types/#typescript

>However, esbuild does not do any type checking so you will still need to run tsc -noEmit in parallel with esbuild to check types. This is not something esbuild does itself.

typescript-eslint: https://typescript-eslint.io/linting/troubleshooting#why-dont-i-see-typescript-errors-in-my-eslint-output

>TypeScript's compiler (or whatever your build chain may be) is specifically designed and built to validate the correctness of your codebase. Our tooling does not reproduce the errors that TypeScript provides, because doing so would slow down the lint run [1], and duplicate the errors that TypeScript already outputs for you.

Adding this step adds tons of TS errors 😱

* Override lib-webworker with service worker
2022-12-12 22:20:35 +09:00

68 lines
2.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Operations
* 各種操作
*/
import * as Misskey from 'misskey-js';
import { SwMessage, swMessageOrderType } from '@/types';
import { acct as getAcct } from '@/filters/user';
import { getAccountFromId } from '@/scripts/get-account-from-id';
import { getUrlWithLoginId } from '@/scripts/login-id';
export const cli = new Misskey.api.APIClient({ origin, fetch: (...args) => fetch(...args) });
export async function api<E extends keyof Misskey.Endpoints>(endpoint: E, userId: string, options?: Misskey.Endpoints[E]['req']) {
const account = await getAccountFromId(userId);
if (!account) return;
return cli.request(endpoint, options, account.token);
}
// rendered acctからユーザーを開く
export function openUser(acct: string, loginId: string) {
return openClient('push', `/@${acct}`, loginId, { acct });
}
// noteIdからートを開く
export function openNote(noteId: string, loginId: string) {
return openClient('push', `/notes/${noteId}`, loginId, { noteId });
}
export async function openChat(body: any, loginId: string) {
if (body.groupId === null) {
return openClient('push', `/my/messaging/${getAcct(body.user)}`, loginId, { body });
} else {
return openClient('push', `/my/messaging/group/${body.groupId}`, loginId, { body });
}
}
// post-formのオプションから投稿フォームを開く
export async function openPost(options: any, loginId: string) {
// クエリを作成しておく
let url = `/share?`;
if (options.initialText) url += `text=${options.initialText}&`;
if (options.reply) url += `replyId=${options.reply.id}&`;
if (options.renote) url += `renoteId=${options.renote.id}&`;
return openClient('post', url, loginId, { options });
}
export async function openClient(order: swMessageOrderType, url: string, loginId: string, query: any = {}) {
const client = await findClient();
if (client) {
client.postMessage({ type: 'order', ...query, order, loginId, url } as SwMessage);
return client;
}
return self.clients.openWindow(getUrlWithLoginId(url, loginId));
}
export async function findClient() {
const clients = await self.clients.matchAll({
type: 'window'
});
for (const c of clients) {
if (c.url.indexOf('?zen') < 0) return c;
}
return null;
}