@hyperjump/json-schema
Advanced tools
Comparing version 1.2.6 to 1.3.0
@@ -10,2 +10,3 @@ import type { SchemaObject } from "./schema.js"; | ||
matcher: (path: string) => boolean; | ||
quality?: string; | ||
}; |
@@ -27,1 +27,23 @@ import contentTypeParser from "content-type"; | ||
}; | ||
export const acceptableMediaTypes = () => { | ||
let accept = ""; | ||
for (const contentType in mediaTypePlugins) { | ||
accept = addAcceptableMediaType(accept, contentType, mediaTypePlugins[contentType].quality); | ||
} | ||
return addAcceptableMediaType(accept, "*/*", "0.1"); | ||
}; | ||
const addAcceptableMediaType = (accept, contentType, quality) => { | ||
if (accept.length > 0) { | ||
accept += ", "; | ||
} | ||
accept += contentType; | ||
if (quality) { | ||
accept += `; q=${quality}`; | ||
} | ||
return accept; | ||
}; |
@@ -7,3 +7,3 @@ import curry from "just-curry-it"; | ||
import { hasDialect, loadDialect, getKeywordName } from "./keywords.js"; | ||
import { parseResponse } from "./media-types.js"; | ||
import { parseResponse, acceptableMediaTypes } from "./media-types.js"; | ||
import * as Reference from "./reference.js"; | ||
@@ -165,6 +165,6 @@ | ||
if (!hasStoredSchema(id)) { | ||
const response = await fetch(id, { headers: { Accept: "application/schema+json" } }); | ||
const response = await fetch(id, { headers: { Accept: acceptableMediaTypes() } }); | ||
if (response.status >= 400) { | ||
await response.text(); // Sometimes node hangs without this hack | ||
throw Error(`Failed to retrieve schema with id: ${id}`); | ||
throw Error(`Failed to retrieve ${id} (${response.status} ${response.statusText})`); | ||
} | ||
@@ -171,0 +171,0 @@ |
@@ -60,2 +60,240 @@ import type { Json } from "@hyperjump/json-pointer"; | ||
type OpenApi = { | ||
openapi: string; | ||
info: Info; | ||
externalDocs?: ExternalDocs; | ||
servers?: Server[]; | ||
security?: SecurityRequirement[]; | ||
tags?: Tag[]; | ||
paths: Paths; | ||
components?: Components; | ||
}; | ||
type Reference = { | ||
$ref: "string" | ||
}; | ||
type Info = { | ||
title: string; | ||
description?: string; | ||
termsOfService?: string; | ||
contact?: Contact; | ||
license?: License; | ||
version: string; | ||
}; | ||
type Contact = { | ||
name?: string; | ||
url?: string; | ||
email?: string; | ||
}; | ||
type License = { | ||
name: string; | ||
url?: string; | ||
}; | ||
type Server = { | ||
url: string; | ||
description?: string; | ||
variables?: Record<string, ServerVariable>; | ||
}; | ||
type ServerVariable = { | ||
enum?: string[]; | ||
default: string; | ||
description?: string; | ||
}; | ||
type Components = { | ||
schemas?: Record<string, OasSchema30>; | ||
responses?: Record<string, Response | Reference>; | ||
parameters?: Record<string, Parameter | Reference>; | ||
examples?: Record<string, Example | Reference>; | ||
requestBodies?: Record<string, RequestBody | Reference>; | ||
headers?: Record<string, Header | Reference>; | ||
securitySchemes: Record<string, SecurityScheme | Reference>; | ||
links: Record<string, Link | Reference>; | ||
callbacks: Record<string, Callback | Reference>; | ||
}; | ||
type Response = { | ||
description: string; | ||
headers?: Record<string, Header | Reference>; | ||
content?: Record<string, MediaType>; | ||
links?: Record<string, Link | Reference>; | ||
}; | ||
type MediaType = { | ||
schema?: OasSchema30; | ||
example?: unknown; | ||
examples?: Record<string, Example | Reference>; | ||
encoding?: Record<string, Encoding>; | ||
}; | ||
type Example = { | ||
summary?: string; | ||
description?: string; | ||
value?: unknown; | ||
externalValue?: string; | ||
}; | ||
type Header = { | ||
description?: string; | ||
required?: boolean; | ||
deprecated?: boolean; | ||
allowEmptyValue?: boolean; | ||
style?: "simple"; | ||
explode?: boolean; | ||
allowReserved?: boolean; | ||
schema?: OasSchema30; | ||
content?: Record<string, MediaType>; | ||
example?: unknown; | ||
examples: Record<string, Example | Reference>; | ||
}; | ||
type Paths = Record<string, PathItem>; | ||
type PathItem = { | ||
$ref?: string; | ||
summary?: string; | ||
description?: string; | ||
servers: Server[]; | ||
parameters: (Parameter | Reference)[]; | ||
get?: Operation; | ||
put?: Operation; | ||
post?: Operation; | ||
delete?: Operation; | ||
options?: Operation; | ||
head?: Operation; | ||
patch?: Operation; | ||
trace?: Operation; | ||
}; | ||
type Operation = { | ||
tags?: string[]; | ||
summary?: string; | ||
description?: string; | ||
externalDocs?: ExternalDocs; | ||
operationId?: string; | ||
parameters?: (Parameter | Reference)[]; | ||
requestBody?: RequestBody | Reference; | ||
responses: Responses; | ||
callbacks?: Record<string, Callback | Reference>; | ||
deprecated?: boolean; | ||
security?: SecurityRequirement[]; | ||
servers?: Server[]; | ||
}; | ||
type Responses = Record<string, Response | Reference>; | ||
type SecurityRequirement = Record<string, string[]>; | ||
type Tag = { | ||
name: string; | ||
description?: string; | ||
externalDocs?: ExternalDocs; | ||
}; | ||
type Parameter = { | ||
name: string; | ||
in: string; | ||
description?: string; | ||
required?: boolean; | ||
deprecated?: boolean; | ||
allowEmptyValue?: boolean; | ||
style?: string; | ||
explode?: boolean; | ||
allowReserved?: boolean; | ||
schema?: OasSchema30; | ||
content?: Record<string, MediaType>; | ||
example?: unknown; | ||
examples?: Record<string, Example | Reference>; | ||
}; | ||
type RequestBody = { | ||
description?: string; | ||
content: Record<string, MediaType>; | ||
required?: boolean; | ||
}; | ||
type SecurityScheme = APIKeySecurityScheme | HTTPSecurityScheme | OAuth2SecurityScheme | OpenIdConnectSecurityScheme; | ||
type APIKeySecurityScheme = { | ||
type: "apiKey"; | ||
name: string; | ||
in: "header" | "query" | "cookie"; | ||
description?: string; | ||
}; | ||
type HTTPSecurityScheme = { | ||
scheme: string; | ||
bearerFormat?: string; | ||
description?: string; | ||
type: "http"; | ||
}; | ||
type OAuth2SecurityScheme = { | ||
type: "oauth2"; | ||
flows: OAuthFlows; | ||
description?: string; | ||
}; | ||
type OpenIdConnectSecurityScheme = { | ||
type: "openIdConnect"; | ||
openIdConnectUrl: string; | ||
description?: string; | ||
}; | ||
type OAuthFlows = { | ||
implicit?: ImplicitOAuthFlow; | ||
password?: PasswordOAuthFlow; | ||
clientCredentials?: ClientCredentialsFlow; | ||
authorizationCode?: AuthorizationCodeOAuthFlow; | ||
}; | ||
type ImplicitOAuthFlow = { | ||
authorizationUrl: string; | ||
refreshUrl?: string; | ||
scopes: Record<string, string>; | ||
}; | ||
type PasswordOAuthFlow = { | ||
tokenUrl: string; | ||
refreshUrl?: string; | ||
scopes: Record<string, string>; | ||
}; | ||
type ClientCredentialsFlow = { | ||
tokenUrl: string; | ||
refreshUrl?: string; | ||
scopes: Record<string, string>; | ||
}; | ||
type AuthorizationCodeOAuthFlow = { | ||
authorizationUrl: string; | ||
tokenUrl: string; | ||
refreshUrl?: string; | ||
scopes: Record<string, string>; | ||
}; | ||
type Link = { | ||
operationId?: string; | ||
operationRef?: string; | ||
parameters?: Record<string, unknown>; | ||
requestBody?: unknown; | ||
description?: string; | ||
server?: Server; | ||
}; | ||
type Callback = Record<string, PathItem>; | ||
type Encoding = { | ||
contentType?: string; | ||
headers?: Record<string, Header | Reference>; | ||
style?: "form" | "spaceDelimited" | "pipeDelimited" | "deepObject"; | ||
explode?: boolean; | ||
allowReserved?: boolean; | ||
}; | ||
export * from "../lib/index.js"; |
@@ -68,6 +68,2 @@ import { addKeyword, defineVocabulary, loadDialect } from "../lib/keywords.js"; | ||
loadDialect("https://spec.openapis.org/oas/3.0/schema", { | ||
[jsonSchemaVersion]: true | ||
}); | ||
addSchema(dialectSchema); | ||
@@ -74,0 +70,0 @@ |
@@ -88,2 +88,229 @@ import type { Json } from "@hyperjump/json-pointer"; | ||
export type OpenApi = { | ||
openapi: string | ||
info: Info; | ||
jsonSchemaDialect?: string; | ||
servers?: Server[]; | ||
security?: SecurityRequirement[] | ||
tags?: Tag[]; | ||
externalDocs?: ExternalDocumentation; | ||
paths?: Record<string, PathItem>; | ||
webhooks?: Record<string, PathItem | Reference>; | ||
components?: Components; | ||
}; | ||
type Info = { | ||
title: string; | ||
summary?: string; | ||
description?: string; | ||
termsOfService?: string; | ||
contact?: Contact; | ||
license?: License; | ||
version: string; | ||
}; | ||
type Contact = { | ||
name?: string; | ||
url?: string; | ||
email?: string; | ||
}; | ||
type License = { | ||
name: string; | ||
url?: string; | ||
identifier?: string; | ||
}; | ||
type Server = { | ||
url: string; | ||
description?: string; | ||
variables?: Record<string, ServerVariable>; | ||
}; | ||
type ServerVariable = { | ||
enum?: string[]; | ||
default: string; | ||
description?: string; | ||
}; | ||
type Components = { | ||
schemas?: Record<string, OasSchema31>; | ||
responses?: Record<string, Response | Reference>; | ||
parameters?: Record<string, Parameter | Reference>; | ||
examples?: Record<string, Example | Reference>; | ||
requestBodies?: Record<string, RequestBody | Reference>; | ||
headers?: Record<string, Header | Reference>; | ||
securitySchemes?: Record<string, SecurityScheme | Reference>; | ||
links?: Record<string, Link | Reference>; | ||
callbacks?: Record<string, Callbacks | Reference>; | ||
pathItems?: Record<string, PathItem | Reference>; | ||
}; | ||
type PathItem = { | ||
summary?: string; | ||
description?: string; | ||
servers?: Server[]; | ||
parameters?: (Parameter | Reference)[]; | ||
get?: Operation; | ||
put?: Operation; | ||
post?: Operation; | ||
delete?: Operation; | ||
options?: Operation; | ||
head?: Operation; | ||
patch?: Operation; | ||
trace?: Operation; | ||
}; | ||
type Operation = { | ||
tags?: string[]; | ||
summary?: string; | ||
description?: string; | ||
externalDocs?: ExternalDocumentation; | ||
operationId?: string; | ||
parameters?: (Parameter | Reference)[]; | ||
requestBody?: RequestBody | Reference; | ||
responses?: Record<string, Response | Reference>; | ||
callbacks?: Record<string, Callbacks | Reference>; | ||
deprecated?: boolean; | ||
security?: SecurityRequirement[]; | ||
servers?: Server[]; | ||
}; | ||
type ExternalDocumentation = { | ||
description?: string; | ||
url: string; | ||
}; | ||
type Parameter = { | ||
name: string; | ||
in: "query" | "header" | "path" | "cookie"; | ||
description?: string; | ||
required?: boolean; | ||
deprecated?: boolean; | ||
allowEmptyValue?: boolean; | ||
style?: "matrix" | "label" | "form" | "simple" | "spaceDelimited" | "pipeDelimited" | "deepObject"; | ||
explode?: boolean; | ||
allowReserved?: boolean; | ||
schema?: OasSchema31; | ||
content?: Content; | ||
} & Examples; | ||
type RequestBody = { | ||
description?: string; | ||
content: Content; | ||
required?: boolean; | ||
}; | ||
type Content = Record<string, MediaType>; | ||
type MediaType = { | ||
schema?: OasSchema31; | ||
encoding?: Record<string, Encoding>; | ||
} & Examples; | ||
type Encoding = { | ||
contentType?: string; | ||
headers?: Record<string, Header | Reference>; | ||
style?: "form" | "spaceDelimited" | "pipeDelimited" | "deepObject"; | ||
explode?: boolean; | ||
allowReserved?: boolean; | ||
}; | ||
type Response = { | ||
description: string; | ||
headers?: Record<string, Header | Reference>; | ||
content?: Content; | ||
links?: Record<string, Link | Reference>; | ||
}; | ||
type Callbacks = Record<string, PathItem | Reference>; | ||
type Example = { | ||
summary?: string; | ||
description?: string; | ||
value?: Json, | ||
externalValue?: string; | ||
}; | ||
type Link = { | ||
operationRef?: string; | ||
operationId?: string; | ||
parameters?: Record<string, string>; | ||
requestBody?: Json; | ||
description?: string; | ||
body?: Server; | ||
}; | ||
type Header = { | ||
description?: string; | ||
required?: boolean; | ||
deprecated?: boolean; | ||
schema?: OpenApi31; | ||
style?: "simple"; | ||
explode?: boolean; | ||
content?: Content; | ||
}; | ||
type Tag = { | ||
name: string; | ||
description?: string; | ||
externalDocs?: ExternalDocumentation; | ||
}; | ||
type Reference = { | ||
$ref: string; | ||
summary?: string; | ||
descriptions?: string; | ||
}; | ||
type SecurityScheme = { | ||
type: "apiKey" | "http" | "mutualTLS" | "oauth2" | "openIdConnect"; | ||
description?: string; | ||
name?: string; | ||
in?: "query" | "header" | "cookie"; | ||
scheme?: string; | ||
bearerFormat?: string; | ||
flows?: OauthFlows; | ||
openIdConnectUrl?: string; | ||
}; | ||
type OauthFlows = { | ||
implicit: Implicit; | ||
Password: Password; | ||
clientCredentials: ClientCredentials; | ||
authorizationCode: AuthorizationCode; | ||
}; | ||
type Implicit = { | ||
authorizationUrl: string; | ||
refreshUrl?: string; | ||
scopes: Record<string, string>; | ||
}; | ||
type Password = { | ||
tokenUrl: string; | ||
refreshUrl?: string; | ||
scopes: Record<string, string>; | ||
}; | ||
type ClientCredentials = { | ||
tokenUrl: string; | ||
refreshUrl?: string; | ||
scopes: Record<string, string>; | ||
}; | ||
type AuthorizationCode = { | ||
authorizationUrl: string; | ||
tokenUrl: string; | ||
refreshUrl?: string; | ||
scopes: Record<string, string>; | ||
}; | ||
type SecurityRequirement = Record<string, string[]>; | ||
type Examples = { | ||
example?: Json; | ||
examples?: Record<string, Example | Reference>; | ||
}; | ||
export * from "../lib/index.js"; |
{ | ||
"name": "@hyperjump/json-schema", | ||
"version": "1.2.6", | ||
"version": "1.3.0", | ||
"description": "A JSON Schema validator with support for custom keywords, vocabularies, and dialects", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -231,3 +231,10 @@ # Hyperjump - JSON Schema | ||
considered a member of this media type. | ||
* quality (optional): string | ||
The registered media type plugins are used to create the `Accept` header | ||
for HTTP requests. This property allows you to specify a quality value for | ||
your media type. A [quality value](https://developer.mozilla.org/en-US/docs/Glossary/Quality_values) | ||
is a string representation of a number between 0 and 1 with up to three | ||
digits. | ||
## Bundling | ||
@@ -234,0 +241,0 @@ ### Usage |
299722
8248
674