-
Notifications
You must be signed in to change notification settings - Fork 63
/
index.js
77 lines (68 loc) · 2.1 KB
/
index.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright IBM Corp. 2014,2019. All Rights Reserved.
// Node module: loopback-swagger
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
'use strict';
// Globalization
var SG = require('strong-globalize');
SG.SetRootDir(__dirname);
var g = SG();
var V2Generator = require('./lib/codegen/generator-v2');
var V12Generator = require('./lib/codegen/generator-v1.2');
var generateModels = require('./lib/codegen/json-schema');
var generateSwaggerSpec = require('./lib/specgen/swagger-spec-generator');
function getGenerator(spec) {
var generator;
if (spec && spec.swagger === '2.0') {
generator = new V2Generator();
} else if (spec && spec.swaggerVersion === '1.2') {
generator = new V12Generator();
} else {
throw new Error(g.f('{{Swagger spec}} version is not supported'));
}
return generator;
}
/**
* Generate remote methods from swagger spec
* @param {Object} spec
* @param {Object} options
* @returns {String}
*/
exports.generateRemoteMethods = function(spec, options) {
return getGenerator(spec).generateRemoteMethods(spec, options);
};
/**
* Generate remote methods for an array of operations
* @param {String|Number} version
* @param {String} modelName
* @param {BaseOperation[]} operations
* @returns {String}
*/
exports.generateCode = function(version, modelName, operations) {
var spec = {};
if (version === '1.2') {
spec.swaggerVersion = '1.2';
} else {
spec.swagger = 2;
}
return getGenerator(spec).generateCodeForOperations(modelName, operations);
};
/**
* Generate model definitions
* @param {Object} spec Swagger spec
* @param {Object} options
* @returns {Object}
*/
exports.generateModels = function(spec, options) {
var models;
if (spec && spec.swagger === '2.0') {
models = spec.definitions;
} else if (spec && spec.swaggerVersion === '1.2') {
models = spec.models;
} else {
throw new Error(g.f('{{Swagger spec}} version is not supported'));
}
return generateModels(models, options || {});
};
exports.getGenerator = getGenerator;
exports.generateSwaggerSpec = generateSwaggerSpec;