Skip to content

Commit ae1afbc

Browse files
committed
✨ feat: support programmatic usage
1 parent 5b3b4fe commit ae1afbc

File tree

7 files changed

+153
-73
lines changed

7 files changed

+153
-73
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
## 0.2.2 (2021-08-31)
1+
## 0.5.3 (2021-11-16)
22

33
### Changelog
44

55
All notable changes to this project will be documented in this file. Dates are displayed in UTC.
66

77
Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
88

9-
#### 0.2.2
9+
#### 0.5.3
1010

1111
- :construction: chore: update release workflow [`5ccfa43`](https://github.com/linbudu599/JSON2TypeGraphQLClass/commit/5ccfa43994a949dd89c1582f191f438312a0240d)
1212
- refactor [`584830a`](https://github.com/linbudu599/JSON2TypeGraphQLClass/commit/584830a263349b6db7653470e9439a70baae150d)

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,42 @@ export class Root {
133133
}
134134
```
135135

136+
## Programmatic Usage
137+
138+
```typescript
139+
import {
140+
reader,
141+
parser,
142+
preprocessor,
143+
generator,
144+
writter,
145+
} from "json-type-graphql";
146+
147+
export default async function handler(options: Options): Promise<void> {
148+
// read from data source you want
149+
// you can also use custom reader
150+
const content = await reader(options.reader);
151+
152+
// make some custom processing
153+
const preprocessed = preprocessor(content, normalizedPreprocessorOptions);
154+
155+
// parse content
156+
const parsedInfo = parser(preprocessed, normalizedParserOptions);
157+
158+
fs.ensureFileSync(normalizedWritterOptions.outputPath);
159+
160+
const source = new Project().addSourceFileAtPath(
161+
normalizedWritterOptions.outputPath
162+
);
163+
164+
// generate AST and result!
165+
generator(source, parsedInfo, normalizedGeneratorOptions);
166+
167+
// write!
168+
writter(normalizedWritterOptions);
169+
}
170+
```
171+
136172
## Options
137173

138174
### Reader

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json-type-graphql",
3-
"version": "0.5.2",
3+
"version": "0.5.3",
44
"description": "Generate TypeGraphQL Class From JSON Object",
55
"main": "dist/index.js",
66
"repository": "https://github.com/linbudu599/JSON2TypeGraphQLClass.git",

src/index.ts

Lines changed: 26 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import fs from "fs-extra";
22
import { Project } from "ts-morph";
3-
import util from "util";
43

54
import { reader } from "./reader";
65
import { preprocessor } from "./preprocessor";
@@ -10,7 +9,7 @@ import { postprocessor } from "./postprocessor";
109
import { checker } from "./checker";
1110
import { writter } from "./writter";
1211

13-
import { ARRAY_ENTRY_STRUCTURE_PROP, DEFAULT_ENTRY_CLASS_NAME } from "./utils";
12+
import { normalizeOptions } from "./utils";
1413
import type { Options } from "./utils";
1514

1615
/**
@@ -22,81 +21,39 @@ import type { Options } from "./utils";
2221
export default async function handler(options: Options): Promise<void> {
2322
const content = await reader(options.reader);
2423

25-
const { preserveObjectOnlyInArray = true, customPreprocessor = undefined } =
26-
options?.preprocessor ?? {};
27-
28-
const {
29-
forceNonNullable = true,
30-
forceReturnType = false,
31-
arrayEntryProp = ARRAY_ENTRY_STRUCTURE_PROP,
32-
forceNonNullableListItem = false,
33-
} = options.parser ?? {};
34-
35-
const {
36-
prefix = false,
37-
publicProps = [],
38-
readonlyProps = [],
39-
suffix = false,
40-
entryClassName = DEFAULT_ENTRY_CLASS_NAME,
41-
sort = true,
42-
} = options.generator ?? {};
43-
44-
const { customPostprocessor = undefined } = options.postprocessor ?? {};
45-
46-
const {
47-
disable: disableChecker = true,
48-
keep = false,
49-
execaOptions = {},
50-
executeOptions = {},
51-
buildSchemaOptions = {},
52-
} = options.checker ?? {};
53-
5424
const {
55-
format = true,
56-
override,
57-
formatOptions,
58-
outputPath,
59-
} = options.writter ?? {};
25+
normalizedPreprocessorOptions,
26+
normalizedParserOptions,
27+
normalizedGeneratorOptions,
28+
normalizedPostprocessorOptions,
29+
normalizedCheckerOptions,
30+
normalizedWritterOptions,
31+
} = normalizeOptions(options);
6032

6133
const originInput = content;
6234

63-
const preprocessed = preprocessor(originInput, {
64-
preserveObjectOnlyInArray,
65-
customPreprocessor,
66-
});
35+
const preprocessed = preprocessor(originInput, normalizedPreprocessorOptions);
6736

68-
const parsedInfo = parser(preprocessed, {
69-
forceNonNullable,
70-
forceReturnType,
71-
arrayEntryProp,
72-
forceNonNullableListItem,
73-
});
37+
const parsedInfo = parser(preprocessed, normalizedParserOptions);
7438

75-
fs.ensureFileSync(outputPath);
39+
fs.ensureFileSync(normalizedWritterOptions.outputPath);
7640

77-
const source = new Project().addSourceFileAtPath(outputPath);
41+
const source = new Project().addSourceFileAtPath(
42+
normalizedWritterOptions.outputPath
43+
);
7844

79-
generator(source, parsedInfo, {
80-
prefix,
81-
publicProps,
82-
readonlyProps,
83-
entryClassName,
84-
suffix,
85-
sort,
86-
});
45+
generator(source, parsedInfo, normalizedGeneratorOptions);
8746

88-
postprocessor(source, {
89-
customPostprocessor,
90-
// removeUnusedDecorators,
91-
});
47+
postprocessor(source, normalizedPostprocessorOptions);
9248

93-
await checker(outputPath, {
94-
disable: sort || disableChecker,
95-
keep,
96-
execaOptions,
97-
executeOptions,
98-
buildSchemaOptions,
99-
});
100-
101-
writter({ outputPath, format, formatOptions, override });
49+
await checker(normalizedWritterOptions.outputPath, normalizedCheckerOptions);
50+
writter(normalizedWritterOptions);
10251
}
52+
53+
export * from "./reader";
54+
export * from "./preprocessor";
55+
export * from "./parser";
56+
export * from "./generator";
57+
export * from "./postprocessor";
58+
export * from "./checker";
59+
export * from "./writter";

src/sample/demo.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"numberField": 200,
1717
"stringField": "success",
1818
"primitiveArrayField": [1, 2, 3, 4, 5],
19-
"mixedFieldrs": [
19+
"mixedFields": [
2020
1,
2121
2,
2222
{

src/sample/demo.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import path from "path";
22
import fs from "fs-extra";
33
import transformer from "..";
4+
import { reader, parser, generator, checker, writter } from "..";
45

56
const outputPath = path.join(__dirname, "./generated.ts");
67

src/utils.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,92 @@ export type Options = {
155155
writter: WriterOptions;
156156
};
157157

158+
export type InferPartialTypeParam<T> = T extends Partial<infer R> ? R : T;
159+
160+
export type NormalizedOptions = {
161+
[K in keyof Omit<
162+
Options,
163+
"reader"
164+
> as `normalized${Capitalize<K>}Options`]-?: InferPartialTypeParam<
165+
Options[K]
166+
>;
167+
};
168+
169+
export function normalizeOptions(options: Options): NormalizedOptions {
170+
const { preserveObjectOnlyInArray = true, customPreprocessor = undefined } =
171+
options?.preprocessor ?? {};
172+
173+
const {
174+
forceNonNullable = true,
175+
forceReturnType = false,
176+
arrayEntryProp = ARRAY_ENTRY_STRUCTURE_PROP,
177+
forceNonNullableListItem = false,
178+
} = options.parser ?? {};
179+
180+
const {
181+
prefix = false,
182+
publicProps = [],
183+
readonlyProps = [],
184+
suffix = false,
185+
entryClassName = DEFAULT_ENTRY_CLASS_NAME,
186+
sort = true,
187+
} = options.generator ?? {};
188+
189+
const { customPostprocessor = undefined } = options.postprocessor ?? {};
190+
191+
const {
192+
disable: disableChecker = true,
193+
keep = false,
194+
execaOptions = {},
195+
executeOptions = {},
196+
buildSchemaOptions = {},
197+
} = options.checker ?? {};
198+
199+
const {
200+
format = true,
201+
override = false,
202+
formatOptions = {},
203+
outputPath,
204+
} = options.writter ?? {};
205+
206+
return {
207+
normalizedPreprocessorOptions: {
208+
preserveObjectOnlyInArray,
209+
customPreprocessor,
210+
},
211+
normalizedParserOptions: {
212+
forceNonNullable,
213+
forceReturnType,
214+
arrayEntryProp,
215+
forceNonNullableListItem,
216+
},
217+
normalizedGeneratorOptions: {
218+
prefix,
219+
publicProps,
220+
readonlyProps,
221+
suffix,
222+
entryClassName,
223+
sort,
224+
},
225+
normalizedPostprocessorOptions: {
226+
customPostprocessor,
227+
},
228+
normalizedCheckerOptions: {
229+
disable: sort || disableChecker,
230+
keep,
231+
execaOptions,
232+
executeOptions,
233+
buildSchemaOptions,
234+
},
235+
normalizedWritterOptions: {
236+
format,
237+
override,
238+
formatOptions,
239+
outputPath,
240+
},
241+
};
242+
}
243+
158244
export type ProcessedFieldInfoObject = Record<string, ParsedFieldInfo>;
159245

160246
/**

0 commit comments

Comments
 (0)