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 from the project root to the root directory where your Routes are located when using App Router. Next REST Framework uses this as the root directory to recursively search for your Routes, so being as specific as possible will improve performance. This option is not required when using Pages Router, but it can be used together with the apiRoutesPath option when using both routers at the same time.
apiRoutesPath Absolute path from the project root to the root directory where your API Routes are located when using Pages Router. Next REST Framework uses this as the root directory to recursively search for your API Routes, so being as specific as possible will improve performance. This option is not required when using App Router, but it can be used together with the appDirPath option when using both routers at the same time.
deniedPathsArray of paths that are denied by Next REST Framework and not included in the OpenAPI spec. Supports wildcards using asterisk * and double asterisk ** for recursive matching. Example: ['/api/disallowed-path', '/api/disallowed-path-2/*', '/api/disallowed-path-3/**'] Defaults to no paths being disallowed.
allowedPathsArray of paths that are allowed by Next REST Framework and included in the OpenAPI spec. Supports wildcards using asterisk * and double asterisk ** for recursive matching. Example: ['/api/allowed-path', '/api/allowed-path-2/*', '/api/allowed-path-3/**'] Defaults to all paths being allowed.
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 and OpenAPI documentation of the incoming request:

NameDescriptionRequired
contentTypeThe content type header of the request. A request with no content type header or a incorrect content type header 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.false
Output object

The output objects define what kind of responses are returned from your API handler and is used for the OpenAPI documentation of the response:

NameDescriptionRequired
statusA status code that your API can return.true
contentTypeThe content type header of the response.true
schemaA Zod schema describing the format of the response data. A response body 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
defaultThemeDefault theme (light/dark) to use for SwaggerUI - defaults to "light"
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.