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

Commit

Permalink
chore: improve types
Browse files Browse the repository at this point in the history
  • Loading branch information
hanspagel committed Feb 22, 2024
1 parent 101bb3b commit eeac7dc
Show file tree
Hide file tree
Showing 18 changed files with 183 additions and 151 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Filesystem, FilesystemEntry, Specification } from '../../types'
import type { AnyObject, Filesystem, FilesystemEntry } from '../../types'
import { escapeJsonPointer } from './escapeJsonPointer'
import { isObject } from './isObject'
import { resolveUri } from './resolveUri'
Expand Down Expand Up @@ -34,7 +34,7 @@ export function resolveReferences(
const pointers: {
[key: string]: {
ref: string
obj: Specification | string
obj: AnyObject | string
prop: string
path: string
id: string
Expand All @@ -44,7 +44,7 @@ export function resolveReferences(
pointers[word] = []
}

function applyRef(path: string, target: Specification) {
function applyRef(path: string, target: AnyObject) {
let root = specification
const paths = path.split('/').slice(1)
const prop = paths.pop()
Expand All @@ -60,7 +60,7 @@ export function resolveReferences(
}
}

function parse(obj: Specification, path: string, id: string) {
function parse(obj: AnyObject, path: string, id: string) {
if (!isObject(obj)) {
return
}
Expand Down
4 changes: 2 additions & 2 deletions packages/openapi-parser/src/lib/Validator/resolveUri.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { unescapeJsonPointer } from 'ajv/dist/compile/util'

import type { Filesystem, FilesystemEntry } from '../../types'
import type { AnyObject, Filesystem, FilesystemEntry } from '../../types'
import { resolveFromFilesystem } from './resolveFromFilesystem'

export function resolveUri(
file: FilesystemEntry,
uri: string,
anchors: Record<string, any>,
anchors: AnyObject,
filesystem?: Filesystem,
) {
const [prefix, path] = uri.split('#', 2)
Expand Down
29 changes: 15 additions & 14 deletions packages/openapi-parser/src/pipeline.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { AnyObject } from './types'
import {
details,
filter,
Expand All @@ -15,13 +16,13 @@ export function openapi() {
}
}

function loadAction(specification: string | Record<string, any>) {
function loadAction(specification: string | AnyObject) {
const normalizedSpecification = normalize(specification)

return {
get: () => getAction(normalizedSpecification),
details: () => detailsAction(normalizedSpecification),
filter: (callback: (Specification: Record<string, any>) => boolean) =>
filter: (callback: (Specification: AnyObject) => boolean) =>
filterAction(normalizedSpecification, callback),
upgrade: () => upgradeAction(normalizedSpecification),
validate: () => validateAction(normalizedSpecification),
Expand All @@ -31,13 +32,13 @@ function loadAction(specification: string | Record<string, any>) {
}
}

function upgradeAction(specification: Record<string, any>) {
function upgradeAction(specification: AnyObject) {
const upgradedSpecification = upgrade(specification)

return {
get: () => getAction(upgradedSpecification),
details: () => detailsAction(upgradedSpecification),
filter: (callback: (Specification: Record<string, any>) => boolean) =>
filter: (callback: (Specification: AnyObject) => boolean) =>
filterAction(upgradedSpecification, callback),
validate: () => validateAction(upgradedSpecification),
resolve: () => resolveAction(upgradedSpecification),
Expand All @@ -46,10 +47,10 @@ function upgradeAction(specification: Record<string, any>) {
}
}

async function validateAction(specification: Record<string, any>) {
async function validateAction(specification: AnyObject) {
return {
...(await validate(specification)),
filter: (callback: (Specification: Record<string, any>) => boolean) =>
filter: (callback: (Specification: AnyObject) => boolean) =>
filterAction(specification, callback),
get: () => getAction(specification),
details: () => detailsAction(specification),
Expand All @@ -59,19 +60,19 @@ async function validateAction(specification: Record<string, any>) {
}
}

async function resolveAction(specification: Record<string, any>) {
async function resolveAction(specification: AnyObject) {
return {
...(await resolve(specification)),
filter: (callback: (Specification: Record<string, any>) => boolean) =>
filter: (callback: (Specification: AnyObject) => boolean) =>
filterAction(specification, callback),
toJson: () => toJsonAction(specification),
toYaml: () => toYamlAction(specification),
}
}

function filterAction(
specification: Record<string, any>,
callback: (specification: Record<string, any>) => boolean,
specification: AnyObject,
callback: (specification: AnyObject) => boolean,
) {
const filteredSpecification = filter(specification, callback)

Expand All @@ -87,18 +88,18 @@ function filterAction(
}
}

function getAction(specification: Record<string, any>) {
function getAction(specification: AnyObject) {
return specification
}

function detailsAction(specification: Record<string, any>) {
function detailsAction(specification: AnyObject) {
return details(specification)
}

function toJsonAction(specification: Record<string, any>) {
function toJsonAction(specification: AnyObject) {
return toJson(specification)
}

function toYamlAction(specification: Record<string, any>) {
function toYamlAction(specification: AnyObject) {
return toYaml(specification)
}
60 changes: 60 additions & 0 deletions packages/openapi-parser/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { OpenAPI } from 'openapi-types'

import { ResolvedOpenAPI } from './openapi'

export {
ResolvedOpenAPI,
ResolvedOpenAPIV2,
ResolvedOpenAPIV3,
ResolvedOpenAPIV3_1,
} from './openapi'

export { OpenAPI, OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'

export type AnyObject = Record<string, any>

export type ValidateResult = {
valid: boolean
specification?: OpenAPI.Document
version?: string
errors?: ErrorObject[]
}

export type ErrorObject = {
start: {
line: number
column: number
offset: number
}
error: string
path: string
}

export type ResolveResult = {
valid: boolean
version: string | undefined
specification?: OpenAPI.Document
schema?: ResolvedOpenAPI.Document
errors?: ErrorObject[]
}

export type AjvOptions = {
strict?: boolean | 'log'
}

/**
* Not literally a filesystem, but a list of files with their content.
* This is an abstraction layer to handle multiple files in the browser (without access to the hard disk).
*/
export type Filesystem = FilesystemEntry[]

/**
* Holds all information about a single file (doesn’t have to be a literal file, see Filesystem).
*/
export type FilesystemEntry = {
dir: string
entrypoint: boolean
references: string[]
filename: string
specification: AnyObject
}
Loading

0 comments on commit eeac7dc

Please sign in to comment.