Routes
Routes in the Nile-JS SDK
The Nile-JS SDK includes generated routes for all its operations.
These routes are used by the SDK methods to proxy requests to nile-auth, as well as directly from the React hooks and components in @niledatabase/react
.
Generating routes
Route generation depends on the framework you are using.
Next.js example
Remix example
Express example
Using routes
You typically don’t need to use the routes directly. The SDK methods and react/web components use the routes under the hood to communicate with nile-auth.
The generated routes are available via nile.api.paths
. For reference, here are the paths for the default routes:
A case where you might want to use the routes directly is when these routes are exposed as a REST API of your application backend.
For example, if you use the @niledatabase/server/express
package, the routes are exposed in the app
object and
while you may have a frontend that uses @niledatabase/react
to call these routes from the application UI,
you may want to also use them as a REST API for another backend or external service.
In this case, you need to use the routes directly. The key is to:
- Include both the session cookie and the CRSF cookie in the request headers.
- Include the CSRF token in the request body.
Here are a few examples of how to call the routes directly:
Overriding routes
Sometimes you might want to intercept or override the routes used by the SDK in order to inject your own logic. For example, adding your own logging or metrics, adding debugging information, or perhaps injecting your own cookies during login.
There are three ways to override routes:
- Route wrappers: Wrap the route in your own logic. This can be done in the routes file, and is useful for minor modifications or debugging.
- Route overrides: Override the route for a specific operation.
Route wrappers
In the examples below, we’ll use route wrappers to log the headers before every request and the body if it’s a POST request. We’ll also log the status code of the response.
Route overrides
In the examples below, we’ll add a new route that will override the default route for \auth\google\callback
with
custom logic. We are using handlersWithContext
to get the nile
object in the route handler.
handlersWithContext
returns a tuple with the nile
object, configured based on the response from the route handler,
and the response from the route handler.