This file is automatically loaded when novel starts.
It still needs you to specify the endpoint of the API via Fastify's routing.
instanceis an instance of a Fastify plugin. All Fastify interfaces are available here and additional ones that are discussed below.
Below are all the examples of valid route registrations
instance.get('/api/v1/account',handler);instance.post('/api/v1/account',handler);instance.patch('/api/v1/account',handler);instance.head('/api/v1/account',handler);instance.put('/api/v1/account',handler);instance.delete('/api/v1/account',handler);instance.options('/api/v1/account',handler);instance.any('/api/v1/account',handler);// you can add fastify route options as wellinstance.get('/api/v1/account',fastifyRouteOptions,handler);
Each route has access to a request context that uses Node's async_hooks, and @fastify/request-context library.
This allows Novel to tap into relevant context of the request outside of the request handler. For example, retrieving an account ID in a database model.
Using API Keys
If you want to restrict endpoints based on API keys, you can use the directive below:
instance.authorized()
These sessions are the same in both cookie and api key contexts.
This will check if the API key belongs to a specific user and fill in the relevant session variables like the above.
You can use both authorized and authenticated directives in the same route definition.