Skip to content

Commit 1e24993

Browse files
committed
docs: update README
1 parent ad1cc06 commit 1e24993

File tree

1 file changed

+204
-0
lines changed

1 file changed

+204
-0
lines changed

README.md

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,210 @@ When we change it to `--module-name-index 1` then Api class have two properties
216216
This option will group your API operations based on their first tag - mirroring how the Swagger UI groups displayed operations
217217

218218

219+
## Modification internal codegen structs with NodeJS API:
220+
221+
You are able to modify TypeScript internal structs using for generating output with using `generateApi` options `codeGenConstructs` and `primitiveTypeConstructs`.
222+
223+
### `codeGenConstructs`
224+
225+
This option has type `(struct: CodeGenConstruct) => Partial<CodeGenConstruct>`.
226+
227+
```ts
228+
generateApi({
229+
// ...
230+
codeGenConstructs: (struct) => ({
231+
Keyword: {
232+
Number: "number",
233+
String: "string",
234+
Boolean: "boolean",
235+
Any: "any",
236+
Void: "void",
237+
Unknown: "unknown",
238+
Null: "null",
239+
Undefined: "undefined",
240+
Object: "object",
241+
File: "File",
242+
Date: "Date",
243+
Type: "type",
244+
Enum: "enum",
245+
Interface: "interface",
246+
Array: "Array",
247+
Record: "Record",
248+
Intersection: "&",
249+
Union: "|",
250+
},
251+
CodeGenKeyword: {
252+
UtilRequiredKeys: "UtilRequiredKeys",
253+
},
254+
/**
255+
* $A[] or Array<$A>
256+
*/
257+
ArrayType: (content) => {
258+
if (this.anotherArrayType) {
259+
return `Array<${content}>`;
260+
}
261+
262+
return `(${content})[]`;
263+
},
264+
/**
265+
* "$A"
266+
*/
267+
StringValue: (content) => `"${content}"`,
268+
/**
269+
* $A
270+
*/
271+
BooleanValue: (content) => `${content}`,
272+
/**
273+
* $A
274+
*/
275+
NumberValue: (content) => `${content}`,
276+
/**
277+
* $A
278+
*/
279+
NullValue: (content) => content,
280+
/**
281+
* $A1 | $A2
282+
*/
283+
UnionType: (contents) => _.join(_.uniq(contents), ` | `),
284+
/**
285+
* ($A1)
286+
*/
287+
ExpressionGroup: (content) => (content ? `(${content})` : ""),
288+
/**
289+
* $A1 & $A2
290+
*/
291+
IntersectionType: (contents) => _.join(_.uniq(contents), ` & `),
292+
/**
293+
* Record<$A1, $A2>
294+
*/
295+
RecordType: (key, value) => `Record<${key}, ${value}>`,
296+
/**
297+
* readonly $key?:$value
298+
*/
299+
TypeField: ({ readonly, key, optional, value }) =>
300+
_.compact([readonly && "readonly ", key, optional && "?", ": ", value]).join(""),
301+
/**
302+
* [key: $A1]: $A2
303+
*/
304+
InterfaceDynamicField: (key, value) => `[key: ${key}]: ${value}`,
305+
/**
306+
* $A1 = $A2
307+
*/
308+
EnumField: (key, value) => `${key} = ${value}`,
309+
/**
310+
* $A0.key = $A0.value,
311+
* $A1.key = $A1.value,
312+
* $AN.key = $AN.value,
313+
*/
314+
EnumFieldsWrapper: (contents) =>
315+
_.map(contents, ({ key, value }) => ` ${key} = ${value}`).join(",\n"),
316+
/**
317+
* {\n $A \n}
318+
*/
319+
ObjectWrapper: (content) => `{\n${content}\n}`,
320+
/**
321+
* /** $A *\/
322+
*/
323+
MultilineComment: (contents, formatFn) =>
324+
[
325+
...(contents.length === 1
326+
? [`/** ${contents[0]} */`]
327+
: ["/**", ...contents.map((content) => ` * ${content}`), " */"]),
328+
].map((part) => `${formatFn ? formatFn(part) : part}\n`),
329+
/**
330+
* $A1<...$A2.join(,)>
331+
*/
332+
TypeWithGeneric: (typeName, genericArgs) => {
333+
return `${typeName}${genericArgs.length ? `<${genericArgs.join(",")}>` : ""}`;
334+
},
335+
})
336+
})
337+
```
338+
339+
For example, if you need to generate output `Record<string, any>` instead of `object` you can do it with using following code:
340+
341+
```ts
342+
generateApi({
343+
// ...
344+
codeGenConstructs: (struct) => ({
345+
Keyword: {
346+
Object: "Record<string, any>",
347+
}
348+
})
349+
})
350+
```
351+
352+
### `primitiveTypeConstructs`
353+
354+
It is type mapper or translator swagger schema objects. `primitiveTypeConstructs` translates `type`/`format` schema fields to typescript structs.
355+
This option has type
356+
```ts
357+
type PrimitiveTypeStructValue =
358+
| string
359+
| ((schema: Record<string, any>, parser: import("./src/schema-parser/schema-parser").SchemaParser) => string);
360+
361+
type PrimitiveTypeStruct = Record<
362+
"integer" | "number" | "boolean" | "object" | "file" | "string" | "array",
363+
string | ({ $default: PrimitiveTypeStructValue } & Record<string, PrimitiveTypeStructValue>)
364+
>
365+
366+
declare const primitiveTypeConstructs: (struct: PrimitiveTypeStruct) => Partial<PrimitiveTypeStruct>
367+
368+
generateApi({
369+
// ...
370+
primitiveTypeConstructs: (struct) => ({
371+
integer: () => "number",
372+
number: () => "number",
373+
boolean: () => "boolean",
374+
object: () => "object",
375+
file: () => "File",
376+
string: {
377+
$default: () => "string",
378+
379+
/** formats */
380+
binary: () => "File",
381+
file: () => "File",
382+
"date-time": () => "string",
383+
time: () => "string",
384+
date: () => "string",
385+
duration: () => "string",
386+
email: () => "string",
387+
"idn-email": () => "string",
388+
"idn-hostname": () => "string",
389+
ipv4: () => "string",
390+
ipv6: () => "string",
391+
uuid: () => "string",
392+
uri: () => "string",
393+
"uri-reference": () => "string",
394+
"uri-template": () => "string",
395+
"json-pointer": () => "string",
396+
"relative-json-pointer": () => "string",
397+
regex: () => "string",
398+
},
399+
array: (schema, parser) => {
400+
const content = parser.getInlineParseContent(schema.items);
401+
return parser.checkAndAddNull(schema, `(${content})[]`);
402+
},
403+
})
404+
})
405+
```
406+
407+
For example, if you need to change `"string"/"date-time"` default output as `string` to `Date` you can do it with using following code:
408+
409+
```ts
410+
411+
generateApi({
412+
primitiveTypeConstructs: (struct) => ({
413+
string: {
414+
"date-time": "Date",
415+
},
416+
})
417+
})
418+
419+
```
420+
421+
See more about [swagger schema type/format data here](https://json-schema.org/understanding-json-schema/reference/string.html#dates-and-times)
422+
219423
## 📄 Mass media
220424

221425
- [5 Lessons learned about swagger-typescript-api](https://christo8989.medium.com/5-lessons-learned-about-swagger-typescript-api-511240b34c1)

0 commit comments

Comments
 (0)