Skip to content

Commit e523d6d

Browse files
authored
Merge pull request #1100 from acacode/feature-more-info-about-extracted-types
Feature more info about extracted types
2 parents caccd4b + 4782f13 commit e523d6d

File tree

6 files changed

+114
-13
lines changed

6 files changed

+114
-13
lines changed

.changeset/loose-lies-fetch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"swagger-typescript-api": patch
3+
---
4+
5+
Improve type safety by adding proper types to SchemaComponent and introducing flags for extracted elements such as request parameters, request body, response body, and response errors.

src/configuration.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import * as typescript from "typescript";
55
import type {
66
ExtractingOptions,
77
GenerateApiConfiguration,
8+
Hooks,
9+
SchemaComponent,
810
} from "../types/index.js";
911
import { ComponentTypeNameResolver } from "./component-type-name-resolver.js";
1012
import * as CONSTANTS from "./constants.js";
@@ -86,11 +88,11 @@ export class CodeGenConfig {
8688
};
8789
routeNameDuplicatesMap = new Map();
8890
prettierOptions = { ...CONSTANTS.PRETTIER_OPTIONS };
89-
hooks = {
91+
hooks: Hooks = {
9092
onPreBuildRoutePath: (_routePath: unknown) => void 0,
9193
onBuildRoutePath: (_routeData: unknown) => void 0,
9294
onInsertPathParam: (_pathParam: unknown) => void 0,
93-
onCreateComponent: (schema: unknown) => schema,
95+
onCreateComponent: (schema: SchemaComponent) => schema,
9496
onPreParseSchema: (
9597
_originalSchema: unknown,
9698
_typeName: unknown,

src/schema-components-map.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,16 @@ export class SchemaComponentsMap {
2121
return ref.split("/");
2222
};
2323

24-
createComponent($ref: string, rawTypeData: string) {
24+
createComponent(
25+
$ref: string,
26+
rawTypeData: SchemaComponent["rawTypeData"],
27+
): SchemaComponent {
2528
const parsed = this.parseRef($ref);
26-
const typeName = parsed[parsed.length - 1];
27-
const componentName = parsed[parsed.length - 2];
28-
const componentSchema = {
29+
const typeName = parsed[parsed.length - 1]!;
30+
const componentName = parsed[
31+
parsed.length - 2
32+
] as SchemaComponent["componentName"];
33+
const componentSchema: SchemaComponent = {
2934
$ref,
3035
typeName,
3136
rawTypeData,
@@ -52,7 +57,7 @@ export class SchemaComponentsMap {
5257
return this._data;
5358
}
5459

55-
filter(...componentNames: string[]) {
60+
filter(...componentNames: (string[] | string)[]) {
5661
return this._data.filter((it) =>
5762
componentNames.some((componentName) =>
5863
it.$ref.startsWith(`#/components/${componentName}`),

src/schema-parser/schema-parser-fabric.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type {
22
ParsedSchema,
3+
SchemaComponent,
34
SchemaTypeEnumContent,
45
SchemaTypeObjectContent,
56
SchemaTypePrimitiveContent,
@@ -63,7 +64,11 @@ export class SchemaParserFabric {
6364
return parser.schema;
6465
};
6566

66-
createParsedComponent = ({ typeName, schema, schemaPath }) => {
67+
createParsedComponent = ({
68+
typeName,
69+
schema,
70+
schemaPath,
71+
}): SchemaComponent => {
6772
const schemaCopy = structuredClone(schema);
6873
const customComponent = this.schemaComponentsMap.createComponent(
6974
this.schemaComponentsMap.createRef(["components", "schemas", typeName]),

src/schema-routes/schema-routes.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,10 @@ export class SchemaRoutes {
597597
typeName,
598598
schemaPath: [operationId],
599599
});
600+
601+
if (schema?.typeData) {
602+
schema.typeData.isExtractedRequestBody = true;
603+
}
600604
content = this.schemaParserFabric.getInlineParseContent({
601605
$ref: schema.$ref,
602606
});
@@ -670,10 +674,16 @@ export class SchemaRoutes {
670674
},
671675
);
672676

673-
return this.schemaParserFabric.createParsedComponent({
677+
const component = this.schemaParserFabric.createParsedComponent({
674678
typeName: generatedTypeName,
675679
schema: schema,
676680
});
681+
682+
if (component.typeData) {
683+
component.typeData.isExtractedRequestParams = true;
684+
}
685+
686+
return component;
677687
}
678688

679689
return schema;
@@ -705,6 +715,9 @@ export class SchemaRoutes {
705715
schemaPath: [routeInfo.operationId],
706716
});
707717
successResponse.schema.contentKind = contentKind;
718+
if (successResponse.schema.typeData) {
719+
successResponse.schema.typeData.isExtractedResponseBody = true;
720+
}
708721
successResponse.type = this.schemaParserFabric.getInlineParseContent({
709722
$ref: successResponse.schema.$ref,
710723
});
@@ -756,6 +769,9 @@ export class SchemaRoutes {
756769
{ ...schema },
757770
);
758771
responseBodyInfo.error.schemas = [component];
772+
if (component.typeData) {
773+
component.typeData.isExtractedResponseError = true;
774+
}
759775
responseBodyInfo.error.type = this.typeNameFormatter.format(
760776
component.typeName,
761777
);
@@ -818,7 +834,7 @@ export class SchemaRoutes {
818834
method,
819835
usageSchema,
820836
parsedSchemas,
821-
) => {
837+
): ParsedRoute => {
822838
const { security: globalSecurity } = usageSchema;
823839
const { moduleNameIndex, moduleNameFirstTag, extractRequestParams } =
824840
this.config;

types/index.ts

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,10 @@ export interface ParsedSchema<C> {
442442
description?: string;
443443
allFieldsAreOptional?: boolean;
444444
content: C;
445+
isExtractedRequestParams?: boolean;
446+
isExtractedRequestBody?: boolean;
447+
isExtractedResponseBody?: boolean;
448+
isExtractedResponseError?: boolean;
445449
}
446450

447451
export interface PathArgInfo {
@@ -514,12 +518,76 @@ export type RawRouteInfo = {
514518
consumes?: string[];
515519
};
516520

521+
export interface ParsedRouteRequest {
522+
contentTypes?: string[];
523+
formData?: boolean;
524+
headers?: {
525+
name: string | null;
526+
optional: boolean | undefined;
527+
type: Record<string, any>;
528+
};
529+
isQueryBody?: boolean;
530+
method?: string;
531+
parameters?: Record<string, unknown>[];
532+
path?: string;
533+
pathParams?: Record<string, unknown>;
534+
payload?: { name: string | null; optional?: boolean; type: string };
535+
query?: Record<string, unknown>;
536+
requestParams?: Record<string, unknown> | null;
537+
security?: boolean;
538+
}
539+
540+
export interface ParsedRouteResponse {
541+
contentTypes?: string[];
542+
errorType?: string;
543+
fullTypes?: string;
544+
type?: string;
545+
}
546+
517547
export interface ParsedRoute {
518548
id: string;
519-
jsDocLines: string;
520549
namespace: string;
521-
request: Request;
522-
response: Response;
550+
// biome-ignore lint/suspicious/noExplicitAny: TODO
551+
routeParams?: Record<string, any>;
552+
requestBodyInfo?: {
553+
// biome-ignore lint/suspicious/noExplicitAny: TODO
554+
paramName: any;
555+
// biome-ignore lint/suspicious/noExplicitAny: TODO
556+
contentTypes: any[];
557+
contentKind: string;
558+
// biome-ignore lint/suspicious/noExplicitAny: TODO
559+
schema: any;
560+
// biome-ignore lint/suspicious/noExplicitAny: TODO
561+
type: any;
562+
// biome-ignore lint/suspicious/noExplicitAny: TODO
563+
required: any;
564+
};
565+
responseBodyInfo?: {
566+
// biome-ignore lint/suspicious/noExplicitAny: TODO
567+
contentTypes: any[];
568+
// biome-ignore lint/suspicious/noExplicitAny: TODO
569+
responses: any[];
570+
// biome-ignore lint/suspicious/noExplicitAny: TODO
571+
success?: Record<string, any>;
572+
// biome-ignore lint/suspicious/noExplicitAny: TODO
573+
error?: Record<string, any>;
574+
// biome-ignore lint/suspicious/noExplicitAny: TODO
575+
full?: Record<string, any>;
576+
};
577+
// biome-ignore lint/suspicious/noExplicitAny: TODO
578+
specificArgs?: Record<string, any>;
579+
// biome-ignore lint/suspicious/noExplicitAny: TODO
580+
queryObjectSchema?: Record<string, any>;
581+
// biome-ignore lint/suspicious/noExplicitAny: TODO
582+
pathObjectSchema?: Record<string, any>;
583+
// biome-ignore lint/suspicious/noExplicitAny: TODO
584+
headersObjectSchema?: Record<string, any>;
585+
// biome-ignore lint/suspicious/noExplicitAny: TODO
586+
responseBodySchema?: Record<string, any>;
587+
requestBodySchema?: Record<string, any>;
588+
specificArgNameResolver?: Record<string, any>;
589+
request: ParsedRouteRequest;
590+
response: ParsedRouteResponse;
523591
routeName: RouteNameInfo;
524592
raw: RawRouteInfo;
525593
}

0 commit comments

Comments
 (0)