Skip to content

Commit

Permalink
fix(exports): export more types for usage in other libraries, fix som…
Browse files Browse the repository at this point in the history
…e types
  • Loading branch information
jonluca committed Mar 6, 2024
1 parent fb49854 commit 56241ad
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 17 deletions.
27 changes: 17 additions & 10 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,7 @@ import maybe from "./util/maybe.js";
import type { ParserOptions } from "./options.js";
import type { $RefsCallback, JSONSchema, SchemaCallback } from "./types/index.js";

export { JSONParserError };
export { InvalidPointerError };
export { MissingPointerError };
export { ResolverError };
export { ParserError };
export { UnmatchedParserError };
export { UnmatchedResolverError };

type RefParserSchema = string | JSONSchema;
export type RefParserSchema = string | JSONSchema;

/**
* This class parses a JSON schema, builds a map of its JSON references and their resolved values,
Expand Down Expand Up @@ -76,7 +68,6 @@ export class $RefParser {
options: ParserOptions,
callback: SchemaCallback,
): Promise<void>;

async parse() {
const args = normalizeArgs(arguments as any);
let promise;
Expand Down Expand Up @@ -418,3 +409,19 @@ export const parse = $RefParser.parse;
export const resolve = $RefParser.resolve;
export const bundle = $RefParser.bundle;
export const dereference = $RefParser.dereference;

export {
UnmatchedResolverError,
JSONParserError,
JSONSchema,
InvalidPointerError,
MissingPointerError,
ResolverError,
ParserError,
UnmatchedParserError,
ParserOptions,
$RefsCallback,
isHandledError,
JSONParserErrorGroup,
SchemaCallback,
};
12 changes: 11 additions & 1 deletion lib/normalize-args.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import { getNewOptions } from "./options.js";
import type { JSONSchema, SchemaCallback } from "./types";
import type $RefParserOptions from "./options";

export default normalizeArgs;

// 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 {
path: string;
schema: JSONSchema;
options: $RefParserOptions;
callback: SchemaCallback;
}
/**
* Normalizes the given arguments, accounting for optional args.
*/
function normalizeArgs(_args: Partial<IArguments>) {
function normalizeArgs(_args: Partial<IArguments>): NormalizedArguments {
let path, schema, options, callback;
const args = Array.prototype.slice.call(_args) as any[];

Expand Down
10 changes: 5 additions & 5 deletions lib/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import httpResolver from "./resolvers/http.js";

import type { HTTPResolverOptions, JSONSchemaObject, Plugin, ResolverOptions } from "./types/index.js";

type DeepPartial<T> = T extends object
export type DeepPartial<T> = T extends object
? {
[P in keyof T]?: DeepPartial<T[P]>;
}
Expand All @@ -18,7 +18,7 @@ type DeepPartial<T> = T extends object
* @param [options] - Overridden options
* @class
*/
interface $RefParserOptions {
export interface $RefParserOptions {
/**
* The `parse` options determine how different types of files will be parsed.
*
Expand Down Expand Up @@ -101,7 +101,7 @@ interface $RefParserOptions {
};
}

const getDefaults = () => {
export const getJsonSchemaRefParserDefaultOptions = () => {
const defaults = {
/**
* Determines how different types of files will be parsed.
Expand Down Expand Up @@ -172,8 +172,8 @@ const getDefaults = () => {
return defaults;
};

export const getNewOptions = (options: DeepPartial<$RefParserOptions>): $RefParserOptions => {
const newOptions = getDefaults();
export const getNewOptions = (options: DeepPartial<$RefParserOptions> | undefined): $RefParserOptions => {
const newOptions = getJsonSchemaRefParserDefaultOptions();
if (options) {
merge(newOptions, options);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ 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?: JSONSchema | object) => any;
export type SchemaCallback = (err: Error | null, schema?: JSONSchema | object | null) => any;
export type $RefsCallback = (err: Error | null, $refs?: $Refs) => any;

/**
Expand Down

0 comments on commit 56241ad

Please sign in to comment.