Skip to content

Commit

Permalink
feat(config): add type guard for if the config object can be written …
Browse files Browse the repository at this point in the history
…to (#2183)
  • Loading branch information
blacha authored May 10, 2022
1 parent 76b6175 commit 0a00e0e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
18 changes: 18 additions & 0 deletions packages/config/src/base.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,20 @@ export abstract class BasemapsConfigObject<T extends BaseConfig> {
constructor(prefix: ConfigPrefix) {
this.prefix = prefix;
}

/**
* Can this config object be written to with this.put()
* @example
* ```typescript
* if (this.isWriteable()) return this.put(obj)
* ```
*
* @returns true if writeable false otherwise
*/
isWriteable(): this is BaseConfigWriteableObject<T> {
return false;
}

/** Create a prefixed id for a object */
id(name: string): string {
if (name.startsWith(`${this.prefix}_`)) return name;
Expand All @@ -105,4 +119,8 @@ export abstract class BasemapsConfigObject<T extends BaseConfig> {
abstract getAll(id: Set<string>): Promise<Map<string, T>>;
}

export interface BaseConfigWriteableObject<T extends BaseConfig> extends BasemapsConfigObject<T> {
put(record: T): Promise<string>;
}

export const Config = new ConfigInstance();
6 changes: 6 additions & 0 deletions packages/config/src/dynamo/__tests__/config.imagery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ o.spec('ConfigProvider.Imagery', () => {

const item: ConfigImagery = { id: 'im_foo', name: 'abc' } as any;

o('isWriteable', () => {
o(provider.Imagery.isWriteable()).equals(true);
// Validate the typing works
if (Config.Imagery.isWriteable()) o(typeof Config.Imagery.put).equals('function');
});

o('prefix', () => {
o(Config.prefix(ConfigPrefix.Imagery, '1234')).equals('im_1234');
o(Config.prefix(ConfigPrefix.Imagery, 'im_1234')).equals('im_1234');
Expand Down
6 changes: 5 additions & 1 deletion packages/config/src/dynamo/dynamo.config.base.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import DynamoDB from 'aws-sdk/clients/dynamodb.js';
import { BasemapsConfigObject } from '../base.config.js';
import { BaseConfigWriteableObject, BasemapsConfigObject } from '../base.config.js';
import { BaseConfig } from '../config/base.js';
import { ConfigPrefix } from '../config/prefix.js';
import { ConfigProviderDynamo } from './dynamo.config.js';
Expand All @@ -20,6 +20,10 @@ export class ConfigDynamoBase<T extends BaseConfig = BaseConfig> extends Basemap
return this.cfg.dynamo;
}

isWriteable(): this is BaseConfigWriteableObject<T> {
return true;
}

clone(rec: T): T {
return DynamoDB.Converter.unmarshall(DynamoDB.Converter.marshall(rec)) as T;
}
Expand Down

0 comments on commit 0a00e0e

Please sign in to comment.