mirror of
https://github.com/vrtmrz/obsidian-livesync.git
synced 2026-07-24 05:22:58 +00:00
Reduce Community lint type-safety warnings
This commit is contained in:
@@ -52,7 +52,8 @@ export const _OpenKeyValueDatabase = async (dbKey: string): Promise<KeyValueData
|
||||
db = await _openDB();
|
||||
databaseCache[dbKey] = db;
|
||||
}
|
||||
return await db.get(storeKey, key);
|
||||
const value: unknown = await db.get(storeKey, key);
|
||||
return value as T;
|
||||
},
|
||||
async set<T>(key: IDBValidKey, value: T) {
|
||||
if (!db) {
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
type CouchDBConfigurationSection = Record<string, string | undefined>;
|
||||
|
||||
export interface CouchDBConfiguration {
|
||||
admins: CouchDBConfigurationSection;
|
||||
chttpd: CouchDBConfigurationSection;
|
||||
chttpd_auth: CouchDBConfigurationSection;
|
||||
couchdb: CouchDBConfigurationSection;
|
||||
cors: CouchDBConfigurationSection;
|
||||
httpd: CouchDBConfigurationSection;
|
||||
}
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null;
|
||||
}
|
||||
|
||||
function normaliseSection(value: unknown): CouchDBConfigurationSection {
|
||||
if (!isRecord(value)) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const entries: Array<[string, string]> = [];
|
||||
for (const [key, entry] of Object.entries(value)) {
|
||||
if (typeof entry === "string") {
|
||||
entries.push([key, entry]);
|
||||
}
|
||||
}
|
||||
return Object.fromEntries(entries);
|
||||
}
|
||||
|
||||
export function normaliseCouchDBConfiguration(value: unknown): CouchDBConfiguration {
|
||||
const source = isRecord(value) ? value : {};
|
||||
return {
|
||||
admins: normaliseSection(source.admins),
|
||||
chttpd: normaliseSection(source.chttpd),
|
||||
chttpd_auth: normaliseSection(source.chttpd_auth),
|
||||
couchdb: normaliseSection(source.couchdb),
|
||||
cors: normaliseSection(source.cors),
|
||||
httpd: normaliseSection(source.httpd),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
import type { App, PluginManifest } from "@/deps.ts";
|
||||
|
||||
export interface ObsidianCommunityPluginManager {
|
||||
enabledPlugins: ReadonlySet<string>;
|
||||
manifests: PluginManifest[];
|
||||
loadPlugin(pluginId: string): Promise<void>;
|
||||
unloadPlugin(pluginId: string): Promise<void>;
|
||||
}
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null;
|
||||
}
|
||||
|
||||
function isPluginManifest(value: unknown): value is PluginManifest {
|
||||
return (
|
||||
isRecord(value) &&
|
||||
typeof value.id === "string" &&
|
||||
typeof value.name === "string" &&
|
||||
(value.dir === undefined || typeof value.dir === "string")
|
||||
);
|
||||
}
|
||||
|
||||
function isStringSet(value: unknown): value is ReadonlySet<string> {
|
||||
if (!(value instanceof Set)) {
|
||||
return false;
|
||||
}
|
||||
const entries = value as ReadonlySet<unknown>;
|
||||
for (const entry of entries) {
|
||||
if (typeof entry !== "string") {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
async function invokeLifecycleMethod(
|
||||
manager: Record<string, unknown>,
|
||||
methodName: "loadPlugin" | "unloadPlugin",
|
||||
pluginId: string
|
||||
): Promise<void> {
|
||||
const method = manager[methodName];
|
||||
if (typeof method !== "function") {
|
||||
throw new TypeError(`Obsidian does not expose ${methodName}`);
|
||||
}
|
||||
const result: unknown = Reflect.apply(method, manager, [pluginId]);
|
||||
await result;
|
||||
}
|
||||
|
||||
export function getObsidianCommunityPluginManager(app: App): ObsidianCommunityPluginManager {
|
||||
const managerValue: unknown = Reflect.get(app, "plugins");
|
||||
if (!isRecord(managerValue) || !isRecord(managerValue.manifests) || !isStringSet(managerValue.enabledPlugins)) {
|
||||
throw new TypeError("Obsidian does not expose the community plug-in manager");
|
||||
}
|
||||
|
||||
const manifests = Object.values(managerValue.manifests).filter(isPluginManifest);
|
||||
return {
|
||||
enabledPlugins: managerValue.enabledPlugins,
|
||||
manifests,
|
||||
loadPlugin: async (pluginId) => await invokeLifecycleMethod(managerValue, "loadPlugin", pluginId),
|
||||
unloadPlugin: async (pluginId) => await invokeLifecycleMethod(managerValue, "unloadPlugin", pluginId),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import type { App } from "@/deps.ts";
|
||||
|
||||
function getSettingsManager(app: App): Record<string, unknown> {
|
||||
const manager: unknown = Reflect.get(app, "setting");
|
||||
if (typeof manager !== "object" || manager === null) {
|
||||
throw new TypeError("Obsidian does not expose the settings manager");
|
||||
}
|
||||
return manager as Record<string, unknown>;
|
||||
}
|
||||
|
||||
function invokeSettingsMethod(app: App, methodName: string, args: unknown[] = []): void {
|
||||
const manager = getSettingsManager(app);
|
||||
const method = manager[methodName];
|
||||
if (typeof method !== "function") {
|
||||
throw new TypeError(`Obsidian does not expose settings.${methodName}`);
|
||||
}
|
||||
Reflect.apply(method, manager, args);
|
||||
}
|
||||
|
||||
export function openObsidianSettings(app: App, tabId: string): void {
|
||||
invokeSettingsMethod(app, "open");
|
||||
invokeSettingsMethod(app, "openTabById", [tabId]);
|
||||
}
|
||||
|
||||
export function closeObsidianSettings(app: App): void {
|
||||
invokeSettingsMethod(app, "close");
|
||||
}
|
||||
Reference in New Issue
Block a user