mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Add containsResource utility.
This commit is contained in:
committed by
Joachim Van Herwegen
parent
ee50f40062
commit
af47083f64
@@ -1,6 +1,6 @@
|
||||
import type { Representation } from '../../ldp/representation/Representation';
|
||||
import type { ResourceIdentifier } from '../../ldp/representation/ResourceIdentifier';
|
||||
import { NotFoundHttpError } from '../../util/errors/NotFoundHttpError';
|
||||
import { containsResource } from '../../storage/StoreUtil';
|
||||
import type { ResourceStore } from '../ResourceStore';
|
||||
import type { PreferenceSupport } from './PreferenceSupport';
|
||||
import { RouterRule } from './RouterRule';
|
||||
@@ -41,27 +41,11 @@ export class ConvertingRouterRule extends RouterRule {
|
||||
} else {
|
||||
// No content-type given so we can only check if one of the stores has data for the identifier
|
||||
store = await this.findStore(async(entry): Promise<boolean> =>
|
||||
this.hasResource(entry.store, input.identifier));
|
||||
containsResource(entry.store, input.identifier));
|
||||
}
|
||||
return store;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function that checks if the given store contains the given identifier or not.
|
||||
*/
|
||||
private async hasResource(store: ResourceStore, identifier: ResourceIdentifier): Promise<boolean> {
|
||||
try {
|
||||
const response = await store.getRepresentation(identifier, {});
|
||||
response.data.destroy();
|
||||
return true;
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof NotFoundHttpError) {
|
||||
return false;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function that runs the given callback function for all the stores
|
||||
* and returns the first one that does not throw an error.
|
||||
|
||||
Reference in New Issue
Block a user