From 71ba6cc8ad4fbcc12dad318228dea0dfeec0a2f1 Mon Sep 17 00:00:00 2001 From: JonLuca DeCaro Date: Wed, 6 Mar 2024 14:59:40 -0800 Subject: [PATCH] fix(types): add correct extends in all places, make all generic --- lib/bundle.ts | 4 ++-- lib/normalize-args.ts | 2 +- lib/options.ts | 6 ++++-- lib/pointer.ts | 2 +- lib/ref.ts | 4 ++-- lib/refs.ts | 6 +++--- lib/resolvers/http.ts | 4 ++-- lib/types/index.ts | 8 ++++---- lib/util/errors.ts | 2 +- lib/util/plugins.ts | 10 +++++----- 10 files changed, 25 insertions(+), 23 deletions(-) diff --git a/lib/bundle.ts b/lib/bundle.ts index ff76fbdb..7aed5a6b 100644 --- a/lib/bundle.ts +++ b/lib/bundle.ts @@ -40,7 +40,7 @@ function bundle( +function crawl( parent: any, key: string | null, path: string, @@ -102,7 +102,7 @@ function crawl( * @param $refs * @param options */ -function inventory$Ref( +function inventory$Ref( $refParent: any, $refKey: any, path: string, diff --git a/lib/normalize-args.ts b/lib/normalize-args.ts index 2e928e76..8a0f3ca4 100644 --- a/lib/normalize-args.ts +++ b/lib/normalize-args.ts @@ -4,7 +4,7 @@ import type { JSONSchema, SchemaCallback } from "./types"; // I really dislike this function and the way it's written. It's not clear what it's doing, and it's way too flexible // In the future, I'd like to deprecate the api and accept only named parameters in index.ts -export interface NormalizedArguments { +export interface NormalizedArguments { path: string; schema: S; options: O & Options; diff --git a/lib/options.ts b/lib/options.ts index 1dc15ff1..7d4e1415 100644 --- a/lib/options.ts +++ b/lib/options.ts @@ -53,7 +53,7 @@ export interface DereferenceOptions { * @param [options] - Overridden options * @class */ -export interface $RefParserOptions { +export interface $RefParserOptions { /** * The `parse` options determine how different types of files will be parsed. * @@ -174,7 +174,9 @@ export const getJsonSchemaRefParserDefaultOptions = () => { return defaults; }; -export const getNewOptions = (options: O | undefined): O & $RefParserOptions => { +export const getNewOptions = ( + options: O | undefined, +): O & $RefParserOptions => { const newOptions = getJsonSchemaRefParserDefaultOptions(); if (options) { merge(newOptions, options); diff --git a/lib/pointer.ts b/lib/pointer.ts index fef7b237..c6cea28b 100644 --- a/lib/pointer.ts +++ b/lib/pointer.ts @@ -26,7 +26,7 @@ const safeDecodeURIComponent = (encodedURIComponent: string): string => { * @param [friendlyPath] - The original user-specified path (used for error messages) * @class */ -class Pointer { +class Pointer { /** * The {@link $Ref} object that contains this {@link Pointer} object. */ diff --git a/lib/ref.ts b/lib/ref.ts index 3f0a1573..c026ac6a 100644 --- a/lib/ref.ts +++ b/lib/ref.ts @@ -14,7 +14,7 @@ export type $RefError = JSONParserError | ResolverError | ParserError | MissingP * * @class */ -class $Ref { +class $Ref { /** * The file path or URL of the referenced file. * This path is relative to the path of the main JSON schema file. @@ -267,7 +267,7 @@ class $Ref { * @param resolvedValue - The resolved value, which can be any type * @returns - Returns the dereferenced value */ - static dereference($ref: $Ref, resolvedValue: S): S { + static dereference($ref: $Ref, resolvedValue: S): S { if (resolvedValue && typeof resolvedValue === "object" && $Ref.isExtended$Ref($ref)) { const merged = {}; for (const key of Object.keys($ref)) { diff --git a/lib/refs.ts b/lib/refs.ts index 8105d926..dc40dc27 100644 --- a/lib/refs.ts +++ b/lib/refs.ts @@ -6,7 +6,7 @@ import type $RefParserOptions from "./options.js"; import convertPathToPosix from "./util/convert-path-to-posix"; import type { JSONSchema } from "./types"; -interface $RefsMap { +interface $RefsMap { [url: string]: $Ref; } /** @@ -16,7 +16,7 @@ interface $RefsMap { * * See https://apitools.dev/json-schema-ref-parser/docs/refs.html */ -export default class $Refs { +export default class $Refs { /** * This property is true if the schema contains any circular references. You may want to check this property before serializing the dereferenced schema as JSON, since JSON.stringify() does not support circular references by default. * @@ -215,7 +215,7 @@ export default class $Refs { * @param [types] - Only return paths of the given types ("file", "http", etc.) * @returns */ -function getPaths($refs: $RefsMap, types: string[]) { +function getPaths($refs: $RefsMap, types: string[]) { let paths = Object.keys($refs); // Filter the paths by type diff --git a/lib/resolvers/http.ts b/lib/resolvers/http.ts index 77885b98..faf8f569 100644 --- a/lib/resolvers/http.ts +++ b/lib/resolvers/http.ts @@ -66,7 +66,7 @@ export default { * @returns * The promise resolves with the raw downloaded data, or rejects if there is an HTTP error. */ -async function download( +async function download( u: URL | string, httpOptions: HTTPResolverOptions, _redirects?: string[], @@ -109,7 +109,7 @@ async function download( * Sends an HTTP GET request. * The promise resolves with the HTTP Response object. */ -async function get(u: RequestInfo | URL, httpOptions: HTTPResolverOptions) { +async function get(u: RequestInfo | URL, httpOptions: HTTPResolverOptions) { let controller: any; let timeoutId: any; if (httpOptions.timeout) { diff --git a/lib/types/index.ts b/lib/types/index.ts index 36754fc0..66e77448 100644 --- a/lib/types/index.ts +++ b/lib/types/index.ts @@ -10,14 +10,14 @@ import type $Refs from "../refs.js"; export type JSONSchema = JSONSchema4 | JSONSchema6 | JSONSchema7; export type JSONSchemaObject = JSONSchema4Object | JSONSchema6Object | JSONSchema7Object; -export type SchemaCallback = (err: Error | null, schema?: S | object | null) => any; -export type $RefsCallback = (err: Error | null, $refs?: $Refs) => any; +export type SchemaCallback = (err: Error | null, schema?: S | object | null) => any; +export type $RefsCallback = (err: Error | null, $refs?: $Refs) => any; /** * See https://apitools.dev/json-schema-ref-parser/docs/options.html */ -export interface HTTPResolverOptions extends Partial> { +export interface HTTPResolverOptions extends Partial> { /** * You can specify any HTTP headers that should be sent when downloading files. For example, some servers may require you to set the `Accept` or `Referrer` header. */ @@ -44,7 +44,7 @@ export interface HTTPResolverOptions extends Partial { +export interface ResolverOptions { name?: string; /** * All resolvers have an order property, even the built-in resolvers. If you don't specify an order property, then your resolver will run last. Specifying `order: 1`, like we did in this example, will make your resolver run first. Or you can squeeze your resolver in-between some of the built-in resolvers. For example, `order: 101` would make it run after the file resolver, but before the HTTP resolver. You can see the order of all the built-in resolvers by looking at their source code. diff --git a/lib/util/errors.ts b/lib/util/errors.ts index e4e172a7..db8c17c4 100644 --- a/lib/util/errors.ts +++ b/lib/util/errors.ts @@ -60,7 +60,7 @@ export class JSONParserErrorGroup< ) { const errors = []; - for (const $ref of Object.values(parser.$refs._$refs) as $Ref[]) { + for (const $ref of Object.values(parser.$refs._$refs) as $Ref[]) { if ($ref.errors) { errors.push(...$ref.errors); } diff --git a/lib/util/plugins.ts b/lib/util/plugins.ts index 7768fbd8..a5baeff0 100644 --- a/lib/util/plugins.ts +++ b/lib/util/plugins.ts @@ -1,4 +1,4 @@ -import type { FileInfo } from "../types/index.js"; +import type { FileInfo, JSONSchema } from "../types/index.js"; import type $RefParserOptions from "../options.js"; import type { ResolverOptions } from "../types/index.js"; import type $Refs from "../refs.js"; @@ -10,7 +10,7 @@ import type { Plugin } from "../types/index.js"; * * @returns */ -export function all(plugins: $RefParserOptions["resolve"]): Plugin[] { +export function all(plugins: $RefParserOptions["resolve"]): Plugin[] { return Object.keys(plugins) .filter((key) => { return typeof plugins[key] === "object"; @@ -43,7 +43,7 @@ export function sort(plugins: Plugin[]) { }); } -export interface PluginResult { +export interface PluginResult { plugin: Plugin; result?: string | Buffer | S; error?: any; @@ -57,7 +57,7 @@ export interface PluginResult { * If the promise rejects, or the callback is called with an error, then the next plugin is called. * If ALL plugins fail, then the last error is thrown. */ -export async function run( +export async function run( plugins: Plugin[], method: keyof Plugin | keyof ResolverOptions, file: FileInfo, @@ -127,7 +127,7 @@ export async function run( * If the value is a RegExp, then it will be tested against the file URL. * If the value is an array, then it will be compared against the file extension. */ -function getResult( +function getResult( obj: Plugin, prop: keyof Plugin | keyof ResolverOptions, file: FileInfo,