@player-ui/types-provider-plugin 0.0.1-next.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.
@@ -0,0 +1,33 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ class TypesProviderPlugin {
6
+ constructor(config) {
7
+ this.name = "TypesProviderPlugin";
8
+ this.config = config;
9
+ }
10
+ apply(player) {
11
+ player.hooks.schema.tap(this.name, (schema) => {
12
+ if (this.config.types) {
13
+ schema.addDataTypes(this.config.types);
14
+ }
15
+ if (this.config.formats) {
16
+ schema.addFormatters(this.config.formats);
17
+ }
18
+ });
19
+ if (this.config.validators) {
20
+ player.hooks.validationController.tap(this.name, (validationController) => {
21
+ validationController.hooks.createValidatorRegistry.tap(this.name, (validationRegistry) => {
22
+ var _a;
23
+ (_a = this.config.validators) == null ? void 0 : _a.forEach(([name, handler]) => {
24
+ validationRegistry.register(name, handler);
25
+ });
26
+ });
27
+ });
28
+ }
29
+ }
30
+ }
31
+
32
+ exports.TypesProviderPlugin = TypesProviderPlugin;
33
+ //# sourceMappingURL=index.cjs.js.map
@@ -0,0 +1,30 @@
1
+ import { PlayerPlugin, Player } from '@player-ui/player';
2
+ import { ValidatorFunction } from '@player-ui/validator';
3
+ import { FormatType } from '@player-ui/schema';
4
+ import { Schema } from '@player-ui/types';
5
+
6
+ interface TypesConfig {
7
+ /**
8
+ * DataTypes to expose to Player instance.
9
+ * The schema definition in authored content can reference these to get common functionality across types
10
+ */
11
+ types?: Array<Schema.DataType<any>>;
12
+ /**
13
+ * Custom validators to add to this player instance.
14
+ * Anything defined here will be available for use in any DataType or view-validation
15
+ */
16
+ validators?: Array<[string, ValidatorFunction<any>]>;
17
+ /** A list of formats (and handler functions) to expose to DataTypes */
18
+ formats?: Array<FormatType<any, any, any>>;
19
+ }
20
+ /**
21
+ * The TypesProvider plugin provides an easy way for users to expose custom validators, DataTypes, or formatters to the content
22
+ */
23
+ declare class TypesProviderPlugin implements PlayerPlugin {
24
+ name: string;
25
+ private config;
26
+ constructor(config: TypesConfig);
27
+ apply(player: Player): void;
28
+ }
29
+
30
+ export { TypesConfig, TypesProviderPlugin };
@@ -0,0 +1,29 @@
1
+ class TypesProviderPlugin {
2
+ constructor(config) {
3
+ this.name = "TypesProviderPlugin";
4
+ this.config = config;
5
+ }
6
+ apply(player) {
7
+ player.hooks.schema.tap(this.name, (schema) => {
8
+ if (this.config.types) {
9
+ schema.addDataTypes(this.config.types);
10
+ }
11
+ if (this.config.formats) {
12
+ schema.addFormatters(this.config.formats);
13
+ }
14
+ });
15
+ if (this.config.validators) {
16
+ player.hooks.validationController.tap(this.name, (validationController) => {
17
+ validationController.hooks.createValidatorRegistry.tap(this.name, (validationRegistry) => {
18
+ var _a;
19
+ (_a = this.config.validators) == null ? void 0 : _a.forEach(([name, handler]) => {
20
+ validationRegistry.register(name, handler);
21
+ });
22
+ });
23
+ });
24
+ }
25
+ }
26
+ }
27
+
28
+ export { TypesProviderPlugin };
29
+ //# sourceMappingURL=index.esm.js.map
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "@player-ui/types-provider-plugin",
3
+ "version": "0.0.1-next.1",
4
+ "private": false,
5
+ "publishConfig": {
6
+ "registry": "https://registry.npmjs.org"
7
+ },
8
+ "peerDependencies": {
9
+ "@player-ui/binding-grammar": "0.0.1-next.1"
10
+ },
11
+ "dependencies": {
12
+ "tapable": "1.1.3",
13
+ "@types/tapable": "^1.0.5",
14
+ "@babel/runtime": "7.15.4"
15
+ },
16
+ "main": "dist/index.cjs.js",
17
+ "module": "dist/index.esm.js",
18
+ "typings": "dist/index.d.ts"
19
+ }
package/src/index.ts ADDED
@@ -0,0 +1,62 @@
1
+ import type { Player, PlayerPlugin } from '@player-ui/player';
2
+ import type { ValidatorFunction } from '@player-ui/validator';
3
+ import type { FormatType } from '@player-ui/schema';
4
+ import type { Schema } from '@player-ui/types';
5
+
6
+ export interface TypesConfig {
7
+ /**
8
+ * DataTypes to expose to Player instance.
9
+ * The schema definition in authored content can reference these to get common functionality across types
10
+ */
11
+ types?: Array<Schema.DataType<any>>;
12
+
13
+ /**
14
+ * Custom validators to add to this player instance.
15
+ * Anything defined here will be available for use in any DataType or view-validation
16
+ */
17
+ validators?: Array<[string, ValidatorFunction<any>]>;
18
+
19
+ /** A list of formats (and handler functions) to expose to DataTypes */
20
+ formats?: Array<FormatType<any, any, any>>;
21
+ }
22
+
23
+ /**
24
+ * The TypesProvider plugin provides an easy way for users to expose custom validators, DataTypes, or formatters to the content
25
+ */
26
+ export class TypesProviderPlugin implements PlayerPlugin {
27
+ name = 'TypesProviderPlugin';
28
+
29
+ private config: TypesConfig;
30
+
31
+ constructor(config: TypesConfig) {
32
+ this.config = config;
33
+ }
34
+
35
+ apply(player: Player) {
36
+ player.hooks.schema.tap(this.name, (schema) => {
37
+ if (this.config.types) {
38
+ schema.addDataTypes(this.config.types);
39
+ }
40
+
41
+ if (this.config.formats) {
42
+ schema.addFormatters(this.config.formats);
43
+ }
44
+ });
45
+
46
+ if (this.config.validators) {
47
+ player.hooks.validationController.tap(
48
+ this.name,
49
+ (validationController) => {
50
+ validationController.hooks.createValidatorRegistry.tap(
51
+ this.name,
52
+ (validationRegistry) => {
53
+ this.config.validators?.forEach(([name, handler]) => {
54
+ validationRegistry.register(name, handler);
55
+ });
56
+ }
57
+ );
58
+ }
59
+ );
60
+ }
61
+ }
62
+ }