@dwp/casa-massimo-cli
v0.1.1
Published
A command line tool for generating CASA massimo plugins (wraps massimo-cli)
Readme
casa-massimo-cli
A command line tool for generating CASA massimo plugins (wraps the Massimo cli tool).
Installation
npm install @dwp/casa-massimo-cli --save-dev
npm install @dwp/casa-massimo-pluginUsage
Generate a CASA Massimo plugin from a local Open API schema (JSON or YAML) and
save it into our /plugins folder:
npx casamassimo ./schemas/movies.openapi.json --output=casa --name=movies --folder=./plugins/moviesYou also fetch the schema from a remote URL:
npx casamassimo https://movies.api/api-docs --output=casa --name=movies --folder=./plugins/moviesThen import it into your CASA app:
import { configure } from "@dwp/govuk-casa";
import moviesPlugin from "./plugins/movies/movies.mjs";
const { ancilloryRouter } = configure({
...
plugins: [await moviesPlugin({
// The base URL of the API
url: "http://movies.api",
// Optional: throw an error on error level status codes. Default: false.
throwOnError: true,
// Set headers on all HTTP requests from the client:
getHeaders(req, res, opts) {
return { "x-correlation-id": req.id };
}
})]
});
ancilloryRouter.get("/movie/:id", async (req, res, next) => {
const movie = await req.movies.getMovieById({ id: 100 });
res.render("movie.njk", movie.body);
});You can also generate standard express middleware:
npx casamassimo ./schemas/movies.openapi.json --output=express --name=movies --folder=./middleware/moviesimport express from "express";
import middleware from "./middleware/movies/movies.mjs";
const app = express();
// Supports the same options
app.use(await middleware({ url: "http://movies.api" }));
app.get("/movie/:id", async (req, res, next) => {
const movie = await req.movies.getMovieById({ id: 100 });
res.json("movie.njk", movie.body);
});Or just a client library:
npx casamassimo ./schemas/movies.openapi.json --output=client --name=movies --folder=./lib/moviesimport client from "./lib/movies/movies.mjs";
const movie = await client.getMovieById({ id: 100 });By default Massimo cli will enable both the "full request" and "full response"
Massimo client options with the --full option. If you just want one or the
other, you must disable --full then enable either --full-request or
--full-response:
npx casamassimo ./schemas/movies.openapi.json --output=client --full=false --full-response=trueFor example:
// Full request clients require you to specify where the params go in the
// request:
const res = fullRequestClient.findMovie({
query: {
title: "Rope"
},
headers: {
auth: "Token"
}
});
// With full request disabled you can use a flat options object:
const res = await notFullRequestClient.findMovie({ title: "Rope", auth: "Token" });
// A full response client returns the status code, headers and body:
const res = await fullBodyClient.findMovie({ title: "Rope", auth: "Token" });
console.log(res); // { statusCode: 200, headers: {...}, body: { id: 100 } }
// With full response disabled the client will only return the body
const movie = await notFullBodyClient.findMovie({ title: "Rope", auth: "Token" });
console.log(movie); // { id: 100 }For more information see the Massimo documentation.
