New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

openapi-typescript

Package Overview
Dependencies
Maintainers
1
Versions
145
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

openapi-typescript - npm Package Compare versions

Comparing version 4.4.0 to 4.5.0

dist/esm/index.mjs

7

bin/cli.js

@@ -128,3 +128,3 @@ #!/usr/bin/env node

await fs.promises.writeFile(outputFilePath, result, "utf8");
fs.writeFileSync(outputFilePath, result, "utf8");

@@ -157,4 +157,3 @@ const timeEnd = process.hrtime(timeStart);

if (/^https?:\/\//.test(pathToSpec)) {
if (output !== "." && output === OUTPUT_FILE)
await fs.promises.mkdir(path.dirname(cli.flags.output), { recursive: true });
if (output !== "." && output === OUTPUT_FILE) fs.mkdirSync(path.dirname(cli.flags.output), { recursive: true });
await generateSchema(pathToSpec);

@@ -188,3 +187,3 @@ return;

}
await fs.promises.mkdir(outputDir, { recursive: true }); // recursively make parent dirs
fs.mkdirSync(outputDir, { recursive: true }); // recursively make parent dirs
}

@@ -191,0 +190,0 @@ await generateSchema(specPath);

@@ -1,5 +0,5 @@

import { OpenAPI2, OpenAPI3, SchemaObject, SwaggerToTSOptions } from "./types";
export * from "./types";
import type { OpenAPI2, OpenAPI3, SchemaObject, SwaggerToTSOptions } from "./types";
export * from "./types.js";
export declare const WARNING_MESSAGE = "/**\n* This file was auto-generated by openapi-typescript.\n* Do not make direct changes to the file.\n*/\n\n\n";
declare function openapiTS(schema: string | OpenAPI2 | OpenAPI3 | Record<string, SchemaObject>, options?: SwaggerToTSOptions): Promise<string>;
export default openapiTS;

@@ -30,10 +30,10 @@ "use strict";

