Skip to content

Commit

Permalink
Update type of options parameter in getByQuery method
Browse files Browse the repository at this point in the history
  • Loading branch information
anpigon committed Mar 11, 2024
1 parent ccf452b commit 0b586e7
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 68 deletions.
2 changes: 1 addition & 1 deletion src/apis/base_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { GoogleBooksApi } from './google_books_api';
import { NaverBooksApi } from './naver_books_api';

export interface BaseBooksApiImpl {
getByQuery(query: string, options?: any): Promise<Book[]>;
getByQuery(query: string, options?: Record<string, string>): Promise<Book[]>;
}

class ConfigurationError extends Error {
Expand Down
4 changes: 2 additions & 2 deletions src/apis/google_books_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class GoogleBooksApi implements BaseBooksApiImpl {
return this.localePreference === 'default' ? window.moment.locale() : this.localePreference;
}

private buildSearchParams(query: string, options?: any): Record<string, string | number> {
private buildSearchParams(query: string, options?: Record<string, string>): Record<string, string | number> {
const params: Record<string, string | number> = {
q: query,
maxResults: GoogleBooksApi.MAX_RESULTS,
Expand All @@ -27,7 +27,7 @@ export class GoogleBooksApi implements BaseBooksApiImpl {
return params;
}

async getByQuery(query: string, options?: any): Promise<Book[]> {
async getByQuery(query: string, options?: Record<string, string>): Promise<Book[]> {
try {
const params = this.buildSearchParams(query, options);
const searchResults = await apiGet<GoogleBooksResponse>('https://www.googleapis.com/books/v1/volumes', params);
Expand Down
140 changes: 75 additions & 65 deletions src/settings/settings.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { App, PluginSettingTab, Setting, Notice } from 'obsidian';
import { replaceDateInString } from '@utils/utils';
import { App, Notice, PluginSettingTab, Setting } from 'obsidian';

import { ServiceProvider } from '@src/constants';
import languages from '@utils/languages';
import { SettingServiceProviderModal } from '@views/setting_service_provider_modal';
import BookSearchPlugin from '../main';
import { FileNameFormatSuggest } from './suggesters/FileNameFormatSuggester';
import { FolderSuggest } from './suggesters/FolderSuggester';
import { FileSuggest } from './suggesters/FileSuggester';
import { ServiceProvider } from '@src/constants';
import { SettingServiceProviderModal } from '@views/setting_service_provider_modal';
import languages from '@utils/languages';
import { FolderSuggest } from './suggesters/FolderSuggester';

const docUrl = 'https://github.com/anpigon/obsidian-book-search-plugin';

Expand Down Expand Up @@ -59,28 +59,37 @@ export class BookSearchSettingTab extends PluginSettingTab {
super(app, plugin);
}

get settings() {
return this.plugin.settings;
private createGeneralSettings(containerEl) {
this.createHeader('General Settings', containerEl);
this.createFileLocationSetting(containerEl);
this.createFileNameFormatSetting(containerEl);
}

display(): void {
const { containerEl } = this;

containerEl.empty();

containerEl.classList.add('book-search-plugin__settings');
private createHeader(title, containerEl) {
const header = document.createDocumentFragment();
header.createEl('h2', { text: title });
return new Setting(containerEl).setHeading().setName(header);
}

createHeader(containerEl, 'General Settings');
private createFoldingHeader(containerEl: HTMLElement, title: string, formatterSettingsChildren: Setting[]) {
return this.createHeader(title, containerEl).addToggle(toggle => {
toggle.onChange(checked => {
formatterSettingsChildren.forEach(({ settingEl }) => {
settingEl.toggleClass('book-search-plugin__show', checked);
});
});
});
}

// New file location
private createFileLocationSetting(containerEl) {
new Setting(containerEl)
.setName('New file location')
.setDesc('New book notes will be placed here.')
.addSearch(cb => {
try {
new FolderSuggest(this.app, cb.inputEl);
} catch {
// eslint-disable
} catch (e) {
console.error(e); // Improved error handling
}
cb.setPlaceholder('Example: folder1/folder2')
.setValue(this.plugin.settings.folder)
Expand All @@ -89,8 +98,9 @@ export class BookSearchSettingTab extends PluginSettingTab {
this.plugin.saveSettings();
});
});
}

// New File Name
private createFileNameFormatSetting(containerEl) {
const newFileNameHint = document.createDocumentFragment().createEl('code', {
text: replaceDateInString(this.plugin.settings.fileNameFormat) || '{{title}} - {{author}}',
});
Expand All @@ -101,8 +111,8 @@ export class BookSearchSettingTab extends PluginSettingTab {
.addSearch(cb => {
try {
new FileNameFormatSuggest(this.app, cb.inputEl);
} catch {
// eslint-disable
} catch (e) {
console.error(e); // Improved error handling
}
cb.setPlaceholder('Example: {{title}} - {{author}}')
.setValue(this.plugin.settings.fileNameFormat)
Expand All @@ -118,8 +128,36 @@ export class BookSearchSettingTab extends PluginSettingTab {
cls: ['setting-item-description', 'book-search-plugin__settings--new_file_name_hint'],
})
.append(newFileNameHint);
}

private createAPIKeySettings(containerEl: HTMLElement) {
const APISettingsChildren: Setting[] = [];
this.createFoldingHeader(containerEl, 'Google API Settings', APISettingsChildren);
let tempKeyValue = '';
APISettingsChildren.push(
new Setting(containerEl)
.setClass('book-search-plugin__hide')
.setName('Google Book API Key')
.setDesc(
'Add your Books API key. **WARNING** please use this field after you must understand Google Cloud API, such as API key security.',
)
.addText(text => {
text.inputEl.type = 'password';
text.setValue(this.plugin.settings.apiKey).onChange(async value => {
tempKeyValue = value;
});
})
.addButton(button => {
button.setButtonText('Save Key').onClick(async () => {
this.plugin.settings.apiKey = tempKeyValue;
await this.plugin.saveSettings();
new Notice('Apikey Saved');
});
}),
);
}

// Template file
private createTemplateFileSetting(containerEl: HTMLElement) {
const templateFileDesc = document.createDocumentFragment();
templateFileDesc.createDiv({ text: 'Files will be available as templates.' });
templateFileDesc.createEl('a', {
Expand All @@ -142,6 +180,15 @@ export class BookSearchSettingTab extends PluginSettingTab {
this.plugin.saveSettings();
});
});
}

display(): void {
const { containerEl } = this;
containerEl.empty();
containerEl.classList.add('book-search-plugin__settings');

this.createGeneralSettings(containerEl);
this.createTemplateFileSetting(containerEl);

// Service Provider
let serviceProviderExtraSettingButton: HTMLElement;
Expand All @@ -163,7 +210,9 @@ export class BookSearchSettingTab extends PluginSettingTab {
preferredLocaleDropdownSetting.settingEl.removeClass('book-search-plugin__hide');
}
};
const toggleServiceProviderExtraSettings = (serviceProvider: ServiceProvider = this.settings?.serviceProvider) => {
const toggleServiceProviderExtraSettings = (
serviceProvider: ServiceProvider = this.plugin.settings?.serviceProvider,
) => {
if (serviceProvider === ServiceProvider.naver) {
showServiceProviderExtraSettingButton();
hideServiceProviderExtraSettingDropdown();
Expand All @@ -183,7 +232,7 @@ export class BookSearchSettingTab extends PluginSettingTab {
dropDown.onChange(async value => {
const newValue = value as ServiceProvider;
toggleServiceProviderExtraSettings(newValue);
this.settings['serviceProvider'] = newValue;
this.plugin.settings['serviceProvider'] = newValue;
await this.plugin.saveSettings();
});
})
Expand All @@ -208,15 +257,15 @@ export class BookSearchSettingTab extends PluginSettingTab {
const localeName = languages[locale];
if (localeName) dropDown.addOption(locale, localeName);
});
const setValue = this.settings.localePreference;
const setValue = this.plugin.settings.localePreference;
if (setValue === 'default') {
dropDown.setValue(defaultLocale);
} else {
dropDown.setValue(setValue);
}
dropDown.onChange(async value => {
const newValue = value;
this.settings.localePreference = newValue;
this.plugin.settings.localePreference = newValue;
await this.plugin.saveSettings();
});
});
Expand Down Expand Up @@ -269,45 +318,6 @@ export class BookSearchSettingTab extends PluginSettingTab {
});

// API Settings
const APISettingsChildren: Setting[] = [];
createFoldingHeader(containerEl, 'Google API Settings', APISettingsChildren);
let tempKeyValue = '';
APISettingsChildren.push(
new Setting(containerEl)
.setClass('book-search-plugin__hide')
.setName('Google Book API Key')
.setDesc(
'Add your Books API key. **WARNING** please use this field after you must understand Google Cloud API, such as API key security.',
)
.addText(text => {
text.inputEl.type = 'password';
text.setValue(this.plugin.settings.apiKey).onChange(async value => {
tempKeyValue = value;
});
})
.addButton(button => {
button.setButtonText('Save Key').onClick(async () => {
this.plugin.settings.apiKey = tempKeyValue;
await this.plugin.saveSettings();
new Notice('Apikey Saved');
});
}),
);
this.createAPIKeySettings(containerEl);
}
}

function createHeader(containerEl: HTMLElement, title: string) {
const titleEl = document.createDocumentFragment();
titleEl.createEl('h2', { text: title });
return new Setting(containerEl).setHeading().setName(titleEl);
}

function createFoldingHeader(containerEl: HTMLElement, title: string, formatterSettingsChildren: Setting[]) {
return createHeader(containerEl, title).addToggle(toggle => {
toggle.onChange(checked => {
formatterSettingsChildren.forEach(({ settingEl }) => {
settingEl.toggleClass('book-search-plugin__show', checked);
});
});
});
}

0 comments on commit 0b586e7

Please sign in to comment.