@airtasker/spot
Advanced tools
Comparing version 0.1.31 to 0.1.32
@@ -11,2 +11,3 @@ "use strict"; | ||
const compact = require("lodash/compact"); | ||
const uniq = require("lodash/uniq"); | ||
const IMPORTED_AXIOS_NAME = "axios"; | ||
@@ -19,3 +20,3 @@ const SPOT_API_CONFIG_INTERFACE = "SpotApiConfig"; | ||
function generateAxiosClientSource(api) { | ||
const typeNames = Object.keys(api.types); | ||
const typeNames = getImportUsedTypes(api); | ||
return ts_writer_1.outputTypeScriptSource([ | ||
@@ -44,2 +45,22 @@ ts.createImportDeclaration( | ||
exports.generateAxiosClientSource = generateAxiosClientSource; | ||
function getImportUsedTypes(api) { | ||
return uniq(Object.values(api.endpoints) | ||
.map(endpoint => { | ||
const types = []; | ||
types.push(extractTypeName(api, endpoint.requestType)); | ||
types.push(extractTypeName(api, endpoint.responseType)); | ||
types.push(extractTypeName(api, endpoint.genericErrorType)); | ||
Object.values(endpoint.specificErrorTypes).forEach(specificError => types.push(extractTypeName(api, specificError.type))); | ||
return compact(types); | ||
}) | ||
.reduce((acc, types) => acc.concat(types), [])); | ||
} | ||
function extractTypeName(api, type) { | ||
if (!validator_1.isVoid(api, type)) { | ||
if (type.kind === "type-reference") { | ||
return type.typeName; | ||
} | ||
} | ||
return ""; | ||
} | ||
function generateSpotApiOptionsInterface() { | ||
@@ -46,0 +67,0 @@ return ts.createInterfaceDeclaration( |
@@ -5,2 +5,3 @@ "use strict"; | ||
const assert_never_1 = require("../../assert-never"); | ||
const models_1 = require("../../models"); | ||
const ts_writer_1 = require("./ts-writer"); | ||
@@ -138,7 +139,9 @@ const types_1 = require("./types"); | ||
/*modifiers*/ undefined, | ||
/*dotDotDotToken*/ undefined, accArgument), | ||
/*dotDotDotToken*/ undefined, accArgument, | ||
/*questionToken*/ undefined, types_1.typeNode(models_1.BOOLEAN)), | ||
ts.createParameter( | ||
/*decorators*/ undefined, | ||
/*modifiers*/ undefined, | ||
/*dotDotDotToken*/ undefined, currArgument) | ||
/*dotDotDotToken*/ undefined, currArgument, | ||
/*questionToken*/ undefined, types_1.typeNode(models_1.BOOLEAN)) | ||
], | ||
@@ -145,0 +148,0 @@ /*type*/ undefined, |
@@ -11,2 +11,3 @@ "use strict"; | ||
const compact = require("lodash/compact"); | ||
const uniq = require("lodash/uniq"); | ||
const IMPORTED_AXIOS_NAME = "axios"; | ||
@@ -19,3 +20,3 @@ const SPOT_API_CONFIG_INTERFACE = "SpotApiConfig"; | ||
function generateAxiosClientSource(api) { | ||
const typeNames = Object.keys(api.types); | ||
const typeNames = getImportUsedTypes(api); | ||
return ts_writer_1.outputTypeScriptSource([ | ||
@@ -44,2 +45,22 @@ ts.createImportDeclaration( | ||
exports.generateAxiosClientSource = generateAxiosClientSource; | ||
function getImportUsedTypes(api) { | ||
return uniq(Object.values(api.endpoints) | ||
.map(endpoint => { | ||
const types = []; | ||
types.push(extractTypeName(api, endpoint.requestType)); | ||
types.push(extractTypeName(api, endpoint.responseType)); | ||
types.push(extractTypeName(api, endpoint.genericErrorType)); | ||
Object.values(endpoint.specificErrorTypes).forEach(specificError => types.push(extractTypeName(api, specificError.type))); | ||
return compact(types); | ||
}) | ||
.reduce((acc, types) => acc.concat(types), [])); | ||
} | ||
function extractTypeName(api, type) { | ||
if (!validator_1.isVoid(api, type)) { | ||
if (type.kind === "type-reference") { | ||
return type.typeName; | ||
} | ||
} | ||
return ""; | ||
} | ||
function generateSpotApiOptionsInterface() { | ||
@@ -46,0 +67,0 @@ return ts.createInterfaceDeclaration( |
@@ -5,2 +5,3 @@ "use strict"; | ||
const assert_never_1 = require("../../assert-never"); | ||
const models_1 = require("../../models"); | ||
const ts_writer_1 = require("./ts-writer"); | ||
@@ -138,7 +139,9 @@ const types_1 = require("./types"); | ||
/*modifiers*/ undefined, | ||
/*dotDotDotToken*/ undefined, accArgument), | ||
/*dotDotDotToken*/ undefined, accArgument, | ||
/*questionToken*/ undefined, types_1.typeNode(models_1.BOOLEAN)), | ||
ts.createParameter( | ||
/*decorators*/ undefined, | ||
/*modifiers*/ undefined, | ||
/*dotDotDotToken*/ undefined, currArgument) | ||
/*dotDotDotToken*/ undefined, currArgument, | ||
/*questionToken*/ undefined, types_1.typeNode(models_1.BOOLEAN)) | ||
], | ||
@@ -145,0 +148,0 @@ /*type*/ undefined, |
@@ -1,1 +0,1 @@ | ||
{"version":"0.1.31","commands":{"generate":{"id":"generate","description":"Runs a generator on an API. Used to produce client libraries, server boilerplates and well-known API contract formats such as OpenAPI.","pluginName":"@airtasker/spot","pluginType":"core","aliases":[],"examples":["$ spot generate --language typescript --generator axios-client --out src/\nGenerated the following files:\n- src/types.ts\n- src/validators.ts\n- src/client.ts\n"],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false},"api":{"name":"api","type":"option","char":"a","description":"Path to a TypeScript API definition","required":true},"language":{"name":"language","type":"option","char":"l","description":"Language to generate"},"generator":{"name":"generator","type":"option","char":"g","description":"Generator to run"},"out":{"name":"out","type":"option","char":"o","description":"Directory in which to output generated files"}},"args":[]},"init":{"id":"init","description":"Generates the boilerplate for an API.","pluginName":"@airtasker/spot","pluginType":"core","aliases":[],"examples":["$ spot init\nGenerated the following files:\n- api.ts\n- tsconfig.json\n- package.json\n"],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[]},"validate":{"id":"validate","description":"Validate a Spot contract","pluginName":"@airtasker/spot","pluginType":"core","aliases":[],"examples":["$ spot validate api.ts"],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[{"name":"spot_contract","description":"path to Spot contract","required":true,"hidden":false}]}}} | ||
{"version":"0.1.32","commands":{"generate":{"id":"generate","description":"Runs a generator on an API. Used to produce client libraries, server boilerplates and well-known API contract formats such as OpenAPI.","pluginName":"@airtasker/spot","pluginType":"core","aliases":[],"examples":["$ spot generate --language typescript --generator axios-client --out src/\nGenerated the following files:\n- src/types.ts\n- src/validators.ts\n- src/client.ts\n"],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false},"api":{"name":"api","type":"option","char":"a","description":"Path to a TypeScript API definition","required":true},"language":{"name":"language","type":"option","char":"l","description":"Language to generate"},"generator":{"name":"generator","type":"option","char":"g","description":"Generator to run"},"out":{"name":"out","type":"option","char":"o","description":"Directory in which to output generated files"}},"args":[]},"init":{"id":"init","description":"Generates the boilerplate for an API.","pluginName":"@airtasker/spot","pluginType":"core","aliases":[],"examples":["$ spot init\nGenerated the following files:\n- api.ts\n- tsconfig.json\n- package.json\n"],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[]},"validate":{"id":"validate","description":"Validate a Spot contract","pluginName":"@airtasker/spot","pluginType":"core","aliases":[],"examples":["$ spot validate api.ts"],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[{"name":"spot_contract","description":"path to Spot contract","required":true,"hidden":false}]}}} |
{ | ||
"name": "@airtasker/spot", | ||
"version": "0.1.31", | ||
"version": "0.1.32", | ||
"author": "Francois Wouts", | ||
@@ -5,0 +5,0 @@ "bin": { |
@@ -402,3 +402,3 @@ # Spot | ||
_See code: [build/cli/src/commands/generate.js](https://github.com/airtasker/spot/blob/v0.1.31/build/cli/src/commands/generate.js)_ | ||
_See code: [build/cli/src/commands/generate.js](https://github.com/airtasker/spot/blob/v0.1.32/build/cli/src/commands/generate.js)_ | ||
@@ -441,3 +441,3 @@ ## `spot help [COMMAND]` | ||
_See code: [build/cli/src/commands/init.js](https://github.com/airtasker/spot/blob/v0.1.31/build/cli/src/commands/init.js)_ | ||
_See code: [build/cli/src/commands/init.js](https://github.com/airtasker/spot/blob/v0.1.32/build/cli/src/commands/init.js)_ | ||
@@ -462,3 +462,3 @@ ## `spot validate SPOT_CONTRACT` | ||
_See code: [build/cli/src/commands/validate.js](https://github.com/airtasker/spot/blob/v0.1.31/build/cli/src/commands/validate.js)_ | ||
_See code: [build/cli/src/commands/validate.js](https://github.com/airtasker/spot/blob/v0.1.32/build/cli/src/commands/validate.js)_ | ||
<!-- commandsstop --> |
352077
8756