-
-
Notifications
You must be signed in to change notification settings - Fork 187
/
Copy pathInputProcessor.ts
63 lines (58 loc) · 2.14 KB
/
InputProcessor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { AbstractInputProcessor } from './AbstractInputProcessor';
import { AsyncAPIInputProcessor } from './AsyncAPIInputProcessor';
import { JsonSchemaInputProcessor } from './JsonSchemaInputProcessor';
import { ProcessorOptions, InputMetaModel } from '../models';
import { SwaggerInputProcessor } from './SwaggerInputProcessor';
import { OpenAPIInputProcessor } from './OpenAPIInputProcessor';
import { TypeScriptInputProcessor } from './TypeScriptInputProcessor';
/**
* Main input processor which figures out the type of input it receives and delegates the processing into separate individual processors.
*/
export class InputProcessor {
public static processor: InputProcessor = new InputProcessor();
private processors: Map<string, AbstractInputProcessor> = new Map();
constructor() {
this.setProcessor('asyncapi', new AsyncAPIInputProcessor());
this.setProcessor('swagger', new SwaggerInputProcessor());
this.setProcessor('openapi', new OpenAPIInputProcessor());
this.setProcessor('default', new JsonSchemaInputProcessor());
this.setProcessor('typescript', new TypeScriptInputProcessor());
}
/**
* Set a processor.
*
* @param type of processor
* @param processor
*/
setProcessor(type: string, processor: AbstractInputProcessor): void {
this.processors.set(type, processor);
}
/**
*
* @returns all processors
*/
getProcessors(): Map<string, AbstractInputProcessor> {
return this.processors;
}
/**
* The processor code which delegates the processing to the correct implementation.
*
* @param input to process
* @param options passed to the processors
*/
process(input: any, options?: ProcessorOptions): Promise<InputMetaModel> {
for (const [type, processor] of this.processors) {
if (type === 'default') {
continue;
}
if (processor.shouldProcess(input)) {
return processor.process(input, options);
}
}
const defaultProcessor = this.processors.get('default');
if (defaultProcessor !== undefined) {
return defaultProcessor.process(input, options);
}
return Promise.reject(new Error('No default processor found'));
}
}