npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

request-typer

v1.4.2

Published

Make typed request schema and build OpenAPI Specification

Downloads

43

Readme

request-typer

npm version npm download

Make typed request schema and build OpenAPI Specification.

Features

Schema

use Schema to create type definition.

const user = Schema.Object({
  id: Schema.String(),
  name: Schema.String(),
  email: Schema.Optional(Schema.String()),
  gender: Schema.Nullable(Schema.Enum(["women", "men"])),
  createdAt: Schema.Number(),
});

const union = Schema.Union([
  Schema.Number(),
  Schema.String(),
  Schema.Union([
    Schema.Number(),
    Schema.String(),
    Schema.Bolean()
  ]),
]);

and it supports static type resolution. import Resolve.

const user = Schema.Object({
  id: Schema.String(),
  name: Schema.String(),
  email: Schema.Optional(Schema.String()),
  gender: Schema.Nullable(Schema.Enum(["women", "men"])),
  createdAt: Schema.Number(),
});

/*
{
  id: string;
  name: string;
  email?: string | undefined;
  gender: "women" | "men" | null;
  createdAt: number;
}
*/
type User = Resolve<typeof user>;

Validator

use Validator to compare Schema with value.

it returns { success: true } if validation succeeded. otherwise, it returns error which includes message.

Validator.validate(Schema.Number(), 1234).success; // true

Validator.validate(Schema.Array(Schema.String()), [1, 2, 3, 4]).success; // false
Validator.validate(Schema.Array(Schema.String()), [1, 2, 3, 4]).error.description; // "should be Array<string>"

HTTP

use HTTP to define HTTP request and response body schema. use Parameter to define request parameter.

/*
GET /users/:id
{
  user: {
    id: string
  }
}
*/
HTTP.GET(
  // unique ID for this request
  "getUser",
  // path
  "/users/:id",
  // request parameters
  {
    id: Parameter.Path(Schema.String()),
  },
  // response json schema
  Schema.Object({
    id: Schema.String(),
  }),
);

and it supports static type resolution for request parameters and response body.

const request = HTTP.PUT(
  "updateUser",
  "/users/:id",
  {
    id: Parameter.Path(Schema.String()),
    name: Parameter.Body(Schema.String()),
    email: Parameter.Body(Schema.String()),
  },
  Schema.Object({
    success: Schema.Boolean(),
  }),
);

// {}
type QueryParameters = ResolveQueryParameters<typeof request.parameters>;

// { id: string }
type PathParameters = ResolvePathParameters<typeof request.parameters>;

// { name: string, email: string }
type RequestBody = ResolveRequestBody<typeof request.parameters>;

// { success: boolean }
type ResponseBody = Resolve<ObjectSchema<typeof request.response>>;

OASBuilder

use OASBuilder to create OpenAPI Specification from HTTP request schemas.

const Responses = {
  User: Schema.Object({
    id: Schema.String(),
    name: Schema.String(),
    gender: Schema.Nullable(Schema.Enum(["men", "women"])),
    email: Schema.Optional(Schema.String()),
  }),
};

const httpRequestSchemas = [
  HTTP.PATCH(
    "updateUser",
    "/user/{id}",
    {
      id: Parameter.Path(Schema.String()),
      name: Parameter.Body(Schema.String()),
    },
    Responses.User,
  ),
];

const oas = new OASBuilder({ title: "api-v1", version: "1.0.0" }, httpRequestSchemas, Responses).build();

console.log(JSON.stringify(oas));

the code above prints:

{
  "info": {
    "title": "api-v1",
    "version": "1.0.0"
  },
  "openapi": "3.0.1",
  "paths": {
    "/user/{id}": {
      "patch": {
        "operationId": "updateUser",
        "parameters": [
          {
            "required": true,
            "name": "id",
            "in": "path",
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "name": {
                    "type": "string"
                  }
                },
                "required": [
                  "name"
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/User"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "User": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "gender": {
            "type": "string",
            "enum": [
              "men",
              "women"
            ],
            "nullable": true
          },
          "email": {
            "type": "string"
          }
        },
        "required": [
          "id",
          "name",
          "gender"
        ]
      }
    }
  }
}