npm install fastify-oapifastify-oapi version | fastify version |
|---|---|
v1.x | v3.x |
v2.x | v4.x, v5.x * |
* Fastify v4 uses Ajv v8 per default. So it's not possible to use json schema draft-04 to fully align to OpenAPI v3.0 specification.
import*asFastifyfrom"fastify";importfastifyOpenApi,{getAjvOptions}from"fastify-oapi";import*ascontrollerfrom"./controller";constspecification=__dirname+"/openapi.yml";// Extend Fastify Ajv options to be fully Open API v3 compliantfastify=Fastify({ajv: getAjvOptions(),});// Configure the pluginfastify.register(fastifyOpenApi,{
specification,
controller,});awaitfastify.listen(3000);specification: A valid Open API v3 specification. It can be provided as a path, an URL or a JavaScript object.resolution: Specify how to resolve controllers. See Controller Resolution.controller: When usinguniqueresolution mode, this value is used to resolve to a controller for the API. See Controller Resolution.controllersDir: When usingper-routeorper-operationresolution mode, this value is used to determine where controllers are located. See Controller Resolution.resolutionConfig: When usingmanualresolution mode, this value is used to configure how routes are mapped to controllers. See Controller Resolution.prefix: Routes URL prefix (eg:/routewith/v1prefix create a/v1/routeroute).
To allow some openapi extensions fastify-oapi provides an ajv plugin. To enable the plugin, fastify-oapi provides the getAjvOptions() help.
import*asFastifyfrom"fastify";import{getAjvOptions}from"fastify-oapi";// Add ajv plugin with no optionsfastify=Fastify({ajv: getAjvOptions()});// Extends Ajv custom optionsfastify=Fastify({ajv: getAjvOptions({removeAdditional: false})});// Add other Ajv pluginsfastify=Fastify({ajv: getAjvOptions({removeAdditional: false},[[otherPlugin,otherPluginOptions]])});
## ControllerResolution`fastify-oapi`canresolvecontrollersinmultipleways.Itcanbeconfiguredviatheoption`resolution`.Thisoptionacceptthefollowingvalues: `per-route`,`per-operation`,`unique`or`manual`.
#### per-routeThemode`per-route`isanautomaticresolutionmode.ItlooksforcontrollersbasedontherouteURL.**Itrequiresthe`controllersDir`option.**Examples:
-`/pets`routesto`pets.controller.js`in`controllersDir`.-`/pets/:petId`routesto`pets.controller.js`in`controllersDir`.-`/pets/getByStatus`routesto`pets.controller.js`in`controllersDir`.-`/`routesto`root.controller.js`in`controllersDir`.-`/:rootId`routesto`root.controller.js`in`controllersDir`.
#### per-operationThemode`per-operation`looksfora`x-controller`taginoperationspecification.**Itrequiresthe`controllersDir`option.**Examples:
```yaml"/pet": post: summary: Add a new pet to the store x-controller: pets.controller operationId: addPet # This operation routes to `pets.controller` in `controllerDir` put: summary: Update an existing pet x-controller: petupdate.controller operationId: updatePet # This operation routes to `petupdate.controller` in `controllerDir`The mode manual allows to configure how routes will be mapped to controllers. The resolutionConfig option is an object where keys could be the route URL, a prefix for the route URL or a RegExp that matche the route URL. If not found, it use the default to route to the default controller. It requires the resolutionConfig option.
Examples:
constoptions={resolution: "manual",controllersDir: "./controllers",resolutionConfig: {"/pets": "pets.controller","/orders/(.*)/action": "orders-action.controller","/orders(.*)": "orders.controller",default: "default.controller",},};Using the configuration below:
/petsroutes topets.controller.jsincontrollersDir./pets/:petIdroutes topets.controller.jsincontrollersDir./pets/getByStatusroutes topets.controller.jsincontrollersDir./ordersroutes toorders.controller.jsincontrollersDir./orders/:orderIdroutes toorders.controller.jsincontrollersDir./orders/:orderId/actionroutes toorders-action.controller.jsincontrollersDir./anythingroutes todefault.controller.jsincontrollersDir.
The mode unique is the simpler mode. It allows to configure an unique controller for the whole API by using the controller option. It requires the controller option.
If the resolution option is not provided, the engine will try to look for options to determine the resolution mode:
- If
controlleris defined, use theuniqueresolution mode. - If
resolutionConfigis defined, use themanualresolution mode. - If
controllersDiris defined, use theper-routeresolution mode.
fastify-oapi supports the x-partial Open API extension which allows to override required configuration of JSON Schemas. It is useful to easily allow some operations to returns partial entities while keeping shared schemas intact.
Examples:
/partial:
get:
operationId: getPartialsummary: x-partial responseresponses:
"200":
description: partialcontent:
application/json:
schema:
x-partial: truetype: objectproperties:
response:
type: stringrequired:
- response"201":
description: partial $refcontent:
application/json:
schema:
x-partial: true$ref: "#/components/schemas/ResponseObject"This project is under MIT License. See the LICENSE file for the full license text.