Skip to content

Commit

Permalink
feat: generate types from openapi service yaml urls
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes Becker committed Dec 22, 2020
1 parent daa5a20 commit 5480c3c
Show file tree
Hide file tree
Showing 11 changed files with 1,821 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cypress/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
"types": ["cypress"],
"moduleResolution": "node"
},
"include": ["**/*.ts"]
"include": ["**/*.ts", "../src/generated/**/*.ts"]
}
80 changes: 80 additions & 0 deletions dtsgen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { promises as fs } from 'fs';
import path from 'path';
import dtsgenerator, { readSchemaFromUrl } from 'dtsgenerator';
import prettier from 'prettier';

const rawOrgUrl = 'https://raw.githubusercontent.com/CaritasDeutschland';
const services = [
{
path: 'caritas-onlineBeratung-userService/develop/api/userservice.yaml',
namespace: 'UserService',
out: 'userservice.d.ts'
},
{
path:
'caritas-onlineBeratung-agencyService/develop/api/agencyservice.yaml',
namespace: 'AgencyService',
out: 'agencyservice.d.ts'
},
{
path:
'caritas-onlineBeratung-uploadService/develop/api/uploadservice.yaml',
namespace: 'UploadService',
out: 'uploadservice.d.ts'
},
{
path:
'caritas-onlineBeratung-messageService/develop/api/messageservice.yaml',
namespace: 'MessageService',
out: 'messageservice.d.ts'
},
{
path: 'caritas-onlineBeratung-mailService/develop/api/mailservice.yaml',
namespace: 'MailService',
out: 'mailservice.d.ts'
},
{
path: 'caritas-onlineBeratung-liveService/develop/api/liveservice.yaml',
namespace: 'LiveService',
out: 'liveservice.d.ts'
}
];

(async () => {
try {
const prettierConfigFile = await prettier.resolveConfigFile();
const prettierConfig = await prettier.resolveConfig(prettierConfigFile);

for (const service of services) {
const schema = await readSchemaFromUrl(
`${rawOrgUrl}/${service.path}`
);
const content = await dtsgenerator({
contents: [schema],
config: {
plugins: {
'@dtsgenerator/replace-namespace': {
map: [
{
from: ['Components', 'Schema'],
to: [service.namespace]
}
]
}
}
}
});

await fs.writeFile(
path.join('src', 'generated', service.out),
prettier.format(content, {
parser: 'typescript',
...prettierConfig
})
);
}
} catch (err) {
console.error(`Something went wrong: ${err}`);
process.exit(1);
}
})();
135 changes: 135 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,15 @@
"devDependencies": {
"@commitlint/cli": "11.0.0",
"@commitlint/config-conventional": "11.0.0",
"@dtsgenerator/replace-namespace": "^1.4.1",
"@types/prettier": "^2.1.5",
"breakpoint-sass": "2.7.1",
"concurrently": "^5.3.0",
"cross-env": "^7.0.2",
"cypress": "^6.1.0",
"cypress-file-upload": "^4.1.1",
"cz-conventional-changelog": "3.3.0",
"dtsgenerator": "^3.5.0",
"husky": "4.3.0",
"lint-staged": "^10.5.1",
"mock-socket": "^9.0.3",
Expand All @@ -108,6 +111,7 @@
"prettier-eslint-cli": "5.0.0",
"serve": "^11.3.2",
"standard-version": "9.0.0",
"ts-node": "^9.1.1",
"uuid": "^8.3.2",
"wait-on": "^5.2.0"
},
Expand All @@ -122,7 +126,8 @@
"dev:cy:start-cra": "BROWSER=none npm start",
"dev:cy:wait-and-open": "run-s dev:cy:wait-and-open:*",
"dev:cy:wait-and-open:wait-on-cra": "wait-on http://caritas.local:9000",
"dev:cy:wait-and-open:open": "cross-env NODE_ENV=development cypress open"
"dev:cy:wait-and-open:open": "cross-env NODE_ENV=development cypress open",
"dtsgen": "ts-node dtsgen.ts"
},
"eslintConfig": {
"extends": [
Expand Down
Loading

0 comments on commit 5480c3c

Please sign in to comment.