Skip to main content

API reference

Config options

The optional config options allow you to customize Next REST Framework. The following options can be passed as a parameter for your NextRestFramework client in an object:

NameDescription
appDirPath Absolute path to the app directory where your routes are located, usually either app or src/app. Required when using App Router. Can be used together with apiRoutesPath
apiRoutesPath Absolute path to the directory where your API routes are located, usually pages/api or src/pages/api. Required when using Pages Router. Can be used together with appDirPath.
openApiJsonPathCustom path for serving openapi.json file. Defaults to /api/openapi.json.
openApiYamlPathCustom path for serving openapi.yaml file. Defaults to /api/openapi.yaml.
swaggerUiPathCustom path for service Swagger UI. Defaults to /api.
swaggerUiConfigA SwaggerUI config object for customizing the generated SwaggerUI.
exposeOpenApiSpecSetting this to false will serve none of the OpenAPI documents neither the Swagger UI. Defaults to true.
errorHandlerAn error handler function used to catch errors in your routes. Both synchronous and asynchronous handlers are supported. The function takes in the same parameters as the Next.js App Router and API Routes handlers. Defaults to a basic error handler logging the errors in non-production mode.
suppressInfoSetting this to true will suppress all informational logs from Next REST Framework. Defaults to false.
generatePathsTimeoutTimeout in milliseconds for generating the OpenAPI spec. Defaults to 5000. For large applications you might have to increase this.

Route config

The route config parameters define an individual route, applicable for all endpoints (methods) that are using that route:

NameDescriptionRequired
GET \| PUT \| POST \| DELETE \| OPTIONS \| HEAD \| PATCHA Method handler object.true
openApiSpecOverridesAn OpenAPI Path Item Object that can be used to override and extend the auto-generated and higher level specifications.false

Method handlers

The method handler parameters define an individual endpoint:

NameDescriptionRequired
inputAn Input object object.false
outputAn array of Output objects. true
handler Your handler function that takes in your typed request and response (when using Pages Router). Both synchronous and asynchronous handlers are supported. The function takes in strongly-typed versions of the same parameters as the Next.js App Router and API Routes handlers.true
openApiSpecOverridesAn OpenAPI Operation object that can be used to override and extend the auto-generated and higher level specifications.false
Input object

The input object is used for the validation of the incoming request:

NameDescriptionRequired
contentTypeThe content type that the request must have - request with no content type or incorrect content type will get an error response.true
bodyA Zod schema describing the format of the request body.true
queryA Zod schema describing the format of the query parameters. Note that Next.js parses the query string into an object containing either strings or arrays of strings.false
Output object

The output objects define what kind of responses you are allowed to return from your API handlers:

NameDescriptionRequired
statusA possible status code that your API can return - using other status codes will lead to a TS error.true
contentTypeThe content type of the response - using other content-types will lead to a TS error.true
schemaA Zod schema describing the format of the response data. A response format not matching to the schema will lead to a TS error. true

SwaggerUI config

The SwaggerUI config object can be used to customize the generated Swagger UI:

NameDescription
titleCustom page title meta tag value.
descriptionCustom page description meta tag value.
logoHrefAn href for a custom logo.
faviconHrefAn href for a custom favicon.

Changelog

See the changelog in CHANGELOG.md

Contributing

All contributions are welcome!

License

ISC, see full license in LICENSE.