
Research
Security News
Lazarus Strikes npm Again with New Wave of Malicious Packages
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
express-openapi-typer
Advanced tools
Code-generation-free conversion of OpenAPI schema into typed Express request handlers
Code-generation-free conversion of OpenAPI v3.1 schema into type-checked Express request handlers.
Derive Express handler types from an OpenAPI schema to get
req.param
, req.query
, req.body
, res.send()
, res.json()
etc.Note that the library does not perform runtime validation against the OpenAPI schema: add something like https://github.com/Hilzu/express-openapi-validate for that purpose.
Requires OpenAPI v3.1. This library relies heavily on existing JSON Schema tooling whereas earlier OpenAPI versions use the OpenAPI Schema Object instead of pure JSON Schema. OpenAPI v3.1
is yet unpublished; track progress here. Read more about the OpenAPI/JSON Schema divergence at https://apisyouwonthate.com/blog/openapi-and-json-schema-divergence-part-1 and how v3.1
solves it at https://phil.tech/2019/09/07/update-openapi-json-schema/.
yarn add express-openapi-typer
First define your OpenAPI schema as a TypeScript type:
interface PetStoreSchema {
openapi: '3.1.0'
info: { ... }
paths: {
'/pets': {
get: { ...}
},
...
}
}
And then override your Express router's type from
const router = express.Router()
into the following:
import { OpenAPIRouter } from 'express-openapi-typer'
const router = (express.Router() as unknown) as OpenAPIRouter<PetStoreSchema>
Handler functions in router
now get type-checked as per PetStoreSchema
! For example when using the full sample PetStore schema we end up with the following:
It can be useful to instantiate the OpenAPI schema as a runtime value instead of a plain type. For example when serving the schema as documentation or handling validation we need to access the schema at runtime. In cases like these combine typeof
and as const
to access the schema type:
const petStoreSchema = {
openapi: '3.1.0',
info: { ... },
paths: {
'/pets': {
get: { ... }
},
...
}
} as const // <-- important!
type PetStoreSchema = typeof petStoreSchema
By default OpenAPIRouter
doesn't allow any additional handlers not defined in the OpenAPI schema. To loosen this restriction you can expand the type as follows:
import * as express from 'express'
const router = express.Router() as OpenAPIRouter<PetStoreSchema> & express.Router
You can also select a subset of express.Router
with Pick
/Omit
when allowing additional methods only for a specific HTTP method, for example.
json-schema-type-mapper
apply here as wellas unknown
cast$ref
s, not just $id
-based ones
"#/components/schemas/NewUser"
apart at type-level[0.0.1] - 2019-12-09
FAQs
Code-generation-free conversion of OpenAPI schema into typed Express request handlers
The npm package express-openapi-typer receives a total of 0 weekly downloads. As such, express-openapi-typer popularity was classified as not popular.
We found that express-openapi-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.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.
Security News
Opengrep continues building momentum with the alpha release of its Playground tool, demonstrating the project's rapid evolution just two months after its initial launch.