Files
CommunitySolidServer/src/storage/RoutingResourceStore.ts
Arthur Joppart e0954cf2a7 feat: Rework ResourceStore to return extra info
* feat: change return types in ResourceStore.ts

* feat: change return types in BaseResourceStore.ts

* feat: change return types in LockingResourceStore.ts

* feat: change return types in RoutingResourceStore.ts

* feat: change return types in MonitoringStore.ts

* feat: change return types in PassthroughStore.ts

* feat: change return types in ReadOnlyStore.ts

* feat: change return types in PatchHandler.ts

* feat: change return types in PatchingStore.ts

* feat: change return types in RepresentationPatchHandler.ts

* feat: create createResourceIdentifier() function for convenience

* feat: adapt PostOperationHandler.ts to new typing

* feat: change return types in RepresentationConvertingStore.ts

* feat: adapt DataAccessorBasedStore.ts implementation to new typings

* feat: adapt UnsecureWebSocketsProtocol.ts to new typing

* chore: add temporary comments

* fix: return correct Location header on POST request with slug

* fix: npm run lint command needs more packages

* fix: linting errors

* chore: revert ed9952b

* test: adapt PostOperationHandler tests

* test: adapt UnsecureWebSocketsProtocol tests

* test: adapt DataAccessorBasedStore tests

* fix: linting errors

* feat: emit specific created, deleted, updated events in MonitoringStore

* test: adapt RepresentationPatchHandler tests

* fix: revert UnsecureWebSocketsProtocol changes

* feat: emit extra parameter on changed

* test: adapt MonitoringStore tests

* fix: linting errors

* test: add test to MonitorStore.test for coverage

* fix: linting error

* chore: update doc in ResourceStore.ts

* test: improve MonitoringStore tests

* chore: update RELEASE_NOTES.md

* chore: add extra info about the MonitoringStore to documentation/resource-store.md

* chore: Update RELEASE_NOTES.md

Co-authored-by: Anton Wiklund <ixuz07@gmail.com>

* chore: Update documentation/resource-store.md

Co-authored-by: Anton Wiklund <ixuz07@gmail.com>

* chore: very small changes

* chore: simplify metadata creation

* fix: DataAccessorBasedStore improvement and bugfix

* chore: improve resource-store.md

* chore: adapt MonitoringStore event names, update docs and apply code suggestion

* chore: use ResourceStoreResponse type

* fix: typo

* chore: rename ResourceStoreResponse type to ChangeMap

* chore: adapt .gitignore to name change

Co-authored-by: Anton Wiklund <ixuz07@gmail.com>
2022-07-06 14:40:28 +02:00

69 lines
3.0 KiB
TypeScript

import type { Patch } from '../http/representation/Patch';
import type { Representation } from '../http/representation/Representation';
import type { RepresentationPreferences } from '../http/representation/RepresentationPreferences';
import type { ResourceIdentifier } from '../http/representation/ResourceIdentifier';
import { NotFoundHttpError } from '../util/errors/NotFoundHttpError';
import { NotImplementedHttpError } from '../util/errors/NotImplementedHttpError';
import type { Conditions } from './Conditions';
import type { ResourceStore, ChangeMap } from './ResourceStore';
import type { RouterRule } from './routing/RouterRule';
/**
* Store that routes the incoming request to a specific store based on the stored ResourceRouter.
* In case no store was found for one of the functions that take no data (GET/PATCH/DELETE),
* a 404 will be thrown. In the other cases the error of the router will be thrown (which would probably be 400).
*/
export class RoutingResourceStore implements ResourceStore {
private readonly rule: RouterRule;
public constructor(rule: RouterRule) {
this.rule = rule;
}
public async hasResource(identifier: ResourceIdentifier):
Promise<boolean> {
return (await this.getStore(identifier)).hasResource(identifier);
}
public async getRepresentation(identifier: ResourceIdentifier, preferences: RepresentationPreferences,
conditions?: Conditions): Promise<Representation> {
return (await this.getStore(identifier)).getRepresentation(identifier, preferences, conditions);
}
public async addResource(container: ResourceIdentifier, representation: Representation,
conditions?: Conditions): Promise<ChangeMap> {
return (await this.getStore(container, representation)).addResource(container, representation, conditions);
}
public async setRepresentation(identifier: ResourceIdentifier, representation: Representation,
conditions?: Conditions): Promise<ChangeMap> {
return (await this.getStore(identifier, representation)).setRepresentation(identifier, representation, conditions);
}
public async deleteResource(identifier: ResourceIdentifier,
conditions?: Conditions): Promise<ChangeMap> {
return (await this.getStore(identifier)).deleteResource(identifier, conditions);
}
public async modifyResource(identifier: ResourceIdentifier, patch: Patch,
conditions?: Conditions): Promise<ChangeMap> {
return (await this.getStore(identifier)).modifyResource(identifier, patch, conditions);
}
private async getStore(identifier: ResourceIdentifier, representation?: Representation): Promise<ResourceStore> {
if (representation) {
return this.rule.handleSafe({ identifier, representation });
}
// In case there is no incoming data we want to return 404 if no store was found
try {
return await this.rule.handleSafe({ identifier });
} catch (error: unknown) {
if (NotImplementedHttpError.isInstance(error)) {
throw new NotFoundHttpError('', { cause: error });
}
throw error;
}
}
}