chore: Remove assumption that DataAccessors have a root container by default

This commit is contained in:
Joachim Van Herwegen
2020-12-14 15:49:58 +01:00
parent 231349b30d
commit 36eed5d620
12 changed files with 86 additions and 33 deletions

View File

@@ -1,6 +1,8 @@
import type { Server } from 'http';
import { join } from 'path';
import * as Path from 'path';
import { Loader } from 'componentsjs';
import fetch from 'cross-fetch';
import type {
BodyParser,
DataAccessor,
@@ -24,6 +26,7 @@ import {
ErrorResponseWriter,
GetOperationHandler,
HeadOperationHandler,
HttpError,
InMemoryDataAccessor,
LinkRelMetadataWriter,
LinkTypeParser,
@@ -192,3 +195,21 @@ export const instantiateFromConfig = async(componentUrl: string, configFile: str
const configPath = Path.join(__dirname, configFile);
return loader.instantiateFromUrl(componentUrl, configPath, undefined, { variables });
};
/**
* Initializes the root container of the server.
* Useful for when the RootContainerInitializer was not instantiated.
*/
export const initServerStore = async(server: Server, baseUrl: string, headers: HeadersInit = {}): Promise<void> => {
const res = await fetch(baseUrl, {
method: 'PUT',
headers: {
...headers,
'content-type': 'text/turtle',
},
body: '',
});
if (res.status >= 400) {
throw new HttpError(res.status, 'Error', res.statusText);
}
};