@airtasker/spot
Advanced tools
Comparing version 0.2.4 to 0.2.5
@@ -8,2 +8,3 @@ "use strict"; | ||
const axios_1 = __importDefault(require("axios")); | ||
const qs_1 = require("qs"); | ||
const json_schema_1 = require("../../../lib/src/generators/contract/json-schema"); | ||
@@ -132,2 +133,5 @@ const data_expression_utils_1 = require("../../../lib/src/utilities/data-expression-utils"); | ||
}, {}); | ||
config.paramsSerializer = params => { | ||
return qs_1.stringify(params); | ||
}; | ||
if (test.request.body) { | ||
@@ -134,0 +138,0 @@ config.data = data_expression_utils_1.valueFromDataExpression(test.request.body); |
@@ -57,3 +57,13 @@ "use strict"; | ||
}, {}), | ||
definitions: contractDefinition.types.reduce((acc, typeNode) => { | ||
definitions: contractDefinition.types | ||
.sort((prevType, currType) => { | ||
if (prevType.name < currType.name) { | ||
return -1; | ||
} | ||
if (prevType.name > currType.name) { | ||
return 1; | ||
} | ||
return 0; | ||
}) | ||
.reduce((acc, typeNode) => { | ||
acc[typeNode.name] = openapi2_schema_1.openApi2TypeSchema(typeNode.type); | ||
@@ -60,0 +70,0 @@ return acc; |
@@ -81,3 +81,13 @@ "use strict"; | ||
components: { | ||
schemas: contractDefinition.types.reduce((acc, typeNode) => { | ||
schemas: contractDefinition.types | ||
.sort((prevType, currType) => { | ||
if (prevType.name < currType.name) { | ||
return -1; | ||
} | ||
if (prevType.name > currType.name) { | ||
return 1; | ||
} | ||
return 0; | ||
}) | ||
.reduce((acc, typeNode) => { | ||
acc[typeNode.name] = openapi3_schema_1.openApi3TypeSchema(contractDefinition.types, typeNode.type); | ||
@@ -84,0 +94,0 @@ return acc; |
@@ -55,10 +55,5 @@ "use strict"; | ||
const endpoints = parseRecursively(file); | ||
// Direct reference types, filtered to unique references for efficiency | ||
const uniqueDirectReferenceTypes = type_reference_resolver_1.uniqueReferences(type_reference_resolver_1.retrieveTypeReferencesFromEndpoints(endpoints)); | ||
// Reference types from the direct reference type hierarchy | ||
const secondaryReferenceTypes = uniqueDirectReferenceTypes.reduce((referenceTypesAcc, currentReferenceType) => referenceTypesAcc.concat(type_reference_resolver_1.retrieveTypeReferencesFromType(currentReferenceType, projectContext)), []); | ||
// Combine and filter to unique type references | ||
const allReferenceTypes = type_reference_resolver_1.uniqueReferences(uniqueDirectReferenceTypes.concat(secondaryReferenceTypes)); | ||
const referenceTypes = type_reference_resolver_1.uniqueReferences(type_reference_resolver_1.retrieveTypeReferencesFromEndpoints(endpoints, projectContext)); | ||
// Construct the equivalent TypeNode for each reference type | ||
const types = allReferenceTypes.map(referenceType => { | ||
const types = referenceTypes.map(referenceType => { | ||
const file = projectContext.getSourceFileOrThrow(referenceType.location); | ||
@@ -65,0 +60,0 @@ const typeAlias = file.getTypeAlias(referenceType.name); |
import Project from "ts-morph"; | ||
import { Locatable } from "../../models/locatable"; | ||
import { EndpointNode } from "../../models/nodes"; | ||
import { DataType, ReferenceType } from "../../models/types"; | ||
import { ReferenceType } from "../../models/types"; | ||
/** | ||
* Recursively retrieves all type references from a data type including itself. | ||
*/ | ||
export declare function retrieveTypeReferencesFromType(dataType: DataType, projectContext: Project): ReferenceType[]; | ||
/** | ||
* Retrieve all the type references from a collection of endpoints. This will only retrieve direct references (not recursive) | ||
* from each node of an endpoint. | ||
* Retrieve all the type references from a collection of endpoints recurisvely. | ||
* | ||
* @param endpoints collection of endpoints | ||
* @param projectContext ts-morph project | ||
*/ | ||
export declare function retrieveTypeReferencesFromEndpoints(endpoints: Array<Locatable<EndpointNode>>): ReferenceType[]; | ||
export declare function retrieveTypeReferencesFromEndpoints(endpoints: Array<Locatable<EndpointNode>>, projectContext: Project): ReferenceType[]; | ||
/** | ||
@@ -17,0 +13,0 @@ * Return the unique collection of reference types from a collection of reference types. |
@@ -7,2 +7,78 @@ "use strict"; | ||
/** | ||
* Retrieve all the type references from a collection of endpoints recurisvely. | ||
* | ||
* @param endpoints collection of endpoints | ||
* @param projectContext ts-morph project | ||
*/ | ||
function retrieveTypeReferencesFromEndpoints(endpoints, projectContext) { | ||
return endpoints.reduce((referenceTypesAcc, currentEndpoint) => { | ||
const fromResponses = retrieveTypeReferencesFromResponses(currentEndpoint.value.responses, projectContext); | ||
if (currentEndpoint.value.request) { | ||
const fromRequest = fromResponses.concat(retrieveTypeReferencesFromRequest(currentEndpoint.value.request, projectContext)); | ||
return referenceTypesAcc.concat(fromRequest).concat(fromResponses); | ||
} | ||
return referenceTypesAcc.concat(fromResponses); | ||
}, []); | ||
} | ||
exports.retrieveTypeReferencesFromEndpoints = retrieveTypeReferencesFromEndpoints; | ||
/** | ||
* Retrieve all type references from a request recursively. | ||
* | ||
* @param request a request | ||
* @param projectContext ts-morph project | ||
*/ | ||
function retrieveTypeReferencesFromRequest(request, projectContext) { | ||
const fromHeaders = request.value.headers | ||
? retrieveTypeReferencesFromHeadersPathParamsQueryParams(request.value.headers.value, projectContext) | ||
: []; | ||
const fromPathParams = request.value.pathParams | ||
? retrieveTypeReferencesFromHeadersPathParamsQueryParams(request.value.pathParams.value, projectContext) | ||
: []; | ||
const fromQueryParams = request.value.queryParams | ||
? retrieveTypeReferencesFromHeadersPathParamsQueryParams(request.value.queryParams.value, projectContext) | ||
: []; | ||
const fromBody = request.value.body | ||
? retrieveTypeReferencesFromBody(request.value.body, projectContext) | ||
: []; | ||
return fromHeaders | ||
.concat(fromPathParams) | ||
.concat(fromQueryParams) | ||
.concat(fromBody); | ||
} | ||
/** | ||
* Retrieve all type references from a collection of responses recursively. | ||
* | ||
* @param requests a collection of responses | ||
* @param projectContext ts-morph project | ||
*/ | ||
function retrieveTypeReferencesFromResponses(responses, projectContext) { | ||
return responses.reduce((typeReferencesAcc, currentResponse) => { | ||
const fromHeaders = currentResponse.value.headers | ||
? retrieveTypeReferencesFromHeadersPathParamsQueryParams(currentResponse.value.headers.value, projectContext) | ||
: []; | ||
const fromBody = currentResponse.value.body | ||
? retrieveTypeReferencesFromBody(currentResponse.value.body, projectContext) | ||
: []; | ||
return typeReferencesAcc.concat(fromHeaders).concat(fromBody); | ||
}, []); | ||
} | ||
/** | ||
* Retrieve all type references from a body recursively. | ||
* | ||
* @param body a body | ||
* @param projectContext ts-morph project | ||
*/ | ||
function retrieveTypeReferencesFromBody(body, projectContext) { | ||
return retrieveTypeReferencesFromType(body.value.type, projectContext); | ||
} | ||
/** | ||
* Retrieve all type references from a collection of headers, path params or query params recursively. | ||
* | ||
* @param nodes a collection of headers, path params or query params | ||
* @param projectContext ts-morph project | ||
*/ | ||
function retrieveTypeReferencesFromHeadersPathParamsQueryParams(nodes, projectContext) { | ||
return nodes.reduce((typeReferencesAcc, currentHeader) => typeReferencesAcc.concat(retrieveTypeReferencesFromType(currentHeader.value.type, projectContext)), []); | ||
} | ||
/** | ||
* Recursively retrieves all type references from a data type including itself. | ||
@@ -60,97 +136,3 @@ */ | ||
} | ||
exports.retrieveTypeReferencesFromType = retrieveTypeReferencesFromType; | ||
/** | ||
* Retrieve all the type references from a collection of endpoints. This will only retrieve direct references (not recursive) | ||
* from each node of an endpoint. | ||
* | ||
* @param endpoints collection of endpoints | ||
*/ | ||
function retrieveTypeReferencesFromEndpoints(endpoints) { | ||
return endpoints.reduce((referenceTypesAcc, currentEndpoint) => { | ||
const fromResponses = retrieveTypeReferencesFromResponses(currentEndpoint.value.responses); | ||
if (currentEndpoint.value.request) { | ||
const fromRequest = fromResponses.concat(retrieveTypeReferencesFromRequest(currentEndpoint.value.request)); | ||
return referenceTypesAcc.concat(fromRequest).concat(fromResponses); | ||
} | ||
return referenceTypesAcc.concat(fromResponses); | ||
}, []); | ||
} | ||
exports.retrieveTypeReferencesFromEndpoints = retrieveTypeReferencesFromEndpoints; | ||
/** | ||
* Retrieve all type references from a request. This will only retrieve direct references (not recursive). | ||
* | ||
* @param request a request | ||
*/ | ||
function retrieveTypeReferencesFromRequest(request) { | ||
const fromHeaders = request.value.headers | ||
? retrieveTypeReferencesFromHeadersPathParamsQueryParams(request.value.headers.value) | ||
: []; | ||
const fromPathParams = request.value.pathParams | ||
? retrieveTypeReferencesFromHeadersPathParamsQueryParams(request.value.pathParams.value) | ||
: []; | ||
const fromQueryParams = request.value.queryParams | ||
? retrieveTypeReferencesFromHeadersPathParamsQueryParams(request.value.queryParams.value) | ||
: []; | ||
const fromBody = request.value.body | ||
? retrieveTypeReferencesFromBody(request.value.body) | ||
: []; | ||
return fromHeaders | ||
.concat(fromPathParams) | ||
.concat(fromQueryParams) | ||
.concat(fromBody); | ||
} | ||
/** | ||
* Retrieve all type references from a collection of responses. This will only retrieve direct references (not recursive). | ||
* | ||
* @param requests a collection of responses | ||
*/ | ||
function retrieveTypeReferencesFromResponses(responses) { | ||
return responses.reduce((typeReferencesAcc, currentResponse) => { | ||
const fromHeaders = currentResponse.value.headers | ||
? retrieveTypeReferencesFromHeadersPathParamsQueryParams(currentResponse.value.headers.value) | ||
: []; | ||
const fromBody = currentResponse.value.body | ||
? retrieveTypeReferencesFromBody(currentResponse.value.body) | ||
: []; | ||
return typeReferencesAcc.concat(fromHeaders).concat(fromBody); | ||
}, []); | ||
} | ||
/** | ||
* Retrieve all type references from a body. This will only retrieve direct references (not recursive). | ||
* | ||
* @param body a body | ||
*/ | ||
function retrieveTypeReferencesFromBody(body) { | ||
const type = body.value.type; | ||
if (types_1.isReferenceType(type)) { | ||
return [type]; | ||
} | ||
else if (types_1.isUnionType(type)) { | ||
return type.types.reduce((typeAcc, currType) => types_1.isReferenceType(currType) ? typeAcc.concat(currType) : typeAcc, []); | ||
} | ||
else { | ||
return []; | ||
} | ||
} | ||
/** | ||
* Retrieve all type references from a collection of headers, path params or query params. | ||
* This will only retrieve direct references (not recursive). | ||
* | ||
* @param nodes a collection of headers, path params or query params | ||
*/ | ||
function retrieveTypeReferencesFromHeadersPathParamsQueryParams(nodes) { | ||
return nodes.reduce((typeReferencesAcc, currentHeader) => { | ||
const type = currentHeader.value.type; | ||
if (types_1.isReferenceType(type)) { | ||
return typeReferencesAcc.concat(type); | ||
} | ||
else if (types_1.isUnionType(type)) { | ||
return typeReferencesAcc.concat(type.types.reduce((typeAcc, currType) => types_1.isReferenceType(currType) ? typeAcc.concat(currType) : typeAcc, [])); | ||
} | ||
else { | ||
return typeReferencesAcc; | ||
} | ||
}, []); | ||
} | ||
/** | ||
* Return the unique collection of reference types from a collection of reference types. | ||
@@ -157,0 +139,0 @@ * |
@@ -1,1 +0,1 @@ | ||
{"version":"0.2.4","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 --contract api.ts --language yaml --generator openapi3 --out output/"],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false},"contract":{"name":"contract","type":"option","char":"c","description":"Path to a TypeScript Contract 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":[]},"lint":{"id":"lint","description":"Lint a Spot contract","pluginName":"@airtasker/spot","pluginType":"core","aliases":[],"examples":["$ spot lint 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}]},"mock":{"id":"mock","description":"Run a mock server based on a Spot contract","pluginName":"@airtasker/spot","pluginType":"core","aliases":[],"examples":["$ spot mock api.ts"],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false},"port":{"name":"port","type":"option","char":"p","description":"Port on which to run the mock server","required":true,"default":3010},"pathPrefix":{"name":"pathPrefix","type":"option","description":"Prefix to prepend to each endpoint path"}},"args":[{"name":"spot_contract","description":"path to Spot contract","required":true,"hidden":false}]},"test":{"id":"test","description":"Test a Spot contract","pluginName":"@airtasker/spot","pluginType":"core","aliases":[],"examples":["$ spot test api.ts -u http://localhost:3000","$ spot test api.ts -u http://localhost:3000 -t MyEndpoint:myTest"],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false},"url":{"name":"url","type":"option","char":"u","description":"Base URL","required":true},"stateUrl":{"name":"stateUrl","type":"option","char":"s","description":"State change URL"},"testFilter":{"name":"testFilter","type":"option","char":"t","description":"Filter by endpoint and test"}},"args":[{"name":"spot_contract","description":"path to Spot contract","required":true,"hidden":false}]},"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.2.5","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 --contract api.ts --language yaml --generator openapi3 --out output/"],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false},"contract":{"name":"contract","type":"option","char":"c","description":"Path to a TypeScript Contract 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":[]},"lint":{"id":"lint","description":"Lint a Spot contract","pluginName":"@airtasker/spot","pluginType":"core","aliases":[],"examples":["$ spot lint 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}]},"mock":{"id":"mock","description":"Run a mock server based on a Spot contract","pluginName":"@airtasker/spot","pluginType":"core","aliases":[],"examples":["$ spot mock api.ts"],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false},"port":{"name":"port","type":"option","char":"p","description":"Port on which to run the mock server","required":true,"default":3010},"pathPrefix":{"name":"pathPrefix","type":"option","description":"Prefix to prepend to each endpoint path"}},"args":[{"name":"spot_contract","description":"path to Spot contract","required":true,"hidden":false}]},"test":{"id":"test","description":"Test a Spot contract","pluginName":"@airtasker/spot","pluginType":"core","aliases":[],"examples":["$ spot test api.ts -u http://localhost:3000","$ spot test api.ts -u http://localhost:3000 -t MyEndpoint:myTest"],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false},"url":{"name":"url","type":"option","char":"u","description":"Base URL","required":true},"stateUrl":{"name":"stateUrl","type":"option","char":"s","description":"State change URL"},"testFilter":{"name":"testFilter","type":"option","char":"t","description":"Filter by endpoint and test"}},"args":[{"name":"spot_contract","description":"path to Spot contract","required":true,"hidden":false}]},"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.2.4", | ||
"version": "0.2.5", | ||
"author": "Francois Wouts, Leslie Fung", | ||
@@ -15,2 +15,3 @@ "bin": { | ||
"@types/express": "^4.16.0", | ||
"@types/qs": "^6.5.3", | ||
"@types/ramda": "^0.26.6", | ||
@@ -26,4 +27,5 @@ "@types/randomstring": "^1.1.6", | ||
"inquirer": "^6.2.0", | ||
"js-yaml": "^3.13.0", | ||
"js-yaml": "^3.13.1", | ||
"lodash": "^4.17.11", | ||
"qs": "^6.7.0", | ||
"ramda": "^0.26.1", | ||
@@ -33,3 +35,3 @@ "randomstring": "^1.1.5", | ||
"tslib": "^1", | ||
"typescript": "^3.4.1" | ||
"typescript": "^3.4.3" | ||
}, | ||
@@ -46,3 +48,3 @@ "devDependencies": { | ||
"@types/nock": "^9.3.1", | ||
"@types/node": "^11.13.0", | ||
"@types/node": "^11.13.2", | ||
"globby": "^9.2.0", | ||
@@ -52,3 +54,3 @@ "jest": "^24.7.1", | ||
"prettier": "^1.14.3", | ||
"ts-jest": "^24.0.1", | ||
"ts-jest": "^24.0.2", | ||
"ts-node": "^8.0.3", | ||
@@ -55,0 +57,0 @@ "tslint": "^5.15.0", |
@@ -140,3 +140,3 @@ # Spot | ||
_See code: [build/cli/src/commands/generate.js](https://github.com/airtasker/spot/blob/v0.2.4/build/cli/src/commands/generate.js)_ | ||
_See code: [build/cli/src/commands/generate.js](https://github.com/airtasker/spot/blob/v0.2.5/build/cli/src/commands/generate.js)_ | ||
@@ -179,3 +179,3 @@ ## `spot help [COMMAND]` | ||
_See code: [build/cli/src/commands/init.js](https://github.com/airtasker/spot/blob/v0.2.4/build/cli/src/commands/init.js)_ | ||
_See code: [build/cli/src/commands/init.js](https://github.com/airtasker/spot/blob/v0.2.5/build/cli/src/commands/init.js)_ | ||
@@ -200,3 +200,3 @@ ## `spot lint SPOT_CONTRACT` | ||
_See code: [build/cli/src/commands/lint.js](https://github.com/airtasker/spot/blob/v0.2.4/build/cli/src/commands/lint.js)_ | ||
_See code: [build/cli/src/commands/lint.js](https://github.com/airtasker/spot/blob/v0.2.5/build/cli/src/commands/lint.js)_ | ||
@@ -223,3 +223,3 @@ ## `spot mock SPOT_CONTRACT` | ||
_See code: [build/cli/src/commands/mock.js](https://github.com/airtasker/spot/blob/v0.2.4/build/cli/src/commands/mock.js)_ | ||
_See code: [build/cli/src/commands/mock.js](https://github.com/airtasker/spot/blob/v0.2.5/build/cli/src/commands/mock.js)_ | ||
@@ -248,3 +248,3 @@ ## `spot test SPOT_CONTRACT` | ||
_See code: [build/cli/src/commands/test.js](https://github.com/airtasker/spot/blob/v0.2.4/build/cli/src/commands/test.js)_ | ||
_See code: [build/cli/src/commands/test.js](https://github.com/airtasker/spot/blob/v0.2.5/build/cli/src/commands/test.js)_ | ||
@@ -269,3 +269,3 @@ ## `spot validate SPOT_CONTRACT` | ||
_See code: [build/cli/src/commands/validate.js](https://github.com/airtasker/spot/blob/v0.2.4/build/cli/src/commands/validate.js)_ | ||
_See code: [build/cli/src/commands/validate.js](https://github.com/airtasker/spot/blob/v0.2.5/build/cli/src/commands/validate.js)_ | ||
<!-- commandsstop --> |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
240828
210
6220
24
+ Added@types/qs@^6.5.3
+ Addedqs@^6.7.0
Updatedjs-yaml@^3.13.1
Updatedtypescript@^3.4.3