-
-
Notifications
You must be signed in to change notification settings - Fork 7.1k
NestJS server codegen #21494
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aryobenholzner
wants to merge
18
commits into
OpenAPITools:master
Choose a base branch
from
aryobenholzner:nestjs-server-codegen
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
NestJS server codegen #21494
Changes from 14 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
23450c1
setup basic codegen for nestjs server
aryobenholzner d886d19
set up generated file structure
aryobenholzner eb834cd
adapted moustache files
aryobenholzner 1844ae8
fixed imports
aryobenholzner a1ddef4
adapted templates
aryobenholzner 387e96e
added module bootstrap
aryobenholzner d4f8807
added model generation
aryobenholzner 07ad56f
fixed error with generic type
aryobenholzner ac039c5
added README
aryobenholzner cd4c0d6
added usage clarification to README, introduced module index.ts
aryobenholzner f55b21f
Update modules/openapi-generator/src/main/resources/typescript-nestjs…
aryobenholzner a81f776
cleaned up package.mustache, added parameters for versions
aryobenholzner b9c18c1
cleaned up unneeded boilerplate templates
aryobenholzner 18ee6ac
fixed indentations from templates
aryobenholzner b769376
implemented useSingleRequestParameter
aryobenholzner fa06a62
fixed parameter handling
aryobenholzner dc89dcc
added samples with tests
aryobenholzner 2f05e15
added docs
aryobenholzner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
592 changes: 592 additions & 0 deletions
592
...rator/src/main/java/org/openapitools/codegen/languages/TypeScriptNestjsServerCodegen.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
modules/openapi-generator/src/main/resources/typescript-nestjs-server/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# OpenApi Generator _typescript-nestjs-server_ | ||
|
||
Usage: The generated output is intended to be its own module, that can be imported into your NestJS App Module. You do not need to change generated files, just import the module and implement the API | ||
|
||
Example usage (with the openapi sample `petstore.yaml`): | ||
|
||
1. Invoke openapi-generator | ||
``` | ||
openapi-generator-cli.jar generate -i petstore.yaml -g typescript-nestjs-server -o api-module/ | ||
``` | ||
2. implement the contracts from `api-module/api` | ||
`handlers/PetService.ts`: | ||
```typescript | ||
import { Pet, ApiResponse } from "models"; | ||
import { Observable } from "rxjs"; | ||
import { PetApi } from "../api"; | ||
import { Inject, Injectable } from "@nestjs/common"; | ||
|
||
@Injectable() | ||
export class PetService implements PetApi { | ||
addPet(pet: Pet, request: Request): Pet | Promise<Pet> | Observable<Pet> { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
deletePet(petId: number, apiKey: string, request: Request): void | Promise<void> | Observable<void> { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
... | ||
``` | ||
3. Import the generated `ApiModule` with `ApiModule.forRoot` and provide a instance of `ApiImplementations` with a reference to your implementation | ||
`app.module.ts` | ||
```typescript | ||
import { Module } from "@nestjs/common"; | ||
import { ApiModule, ApiImplementations } from "api-module"; | ||
import { PetService } from "./handlers/PetService"; | ||
import { UserService } from "./handlers/UserService"; | ||
import { StoreService } from "./handlers/StoreService"; | ||
|
||
const apiImplementations: ApiImplementations = { | ||
petApi: PetService, | ||
userApi: UserService, | ||
storeApi: StoreService, | ||
} | ||
|
||
@Module({ | ||
imports: [ | ||
ApiModule.forRoot(apiImplementations), | ||
], | ||
controllers: [], | ||
providers: [], | ||
}) | ||
export class AppModule {} | ||
``` | ||
|
||
You now can regenerate the API module as often as you want without overriding your implementation. |
19 changes: 19 additions & 0 deletions
19
...penapi-generator/src/main/resources/typescript-nestjs-server/api-implementations.mustache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Type } from '@nestjs/common'; | ||
{{#apiInfo}} | ||
{{#apis}} | ||
{{#operations}} | ||
import { {{classname}} } from '{{apiPackage}}'; | ||
{{/operations}} | ||
{{/apis}} | ||
|
||
/** | ||
* Provide this type to {@link ApiModule} to provide your API implementations | ||
**/ | ||
export type ApiImplementations = { | ||
{{#apis}} | ||
{{#operations}} | ||
{{#lambda.camelcase}}{{classname}}{{/lambda.camelcase}}: Type<{{classname}}> | ||
{{/operations}} | ||
{{/apis}} | ||
}; | ||
{{/apiInfo}} |
37 changes: 37 additions & 0 deletions
37
modules/openapi-generator/src/main/resources/typescript-nestjs-server/api.module.mustache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { DynamicModule, Module, Provider } from '@nestjs/common'; | ||
import { ApiImplementations } from './api-implementations' | ||
{{#apiInfo}} | ||
{{#apis}} | ||
import { {{classname}} } from './{{apiPackage}}'; | ||
import { {{classname}}Controller } from './controllers'; | ||
{{/apis}} | ||
{{/apiInfo}} | ||
|
||
@Module({}) | ||
export class ApiModule { | ||
static forRoot(apiImplementations: ApiImplementations): DynamicModule { | ||
const providers: Provider[] = [ | ||
{{#apiInfo}} | ||
{{#apis}} | ||
{ | ||
provide: {{classname}}, | ||
useClass: apiImplementations.{{#lambda.camelcase}}{{classname}}{{/lambda.camelcase}} | ||
}, | ||
{{/apis}} | ||
{{/apiInfo}} | ||
]; | ||
|
||
return { | ||
module: ApiModule, | ||
controllers: [ | ||
{{#apiInfo}} | ||
{{#apis}} | ||
{{classname}}Controller, | ||
{{/apis}} | ||
{{/apiInfo}} | ||
], | ||
providers: [...providers], | ||
exports: [...providers] | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
modules/openapi-generator/src/main/resources/typescript-nestjs-server/api.mustache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { Observable } from 'rxjs'; | ||
import { {{#tsImports}}{{classname}}, {{/tsImports}} } from '../{{modelPackage}}'; | ||
|
||
@Injectable() | ||
export abstract class {{classname}} { | ||
{{#operations}} | ||
{{#operation}} | ||
abstract {{operationId}}({{#allParams}}{{paramName}}: {{{dataType}}}, {{/allParams}} request: Request): {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}} | Promise<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}> | Observable<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}>; | ||
|
||
{{/operation}} | ||
{{/operations}} | ||
} |
7 changes: 7 additions & 0 deletions
7
modules/openapi-generator/src/main/resources/typescript-nestjs-server/apis.mustache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{{#apiInfo}} | ||
{{#apis}} | ||
{{#operations}} | ||
export * from './{{classFilename}}'; | ||
{{/operations}} | ||
{{/apis}} | ||
{{/apiInfo}} |
19 changes: 19 additions & 0 deletions
19
modules/openapi-generator/src/main/resources/typescript-nestjs-server/controller.mustache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Controller{{#httpMethods}}, {{.}}{{/httpMethods}}, Req } from '@nestjs/common'; | ||
import { Observable } from 'rxjs'; | ||
import { {{classname}} } from '../{{apiPackage}}'; | ||
import { {{#tsImports}}{{classname}}, {{/tsImports}} } from '../{{modelPackage}}'; | ||
|
||
@Controller() | ||
export class {{classname}}Controller { | ||
constructor(private readonly {{classVarName}}: {{classname}}) {} | ||
|
||
{{#operations}} | ||
{{#operation}} | ||
@{{#vendorExtensions.x-http-method}}{{.}}{{/vendorExtensions.x-http-method}}{{^vendorExtensions.x-http-method}}{{httpMethod}}{{/vendorExtensions.x-http-method}}('{{path}}') | ||
{{operationId}}({{#allParams}}{{paramName}}: {{{dataType}}}, {{/allParams}}@Req() request: Request): {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}} | Promise<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}> | Observable<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}> { | ||
return this.{{classVarName}}.{{operationId}}({{#allParams}}{{paramName}}, {{/allParams}}request); | ||
} | ||
|
||
{{/operation}} | ||
{{/operations}} | ||
} |
7 changes: 7 additions & 0 deletions
7
modules/openapi-generator/src/main/resources/typescript-nestjs-server/controllers.mustache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{{#apiInfo}} | ||
{{#apis}} | ||
{{#operations}} | ||
export * from './{{classFilename}}.controller'; | ||
{{/operations}} | ||
{{/apis}} | ||
{{/apiInfo}} |
30 changes: 30 additions & 0 deletions
30
modules/openapi-generator/src/main/resources/typescript-nestjs-server/git_push.sh.mustache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/bin/bash | ||
|
||
# Git push script for {{npmName}} | ||
|
||
# Check if we're in a git repository | ||
if ! git rev-parse --git-dir > /dev/null 2>&1; then | ||
echo "Error: Not in a git repository" | ||
exit 1 | ||
fi | ||
|
||
# Get the current branch | ||
CURRENT_BRANCH=$(git branch --show-current) | ||
|
||
echo "Pushing to git repository..." | ||
echo "Current branch: $CURRENT_BRANCH" | ||
|
||
# Add all changes | ||
git add . | ||
|
||
# Commit with a default message if no commit message provided | ||
if [ -z "$1" ]; then | ||
git commit -m "Update generated NestJS server code" | ||
else | ||
git commit -m "$1" | ||
fi | ||
|
||
# Push to the current branch | ||
git push origin $CURRENT_BRANCH | ||
|
||
echo "Successfully pushed to git repository!" |
61 changes: 61 additions & 0 deletions
61
modules/openapi-generator/src/main/resources/typescript-nestjs-server/gitignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# compiled output | ||
/dist | ||
/node_modules | ||
|
||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
pnpm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
lerna-debug.log* | ||
|
||
# OS | ||
.DS_Store | ||
|
||
# Tests | ||
/coverage | ||
/.nyc_output | ||
|
||
# IDEs and editors | ||
/.idea | ||
.project | ||
.classpath | ||
.c9/ | ||
*.launch | ||
.settings/ | ||
*.sublime-workspace | ||
|
||
# IDE - VSCode | ||
.vscode/* | ||
!.vscode/settings.json | ||
!.vscode/tasks.json | ||
!.vscode/launch.json | ||
!.vscode/extensions.json | ||
|
||
# dotenv environment variable files | ||
.env | ||
.env.development.local | ||
.env.test.local | ||
.env.production.local | ||
.env.local | ||
|
||
# parcel-bundler cache (https://parceljs.org/) | ||
.cache | ||
.parcel-cache | ||
|
||
# Next.js build output | ||
.next | ||
|
||
# Nuxt.js build / generate output | ||
.nuxt | ||
dist | ||
|
||
# Storybook build outputs | ||
.out | ||
.storybook-out | ||
|
||
# Temporary folders | ||
tmp/ | ||
temp/ |
15 changes: 15 additions & 0 deletions
15
modules/openapi-generator/src/main/resources/typescript-nestjs-server/model.mustache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{{#models}} | ||
{{#model}} | ||
{{#tsImports}} | ||
import { {{classname}} } from './{{filename}}'; | ||
{{/tsImports}} | ||
|
||
|
||
{{#description}} | ||
/** | ||
* {{{.}}} | ||
*/ | ||
{{/description}} | ||
{{#isEnum}}{{>modelEnum}}{{/isEnum}}{{^isEnum}}{{#isAlias}}{{>modelAlias}}{{/isAlias}}{{^isAlias}}{{#taggedUnions}}{{>modelTaggedUnion}}{{/taggedUnions}}{{^taggedUnions}}{{#oneOf}}{{#-first}}{{>modelOneOf}}{{/-first}}{{/oneOf}}{{^oneOf}}{{>modelGeneric}}{{/oneOf}}{{/taggedUnions}}{{/isAlias}}{{/isEnum}} | ||
{{/model}} | ||
{{/models}} |
1 change: 1 addition & 0 deletions
1
modules/openapi-generator/src/main/resources/typescript-nestjs-server/modelAlias.mustache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type {{classname}} = {{dataType}}; |
30 changes: 30 additions & 0 deletions
30
modules/openapi-generator/src/main/resources/typescript-nestjs-server/modelEnum.mustache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{{#stringEnums}} | ||
export enum {{classname}} { | ||
{{#allowableValues}} | ||
{{#enumVars}} | ||
{{#enumDescription}} | ||
|
||
/** | ||
* {{.}} | ||
*/{{/enumDescription}} | ||
{{name}} = {{{value}}}{{^-last}},{{/-last}} | ||
{{/enumVars}} | ||
{{/allowableValues}} | ||
} | ||
{{/stringEnums}} | ||
{{^stringEnums}} | ||
export const {{classname}} = { | ||
{{#allowableValues}} | ||
{{#enumVars}} | ||
{{#enumDescription}} | ||
|
||
/** | ||
* {{.}} | ||
*/ | ||
{{/enumDescription}} | ||
{{name}}: {{{value}}}{{^-last}},{{/-last}} | ||
{{/enumVars}} | ||
{{/allowableValues}} | ||
} as const; | ||
export type {{classname}} = typeof {{classname}}[keyof typeof {{classname}}]; | ||
{{/stringEnums}} |
23 changes: 23 additions & 0 deletions
23
modules/openapi-generator/src/main/resources/typescript-nestjs-server/modelGeneric.mustache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
export interface {{classname}}{{#allParents}}{{#-first}} extends {{/-first}}{{{.}}}{{^-last}}, {{/-last}}{{/allParents}} { {{>modelGenericAdditionalProperties}} | ||
{{#vars}} | ||
{{#description}} | ||
/** | ||
* {{{description}}} | ||
{{#deprecated}} | ||
* @deprecated | ||
{{/deprecated}} | ||
*/ | ||
{{/description}} | ||
{{^description}} | ||
{{#deprecated}} | ||
/** @deprecated */ | ||
{{/deprecated}} | ||
{{/description}} | ||
{{^modelPropertyNamingOriginal}} | ||
{{#isReadOnly}}readonly {{/isReadOnly}}{{{name}}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{#isNullable}} | null{{/isNullable}}; | ||
{{/modelPropertyNamingOriginal}} | ||
{{#modelPropertyNamingOriginal}} | ||
{{#isReadOnly}}readonly {{/isReadOnly}}{{#hasSanitizedName}}'{{{baseName}}}'{{/hasSanitizedName}}{{^hasSanitizedName}}{{{name}}}{{/hasSanitizedName}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{#isNullable}} | null{{/isNullable}}; | ||
{{/modelPropertyNamingOriginal}} | ||
{{/vars}} | ||
}{{>modelGenericEnums}} |
5 changes: 5 additions & 0 deletions
5
...tor/src/main/resources/typescript-nestjs-server/modelGenericAdditionalProperties.mustache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{{#additionalPropertiesType}} | ||
|
||
[key: string]: {{{additionalPropertiesType}}}{{#hasVars}} | any{{/hasVars}}; | ||
|
||
{{/additionalPropertiesType}} |
30 changes: 30 additions & 0 deletions
30
.../openapi-generator/src/main/resources/typescript-nestjs-server/modelGenericEnums.mustache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{{#hasEnums}} | ||
|
||
{{^stringEnums}} | ||
export namespace {{classname}} { | ||
{{/stringEnums}} | ||
{{#vars}} | ||
{{#isEnum}} | ||
{{#stringEnums}} | ||
export enum {{classname}}{{enumName}} { | ||
{{#allowableValues}} | ||
{{#enumVars}} | ||
{{name}} = {{{value}}}{{^-last}},{{/-last}} | ||
{{/enumVars}} | ||
{{/allowableValues}} | ||
}; | ||
{{/stringEnums}} | ||
{{^stringEnums}} | ||
export const {{enumName}} = { | ||
{{#allowableValues}} | ||
{{#enumVars}} | ||
{{name}}: {{{value}}}{{^-last}},{{/-last}} | ||
{{/enumVars}} | ||
{{/allowableValues}} | ||
} as const; | ||
export type {{enumName}} = typeof {{enumName}}[keyof typeof {{enumName}}]; | ||
{{/stringEnums}} | ||
{{/isEnum}} | ||
{{/vars}} | ||
{{^stringEnums}}}{{/stringEnums}} | ||
{{/hasEnums}} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.