forked from xolvio/chimp-datasources-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [add-circuit-breaker-to-api] Allows addition of circuit breaker…
… to api calls
- Loading branch information
Showing
13 changed files
with
22,624 additions
and
7,729 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,5 @@ | |
node_modules | ||
coverage | ||
.eslintcache | ||
.sdkmanrc | ||
sample-apis/generated |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const extractDataSource = require("./customDataSourceExtractor"); | ||
const extractCircuitBreakerConfig = require("./extractCircuitBreakerConfig"); | ||
|
||
const buildSpecGenCommandWithOptions = ( | ||
pathToSwaggerJar, | ||
pathToTemplate, | ||
pathToSpecFile, | ||
codeOutputFolder, | ||
serviceName, | ||
options | ||
) => { | ||
const additionalProps = []; | ||
let shellCommand = `java -jar ${pathToSwaggerJar} generate -l typescript-fetch --template-dir ${pathToTemplate} -i ${pathToSpecFile} -o ${codeOutputFolder}`; | ||
|
||
additionalProps.push(`serviceName=${serviceName}`); | ||
|
||
if (options.withCircuitBreaker) { | ||
additionalProps.push("withCircuitBreaker=true"); | ||
} | ||
|
||
if (options.circuitBreakerConfig) { | ||
const config = extractCircuitBreakerConfig(options.circuitBreakerConfig); | ||
if (config) { | ||
const { circuitBreakerImportString, circuitBreakerConfigFn } = config; | ||
additionalProps.push( | ||
`circuitBreakerImportString=${circuitBreakerImportString}` | ||
); | ||
additionalProps.push( | ||
`circuitBreakerConfigFn="${circuitBreakerConfigFn}"` | ||
); | ||
} | ||
} | ||
|
||
if (options.dataSourceImport) { | ||
const { dataSourceName, importString } = extractDataSource( | ||
options.dataSourceImport | ||
); | ||
additionalProps.push(`dataSourceName=${dataSourceName}`); | ||
additionalProps.push(`dataSourceImportString="${importString}"`); | ||
} else { | ||
additionalProps.push("dataSourceName=RESTDataSource"); | ||
} | ||
|
||
if (additionalProps.length > 0) { | ||
shellCommand = shellCommand.concat( | ||
` --additional-properties=${additionalProps.toString()}` | ||
); | ||
} | ||
return shellCommand; | ||
}; | ||
|
||
module.exports = buildSpecGenCommandWithOptions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
const buildSpecGenCommandWithOptions = require("./buildSpecGenCommandWithOptions"); | ||
|
||
describe("Build spec gen command test", () => { | ||
const pathToSpecFile = "/spec/input.json"; | ||
const codeOutputFolder = "/output"; | ||
const pathToTemplate = "/template"; | ||
const pathToSwaggerJar = "/swagger.jar"; | ||
const baseCommand = | ||
"java -jar /swagger.jar generate -l typescript-fetch --template-dir /template -i /spec/input.json -o /output"; | ||
test("should create command for no options passed", () => { | ||
const options = {}; | ||
const result = buildSpecGenCommandWithOptions( | ||
pathToSwaggerJar, | ||
pathToTemplate, | ||
pathToSpecFile, | ||
codeOutputFolder, | ||
"test-service-name", | ||
options | ||
); | ||
expect(result).toBe( | ||
`${baseCommand} --additional-properties=serviceName=test-service-name,dataSourceName=RESTDataSource` | ||
); | ||
}); | ||
|
||
test("should create command for custom datasource passed", () => { | ||
const options = { | ||
dataSourceImport: "@app/apis/DataSource#CustomDataSource", | ||
}; | ||
const result = buildSpecGenCommandWithOptions( | ||
pathToSwaggerJar, | ||
pathToTemplate, | ||
pathToSpecFile, | ||
codeOutputFolder, | ||
"test-service-name", | ||
options | ||
); | ||
expect(result).toBe( | ||
`${baseCommand} --additional-properties=serviceName=test-service-name,dataSourceName=CustomDataSource,dataSourceImportString="@app/apis/DataSource"` | ||
); | ||
}); | ||
|
||
test("should create command for custom datasource passed", () => { | ||
const options = { | ||
dataSourceImport: "@app/apis/DataSource#CustomDataSource", | ||
}; | ||
const result = buildSpecGenCommandWithOptions( | ||
pathToSwaggerJar, | ||
pathToTemplate, | ||
pathToSpecFile, | ||
codeOutputFolder, | ||
"test-service-name", | ||
options | ||
); | ||
expect(result).toBe( | ||
`${baseCommand} --additional-properties=serviceName=test-service-name,dataSourceName=CustomDataSource,dataSourceImportString="@app/apis/DataSource"` | ||
); | ||
}); | ||
|
||
test("should create command for circuit breaker options", () => { | ||
const options = { withCircuitBreaker: "--withCircuitBreaker" }; | ||
const result = buildSpecGenCommandWithOptions( | ||
pathToSwaggerJar, | ||
pathToTemplate, | ||
pathToSpecFile, | ||
codeOutputFolder, | ||
"test-service-name", | ||
options | ||
); | ||
expect(result).toBe( | ||
`${baseCommand} --additional-properties=serviceName=test-service-name,withCircuitBreaker=true,dataSourceName=RESTDataSource` | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
module.exports = (dataSourcePath) => { | ||
const extractDataSource = (dataSourcePath) => { | ||
const split = dataSourcePath.split("#"); | ||
|
||
if (split.length > 1) { | ||
return { | ||
importString: `import { ${split[1]} } from "${split[0]}"`, | ||
importString: `${split[0]}`, | ||
dataSourceName: split[1], | ||
}; | ||
} | ||
return { | ||
importString: `import DataSource from "${split[0]}"`, | ||
importString: `${split[0]}`, | ||
dataSourceName: "DataSource", | ||
}; | ||
}; | ||
|
||
module.exports = extractDataSource; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
const extractor = require("./customDataSourceExtractor"); | ||
const extractDataSource = require("./customDataSourceExtractor"); | ||
|
||
test("named import", () => { | ||
const dataSourcePath = "@app/apis/DataSource#CustomDataSource"; | ||
expect(extractor(dataSourcePath)).toEqual({ | ||
importString: 'import { CustomDataSource } from "@app/apis/DataSource"', | ||
expect(extractDataSource(dataSourcePath)).toEqual({ | ||
importString: "@app/apis/DataSource", | ||
dataSourceName: "CustomDataSource", | ||
}); | ||
}); | ||
|
||
test("default import", () => { | ||
const dataSourcePath = "@app/MyDataSource"; | ||
expect(extractor(dataSourcePath)).toEqual({ | ||
importString: 'import DataSource from "@app/MyDataSource"', | ||
expect(extractDataSource(dataSourcePath)).toEqual({ | ||
importString: "@app/MyDataSource", | ||
dataSourceName: "DataSource", | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const extractCircuitBreakerConfig = (circuitBreakerConfig = "") => { | ||
const split = circuitBreakerConfig.split("#"); | ||
|
||
if (split.length > 1) { | ||
return { | ||
circuitBreakerImportString: `${split[0]}`, | ||
circuitBreakerConfigFn: split[1], | ||
}; | ||
} | ||
return undefined; | ||
}; | ||
|
||
module.exports = extractCircuitBreakerConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const extractCircuitBreakerConfig = require("./extractCircuitBreakerConfig"); | ||
|
||
describe("extract circuit breaker config", () => { | ||
test("should return imports for circuit breaker config", () => { | ||
const dataSourcePath = | ||
"@app/circuit-breakers/CircuitBreakerConfig#getServiceCircuitBreaker"; | ||
expect(extractCircuitBreakerConfig(dataSourcePath)).toEqual({ | ||
circuitBreakerImportString: "@app/circuit-breakers/CircuitBreakerConfig", | ||
circuitBreakerConfigFn: "getServiceCircuitBreaker", | ||
}); | ||
}); | ||
|
||
test("should return undefined", () => { | ||
let dataSourcePath; | ||
expect(extractCircuitBreakerConfig(dataSourcePath)).not.toBeDefined(); | ||
|
||
dataSourcePath = "undefined"; | ||
expect(extractCircuitBreakerConfig(dataSourcePath)).not.toBeDefined(); | ||
}); | ||
}); |
Oops, something went wrong.