Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
refactor: use global type
Browse files Browse the repository at this point in the history
  • Loading branch information
hanspagel committed Jun 20, 2024
1 parent 80f9995 commit 6cddc37
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 22 deletions.
9 changes: 7 additions & 2 deletions packages/openapi-parser/src/lib/Validator/Validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import {
type OpenApiVersion,
OpenApiVersions,
} from '../../configuration'
import type { AnyObject, Filesystem, ValidateResult } from '../../types'
import type {
AnyObject,
Filesystem,
ThrowOnErrorOption,
ValidateResult,
} from '../../types'
import { details as getOpenApiVersion } from '../../utils/details'
import { resolveReferences } from '../../utils/resolveReferences'
import { transformErrors } from '../../utils/transformErrors'
Expand Down Expand Up @@ -47,7 +52,7 @@ export class Validator {
*/
async validate(
filesystem: Filesystem,
options?: { throwOnError?: boolean },
options?: ThrowOnErrorOption,
): Promise<ValidateResult> {
const entrypoint = filesystem.find((file) => file.isEntrypoint)
const specification = entrypoint?.specification
Expand Down
12 changes: 6 additions & 6 deletions packages/openapi-parser/src/pipeline.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type { AnyObject, DetailsResult, Filesystem } from './types'
import type {
AnyObject,
DetailsResult,
Filesystem,
ThrowOnErrorOption,
} from './types'
import {
dereference,
details,
Expand Down Expand Up @@ -34,11 +39,6 @@ export type Queue = {
tasks: Action[]
}

/**
* If `true`, the function will throw an error if the document is invalid.
*/
export type ThrowOnErrorOption = { throwOnError?: boolean }

/**
* JSON, YAML or object representation of an OpenAPI API definition
*/
Expand Down
9 changes: 9 additions & 0 deletions packages/openapi-parser/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,12 @@ export type FilesystemEntry = {
filename: string
specification: AnyObject
}

export type ThrowOnErrorOption = {
/**
* If `true`, the function will throw an error if the document is invalid.
*
* @default false
*/
throwOnError?: boolean
}
9 changes: 7 additions & 2 deletions packages/openapi-parser/src/utils/dereference.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type { AnyObject, DereferenceResult, Filesystem } from '../types'
import type {
AnyObject,
DereferenceResult,
Filesystem,
ThrowOnErrorOption,
} from '../types'
import { details } from './details'
import { getEntrypoint } from './getEntrypoint'
import { makeFilesystem } from './makeFilesystem'
Expand All @@ -9,7 +14,7 @@ import { resolveReferences } from './resolveReferences'
*/
export async function dereference(
value: string | AnyObject | Filesystem,
options?: { throwOnError?: boolean },
options?: ThrowOnErrorOption,
): Promise<DereferenceResult> {
const filesystem = makeFilesystem(value)

Expand Down
8 changes: 3 additions & 5 deletions packages/openapi-parser/src/utils/resolveReferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
ErrorObject,
Filesystem,
FilesystemEntry,
ThrowOnErrorOption,
} from '../types'
import { getEntrypoint } from './getEntrypoint'
import { getSegmentsFromPath } from './getSegmentsFromPath'
Expand Down Expand Up @@ -38,10 +39,7 @@ export function resolveReferences(
// Just a specification, or a set of files.
input: AnyObject | Filesystem,
// Additional options to control the behaviour
options?: {
// Throw an error if something goes wrong
throwOnError?: boolean
},
options?: ThrowOnErrorOption,
// Fallback to the entrypoint
file?: FilesystemEntry,
// Errors that occurred during the process
Expand Down Expand Up @@ -159,7 +157,7 @@ function isCircular(schema: AnyObject) {
function resolveUri(
// 'foobar.json#/foo/bar'
uri: string,
options: { throwOnError?: boolean },
options: ThrowOnErrorOption,
// { filename: './foobar.json '}
file: FilesystemEntry,
// [ { filename: './foobar.json '} ]
Expand Down
15 changes: 8 additions & 7 deletions packages/openapi-parser/src/utils/validate.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { Validator } from '../lib/Validator'
import type { AnyObject, Filesystem, OpenAPI, ValidateResult } from '../types'
import type {
AnyObject,
Filesystem,
OpenAPI,
ThrowOnErrorOption,
ValidateResult,
} from '../types'
import { makeFilesystem } from './makeFilesystem'

/**
* Validates an OpenAPI schema.
*/
export async function validate(
value: string | AnyObject | Filesystem,
options?: {
/**
* If `true`, the function will throw an error if the document is invalid.
*/
throwOnError?: boolean
},
options?: ThrowOnErrorOption,
): Promise<ValidateResult> {
const filesystem = makeFilesystem(value)

Expand Down

0 comments on commit 6cddc37

Please sign in to comment.