@open-apiary/nuxt
v0.0.8
Published
The Nuxt module for open-apiary. It mounts a package that `apiary build` produced, with one server route for each endpoint.
Readme
@open-apiary/nuxt
The Nuxt module for open-apiary. It mounts a package that apiary build
produced, with one server route for each endpoint.
Install
pnpm add @open-apiary/nuxt open-apiary @acme/apiSetup
// nuxt.config.ts
export default defineNuxtConfig({
modules: ["@open-apiary/nuxt"],
openApiary: { package: "@acme/api", base: "/api" },
});packagenames the package thatapiary buildproduced. It exports./manifest.json,./openapi.json, and./routes/*.baseis the path the API mounts at. The default is/api.
The app registers no provider. The package holds the classes of the API and
of its presets, under its providers/, and the module hands them to the
engine.
The module reads the manifest of the package at build time. It makes one
Apiary engine for the server, in a virtual module, over the data of the
manifest and one dynamic import for each route module of the package. Then
it registers:
- One lazy server handler for each endpoint, under its method and its
route:
GET /users/{id}isGET <base>/users/:id. The handler is thedefineApiaryRouteof@open-apiary/h3over the engine. Nitro writes one chunk for each endpoint, and the engine loads it at the first request to its route. The handler reads the path parameters, the query, the headers, and the body, and the engine runs the call. An exception that the endpoint declares becomes a response with its status and its problem details object, asapplication/problem+json. Any other error is a 500. GET <base>/openapi.json, the document of the package.GET <base>/docs, a Scalar page over it.<base>/**, which answers 404 for a request that no endpoint takes. The module leaves it out when the base is the root, where it would take the pages of the app.
When the package has the types of its document, openapi.d.ts beside
openapi.json, the module types each route for Nitro from them. $fetch
and useFetch then know what a route of the API answers, with no client
and no import:
const user = await $fetch("/api/users/123"); // the `User` of the document
const { data } = await useFetch("/api/users"); // `User[] | undefined`The type is the JSON body of the response with the status of the endpoint,
and undefined for an endpoint with no body. Nitro types what a route
answers, and not what it takes: for the body, the query, and the path of a
request, import the types from <package>/openapi. The package exports
them as ./openapi.
When the manifest lists providers, the module also imports <package>/providers
and hands the list to the engine, so the classes that the API and its
presets ship fill their contracts. The package then exports ./providers.
When the manifest lists contracts, the server checks that a class fills
each one as it makes the engine, and fails with UnfilledContractsError
for those that none does.
The module reads the built package, so run apiary build before the build
of the app, and again after a change of the API.
The server file
A config holds no code, so the module takes the code of the server from a
file of the app. server names it, and its default export is the options:
// nuxt.config.ts
export default defineNuxtConfig({
modules: ["@open-apiary/nuxt"],
openApiary: { package: "@acme/api", server: "~~/server/apiary" },
});// server/apiary.ts
import type { ServerOptions } from "@open-apiary/nuxt";
export default {
challenge: "Bearer",
caller: async (event) => {
const user = await userOf(event);
return user === undefined
? undefined
: { scopes: user.scopes, subject: user.id };
},
onError: (error, { id }) => report(error, id),
} satisfies ServerOptions;callerbuilds the caller of a request, or returnsundefinedfor a request with none. It runs for a request that an endpoint takes, and for no other request of the app: a page, an asset, or a route of the app itself does not pay for it.challengeis theWWW-Authenticateof a 401.authorizedecides whether a caller may call an endpoint with scopes. The default requires every scope of the endpoint.onErrortakes each error that the engine answers with a 500 for.
The caller
Without a caller in the server file, each handler reads the caller from
event.context.apiary.caller. A server middleware sets it from the
request, and runs for every request of the app:
// server/middleware/auth.ts
export default defineEventHandler((event) => {
const token = getHeader(event, "authorization");
if (token === undefined) {
return;
}
event.context.apiary = {
caller: { scopes: scopesOf(token), subject: subjectOf(token) },
};
});A request that no middleware describes has no caller, and an endpoint that requires one answers 401. A caller without the scopes of an endpoint gets a 403.
