Skip to content

normeno/google-json-style-guide

Repository files navigation

Google JSON Style Guide

Software License Build Status npm version npm downloads

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.

Basic Usage

Install

npm i google-json-style-guide

Import

import GjsonResponse from "google-json-style-guide";

Basic usage

const apiVersion = "1.0";
const context = "My Context";
const gjsonResponse = new GjsonResponse(apiVersion, context);

Parse

Dates

* Dates are not automatically transformed

gjsonResponse.format().dateTimeToRFC3339('31-10-2020', 'DD-MM-YYYY');

Camel Case

gjsonResponse.format().toCamelCase(data)

Remove empty, null and undefined

gjsonResponse.format().removeByValue(data['key'])

Parse all response (Except dates)

gjsonResponse.format().formatAll(data);

Responses

List

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));

item

const data = {
    id: 2,
    email: "[email protected]",
    firstName: "First",
    lastName: "Last"
};

res.json(gjsonResponse.one(data));

Error

const data = {
    code: "404",
    message: "Not Found"
};

res.status(404).json(gjsonResponse.error(data));

Stored/Updated/Deleted

res.json(gjsonResponse.store(true));
res.json(gjsonResponse.update(true));
res.json(gjsonResponse.delete(true));

About

Format responses with Google Json Style Guide

Resources

License

Stars

Watchers

Forks

Packages

No packages published