Skip to content

Commit

Permalink
feat: Add BaseResourceStore.
Browse files Browse the repository at this point in the history
  • Loading branch information
RubenVerborgh committed Jan 8, 2021
1 parent 27a5711 commit 998296a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export * from './storage/routing/RouterRule';

// Storage
export * from './storage/AtomicResourceStore';
export * from './storage/BaseResourceStore';
export * from './storage/Conditions';
export * from './storage/DataAccessorBasedStore';
export * from './storage/LockingResourceStore';
Expand Down
36 changes: 36 additions & 0 deletions src/storage/BaseResourceStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { Patch } from '../ldp/http/Patch';
import type { Representation } from '../ldp/representation/Representation';
import type { RepresentationPreferences } from '../ldp/representation/RepresentationPreferences';
import type { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier';
import { NotImplementedHttpError } from '../util/errors/NotImplementedHttpError';
import type { Conditions } from './Conditions';
import type { ResourceStore } from './ResourceStore';

/**
* Base implementation of ResourceStore for implementers of custom stores.
*/
/* eslint-disable @typescript-eslint/no-unused-vars */
export class BaseResourceStore implements ResourceStore {
public async addResource(container: ResourceIdentifier, representation: Representation,
conditions?: Conditions): Promise<ResourceIdentifier> {
throw new NotImplementedHttpError();
}

public async deleteResource(identifier: ResourceIdentifier, conditions?: Conditions): Promise<void> {
throw new NotImplementedHttpError();
}

public async getRepresentation(identifier: ResourceIdentifier, preferences: RepresentationPreferences,
conditions?: Conditions): Promise<Representation> {
throw new NotImplementedHttpError();
}

public async modifyResource(identifier: ResourceIdentifier, patch: Patch, conditions?: Conditions): Promise<void> {
throw new NotImplementedHttpError();
}

public async setRepresentation(identifier: ResourceIdentifier, representation: Representation,
conditions?: Conditions): Promise<void> {
throw new NotImplementedHttpError();
}
}
27 changes: 27 additions & 0 deletions test/unit/storage/BaseResourceStore.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { BaseResourceStore } from '../../../src/storage/BaseResourceStore';
import { NotImplementedHttpError } from '../../../src/util/errors/NotImplementedHttpError';

const any: any = {};

describe('A BaseResourceStore', (): void => {
const store = new BaseResourceStore();
it('errors on getRepresentation.', async(): Promise<void> => {
await expect(store.getRepresentation(any, any)).rejects.toThrow(NotImplementedHttpError);
});

it('errors on addResource.', async(): Promise<void> => {
await expect(store.addResource(any, any)).rejects.toThrow(NotImplementedHttpError);
});

it('errors on setRepresentation.', async(): Promise<void> => {
await expect(store.setRepresentation(any, any)).rejects.toThrow(NotImplementedHttpError);
});

it('errors on deleteResource.', async(): Promise<void> => {
await expect(store.deleteResource(any, any)).rejects.toThrow(NotImplementedHttpError);
});

it('errors on modifyResource.', async(): Promise<void> => {
await expect(store.modifyResource(any, any)).rejects.toThrow(NotImplementedHttpError);
});
});

3 comments on commit 998296a

@matthieubosquet
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be the typical use case where someone would want to class X extends BaseResourceStore instead of class X implements ResourceStore?

@joachimvh
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably a store that only implements part of the functionality but wants to error in case the function is not supported instead of passing it along (like a PassthroughStore does). But perhaps @RubenVerborgh had a specific case in mind.

@RubenVerborgh
Copy link
Member Author

@RubenVerborgh RubenVerborgh commented on 998296a Jan 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matthieubosquet I've done a couple of commits like these, based on my first experiences as an external module developer. This cuts like 10–15 lines: RubenVerborgh/solid-hue@0ea7ffe#diff-fed75016ea0a947acb8e85047bc43ab685e8dc5a07e15b0ccf510ce957d6224d

The reason this matters is it looks much harder than it was to implement a store. Compare:

And not just the code but also the imports and knowledge. What should I do when I don't support deleteResource? With this base class, you don't even have to care.

Please sign in to comment.