UNPKG

2.94 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.validateDomainAndModule = exports.getModuleBaseUri = exports.getModuleDomain = exports.moduleToUri = exports.domainToUri = void 0;
4const bf_types_1 = require("bf-types");
5const utils_1 = require("./utils");
6const domainUriMap = new Map();
7const moduleUriMap = new Map();
8for (const domain of Object.values(bf_types_1.DOMAINS)) {
9 domainUriMap.set(domain, utils_1.toLowerCamel(domain));
10}
11for (const module of Object.values(bf_types_1.ALL_MODULES)) {
12 moduleUriMap.set(module, utils_1.toLowerCamel(module));
13}
14const moduleToDomainMap = new Map();
15for (const domainName of Object.keys(bf_types_1.DOMAIN_MODULES)) {
16 for (const moduleName of Object.keys(bf_types_1.DOMAIN_MODULES[domainName])) {
17 moduleToDomainMap.set(moduleName, domainName);
18 }
19}
20/**
21 * Convert a block-5 domain name into a URI formatted string
22 *
23 * @param domainName The name of a block-5 domain
24 */
25function domainToUri(domainName) {
26 const uri = domainUriMap.get(domainName);
27 if (!uri) {
28 throw new Error(`there is no URI for the ${domainName} domain`);
29 }
30 return uri;
31}
32exports.domainToUri = domainToUri;
33/**
34 * Convert a block-5 module name into a URI formatted string
35 *
36 * @param moduleName The name of a block-5 module
37 */
38function moduleToUri(moduleName) {
39 const uri = moduleUriMap.get(moduleName);
40 if (!uri) {
41 throw new Error(`there is no URI for the ${moduleName} module`);
42 }
43 return uri;
44}
45exports.moduleToUri = moduleToUri;
46/**
47 * Get the domain name associated with a particular module name
48 *
49 * @param moduleName The name of a block-5 module
50 */
51function getModuleDomain(moduleName) {
52 return moduleToDomainMap.get(moduleName) || null;
53}
54exports.getModuleDomain = getModuleDomain;
55/**
56 * Get the base URI associated with a particular block-5 module name
57 *
58 * @param moduleName The name of a block-5 module
59 */
60function getModuleBaseUri(moduleName, uri) {
61 const domainName = getModuleDomain(moduleName);
62 return `${domainToUri(domainName)}/${moduleToUri(moduleName)}${uri ? `/${uri}` : ''}`;
63}
64exports.getModuleBaseUri = getModuleBaseUri;
65/**
66 * Ensure that both a domain name and module name are valid
67 *
68 * @param domainName The name of a block-5 domain
69 * @param moduleName The name of a block-5 module
70 */
71function validateDomainAndModule(domainName, moduleName) {
72 if (!bf_types_1.DOMAINS[domainName]) {
73 throw new Error(`${domainName} is not a valid entity domain`);
74 }
75 if (!bf_types_1.ALL_MODULES[moduleName]) {
76 throw new Error(`${moduleName} is not a valid entity module`);
77 }
78 const domainModules = bf_types_1.DOMAIN_MODULES[domainName];
79 if (typeof domainModules[moduleName] !== 'string') {
80 throw new Error(`${module} is not a module of the ${utils_1.toDisplay(domainName)}`);
81 }
82}
83exports.validateDomainAndModule = validateDomainAndModule;