const path_1 = __importDefault(require("path"));
const kleur_1 = require("kleur");
const color = __importStar(require("kleur/colors"));
const prettier_1 = __importDefault(require("prettier"));
const parser_typescript_1 = __importDefault(require("prettier/parser-typescript"));
const parser_typescript_js_1 = __importDefault(require("prettier/parser-typescript.js"));
const url_1 = require("url");
const load_1 = __importStar(require("./load"));
const utils_1 = require("./utils");
const index_1 = require("./transform/index");
__exportStar(require("./types"), exports);
const load_js_1 = __importStar(require("./load.js"));
const utils_js_1 = require("./utils.js");
const index_js_1 = require("./transform/index.js");
__exportStar(require("./types.js"), exports);
exports.WARNING_MESSAGE = `/**

@@ -60,6 +60,6 @@ * This file was auto-generated by openapi-typescript.

if (typeof schema === "string") {
const schemaURL = (0, load_1.resolveSchema)(schema);
const schemaURL = (0, load_js_1.resolveSchema)(schema);
if (options.silent === false)
console.log((0, kleur_1.yellow)(`🔭 Loading spec from ${(0, kleur_1.bold)(schemaURL.href)}…`));
await (0, load_1.default)(schemaURL, {
console.log(color.yellow(`🔭 Loading spec from ${color.bold(schemaURL.href)}…`));
await (0, load_js_1.default)(schemaURL, {
...ctx,

@@ -81,6 +81,6 @@ schemas: allSchemas,

else {
await (0, load_1.default)(schema, {
await (0, load_js_1.default)(schema, {
...ctx,
schemas: allSchemas,
rootURL: new url_1.URL(load_1.VIRTUAL_JSON_URL),
rootURL: new url_1.URL(load_js_1.VIRTUAL_JSON_URL),
httpHeaders: options.httpHeaders,

@@ -90,3 +90,3 @@ httpMethod: options.httpMethod,

for (const k of Object.keys(allSchemas)) {
if (k === load_1.VIRTUAL_JSON_URL) {
if (k === load_js_1.VIRTUAL_JSON_URL) {
rootSchema = allSchemas[k];

@@ -101,4 +101,4 @@ }

if (!(options === null || options === void 0 ? void 0 : options.version) && !ctx.rawSchema)
ctx.version = (0, utils_1.swaggerVersion)(rootSchema);
const rootTypes = (0, index_1.transformAll)(rootSchema, { ...ctx });
ctx.version = (0, utils_js_1.swaggerVersion)(rootSchema);
const rootTypes = (0, index_js_1.transformAll)(rootSchema, { ...ctx });
for (const k of Object.keys(rootTypes)) {

@@ -114,3 +114,3 @@ if (typeof rootTypes[k] === "string") {

output += ` "${subschemaURL}": {\n`;
const subschemaTypes = (0, index_1.transformAll)(external[subschemaURL], { ...ctx, namespace: subschemaURL });
const subschemaTypes = (0, index_js_1.transformAll)(external[subschemaURL], { ...ctx, namespace: subschemaURL });
for (const k of Object.keys(subschemaTypes)) {

@@ -124,3 +124,3 @@ output += ` "${k}": {\n ${subschemaTypes[k]}\n }\n`;

parser: "typescript",
plugins: [parser_typescript_1.default],
plugins: [parser_typescript_js_1.default],
};

@@ -127,0 +127,0 @@ if (options && options.prettierConfig) {

/// <reference types="node" />
import type { GlobalContext, Headers } from "./types";
import { URL } from "url";
import { GlobalContext, Headers } from "./types";
declare type PartialSchema = Record<string, any>;

@@ -5,0 +5,0 @@ declare type SchemaMap = {

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {

@@ -14,4 +33,4 @@ return (mod && mod.__esModule) ? mod : { "default": mod };

const js_yaml_1 = __importDefault(require("js-yaml"));
const kleur_1 = require("kleur");
const utils_1 = require("./utils");
const color = __importStar(require("kleur/colors"));
const utils_js_1 = require("./utils.js");
exports.VIRTUAL_JSON_URL = `file:///_json`;

@@ -67,3 +86,3 @@ function parseSchema(schema, type) {

catch (err) {
console.error((0, kleur_1.red)(`Cannot parse key: ${k} into JSON format. Continuing with the next HTTP header that is specified`));
console.error(color.red(`Cannot parse key: ${k} into JSON format. Continuing with the next HTTP header that is specified`));
}

@@ -90,3 +109,3 @@ }

if (isFile(schemaURL)) {
contents = await fs_1.default.promises.readFile(schemaURL, "utf8");
contents = fs_1.default.readFileSync(schemaURL, "utf8");
contentType = mime_1.default.getType(schemaID) || "";

@@ -99,3 +118,3 @@ }

if (options.auth)
headers.Authorizaton = options.auth;
headers.Authorization = options.auth;
if (options.httpHeaders) {

@@ -139,3 +158,3 @@ const parsedHeaders = parseHttpHeaders(options.httpHeaders);

return v;
const { url: refURL } = (0, utils_1.parseRef)(v);
const { url: refURL } = (0, utils_js_1.parseRef)(v);
if (refURL) {

@@ -164,3 +183,3 @@ const isRemoteURL = refURL.startsWith("http://") || refURL.startsWith("https://");

return v;
const { url, parts } = (0, utils_1.parseRef)(v);
const { url, parts } = (0, utils_js_1.parseRef)(v);
if (url && new url_1.URL(url).href !== options.rootURL.href) {

@@ -167,0 +186,0 @@ const relativeURL = isFile(new url_1.URL(url)) && isFile(options.rootURL)

@@ -1,2 +0,2 @@

import { GlobalContext, HeaderObject } from "../types";
import type { GlobalContext, HeaderObject } from "../types";
interface TransformHeadersOptions extends GlobalContext {

@@ -3,0 +3,0 @@ required: Set<string>;

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformHeaderObjMap = void 0;
const utils_1 = require("../utils");
const schema_1 = require("./schema");
const utils_js_1 = require("../utils.js");
const schema_js_1 = require("./schema.js");
function transformHeaderObjMap(headerMap, options) {

@@ -13,6 +13,6 @@ let output = "";

if (v.description)
output += (0, utils_1.comment)(v.description);
const readonly = (0, utils_1.tsReadonly)(options.immutableTypes);
output += (0, utils_js_1.comment)(v.description);
const readonly = (0, utils_js_1.tsReadonly)(options.immutableTypes);
const required = v.required ? "" : "?";
output += ` ${readonly}"${k}"${required}: ${(0, schema_1.transformSchemaObj)(v.schema, options)}\n`;
output += ` ${readonly}"${k}"${required}: ${(0, schema_js_1.transformSchemaObj)(v.schema, options)}\n`;
}

@@ -19,0 +19,0 @@ return output;

@@ -1,2 +0,2 @@

import { GlobalContext } from "../types";
import type { GlobalContext } from "../types";
export declare function transformAll(schema: any, ctx: GlobalContext): Record<string, string>;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformAll = void 0;
const utils_1 = require("../utils");
const headers_1 = require("./headers");
const operation_1 = require("./operation");
const paths_1 = require("./paths");
const request_1 = require("./request");
const responses_1 = require("./responses");
const schema_1 = require("./schema");
const utils_js_1 = require("../utils.js");
const headers_js_1 = require("./headers.js");
const operation_js_1 = require("./operation.js");
const paths_js_1 = require("./paths.js");
const request_js_1 = require("./request.js");
const responses_js_1 = require("./responses.js");
const schema_js_1 = require("./schema.js");
function transformAll(schema, ctx) {
const readonly = (0, utils_1.tsReadonly)(ctx.immutableTypes);
const readonly = (0, utils_js_1.tsReadonly)(ctx.immutableTypes);
let output = {};

@@ -19,7 +19,7 @@ let operations = {};

case 2: {
output.definitions = (0, schema_1.transformSchemaObjMap)(schema, { ...ctx, required });
output.definitions = (0, schema_js_1.transformSchemaObjMap)(schema, { ...ctx, required });
return output;
}
case 3: {
output.schemas = (0, schema_1.transformSchemaObjMap)(schema, { ...ctx, required });
output.schemas = (0, schema_js_1.transformSchemaObjMap)(schema, { ...ctx, required });
return output;

@@ -31,3 +31,3 @@ }

if (schema.paths) {
output.paths += (0, paths_1.transformPathsObj)(schema.paths, {
output.paths += (0, paths_js_1.transformPathsObj)(schema.paths, {
...ctx,

@@ -41,3 +41,3 @@ globalParameters: (schema.components && schema.components.parameters) || schema.parameters,

if (schema.definitions) {
output.definitions = (0, schema_1.transformSchemaObjMap)(schema.definitions, {
output.definitions = (0, schema_js_1.transformSchemaObjMap)(schema.definitions, {
...ctx,

@@ -48,3 +48,3 @@ required: new Set(Object.keys(schema.definitions)),

if (schema.parameters) {
output.parameters = (0, schema_1.transformSchemaObjMap)(schema.parameters, {
output.parameters = (0, schema_js_1.transformSchemaObjMap)(schema.parameters, {
...ctx,

@@ -55,3 +55,3 @@ required: new Set(Object.keys(schema.parameters)),

if (schema.responses) {
output.responses = (0, responses_1.transformResponsesObj)(schema.responses, ctx);
output.responses = (0, responses_js_1.transformResponsesObj)(schema.responses, ctx);
}

@@ -64,3 +64,3 @@ break;

if (schema.components.schemas) {
output.components += ` ${readonly}schemas: {\n ${(0, schema_1.transformSchemaObjMap)(schema.components.schemas, {
output.components += ` ${readonly}schemas: {\n ${(0, schema_js_1.transformSchemaObjMap)(schema.components.schemas, {
...ctx,

@@ -71,6 +71,6 @@ required: new Set(Object.keys(schema.components.schemas)),

if (schema.components.responses) {
output.components += ` ${readonly}responses: {\n ${(0, responses_1.transformResponsesObj)(schema.components.responses, ctx)}\n }\n`;
output.components += ` ${readonly}responses: {\n ${(0, responses_js_1.transformResponsesObj)(schema.components.responses, ctx)}\n }\n`;
}
if (schema.components.parameters) {
output.components += ` ${readonly}parameters: {\n ${(0, schema_1.transformSchemaObjMap)(schema.components.parameters, {
output.components += ` ${readonly}parameters: {\n ${(0, schema_js_1.transformSchemaObjMap)(schema.components.parameters, {
...ctx,

@@ -81,6 +81,6 @@ required: new Set(Object.keys(schema.components.parameters)),

if (schema.components.requestBodies) {
output.components += ` ${readonly}requestBodies: {\n ${(0, request_1.transformRequestBodies)(schema.components.requestBodies, ctx)}\n }\n`;
output.components += ` ${readonly}requestBodies: {\n ${(0, request_js_1.transformRequestBodies)(schema.components.requestBodies, ctx)}\n }\n`;
}
if (schema.components.headers) {
output.components += ` ${readonly}headers: {\n ${(0, headers_1.transformHeaderObjMap)(schema.components.headers, {
output.components += ` ${readonly}headers: {\n ${(0, headers_js_1.transformHeaderObjMap)(schema.components.headers, {
...ctx,

@@ -99,4 +99,4 @@ required: new Set(),

if (operation.description)
output.operations += (0, utils_1.comment)(operation.description);
output.operations += ` ${readonly}"${id}": {\n ${(0, operation_1.transformOperationObj)(operation, {
output.operations += (0, utils_js_1.comment)(operation.description);
output.operations += ` ${readonly}"${id}": {\n ${(0, operation_js_1.transformOperationObj)(operation, {
...ctx,

@@ -103,0 +103,0 @@ pathItem,

@@ -1,2 +0,2 @@

import { GlobalContext, OperationObject, ParameterObject, PathItemObject } from "../types";
import type { GlobalContext, OperationObject, ParameterObject, PathItemObject } from "../types";
interface TransformOperationOptions extends GlobalContext {

@@ -3,0 +3,0 @@ globalParameters?: Record<string, ParameterObject>;

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformOperationObj = void 0;
const utils_1 = require("../utils");
const parameters_1 = require("./parameters");
const request_1 = require("./request");
const responses_1 = require("./responses");
const utils_js_1 = require("../utils.js");
const parameters_js_1 = require("./parameters.js");
const request_js_1 = require("./request.js");
const responses_js_1 = require("./responses.js");
function transformOperationObj(operation, options) {
const { pathItem = {}, globalParameters, ...ctx } = options;
const readonly = (0, utils_1.tsReadonly)(ctx.immutableTypes);
const readonly = (0, utils_js_1.tsReadonly)(ctx.immutableTypes);
let output = "";
if (operation.parameters || pathItem.parameters) {
const parameters = (pathItem.parameters || []).concat(operation.parameters || []);
output += ` ${readonly}parameters: {\n ${(0, parameters_1.transformParametersArray)(parameters, {
output += ` ${readonly}parameters: {\n ${(0, parameters_js_1.transformParametersArray)(parameters, {
...ctx,

@@ -20,6 +20,6 @@ globalParameters,

if (operation.responses) {
output += ` ${readonly}responses: {\n ${(0, responses_1.transformResponsesObj)(operation.responses, ctx)}\n }\n`;
output += ` ${readonly}responses: {\n ${(0, responses_js_1.transformResponsesObj)(operation.responses, ctx)}\n }\n`;
}
if (operation.requestBody) {
if ((0, utils_1.isRef)(operation.requestBody)) {
if ((0, utils_js_1.isRef)(operation.requestBody)) {
output += ` ${readonly}requestBody: ${operation.requestBody.$ref};\n`;

@@ -29,4 +29,4 @@ }

if (operation.requestBody.description)
output += (0, utils_1.comment)(operation.requestBody.description);
output += ` ${readonly}requestBody: {\n ${(0, request_1.transformRequestBodyObj)(operation.requestBody, ctx)} }\n`;
output += (0, utils_js_1.comment)(operation.requestBody.description);
output += ` ${readonly}requestBody: {\n ${(0, request_js_1.transformRequestBodyObj)(operation.requestBody, ctx)} }\n`;
}

@@ -33,0 +33,0 @@ }

@@ -1,2 +0,2 @@

import { GlobalContext, ParameterObject, ReferenceObject } from "../types";
import type { GlobalContext, ParameterObject, ReferenceObject } from "../types";
interface TransformParametersOptions extends GlobalContext {

@@ -3,0 +3,0 @@ globalParameters?: Record<string, ParameterObject>;

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformParametersArray = void 0;
const utils_1 = require("../utils");
const schema_1 = require("./schema");
const utils_js_1 = require("../utils.js");
const schema_js_1 = require("./schema.js");
function transformParametersArray(parameters, options) {
const { globalParameters = {}, ...ctx } = options;
const readonly = (0, utils_1.tsReadonly)(ctx.immutableTypes);
const readonly = (0, utils_js_1.tsReadonly)(ctx.immutableTypes);
let output = "";

@@ -52,3 +52,3 @@ let mappedParams = {};

if (paramComment)
output += (0, utils_1.comment)(paramComment);
output += (0, utils_js_1.comment)(paramComment);
const required = paramObj.required ? `` : `?`;

@@ -59,3 +59,3 @@ let paramType = ``;

paramType = paramObj.schema
? (0, schema_1.transformSchemaObj)(paramObj.schema, { ...ctx, required: new Set() })
? (0, schema_js_1.transformSchemaObj)(paramObj.schema, { ...ctx, required: new Set() })
: "unknown";

@@ -66,6 +66,6 @@ break;

if (paramObj.in === "body" && paramObj.schema) {
paramType = (0, schema_1.transformSchemaObj)(paramObj.schema, { ...ctx, required: new Set() });
paramType = (0, schema_js_1.transformSchemaObj)(paramObj.schema, { ...ctx, required: new Set() });
}
else if (paramObj.type) {
paramType = (0, schema_1.transformSchemaObj)(paramObj, { ...ctx, required: new Set() });
paramType = (0, schema_js_1.transformSchemaObj)(paramObj, { ...ctx, required: new Set() });
}

@@ -72,0 +72,0 @@ else {

@@ -1,2 +0,2 @@

import { GlobalContext, OperationObject, ParameterObject, PathItemObject } from "../types";
import type { GlobalContext, OperationObject, ParameterObject, PathItemObject } from "../types";
interface TransformPathsObjOption extends GlobalContext {

@@ -3,0 +3,0 @@ globalParameters: Record<string, ParameterObject>;

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformPathsObj = void 0;
const utils_1 = require("../utils");
const operation_1 = require("./operation");
const parameters_1 = require("./parameters");
const utils_js_1 = require("../utils.js");
const operation_js_1 = require("./operation.js");
const parameters_js_1 = require("./parameters.js");
function transformPathsObj(paths, options) {
const { globalParameters, operations, ...ctx } = options;
const readonly = (0, utils_1.tsReadonly)(ctx.immutableTypes);
const readonly = (0, utils_js_1.tsReadonly)(ctx.immutableTypes);
let output = "";
for (const [url, pathItem] of Object.entries(paths)) {
if (pathItem.description)
output += (0, utils_1.comment)(pathItem.description);
output += (0, utils_js_1.comment)(pathItem.description);
if (pathItem.$ref) {

@@ -24,3 +24,3 @@ output += ` ${readonly}"${url}": ${pathItem.$ref};\n`;

if (operation.description)
output += (0, utils_1.comment)(operation.description);
output += (0, utils_js_1.comment)(operation.description);
if (operation.operationId) {

@@ -32,3 +32,3 @@ operations[operation.operationId] = { operation, pathItem };

else {
output += ` ${readonly}"${method}": {\n ${(0, operation_1.transformOperationObj)(operation, {
output += ` ${readonly}"${method}": {\n ${(0, operation_js_1.transformOperationObj)(operation, {
...ctx,

@@ -41,3 +41,3 @@ globalParameters,

if (pathItem.parameters) {
output += ` ${readonly}parameters: {\n ${(0, parameters_1.transformParametersArray)(pathItem.parameters, {
output += ` ${readonly}parameters: {\n ${(0, parameters_js_1.transformParametersArray)(pathItem.parameters, {
...ctx,

@@ -44,0 +44,0 @@ globalParameters,

@@ -1,3 +0,3 @@

import { GlobalContext, RequestBody } from "../types";
import type { GlobalContext, RequestBody } from "../types";
export declare function transformRequestBodies(requestBodies: Record<string, RequestBody>, ctx: GlobalContext): string;
export declare function transformRequestBodyObj(requestBody: RequestBody, ctx: GlobalContext): string;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformRequestBodyObj = exports.transformRequestBodies = void 0;
const utils_1 = require("../utils");
const schema_1 = require("./schema");
const utils_js_1 = require("../utils.js");
const schema_js_1 = require("./schema.js");
function transformRequestBodies(requestBodies, ctx) {

@@ -10,3 +10,3 @@ let output = "";

if (requestBody && requestBody.description)
output += ` ${(0, utils_1.comment)(requestBody.description)}`;
output += ` ${(0, utils_js_1.comment)(requestBody.description)}`;
output += ` "${name}": {\n ${transformRequestBodyObj(requestBody, ctx)}\n }\n`;

@@ -18,3 +18,3 @@ }

function transformRequestBodyObj(requestBody, ctx) {
const readonly = (0, utils_1.tsReadonly)(ctx.immutableTypes);
const readonly = (0, utils_js_1.tsReadonly)(ctx.immutableTypes);
let output = "";

@@ -24,3 +24,3 @@ if (requestBody.content && Object.keys(requestBody.content).length) {

for (const [k, v] of Object.entries(requestBody.content)) {
output += ` ${readonly}"${k}": ${(0, schema_1.transformSchemaObj)(v.schema, { ...ctx, required: new Set() })};\n`;
output += ` ${readonly}"${k}": ${(0, schema_js_1.transformSchemaObj)(v.schema, { ...ctx, required: new Set() })};\n`;
}

@@ -27,0 +27,0 @@ output += ` }\n`;

@@ -1,2 +0,2 @@

import { GlobalContext } from "../types";
import type { GlobalContext } from "../types";
export declare function transformResponsesObj(responsesObj: Record<string, any>, ctx: GlobalContext): string;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformResponsesObj = void 0;
const utils_1 = require("../utils");
const headers_1 = require("./headers");
const schema_1 = require("./schema");
const utils_js_1 = require("../utils.js");
const headers_js_1 = require("./headers.js");
const schema_js_1 = require("./schema.js");
const resType = (res) => (res === 204 || (res >= 300 && res < 400) ? "never" : "unknown");
function transformResponsesObj(responsesObj, ctx) {
const readonly = (0, utils_1.tsReadonly)(ctx.immutableTypes);
const readonly = (0, utils_js_1.tsReadonly)(ctx.immutableTypes);
let output = "";

@@ -15,3 +15,3 @@ for (const httpStatusCode of Object.keys(responsesObj)) {

if (response.description)
output += (0, utils_1.comment)(response.description);
output += (0, utils_js_1.comment)(response.description);
if (response.$ref) {

@@ -31,3 +31,3 @@ output += ` ${readonly}${statusCode}: ${response.$ref};\n`;

else {
output += ` ${readonly}headers: {\n ${(0, headers_1.transformHeaderObjMap)(response.headers, {
output += ` ${readonly}headers: {\n ${(0, headers_js_1.transformHeaderObjMap)(response.headers, {
...ctx,

@@ -44,3 +44,3 @@ required: new Set(),

const responseType = contentResponse && (contentResponse === null || contentResponse === void 0 ? void 0 : contentResponse.schema)
? (0, schema_1.transformSchemaObj)(contentResponse.schema, { ...ctx, required: new Set() })
? (0, schema_js_1.transformSchemaObj)(contentResponse.schema, { ...ctx, required: new Set() })
: "unknown";

@@ -53,3 +53,3 @@ output += ` ${readonly}"${contentType}": ${responseType};\n`;

case 2: {
output += ` ${readonly} schema: ${(0, schema_1.transformSchemaObj)(response.schema, {
output += ` ${readonly} schema: ${(0, schema_js_1.transformSchemaObj)(response.schema, {
...ctx,

@@ -56,0 +56,0 @@ required: new Set(),

@@ -1,2 +0,2 @@

import { GlobalContext } from "../types";
import type { GlobalContext } from "../types";
interface TransformSchemaObjOptions extends GlobalContext {

@@ -3,0 +3,0 @@ required: Set<string>;

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformSchemaObj = exports.transformOneOf = exports.transformAnyOf = exports.addRequiredProps = exports.transformSchemaObjMap = void 0;
const utils_1 = require("../utils");
const utils_js_1 = require("../utils.js");
function hasDefaultValue(node) {

@@ -14,10 +14,6 @@ if (node.hasOwnProperty("default"))

const v = obj[k];
let schemaComment = "";
if (v.deprecated)
schemaComment += `@deprecated `;
if (v.description)
schemaComment += v.description;
if (schemaComment)
output += (0, utils_1.comment)(schemaComment);
const readonly = (0, utils_1.tsReadonly)(options.immutableTypes);
const comment = (0, utils_js_1.prepareComment)(v);
if (comment)
output += comment;
const readonly = (0, utils_js_1.tsReadonly)(options.immutableTypes);
const required = options.required.has(k) || (options.defaultNonNullable && hasDefaultValue(v.schema || v)) ? "" : "?";

@@ -54,7 +50,7 @@ output += `${readonly}"${k}"${required}: `;

}
return (0, utils_1.tsIntersectionOf)(schemas.map((s) => (0, utils_1.tsPartial)(transformSchemaObj(s, options))));
return (0, utils_js_1.tsIntersectionOf)(schemas.map((s) => (0, utils_js_1.tsPartial)(transformSchemaObj(s, options))));
}
exports.transformAnyOf = transformAnyOf;
function transformOneOf(oneOf, options) {
return (0, utils_1.tsUnionOf)(oneOf.map((value) => transformSchemaObj(value, options)));
return (0, utils_js_1.tsUnionOf)(oneOf.map((value) => transformSchemaObj(value, options)));
}

@@ -64,3 +60,3 @@ exports.transformOneOf = transformOneOf;

var _a;
const readonly = (0, utils_1.tsReadonly)(options.immutableTypes);
const readonly = (0, utils_js_1.tsReadonly)(options.immutableTypes);
let output = "";

@@ -75,3 +71,3 @@ const overriddenType = options.formatter && options.formatter(node);

else {
switch ((0, utils_1.nodeType)(node)) {
switch ((0, utils_js_1.nodeType)(node)) {
case "ref": {

@@ -85,3 +81,3 @@ output += node.$ref;

case "unknown": {
output += (0, utils_1.nodeType)(node);
output += (0, utils_js_1.nodeType)(node);
break;

@@ -99,3 +95,3 @@ }

});
output += (0, utils_1.tsUnionOf)(items);
output += (0, utils_js_1.tsUnionOf)(items);
break;

@@ -110,3 +106,3 @@ }

const emptyObj = `{ ${readonly}[key: string]: unknown }`;
output += (0, utils_1.tsIntersectionOf)([emptyObj, ...missingRequired]);
output += (0, utils_js_1.tsIntersectionOf)([emptyObj, ...missingRequired]);
break;

@@ -138,3 +134,3 @@ }

}
output += (0, utils_1.tsIntersectionOf)([
output += (0, utils_js_1.tsIntersectionOf)([
...(node.allOf ? node.allOf.map((node) => transformSchemaObj(node, options)) : []),

@@ -151,6 +147,6 @@ ...(node.anyOf ? [transformAnyOf(node.anyOf, options)] : []),

if (Array.isArray(node.items)) {
output += `${readonly}${(0, utils_1.tsTupleOf)(node.items.map((node) => transformSchemaObj(node, options)))}`;
output += `${readonly}${(0, utils_js_1.tsTupleOf)(node.items.map((node) => transformSchemaObj(node, options)))}`;
}
else {
output += `${readonly}${(0, utils_1.tsArrayOf)(node.items ? transformSchemaObj(node.items, options) : "unknown")}`;
output += `${readonly}${(0, utils_js_1.tsArrayOf)(node.items ? transformSchemaObj(node.items, options) : "unknown")}`;
}

@@ -157,0 +153,0 @@ break;

import { OpenAPI2, OpenAPI3, ReferenceObject } from "./types";
declare type CommentObject = {
title?: string;
format?: string;
deprecated?: boolean;
description?: string;
default?: string;
example?: string;
};
export declare function prepareComment(v: CommentObject): string | void;
export declare function comment(text: string): string;

@@ -3,0 +12,0 @@ export declare function parseRef(ref: string): {

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.tsUnionOf = exports.tsReadonly = exports.tsPartial = exports.tsIntersectionOf = exports.tsTupleOf = exports.tsArrayOf = exports.encodeRef = exports.decodeRef = exports.swaggerVersion = exports.nodeType = exports.isRef = exports.parseRef = exports.comment = void 0;
exports.tsUnionOf = exports.tsReadonly = exports.tsPartial = exports.tsIntersectionOf = exports.tsTupleOf = exports.tsArrayOf = exports.encodeRef = exports.decodeRef = exports.swaggerVersion = exports.nodeType = exports.isRef = exports.parseRef = exports.comment = exports.prepareComment = void 0;
function prepareComment(v) {
const commentsArray = [];
if (v.title)
commentsArray.push(`${v.title} `);
if (v.format)
commentsArray.push(`Format: ${v.format} `);
if (v.deprecated)
commentsArray.push(`@deprecated `);
const supportedJsDocTags = ["description", "default", "example"];
for (let index = 0; index < supportedJsDocTags.length; index++) {
const field = supportedJsDocTags[index];
if (v[field])
commentsArray.push(`@${field} ${v[field]} `);
}
if (!commentsArray.length)
return;
return comment(commentsArray.join("\n"));
}
exports.prepareComment = prepareComment;
function comment(text) {

@@ -5,0 +24,0 @@ const commentText = text.trim().replace(/\*\//g, "*\\/");

@@ -1,5 +0,5 @@

import { OpenAPI2, OpenAPI3, SchemaObject, SwaggerToTSOptions } from "./types";
export * from "./types";
import type { OpenAPI2, OpenAPI3, SchemaObject, SwaggerToTSOptions } from "./types";
export * from "./types.js";
export declare const WARNING_MESSAGE = "/**\n* This file was auto-generated by openapi-typescript.\n* Do not make direct changes to the file.\n*/\n\n\n";
declare function openapiTS(schema: string | OpenAPI2 | OpenAPI3 | Record<string, SchemaObject>, options?: SwaggerToTSOptions): Promise<string>;
export default openapiTS;
/// <reference types="node" />
import type { GlobalContext, Headers } from "./types";
import { URL } from "url";
import { GlobalContext, Headers } from "./types";
declare type PartialSchema = Record<string, any>;

@@ -5,0 +5,0 @@ declare type SchemaMap = {

@@ -1,2 +0,2 @@

import { GlobalContext, HeaderObject } from "../types";
import type { GlobalContext, HeaderObject } from "../types";
interface TransformHeadersOptions extends GlobalContext {

@@ -3,0 +3,0 @@ required: Set<string>;

@@ -1,2 +0,2 @@

import { GlobalContext } from "../types";
import type { GlobalContext } from "../types";
export declare function transformAll(schema: any, ctx: GlobalContext): Record<string, string>;

@@ -1,2 +0,2 @@

import { GlobalContext, OperationObject, ParameterObject, PathItemObject } from "../types";
import type { GlobalContext, OperationObject, ParameterObject, PathItemObject } from "../types";
interface TransformOperationOptions extends GlobalContext {

@@ -3,0 +3,0 @@ globalParameters?: Record<string, ParameterObject>;

@@ -1,2 +0,2 @@

import { GlobalContext, ParameterObject, ReferenceObject } from "../types";
import type { GlobalContext, ParameterObject, ReferenceObject } from "../types";
interface TransformParametersOptions extends GlobalContext {

@@ -3,0 +3,0 @@ globalParameters?: Record<string, ParameterObject>;

@@ -1,2 +0,2 @@

import { GlobalContext, OperationObject, ParameterObject, PathItemObject } from "../types";
import type { GlobalContext, OperationObject, ParameterObject, PathItemObject } from "../types";
interface TransformPathsObjOption extends GlobalContext {

@@ -3,0 +3,0 @@ globalParameters: Record<string, ParameterObject>;

@@ -1,3 +0,3 @@

import { GlobalContext, RequestBody } from "../types";
import type { GlobalContext, RequestBody } from "../types";
export declare function transformRequestBodies(requestBodies: Record<string, RequestBody>, ctx: GlobalContext): string;
export declare function transformRequestBodyObj(requestBody: RequestBody, ctx: GlobalContext): string;

@@ -1,2 +0,2 @@

import { GlobalContext } from "../types";
import type { GlobalContext } from "../types";
export declare function transformResponsesObj(responsesObj: Record<string, any>, ctx: GlobalContext): string;

@@ -1,2 +0,2 @@

import { GlobalContext } from "../types";
import type { GlobalContext } from "../types";
interface TransformSchemaObjOptions extends GlobalContext {

@@ -3,0 +3,0 @@ required: Set<string>;

import { OpenAPI2, OpenAPI3, ReferenceObject } from "./types";
declare type CommentObject = {
title?: string;
format?: string;
deprecated?: boolean;
description?: string;
default?: string;
example?: string;
};
export declare function prepareComment(v: CommentObject): string | void;
export declare function comment(text: string): string;

@@ -3,0 +12,0 @@ export declare function parseRef(ref: string): {

{
"name": "openapi-typescript",
"description": "Generate TypeScript types from Swagger OpenAPI specs",
"version": "4.4.0",
"version": "4.5.0",
"engines": {

@@ -10,3 +10,3 @@ "node": ">= 12.0.0",

"author": "drew@pow.rs",
"license": "ISC",
"license": "MIT",
"bin": {

@@ -18,5 +18,8 @@ "openapi-typescript": "bin/cli.js"

"exports": {
"browser": "./dist/esm/index.js",
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js"
".": {
"browser": "./dist/esm/index.mjs",
"import": "./dist/esm/index.mjs",
"require": "./dist/cjs/index.js"
},
"./*": "./*"
},

@@ -49,3 +52,5 @@ "files": [

"scripts": {
"build": "rm -rf dist && tsc --project tsconfig.esm.json && tsc --project tsconfig.cjs.json",
"build": "rm -rf dist && npm run build:esm && npm run build:cjs",
"build:cjs": "tsc --project tsconfig.cjs.json",
"build:esm": "tsc --project tsconfig.esm.json && renamer --find .js --replace .mjs \"dist/esm/**/*\"",
"format": "npm run prettier -w .",

@@ -55,7 +60,4 @@ "lint": "eslint .",

"pregenerate": "npm run build",
"test": "npm run build && jest --testTimeout=30000",
"test:watch": "npm run build && jest --no-cache --watch",
"test:update": "npm run build && jest -u",
"test:coverage": "npm run build && jest --no-cache --coverage && codecov",
"test:coverage:local": "npm run build && jest --no-cache --collectCoverage",
"test": "mocha --recursive --parallel --timeout 30000",
"test:coverage": "nyc --reporter=lcov mocha --recursive --parallel --timeout 30000 && codecov",
"typecheck": "tsc --noEmit --project tsconfig.esm.json",

@@ -69,5 +71,5 @@ "version": "npm run build"

"meow": "^9.0.0",
"mime": "^2.5.2",
"node-fetch": "^2.6.5",
"prettier": "^2.4.1",
"mime": "^3.0.0",
"node-fetch": "^2.6.6",
"prettier": "^2.5.1",
"slash": "^3.0.0",

@@ -77,16 +79,19 @@ "tiny-glob": "^0.2.9"

"devDependencies": {
"@types/jest": "^27.0.2",
"@types/js-yaml": "^4.0.1",
"@types/mime": "^2.0.3",
"@types/node-fetch": "^2.5.12",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"codecov": "^3.8.2",
"eslint": "^8.0.1",
"@types/prettier": "^2.4.2",
"@typescript-eslint/eslint-plugin": "^5.8.0",
"@typescript-eslint/parser": "^5.8.0",
"chai": "^4.3.4",
"codecov": "^3.8.3",
"eol": "^0.9.1",
"eslint": "^8.5.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"jest": "^27.2.5",
"ts-jest": "^27.0.5",
"typescript": "^4.4.3"
"mocha": "^9.1.3",
"nyc": "^15.1.0",
"renamer": "^4.0.0",
"typescript": "^4.5.4"
}
}

@@ -6,3 +6,3 @@ [![version(scoped)](https://img.shields.io/npm/v/openapi-typescript.svg)](https://www.npmjs.com/package/openapi-typescript)

<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-43-orange.svg?style=flat-square)](#contributors-)
[![All Contributors](https://img.shields.io/badge/all_contributors-46-orange.svg?style=flat-square)](#contributors-)
<!-- ALL-CONTRIBUTORS-BADGE:END -->

@@ -92,2 +92,51 @@

#### openapi-typescript-fetch
The generated spec can also be used with [openapi-typescript-fetch](https://www.npmjs.com/package/openapi-typescript-fetch) which implements a typed fetch client for openapi-typescript.
```ts
import { paths } from './petstore'
import { Fetcher } from 'openapi-typescript-fetch'
// declare fetcher for paths
const fetcher = Fetcher.for<paths>()
// global configuration
fetcher.configure({
baseUrl: 'https://petstore.swagger.io/v2',
init: {
headers: {
...
},
},
use: [...] // middlewares
})
// create fetch operations
const findPetsByStatus = fetcher.path('/pet/findByStatus').method('get').create()
const addPet = fetcher.path('/pet').method('post').create()
// fetch
try {
const { status, data: pets } = await findPetsByStatus({
status: ['available', 'pending'],
})
await addPet({ ... })
} catch(e) {
// check which operation threw the exception
if (e instanceof addPet.Error) {
// get discriminated union { status, data }
const error = e.getActualType()
if (error.status === 400) {
error.data.validationErrors // 400 response data
} else if (error.status === 500) {
error.data.errorMessage // 500 response data
} else {
...
}
}
}
```
#### Outputting to stdout

@@ -258,2 +307,5 @@

<td align="center"><a href="https://github.com/bunkscene"><img src="https://avatars.githubusercontent.com/u/2693678?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Dave Carlson</b></sub></a><br /><a href="https://github.com/drwpow/openapi-typescript/commits?author=bunkscene" title="Code">💻</a></td>
<td align="center"><a href="https://ottomated.net"><img src="https://avatars.githubusercontent.com/u/31470743?v=4?s=100" width="100px;" alt=""/><br /><sub><b>ottomated</b></sub></a><br /><a href="https://github.com/drwpow/openapi-typescript/commits?author=ottomated" title="Code">💻</a> <a href="https://github.com/drwpow/openapi-typescript/issues?q=author%3Aottomated" title="Bug reports">🐛</a></td>
<td align="center"><a href="https://karanarqq.dev"><img src="https://avatars.githubusercontent.com/u/28733669?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Artem Shuvaev</b></sub></a><br /><a href="https://github.com/drwpow/openapi-typescript/commits?author=sadfsdfdsa" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/ajaishankar"><img src="https://avatars.githubusercontent.com/u/328008?v=4?s=100" width="100px;" alt=""/><br /><sub><b>ajaishankar</b></sub></a><br /><a href="https://github.com/drwpow/openapi-typescript/commits?author=ajaishankar" title="Documentation">📖</a></td>
</tr>

@@ -260,0 +312,0 @@ </table>

@@ -0,11 +1,11 @@

import type { GlobalContext, OpenAPI2, OpenAPI3, SchemaObject, SwaggerToTSOptions } from "./types";
import path from "path";
import { bold, yellow } from "kleur";
import * as color from "kleur/colors";
import prettier from "prettier";
import parserTypescript from "prettier/parser-typescript";
import parserTypescript from "prettier/parser-typescript.js";
import { URL } from "url";
import load, { resolveSchema, VIRTUAL_JSON_URL } from "./load";
import { swaggerVersion } from "./utils";
import { transformAll } from "./transform/index";
import { GlobalContext, OpenAPI2, OpenAPI3, SchemaObject, SwaggerToTSOptions } from "./types";
export * from "./types"; // expose all types to consumers
import load, { resolveSchema, VIRTUAL_JSON_URL } from "./load.js";
import { swaggerVersion } from "./utils.js";
import { transformAll } from "./transform/index.js";
export * from "./types.js"; // expose all types to consumers

@@ -55,3 +55,3 @@ export const WARNING_MESSAGE = `/**

const schemaURL = resolveSchema(schema);
if (options.silent === false) console.log(yellow(`🔭 Loading spec from ${bold(schemaURL.href)}…`));
if (options.silent === false) console.log(color.yellow(`🔭 Loading spec from ${color.bold(schemaURL.href)}…`));

@@ -58,0 +58,0 @@ await load(schemaURL, {

@@ -0,1 +1,2 @@

import type { GlobalContext, Headers } from "./types";
import fetch from "node-fetch";

@@ -8,5 +9,4 @@ import fs from "fs";

import yaml from "js-yaml";
import { red } from "kleur";
import { GlobalContext, Headers } from "./types";
import { parseRef } from "./utils";
import * as color from "kleur/colors";
import { parseRef } from "./utils.js";

@@ -80,3 +80,3 @@ type PartialSchema = Record<string, any>; // not a very accurate type, but this is easier to deal with before we know we’re dealing with a valid spec

console.error(
red(`Cannot parse key: ${k} into JSON format. Continuing with the next HTTP header that is specified`)
color.red(`Cannot parse key: ${k} into JSON format. Continuing with the next HTTP header that is specified`)
);

@@ -125,3 +125,3 @@ }

// load local
contents = await fs.promises.readFile(schemaURL, "utf8");
contents = fs.readFileSync(schemaURL, "utf8");
contentType = mime.getType(schemaID) || "";

@@ -133,3 +133,3 @@ } else {

};
if (options.auth) headers.Authorizaton = options.auth;
if (options.auth) headers.Authorization = options.auth;

@@ -136,0 +136,0 @@ // Add custom parsed HTTP headers

@@ -1,4 +0,4 @@

import { GlobalContext, HeaderObject } from "../types";
import { comment, tsReadonly } from "../utils";
import { transformSchemaObj } from "./schema";
import type { GlobalContext, HeaderObject } from "../types";
import { comment, tsReadonly } from "../utils.js";
import { transformSchemaObj } from "./schema.js";

@@ -5,0 +5,0 @@ interface TransformHeadersOptions extends GlobalContext {

@@ -1,9 +0,9 @@

import { GlobalContext, OperationObject, PathItemObject } from "../types";
import { comment, tsReadonly } from "../utils";
import { transformHeaderObjMap } from "./headers";
import { transformOperationObj } from "./operation";
import { transformPathsObj } from "./paths";
import { transformRequestBodies } from "./request";
import { transformResponsesObj } from "./responses";
import { transformSchemaObjMap } from "./schema";
import type { GlobalContext, OperationObject, PathItemObject } from "../types";
import { comment, tsReadonly } from "../utils.js";
import { transformHeaderObjMap } from "./headers.js";
import { transformOperationObj } from "./operation.js";
import { transformPathsObj } from "./paths.js";
import { transformRequestBodies } from "./request.js";
import { transformResponsesObj } from "./responses.js";
import { transformSchemaObjMap } from "./schema.js";

@@ -10,0 +10,0 @@ export function transformAll(schema: any, ctx: GlobalContext): Record<string, string> {

@@ -1,6 +0,6 @@

import { GlobalContext, OperationObject, ParameterObject, PathItemObject } from "../types";
import { comment, isRef, tsReadonly } from "../utils";
import { transformParametersArray } from "./parameters";
import { transformRequestBodyObj } from "./request";
import { transformResponsesObj } from "./responses";
import type { GlobalContext, OperationObject, ParameterObject, PathItemObject } from "../types";
import { comment, isRef, tsReadonly } from "../utils.js";
import { transformParametersArray } from "./parameters.js";
import { transformRequestBodyObj } from "./request.js";
import { transformResponsesObj } from "./responses.js";

@@ -7,0 +7,0 @@ interface TransformOperationOptions extends GlobalContext {

@@ -1,4 +0,4 @@

import { GlobalContext, ParameterObject, ReferenceObject } from "../types";
import { comment, tsReadonly } from "../utils";
import { transformSchemaObj } from "./schema";
import type { GlobalContext, ParameterObject, ReferenceObject } from "../types";
import { comment, tsReadonly } from "../utils.js";
import { transformSchemaObj } from "./schema.js";

@@ -5,0 +5,0 @@ interface TransformParametersOptions extends GlobalContext {

@@ -1,5 +0,5 @@

import { GlobalContext, OperationObject, ParameterObject, PathItemObject } from "../types";
import { comment, tsReadonly } from "../utils";
import { transformOperationObj } from "./operation";
import { transformParametersArray } from "./parameters";
import type { GlobalContext, OperationObject, ParameterObject, PathItemObject } from "../types";
import { comment, tsReadonly } from "../utils.js";
import { transformOperationObj } from "./operation.js";
import { transformParametersArray } from "./parameters.js";

@@ -6,0 +6,0 @@ interface TransformPathsObjOption extends GlobalContext {

@@ -1,4 +0,4 @@

import { GlobalContext, RequestBody } from "../types";
import { comment, tsReadonly } from "../utils";
import { transformSchemaObj } from "./schema";
import type { GlobalContext, RequestBody } from "../types";
import { comment, tsReadonly } from "../utils.js";
import { transformSchemaObj } from "./schema.js";

@@ -5,0 +5,0 @@ export function transformRequestBodies(requestBodies: Record<string, RequestBody>, ctx: GlobalContext) {

@@ -1,5 +0,5 @@

import { GlobalContext } from "../types";
import { comment, tsReadonly } from "../utils";
import { transformHeaderObjMap } from "./headers";
import { transformSchemaObj } from "./schema";
import type { GlobalContext } from "../types";
import { comment, tsReadonly } from "../utils.js";
import { transformHeaderObjMap } from "./headers.js";
import { transformSchemaObj } from "./schema.js";

@@ -6,0 +6,0 @@ const resType = (res: string | number) => (res === 204 || (res >= 300 && res < 400) ? "never" : "unknown");

@@ -1,3 +0,12 @@

import { GlobalContext } from "../types";
import { comment, nodeType, tsArrayOf, tsIntersectionOf, tsPartial, tsReadonly, tsTupleOf, tsUnionOf } from "../utils";
import type { GlobalContext } from "../types";
import {
prepareComment,
nodeType,
tsArrayOf,
tsIntersectionOf,
tsPartial,
tsReadonly,
tsTupleOf,
tsUnionOf,
} from "../utils.js";

@@ -21,7 +30,5 @@ interface TransformSchemaObjOptions extends GlobalContext {

// 1. JSDoc comment (goes above property)
let schemaComment = "";
if (v.deprecated) schemaComment += `@deprecated `;
if (v.description) schemaComment += v.description;
if (schemaComment) output += comment(schemaComment);
// 1. Add comment in jsdoc notation
const comment = prepareComment(v);
if (comment) output += comment;

@@ -28,0 +35,0 @@ // 2. name (with “?” if optional property)

import { OpenAPI2, OpenAPI3, ReferenceObject } from "./types";
type CommentObject = {
title?: string; // not jsdoc
format?: string; // not jsdoc
deprecated?: boolean; // jsdoc without value
description?: string; // jsdoc with value
default?: string; // jsdoc with value
example?: string; // jsdoc with value
};
/**
* Preparing comments from fields
* @see {comment} for output examples
* @returns void if not comments or jsdoc format comment string
*/
export function prepareComment(v: CommentObject): string | void {
const commentsArray: Array<string> = [];
// * Not JSDOC tags: [title, format]
if (v.title) commentsArray.push(`${v.title} `);
if (v.format) commentsArray.push(`Format: ${v.format} `);
// * JSDOC tags without value
// 'Deprecated' without value
if (v.deprecated) commentsArray.push(`@deprecated `);
// * JSDOC tags with value
const supportedJsDocTags: Array<keyof CommentObject> = ["description", "default", "example"];
for (let index = 0; index < supportedJsDocTags.length; index++) {
const field = supportedJsDocTags[index];
if (v[field]) commentsArray.push(`@${field} ${v[field]} `);
}
if (!commentsArray.length) return;
return comment(commentsArray.join("\n"));
}
export function comment(text: string): string {

@@ -4,0 +41,0 @@ const commentText = text.trim().replace(/\*\//g, "*\\/");

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc