-
Notifications
You must be signed in to change notification settings - Fork 191
/
Copy pathtypings.js
124 lines (108 loc) · 2.72 KB
/
typings.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env node
'use strict'
/* eslint-disable no-sync, no-console */
const fs = require('fs')
const mustache = require('mustache')
const path = require('path')
const zlib = require('zlib')
const Client = require('..').Client
/**
* Get the typescript interface name for a swagger-fluent component.
* @param {object} component - swagger-fluent Component.
* @returns {string} TypeScript interface name
*/
function interfaceName (component) {
// Root doesn't have splits
if (component.splits.length === 0) {
return 'ApiRoot'
}
//
// Replace '.'s with '_'s and CamelCase.
//
return component.splits
.map(split => split.replace(/\./g, '_').replace(/./, first => first.toUpperCase()))
.join('')
}
function walk (component, interfaces) {
const properties = []
let callable = null
if (component.children.length) {
component.children.forEach(child => {
const type = walk(component[child], interfaces)
properties.push({ name: child, type })
})
}
if (component.templated) {
const type = walk(component('name'), interfaces)
callable = { name: 'name', type }
}
const calls = [
'get',
'getStream',
'delete',
'patch',
'post',
'put'
].filter(call => call in component)
.map(method => ({
method,
parameterType: 'any',
returnType: 'any'
}))
const tsName = interfaceName(component)
const templateOptions = {
callable,
calls,
properties,
tsName
}
if (!interfaces.find(iface => iface.tsName === tsName)) {
interfaces.push(templateOptions)
}
return tsName
}
function main (args) {
let raw = fs.readFileSync(args.spec)
if (args.spec.endsWith('.gz')) {
raw = zlib.gunzipSync(raw)
}
const spec = JSON.parse(raw)
let clientSuffix = ''
if (spec.info.version) {
clientSuffix = spec.info.version.replace(/v/, '').split('.').slice(0, 2).join('_')
}
const interfaces = []
const backend = {}
const client = new Client({ backend, spec })
walk(client, interfaces)
const templateOptions = {
clientSuffix,
interfaces
}
const source = mustache.render(
fs.readFileSync(path.join(__dirname, 'templates/ts-namespace.mustache')).toString(),
templateOptions,
{
tsInterface: fs.readFileSync(path.join(__dirname, 'templates/ts-interface.mustache')).toString()
})
if (args.output) {
fs.writeFileSync(args.output, source)
} else {
console.log(source)
}
}
const argv = require('yargs')
.usage('Usage: $0 [options]')
.option('spec', {
alias: 's',
default: './lib/specs/swagger-1.13.json.gz',
describe: 'Swagger / OpenAPI specification'
})
.option('output', {
alias: 'o',
describe: 'Declaration file'
})
.strict()
.help()
.argv
main(argv)