oas-normalize 13.0.1 → 13.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -26,6 +26,7 @@ var OASNormalize = class _OASNormalize {
26
26
  this.opts = {
27
27
  colorizeErrors: false,
28
28
  enablePaths: false,
29
+ parser: {},
29
30
  ...opts
30
31
  };
31
32
  this.type = _chunkZTTIELJHcjs.getType.call(void 0, this.file);
@@ -81,12 +82,13 @@ var OASNormalize = class _OASNormalize {
81
82
  */
82
83
  async bundle() {
83
84
  if (this.cache.bundle) return this.cache.bundle;
85
+ const parserOptions = this.opts.parser || {};
84
86
  return this.load().then((schema) => {
85
87
  if (_chunkZTTIELJHcjs.isPostman.call(void 0, schema)) {
86
88
  return _OASNormalize.convertPostmanToOpenAPI(schema);
87
89
  }
88
90
  return schema;
89
- }).then((schema) => _openapiparser.bundle.call(void 0, schema)).then((bundled) => {
91
+ }).then((schema) => _openapiparser.bundle.call(void 0, schema, parserOptions)).then((bundled) => {
90
92
  this.cache.bundle = bundled;
91
93
  return bundled;
92
94
  });
@@ -95,18 +97,29 @@ var OASNormalize = class _OASNormalize {
95
97
  * Dereference the given API definition.
96
98
  *
97
99
  */
98
- async deref() {
100
+ async dereference() {
99
101
  if (this.cache.deref) return this.cache.deref;
102
+ const parserOptions = this.opts.parser || {};
100
103
  return this.load().then((schema) => {
101
104
  if (_chunkZTTIELJHcjs.isPostman.call(void 0, schema)) {
102
105
  return _OASNormalize.convertPostmanToOpenAPI(schema);
103
106
  }
104
107
  return schema;
105
- }).then((schema) => _openapiparser.dereference.call(void 0, schema)).then((dereferenced) => {
108
+ }).then((schema) => _openapiparser.dereference.call(void 0, schema, parserOptions)).then((dereferenced) => {
106
109
  this.cache.deref = dereferenced;
107
110
  return dereferenced;
108
111
  });
109
112
  }
113
+ /**
114
+ * Dereference the given API definition.
115
+ *
116
+ * This method is deprecated in favor of `dereference`. It will be removed in a future release.
117
+ *
118
+ * @deprecated
119
+ */
120
+ async deref() {
121
+ return this.dereference();
122
+ }
110
123
  /**
111
124
  * Convert a given API definition to OpenAPI if it is not already.
112
125
  *
@@ -136,7 +149,7 @@ var OASNormalize = class _OASNormalize {
136
149
  *
137
150
  */
138
151
  async validate(opts = {}) {
139
- const parserOptions = opts.parser || {};
152
+ const parserOptions = opts.parser || this.opts.parser || {};
140
153
  if (!parserOptions.validate) parserOptions.validate = {};
141
154
  if (!parserOptions.validate.errors) parserOptions.validate.errors = {};
142
155
  parserOptions.validate.errors.colorize = this.opts.colorizeErrors;
@@ -1 +1 @@
1
- {"version":3,"sources":["/Users/erunion/code/readme/oas/packages/oas-normalize/dist/index.cjs","../src/index.ts"],"names":[],"mappings":"AAAA;AACE;AACF,wDAAA;AACA;AACE;AACA;AACA;AACA;AACA;AACA;AACA;AACF,wDAAA;AACA;AACA;ACTA,gEAAe;AAEf,uDAA6D;AAC7D,kIAA6B;AAC7B,oHAAsB;AAKtB,IAAqB,aAAA,EAArB,MAAqB,cAAa;AAAA,EAChC;AAAA,EAOA;AAAA,EAEA;AAAA,EAEA;AAAA,EAEA,WAAA,CAAY,IAAA,EAAW,IAAA,EAAgB;AACrC,IAAA,IAAA,CAAK,KAAA,EAAO,IAAA;AACZ,IAAA,IAAA,CAAK,KAAA,EAAO;AAAA,MACV,cAAA,EAAgB,KAAA;AAAA,MAChB,WAAA,EAAa,KAAA;AAAA,MACb,GAAG;AAAA,IACL,CAAA;AAEA,IAAA,IAAA,CAAK,KAAA,EAAa,uCAAA,IAAQ,CAAK,IAAI,CAAA;AAEnC,IAAA,IAAA,CAAK,MAAA,EAAQ;AAAA,MACX,IAAA,EAAM,KAAA;AAAA,MACN,MAAA,EAAQ,KAAA;AAAA,MACR,KAAA,EAAO;AAAA,IACT,CAAA;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,IAAA,CAAA,EAAyC;AAC7C,IAAA,GAAA,CAAI,IAAA,CAAK,KAAA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SAAA;AAAA,YACf;AAAA,UACF,CAAA;AAAA,QAEF,KAAK,SAAA;AACH,UAAA,OAAO;AAAA,YACL,aAAA,EAAe,SAAA;AAAA,YACf,OAAA,EAAU,MAAA,CAAyC;AAAA,UACrD,CAAA;AAAA,QAEF,OAAA;AACE,UAAA,MAAM,IAAI,KAAA,CAAM,wBAAwB,CAAA;AAAA,MAC5C;AAAA,IACF,CAAC,CAAA;AAAA,EACH;AACF,CAAA;AD1EA;AACE;AACF,+BAAA","file":"/Users/erunion/code/readme/oas/packages/oas-normalize/dist/index.cjs","sourcesContent":[null,"import type { Options } from './lib/types.js';\nimport type { ParserOptions, ValidationResult } from '@readme/openapi-parser';\nimport type { OpenAPI, OpenAPIV2, OpenAPIV3 } from 'openapi-types';\n\nimport fs from 'node:fs';\n\nimport { bundle, compileErrors, dereference, validate } from '@readme/openapi-parser';\nimport postmanToOpenAPI from '@readme/postman-to-openapi';\nimport converter from 'swagger2openapi';\n\nimport { ValidationError } from './lib/errors.js';\nimport * as utils from './lib/utils.js';\n\nexport default class OASNormalize {\n cache: {\n bundle?: OpenAPI.Document | false;\n convert?: OpenAPI.Document | false;\n deref?: OpenAPI.Document | false;\n load?: Record<string, unknown> | false;\n };\n\n file: any;\n\n opts: Options;\n\n type: ReturnType<typeof utils.getType>;\n\n constructor(file: any, opts?: Options) {\n this.file = file;\n this.opts = {\n colorizeErrors: false,\n enablePaths: false,\n ...opts,\n };\n\n this.type = utils.getType(this.file);\n\n this.cache = {\n load: false,\n bundle: false,\n deref: false,\n };\n }\n\n /**\n * Load and return the API definition that `oas-normalize` was initialized with.\n *\n */\n async load(): Promise<Record<string, unknown>> {\n if (this.cache.load) return this.cache.load;\n\n const resolve = (obj: Parameters<typeof utils.stringToJSON>[0]) => {\n const ret = utils.stringToJSON(obj);\n this.cache.load = ret;\n return ret;\n };\n\n switch (this.type) {\n case 'json':\n case 'string-json':\n case 'string-yaml':\n return resolve(this.file);\n\n case 'buffer':\n return resolve(this.file.toString());\n\n case 'url':\n const { url, options } = utils.prepareURL(this.file);\n const resp = await fetch(url, options).then(res => res.text());\n return resolve(resp);\n\n case 'path':\n // Load a local file\n if (!this.opts.enablePaths) {\n throw new Error('Use `opts.enablePaths` to enable accessing local files.');\n }\n\n const contents = fs.readFileSync(this.file).toString();\n if (!contents.trim()) {\n throw new Error('No file contents found.');\n }\n return resolve(contents);\n\n default:\n throw new Error('Could not load this file.');\n }\n }\n\n private static async convertPostmanToOpenAPI(schema: any) {\n return postmanToOpenAPI(JSON.stringify(schema), undefined, { outputFormat: 'json', replaceVars: true }).then(\n JSON.parse,\n );\n }\n\n /**\n * Bundle up the given API definition, resolving any external `$ref` pointers in the process.\n *\n */\n async bundle(): Promise<OpenAPI.Document> {\n if (this.cache.bundle) return this.cache.bundle;\n\n return this.load()\n .then(schema => {\n // Though Postman collections don't support `$ref` pointers for us to bundle we'll still\n // upconvert it to an OpenAPI definition file so our returned dataset is always one of\n // those for a Postman dataset.\n if (utils.isPostman(schema)) {\n return OASNormalize.convertPostmanToOpenAPI(schema);\n }\n\n return schema;\n })\n .then(schema => bundle(schema))\n .then(bundled => {\n this.cache.bundle = bundled;\n return bundled;\n });\n }\n\n /**\n * Dereference the given API definition.\n *\n */\n async deref(): Promise<OpenAPI.Document> {\n if (this.cache.deref) return this.cache.deref;\n\n return this.load()\n .then(schema => {\n // Though Postman collections don't support `$ref` pointers for us to dereference we'll\n // still upconvert it to an OpenAPI definition file so our returned dataset is always one\n // of those for a Postman dataset.\n if (utils.isPostman(schema)) {\n return OASNormalize.convertPostmanToOpenAPI(schema);\n }\n\n return schema;\n })\n .then(schema => dereference(schema))\n .then(dereferenced => {\n this.cache.deref = dereferenced;\n return dereferenced;\n });\n }\n\n /**\n * Convert a given API definition to OpenAPI if it is not already.\n *\n */\n async convert(): Promise<OpenAPI.Document> {\n if (this.cache.convert) return this.cache.convert;\n\n return this.load()\n .then(async schema => {\n // If we have a Postman collection we need to convert it to OpenAPI.\n return utils.isPostman(schema) ? OASNormalize.convertPostmanToOpenAPI(schema) : schema;\n })\n .then(async schema => {\n if (!utils.isSwagger(schema) && !utils.isOpenAPI(schema)) {\n throw new Error('The supplied API definition is unsupported.');\n } else if (utils.isOpenAPI(schema)) {\n return schema;\n }\n\n const baseVersion = parseInt(schema.swagger, 10);\n if (baseVersion === 1) {\n throw new Error('Swagger v1.2 is unsupported.');\n }\n\n return converter\n .convertObj(schema, { anchors: true })\n .then((options: { openapi: OpenAPI.Document }) => options.openapi);\n });\n }\n\n /**\n * Validate a given API definition.\n *\n * If supplied a Postman collection it will be converted to OpenAPI first and then run through\n * standard OpenAPI validation.\n *\n */\n async validate(\n opts: {\n parser?: ParserOptions;\n } = {},\n ): Promise<ValidationResult> {\n const parserOptions = opts.parser || {};\n if (!parserOptions.validate) parserOptions.validate = {};\n if (!parserOptions.validate.errors) parserOptions.validate.errors = {};\n\n parserOptions.validate.errors.colorize = this.opts.colorizeErrors;\n\n return this.load()\n .then(async schema => {\n // Because we don't have something akin to `openapi-parser` for Postman collections we just\n // always convert them to OpenAPI.\n return utils.isPostman(schema) ? OASNormalize.convertPostmanToOpenAPI(schema) : schema;\n })\n .then(async schema => {\n if (!utils.isSwagger(schema) && !utils.isOpenAPI(schema)) {\n throw new ValidationError('The supplied API definition is unsupported.');\n } else if (utils.isSwagger(schema)) {\n const baseVersion = parseInt(schema.swagger, 10);\n if (baseVersion === 1) {\n throw new ValidationError('Swagger v1.2 is unsupported.');\n }\n }\n\n /**\n * `OpenAPIParser.validate()` dereferences schemas at the same time as validation, mutating\n * the supplied parameter in the process, and does not give us an option to disable this.\n * As we already have a dereferencing method on this library, and this method just needs to\n * tell us if the API definition is valid or not, we need to clone the schema before\n * supplying it to `openapi-parser`.\n */\n // eslint-disable-next-line try-catch-failsafe/json-parse\n const clonedSchema = JSON.parse(JSON.stringify(schema));\n\n const result = await validate(clonedSchema, parserOptions);\n if (!result.valid) {\n throw new ValidationError(compileErrors(result));\n }\n\n // The API definition, whatever its format or specification, is valid.\n return result;\n });\n }\n\n /**\n * Retrieve OpenAPI, Swagger, or Postman version information about the supplied API definition.\n *\n */\n async version(): Promise<{ specification: 'openapi' | 'postman' | 'swagger'; version: string | 'unknown' }> {\n return this.load().then(schema => {\n switch (utils.getAPIDefinitionType(schema)) {\n case 'openapi':\n return {\n specification: 'openapi',\n version: (schema as unknown as OpenAPIV3.Document).openapi,\n };\n\n case 'postman':\n let version = 'unknown';\n if ((schema?.info as Record<string, string>)?.schema) {\n // Though `info.schema` is required by the Postman spec there's no strictness to its\n // contents so we'll do our best to extract a version out of this schema URL that they\n // seem to usually match. If not we'll fallback to treating it as an `unknown` version.\n const match = (schema?.info as Record<string, string>).schema.match(\n /http(s?):\\/\\/schema.getpostman.com\\/json\\/collection\\/v([0-9.]+)\\//,\n );\n\n if (match) {\n version = match[2];\n }\n }\n\n return {\n specification: 'postman',\n version,\n };\n\n case 'swagger':\n return {\n specification: 'swagger',\n version: (schema as unknown as OpenAPIV2.Document).swagger,\n };\n\n default:\n throw new Error('Unknown file detected.');\n }\n });\n }\n}\n"]}
1
+ {"version":3,"sources":["/Users/erunion/code/readme/oas/packages/oas-normalize/dist/index.cjs","../src/index.ts"],"names":[],"mappings":"AAAA;AACE;AACF,wDAAA;AACA;AACE;AACA;AACA;AACA;AACA;AACA;AACA;AACF,wDAAA;AACA;AACA;ACTA,gEAAe;AAEf,uDAA6D;AAC7D,kIAA6B;AAC7B,oHAAsB;AAKtB,IAAqB,aAAA,EAArB,MAAqB,cAAa;AAAA,EAChC;AAAA,EAOA;AAAA,EAEA;AAAA,EAEA;AAAA,EAEA,WAAA,CAAY,IAAA,EAAW,IAAA,EAAgB;AACrC,IAAA,IAAA,CAAK,KAAA,EAAO,IAAA;AACZ,IAAA,IAAA,CAAK,KAAA,EAAO;AAAA,MACV,cAAA,EAAgB,KAAA;AAAA,MAChB,WAAA,EAAa,KAAA;AAAA,MACb,MAAA,EAAQ,CAAC,CAAA;AAAA,MACT,GAAG;AAAA,IACL,CAAA;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AAM,OAAA,CAAA,EAAsG;AAC1G,IAAA,OAAO,IAAA,CAAK,IAAA,CAAK,CAAA,CAAE,IAAA,CAAK,CAAA,MAAA,EAAA,GAAU;AAChC,MAAA,OAAA,CAAc,oDAAA,MAA2B,CAAA,EAAG;AAAA,QAC1C,KAAK,SAAA;AACH,UAAA,OAAO;AAAA,YACL,aAAA,EAAe,SAAA;AAAA,YACf,OAAA,EAAU,MAAA,CAAyC;AAAA,UACrD,CAAA;AAAA,QAEF,KAAK,SAAA;AACH,UAAA,IAAI,QAAA,EAAU,SAAA;AACd,UAAA,GAAA,iBAAK,MAAA,2BAAQ,IAAA,6BAAiC,QAAA,EAAQ;AAIpD,YAAA,MAAM,MAAA,EAAA,iBAAS,MAAA,6BAAQ,MAAA,CAAA,CAAgC,MAAA,CAAO,KAAA;AAAA,cAC5D;AAAA,YACF,CAAA;AAEA,YAAA,GAAA,CAAI,KAAA,EAAO;AACT,cAAA,QAAA,EAAU,KAAA,CAAM,CAAC,CAAA;AAAA,YACnB;AAAA,UACF;AAEA,UAAA,OAAO;AAAA,YACL,aAAA,EAAe,SAAA;AAAA,YACf;AAAA,UACF,CAAA;AAAA,QAEF,KAAK,SAAA;AACH,UAAA,OAAO;AAAA,YACL,aAAA,EAAe,SAAA;AAAA,YACf,OAAA,EAAU,MAAA,CAAyC;AAAA,UACrD,CAAA;AAAA,QAEF,OAAA;AACE,UAAA,MAAM,IAAI,KAAA,CAAM,wBAAwB,CAAA;AAAA,MAC5C;AAAA,IACF,CAAC,CAAA;AAAA,EACH;AACF,CAAA;ADnFA;AACE;AACF,+BAAA","file":"/Users/erunion/code/readme/oas/packages/oas-normalize/dist/index.cjs","sourcesContent":[null,"import type { Options } from './lib/types.js';\nimport type { ParserOptions, ValidationResult } from '@readme/openapi-parser';\nimport type { OpenAPI, OpenAPIV2, OpenAPIV3 } from 'openapi-types';\n\nimport fs from 'node:fs';\n\nimport { bundle, compileErrors, dereference, validate } from '@readme/openapi-parser';\nimport postmanToOpenAPI from '@readme/postman-to-openapi';\nimport converter from 'swagger2openapi';\n\nimport { ValidationError } from './lib/errors.js';\nimport * as utils from './lib/utils.js';\n\nexport default class OASNormalize {\n cache: {\n bundle?: OpenAPI.Document | false;\n convert?: OpenAPI.Document | false;\n deref?: OpenAPI.Document | false;\n load?: Record<string, unknown> | false;\n };\n\n file: any;\n\n opts: Options;\n\n type: ReturnType<typeof utils.getType>;\n\n constructor(file: any, opts?: Options) {\n this.file = file;\n this.opts = {\n colorizeErrors: false,\n enablePaths: false,\n parser: {},\n ...opts,\n };\n\n this.type = utils.getType(this.file);\n\n this.cache = {\n load: false,\n bundle: false,\n deref: false,\n };\n }\n\n /**\n * Load and return the API definition that `oas-normalize` was initialized with.\n *\n */\n async load(): Promise<Record<string, unknown>> {\n if (this.cache.load) return this.cache.load;\n\n const resolve = (obj: Parameters<typeof utils.stringToJSON>[0]) => {\n const ret = utils.stringToJSON(obj);\n this.cache.load = ret;\n return ret;\n };\n\n switch (this.type) {\n case 'json':\n case 'string-json':\n case 'string-yaml':\n return resolve(this.file);\n\n case 'buffer':\n return resolve(this.file.toString());\n\n case 'url':\n const { url, options } = utils.prepareURL(this.file);\n const resp = await fetch(url, options).then(res => res.text());\n return resolve(resp);\n\n case 'path':\n // Load a local file\n if (!this.opts.enablePaths) {\n throw new Error('Use `opts.enablePaths` to enable accessing local files.');\n }\n\n const contents = fs.readFileSync(this.file).toString();\n if (!contents.trim()) {\n throw new Error('No file contents found.');\n }\n return resolve(contents);\n\n default:\n throw new Error('Could not load this file.');\n }\n }\n\n private static async convertPostmanToOpenAPI(schema: any) {\n return postmanToOpenAPI(JSON.stringify(schema), undefined, { outputFormat: 'json', replaceVars: true }).then(\n JSON.parse,\n );\n }\n\n /**\n * Bundle up the given API definition, resolving any external `$ref` pointers in the process.\n *\n */\n async bundle(): Promise<OpenAPI.Document> {\n if (this.cache.bundle) return this.cache.bundle;\n const parserOptions = this.opts.parser || {};\n\n return this.load()\n .then(schema => {\n // Though Postman collections don't support `$ref` pointers for us to bundle we'll still\n // upconvert it to an OpenAPI definition file so our returned dataset is always one of\n // those for a Postman dataset.\n if (utils.isPostman(schema)) {\n return OASNormalize.convertPostmanToOpenAPI(schema);\n }\n\n return schema;\n })\n .then(schema => bundle(schema, parserOptions))\n .then(bundled => {\n this.cache.bundle = bundled;\n return bundled;\n });\n }\n\n /**\n * Dereference the given API definition.\n *\n */\n async dereference(): Promise<OpenAPI.Document> {\n if (this.cache.deref) return this.cache.deref;\n const parserOptions = this.opts.parser || {};\n\n return this.load()\n .then(schema => {\n // Though Postman collections don't support `$ref` pointers for us to dereference we'll\n // still upconvert it to an OpenAPI definition file so our returned dataset is always one\n // of those for a Postman dataset.\n if (utils.isPostman(schema)) {\n return OASNormalize.convertPostmanToOpenAPI(schema);\n }\n\n return schema;\n })\n .then(schema => dereference(schema, parserOptions))\n .then(dereferenced => {\n this.cache.deref = dereferenced;\n return dereferenced;\n });\n }\n\n /**\n * Dereference the given API definition.\n *\n * This method is deprecated in favor of `dereference`. It will be removed in a future release.\n *\n * @deprecated\n */\n async deref(): Promise<OpenAPI.Document> {\n return this.dereference();\n }\n\n /**\n * Convert a given API definition to OpenAPI if it is not already.\n *\n */\n async convert(): Promise<OpenAPI.Document> {\n if (this.cache.convert) return this.cache.convert;\n\n return this.load()\n .then(async schema => {\n // If we have a Postman collection we need to convert it to OpenAPI.\n return utils.isPostman(schema) ? OASNormalize.convertPostmanToOpenAPI(schema) : schema;\n })\n .then(async schema => {\n if (!utils.isSwagger(schema) && !utils.isOpenAPI(schema)) {\n throw new Error('The supplied API definition is unsupported.');\n } else if (utils.isOpenAPI(schema)) {\n return schema;\n }\n\n const baseVersion = parseInt(schema.swagger, 10);\n if (baseVersion === 1) {\n throw new Error('Swagger v1.2 is unsupported.');\n }\n\n return converter\n .convertObj(schema, { anchors: true })\n .then((options: { openapi: OpenAPI.Document }) => options.openapi);\n });\n }\n\n /**\n * Validate a given API definition.\n *\n * If supplied a Postman collection it will be converted to OpenAPI first and then run through\n * standard OpenAPI validation.\n *\n */\n async validate(\n opts: {\n /**\n * Options to supply to our OpenAPI parser. See `@readme/openapi-parser` for documentation.\n * This option is deprecated in favor of the `parser` option on the constructor. It will be\n * removed in a future release.\n *\n * @see {@link https://npm.im/@readme/openapi-parser}\n * @deprecated\n */\n parser?: ParserOptions;\n } = {},\n ): Promise<ValidationResult> {\n const parserOptions = opts.parser || this.opts.parser || {};\n if (!parserOptions.validate) parserOptions.validate = {};\n if (!parserOptions.validate.errors) parserOptions.validate.errors = {};\n\n parserOptions.validate.errors.colorize = this.opts.colorizeErrors;\n\n return this.load()\n .then(async schema => {\n // Because we don't have something akin to `openapi-parser` for Postman collections we just\n // always convert them to OpenAPI.\n return utils.isPostman(schema) ? OASNormalize.convertPostmanToOpenAPI(schema) : schema;\n })\n .then(async schema => {\n if (!utils.isSwagger(schema) && !utils.isOpenAPI(schema)) {\n throw new ValidationError('The supplied API definition is unsupported.');\n } else if (utils.isSwagger(schema)) {\n const baseVersion = parseInt(schema.swagger, 10);\n if (baseVersion === 1) {\n throw new ValidationError('Swagger v1.2 is unsupported.');\n }\n }\n\n /**\n * `OpenAPIParser.validate()` dereferences schemas at the same time as validation, mutating\n * the supplied parameter in the process, and does not give us an option to disable this.\n * As we already have a dereferencing method on this library, and this method just needs to\n * tell us if the API definition is valid or not, we need to clone the schema before\n * supplying it to `openapi-parser`.\n */\n // eslint-disable-next-line try-catch-failsafe/json-parse\n const clonedSchema = JSON.parse(JSON.stringify(schema));\n\n const result = await validate(clonedSchema, parserOptions);\n if (!result.valid) {\n throw new ValidationError(compileErrors(result));\n }\n\n // The API definition, whatever its format or specification, is valid.\n return result;\n });\n }\n\n /**\n * Retrieve OpenAPI, Swagger, or Postman version information about the supplied API definition.\n *\n */\n async version(): Promise<{ specification: 'openapi' | 'postman' | 'swagger'; version: string | 'unknown' }> {\n return this.load().then(schema => {\n switch (utils.getAPIDefinitionType(schema)) {\n case 'openapi':\n return {\n specification: 'openapi',\n version: (schema as unknown as OpenAPIV3.Document).openapi,\n };\n\n case 'postman':\n let version = 'unknown';\n if ((schema?.info as Record<string, string>)?.schema) {\n // Though `info.schema` is required by the Postman spec there's no strictness to its\n // contents so we'll do our best to extract a version out of this schema URL that they\n // seem to usually match. If not we'll fallback to treating it as an `unknown` version.\n const match = (schema?.info as Record<string, string>).schema.match(\n /http(s?):\\/\\/schema.getpostman.com\\/json\\/collection\\/v([0-9.]+)\\//,\n );\n\n if (match) {\n version = match[2];\n }\n }\n\n return {\n specification: 'postman',\n version,\n };\n\n case 'swagger':\n return {\n specification: 'swagger',\n version: (schema as unknown as OpenAPIV2.Document).swagger,\n };\n\n default:\n throw new Error('Unknown file detected.');\n }\n });\n }\n}\n"]}
package/dist/index.d.cts CHANGED
@@ -29,6 +29,14 @@ declare class OASNormalize {
29
29
  * Dereference the given API definition.
30
30
  *
31
31
  */
32
+ dereference(): Promise<OpenAPI.Document>;
33
+ /**
34
+ * Dereference the given API definition.
35
+ *
36
+ * This method is deprecated in favor of `dereference`. It will be removed in a future release.
37
+ *
38
+ * @deprecated
39
+ */
32
40
  deref(): Promise<OpenAPI.Document>;
33
41
  /**
34
42
  * Convert a given API definition to OpenAPI if it is not already.
@@ -43,6 +51,14 @@ declare class OASNormalize {
43
51
  *
44
52
  */
45
53
  validate(opts?: {
54
+ /**
55
+ * Options to supply to our OpenAPI parser. See `@readme/openapi-parser` for documentation.
56
+ * This option is deprecated in favor of the `parser` option on the constructor. It will be
57
+ * removed in a future release.
58
+ *
59
+ * @see {@link https://npm.im/@readme/openapi-parser}
60
+ * @deprecated
61
+ */
46
62
  parser?: ParserOptions;
47
63
  }): Promise<ValidationResult>;
48
64
  /**
package/dist/index.d.ts CHANGED
@@ -29,6 +29,14 @@ declare class OASNormalize {
29
29
  * Dereference the given API definition.
30
30
  *
31
31
  */
32
+ dereference(): Promise<OpenAPI.Document>;
33
+ /**
34
+ * Dereference the given API definition.
35
+ *
36
+ * This method is deprecated in favor of `dereference`. It will be removed in a future release.
37
+ *
38
+ * @deprecated
39
+ */
32
40
  deref(): Promise<OpenAPI.Document>;
33
41
  /**
34
42
  * Convert a given API definition to OpenAPI if it is not already.
@@ -43,6 +51,14 @@ declare class OASNormalize {
43
51
  *
44
52
  */
45
53
  validate(opts?: {
54
+ /**
55
+ * Options to supply to our OpenAPI parser. See `@readme/openapi-parser` for documentation.
56
+ * This option is deprecated in favor of the `parser` option on the constructor. It will be
57
+ * removed in a future release.
58
+ *
59
+ * @see {@link https://npm.im/@readme/openapi-parser}
60
+ * @deprecated
61
+ */
46
62
  parser?: ParserOptions;
47
63
  }): Promise<ValidationResult>;
48
64
  /**
package/dist/index.js CHANGED
@@ -26,6 +26,7 @@ var OASNormalize = class _OASNormalize {
26
26
  this.opts = {
27
27
  colorizeErrors: false,
28
28
  enablePaths: false,
29
+ parser: {},
29
30
  ...opts
30
31
  };
31
32
  this.type = getType(this.file);
@@ -81,12 +82,13 @@ var OASNormalize = class _OASNormalize {
81
82
  */
82
83
  async bundle() {
83
84
  if (this.cache.bundle) return this.cache.bundle;
85
+ const parserOptions = this.opts.parser || {};
84
86
  return this.load().then((schema) => {
85
87
  if (isPostman(schema)) {
86
88
  return _OASNormalize.convertPostmanToOpenAPI(schema);
87
89
  }
88
90
  return schema;
89
- }).then((schema) => bundle(schema)).then((bundled) => {
91
+ }).then((schema) => bundle(schema, parserOptions)).then((bundled) => {
90
92
  this.cache.bundle = bundled;
91
93
  return bundled;
92
94
  });
@@ -95,18 +97,29 @@ var OASNormalize = class _OASNormalize {
95
97
  * Dereference the given API definition.
96
98
  *
97
99
  */
98
- async deref() {
100
+ async dereference() {
99
101
  if (this.cache.deref) return this.cache.deref;
102
+ const parserOptions = this.opts.parser || {};
100
103
  return this.load().then((schema) => {
101
104
  if (isPostman(schema)) {
102
105
  return _OASNormalize.convertPostmanToOpenAPI(schema);
103
106
  }
104
107
  return schema;
105
- }).then((schema) => dereference(schema)).then((dereferenced) => {
108
+ }).then((schema) => dereference(schema, parserOptions)).then((dereferenced) => {
106
109
  this.cache.deref = dereferenced;
107
110
  return dereferenced;
108
111
  });
109
112
  }
113
+ /**
114
+ * Dereference the given API definition.
115
+ *
116
+ * This method is deprecated in favor of `dereference`. It will be removed in a future release.
117
+ *
118
+ * @deprecated
119
+ */
120
+ async deref() {
121
+ return this.dereference();
122
+ }
110
123
  /**
111
124
  * Convert a given API definition to OpenAPI if it is not already.
112
125
  *
@@ -136,7 +149,7 @@ var OASNormalize = class _OASNormalize {
136
149
  *
137
150
  */
138
151
  async validate(opts = {}) {
139
- const parserOptions = opts.parser || {};
152
+ const parserOptions = opts.parser || this.opts.parser || {};
140
153
  if (!parserOptions.validate) parserOptions.validate = {};
141
154
  if (!parserOptions.validate.errors) parserOptions.validate.errors = {};
142
155
  parserOptions.validate.errors.colorize = this.opts.colorizeErrors;
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { Options } from './lib/types.js';\nimport type { ParserOptions, ValidationResult } from '@readme/openapi-parser';\nimport type { OpenAPI, OpenAPIV2, OpenAPIV3 } from 'openapi-types';\n\nimport fs from 'node:fs';\n\nimport { bundle, compileErrors, dereference, validate } from '@readme/openapi-parser';\nimport postmanToOpenAPI from '@readme/postman-to-openapi';\nimport converter from 'swagger2openapi';\n\nimport { ValidationError } from './lib/errors.js';\nimport * as utils from './lib/utils.js';\n\nexport default class OASNormalize {\n cache: {\n bundle?: OpenAPI.Document | false;\n convert?: OpenAPI.Document | false;\n deref?: OpenAPI.Document | false;\n load?: Record<string, unknown> | false;\n };\n\n file: any;\n\n opts: Options;\n\n type: ReturnType<typeof utils.getType>;\n\n constructor(file: any, opts?: Options) {\n this.file = file;\n this.opts = {\n colorizeErrors: false,\n enablePaths: false,\n ...opts,\n };\n\n this.type = utils.getType(this.file);\n\n this.cache = {\n load: false,\n bundle: false,\n deref: false,\n };\n }\n\n /**\n * Load and return the API definition that `oas-normalize` was initialized with.\n *\n */\n async load(): Promise<Record<string, unknown>> {\n if (this.cache.load) return this.cache.load;\n\n const resolve = (obj: Parameters<typeof utils.stringToJSON>[0]) => {\n const ret = utils.stringToJSON(obj);\n this.cache.load = ret;\n return ret;\n };\n\n switch (this.type) {\n case 'json':\n case 'string-json':\n case 'string-yaml':\n return resolve(this.file);\n\n case 'buffer':\n return resolve(this.file.toString());\n\n case 'url':\n const { url, options } = utils.prepareURL(this.file);\n const resp = await fetch(url, options).then(res => res.text());\n return resolve(resp);\n\n case 'path':\n // Load a local file\n if (!this.opts.enablePaths) {\n throw new Error('Use `opts.enablePaths` to enable accessing local files.');\n }\n\n const contents = fs.readFileSync(this.file).toString();\n if (!contents.trim()) {\n throw new Error('No file contents found.');\n }\n return resolve(contents);\n\n default:\n throw new Error('Could not load this file.');\n }\n }\n\n private static async convertPostmanToOpenAPI(schema: any) {\n return postmanToOpenAPI(JSON.stringify(schema), undefined, { outputFormat: 'json', replaceVars: true }).then(\n JSON.parse,\n );\n }\n\n /**\n * Bundle up the given API definition, resolving any external `$ref` pointers in the process.\n *\n */\n async bundle(): Promise<OpenAPI.Document> {\n if (this.cache.bundle) return this.cache.bundle;\n\n return this.load()\n .then(schema => {\n // Though Postman collections don't support `$ref` pointers for us to bundle we'll still\n // upconvert it to an OpenAPI definition file so our returned dataset is always one of\n // those for a Postman dataset.\n if (utils.isPostman(schema)) {\n return OASNormalize.convertPostmanToOpenAPI(schema);\n }\n\n return schema;\n })\n .then(schema => bundle(schema))\n .then(bundled => {\n this.cache.bundle = bundled;\n return bundled;\n });\n }\n\n /**\n * Dereference the given API definition.\n *\n */\n async deref(): Promise<OpenAPI.Document> {\n if (this.cache.deref) return this.cache.deref;\n\n return this.load()\n .then(schema => {\n // Though Postman collections don't support `$ref` pointers for us to dereference we'll\n // still upconvert it to an OpenAPI definition file so our returned dataset is always one\n // of those for a Postman dataset.\n if (utils.isPostman(schema)) {\n return OASNormalize.convertPostmanToOpenAPI(schema);\n }\n\n return schema;\n })\n .then(schema => dereference(schema))\n .then(dereferenced => {\n this.cache.deref = dereferenced;\n return dereferenced;\n });\n }\n\n /**\n * Convert a given API definition to OpenAPI if it is not already.\n *\n */\n async convert(): Promise<OpenAPI.Document> {\n if (this.cache.convert) return this.cache.convert;\n\n return this.load()\n .then(async schema => {\n // If we have a Postman collection we need to convert it to OpenAPI.\n return utils.isPostman(schema) ? OASNormalize.convertPostmanToOpenAPI(schema) : schema;\n })\n .then(async schema => {\n if (!utils.isSwagger(schema) && !utils.isOpenAPI(schema)) {\n throw new Error('The supplied API definition is unsupported.');\n } else if (utils.isOpenAPI(schema)) {\n return schema;\n }\n\n const baseVersion = parseInt(schema.swagger, 10);\n if (baseVersion === 1) {\n throw new Error('Swagger v1.2 is unsupported.');\n }\n\n return converter\n .convertObj(schema, { anchors: true })\n .then((options: { openapi: OpenAPI.Document }) => options.openapi);\n });\n }\n\n /**\n * Validate a given API definition.\n *\n * If supplied a Postman collection it will be converted to OpenAPI first and then run through\n * standard OpenAPI validation.\n *\n */\n async validate(\n opts: {\n parser?: ParserOptions;\n } = {},\n ): Promise<ValidationResult> {\n const parserOptions = opts.parser || {};\n if (!parserOptions.validate) parserOptions.validate = {};\n if (!parserOptions.validate.errors) parserOptions.validate.errors = {};\n\n parserOptions.validate.errors.colorize = this.opts.colorizeErrors;\n\n return this.load()\n .then(async schema => {\n // Because we don't have something akin to `openapi-parser` for Postman collections we just\n // always convert them to OpenAPI.\n return utils.isPostman(schema) ? OASNormalize.convertPostmanToOpenAPI(schema) : schema;\n })\n .then(async schema => {\n if (!utils.isSwagger(schema) && !utils.isOpenAPI(schema)) {\n throw new ValidationError('The supplied API definition is unsupported.');\n } else if (utils.isSwagger(schema)) {\n const baseVersion = parseInt(schema.swagger, 10);\n if (baseVersion === 1) {\n throw new ValidationError('Swagger v1.2 is unsupported.');\n }\n }\n\n /**\n * `OpenAPIParser.validate()` dereferences schemas at the same time as validation, mutating\n * the supplied parameter in the process, and does not give us an option to disable this.\n * As we already have a dereferencing method on this library, and this method just needs to\n * tell us if the API definition is valid or not, we need to clone the schema before\n * supplying it to `openapi-parser`.\n */\n // eslint-disable-next-line try-catch-failsafe/json-parse\n const clonedSchema = JSON.parse(JSON.stringify(schema));\n\n const result = await validate(clonedSchema, parserOptions);\n if (!result.valid) {\n throw new ValidationError(compileErrors(result));\n }\n\n // The API definition, whatever its format or specification, is valid.\n return result;\n });\n }\n\n /**\n * Retrieve OpenAPI, Swagger, or Postman version information about the supplied API definition.\n *\n */\n async version(): Promise<{ specification: 'openapi' | 'postman' | 'swagger'; version: string | 'unknown' }> {\n return this.load().then(schema => {\n switch (utils.getAPIDefinitionType(schema)) {\n case 'openapi':\n return {\n specification: 'openapi',\n version: (schema as unknown as OpenAPIV3.Document).openapi,\n };\n\n case 'postman':\n let version = 'unknown';\n if ((schema?.info as Record<string, string>)?.schema) {\n // Though `info.schema` is required by the Postman spec there's no strictness to its\n // contents so we'll do our best to extract a version out of this schema URL that they\n // seem to usually match. If not we'll fallback to treating it as an `unknown` version.\n const match = (schema?.info as Record<string, string>).schema.match(\n /http(s?):\\/\\/schema.getpostman.com\\/json\\/collection\\/v([0-9.]+)\\//,\n );\n\n if (match) {\n version = match[2];\n }\n }\n\n return {\n specification: 'postman',\n version,\n };\n\n case 'swagger':\n return {\n specification: 'swagger',\n version: (schema as unknown as OpenAPIV2.Document).swagger,\n };\n\n default:\n throw new Error('Unknown file detected.');\n }\n });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;AAIA,OAAO,QAAQ;AAEf,SAAS,QAAQ,eAAe,aAAa,gBAAgB;AAC7D,OAAO,sBAAsB;AAC7B,OAAO,eAAe;AAKtB,IAAqB,eAArB,MAAqB,cAAa;AAAA,EAChC;AAAA,EAOA;AAAA,EAEA;AAAA,EAEA;AAAA,EAEA,YAAY,MAAW,MAAgB;AACrC,SAAK,OAAO;AACZ,SAAK,OAAO;AAAA,MACV,gBAAgB;AAAA,MAChB,aAAa;AAAA,MACb,GAAG;AAAA,IACL;AAEA,SAAK,O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AQ,MAAiC,QAAQ;AAIpD,kBAAM,SAAS,QAAQ,MAAgC,OAAO;AAAA,cAC5D;AAAA,YACF;AAEA,gBAAI,OAAO;AACT,wBAAU,MAAM,CAAC;AAAA,YACnB;AAAA,UACF;AAEA,iBAAO;AAAA,YACL,eAAe;AAAA,YACf;AAAA,UACF;AAAA,QAEF,KAAK;AACH,iBAAO;AAAA,YACL,eAAe;AAAA,YACf,SAAU,OAAyC;AAAA,UACrD;AAAA,QAEF;AACE,gBAAM,IAAI,MAAM,wBAAwB;AAAA,MAC5C;AAAA,IACF,CAAC;AAAA,EACH;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { Options } from './lib/types.js';\nimport type { ParserOptions, ValidationResult } from '@readme/openapi-parser';\nimport type { OpenAPI, OpenAPIV2, OpenAPIV3 } from 'openapi-types';\n\nimport fs from 'node:fs';\n\nimport { bundle, compileErrors, dereference, validate } from '@readme/openapi-parser';\nimport postmanToOpenAPI from '@readme/postman-to-openapi';\nimport converter from 'swagger2openapi';\n\nimport { ValidationError } from './lib/errors.js';\nimport * as utils from './lib/utils.js';\n\nexport default class OASNormalize {\n cache: {\n bundle?: OpenAPI.Document | false;\n convert?: OpenAPI.Document | false;\n deref?: OpenAPI.Document | false;\n load?: Record<string, unknown> | false;\n };\n\n file: any;\n\n opts: Options;\n\n type: ReturnType<typeof utils.getType>;\n\n constructor(file: any, opts?: Options) {\n this.file = file;\n this.opts = {\n colorizeErrors: false,\n enablePaths: false,\n parser: {},\n ...opts,\n };\n\n this.type = utils.getType(this.file);\n\n this.cache = {\n load: false,\n bundle: false,\n deref: false,\n };\n }\n\n /**\n * Load and return the API definition that `oas-normalize` was initialized with.\n *\n */\n async load(): Promise<Record<string, unknown>> {\n if (this.cache.load) return this.cache.load;\n\n const resolve = (obj: Parameters<typeof utils.stringToJSON>[0]) => {\n const ret = utils.stringToJSON(obj);\n this.cache.load = ret;\n return ret;\n };\n\n switch (this.type) {\n case 'json':\n case 'string-json':\n case 'string-yaml':\n return resolve(this.file);\n\n case 'buffer':\n return resolve(this.file.toString());\n\n case 'url':\n const { url, options } = utils.prepareURL(this.file);\n const resp = await fetch(url, options).then(res => res.text());\n return resolve(resp);\n\n case 'path':\n // Load a local file\n if (!this.opts.enablePaths) {\n throw new Error('Use `opts.enablePaths` to enable accessing local files.');\n }\n\n const contents = fs.readFileSync(this.file).toString();\n if (!contents.trim()) {\n throw new Error('No file contents found.');\n }\n return resolve(contents);\n\n default:\n throw new Error('Could not load this file.');\n }\n }\n\n private static async convertPostmanToOpenAPI(schema: any) {\n return postmanToOpenAPI(JSON.stringify(schema), undefined, { outputFormat: 'json', replaceVars: true }).then(\n JSON.parse,\n );\n }\n\n /**\n * Bundle up the given API definition, resolving any external `$ref` pointers in the process.\n *\n */\n async bundle(): Promise<OpenAPI.Document> {\n if (this.cache.bundle) return this.cache.bundle;\n const parserOptions = this.opts.parser || {};\n\n return this.load()\n .then(schema => {\n // Though Postman collections don't support `$ref` pointers for us to bundle we'll still\n // upconvert it to an OpenAPI definition file so our returned dataset is always one of\n // those for a Postman dataset.\n if (utils.isPostman(schema)) {\n return OASNormalize.convertPostmanToOpenAPI(schema);\n }\n\n return schema;\n })\n .then(schema => bundle(schema, parserOptions))\n .then(bundled => {\n this.cache.bundle = bundled;\n return bundled;\n });\n }\n\n /**\n * Dereference the given API definition.\n *\n */\n async dereference(): Promise<OpenAPI.Document> {\n if (this.cache.deref) return this.cache.deref;\n const parserOptions = this.opts.parser || {};\n\n return this.load()\n .then(schema => {\n // Though Postman collections don't support `$ref` pointers for us to dereference we'll\n // still upconvert it to an OpenAPI definition file so our returned dataset is always one\n // of those for a Postman dataset.\n if (utils.isPostman(schema)) {\n return OASNormalize.convertPostmanToOpenAPI(schema);\n }\n\n return schema;\n })\n .then(schema => dereference(schema, parserOptions))\n .then(dereferenced => {\n this.cache.deref = dereferenced;\n return dereferenced;\n });\n }\n\n /**\n * Dereference the given API definition.\n *\n * This method is deprecated in favor of `dereference`. It will be removed in a future release.\n *\n * @deprecated\n */\n async deref(): Promise<OpenAPI.Document> {\n return this.dereference();\n }\n\n /**\n * Convert a given API definition to OpenAPI if it is not already.\n *\n */\n async convert(): Promise<OpenAPI.Document> {\n if (this.cache.convert) return this.cache.convert;\n\n return this.load()\n .then(async schema => {\n // If we have a Postman collection we need to convert it to OpenAPI.\n return utils.isPostman(schema) ? OASNormalize.convertPostmanToOpenAPI(schema) : schema;\n })\n .then(async schema => {\n if (!utils.isSwagger(schema) && !utils.isOpenAPI(schema)) {\n throw new Error('The supplied API definition is unsupported.');\n } else if (utils.isOpenAPI(schema)) {\n return schema;\n }\n\n const baseVersion = parseInt(schema.swagger, 10);\n if (baseVersion === 1) {\n throw new Error('Swagger v1.2 is unsupported.');\n }\n\n return converter\n .convertObj(schema, { anchors: true })\n .then((options: { openapi: OpenAPI.Document }) => options.openapi);\n });\n }\n\n /**\n * Validate a given API definition.\n *\n * If supplied a Postman collection it will be converted to OpenAPI first and then run through\n * standard OpenAPI validation.\n *\n */\n async validate(\n opts: {\n /**\n * Options to supply to our OpenAPI parser. See `@readme/openapi-parser` for documentation.\n * This option is deprecated in favor of the `parser` option on the constructor. It will be\n * removed in a future release.\n *\n * @see {@link https://npm.im/@readme/openapi-parser}\n * @deprecated\n */\n parser?: ParserOptions;\n } = {},\n ): Promise<ValidationResult> {\n const parserOptions = opts.parser || this.opts.parser || {};\n if (!parserOptions.validate) parserOptions.validate = {};\n if (!parserOptions.validate.errors) parserOptions.validate.errors = {};\n\n parserOptions.validate.errors.colorize = this.opts.colorizeErrors;\n\n return this.load()\n .then(async schema => {\n // Because we don't have something akin to `openapi-parser` for Postman collections we just\n // always convert them to OpenAPI.\n return utils.isPostman(schema) ? OASNormalize.convertPostmanToOpenAPI(schema) : schema;\n })\n .then(async schema => {\n if (!utils.isSwagger(schema) && !utils.isOpenAPI(schema)) {\n throw new ValidationError('The supplied API definition is unsupported.');\n } else if (utils.isSwagger(schema)) {\n const baseVersion = parseInt(schema.swagger, 10);\n if (baseVersion === 1) {\n throw new ValidationError('Swagger v1.2 is unsupported.');\n }\n }\n\n /**\n * `OpenAPIParser.validate()` dereferences schemas at the same time as validation, mutating\n * the supplied parameter in the process, and does not give us an option to disable this.\n * As we already have a dereferencing method on this library, and this method just needs to\n * tell us if the API definition is valid or not, we need to clone the schema before\n * supplying it to `openapi-parser`.\n */\n // eslint-disable-next-line try-catch-failsafe/json-parse\n const clonedSchema = JSON.parse(JSON.stringify(schema));\n\n const result = await validate(clonedSchema, parserOptions);\n if (!result.valid) {\n throw new ValidationError(compileErrors(result));\n }\n\n // The API definition, whatever its format or specification, is valid.\n return result;\n });\n }\n\n /**\n * Retrieve OpenAPI, Swagger, or Postman version information about the supplied API definition.\n *\n */\n async version(): Promise<{ specification: 'openapi' | 'postman' | 'swagger'; version: string | 'unknown' }> {\n return this.load().then(schema => {\n switch (utils.getAPIDefinitionType(schema)) {\n case 'openapi':\n return {\n specification: 'openapi',\n version: (schema as unknown as OpenAPIV3.Document).openapi,\n };\n\n case 'postman':\n let version = 'unknown';\n if ((schema?.info as Record<string, string>)?.schema) {\n // Though `info.schema` is required by the Postman spec there's no strictness to its\n // contents so we'll do our best to extract a version out of this schema URL that they\n // seem to usually match. If not we'll fallback to treating it as an `unknown` version.\n const match = (schema?.info as Record<string, string>).schema.match(\n /http(s?):\\/\\/schema.getpostman.com\\/json\\/collection\\/v([0-9.]+)\\//,\n );\n\n if (match) {\n version = match[2];\n }\n }\n\n return {\n specification: 'postman',\n version,\n };\n\n case 'swagger':\n return {\n specification: 'swagger',\n version: (schema as unknown as OpenAPIV2.Document).swagger,\n };\n\n default:\n throw new Error('Unknown file detected.');\n }\n });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;AAIA,OAAO,QAAQ;AAEf,SAAS,QAAQ,eAAe,aAAa,gBAAgB;AAC7D,OAAO,sBAAsB;AAC7B,OAAO,eAAe;AAKtB,IAAqB,eAArB,MAAqB,cAAa;AAAA,EAChC;AAA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AC;AAEtD,YAAM,SAAS,MAAM,SAAS,cAAc,aAAa;AACzD,UAAI,CAAC,OAAO,OAAO;AACjB,cAAM,IAAI,gBAAgB,cAAc,MAAM,CAAC;AAAA,MACjD;AAGA,aAAO;AAAA,IACT,CAAC;AAAA,EACL;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,UAAsG;AAC1G,WAAO,KAAK,KAAK,EAAE,KAAK,YAAU;AAChC,cAAc,qBAAqB,MAAM,GAAG;AAAA,QAC1C,KAAK;AACH,iBAAO;AAAA,YACL,eAAe;AAAA,YACf,SAAU,OAAyC;AAAA,UACrD;AAAA,QAEF,KAAK;AACH,cAAI,UAAU;AACd,cAAK,QAAQ,MAAiC,QAAQ;AAIpD,kBAAM,SAAS,QAAQ,MAAgC,OAAO;AAAA,cAC5D;AAAA,YACF;AAEA,gBAAI,OAAO;AACT,wBAAU,MAAM,CAAC;AAAA,YACnB;AAAA,UACF;AAEA,iBAAO;AAAA,YACL,eAAe;AAAA,YACf;AAAA,UACF;AAAA,QAEF,KAAK;AACH,iBAAO;AAAA,YACL,eAAe;AAAA,YACf,SAAU,OAAyC;AAAA,UACrD;AAAA,QAEF;AACE,gBAAM,IAAI,MAAM,wBAAwB;AAAA,MAC5C;AAAA,IACF,CAAC;AAAA,EACH;AACF;","names":[]}
@@ -1,6 +1,21 @@
1
+ import { ParserOptions } from '@readme/openapi-parser';
2
+
1
3
  interface Options {
4
+ /**
5
+ * Configures if you want validation errors that are thrown to be colorized. The default is
6
+ * `false`.
7
+ */
2
8
  colorizeErrors?: boolean;
9
+ /**
10
+ * If you want to allow fetching of local paths. For security reasons the default is `false`.
11
+ */
3
12
  enablePaths?: boolean;
13
+ /**
14
+ * Options to supply to our OpenAPI parser. See `@readme/openapi-parser` for documentation.
15
+ *
16
+ * @see {@link https://npm.im/@readme/openapi-parser}
17
+ */
18
+ parser?: ParserOptions;
4
19
  }
5
20
 
6
21
  export type { Options };
@@ -1,6 +1,21 @@
1
+ import { ParserOptions } from '@readme/openapi-parser';
2
+
1
3
  interface Options {
4
+ /**
5
+ * Configures if you want validation errors that are thrown to be colorized. The default is
6
+ * `false`.
7
+ */
2
8
  colorizeErrors?: boolean;
9
+ /**
10
+ * If you want to allow fetching of local paths. For security reasons the default is `false`.
11
+ */
3
12
  enablePaths?: boolean;
13
+ /**
14
+ * Options to supply to our OpenAPI parser. See `@readme/openapi-parser` for documentation.
15
+ *
16
+ * @see {@link https://npm.im/@readme/openapi-parser}
17
+ */
18
+ parser?: ParserOptions;
4
19
  }
5
20
 
6
21
  export type { Options };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oas-normalize",
3
- "version": "13.0.1",
3
+ "version": "13.1.1",
4
4
  "description": "Tooling for converting, validating, and parsing OpenAPI, Swagger, and Postman API definitions",
5
5
  "type": "module",
6
6
  "exports": {
@@ -75,7 +75,7 @@
75
75
  "swagger2openapi": "^7.0.8"
76
76
  },
77
77
  "devDependencies": {
78
- "@readme/oas-examples": "^5.19.1",
78
+ "@readme/oas-examples": "^5.19.2",
79
79
  "@types/js-yaml": "^4.0.9",
80
80
  "@types/swagger2openapi": "^7.0.4",
81
81
  "eslint": "^8.57.0",
@@ -84,5 +84,5 @@
84
84
  "vitest": "^3.0.5"
85
85
  },
86
86
  "prettier": "@readme/eslint-config/prettier",
87
- "gitHead": "16b03c527ed95c026e5781bfb1840c7278361cb5"
87
+ "gitHead": "e0b2cf88df2dffdeca234882fa966356041121e3"
88
88
  }