Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module.exports = {
root: true,
env: {
jest: true,
},
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended', 'prettier'],
Expand Down
2 changes: 2 additions & 0 deletions packages/taxios-generate/.npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Source files
/src
/tsconfig.build.json
/index.test.js
/jest.config.js
33 changes: 33 additions & 0 deletions packages/taxios-generate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

## Generate

### CLI

```sh
taxios-generate [options] <input-file-or-url>
```
Expand All @@ -18,14 +20,45 @@ Options:
--sort-fields [0.2.10+] Sort fields in interfaces instead of keeping the order from source
```

### As module (programmatically)

```typescript
interface GenerateProps {
exportName: string;
inputPath: string;
outputPath?: string;
skipValidate?: boolean;
sortFields?: boolean;
unionEnums?: boolean;
keepAdditionalProperties?: boolean;
}

export function generate(options: GenerateProps): Promise<string>;
```

## Example

Swagger: https://petstore.swagger.io/

### CLI

```
taxios-generate -o PetStore.ts -e PetStore https://petstore.swagger.io/v2/swagger.json
```

### As module (programmatically)

```javascript
import { generate } from '@simplesmiler/taxios-generate';
// import taxiosGenerate from '@simplesmiler/taxios-generate';

const result = await generate({
exportName: 'PetStore',
inputPath: 'https://petstore.swagger.io/v2/swagger.json',
outputPath: path.resolve('./PetStore.ts'), // optional
});
```

Result: [PetStore.ts](https://github.com/simplesmiler/taxios/blob/master/packages/taxios-sandbox/src/generated/PetStore.ts)

## Use
Expand Down
75 changes: 75 additions & 0 deletions packages/taxios-generate/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const outerFs = require('fs').promises;
const path = require('path');
const taxiosGenerate = require('./dist/index').default;

const generatedDir = path.resolve('./generated');

const removeGeneratedDir = () => {
return outerFs
.stat(generatedDir) // exists
.catch(() => {
return null;
})
.then((stats) => {
if (!stats) return;
return outerFs.rmdir(generatedDir, { recursive: true });
});
};

beforeAll(removeGeneratedDir);
afterAll(removeGeneratedDir);

describe('Taxios Generate (without CLI)', () => {
describe('Generate without output path', () => {
test('When minimal config passed and inputPath is external link, then result contains "PetstoreAPI"', async () => {
const result = await taxiosGenerate({
exportName: 'PetstoreAPI',
inputPath: 'https://petstore.swagger.io/v2/swagger.json',
});

expect(result).toContain('PetstoreAPI');
});

test('When full config passed and inputPath is external link, then result contains "PetstoreAPI"', async () => {
const result = await taxiosGenerate({
exportName: 'PetstoreAPI',
inputPath: 'https://petstore.swagger.io/v2/swagger.json',
skipValidate: true,
sortFields: true,
unionEnums: true,
keepAdditionalProperties: true,
});

expect(result).toContain('PetstoreAPI');
});
});

describe('Generate with output path', () => {
const outputPath = path.resolve(generatedDir, './PetstoreAPI.ts');

test(`When minimal config passed and inputPath is external link, then result emits in ${outputPath} file`, async () => {
await taxiosGenerate({
exportName: 'PetstoreAPI',
inputPath: 'https://petstore.swagger.io/v2/swagger.json',
outputPath,
});

const stats = await outerFs.stat(outputPath);

expect(stats).not.toBeNull();
});

test(`When minimal config passed and inputPath is external link, then emitted result in ${outputPath} file contains "PetstoreAPI"`, async () => {
await taxiosGenerate({
exportName: 'PetstoreAPI',
inputPath: 'https://petstore.swagger.io/v2/swagger.json',
outputPath,
});

const result = await outerFs.readFile(outputPath, { encoding: 'utf8' });

expect(result).toContain('PetstoreAPI');
});
});
});
4 changes: 4 additions & 0 deletions packages/taxios-generate/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// eslint-disable-next-line no-undef
module.exports = {
name: 'taxios-generate',
};
6 changes: 5 additions & 1 deletion packages/taxios-generate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"author": "Denis Karabaza <denis.karabaza@gmail.com>",
"homepage": "https://github.com/simplesmiler/taxios/packages/taxios-generate",
"license": "ISC",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"bin": "dist/bin.js",
"repository": {
"type": "git",
Expand All @@ -22,7 +24,8 @@
"scripts": {
"build": "npm run clean && npm run compile",
"clean": "shx rm -rf ./dist",
"compile": "tsc -p tsconfig.build.json"
"compile": "tsc -p tsconfig.build.json",
"test": "jest"
},
"dependencies": {
"@apidevtools/swagger-parser": "^10.0.3",
Expand All @@ -39,6 +42,7 @@
},
"devDependencies": {
"@types/node": "^16.7.1",
"jest": "^27.5.1",
"shx": "^0.3.3",
"typescript": "^4.3.5"
},
Expand Down
Loading