Skip to content

Commit 5b3b4fe

Browse files
committed
✏️ docs: update docs
1 parent 9e80d80 commit 5b3b4fe

File tree

2 files changed

+75
-67
lines changed

2 files changed

+75
-67
lines changed

.cz-config.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ module.exports = {
99
{ value: ":zap: perf", name: "perf: " },
1010
{ value: ":construction_worker: ci", name: "ci: " },
1111
{ value: ":green_heart: ci", name: "ci: " },
12-
{ value: ":white_check_mark: test", name: "test: 增删测试" },
12+
{ value: ":white_check_mark: test", name: "test: " },
1313
{ value: ":hammer: refactor", name: "refactor: " },
14-
{ value: ":lock: fix", name: "fix: 修正安全问题" },
15-
{ value: ":rocket: deploy", name: "deploy: 部署" },
16-
{ value: ":art: style", name: "style: 代码样式" },
14+
{ value: ":lock: fix", name: "fix: " },
15+
{ value: ":rocket: deploy", name: "deploy: " },
16+
{ value: ":art: style", name: "style: " },
1717
{ value: ":heavy_plus_sign: add", name: "add: " },
1818
{ value: ":fire: del", name: "del: " },
1919
{ value: ":pencil2: docs", name: "docs: " },

README.md

Lines changed: 71 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,13 @@ pnpm i json-type-graphql --save
1212

1313
This project is still under heavy development, the documentation is far from ready, but basic features are already supported:
1414

15-
- Nest object type generation.
15+
- Support `nested object type`(like: `{ foo: { bar: { baz:{} } } }`) and `array entry json`(like: `[{},{}]`) type generation.
1616
- Normal generator order as `P-C1-C11-C12-C2-C21-C3-C31`.
17-
- Fully customizable processing flow: reader -> preprocessor -> parser -> generator -> postprocessor -> writer
17+
- Customizable processing flow: reader / preprocessor / postprocessor / writer
1818
- ...
1919

20-
### Towards V 1.0
21-
22-
- [ ] Detailed Documentation
23-
- [ ] Unit Test
24-
- [ ] Features
25-
- [ ] Support full customizable.
26-
- [ ] Powerful postprocessor.
27-
- [ ] Better checker.
28-
- [ ] Basic AST utils to use.
29-
- [ ] Better control on field return type.
30-
- [ ] `buildSchemaSync` options for checker.
31-
3220
## Example
3321

34-
Generate TypeGraphQL class from JSON object:
35-
3622
> Run `yarn demo` to explore!
3723
3824
JSON:
@@ -64,32 +50,31 @@ JSON:
6450
```typescript
6551
import path from "path";
6652
import fs from "fs-extra";
67-
import transformer from "..";
68-
69-
const outputPath = path.join(__dirname, "./generated.ts");
70-
71-
fs.existsSync(outputPath) && fs.rmSync(outputPath);
53+
import transformer from "json-type-garphql";
7254

7355
(async () => {
7456
await transformer({
57+
// Provide json file path
7558
reader: { path: path.join(__dirname, "./demo.json") },
59+
// Customize parser behaviour
7660
parser: {
7761
forceNonNullable: false,
78-
forceReturnType: false,
79-
forceNonNullableListItem: false,
8062
},
63+
// Customize generator behaviour
8164
generator: { entryClassName: "Root", sort: true },
65+
// Check can generated TypeGraphQL class be used normally
8266
checker: {
8367
disable: false,
8468
},
69+
// Write generated file!
8570
writter: {
86-
outputPath,
71+
outputPath: path.join(__dirname, "./generated.ts"),
8772
},
8873
});
8974
})();
9075
```
9176

92-
> More options can be found in [utils.ts](./src/utils.ts)
77+
> More options will be introduced below.
9378
9479
generated:
9580

@@ -148,89 +133,112 @@ export class Root {
148133
}
149134
```
150135

151-
## Simple Documentation
136+
## Options
152137

153138
### Reader
154139

155-
Reader handles content obtainment from JSON file / URL request / raw JavaScript object, you must provide at least one of `reader.path` / `reader.url` / `reader.raw` options.
140+
**Reader** is responsible for reading data from different sources including `JSON File` / `URL Request` / `Raw JavaScript Object`, you must provide one of `reader.path` / `reader.url` / `reader.raw` options.
156141

157142
#### Reader.Options
158143

159-
- `path`: **Absoulte** JSON file path.
160-
- `url` & `options`: Using [got](https://www.npmjs.com/package/got) for data fetching like `got(url, options)`.
161-
- `raw`: Plain JavaScript Object / Array.
144+
- `path`(`string`): **Absoulte** JSON file path.
145+
- `url`(`string`) & `options`(`Got Options`): Using [got](https://www.npmjs.com/package/got) for data fetching: `got(url, options)`.
146+
- `raw`(`object` | `array`): Vanilla JavaScript Object / Array.
162147

163-
After raw content is readed, it will be passed to `preprocessor`.
148+
After content acquisition got completed, the content will be passed to next handler called **preprocessor**.
164149

165150
### Preprocessor
166151

167-
Preprocessor will perform some extra processing on the input content:
152+
Preprocessor will perform some extra pre-processing works in the incoming content:
168153

169-
- Recursively **delete** object pairs which value is kind of **nested array** like `[[]]`, this is not supported yet which may also cause unexpected errors.
170-
- Ensure array contains **either primitive values or object values**, only obejct values will be preserved when the array
171-
contains mixed members. You can control this behaviour by `preprocessor.preserveObjectOnlyInArray`.
154+
- **Recursively delete** object field which value is kind of **nested array** like `[[]]`, this is not supported yet which may cause unexpected behaviours or errors.
155+
- Ensure array contains either **primitive type values** or **object type values**, by default,**only obejct values will be preserved** when the array
156+
contains both kinds of members(You can control this behaviour by `preprocessor.preserveObjectOnlyInArray`).
172157

173158
#### Preprocessor.Options
174159

175-
- `preserveObjectOnlyInArray`: `default: true`
176-
- `customPreprocessor`: Use your own custom preprocessor, which accepts `raw` from reader.
160+
- `preserveObjectOnlyInArray`(`boolean`): `default: true`
161+
- `customPreprocessor`(`(raw: object | array) => object | array`): Use your own custom preprocessor, which accepts content from reader, and should return JavaScript Object / Array.
177162

178163
### Parser
179164

180-
Parser will transform the preprocessed raw content to specific object structure,
165+
**Parser** will transform the pre-processed content to specific object structure,
181166
which will be consumed by `generator`.
182167

183-
Array entry structure(like `[]`) and object entry structure(like `{}`) will be parsed differently.
168+
> Array entry structure(like `[]`) and object entry structure(like `{}`) will be parsed differently.
184169
185170
#### Parser.Options
186171

187-
- `forceNonNullable`: Mark all field as non-nullale. `default: true`
188-
- `forceNonNullableListItem`: Mark all list item as non-nullale.`default: false`
189-
- `forceReturnType`: Generate return type for even string / boolean / field like `@Field((type) => String)`. `default: false`
190-
- `arrayEntryProp`: When parsing array-entry structure, use specified prop name like: `data: Data[]`. `default: 'data'`
172+
- `forceNonNullable`(`boolean`): Mark all field as non-nullale. `default: true`
173+
- `forceNonNullableListItem`(`boolean`): Mark all list item as non-nullale. `default: false`
174+
- `forceReturnType`(`boolean`): Generate return type for even `string` / `boolean` field like `@Field((type) => String)`. `default: false`
175+
- `arrayEntryProp`(`string`): When parsing array-entry structure, use specified prop name like: `data: Data[]`. `default: 'data'`.
176+
For example, `[{ foo: 1 }]` will be parsed to:
177+
178+
```javascript
179+
class Data {
180+
foo: number;
181+
}
191182

192-
> Custom parser is not supported now.
183+
class Root {
184+
data: Data[];
185+
}
186+
```
193187

194188
### Generator
195189

196-
Generator will traverse the parsed info record, perform corresponding AST operations to generate TypeGraphQL class-based types.
190+
**Generator** will traverse the parsed info, perform corresponding AST operations to generate class definitions with TypeGraphQL decorators.
197191

198192
#### Generator.Options
199193

200-
- `entryClassName`: The top-level generated entry class name. `default: 'Root'`.
201-
- `prefix`: Prefix for generated class name, you can set `prefix: true` to simply avoid repeated class specifier(by using parent class as child class name prefix, like `RootChildSomeChildProp` is from `Root-Child-SomeType`). `default: false`
202-
- `suffix`: Suffix for generated class name. `default: false`.
203-
- `publicProps`: Prop names included by it will be attatched with `public` keyword.
204-
- `readonlyProps`: Prop names included by it will be attatched with `readonly` keyword.
205-
- `sort`: Should sort generated class in normal order like `P-C1-C11-C12-C2-C21-C3-C31`. `default: true`.
194+
- `entryClassName`(`string`): The top-level generated entry class name. `default: 'Root'`.
195+
- `prefix`(`boolean` | `string`): Prefix for generated class name, you can set `prefix: true` to simply avoid repeated class specifier. `default: false`.
196+
By using parent class in child class name's prefix, like `RootChildSomeChildProp` is from:
197+
198+
```javascript
199+
class Root {
200+
child: RootChild;
201+
}
202+
203+
class RootChild {
204+
someChildProp: RootChildSomeChildProp;
205+
}
206+
207+
class RootChildSomeChildProp {}
208+
```
209+
210+
- `suffix`(`boolean` | `string`): Suffix for generated class name, e.g. `RootType`, `Type` is the specified suffix.`default: false`.
211+
- `publicProps`(`string[]`): Prop names included by it will be attatched with `public` keyword.
212+
- `readonlyProps`(`string[]`): Prop names included by it will be attatched with `readonly` keyword.
213+
- `sort`(`boolean`): Should sort generated class in normal order like `P-C1-C11-C12-C2-C21-C3-C31`. `default: true`.
206214

207215
### Postprocessor
208216

209-
Postprocessor is used to apply some post-processing works on generated source (`SourceFile`), you can use [ts-morph](https://ts-morph.com/) for simple and flexiable AST operations, which also powers the generator part.
217+
**Postprocessor** is used to apply some post-process works on generated source (`TypeScript SourceFile`), you can use [ts-morph](https://ts-morph.com/) for simple and flexiable AST operations, which also powers the generator part indeed.
210218

211219
#### Postprocessor.Options
212220

213-
- `customPostprocessor`: Custom post-processor accepts the source and perform extra options.
221+
- `customPostprocessor`(`(source: SourceFile) => SourceFile`): Custom post-processor accepts the AST source file.
214222

215223
### Checker
216224

217-
Checker will use generated class to create a tmp reoslver, invoking `TypeGraphQL.buildSchemaSync` method to check does generated file work correctly.
225+
**Checker** will use generated class definitions to create a tmp reoslver, invoking `TypeGraphQL.buildSchemaSync` method to check if generated file works correctly.
218226

219-
Under the hood, we're using `ts-node tmp-file.ts --compiler-options [options]` to perform the check.
227+
We're using `ts-node tmp-file.ts --compiler-options [options]` to perform the check under the hood.
220228

221229
#### Checker.Options
222230

223-
- `disable`: Disable checker. `default: true`
224-
- `keep`: Keey generated tmp checker file. `default: false`
225-
- `execaOptions`: Extra options passed to [execa](https://www.npmjs.com/package/execa).
226-
- `executeOptions`: Extra options passed to ts-node `--compiler-options`, which keeps same with TypeSctipt CompilerOptions.
231+
- `disable`(`boolean`): Disable checker. `default: true`
232+
- `keep`(`boolean`): Keey generated tmp checker file. `default: false`
233+
- `execaOptions`(`Execa Options`): Extra options passed to [execa](https://www.npmjs.com/package/execa).
234+
- `executeOptions`(`Ts-node compile Options`): Extra options passed to ts-node `--compiler-options`, which keeps same with TypeSctipt CompilerOptions.
227235

228236
### Writer
229237

230-
Writer will format generated source file, you can also use custom writer(coming soon).
238+
**Writer** will format generated source file.
231239

232240
#### Writer.options
233241

234-
- `outputPath`: Output path. required.
235-
- `format`: Should perform formatting by `Prettier`.
236-
- `formatOptions`: Options passed to `Prettier`.
242+
- `outputPath`(`string`): Output path. required.
243+
- `format`(`boolean`): Should perform formatting by `Prettier`. `default: true`.
244+
- `formatOptions`(`Prettier Options`): Options passed to `Prettier.format`.

0 commit comments

Comments
 (0)