0.9.0 • Published 5 years ago

partial-responsify v0.9.0

Weekly downloads
38
License
MIT
Repository
-
Last release
5 years ago

Partial Responsify

npm downloads

Validate and return partial response from field, just like graphql, but without the black magic.

Mainly for using with RESTful API gateway

Build with typescript. (Will not even think of making this kind of library without typing)

Installation

npm install --save-exact partial-responsify

Note: install the exact version or the patch version before the project pass 1.0, breaking change might happen in minor release pre 1.0

Usage

A better way is to follow the test/*.test.ts

  • simple csv with graphql like nested field
import { PartialResponsify, ResponseFormat } from "partial-responsify";

// ideally should be inside DI
const pr = new PartialResponsify();

// it is normally inside ctx.query for Koa
// default don't support whitespace or - or _
// (normally should be camelcase?)
const fields = "name,coords,author{name{first}}";
const responseFormat: ResponseFormat = {
    fields: {
        author: {
            fields: {
                name: {
                    fields: {
                        first: {
                            type: "string",
                        },
                        last: {
                            type: "string",
                        },
                    },
                    type: "object",
                },
            },
            type: "object",
        },
        coords: {
            items: {
                items: {
                    type: "number",
                },
                type: "array",
            },
            type: "array",
        },
        license: {
            type: "string",
        },
        name: {
            type: "string",
        },
    },
    type: "object",
};

// you can use this fieldsToParse to track the fields usage
const fieldsToParse = pr.parseFields(fields, responseFormat);
console.log(fieldsToParse);

// and then you perform some logic and got the result
const result = {
    author: {
        name: {
            first: "Liam",
            last: "Ng",
        },
        url: "https://www.leliam.com",
    },
    coords: [[13.37, 1.337], [0, 0]],
    license: "MIT",
    name: "partial-responsify",
};
const res = pr.parseResult<any>(fieldsToParse, responseFormat, result);
console.log(res);
/*
the result should be:
[ [ [], 'name' ],
  [ [], 'coords' ],
  [ [ 'author', 'name' ], 'first' ] ]
{ author: { name: { first: 'Liam' } },
  coords: [ [ 13.37, 1.337 ], [ 0, 0 ] ],
  name: 'partial-responsify' }
*/
  • generate schema for use with swagger
import { ResponseFormat, SchemaGenerator } from "partial-responsify";
const sgen = new SchemaGenerator();
const responseFormat: ResponseFormat = {
    items: {
        fields: {
            a: {
                type: "number",
            },
            b: {
                type: "string",
            },
            c: {
                type: "integer",
            },
            d: {
                format: "uuid",
                type: "string",
            },
            e: {
                fields: {
                    a: {
                        type: "number",
                    },
                },
                type: "object",
            },
        },
        type: "object",
    },
    type: "array",
};
const result = sgen.generate(responseFormat);
console.log(result);
/*
{
    items: {
        properties: {
            a: {
                type: "number",
            },
            b: {
                type: "string",
            },
            c: {
                type: "integer",
            },
            d: {
                format: "uuid",
                type: "string",
            },
            e: {
                properties: {
                    a: {
                        type: "number",
                    },
                },
                type: "object",
            },
        },
        type: "object",
    },
    type: "array",
}
*/
  • nested fields
    • graphql way: const fields = "name,license,author{name{first,last},url}";
  • generate example string to get full response based on ResponseBody with FieldsExampleGenerator

Validation Handling

In case of validation failure, it will return a PartialResponsifyValidationError. The error default with a message, but has sufficient fields to let you format into any language

Additional Validation

Able to add additional validation by user in case it is not sufficient

Still thinking

  • should we support google way: const fields = "name,license,author(name(first,last),url)";
  • should we allow optional field?
  • should we validate additional type such as minLength, maxLength
0.9.0

5 years ago

0.8.1

5 years ago

0.8.0

5 years ago

0.7.0

5 years ago

0.6.4

6 years ago

0.6.3

6 years ago

0.6.2

6 years ago

0.6.1

6 years ago

0.6.0

6 years ago

0.5.1

6 years ago

0.5.0

6 years ago

0.4.0

6 years ago

0.3.5

6 years ago

0.3.4

6 years ago

0.3.3

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.0

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.0

6 years ago