Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
request-typer
Advanced tools
Make typed request schema and build OpenAPI Specification.
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>;
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>"
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
{
user: 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()),
},
{
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>>;
use OASBuilder
to create OpenAPI Specification from HTTP request schemas.
const Responses = {
User: {
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"
]
}
}
}
}
FAQs
Make typed request schema and build OpenAPI Specification
The npm package request-typer receives a total of 3 weekly downloads. As such, request-typer popularity was classified as not popular.
We found that request-typer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.