Skip to content

Commit

Permalink
fix(elements): 🐛 validate databaseId before retrieving database
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNZL committed Jul 17, 2022
1 parent 060dbe0 commit 85618aa
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/options/PropertySelects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { NotionClient } from '../apis/notion';
import { Storage } from '../apis/storage';

import { SavedFields } from '.';
import { InputFieldValidator } from './validator';
import { CONFIGURATION, SupportedTypes } from './configuration';

import { Select } from '../elements';
Expand Down Expand Up @@ -61,13 +62,13 @@ export class PropertySelect extends Select {
export class SelectPropertyValueSelect extends PropertySelect {
private propertySelect: PropertySelect;

protected constructor(id: string, type: PropertySelect['type'], fieldKey: PropertySelect['fieldKey'], getDatabaseId: () => SupportedTypes, propertySelect: PropertySelect) {
protected constructor(id: string, type: PropertySelect['type'], fieldKey: PropertySelect['fieldKey'], getDatabaseId: () => Promise<SupportedTypes | typeof InputFieldValidator.INVALID_INPUT>, propertySelect: PropertySelect) {
super(id, type, fieldKey);

this.propertySelect = propertySelect;

this.propertySelect.addEventListener('input', async () => {
const databaseId = getDatabaseId();
const databaseId = await getDatabaseId();
if (typeof databaseId !== 'string') return;

const accessToken = (await Storage.getNotionAuthorisation()).accessToken ?? await CONFIGURATION.FIELDS['notion.accessToken'].input.validate(true);
Expand All @@ -80,7 +81,7 @@ export class SelectPropertyValueSelect extends PropertySelect {
});
}

public static override getInstance<T extends string>(id: T, type?: PropertySelect['type'], fieldKey?: PropertySelect['fieldKey'], getDatabaseId?: () => SupportedTypes, propertySelect?: PropertySelect): PropertySelect {
public static override getInstance<T extends string>(id: T, type?: PropertySelect['type'], fieldKey?: PropertySelect['fieldKey'], getDatabaseId?: () => Promise<SupportedTypes | typeof InputFieldValidator.INVALID_INPUT>, propertySelect?: PropertySelect): PropertySelect {
if (!type) throw new Error('Argument type must be defined for class SelectPropertyValueSelect!');
if (!fieldKey) throw new Error('Argument fieldKey must be defined for class SelectPropertyValueSelect!');
if (!getDatabaseId) throw new Error('Argument getDatabaseId must be defined for class SelectPropertyValueSelect!');
Expand Down
2 changes: 1 addition & 1 deletion src/options/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ const DatabaseSelect = <const>{
},
propertyValueSelects: {
get categoryCanvas() {
return SelectPropertyValueSelect.getInstance<OptionsSelectId>('notion-category-canvas', 'select', 'notion.propertyValues.categoryCanvas', DatabaseSelect.element.getValue.bind(DatabaseSelect.element), DatabaseSelect.propertySelects.category);
return SelectPropertyValueSelect.getInstance<OptionsSelectId>('notion-category-canvas', 'select', 'notion.propertyValues.categoryCanvas', DatabaseSelect.element.validate.bind(DatabaseSelect.element), DatabaseSelect.propertySelects.category);
},
},

Expand Down

0 comments on commit 85618aa

Please sign in to comment.