Google JSON Style Guide is a NPM library to work under the Google Json Style Guide standard. This package tries to offer facilities to handle the standard, or simply to force it.
npm i google-json-style-guide
import GjsonResponse from "google-json-style-guide";
const apiVersion = "1.0";
const context = "My Context";
const gjsonResponse = new GjsonResponse(apiVersion, context);
* Dates are not automatically transformed
gjsonResponse.format().dateTimeToRFC3339('31-10-2020', 'DD-MM-YYYY');
gjsonResponse.format().toCamelCase(data)
gjsonResponse.format().removeByValue(data['key'])
gjsonResponse.format().formatAll(data);
const results = {};
const data = {
totalItems: 40,
itemsPerPage: 10,
totalPages: 4,
previousLink: "https://my.url?page=1",
selfLink: "https://my.url?page=2",
nextLink: "https://my.url?page=3",
items: results
};
res.json(gjsonResponse.list(data));
const data = {
id: 2,
email: "[email protected]",
firstName: "First",
lastName: "Last"
};
res.json(gjsonResponse.one(data));
const data = {
code: "404",
message: "Not Found"
};
res.status(404).json(gjsonResponse.error(data));
res.json(gjsonResponse.store(true));
res.json(gjsonResponse.update(true));
res.json(gjsonResponse.delete(true));