
Security News
Open Source Maintainers Demand Ability to Block Copilot-Generated Issues and PRs
Open source maintainers are urging GitHub to let them block Copilot from submitting AI-generated issues and pull requests to their repositories.
ts-openapi-dsl
Advanced tools
Build OpenAPI3 contracts declaratively.
npm i ts-openapi-dsl
spec.paths = paths({
getUser: operation({
method: 'get',
path: '/users/:id',
responses: {
200: json({
schema: array(string()),
}),
},
}),
});
// same as
spec.paths = {
'/users/:id': {
'get': {
operationId: 'getUser',
responses: {
'200': {
content: {
'application/json': {
schema: {
type: 'array',
items: {
type: 'string',
},
},
},
},
},
},
},
},
};
Schema helpers can be used to remove some boilerplate around JSON schema definitions. Every helper returns a JSON schema object.
import { types as t } from 'ts-openapi-dsl';
const User = t.object('An object that represents a user', {
id: t.integer('The ID of the user'),
name: t.string('The name of the user'),
createdAt: t.dateTime(),
foo: {
type: 'string',
description: 'You can use raw JSON schema as well...',
},
});
Every helper accepts an extra
parameter that can be used to decorate the schema with additional properties.
boolean(description?: string, extra?: Schema): Schema
Defines a boolean schema, essentially:
const type = {
type: 'boolean',
description,
...extra,
};
integer(description?: string, extra?: Schema): Schema
Defines an integer schema, essentially:
const type = {
type: 'integer',
description,
...extra,
};
number(description?: string, extra?: Schema): Schema
Defines an number schema, essentially:
const type = {
type: 'number',
description,
...extra,
};
string(description?: string, extra?: Schema): Schema
Defines a string schema, essentially:
const type = {
type: 'string',
description,
...extra,
};
pattern(pattern: RegExp, description?: string, extra?: Schema): Schema
Defines a string schema with a regex pattern.
date(description?: string, extra?: Schema): Schema
Defines a string schema with date
format.
dateTime(description?: string, extra?: Schema): Schema
Defines a string schema with date-time
format.
binary(description?: string, extra?: Schema): Schema
Defines a string schema with binary
format.
email(description?: string, extra?: Schema): Schema
Defines a string schema with email
format.
constant(value: string, description?: string, extra?: Schema): Schema
Defines a string schema with an enum
that only accept a single value, essentially:
const type = {
type: 'string',
description,
enum: [value],
...extra,
};
choice(values: Record<string, string>, extra?: Schema): Schema
Defines a string schema with an enum
that only accepts predefined values. Keys of values
are the allowed values, and the corresponding values in the object are the descriptions.
const type = choice({
FOO: 'Description of FOO',
BAR: 'Description of BAR',
});
// same as
cons type = {
type: 'string',
enum: ['FOO', 'BAR'],
description: '* `FOO` - Description of FOO\n* `BAR` - Description of BAR',
};
array(items: Schema, description?: string, extra?: Schema): Schema
Defines an array schema with the specified items
, essentially:
const type = {
type: 'array',
description,
items,
...extra,
};
object(properties: Record<string, Schema>, description?: string, extra?: Schema): Schema
Defines an object schema with the specified properties.
Every helpers has an optional
variant, eg. string.optional(...)
, which signals to its parent object to make it an optional property.
Objects are strict by default (ie. additionalProperties
is set to false
).
const type = object({
foo: string(),
bar: string.optional(),
});
// same as
const type = {
type: 'object',
additionalProperties: false,
required: ['foo'],
properties: {
foo: {
type: 'string',
},
bar: {
type: 'string',
},
},
};
required(schema: Schema): Schema
Turns the given schema into a required schema (ie. in a wrapper object
, it'll
be a required
property).
const wrapped = string.optional();
const wrapper = object({
foo: wrapped,
bar: required(wrapped),
});
// same as
const wrapper = {
type: 'object',
additionalProperties: false,
required: ['bar'],
properties: {
foo: { type: 'string' },
bar: { type: 'string' },
},
};
optional(schema: Schema): Schema
Turns the given schema into an optional schema (ie. in a wrapper object
, it
won't be a required
property).
const wrapped = string.required();
const wrapper = object({
foo: wrapped,
bar: optional(wrapped),
});
// same as
const wrapper = {
type: 'object',
additionalProperties: false,
required: ['foo'],
properties: {
foo: { type: 'string' },
bar: { type: 'string' },
},
};
response(props: Response): ResponseObject
Defines a response object with a single media type:
const res = response({
description: 'Response description',
headers,
links,
type: 'application/json',
schema,
examples,
encoding,
});
// same as
const res = {
description: 'Response description',
headers,
links,
content: {
'application/json': {
schema,
examples,
encoding,
},
},
};
json(props: Response): ResponseObject
Shorthand for creating application/json
responses.
const res = json({
description: 'Response description',
headers,
links,
schema,
examples,
encoding,
});
// same as
const res = {
description: 'Response description',
headers,
links,
content: {
'application/json': {
schema,
examples,
encoding,
},
},
};
operation(op: Operation): OperationObject
Defines an operation with path
and method
.
paths(ops: Record<string, Operation>): PathsObject
Creates paths
based on the provided ops
.
MIT
FAQs
TypeScript DSL for building OpenAPI specifications
The npm package ts-openapi-dsl receives a total of 4 weekly downloads. As such, ts-openapi-dsl popularity was classified as not popular.
We found that ts-openapi-dsl 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
Open source maintainers are urging GitHub to let them block Copilot from submitting AI-generated issues and pull requests to their repositories.
Research
Security News
Malicious Koishi plugin silently exfiltrates messages with hex strings to a hardcoded QQ account, exposing secrets in chatbots across platforms.
Research
Security News
Malicious PyPI checkers validate stolen emails against TikTok and Instagram APIs, enabling targeted account attacks and dark web credential sales.