Skip to content

Commit

Permalink
feat(typescript): Support inline types in TypeScript generator (#5350)
Browse files Browse the repository at this point in the history
Convert inline schemas to inline types in TypeScript
---------

Co-authored-by: Niels Swimberghe <3382717+Swimburger@users.noreply.github.com>
Co-authored-by: Swimburger <Swimburger@users.noreply.github.com>
  • Loading branch information
3 people authored Dec 11, 2024
1 parent 710060b commit 5c17b3e
Show file tree
Hide file tree
Showing 1,202 changed files with 38,193 additions and 3,659 deletions.
9 changes: 2 additions & 7 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,7 @@ module.exports = {
ignoreRestSiblings: true
}
],
"@typescript-eslint/no-namespace": [
"error",
{
allowDeclarations: true
}
],
"@typescript-eslint/no-namespace": "off",
"@typescript-eslint/explicit-module-boundary-types": [
"error",
{
Expand Down Expand Up @@ -129,7 +124,7 @@ module.exports = {
{
files: ['**/*.test.ts', '**/*.spec.ts'],
rules: {
'no-console': 'off'
'no-console': 'off'
}
}
]
Expand Down
2 changes: 1 addition & 1 deletion fern/pages/changelogs/cli/2024-12-05.mdx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## 0.45.3
**`(fix):`** Unknown schemas are no longer incorrectly marked as `additionalProperties: true`.
**`(fix):`** Unknown schemas are no longer incorrectly marked as `additionalProperties: true`.


2 changes: 1 addition & 1 deletion fern/pages/changelogs/cli/2024-12-10.mdx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## 0.45.4-rc1
**`(chore):`** Bumped Java IR to latest (v53)
**`(chore):`** Unknown schemas are no longer incorrectly marked as `additionalProperties: true`.


2 changes: 1 addition & 1 deletion generators/typescript/codegen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"devDependencies": {
"@fern-api/core-utils": "workspace:*",
"@fern-api/base-generator": "workspace:*",
"@fern-fern/ir-sdk": "53.8.0",
"@fern-fern/ir-sdk": "53.23.0",
"@types/jest": "^29.5.12",
"@types/node": "18.7.18",
"depcheck": "^1.4.6",
Expand Down
2 changes: 1 addition & 1 deletion generators/typescript/express/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
"devDependencies": {
"@fern-fern/generator-exec-sdk": "^0.0.898",
"@fern-fern/ir-sdk": "53.8.0",
"@fern-fern/ir-sdk": "53.23.0",
"@fern-typescript/abstract-generator-cli": "workspace:*",
"@fern-typescript/commons": "workspace:*",
"@fern-typescript/contexts": "workspace:*",
Expand Down
5 changes: 3 additions & 2 deletions generators/typescript/express/cli/src/ExpressGeneratorCli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import { NpmPackage, PersistedTypescriptProject } from "@fern-typescript/commons
import { GeneratorContext } from "@fern-typescript/contexts";
import { ExpressGenerator } from "@fern-typescript/express-generator";
import { camelCase, upperFirst } from "lodash-es";
import { custom } from "zod";
import { ExpressCustomConfig } from "./custom-config/ExpressCustomConfig";
import { ExpressCustomConfigSchema } from "./custom-config/schema/ExpressCustomConfigSchema";

export class ExpressGeneratorCli extends AbstractGeneratorCli<ExpressCustomConfig> {
protected parseCustomConfig(customConfig: unknown): ExpressCustomConfig {
const parsed = customConfig != null ? ExpressCustomConfigSchema.parse(customConfig) : undefined;
const noSerdeLayer = parsed?.noSerdeLayer ?? false;
const enableInlineTypes = false; // hardcode, not supported in Express
return {
useBrandedStringAliases: parsed?.useBrandedStringAliases ?? false,
areImplementationsOptional: parsed?.optionalImplementations ?? false,
Expand All @@ -29,7 +29,8 @@ export class ExpressGeneratorCli extends AbstractGeneratorCli<ExpressCustomConfi
skipRequestValidation: parsed?.skipRequestValidation ?? false,
skipResponseValidation: parsed?.skipResponseValidation ?? false,
useBigInt: parsed?.useBigInt ?? false,
noOptionalProperties: parsed?.noOptionalProperties ?? false
noOptionalProperties: parsed?.noOptionalProperties ?? false,
enableInlineTypes
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export interface ExpressCustomConfig {
allowExtraFields: boolean;
useBigInt: boolean;
noOptionalProperties: boolean;
enableInlineTypes: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
"dependencies": {
"@fern-api/core-utils": "workspace:*",
"@fern-fern/ir-sdk": "53.8.0",
"@fern-fern/ir-sdk": "53.23.0",
"@fern-typescript/abstract-schema-generator": "workspace:*",
"@fern-typescript/commons": "workspace:*",
"@fern-typescript/contexts": "workspace:*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"depcheck": "depcheck"
},
"dependencies": {
"@fern-fern/ir-sdk": "53.8.0",
"@fern-fern/ir-sdk": "53.23.0",
"@fern-typescript/abstract-error-class-generator": "workspace:*",
"@fern-typescript/commons": "workspace:*",
"@fern-typescript/contexts": "workspace:*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
"dependencies": {
"@fern-api/core-utils": "workspace:*",
"@fern-fern/ir-sdk": "53.8.0",
"@fern-fern/ir-sdk": "53.23.0",
"@fern-typescript/abstract-schema-generator": "workspace:*",
"@fern-typescript/commons": "workspace:*",
"@fern-typescript/contexts": "workspace:*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"depcheck": "depcheck"
},
"dependencies": {
"@fern-fern/ir-sdk": "53.8.0",
"@fern-fern/ir-sdk": "53.23.0",
"@fern-typescript/commons": "workspace:*",
"@fern-typescript/contexts": "workspace:*"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"depcheck": "depcheck"
},
"dependencies": {
"@fern-fern/ir-sdk": "53.8.0",
"@fern-fern/ir-sdk": "53.23.0",
"@fern-typescript/abstract-schema-generator": "workspace:*",
"@fern-typescript/commons": "workspace:*",
"@fern-typescript/contexts": "workspace:*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"depcheck": "depcheck"
},
"dependencies": {
"@fern-fern/ir-sdk": "53.8.0",
"@fern-fern/ir-sdk": "53.23.0",
"@fern-typescript/commons": "workspace:*",
"@fern-typescript/contexts": "workspace:*",
"@fern-typescript/resolvers": "workspace:*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"depcheck": "depcheck"
},
"dependencies": {
"@fern-fern/ir-sdk": "53.8.0",
"@fern-fern/ir-sdk": "53.23.0",
"@fern-typescript/commons": "workspace:*",
"@fern-typescript/contexts": "workspace:*",
"@fern-typescript/resolvers": "workspace:*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
Package,
PathParameter
} from "@fern-fern/ir-sdk/api";
import { convertHttpPathToExpressRoute, getTextOfTsNode, maybeAddDocs, PackageId } from "@fern-typescript/commons";
import { convertHttpPathToExpressRoute, getTextOfTsNode, maybeAddDocsNode, PackageId } from "@fern-typescript/commons";
import { ExpressContext, GeneratedExpressService } from "@fern-typescript/contexts";
import { ClassDeclaration, InterfaceDeclaration, Scope, ts } from "ts-morph";

Expand Down Expand Up @@ -82,7 +82,7 @@ export class GeneratedExpressServiceImpl implements GeneratedExpressService {
name: this.serviceClassName,
isExported: true
});
maybeAddDocs(serviceClass, this.package_.docs);
maybeAddDocsNode(serviceClass, this.package_.docs);

serviceClass.addProperty({
scope: Scope.Private,
Expand Down
3 changes: 2 additions & 1 deletion generators/typescript/express/generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"dependencies": {
"@fern-api/core-utils": "workspace:*",
"@fern-api/fs-utils": "workspace:*",
"@fern-fern/ir-sdk": "53.8.0",
"@fern-api/logger": "workspace:*",
"@fern-fern/ir-sdk": "53.23.0",
"@fern-typescript/commons": "workspace:*",
"@fern-typescript/contexts": "workspace:*",
"@fern-typescript/express-endpoint-type-schemas-generator": "workspace:*",
Expand Down
80 changes: 65 additions & 15 deletions generators/typescript/express/generator/src/ExpressGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AbsoluteFilePath } from "@fern-api/fs-utils";
import { HttpService, IntermediateRepresentation } from "@fern-fern/ir-sdk/api";
import { HttpService, IntermediateRepresentation, TypeDeclaration, TypeId } from "@fern-fern/ir-sdk/api";
import {
convertExportedFilePathToFilePath,
CoreUtilitiesManager,
Expand Down Expand Up @@ -27,6 +27,7 @@ import { TypeGenerator } from "@fern-typescript/type-generator";
import { TypeReferenceExampleGenerator } from "@fern-typescript/type-reference-example-generator";
import { TypeSchemaGenerator } from "@fern-typescript/type-schema-generator";
import { Directory, Project, SourceFile } from "ts-morph";
import { Logger } from "@fern-api/logger";
import { ExpressContextImpl } from "./contexts/ExpressContextImpl";
import { EndpointDeclarationReferencer } from "./declaration-referencers/EndpointDeclarationReferencer";
import { ExpressErrorDeclarationReferencer } from "./declaration-referencers/ExpressErrorDeclarationReferencer";
Expand Down Expand Up @@ -188,7 +189,8 @@ export class ExpressGenerator {
includeOtherInUnionTypes: config.includeOtherInUnionTypes,
includeSerdeLayer: config.includeSerdeLayer,
retainOriginalCasing: config.retainOriginalCasing,
noOptionalProperties: config.noOptionalProperties
noOptionalProperties: config.noOptionalProperties,
enableInlineTypes: false
});
this.typeSchemaGenerator = new TypeSchemaGenerator({
includeUtilsOnUnionMembers: config.includeUtilsOnUnionMembers,
Expand Down Expand Up @@ -276,24 +278,36 @@ export class ExpressGenerator {
await this.coreUtilitiesManager.copyCoreUtilities({ pathToSrc, pathToRoot });
}

private getTypesToGenerate(): Record<TypeId, TypeDeclaration> {
return this.intermediateRepresentation.types;
}

private generateTypeDeclarations() {
for (const typeDeclaration of Object.values(this.intermediateRepresentation.types)) {
for (const typeDeclaration of Object.values(this.getTypesToGenerate())) {
this.withSourceFile({
filepath: this.typeDeclarationReferencer.getExportedFilepath(typeDeclaration.name),
run: ({ sourceFile, importsManager }) => {
const context = this.generateExpressContext({ sourceFile, importsManager });
const context = this.generateExpressContext({
logger: this.context.logger,
sourceFile,
importsManager
});
context.type.getGeneratedType(typeDeclaration.name).writeToFile(context);
}
});
}
}

private generateTypeSchemas() {
for (const typeDeclaration of Object.values(this.intermediateRepresentation.types)) {
for (const typeDeclaration of Object.values(this.getTypesToGenerate())) {
this.withSourceFile({
filepath: this.typeSchemaDeclarationReferencer.getExportedFilepath(typeDeclaration.name),
run: ({ sourceFile, importsManager }) => {
const context = this.generateExpressContext({ sourceFile, importsManager });
const context = this.generateExpressContext({
logger: this.context.logger,
sourceFile,
importsManager
});
context.typeSchema.getGeneratedTypeSchema(typeDeclaration.name).writeToFile(context);
}
});
Expand All @@ -305,7 +319,11 @@ export class ExpressGenerator {
this.withSourceFile({
filepath: this.expressErrorDeclarationReferencer.getExportedFilepath(errorDeclaration.name),
run: ({ sourceFile, importsManager }) => {
const context = this.generateExpressContext({ sourceFile, importsManager });
const context = this.generateExpressContext({
logger: this.context.logger,
sourceFile,
importsManager
});
context.expressError.getGeneratedExpressError(errorDeclaration.name).writeToFile(context);
}
});
Expand All @@ -317,7 +335,11 @@ export class ExpressGenerator {
this.withSourceFile({
filepath: this.expressErrorSchemaDeclarationReferencer.getExportedFilepath(errorDeclaration.name),
run: ({ sourceFile, importsManager }) => {
const context = this.generateExpressContext({ sourceFile, importsManager });
const context = this.generateExpressContext({
logger: this.context.logger,
sourceFile,
importsManager
});
context.expressErrorSchema
.getGeneratedExpressErrorSchema(errorDeclaration.name)
?.writeToFile(context);
Expand All @@ -336,7 +358,11 @@ export class ExpressGenerator {
endpoint
}),
run: ({ sourceFile, importsManager }) => {
const context = this.generateExpressContext({ sourceFile, importsManager });
const context = this.generateExpressContext({
logger: this.context.logger,
sourceFile,
importsManager
});
context.expressInlinedRequestBody
.getGeneratedInlinedRequestBody(packageId, endpoint.name)
.writeToFile(context);
Expand All @@ -357,7 +383,11 @@ export class ExpressGenerator {
endpoint
}),
run: ({ sourceFile, importsManager }) => {
const context = this.generateExpressContext({ sourceFile, importsManager });
const context = this.generateExpressContext({
logger: this.context.logger,
sourceFile,
importsManager
});
context.expressInlinedRequestBodySchema
.getGeneratedInlinedRequestBodySchema(packageId, endpoint.name)
.writeToFile(context);
Expand All @@ -377,7 +407,11 @@ export class ExpressGenerator {
endpoint
}),
run: ({ sourceFile, importsManager }) => {
const context = this.generateExpressContext({ sourceFile, importsManager });
const context = this.generateExpressContext({
logger: this.context.logger,
sourceFile,
importsManager
});
context.expressEndpointTypeSchemas
.getGeneratedEndpointTypeSchemas(packageId, endpoint.name)
.writeToFile(context);
Expand All @@ -392,7 +426,11 @@ export class ExpressGenerator {
this.withSourceFile({
filepath: this.expressServiceDeclarationReferencer.getExportedFilepath(packageId),
run: ({ sourceFile, importsManager }) => {
const context = this.generateExpressContext({ sourceFile, importsManager });
const context = this.generateExpressContext({
logger: this.context.logger,
sourceFile,
importsManager
});
context.expressService.getGeneratedExpressService(packageId).writeToFile(context);
}
});
Expand All @@ -403,7 +441,11 @@ export class ExpressGenerator {
this.withSourceFile({
filepath: this.expressRegisterDeclarationReferencer.getExportedFilepath(),
run: ({ sourceFile, importsManager }) => {
const context = this.generateExpressContext({ sourceFile, importsManager });
const context = this.generateExpressContext({
logger: this.context.logger,
sourceFile,
importsManager
});
context.expressRegister.getGeneratedExpressRegister()?.writeToFile(context);
}
});
Expand All @@ -413,7 +455,11 @@ export class ExpressGenerator {
this.withSourceFile({
filepath: this.genericApiExpressErrorDeclarationReferencer.getExportedFilepath(),
run: ({ sourceFile, importsManager }) => {
const context = this.generateExpressContext({ sourceFile, importsManager });
const context = this.generateExpressContext({
logger: this.context.logger,
sourceFile,
importsManager
});
context.genericAPIExpressError.getGeneratedGenericAPIExpressError().writeToFile(context);
}
});
Expand Down Expand Up @@ -470,13 +516,16 @@ export class ExpressGenerator {
}

private generateExpressContext({
logger,
sourceFile,
importsManager
}: {
logger: Logger;
sourceFile: SourceFile;
importsManager: ImportsManager;
}): ExpressContextImpl {
return new ExpressContextImpl({
logger,
sourceFile,
coreUtilitiesManager: this.coreUtilitiesManager,
dependencyManager: this.dependencyManager,
Expand Down Expand Up @@ -509,7 +558,8 @@ export class ExpressGenerator {
expressErrorSchemaGenerator: this.expressErrorSchemaGenerator,
includeSerdeLayer: this.config.includeSerdeLayer,
retainOriginalCasing: this.config.retainOriginalCasing,
useBigInt: this.config.useBigInt
useBigInt: this.config.useBigInt,
enableInlineTypes: false
});
}
}
Loading

0 comments on commit 5c17b3e

Please sign in to comment.