UNPKG

8.74 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.Package = void 0;
4/**
5 * A Dgeni Package containing processors, services and config blocks.
6 * @param {string} name The name of the package
7 * @param {string[]} dependencies The names of packages (or the actual packages) that this package
8 * depends upon
9 */
10var Package = /** @class */ (function () {
11 function Package(name, dependencies) {
12 if (dependencies === void 0) { dependencies = []; }
13 this.name = name;
14 this.dependencies = dependencies;
15 this.processors = [];
16 this.configFns = [];
17 this.handlers = {};
18 // We can't use a real di.Module here as di uses instanceof to detect module instances
19 this.module = {};
20 if (typeof name !== 'string') {
21 throw new Error('You must provide a name for the package');
22 }
23 if (dependencies && !Array.isArray(dependencies)) {
24 throw new Error('dependencies must be an array');
25 }
26 }
27 Package.isPackage = function (pkg) {
28 // Check the most important properties for dgeni to use a package
29 return isString(pkg.name) && Array.isArray(pkg.dependencies) && isObject(pkg.module);
30 };
31 /**
32 * Add a new processor to the package. The processor can be defined by a processor definition object
33 * or a factory function, which can be injected with services and will return the processor definition
34 * object. The following properties of a processor definition object have special meaning to Dgeni:
35 *
36 * * `name : {string}`: The name of the processor - if the processor is defined by a factory
37 * function or a name is explicitly provided as the first parameter, then this is ignored
38 * * `$process(docs : {string}) : {Array|Promise|undefined}`: The method that will be called to
39 * process the documents. If it is async then it should return a Promise.
40 * * `$runAfter : {string[]}`: Dgeni will ensure that this processor runs after those named here.
41 * * `$runBefore : {string[]}`: Dgeni will ensure that this processor runs before those named here.
42 * * `$validate: {Object}`: Dgeni will check that the properties of the processor, which match the
43 * keys of this object, pass the validation rules provided as the values of this object. See
44 * http://validatejs.org
45 *
46 * @param {function|object|string} processorDefOrName
47 * If this parameter is a string then it will be used as the processor's name, otherwise it is
48 * assumed that it used as the `processorDef`
49 *
50 * @param {function|object} processorDef
51 * The factory function or object that will be used by the injector to create the processor.
52 * * If a function then it is a factory and it must not be anonymous - it must have a name, e.g.
53 * `function myProcessor(dep1, dep2) { ... }` - since the name of the processor is taken from the
54 * name of the factory function.
55 * * If an object, then it is the actual processor and must have a `name` property. In this case,
56 * you cannot inject services into this processor.
57 *
58 * @return {Package}
59 * `this` package, to allow methods to be chained.
60 */
61 Package.prototype.processor = function (processorDefOrName, processorDef) {
62 var name;
63 if (isString(processorDefOrName)) {
64 name = processorDefOrName;
65 }
66 else {
67 processorDef = processorDefOrName;
68 // Using `any` here because Functions do usually have names (if they are not anonymous)
69 if (!processorDef.name) {
70 throw new Error('processorDef must be an object or a function with a name');
71 }
72 name = processorDef.name;
73 }
74 if (typeof processorDef === 'function') {
75 this.module[name] = ['factory', processorDef];
76 }
77 else {
78 this.module[name] = ['value', processorDef];
79 }
80 this.processors.push(name);
81 return this;
82 };
83 /**
84 * Add a new service, defined by a factory function, to the package
85 *
86 * @param {function|string} serviceFactoryOrName
87 * If a string then this is the name of the service, otherwise it is assumed to be the
88 * `serviceFactory`.
89 *
90 * @param {function} serviceFactory
91 * The factory function that will be used by the injector to create the service. The function must
92 * not be anonymous - it must have a name, e.g. `function myService() { ... }` - since the name of
93 * the service is taken from the name of the factory function.
94 *
95 * @return {Package}
96 * "This" package, to allow methods to be chained.
97 */
98 Package.prototype.factory = function (serviceFactoryOrName, serviceFactory) {
99 var name;
100 if (typeof serviceFactoryOrName === 'string') {
101 name = serviceFactoryOrName;
102 }
103 else {
104 serviceFactory = serviceFactoryOrName;
105 if (!serviceFactory.name) {
106 throw new Error('serviceFactory must have a name');
107 }
108 name = serviceFactory.name;
109 }
110 if (typeof serviceFactory !== 'function') {
111 throw new Error('serviceFactory must be a function.\nGot "' + typeof serviceFactory + '"');
112 }
113 this.module[name] = ['factory', serviceFactory];
114 return this;
115 };
116 /**
117 * Add a new service, defined as a Type to instantiated, to the package
118 *
119 * @param {function|string} ServiceTypeOrName
120 * If a string then this is the name of the service, otherwise it is assumed to be the
121 * `ServiceType`.
122 *
123 * @param {function} ServiceType
124 * The constructor function that will be used by the injector to create the processor. The function
125 * must not be anonymous - it must have a name, e.g. `function MyType() { ... }` - since the name of
126 * the service is taken from the name of the constructor function.
127 *
128 * @return {Package}
129 * "This" package, to allow methods to be chained.
130 */
131 Package.prototype.type = function (ServiceTypeOrName, ServiceType) {
132 var name;
133 if (typeof ServiceTypeOrName === 'string') {
134 name = ServiceTypeOrName;
135 }
136 else {
137 ServiceType = ServiceTypeOrName;
138 if (!ServiceType.name) {
139 throw new Error('ServiceType must have a name');
140 }
141 name = ServiceType.name;
142 }
143 if (typeof ServiceType !== 'function') {
144 throw new Error('ServiceType must be a constructor function');
145 }
146 this.module[name] = ['type', ServiceType];
147 return this;
148 };
149 /**
150 * Add a new config block to the package. Config blocks are run at the beginning of the doc
151 * generation before the processors are run. They can be injected with services and processors
152 * to allow you access to their properties so that you can configure them.
153 *
154 * @param {function} configFn The config block function to run
155 *
156 * @return {Package}
157 * "This" package, to allow methods to be chained.
158 */
159 Package.prototype.config = function (configFn) {
160 if (typeof configFn !== 'function') {
161 throw new Error('configFn must be a function');
162 }
163 this.configFns.push(configFn);
164 return this;
165 };
166 /**
167 * Add an event handler to this package
168 * @param {string} eventName The name of the event to handle
169 * @param {function} handlerFactory An injectable factory function that will return the handler
170 * @return {Package} This package for chaining
171 */
172 Package.prototype.eventHandler = function (eventName, handlerFactory) {
173 if (typeof eventName !== 'string') {
174 throw new Error('You must provide a string identifying the type of event to handle');
175 }
176 if (typeof handlerFactory !== 'function') {
177 throw new Error('handlerFactory must be a function.\nGot "' + typeof handlerFactory + '"');
178 }
179 var handlers = this.handlers[eventName] = this.handlers[eventName] || [];
180 var handlerName = handlerFactory.name || (this.name + '_' + eventName + '_' + handlers.length);
181 this.factory(handlerName, handlerFactory);
182 handlers.push(handlerName);
183 return this;
184 };
185 return Package;
186}());
187exports.Package = Package;
188function isObject(value) {
189 var type = typeof value;
190 return !!value && (type === 'object' || type === 'function');
191}
192function isString(value) {
193 return typeof value === 'string';
194}
195//# sourceMappingURL=Package.js.map
\No newline at end of file