@shipworthy/openapi-parser 0.0.0-stage → 0.0.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/LICENSE +21 -0
- package/README.md +361 -2
- package/dist/bundle.d.ts +973 -0
- package/dist/bundle.js +11 -0
- package/dist/codec.d.ts +2188 -0
- package/dist/codec.js +42 -0
- package/dist/document.d.ts +9 -0
- package/dist/document.js +28 -0
- package/dist/errors.d.ts +105 -0
- package/dist/errors.js +162 -0
- package/dist/hoist.d.ts +10 -0
- package/dist/hoist.js +444 -0
- package/dist/http.d.ts +2 -0
- package/dist/http.js +163 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +2 -0
- package/dist/load.d.ts +17 -0
- package/dist/load.js +128 -0
- package/dist/loader.d.ts +11 -0
- package/dist/loader.js +89 -0
- package/dist/parsers.d.ts +5 -0
- package/dist/parsers.js +90 -0
- package/dist/pipeline.d.ts +3 -0
- package/dist/pipeline.js +446 -0
- package/dist/plugins.d.ts +95 -0
- package/dist/plugins.js +61 -0
- package/dist/pointer.d.ts +7 -0
- package/dist/pointer.js +80 -0
- package/dist/run.d.ts +17 -0
- package/dist/run.js +313 -0
- package/dist/schemas/3.1/callback.d.ts +205 -0
- package/dist/schemas/3.1/callback.js +4 -0
- package/dist/schemas/3.1/components.d.ts +801 -0
- package/dist/schemas/3.1/components.js +25 -0
- package/dist/schemas/3.1/contact.d.ts +7 -0
- package/dist/schemas/3.1/contact.js +7 -0
- package/dist/schemas/3.1/document.d.ts +1093 -0
- package/dist/schemas/3.1/document.js +25 -0
- package/dist/schemas/3.1/encoding.d.ts +14 -0
- package/dist/schemas/3.1/encoding.js +11 -0
- package/dist/schemas/3.1/example.d.ts +8 -0
- package/dist/schemas/3.1/example.js +10 -0
- package/dist/schemas/3.1/external-docs.d.ts +6 -0
- package/dist/schemas/3.1/external-docs.js +6 -0
- package/dist/schemas/3.1/header.d.ts +194 -0
- package/dist/schemas/3.1/header.js +29 -0
- package/dist/schemas/3.1/http-method.d.ts +4 -0
- package/dist/schemas/3.1/http-method.js +12 -0
- package/dist/schemas/3.1/index.d.ts +29 -0
- package/dist/schemas/3.1/index.js +29 -0
- package/dist/schemas/3.1/info.d.ts +19 -0
- package/dist/schemas/3.1/info.js +13 -0
- package/dist/schemas/3.1/license.d.ts +7 -0
- package/dist/schemas/3.1/license.js +9 -0
- package/dist/schemas/3.1/link.d.ts +18 -0
- package/dist/schemas/3.1/link.js +13 -0
- package/dist/schemas/3.1/media-type.d.ts +27 -0
- package/dist/schemas/3.1/media-type.js +14 -0
- package/dist/schemas/3.1/oauth-flow.d.ts +8 -0
- package/dist/schemas/3.1/oauth-flow.js +8 -0
- package/dist/schemas/3.1/oauth-flows.d.ts +28 -0
- package/dist/schemas/3.1/oauth-flows.js +13 -0
- package/dist/schemas/3.1/object.d.ts +5 -0
- package/dist/schemas/3.1/object.js +17 -0
- package/dist/schemas/3.1/operation.d.ts +1040 -0
- package/dist/schemas/3.1/operation.js +25 -0
- package/dist/schemas/3.1/parameter.d.ts +101 -0
- package/dist/schemas/3.1/parameter.js +33 -0
- package/dist/schemas/3.1/path-item.d.ts +127 -0
- package/dist/schemas/3.1/path-item.js +22 -0
- package/dist/schemas/3.1/paths.d.ts +126 -0
- package/dist/schemas/3.1/paths.js +4 -0
- package/dist/schemas/3.1/reference.d.ts +13 -0
- package/dist/schemas/3.1/reference.js +11 -0
- package/dist/schemas/3.1/request-body.d.ts +31 -0
- package/dist/schemas/3.1/request-body.js +8 -0
- package/dist/schemas/3.1/response.d.ts +149 -0
- package/dist/schemas/3.1/response.js +12 -0
- package/dist/schemas/3.1/responses.d.ts +152 -0
- package/dist/schemas/3.1/responses.js +7 -0
- package/dist/schemas/3.1/schema.d.ts +4 -0
- package/dist/schemas/3.1/schema.js +6 -0
- package/dist/schemas/3.1/security-requirement.d.ts +3 -0
- package/dist/schemas/3.1/security-requirement.js +2 -0
- package/dist/schemas/3.1/security-scheme.d.ts +49 -0
- package/dist/schemas/3.1/security-scheme.js +25 -0
- package/dist/schemas/3.1/server-variable.d.ts +7 -0
- package/dist/schemas/3.1/server-variable.js +7 -0
- package/dist/schemas/3.1/server.d.ts +11 -0
- package/dist/schemas/3.1/server.js +8 -0
- package/dist/schemas/3.1/tag.d.ts +10 -0
- package/dist/schemas/3.1/tag.js +8 -0
- package/dist/upgraders/3.0-to-3.1.d.ts +9 -0
- package/dist/upgraders/3.0-to-3.1.js +310 -0
- package/dist/url.d.ts +29 -0
- package/dist/url.js +255 -0
- package/dist/yaml.d.ts +6 -0
- package/dist/yaml.js +235 -0
- package/package.json +50 -4
package/dist/codec.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Effect, Schema, SchemaGetter, SchemaIssue, SchemaTransformation } from "effect";
|
|
2
|
+
import { decodeDocument } from "./document.js";
|
|
3
|
+
import { parseText } from "./parsers.js";
|
|
4
|
+
import { runEffect } from "./run.js";
|
|
5
|
+
import { ParsedOpenApi } from "./schemas/3.1/document.js";
|
|
6
|
+
const parseOptions = Schema.Struct({
|
|
7
|
+
sourceUrl: Schema.Union([Schema.String, Schema.URL]),
|
|
8
|
+
});
|
|
9
|
+
const sourceDocument = Schema.StructWithRest(Schema.Struct({
|
|
10
|
+
...ParsedOpenApi.schema.fields,
|
|
11
|
+
openapi: Schema.String.check(Schema.isPattern(/^3\.[01]\.\d+(?:[-+].*)?$/)),
|
|
12
|
+
}), ParsedOpenApi.records);
|
|
13
|
+
function prepareDocumentTransformation(options) {
|
|
14
|
+
// oxlint-disable-next-line effecttsgo/schema-sync -- The public synchronous codec factory validates options before returning a Schema.
|
|
15
|
+
const { sourceUrl } = Schema.decodeSync(parseOptions)(options);
|
|
16
|
+
const source = sourceUrl instanceof URL ? sourceUrl.href : sourceUrl;
|
|
17
|
+
return SchemaTransformation.makeTransformation({
|
|
18
|
+
decode: SchemaGetter.transformEffect(Effect.fnUntraced(function* (input, options) {
|
|
19
|
+
const document = yield* Effect.fromResult(decodeDocument(input)).pipe(Effect.mapError((error) => new SchemaIssue.InvalidValue({ message: error.message }, input, options)));
|
|
20
|
+
// Check the source before the bundler can normalize or remove malformed fields.
|
|
21
|
+
const checked = yield* Schema.decodeUnknownEffect(sourceDocument)(document, options).pipe(Effect.mapError((error) => error.issue));
|
|
22
|
+
const result = yield* runEffect({ path: source, schema: checked }).pipe(Effect.mapError((error) => new SchemaIssue.InvalidValue({
|
|
23
|
+
message: "source" in error ? `${error.source}: ${error.message}` : error.message,
|
|
24
|
+
}, input, options)));
|
|
25
|
+
return result.schema;
|
|
26
|
+
})),
|
|
27
|
+
encode: SchemaGetter.passthrough(),
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
/** Object input shares the preparation path without a JSON stringify/parse round trip. */
|
|
31
|
+
export function ParsedOpenApiFromDocument(options) {
|
|
32
|
+
return Schema.Unknown.pipe(Schema.decodeTo(Schema.Unknown, prepareDocumentTransformation(options)), Schema.decodeTo(ParsedOpenApi));
|
|
33
|
+
}
|
|
34
|
+
const documentTransformation = SchemaTransformation.transformEffect({
|
|
35
|
+
decode: (source, options) => parseText(source).pipe(Effect.mapError((error) => new SchemaIssue.InvalidValue({ message: error.message }, source, options))),
|
|
36
|
+
encode: (document, options) => Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown, { space: 2 }))(document, options).pipe(Effect.mapError((error) => error.issue)),
|
|
37
|
+
});
|
|
38
|
+
/** Parse JSON/YAML, bundle external references, upgrade 3.0 to 3.1, and validate the document. */
|
|
39
|
+
export function ParsedOpenApiFromString(options) {
|
|
40
|
+
const transformation = SchemaTransformation.composeTransformation(documentTransformation, prepareDocumentTransformation(options));
|
|
41
|
+
return Schema.String.pipe(Schema.decodeTo(Schema.Unknown, transformation), Schema.decodeTo(ParsedOpenApi));
|
|
42
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Result, Schema } from "effect";
|
|
2
|
+
import { UnsupportedVersion } from "./errors.ts";
|
|
3
|
+
/** An OpenAPI document: its `openapi` version, with every other field unchecked. */
|
|
4
|
+
export declare const OpenAPIDocument: Schema.StructWithRest<Schema.Struct<{
|
|
5
|
+
readonly openapi: Schema.String;
|
|
6
|
+
}>, readonly [Schema.$Record<Schema.String, Schema.Unknown>]>;
|
|
7
|
+
export type OpenAPIDocument = typeof OpenAPIDocument.Type;
|
|
8
|
+
/** An OpenAPI 3.x document, as loaded and before bundling. */
|
|
9
|
+
export declare function decodeDocument(document: unknown): Result.Result<OpenAPIDocument, UnsupportedVersion>;
|
package/dist/document.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Predicate, Result, Schema } from "effect";
|
|
2
|
+
import { UnsupportedVersion } from "./errors.js";
|
|
3
|
+
/** An OpenAPI document: its `openapi` version, with every other field unchecked. */
|
|
4
|
+
export const OpenAPIDocument = Schema.StructWithRest(Schema.Struct({ openapi: Schema.String }), [
|
|
5
|
+
Schema.Record(Schema.String, Schema.Unknown),
|
|
6
|
+
]);
|
|
7
|
+
const unsupportedFormat = "Unsupported schema format, expected `openapi: 3.x`";
|
|
8
|
+
/** Rejects anything but OpenAPI 3.x, with the messages users see. */
|
|
9
|
+
const openApi3 = Schema.Unknown.check(Schema.makeFilter((document) => {
|
|
10
|
+
if (!Predicate.isObject(document))
|
|
11
|
+
return unsupportedFormat;
|
|
12
|
+
if (document.swagger)
|
|
13
|
+
return "Unsupported Swagger version: 2.x. Use OpenAPI 3.x instead.";
|
|
14
|
+
const openapi = document.openapi;
|
|
15
|
+
if (typeof openapi !== "string" || openapi.length === 0)
|
|
16
|
+
return unsupportedFormat;
|
|
17
|
+
const version = Number.parseFloat(openapi);
|
|
18
|
+
if (Number.isNaN(version) || version < 3 || version >= 4) {
|
|
19
|
+
return `Unsupported OpenAPI version: ${openapi}`;
|
|
20
|
+
}
|
|
21
|
+
return undefined;
|
|
22
|
+
}));
|
|
23
|
+
const decodeVersion = Schema.decodeUnknownResult(openApi3);
|
|
24
|
+
const decodeEnvelope = Schema.decodeUnknownResult(OpenAPIDocument);
|
|
25
|
+
/** An OpenAPI 3.x document, as loaded and before bundling. */
|
|
26
|
+
export function decodeDocument(document) {
|
|
27
|
+
return decodeVersion(document).pipe(Result.flatMap(decodeEnvelope), Result.mapError((issue) => new UnsupportedVersion({ message: issue.message })));
|
|
28
|
+
}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
declare const ParserFailure_base: Schema.Class<ParserFailure, Schema.TaggedStruct<"ParserFailure", {
|
|
3
|
+
readonly message: Schema.String;
|
|
4
|
+
readonly source: Schema.String;
|
|
5
|
+
readonly cause: Schema.optionalKey<Schema.Defect>;
|
|
6
|
+
}>, import("effect/Cause").YieldableError>;
|
|
7
|
+
export declare class ParserFailure extends ParserFailure_base {
|
|
8
|
+
}
|
|
9
|
+
declare const ResolverFailure_base: Schema.Class<ResolverFailure, Schema.TaggedStruct<"ResolverFailure", {
|
|
10
|
+
readonly message: Schema.String;
|
|
11
|
+
readonly source: Schema.String;
|
|
12
|
+
readonly ioErrorCode: Schema.optionalKey<Schema.String>;
|
|
13
|
+
readonly cause: Schema.optionalKey<Schema.Defect>;
|
|
14
|
+
}>, import("effect/Cause").YieldableError>;
|
|
15
|
+
export declare class ResolverFailure extends ResolverFailure_base {
|
|
16
|
+
}
|
|
17
|
+
declare const UnmatchedParserFailure_base: Schema.Class<UnmatchedParserFailure, Schema.TaggedStruct<"UnmatchedParserFailure", {
|
|
18
|
+
message: Schema.String;
|
|
19
|
+
source: Schema.String;
|
|
20
|
+
}>, import("effect/Cause").YieldableError>;
|
|
21
|
+
export declare class UnmatchedParserFailure extends UnmatchedParserFailure_base {
|
|
22
|
+
}
|
|
23
|
+
declare const UnmatchedResolverFailure_base: Schema.Class<UnmatchedResolverFailure, Schema.TaggedStruct<"UnmatchedResolverFailure", {
|
|
24
|
+
message: Schema.String;
|
|
25
|
+
source: Schema.String;
|
|
26
|
+
}>, import("effect/Cause").YieldableError>;
|
|
27
|
+
export declare class UnmatchedResolverFailure extends UnmatchedResolverFailure_base {
|
|
28
|
+
}
|
|
29
|
+
declare const MissingPointerFailure_base: Schema.Class<MissingPointerFailure, Schema.TaggedStruct<"MissingPointerFailure", {
|
|
30
|
+
readonly message: Schema.String;
|
|
31
|
+
readonly source: Schema.String;
|
|
32
|
+
readonly token: Schema.String;
|
|
33
|
+
readonly targetRef: Schema.String;
|
|
34
|
+
readonly targetFound: Schema.String;
|
|
35
|
+
readonly parentPath: Schema.String;
|
|
36
|
+
}>, import("effect/Cause").YieldableError>;
|
|
37
|
+
export declare class MissingPointerFailure extends MissingPointerFailure_base {
|
|
38
|
+
}
|
|
39
|
+
declare const InvalidPointerFailure_base: Schema.Class<InvalidPointerFailure, Schema.TaggedStruct<"InvalidPointerFailure", {
|
|
40
|
+
readonly message: Schema.String;
|
|
41
|
+
readonly source: Schema.String;
|
|
42
|
+
readonly pointer: Schema.String;
|
|
43
|
+
}>, import("effect/Cause").YieldableError>;
|
|
44
|
+
export declare class InvalidPointerFailure extends InvalidPointerFailure_base {
|
|
45
|
+
}
|
|
46
|
+
declare const CircularAliasFailure_base: Schema.Class<CircularAliasFailure, Schema.TaggedStruct<"CircularAliasFailure", {
|
|
47
|
+
readonly message: Schema.String;
|
|
48
|
+
readonly definition: Schema.String;
|
|
49
|
+
}>, import("effect/Cause").YieldableError>;
|
|
50
|
+
export declare class CircularAliasFailure extends CircularAliasFailure_base {
|
|
51
|
+
}
|
|
52
|
+
declare const BundleFailure_base: Schema.Class<BundleFailure, Schema.TaggedStruct<"BundleFailure", {
|
|
53
|
+
readonly message: Schema.String;
|
|
54
|
+
}>, import("effect/Cause").YieldableError>;
|
|
55
|
+
export declare class BundleFailure extends BundleFailure_base {
|
|
56
|
+
}
|
|
57
|
+
declare const TimeoutFailure_base: Schema.Class<TimeoutFailure, Schema.TaggedStruct<"TimeoutFailure", {
|
|
58
|
+
readonly message: Schema.String;
|
|
59
|
+
readonly timeout: Schema.Finite;
|
|
60
|
+
}>, import("effect/Cause").YieldableError>;
|
|
61
|
+
export declare class TimeoutFailure extends TimeoutFailure_base {
|
|
62
|
+
}
|
|
63
|
+
declare const leafFailure: Schema.Union<readonly [typeof ParserFailure, typeof ResolverFailure, typeof UnmatchedParserFailure, typeof UnmatchedResolverFailure, typeof MissingPointerFailure, typeof InvalidPointerFailure, typeof CircularAliasFailure, typeof BundleFailure, typeof TimeoutFailure]>;
|
|
64
|
+
declare const GroupFailure_base: Schema.Class<GroupFailure, Schema.TaggedStruct<"GroupFailure", {
|
|
65
|
+
readonly source: Schema.String;
|
|
66
|
+
readonly message: Schema.String;
|
|
67
|
+
readonly errors: Schema.$Array<Schema.Union<readonly [typeof ParserFailure, typeof ResolverFailure, typeof UnmatchedParserFailure, typeof UnmatchedResolverFailure, typeof MissingPointerFailure, typeof InvalidPointerFailure, typeof CircularAliasFailure, typeof BundleFailure, typeof TimeoutFailure]>>;
|
|
68
|
+
}>, import("effect/Cause").YieldableError>;
|
|
69
|
+
export declare class GroupFailure extends GroupFailure_base {
|
|
70
|
+
}
|
|
71
|
+
export type LeafFailure = typeof leafFailure.Type;
|
|
72
|
+
export type TaggedFailure = LeafFailure | GroupFailure;
|
|
73
|
+
export declare const isTaggedFailure: <I>(input: I) => input is I & (BundleFailure | CircularAliasFailure | GroupFailure | InvalidPointerFailure | MissingPointerFailure | ParserFailure | ResolverFailure | TimeoutFailure | UnmatchedParserFailure | UnmatchedResolverFailure);
|
|
74
|
+
export declare function isLeafFailure(cause: TaggedFailure): cause is LeafFailure;
|
|
75
|
+
export declare function nodeErrorCode(cause: unknown): string | undefined;
|
|
76
|
+
export declare function missingPointerFailure(token: string, source: string, targetRef?: string, targetFound?: string, parentPath?: string): MissingPointerFailure;
|
|
77
|
+
export declare function invalidPointerFailure(pointer: string, source?: string): InvalidPointerFailure;
|
|
78
|
+
export declare function circularAliasFailure(definition: string): CircularAliasFailure;
|
|
79
|
+
export declare function bundleFailure(message: string): BundleFailure;
|
|
80
|
+
export declare function timeoutFailure(timeout: number): TimeoutFailure;
|
|
81
|
+
export declare function groupFailure(source: string, errors: ReadonlyArray<LeafFailure>): GroupFailure;
|
|
82
|
+
export declare function syncFailure(cause: unknown): TaggedFailure;
|
|
83
|
+
declare const EmptySource_base: Schema.Class<EmptySource, Schema.TaggedStruct<"EmptySource", {
|
|
84
|
+
readonly message: Schema.String;
|
|
85
|
+
}>, import("effect/Cause").YieldableError>;
|
|
86
|
+
export declare class EmptySource extends EmptySource_base {
|
|
87
|
+
}
|
|
88
|
+
declare const InvalidSource_base: Schema.Class<InvalidSource, Schema.TaggedStruct<"InvalidSource", {
|
|
89
|
+
readonly message: Schema.String;
|
|
90
|
+
}>, import("effect/Cause").YieldableError>;
|
|
91
|
+
export declare class InvalidSource extends InvalidSource_base {
|
|
92
|
+
}
|
|
93
|
+
declare const UnsupportedVersion_base: Schema.Class<UnsupportedVersion, Schema.TaggedStruct<"UnsupportedVersion", {
|
|
94
|
+
readonly message: Schema.String;
|
|
95
|
+
}>, import("effect/Cause").YieldableError>;
|
|
96
|
+
export declare class UnsupportedVersion extends UnsupportedVersion_base {
|
|
97
|
+
}
|
|
98
|
+
declare const ParseError_base: Schema.Class<ParseError, Schema.TaggedStruct<"ParseError", {
|
|
99
|
+
readonly message: Schema.String;
|
|
100
|
+
readonly cause: Schema.Defect;
|
|
101
|
+
}>, import("effect/Cause").YieldableError>;
|
|
102
|
+
export declare class ParseError extends ParseError_base {
|
|
103
|
+
}
|
|
104
|
+
export type LoadError = EmptySource | InvalidSource | UnsupportedVersion | ParseError;
|
|
105
|
+
export {};
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
const failureFields = {
|
|
3
|
+
message: Schema.String,
|
|
4
|
+
source: Schema.String,
|
|
5
|
+
};
|
|
6
|
+
export class ParserFailure extends Schema.TaggedError()("ParserFailure", {
|
|
7
|
+
...failureFields,
|
|
8
|
+
cause: Schema.optionalKey(Schema.Defect()),
|
|
9
|
+
}) {
|
|
10
|
+
}
|
|
11
|
+
export class ResolverFailure extends Schema.TaggedError()("ResolverFailure", {
|
|
12
|
+
...failureFields,
|
|
13
|
+
ioErrorCode: Schema.optionalKey(Schema.String),
|
|
14
|
+
cause: Schema.optionalKey(Schema.Defect()),
|
|
15
|
+
}) {
|
|
16
|
+
}
|
|
17
|
+
export class UnmatchedParserFailure extends Schema.TaggedError()("UnmatchedParserFailure", failureFields) {
|
|
18
|
+
}
|
|
19
|
+
export class UnmatchedResolverFailure extends Schema.TaggedError()("UnmatchedResolverFailure", failureFields) {
|
|
20
|
+
}
|
|
21
|
+
export class MissingPointerFailure extends Schema.TaggedError()("MissingPointerFailure", {
|
|
22
|
+
...failureFields,
|
|
23
|
+
token: Schema.String,
|
|
24
|
+
targetRef: Schema.String,
|
|
25
|
+
targetFound: Schema.String,
|
|
26
|
+
parentPath: Schema.String,
|
|
27
|
+
}) {
|
|
28
|
+
}
|
|
29
|
+
export class InvalidPointerFailure extends Schema.TaggedError()("InvalidPointerFailure", {
|
|
30
|
+
...failureFields,
|
|
31
|
+
pointer: Schema.String,
|
|
32
|
+
}) {
|
|
33
|
+
}
|
|
34
|
+
export class CircularAliasFailure extends Schema.TaggedError()("CircularAliasFailure", {
|
|
35
|
+
message: Schema.String,
|
|
36
|
+
definition: Schema.String,
|
|
37
|
+
}) {
|
|
38
|
+
}
|
|
39
|
+
export class BundleFailure extends Schema.TaggedError()("BundleFailure", {
|
|
40
|
+
message: Schema.String,
|
|
41
|
+
}) {
|
|
42
|
+
}
|
|
43
|
+
export class TimeoutFailure extends Schema.TaggedError()("TimeoutFailure", {
|
|
44
|
+
message: Schema.String,
|
|
45
|
+
timeout: Schema.Finite,
|
|
46
|
+
}) {
|
|
47
|
+
}
|
|
48
|
+
const leafFailure = Schema.Union([
|
|
49
|
+
ParserFailure,
|
|
50
|
+
ResolverFailure,
|
|
51
|
+
UnmatchedParserFailure,
|
|
52
|
+
UnmatchedResolverFailure,
|
|
53
|
+
MissingPointerFailure,
|
|
54
|
+
InvalidPointerFailure,
|
|
55
|
+
CircularAliasFailure,
|
|
56
|
+
BundleFailure,
|
|
57
|
+
TimeoutFailure,
|
|
58
|
+
]);
|
|
59
|
+
export class GroupFailure extends Schema.TaggedError()("GroupFailure", {
|
|
60
|
+
source: Schema.String,
|
|
61
|
+
message: Schema.String,
|
|
62
|
+
errors: Schema.Array(leafFailure),
|
|
63
|
+
}) {
|
|
64
|
+
}
|
|
65
|
+
const taggedFailures = [
|
|
66
|
+
ParserFailure,
|
|
67
|
+
ResolverFailure,
|
|
68
|
+
UnmatchedParserFailure,
|
|
69
|
+
UnmatchedResolverFailure,
|
|
70
|
+
MissingPointerFailure,
|
|
71
|
+
InvalidPointerFailure,
|
|
72
|
+
CircularAliasFailure,
|
|
73
|
+
BundleFailure,
|
|
74
|
+
TimeoutFailure,
|
|
75
|
+
GroupFailure,
|
|
76
|
+
];
|
|
77
|
+
export const isTaggedFailure = Schema.is(Schema.Union(taggedFailures));
|
|
78
|
+
export function isLeafFailure(cause) {
|
|
79
|
+
return cause._tag !== "GroupFailure";
|
|
80
|
+
}
|
|
81
|
+
export function nodeErrorCode(cause) {
|
|
82
|
+
const code = errorCode(cause);
|
|
83
|
+
if (code)
|
|
84
|
+
return code;
|
|
85
|
+
if (typeof cause === "object" && cause !== null && "cause" in cause)
|
|
86
|
+
return errorCode(cause.cause);
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
89
|
+
function errorCode(cause) {
|
|
90
|
+
if (typeof cause !== "object" || cause === null || !("code" in cause))
|
|
91
|
+
return undefined;
|
|
92
|
+
const { code } = cause;
|
|
93
|
+
if (typeof code === "string")
|
|
94
|
+
return code.length > 0 ? code : undefined;
|
|
95
|
+
if (typeof code === "number" && Number.isFinite(code))
|
|
96
|
+
return String(code);
|
|
97
|
+
return undefined;
|
|
98
|
+
}
|
|
99
|
+
export function missingPointerFailure(token, source, targetRef = "", targetFound = "", parentPath = "") {
|
|
100
|
+
const hash = source.includes("#") ? source.slice(source.indexOf("#")) : source;
|
|
101
|
+
return new MissingPointerFailure({
|
|
102
|
+
message: `Missing $ref pointer "${hash}". Token "${token}" does not exist.`,
|
|
103
|
+
source,
|
|
104
|
+
token,
|
|
105
|
+
targetRef,
|
|
106
|
+
targetFound,
|
|
107
|
+
parentPath,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
export function invalidPointerFailure(pointer, source = pointer) {
|
|
111
|
+
return new InvalidPointerFailure({
|
|
112
|
+
message: `Invalid $ref pointer "${pointer}". Pointers must begin with "#/"`,
|
|
113
|
+
source,
|
|
114
|
+
pointer,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
export function circularAliasFailure(definition) {
|
|
118
|
+
return new CircularAliasFailure({
|
|
119
|
+
message: `Definition ${JSON.stringify(definition)} is a circular alias.`,
|
|
120
|
+
definition,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
export function bundleFailure(message) {
|
|
124
|
+
return new BundleFailure({ message });
|
|
125
|
+
}
|
|
126
|
+
export function timeoutFailure(timeout) {
|
|
127
|
+
return new TimeoutFailure({
|
|
128
|
+
message: `Dereferencing timeout reached: ${timeout}ms`,
|
|
129
|
+
timeout,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
export function groupFailure(source, errors) {
|
|
133
|
+
return new GroupFailure({
|
|
134
|
+
source,
|
|
135
|
+
message: `${errors.length} error${errors.length === 1 ? "" : "s"} occurred while reading '${source}'`,
|
|
136
|
+
errors: [...errors],
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
export function syncFailure(cause) {
|
|
140
|
+
if (isTaggedFailure(cause))
|
|
141
|
+
return cause;
|
|
142
|
+
return new BundleFailure({
|
|
143
|
+
message: cause instanceof Error ? cause.message : String(cause),
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
export class EmptySource extends Schema.TaggedError()("EmptySource", {
|
|
147
|
+
message: Schema.String,
|
|
148
|
+
}) {
|
|
149
|
+
}
|
|
150
|
+
export class InvalidSource extends Schema.TaggedError()("InvalidSource", {
|
|
151
|
+
message: Schema.String,
|
|
152
|
+
}) {
|
|
153
|
+
}
|
|
154
|
+
export class UnsupportedVersion extends Schema.TaggedError()("UnsupportedVersion", {
|
|
155
|
+
message: Schema.String,
|
|
156
|
+
}) {
|
|
157
|
+
}
|
|
158
|
+
export class ParseError extends Schema.TaggedError()("ParseError", {
|
|
159
|
+
message: Schema.String,
|
|
160
|
+
cause: Schema.Defect(),
|
|
161
|
+
}) {
|
|
162
|
+
}
|
package/dist/hoist.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { BundleHooks } from "./plugins.ts";
|
|
2
|
+
export interface ExternalRef {
|
|
3
|
+
parent: Record<string, unknown>;
|
|
4
|
+
key: string;
|
|
5
|
+
ref: string;
|
|
6
|
+
pointer: string;
|
|
7
|
+
}
|
|
8
|
+
export declare function findExternalRef(root: unknown, keep: (ref: string) => boolean, abandoned?: ReadonlySet<string>): ExternalRef | undefined;
|
|
9
|
+
export declare function hoistExternalRefs(root: Record<string, unknown>, readValue: (ref: string) => unknown, hooks: BundleHooks | undefined, keep: (ref: string) => boolean): Map<string, string>;
|
|
10
|
+
export declare function oas3DefaultRoot(pathFromRoot: string): string | null;
|