forked from finos/architecture-as-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument-loader.ts
More file actions
64 lines (52 loc) · 1.94 KB
/
document-loader.ts
File metadata and controls
64 lines (52 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { CALM_META_SCHEMA_DIRECTORY } from '../consts';
import { SchemaDirectory } from '../schema-directory';
import { CalmHubDocumentLoader } from './calmhub-document-loader';
import { FileSystemDocumentLoader } from './file-system-document-loader';
import { DirectUrlDocumentLoader } from './direct-url-document-loader';
import { MultiStrategyDocumentLoader } from './multi-strategy-document-loader';
export type CalmDocumentType = 'architecture' | 'pattern' | 'schema';
export const CALM_HUB_PROTO = 'calm:';
export interface DocumentLoader {
initialise(schemaDirectory: SchemaDirectory): Promise<void>;
loadMissingDocument(documentId: string, type: CalmDocumentType): Promise<object>;
}
export type DocumentLoaderOptions = {
calmHubUrl?: string;
schemaDirectoryPath?: string;
debug?: boolean;
};
export function buildDocumentLoader(docLoaderOpts: DocumentLoaderOptions): DocumentLoader {
const loaders = [];
const debug = docLoaderOpts.debug ?? false;
if (docLoaderOpts.calmHubUrl) {
loaders.push(new CalmHubDocumentLoader(docLoaderOpts.calmHubUrl, debug));
}
// Always configure FileSystemDocumentLoader with CALM_META_SCHEMA_DIRECTORY
const directoryPaths = [CALM_META_SCHEMA_DIRECTORY];
if (docLoaderOpts.schemaDirectoryPath) {
directoryPaths.push(docLoaderOpts.schemaDirectoryPath);
}
loaders.push(new FileSystemDocumentLoader(directoryPaths, debug));
loaders.push(new DirectUrlDocumentLoader(debug));
return new MultiStrategyDocumentLoader(loaders, debug);
}
type ErrorName = 'OPERATION_NOT_IMPLEMENTED' | 'UNKNOWN';
export class DocumentLoadError extends Error {
name: ErrorName;
message: string;
cause: Error;
constructor({
name,
message,
cause
}: {
name: ErrorName;
message: string;
cause?: Error;
}) {
super();
this.name = name;
this.message = message;
this.cause = cause;
}
}