feat: Add containsResource utility.

This commit is contained in:
Ruben Verborgh
2021-01-15 00:12:27 +01:00
committed by Joachim Van Herwegen
parent ee50f40062
commit af47083f64
5 changed files with 27 additions and 61 deletions

View File

@@ -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.