Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…pt-codegen into union-bugs
  • Loading branch information
mrlubos committed Jan 30, 2024
2 parents 4174eb7 + 76e6851 commit 41743e8
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 18 deletions.
27 changes: 22 additions & 5 deletions .github/workflows/unittest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,32 @@ jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- name: Checkout
uses: actions/[email protected]

- name: Setup Node environment
uses: actions/[email protected]
with:
node-version: 20

- name: Cache Modules
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: "**/node_modules"
key: ${{ runner.os }}-modules-${{ hashFiles('**/package-lock.json') }}
- run: npm install
- run: npm run test

- name: Install dependencies
run: npm install

- name: Build library
run: npm run release

- name: Run unit tests
run: npm run test

# - name: Run e2e tests
# run: npm run test:e2e

# - name: Submit to Codecov
# run: npm run codecov

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ $ openapi --help
--useUnionTypes Use union types instead of enums
--exportCore <value> Write core files to disk (default: true)
--exportServices <value> Write services to disk [true, false, regexp] (default: true)
--exportModels <value> Write models to disk (default: true)
--exportModels <value> Write models to disk [true, false, regexp] (default: true)
--exportSchemas <value> Write schemas to disk (default: false)
--indent <value> Indentation options [4, 2, tab] (default: "4")
--postfixServices Service name postfix (default: "Service")
Expand Down
14 changes: 8 additions & 6 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ const params = program

const OpenAPI = require(path.resolve(__dirname, '../dist/index.js'));

if (OpenAPI) {
let exportServices;
const parseBooleanOrString = value => {
try {
exportServices = JSON.parse(params.exportServices) === true;
return JSON.parse(value) === true;
} catch (error) {
exportServices = params.exportServices;
return value;
}
};

if (OpenAPI) {
OpenAPI.generate({
input: params.input,
output: params.output,
Expand All @@ -44,8 +46,8 @@ if (OpenAPI) {
useOptions: params.useOptions,
useUnionTypes: params.useUnionTypes,
exportCore: JSON.parse(params.exportCore) === true,
exportServices,
exportModels: JSON.parse(params.exportModels) === true,
exportServices: parseBooleanOrString(params.exportServices),
exportModels: parseBooleanOrString(params.exportModels),
exportSchemas: JSON.parse(params.exportSchemas) === true,
indent: params.indent,
postfixServices: params.postfixServices,
Expand Down
4 changes: 3 additions & 1 deletion bin/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('bin', () => {
expect(result.stderr.toString()).toBe('');
});

it('it should support regexp in exportSchemas', async () => {
it('it should support regexp params', async () => {
const result = crossSpawn.sync('node', [
'./bin/index.js',
'--input',
Expand All @@ -52,6 +52,8 @@ describe('bin', () => {
'./test/generated/bin',
'--exportServices',
'^(Simple|Types)',
'--exportModels',
'^(Simple|Types)',
]);
expect(result.stdout.toString()).toBe('');
expect(result.stderr.toString()).toBe('');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@canoapbc/openapi-typescript-codegen",
"name": "@nicolas-chaulet/openapi-typescript-codegen",
"version": "0.27.2",
"description": "Library that generates Typescript clients based on the OpenAPI specification.",
"author": "Ferdi Koomen",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export type Options = {
useUnionTypes?: boolean;
exportCore?: boolean;
exportServices?: boolean | string;
exportModels?: boolean;
exportModels?: boolean | string;
exportSchemas?: boolean;
indent?: Indent;
postfixServices?: string;
Expand Down
7 changes: 6 additions & 1 deletion src/utils/writeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const writeClient = async (
useUnionTypes: boolean,
exportCore: boolean,
exportServices: boolean | string,
exportModels: boolean,
exportModels: boolean | string,
exportSchemas: boolean,
indent: Indent,
postfixServices: string,
Expand All @@ -65,6 +65,11 @@ export const writeClient = async (
client.services = client.services.filter(service => regexp.test(service.name));
}

if (typeof exportModels === 'string') {
const regexp = new RegExp(exportModels);
client.models = client.models.filter(model => regexp.test(model.name));
}

if (exportCore) {
await rmdir(outputPathCore);
await mkdir(outputPathCore);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/writeClientIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const writeClientIndex = async (
useUnionTypes: boolean,
exportCore: boolean,
exportServices: boolean | string,
exportModels: boolean,
exportModels: boolean | string,
exportSchemas: boolean,
postfixServices: string,
postfixModels: string,
Expand Down
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export type Options = {
useUnionTypes?: boolean;
exportCore?: boolean;
exportServices?: boolean | string;
exportModels?: boolean;
exportModels?: boolean | string;
exportSchemas?: boolean;
indent?: Indent | '4' | '2' | 'tab';
postfixServices?: string;
Expand Down

0 comments on commit 41743e8

Please sign in to comment.