@openmrs/esm-config 4.0.0-pre.0 → 4.0.0-pre.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +10 -10
- package/.turbo/turbo-lint.log +1 -1
- package/.turbo/turbo-test.log +9 -8
- package/.turbo/turbo-typescript.log +2 -2
- package/dist/__mocks__/openmrs-esm-state.mock.d.ts +12 -0
- package/dist/openmrs-esm-module-config.js +1 -1
- package/dist/openmrs-esm-module-config.js.map +1 -1
- package/dist/src/index.d.ts +7 -0
- package/dist/src/module-config/module-config.d.ts +57 -0
- package/dist/src/module-config/module-config.test.d.ts +1 -0
- package/dist/src/module-config/state.d.ts +109 -0
- package/dist/src/navigation/interpolate-string.d.ts +34 -0
- package/dist/src/navigation/interpolate-string.test.d.ts +1 -0
- package/dist/src/navigation/navigate.d.ts +26 -0
- package/dist/src/navigation/navigate.test.d.ts +1 -0
- package/dist/src/public.d.ts +7 -0
- package/dist/src/types.d.ts +49 -0
- package/dist/src/validators/type-validators.d.ts +6 -0
- package/dist/src/validators/type-validators.test.d.ts +1 -0
- package/dist/src/validators/validator.d.ts +24 -0
- package/dist/src/validators/validators.d.ts +32 -0
- package/dist/src/validators/validators.test.d.ts +1 -0
- package/package.json +4 -4
- package/src/module-config/module-config.test.ts +63 -5
- package/src/module-config/module-config.ts +100 -42
- package/src/navigation/interpolate-string.test.ts +3 -3
- package/src/navigation/interpolate-string.ts +10 -2
- package/src/navigation/navigate.ts +10 -4
- package/src/validators/validator.ts +27 -2
- package/src/validators/validators.test.ts +18 -1
- package/src/validators/validators.ts +13 -1
|
@@ -15,14 +15,14 @@ describe("interpolateUrl", () => {
|
|
|
15
15
|
describe("interpolateString", () => {
|
|
16
16
|
it("interpolates template elements", () => {
|
|
17
17
|
const result = interpolateString("test ${one} ${two} 3", {
|
|
18
|
-
one: 1,
|
|
19
|
-
two: 2,
|
|
18
|
+
one: "1",
|
|
19
|
+
two: "2",
|
|
20
20
|
});
|
|
21
21
|
expect(result).toBe("test 1 2 3");
|
|
22
22
|
});
|
|
23
23
|
|
|
24
24
|
it("tolerates extra parameters", () => {
|
|
25
|
-
const result = interpolateString("test ok", { one: 1, two: 2 });
|
|
25
|
+
const result = interpolateString("test ok", { one: "1", two: "2" });
|
|
26
26
|
expect(result).toBe("test ok");
|
|
27
27
|
});
|
|
28
28
|
});
|
|
@@ -11,12 +11,17 @@ function trimTrailingSlash(str: string) {
|
|
|
11
11
|
* parameters in configurable URLs.
|
|
12
12
|
*
|
|
13
13
|
* @param template A string to interpolate
|
|
14
|
+
* @param additionalParams Additional values to interpolate into the string template
|
|
14
15
|
*/
|
|
15
|
-
export function interpolateUrl(
|
|
16
|
+
export function interpolateUrl(
|
|
17
|
+
template: string,
|
|
18
|
+
additionalParams?: { [key: string]: string }
|
|
19
|
+
): string {
|
|
16
20
|
const openmrsSpaBase = trimTrailingSlash(window.getOpenmrsSpaBase());
|
|
17
21
|
return interpolateString(template, {
|
|
18
22
|
openmrsBase: window.openmrsBase,
|
|
19
23
|
openmrsSpaBase: openmrsSpaBase,
|
|
24
|
+
...additionalParams,
|
|
20
25
|
}).replace(/^\/\//, "/"); // remove extra initial slash if present
|
|
21
26
|
}
|
|
22
27
|
|
|
@@ -38,7 +43,10 @@ export function interpolateUrl(template: string): string {
|
|
|
38
43
|
* @param template With optional params wrapped in `${ }`
|
|
39
44
|
* @param params Values to interpolate into the string template
|
|
40
45
|
*/
|
|
41
|
-
export function interpolateString(
|
|
46
|
+
export function interpolateString(
|
|
47
|
+
template: string,
|
|
48
|
+
params: { [key: string]: string }
|
|
49
|
+
): string {
|
|
42
50
|
const names = Object.keys(params);
|
|
43
51
|
return names.reduce(
|
|
44
52
|
(prev, curr) => prev.split("${" + curr + "}").join(params[curr]),
|
|
@@ -7,8 +7,11 @@ function trimTrailingSlash(str: string) {
|
|
|
7
7
|
return str.replace(/\/$/, "");
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
+
export type TemplateParams = { [key: string]: string };
|
|
11
|
+
|
|
10
12
|
export interface NavigateOptions {
|
|
11
13
|
to: string;
|
|
14
|
+
templateParams?: TemplateParams;
|
|
12
15
|
}
|
|
13
16
|
|
|
14
17
|
/**
|
|
@@ -16,19 +19,22 @@ export interface NavigateOptions {
|
|
|
16
19
|
*
|
|
17
20
|
* Example usage:
|
|
18
21
|
* ```js
|
|
19
|
-
* const config =
|
|
22
|
+
* const config = useConfig();
|
|
20
23
|
* const submitHandler = () => {
|
|
21
24
|
* navigate({ to: config.links.submitSuccess });
|
|
22
25
|
* };
|
|
23
26
|
* ```
|
|
24
27
|
*
|
|
25
|
-
* @param to The target path or URL. Supports templating with 'openmrsBase'
|
|
28
|
+
* @param to The target path or URL. Supports templating with 'openmrsBase', 'openmrsSpaBase',
|
|
29
|
+
* and any additional template parameters defined in `templateParams`.
|
|
26
30
|
* For example, `${openmrsSpaBase}/home` will resolve to `/openmrs/spa/home`
|
|
27
31
|
* for implementations using the standard OpenMRS and SPA base paths.
|
|
32
|
+
* If `templateParams` contains `{ foo: "bar" }`, then the URL `${openmrsBase}/${foo}`
|
|
33
|
+
* will become `/openmrs/bar`.
|
|
28
34
|
*/
|
|
29
|
-
export function navigate({ to }: NavigateOptions): void {
|
|
35
|
+
export function navigate({ to, templateParams }: NavigateOptions): void {
|
|
30
36
|
const openmrsSpaBase = trimTrailingSlash(window.getOpenmrsSpaBase());
|
|
31
|
-
const target = interpolateUrl(to);
|
|
37
|
+
const target = interpolateUrl(to, templateParams);
|
|
32
38
|
const isSpaPath = target.startsWith(openmrsSpaBase);
|
|
33
39
|
|
|
34
40
|
if (isSpaPath) {
|
|
@@ -1,13 +1,38 @@
|
|
|
1
1
|
/** @module @category Config Validation */
|
|
2
2
|
import { Validator, ValidatorFunction } from "../types";
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Constructs a custom validator.
|
|
6
|
+
*
|
|
7
|
+
* ### Example
|
|
8
|
+
*
|
|
9
|
+
* ```typescript
|
|
10
|
+
* {
|
|
11
|
+
* foo: {
|
|
12
|
+
* _default: 0,
|
|
13
|
+
* _validators: [
|
|
14
|
+
* validator(val => val >= 0, "Must not be negative.")
|
|
15
|
+
* ]
|
|
16
|
+
* }
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
* @param validationFunction Takes the configured value as input. Returns true
|
|
20
|
+
* if it is valid, false otherwise.
|
|
21
|
+
* @param message A string message that explains why the value is invalid. Can
|
|
22
|
+
* also be a function that takes the value as input and returns a string.
|
|
23
|
+
* @returns A validator ready for use in a config schema
|
|
24
|
+
*/
|
|
4
25
|
export function validator(
|
|
5
26
|
validationFunction: ValidatorFunction,
|
|
6
|
-
message: string
|
|
27
|
+
message: string | ((value: any) => string)
|
|
7
28
|
): Validator {
|
|
8
29
|
return (value) => {
|
|
9
30
|
if (!validationFunction(value)) {
|
|
10
|
-
|
|
31
|
+
if (typeof message === "function") {
|
|
32
|
+
return message(value);
|
|
33
|
+
} else {
|
|
34
|
+
return message;
|
|
35
|
+
}
|
|
11
36
|
}
|
|
12
37
|
};
|
|
13
38
|
}
|
|
@@ -1,9 +1,16 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
inRange,
|
|
3
|
+
isUrl,
|
|
4
|
+
isUrlWithTemplateParameters,
|
|
5
|
+
oneOf,
|
|
6
|
+
} from "./validators";
|
|
2
7
|
|
|
3
8
|
describe("all validators", () => {
|
|
4
9
|
it("fail on undefined", () => {
|
|
10
|
+
expect(inRange(0, 10)(undefined)).toMatch(/.*/);
|
|
5
11
|
expect(isUrl(undefined)).toMatch(/.*/);
|
|
6
12
|
expect(isUrlWithTemplateParameters(["foo"])(undefined)).toMatch(/.*/);
|
|
13
|
+
expect(oneOf(["foo", "bar"])(undefined)).toMatch(/.*/);
|
|
7
14
|
});
|
|
8
15
|
});
|
|
9
16
|
|
|
@@ -22,3 +29,13 @@ describe("isUrl", () => {
|
|
|
22
29
|
);
|
|
23
30
|
});
|
|
24
31
|
});
|
|
32
|
+
|
|
33
|
+
describe("oneOf", () => {
|
|
34
|
+
it("accepts one of the valid options", () => {
|
|
35
|
+
expect(oneOf(["foo", "bar"])("foo")).toBeUndefined();
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("rejects anything else", () => {
|
|
39
|
+
expect(oneOf(["foo", "bar"])("baz")).toMatch(/one of.*foo.*bar/i);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
@@ -45,7 +45,7 @@ export const isUrlWithTemplateParameters = (
|
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
return true;
|
|
48
|
-
}, "
|
|
48
|
+
}, "Should be a URL or path. The allowed template parameters are " + allowedParams.map((p) => "${" + p + "}").join(", "));
|
|
49
49
|
};
|
|
50
50
|
|
|
51
51
|
/**
|
|
@@ -55,8 +55,20 @@ export const isUrlWithTemplateParameters = (
|
|
|
55
55
|
*/
|
|
56
56
|
export const isUrl = isUrlWithTemplateParameters([]);
|
|
57
57
|
|
|
58
|
+
/**
|
|
59
|
+
* Verifies that the value is one of the allowed options.
|
|
60
|
+
* @param allowedValues The list of allowable values
|
|
61
|
+
*/
|
|
62
|
+
export const oneOf = (allowedValues: Array<any>) => {
|
|
63
|
+
return validator(
|
|
64
|
+
(val) => allowedValues.includes(val),
|
|
65
|
+
`Must be one of the following: '${allowedValues.join("', '")}'.`
|
|
66
|
+
);
|
|
67
|
+
};
|
|
68
|
+
|
|
58
69
|
export const validators = {
|
|
59
70
|
inRange,
|
|
60
71
|
isUrl,
|
|
61
72
|
isUrlWithTemplateParameters,
|
|
73
|
+
oneOf,
|
|
62
74
|
};
|