Skip to content

Instantly share code, notes, and snippets.

@0xF013
Created March 19, 2020 10:58
Show Gist options
  • Save 0xF013/8e7575a1e1bc3dd031909802defa8c91 to your computer and use it in GitHub Desktop.
Save 0xF013/8e7575a1e1bc3dd031909802defa8c91 to your computer and use it in GitHub Desktop.
const https = require('https');
const toCamel = (s) => {
return s.replace(/([-_][a-z])/ig, ($1) => {
return $1.toUpperCase()
.replace('-', '')
.replace('_', '');
});
}
const typesMap = {
integer: 'number',
}
const mapTypeName = swaggerType => typesMap[swaggerType] || swaggerType;
const getRecursiveType = (propDetails) => {
if (propDetails.type === 'array') {
return `${getRecursiveType(propDetails.items)}[]`;
} else {
return mapTypeName(propDetails.type);
}
}
https.get('https://***', (resp) => {
let data = '';
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
let body = '';
Object.entries(JSON.parse(data).definitions).map(([definitionName, definition]) => {
if (definition.type === 'object') {
body += `export type ${definitionName} = {\n`;
Object.entries(definition.properties).map(([propName, propDetails]) => {
body += ` ${toCamel(propName)}: ${getRecursiveType(propDetails)};\n`;
});
body += `};\n\n`;
} else {
console.log('idk how to process ' + definitionName);
}
});
console.log(body);
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment