1 | 'use strict';
|
2 | const path = require('path');
|
3 |
|
4 | const ADAPTERS = require(path.join(__dirname, './adapters/index'));
|
5 |
|
6 | const CONTENT_ADAPTER_INTERFACE = class Adapter_Interface {
|
7 | |
8 |
|
9 |
|
10 |
|
11 | constructor (options = {}) {
|
12 | this.interface = (this.interface && typeof this.interface === 'object') ? this.interface : {};
|
13 | for (let key in options) {
|
14 | this.interface[key] = options[key];
|
15 | }
|
16 | }
|
17 | |
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | create (options = {}) {
|
25 | options.adapter = (options.responder) ? options.responder : options.adapter;
|
26 | let Adapter = (typeof options.adapter === 'string') ? ADAPTERS[options.adapter] : options.adapter;
|
27 | if (!Adapter) throw new Error('Could not find a corresponding adapter - for custom adapters pass the constructor as the "adapter" options');
|
28 | let adapter = new Adapter(options);
|
29 | let errors = [];
|
30 | for (let key in this.interface) {
|
31 | if (this.interface[key] !== typeof adapter[key]) errors.push(`${ key } is invalid type ${ typeof adapter[key] } and should be ${ this.interface[key] }`);
|
32 | }
|
33 | if (errors.length) {
|
34 | let compiledErrors = errors.reduce((result, error, index) => {
|
35 | if (index === errors.length - 1) result += error;
|
36 | else result += `${ error }, `;
|
37 | return result;
|
38 | }, '');
|
39 | throw new Error(compiledErrors);
|
40 | }
|
41 | return adapter;
|
42 | }
|
43 | };
|
44 |
|
45 | module.exports = new CONTENT_ADAPTER_INTERFACE({
|
46 | render: 'function',
|
47 | error: 'function'
|
48 | });
|