Deprecating Node JS REST APIs in 6 Frameworks
Deprecating an API is never is easy (in fact we wrote a full guide on API deprecation) - but many popular Node JS frameworks can ease the pain. Deprecating an API endpoint typically involves notifying consumers that the endpoint is outdated and will be removed in the future. As a developer, you can communicate this two ways
- Through API documentation (an OpenAPI spec generated from your framework of choice).
- By sending the
HTTP
Deprecation
header.
Here's how different Node.js frameworks handle API deprecation:
1. Fastify#
Support Level: Built-in Support via Plugins
Fastify allows you to set custom metadata for routes, and when combined with the
fastify-swagger
plugin, you can
mark routes as deprecated in your OpenAPI documentation.
Fastify API Deprecation Example#
Generate an OpenAPI file using fastify-swagger
, and use the deprecated
property to mark the API endpoint as deprecated . Additionally, send the
deprecation header back in the route's implementation.
2. Express JS#
Support Level: Manual Implementation
Express.js doesn't have built-in support for deprecating APIs, but you can implement deprecation logic manually.
Express JS Deprecation Header Example#
-
Adding the header at the route level:
You can add the header directly in your route's response.
-
Using Middleware:
If you are deprecating multiple endpoints, you can use middleware to maximize code reuse.
Express JS OpenAPI Deprecation Example#
You can use the swagger-jsdoc package to generate an OpenAPI file from your routes, and mark them deprecated. You can additionally use swagger-ui-express to serve your docs.
3. NestJS#
Support Level: Built-in Support via Decorators
NestJS is a progressive Node.js framework that uses decorators and metadata,
making it easy to mark endpoints as deprecated. With the
@nestjs/swagger
package, you
can integrate Swagger/OpenAPI and use the @ApiOperation()
decorator.
NestJS API Deprecation Example#
Set the @ApiOperation({ deprecated: true })
directive to handle updating your
OpenAPI docs and the @Header("Deprecation", "@1688169599")
directive will
return the deprecation header for you.
4. Hapi.js#
Support Level: Built-in Support via Route Options
Hapi.js allows you to set route-specific metadata, including marking routes as
deprecated, especially when using the
hapi-swagger
plugin for
documentation.
Hapi.js API Deprecation Example#
Simply use the top-level deprecated
property to update your OpenAPI, and set
the deprecation header in your route's response.
5. Restify#
Support Level: Manual Implementation
Restify is focused on building RESTful APIs and allows for middleware and custom headers, but doesn't have built-in deprecation support. As far as I can tell, there isn't a widely accepted OpenAPI/Swagger generator either.
Restify API Deprecation Example#
This is pretty obvious, but simply return the deprecation header via
res.header
.
6. Koa.js#
Support Level: Manual Implementation
Koa.js allows you to set response headers and create middleware but doesn't have built-in support for deprecation. I scoured NPM but couldn't find any modules for OpenAPI generation from Koa - but I did find 2 solutions that might work for you.
koa-swagger-decorator
: A library that only supports OpenAPI 2.0 (which does have adeprecated
property)- Use Tsoa over your Koa API: You can read this awesome guide
Koa.js API Deprecation Example#
In case you don't want/care about OpenAPI support, you can just send the deprecation header back.
Takeaways#
Deprecating API endpoints requires a surprising amount of manual effort in most Node JS frameworks - and deprecating an entire API is tedious in all frameworks. Here's my recommendations:
- Utilizing Middleware: Utilize middleware to set deprecation headers consistently. This seems to be the only way to "deprecate" an entire API in most frameworks.
- Consider Frameworks with OpenAPI Support: It might be time to ditch legacy frameworks that lack OpenAPI support - your users will thank you. The irony is that you won't have an easy way to deprecate these APIs as you migrate!