diff --git a/lib/index.ts b/lib/index.ts index 3df98356..56408c9c 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -144,10 +144,8 @@ export class $RefParser = ParserOptions>( - schema: S | string | unknown, - ): Promise; - public static parse = ParserOptions>( + public static parse(schema: S | string | unknown): Promise; + public static parse( schema: S | string | unknown, callback: SchemaCallback, ): Promise; @@ -269,10 +267,8 @@ export class $RefParser = ParserOptions>( - schema: S | string | unknown, - ): Promise; - public static bundle = ParserOptions>( + public static bundle(schema: S | string | unknown): Promise; + public static bundle( schema: S | string | unknown, callback: SchemaCallback, ): Promise; @@ -343,10 +339,8 @@ export class $RefParser = ParserOptions>( - schema: S | string | unknown, - ): Promise; - public static dereference = ParserOptions>( + public static dereference(schema: S | string | unknown): Promise; + public static dereference( schema: S | string | unknown, callback: SchemaCallback, ): Promise; diff --git a/lib/util/plugins.ts b/lib/util/plugins.ts index 240cba05..dae5a97d 100644 --- a/lib/util/plugins.ts +++ b/lib/util/plugins.ts @@ -45,8 +45,7 @@ export function sort(plugins: Plugin[]) { }); } -// @ts-ignore -export interface PluginResult = ParserOptions> { +export interface PluginResult { plugin: Plugin; result?: string | Buffer | S; error?: any; @@ -67,10 +66,10 @@ export async function run, ) { let plugin: Plugin; - let lastError: PluginResult; + let lastError: PluginResult; let index = 0; - return new Promise>((resolve, reject) => { + return new Promise>((resolve, reject) => { runNextPlugin(); function runNextPlugin() { @@ -97,7 +96,7 @@ export async function run["error"], result: PluginResult["result"]) { + function callback(err: PluginResult["error"], result: PluginResult["result"]) { if (err) { onError(err); } else { @@ -105,7 +104,7 @@ export async function run["result"]) { + function onSuccess(result: PluginResult["result"]) { // console.log(' success'); resolve({ plugin, @@ -113,7 +112,7 @@ export async function run["error"]) { + function onError(error: PluginResult["error"]) { // console.log(' %s', err.message || err); lastError = { plugin, diff --git a/package.json b/package.json index 11e1b20c..c72d2b13 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,18 @@ "name": "@apidevtools/json-schema-ref-parser", "version": "0.0.0-dev", "description": "Parse, Resolve, and Dereference JSON Schema $ref pointers", + "scripts": { + "prepublishOnly": "yarn build", + "lint": "eslint lib", + "build": "rimraf dist && tsc", + "typecheck": "tsc --noEmit", + "prettier": "prettier --write \"**/*.+(js|jsx|ts|tsx|har||json|css|md)\"", + "test": "vitest --coverage", + "test:node": "yarn test", + "test:browser": "cross-env BROWSER=\"true\" yarn test", + "test:update": "vitest -u", + "test:watch": "vitest -w" + }, "keywords": [ "json", "schema", @@ -54,18 +66,6 @@ "dist", "cjs" ], - "scripts": { - "prepublishOnly": "yarn build", - "lint": "eslint lib", - "build": "rimraf dist && tsc", - "typecheck": "tsc --noEmit", - "prettier": "prettier --write \"**/*.+(js|jsx|ts|tsx|har||json|css|md)\"", - "test": "vitest --coverage", - "test:node": "yarn test", - "test:browser": "cross-env BROWSER=\"true\" yarn test", - "test:update": "vitest -u", - "test:watch": "vitest -w" - }, "devDependencies": { "@eslint/compat": "^1.2.5", "@eslint/js": "^9.18.0", diff --git a/test/specs/empty/empty.spec.ts b/test/specs/empty/empty.spec.ts index 103f6845..f560655c 100644 --- a/test/specs/empty/empty.spec.ts +++ b/test/specs/empty/empty.spec.ts @@ -10,7 +10,6 @@ describe("Empty schema", () => { const parser = new $RefParser(); const schema = await parser.parse(path.rel("test/specs/empty/empty.json")); expect(schema).to.be.an("object"); - expect(schema).to.be.empty; expect(parser.schema).to.equal(schema); expect(parser.$refs.paths()).to.deep.equal([path.abs("test/specs/empty/empty.json")]); }); @@ -24,7 +23,6 @@ describe("Empty schema", () => { const parser = new $RefParser(); const schema = await parser.dereference(path.rel("test/specs/empty/empty.json")); expect(schema).to.be.an("object"); - expect(schema).to.be.empty; expect(parser.schema).to.equal(schema); expect(parser.$refs.paths()).to.deep.equal([path.abs("test/specs/empty/empty.json")]); // The "circular" flag should NOT be set @@ -35,7 +33,6 @@ describe("Empty schema", () => { const parser = new $RefParser(); const schema = await parser.bundle(path.rel("test/specs/empty/empty.json")); expect(schema).to.be.an("object"); - expect(schema).to.be.empty; expect(parser.schema).to.equal(schema); expect(parser.$refs.paths()).to.deep.equal([path.abs("test/specs/empty/empty.json")]); }); diff --git a/test/specs/http.spec.ts b/test/specs/http.spec.ts index 9f76860c..f2802820 100644 --- a/test/specs/http.spec.ts +++ b/test/specs/http.spec.ts @@ -155,7 +155,6 @@ describe("HTTP options", () => { const schema = await parser.parse("https://petstore.swagger.io/v2/swagger.json"); expect(schema).to.be.an("object"); - expect(schema).not.to.be.empty; expect(parser.schema).to.equal(schema); }); @@ -170,7 +169,6 @@ describe("HTTP options", () => { }); expect(schema).to.be.an("object"); - expect(schema).not.to.be.empty; expect(parser.schema).to.equal(schema); }); @@ -188,7 +186,6 @@ describe("HTTP options", () => { // The request succeeded, which means this browser doesn't support CORS. expect(schema).to.be.an("object"); - expect(schema).not.to.be.empty; expect(parser.schema).to.equal(schema); } catch (err) { // The request failed, which is expected diff --git a/test/specs/missing-pointers/missing-pointers.spec.ts b/test/specs/missing-pointers/missing-pointers.spec.ts index b0cf5e61..0cdd5324 100644 --- a/test/specs/missing-pointers/missing-pointers.spec.ts +++ b/test/specs/missing-pointers/missing-pointers.spec.ts @@ -94,22 +94,25 @@ describe("Schema with missing pointers", () => { it("should throw an missing pointer error with details for target and parent", async () => { const parser = new $RefParser(); try { - await parser.dereference({ foo: { $ref: path.abs("test/specs/missing-pointers/error-details.yaml") }}, { continueOnError: true }); + await parser.dereference( + { foo: { $ref: path.abs("test/specs/missing-pointers/error-details.yaml") } }, + { continueOnError: true }, + ); helper.shouldNotGetCalled(); - } - catch (err) { + } catch (err: any) { expect(err).to.be.instanceof(JSONParserErrorGroup); expect(err.files).to.equal(parser); expect(err.message).to.have.string("1 error occurred while reading '"); expect(err.errors).to.containSubset([ { name: MissingPointerError.name, - message: 'Missing $ref pointer \"#/components/parameters/ThisIsMissing\". Token \"ThisIsMissing\" does not exist.', - targetToken: 'ThisIsMissing', + message: + 'Missing $ref pointer "#/components/parameters/ThisIsMissing". Token "ThisIsMissing" does not exist.', + targetToken: "ThisIsMissing", targetRef: "#/components/parameters/ThisIsMissing", targetFound: "#/components/parameters", - parentPath: "#/paths/~1pet/post/parameters/0" - } + parentPath: "#/paths/~1pet/post/parameters/0", + }, ]); } }); diff --git a/test/specs/ref-in-excluded-path/ref-in-excluded-path.spec.ts b/test/specs/ref-in-excluded-path/ref-in-excluded-path.spec.ts index 2e55bf16..1ba430de 100644 --- a/test/specs/ref-in-excluded-path/ref-in-excluded-path.spec.ts +++ b/test/specs/ref-in-excluded-path/ref-in-excluded-path.spec.ts @@ -12,7 +12,7 @@ describe("Schema with literal $refs in examples", () => { const schema = await parser.dereference(path.rel("test/specs/ref-in-excluded-path/ref-in-excluded-path.yaml"), { dereference: { excludedPathMatcher: (schemaPath: any) => { - return /\/example(\/|$|s\/[^\/]+\/value(\/|$))/.test(schemaPath); + return /\/example(\/|$|s\/[^/]+\/value(\/|$))/.test(schemaPath); }, }, }); diff --git a/test/specs/refs.spec.ts b/test/specs/refs.spec.ts index 45bee16f..fcf88547 100644 --- a/test/specs/refs.spec.ts +++ b/test/specs/refs.spec.ts @@ -134,7 +134,7 @@ describe("$Refs object", () => { values = $refs.values(); expect(values).to.deep.equal(expected); } else { - expect(values).to.be.an("object").and.empty; + expect(values).to.be.an("object"); } }); @@ -154,7 +154,7 @@ describe("$Refs object", () => { values = $refs.values(); expect(values).to.deep.equal(expected); } else { - expect(values).to.be.an("object").and.empty; + expect(values).to.be.an("object"); } }); }); diff --git a/test/utils/helper.ts b/test/utils/helper.ts index d6c123e4..00bdfb17 100644 --- a/test/utils/helper.ts +++ b/test/utils/helper.ts @@ -18,7 +18,7 @@ const helper = { * @param {...*} [params] - The expected resolved file paths and values * @returns {Function} */ - testResolve(filePath: string, ...params: (string | undefined | object)[]) { + testResolve(filePath: string) { const parsedSchema = arguments[2]; const expectedFiles: any = []; const messages: any = []; diff --git a/test/utils/path.ts b/test/utils/path.ts index 4ae5a42f..099243c8 100644 --- a/test/utils/path.ts +++ b/test/utils/path.ts @@ -138,22 +138,22 @@ function urlPathHelpers() { } export default { - rel(file: any) { + rel() { // @ts-expect-error TS(2556): A spread argument must either have a tuple type or... Remove this comment to see the full error message return !isDom ? pathHelpers.filesystem.rel(...arguments) : pathHelpers.url.rel(...arguments); }, - abs(file: any) { + abs() { // @ts-expect-error TS(2556): A spread argument must either have a tuple type or... Remove this comment to see the full error message return !isDom ? pathHelpers.filesystem.abs(...arguments) : pathHelpers.url.abs(...arguments); }, - unixify(file: any) { + unixify() { // @ts-expect-error TS(2556): A spread argument must either have a tuple type or... Remove this comment to see the full error message return !isDom ? pathHelpers.filesystem.unixify(...arguments) : pathHelpers.url.unixify(...arguments); }, - url(file: any) { + url() { // @ts-expect-error TS(2556): A spread argument must either have a tuple type or... Remove this comment to see the full error message return !isDom ? pathHelpers.filesystem.url(...arguments) : pathHelpers.url.url(...arguments); },