Codegen REST API types and requests for Angular
Automatic code generation from OpenAPI 3 for Angular
- Authors
Marc Stammerjohann
- Published at
Are you using a REST API in your Angular app to fetch data from a server? Are you manually adding model types, specifying each REST call with HttpClient
? Stop right there!
Use ng-openapi-gen for a hassle free and automatic generation of models and services based on your OpenAPI 3 specification.
Nx monorepo
Setup a monorepo using nx with Angular and NestJS.
Open package.json
and replace the start
script with:
This will start both your Angular and NestJS app in serve mode.
Angular will be exposed on localhost:4200 and Nest on localhost:3333.
Setup Swagger
Setup Swagger in NestJS for type-safe REST endpoints.
First, install the Nest swagger library.
Configure Swagger in your main.ts
file:
Visit localhost:3333/api to see the Swagger UI.
While you are add it. Create a message.entity.ts
class and annotate the attribute with @ApiProperty()
.
Use @ApiResponse({ type: Message })
to inform Swagger about the response type.
Configure Codegen
Next, you will configure ng-openapi-gen as the OpenAPI 3 code generator for Angular.
Add a config file with the name ng-openapi-gen.json
to the root folder. ng-openapi-gen
loads the config file with this name automatically. Pass ng-openapi-gen --config your-config.json
for an alternative config file name.
Copy the following config content into the file, will explain the configuration options next.
With $schema
you get autocomplete in JSON for all possible configuration options of ng-openapi-gen
.
The input
requires a file or URL of a OpenAPI 3 specification. In your case the Swagger JSON is available at localhost:3333/api-json. You should see openapi
as first value with version 3.0.0
.
output
is the directory for the generated files. The files are generated inside your Angular app. Change the directory if you choose a different app name (apps/your-name/src/api
) or perhaps you want to generate into a shared library (libs/api/src/lib
).
Last step, add a new script to your package.json
let's call it "codegen": "ng-openapi-gen"
. Run the new script, also make sure the Nest app is serving.
Your output directory (apps/your-name/src/api
) now contains a couple of new files. Next, you will start with the configuration of the REST API endpoint.
Specify API url with ApiModule
Add ApiModule
, import from apps/your-name/src/api/api.module.ts
, to your Angular AppModule
imports. Use forRoot(...)
to specify the rootUrl
pointing to localhost:3333 in development.
Because your API endpoint will be different in production you need to add the apiUrl
to the environment(.prod).ts
files.
First generated REST request
Lastly, you'll be making a REST request with the generated API. Look into the api/services
directory. A service will be generated for each tag (e.g. @ApiTags('users')
) in your Swagger API.
Use ApiService
in AppComponent
to replace the direct HttpClient
call. Under the hood ApiService
uses HttpClient
.
Perfect, you made it till the end. Keep in mind that the codegen is only as good as your Swagger documentation. If you are missing type information for the request or response, you're also missing out in codegen.
Checkout the following two posts as inspiration about type-safe endpoints with Nest.
Sponsor us
Did you find this post useful? We at notiz.dev write about our experiences developing Apps, Websites and APIs and develop Open Source tools. Your support would mean a lot to us 🙏. Receive a reward by sponsoring us on Patreon or start with a one-time donation on GitHub Sponsors.