Skip to content

Commit 9d6e079

Browse files
committed
feat: add index.ts, generate function and update main section
BREAKING CHANGE: move serializer to languages/typescript, rename default serializer
1 parent 8cbae07 commit 9d6e079

File tree

6 files changed

+73
-32
lines changed

6 files changed

+73
-32
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "@devexperts/swagger-codegen-ts",
33
"version": "0.1.0",
44
"description": "TS generator for swagger spec",
5-
"main": "index.js",
5+
"main": "dist/index.js",
66
"scripts": {
77
"ts-node": "ts-node",
88
"tslint": "tslint -c tslint.json --project tsconfig.json './src/**/*.ts' './test/**/*.ts'",

src/index.ts

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { SwaggerObject } from './swagger';
2+
import * as prettier from 'prettier';
3+
import { map, write } from './fs';
4+
import { TSerializer } from './utils';
5+
import * as fs from 'fs-extra';
6+
import { ThrowReporter } from 'io-ts/lib/ThrowReporter';
7+
import { fromNullable } from 'fp-ts/lib/Option';
8+
import * as path from 'path';
9+
10+
const log = console.log.bind(console, '[SWAGGER-CODEGEN-TS]:');
11+
12+
export type TGenerateOptions = {
13+
/**
14+
* Absolute path to json spec
15+
*/
16+
pathToSpec: string;
17+
/**
18+
* Absolute path to output directory (should be empty)
19+
*/
20+
out: string;
21+
/**
22+
* Spec serializer
23+
*/
24+
serialize: TSerializer;
25+
};
26+
27+
export async function generate(options: TGenerateOptions): Promise<void> {
28+
const { pathToSpec, out, serialize } = options;
29+
log('Reading spec from', pathToSpec);
30+
const buffer = await fs.readFile(pathToSpec);
31+
log('Parsing spec');
32+
const json = buffer.toJSON();
33+
log('Decoding spec');
34+
const decoded = SwaggerObject.decode(json);
35+
if (decoded.isLeft()) {
36+
ThrowReporter.report(decoded);
37+
return;
38+
}
39+
log('Serializing spec');
40+
const specName = path.dirname(out);
41+
const serialized = serialize(specName, decoded.value);
42+
log('Resolving .prettierrc');
43+
const prettierConfig = fromNullable(await prettier.resolveConfig(path.resolve(__dirname, '../.prettierrc')));
44+
const formatted = prettierConfig
45+
.map(config => map(serialized, content => prettier.format(content, config)))
46+
.getOrElse(serialized);
47+
log('Writing to', out);
48+
const destination = path.basename(out);
49+
if (!fs.pathExists(destination)) {
50+
await fs.mkdirp(destination);
51+
await write(destination, formatted);
52+
}
53+
log('Done.');
54+
}

src/serializer.ts renamed to src/language/typescript.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import {
1111
TResponsesObject,
1212
TSchemaObject,
1313
TSwaggerObject,
14-
} from './swagger';
15-
import { directory, file, TDirectory, TFile } from './fs';
14+
} from '../swagger';
15+
import { directory, file, TDirectory, TFile } from '../fs';
1616
import { array, catOptions, uniq } from 'fp-ts/lib/Array';
1717
import { getRecordSetoid, Setoid, setoidString } from 'fp-ts/lib/Setoid';
1818
import { groupBy } from 'fp-ts/lib/NonEmptyArray';
@@ -21,7 +21,8 @@ import {
2121
getOperationParametersInPath,
2222
getOperationParametersInQuery,
2323
groupPathsByTag,
24-
} from './utils';
24+
TSerializer,
25+
} from '../utils';
2526
import { none, Option, some } from 'fp-ts/lib/Option';
2627
import { getArrayMonoid, getRecordMonoid, monoidString, fold, monoidAny } from 'fp-ts/lib/Monoid';
2728
import { camelize } from '@devexperts/utils/dist/string/string';
@@ -112,7 +113,7 @@ const intercalateSerializedParameter = intercalate(monoidSerializedParameter, ar
112113
const uniqString = uniq(setoidString);
113114
const uniqSerializedWithoutDependencies = uniq(setoidSerializedTypeWithoutDependencies);
114115

115-
export const serializeSwaggerObject = (name: string, swaggerObject: TSwaggerObject): TDirectory =>
116+
export const serialize: TSerializer = (name: string, swaggerObject: TSwaggerObject): TDirectory =>
116117
directory(name, [
117118
directory('client', [file('client.ts', client)]),
118119
...catOptions([swaggerObject.definitions.map(serializeDefinitions)]),

src/utils.ts

+4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ import {
88
TPathParameterObject,
99
TReferenceObject,
1010
TBodyParameterObject,
11+
TSwaggerObject,
1112
} from './swagger';
1213
import { tuple } from 'fp-ts/lib/function';
1314
import { setoidString } from 'fp-ts/lib/Setoid';
1415
import { TQueryParameterObject } from './swagger';
16+
import { TFSEntity } from './fs';
17+
18+
export type TSerializer = (name: string, schema: TSwaggerObject) => TFSEntity;
1519

1620
export const getOperationsFromPath = (path: TPathItemObject): TDictionary<TOperationObject> => {
1721
const result: TDictionary<TOperationObject> = {};

test/index.ts

+7-26
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,14 @@
11
#!/usr/bin/env node
22

33
import * as path from 'path';
4-
import * as fs from 'fs-extra';
5-
import { fromNullable } from 'fp-ts/lib/Option';
6-
import * as prettier from 'prettier';
7-
8-
import { SwaggerObject } from '../src/swagger';
9-
import { map, write } from '../src/fs';
10-
import { serializeSwaggerObject } from '../src/serializer';
4+
import { generate } from '../src';
5+
import { serialize } from '../src/language/typescript';
116

127
const dirname = path.resolve(__dirname);
138
const root = path.resolve(dirname, '..');
14-
const destination = path.resolve('./out');
15-
const name = 'spec';
16-
const json = path.resolve(root, './specs/swagger.json');
17-
18-
const prettierConfig = path.resolve(root, '.prettierrc');
199

20-
async function run() {
21-
const raw = await fs.readFile(json);
22-
const spec = JSON.parse(raw.toString());
23-
const decoded = SwaggerObject.decode(spec);
24-
const serialized = decoded.map(spec => serializeSwaggerObject(name, spec));
25-
const config = fromNullable(await prettier.resolveConfig(prettierConfig));
26-
await fs.remove(destination);
27-
if (serialized.isRight() && config.isSome()) {
28-
const formatted = map(serialized.value, serialized => prettier.format(serialized, config.value));
29-
await fs.mkdirp(destination);
30-
await write(destination, formatted);
31-
}
32-
}
33-
run();
10+
generate({
11+
pathToSpec: path.resolve(root, './specs/swagger.json'),
12+
out: path.resolve('./out/spec'),
13+
serialize,
14+
}).catch(error => console.error(error));

tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"noUnusedLocals": true
99
},
1010
"exclude": [
11-
"node_modules"
11+
"node_modules",
12+
"dist"
1213
]
1314
}

0 commit comments

Comments
 (0)