Skip to content

Commit

Permalink
fix(types): add correct extends in all places, make all generic
Browse files Browse the repository at this point in the history
  • Loading branch information
jonluca committed Mar 6, 2024
1 parent 173fe97 commit 71ba6cc
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 23 deletions.
4 changes: 2 additions & 2 deletions lib/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions = Par
* @param $refs
* @param options
*/
function crawl<S, O>(
function crawl<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
parent: any,
key: string | null,
path: string,
Expand Down Expand Up @@ -102,7 +102,7 @@ function crawl<S, O>(
* @param $refs
* @param options
*/
function inventory$Ref<S, O>(
function inventory$Ref<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
$refParent: any,
$refKey: any,
path: string,
Expand Down
2 changes: 1 addition & 1 deletion lib/normalize-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<S, O> {
export interface NormalizedArguments<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions> {
path: string;
schema: S;
options: O & Options;
Expand Down
6 changes: 4 additions & 2 deletions lib/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export interface DereferenceOptions {
* @param [options] - Overridden options
* @class
*/
export interface $RefParserOptions<S> {
export interface $RefParserOptions<S extends JSONSchema = JSONSchema> {
/**
* The `parse` options determine how different types of files will be parsed.
*
Expand Down Expand Up @@ -174,7 +174,9 @@ export const getJsonSchemaRefParserDefaultOptions = () => {
return defaults;
};

export const getNewOptions = <S, O>(options: O | undefined): O & $RefParserOptions<S> => {
export const getNewOptions = <S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
options: O | undefined,
): O & $RefParserOptions<S> => {
const newOptions = getJsonSchemaRefParserDefaultOptions();
if (options) {
merge(newOptions, options);
Expand Down
2 changes: 1 addition & 1 deletion lib/pointer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const safeDecodeURIComponent = (encodedURIComponent: string): string => {
* @param [friendlyPath] - The original user-specified path (used for error messages)
* @class
*/
class Pointer<S = JSONSchema> {
class Pointer<S extends JSONSchema = JSONSchema> {
/**
* The {@link $Ref} object that contains this {@link Pointer} object.
*/
Expand Down
4 changes: 2 additions & 2 deletions lib/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type $RefError = JSONParserError | ResolverError | ParserError | MissingP
*
* @class
*/
class $Ref<S = JSONSchema> {
class $Ref<S extends JSONSchema = JSONSchema> {
/**
* The file path or URL of the referenced file.
* This path is relative to the path of the main JSON schema file.
Expand Down Expand Up @@ -267,7 +267,7 @@ class $Ref<S = JSONSchema> {
* @param resolvedValue - The resolved value, which can be any type
* @returns - Returns the dereferenced value
*/
static dereference<S>($ref: $Ref<S>, resolvedValue: S): S {
static dereference<S extends JSONSchema = JSONSchema>($ref: $Ref<S>, resolvedValue: S): S {
if (resolvedValue && typeof resolvedValue === "object" && $Ref.isExtended$Ref($ref)) {
const merged = {};
for (const key of Object.keys($ref)) {
Expand Down
6 changes: 3 additions & 3 deletions lib/refs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<S> {
interface $RefsMap<S extends JSONSchema = JSONSchema> {
[url: string]: $Ref<S>;
}
/**
Expand All @@ -16,7 +16,7 @@ interface $RefsMap<S> {
*
* See https://apitools.dev/json-schema-ref-parser/docs/refs.html
*/
export default class $Refs<S = JSONSchema> {
export default class $Refs<S extends JSONSchema = JSONSchema> {
/**
* 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.
*
Expand Down Expand Up @@ -215,7 +215,7 @@ export default class $Refs<S = JSONSchema> {
* @param [types] - Only return paths of the given types ("file", "http", etc.)
* @returns
*/
function getPaths<S>($refs: $RefsMap<S>, types: string[]) {
function getPaths<S extends JSONSchema = JSONSchema>($refs: $RefsMap<S>, types: string[]) {
let paths = Object.keys($refs);

// Filter the paths by type
Expand Down
4 changes: 2 additions & 2 deletions lib/resolvers/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<S>(
async function download<S extends JSONSchema = JSONSchema>(
u: URL | string,
httpOptions: HTTPResolverOptions<S>,
_redirects?: string[],
Expand Down Expand Up @@ -109,7 +109,7 @@ async function download<S>(
* Sends an HTTP GET request.
* The promise resolves with the HTTP Response object.
*/
async function get<S>(u: RequestInfo | URL, httpOptions: HTTPResolverOptions<S>) {
async function get<S extends JSONSchema = JSONSchema>(u: RequestInfo | URL, httpOptions: HTTPResolverOptions<S>) {
let controller: any;
let timeoutId: any;
if (httpOptions.timeout) {
Expand Down
8 changes: 4 additions & 4 deletions lib/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<S = JSONSchema> = (err: Error | null, schema?: S | object | null) => any;
export type $RefsCallback<S = JSONSchema> = (err: Error | null, $refs?: $Refs<S>) => any;
export type SchemaCallback<S extends JSONSchema = JSONSchema> = (err: Error | null, schema?: S | object | null) => any;
export type $RefsCallback<S extends JSONSchema = JSONSchema> = (err: Error | null, $refs?: $Refs<S>) => any;

/**
* See https://apitools.dev/json-schema-ref-parser/docs/options.html
*/

export interface HTTPResolverOptions<S = JSONSchema> extends Partial<ResolverOptions<S>> {
export interface HTTPResolverOptions<S extends JSONSchema = JSONSchema> extends Partial<ResolverOptions<S>> {
/**
* 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.
*/
Expand All @@ -44,7 +44,7 @@ export interface HTTPResolverOptions<S = JSONSchema> extends Partial<ResolverOpt
*
* See https://apitools.dev/json-schema-ref-parser/docs/plugins/resolvers.html
*/
export interface ResolverOptions<S = JSONSchema> {
export interface ResolverOptions<S extends JSONSchema = JSONSchema> {
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.
Expand Down
2 changes: 1 addition & 1 deletion lib/util/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class JSONParserErrorGroup<
) {
const errors = [];

for (const $ref of Object.values(parser.$refs._$refs) as $Ref<unknown>[]) {
for (const $ref of Object.values(parser.$refs._$refs) as $Ref<S>[]) {
if ($ref.errors) {
errors.push(...$ref.errors);
}
Expand Down
10 changes: 5 additions & 5 deletions lib/util/plugins.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -10,7 +10,7 @@ import type { Plugin } from "../types/index.js";
*
* @returns
*/
export function all<S>(plugins: $RefParserOptions<S>["resolve"]): Plugin[] {
export function all<S extends JSONSchema = JSONSchema>(plugins: $RefParserOptions<S>["resolve"]): Plugin[] {
return Object.keys(plugins)
.filter((key) => {
return typeof plugins[key] === "object";
Expand Down Expand Up @@ -43,7 +43,7 @@ export function sort(plugins: Plugin[]) {
});
}

export interface PluginResult<S> {
export interface PluginResult<S extends JSONSchema = JSONSchema> {
plugin: Plugin;
result?: string | Buffer | S;
error?: any;
Expand All @@ -57,7 +57,7 @@ export interface PluginResult<S> {
* 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<S>(
export async function run<S extends JSONSchema = JSONSchema>(
plugins: Plugin[],
method: keyof Plugin | keyof ResolverOptions<S>,
file: FileInfo,
Expand Down Expand Up @@ -127,7 +127,7 @@ export async function run<S>(
* 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<S>(
function getResult<S extends JSONSchema = JSONSchema>(
obj: Plugin,
prop: keyof Plugin | keyof ResolverOptions<S>,
file: FileInfo,
Expand Down

0 comments on commit 71ba6cc

Please sign in to comment.