@hyperjump/json-schema 1.2.6 → 1.3.0

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/README.md CHANGED
@@ -228,6 +228,13 @@ The following types are used in the above definitions
228
228
 
229
229
  Given a filesystem path, return whether or not the file should be
230
230
  considered a member of this media type.
231
+ * quality (optional): string
232
+
233
+ The registered media type plugins are used to create the `Accept` header
234
+ for HTTP requests. This property allows you to specify a quality value for
235
+ your media type. A [quality value](https://developer.mozilla.org/en-US/docs/Glossary/Quality_values)
236
+ is a string representation of a number between 0 and 1 with up to three
237
+ digits.
231
238
 
232
239
  ## Bundling
233
240
  ### Usage
@@ -7,4 +7,5 @@ export const addMediaTypePlugin: (contentType: string, plugin: MediaTypePlugin)
7
7
  export type MediaTypePlugin = {
8
8
  parse: (response: Response, mediaTypeParameters: { [parameter: string]: string }) => Promise<[SchemaObject, string | undefined]>;
9
9
  matcher: (path: string) => boolean;
10
+ quality?: string;
10
11
  };
@@ -24,3 +24,25 @@ export const getContentType = (path) => {
24
24
 
25
25
  return "application/octet-stream";
26
26
  };
27
+
28
+ export const acceptableMediaTypes = () => {
29
+ let accept = "";
30
+
31
+ for (const contentType in mediaTypePlugins) {
32
+ accept = addAcceptableMediaType(accept, contentType, mediaTypePlugins[contentType].quality);
33
+ }
34
+
35
+ return addAcceptableMediaType(accept, "*/*", "0.1");
36
+ };
37
+
38
+ const addAcceptableMediaType = (accept, contentType, quality) => {
39
+ if (accept.length > 0) {
40
+ accept += ", ";
41
+ }
42
+ accept += contentType;
43
+ if (quality) {
44
+ accept += `; q=${quality}`;
45
+ }
46
+
47
+ return accept;
48
+ };
package/lib/schema.js CHANGED
@@ -4,7 +4,7 @@ import { nil as nilPointer, append as pointerAppend, get as pointerGet } from "@
4
4
  import { jsonTypeOf, resolveUri, toAbsoluteUri, uriFragment, pathRelative, jsonStringify } from "./common.js";
5
5
  import fetch from "./fetch.js";
6
6
  import { hasDialect, loadDialect, getKeywordName } from "./keywords.js";
7
- import { parseResponse } from "./media-types.js";
7
+ import { parseResponse, acceptableMediaTypes } from "./media-types.js";
8
8
  import * as Reference from "./reference.js";
9
9
 
10
10
 
@@ -161,10 +161,10 @@ export const get = async (url, contextDoc = nil) => {
161
161
  const fragment = uriFragment(resolvedUrl);
162
162
 
163
163
  if (!hasStoredSchema(id)) {
164
- const response = await fetch(id, { headers: { Accept: "application/schema+json" } });
164
+ const response = await fetch(id, { headers: { Accept: acceptableMediaTypes() } });
165
165
  if (response.status >= 400) {
166
166
  await response.text(); // Sometimes node hangs without this hack
167
- throw Error(`Failed to retrieve schema with id: ${id}`);
167
+ throw Error(`Failed to retrieve ${id} (${response.status} ${response.statusText})`);
168
168
  }
169
169
 
170
170
  const [schema, contextDialectId] = await parseResponse(response);
@@ -57,4 +57,242 @@ type Xml = {
57
57
  wrapped?: boolean;
58
58
  };
59
59
 
60
+ type OpenApi = {
61
+ openapi: string;
62
+ info: Info;
63
+ externalDocs?: ExternalDocs;
64
+ servers?: Server[];
65
+ security?: SecurityRequirement[];
66
+ tags?: Tag[];
67
+ paths: Paths;
68
+ components?: Components;
69
+ };
70
+
71
+ type Reference = {
72
+ $ref: "string"
73
+ };
74
+
75
+ type Info = {
76
+ title: string;
77
+ description?: string;
78
+ termsOfService?: string;
79
+ contact?: Contact;
80
+ license?: License;
81
+ version: string;
82
+ };
83
+
84
+ type Contact = {
85
+ name?: string;
86
+ url?: string;
87
+ email?: string;
88
+ };
89
+
90
+ type License = {
91
+ name: string;
92
+ url?: string;
93
+ };
94
+
95
+ type Server = {
96
+ url: string;
97
+ description?: string;
98
+ variables?: Record<string, ServerVariable>;
99
+ };
100
+
101
+ type ServerVariable = {
102
+ enum?: string[];
103
+ default: string;
104
+ description?: string;
105
+ };
106
+
107
+ type Components = {
108
+ schemas?: Record<string, OasSchema30>;
109
+ responses?: Record<string, Response | Reference>;
110
+ parameters?: Record<string, Parameter | Reference>;
111
+ examples?: Record<string, Example | Reference>;
112
+ requestBodies?: Record<string, RequestBody | Reference>;
113
+ headers?: Record<string, Header | Reference>;
114
+ securitySchemes: Record<string, SecurityScheme | Reference>;
115
+ links: Record<string, Link | Reference>;
116
+ callbacks: Record<string, Callback | Reference>;
117
+ };
118
+
119
+ type Response = {
120
+ description: string;
121
+ headers?: Record<string, Header | Reference>;
122
+ content?: Record<string, MediaType>;
123
+ links?: Record<string, Link | Reference>;
124
+ };
125
+
126
+ type MediaType = {
127
+ schema?: OasSchema30;
128
+ example?: unknown;
129
+ examples?: Record<string, Example | Reference>;
130
+ encoding?: Record<string, Encoding>;
131
+ };
132
+
133
+ type Example = {
134
+ summary?: string;
135
+ description?: string;
136
+ value?: unknown;
137
+ externalValue?: string;
138
+ };
139
+
140
+ type Header = {
141
+ description?: string;
142
+ required?: boolean;
143
+ deprecated?: boolean;
144
+ allowEmptyValue?: boolean;
145
+ style?: "simple";
146
+ explode?: boolean;
147
+ allowReserved?: boolean;
148
+ schema?: OasSchema30;
149
+ content?: Record<string, MediaType>;
150
+ example?: unknown;
151
+ examples: Record<string, Example | Reference>;
152
+ };
153
+
154
+ type Paths = Record<string, PathItem>;
155
+
156
+ type PathItem = {
157
+ $ref?: string;
158
+ summary?: string;
159
+ description?: string;
160
+ servers: Server[];
161
+ parameters: (Parameter | Reference)[];
162
+ get?: Operation;
163
+ put?: Operation;
164
+ post?: Operation;
165
+ delete?: Operation;
166
+ options?: Operation;
167
+ head?: Operation;
168
+ patch?: Operation;
169
+ trace?: Operation;
170
+ };
171
+
172
+ type Operation = {
173
+ tags?: string[];
174
+ summary?: string;
175
+ description?: string;
176
+ externalDocs?: ExternalDocs;
177
+ operationId?: string;
178
+ parameters?: (Parameter | Reference)[];
179
+ requestBody?: RequestBody | Reference;
180
+ responses: Responses;
181
+ callbacks?: Record<string, Callback | Reference>;
182
+ deprecated?: boolean;
183
+ security?: SecurityRequirement[];
184
+ servers?: Server[];
185
+ };
186
+
187
+ type Responses = Record<string, Response | Reference>;
188
+
189
+ type SecurityRequirement = Record<string, string[]>;
190
+
191
+ type Tag = {
192
+ name: string;
193
+ description?: string;
194
+ externalDocs?: ExternalDocs;
195
+ };
196
+
197
+ type Parameter = {
198
+ name: string;
199
+ in: string;
200
+ description?: string;
201
+ required?: boolean;
202
+ deprecated?: boolean;
203
+ allowEmptyValue?: boolean;
204
+ style?: string;
205
+ explode?: boolean;
206
+ allowReserved?: boolean;
207
+ schema?: OasSchema30;
208
+ content?: Record<string, MediaType>;
209
+ example?: unknown;
210
+ examples?: Record<string, Example | Reference>;
211
+ };
212
+
213
+ type RequestBody = {
214
+ description?: string;
215
+ content: Record<string, MediaType>;
216
+ required?: boolean;
217
+ };
218
+
219
+ type SecurityScheme = APIKeySecurityScheme | HTTPSecurityScheme | OAuth2SecurityScheme | OpenIdConnectSecurityScheme;
220
+
221
+ type APIKeySecurityScheme = {
222
+ type: "apiKey";
223
+ name: string;
224
+ in: "header" | "query" | "cookie";
225
+ description?: string;
226
+ };
227
+
228
+ type HTTPSecurityScheme = {
229
+ scheme: string;
230
+ bearerFormat?: string;
231
+ description?: string;
232
+ type: "http";
233
+ };
234
+
235
+ type OAuth2SecurityScheme = {
236
+ type: "oauth2";
237
+ flows: OAuthFlows;
238
+ description?: string;
239
+ };
240
+
241
+ type OpenIdConnectSecurityScheme = {
242
+ type: "openIdConnect";
243
+ openIdConnectUrl: string;
244
+ description?: string;
245
+ };
246
+
247
+ type OAuthFlows = {
248
+ implicit?: ImplicitOAuthFlow;
249
+ password?: PasswordOAuthFlow;
250
+ clientCredentials?: ClientCredentialsFlow;
251
+ authorizationCode?: AuthorizationCodeOAuthFlow;
252
+ };
253
+
254
+ type ImplicitOAuthFlow = {
255
+ authorizationUrl: string;
256
+ refreshUrl?: string;
257
+ scopes: Record<string, string>;
258
+ };
259
+
260
+ type PasswordOAuthFlow = {
261
+ tokenUrl: string;
262
+ refreshUrl?: string;
263
+ scopes: Record<string, string>;
264
+ };
265
+
266
+ type ClientCredentialsFlow = {
267
+ tokenUrl: string;
268
+ refreshUrl?: string;
269
+ scopes: Record<string, string>;
270
+ };
271
+
272
+ type AuthorizationCodeOAuthFlow = {
273
+ authorizationUrl: string;
274
+ tokenUrl: string;
275
+ refreshUrl?: string;
276
+ scopes: Record<string, string>;
277
+ };
278
+
279
+ type Link = {
280
+ operationId?: string;
281
+ operationRef?: string;
282
+ parameters?: Record<string, unknown>;
283
+ requestBody?: unknown;
284
+ description?: string;
285
+ server?: Server;
286
+ };
287
+
288
+ type Callback = Record<string, PathItem>;
289
+
290
+ type Encoding = {
291
+ contentType?: string;
292
+ headers?: Record<string, Header | Reference>;
293
+ style?: "form" | "spaceDelimited" | "pipeDelimited" | "deepObject";
294
+ explode?: boolean;
295
+ allowReserved?: boolean;
296
+ };
297
+
60
298
  export * from "../lib/index.js";
@@ -65,10 +65,6 @@ loadDialect(jsonSchemaVersion, {
65
65
  [jsonSchemaVersion]: true
66
66
  });
67
67
 
68
- loadDialect("https://spec.openapis.org/oas/3.0/schema", {
69
- [jsonSchemaVersion]: true
70
- });
71
-
72
68
  addSchema(dialectSchema);
73
69
 
74
70
  addSchema(schema20210928, "https://spec.openapis.org/oas/3.0/schema");
@@ -85,4 +85,231 @@ type Xml = {
85
85
  wrapped?: boolean;
86
86
  };
87
87
 
88
+ export type OpenApi = {
89
+ openapi: string
90
+ info: Info;
91
+ jsonSchemaDialect?: string;
92
+ servers?: Server[];
93
+ security?: SecurityRequirement[]
94
+ tags?: Tag[];
95
+ externalDocs?: ExternalDocumentation;
96
+ paths?: Record<string, PathItem>;
97
+ webhooks?: Record<string, PathItem | Reference>;
98
+ components?: Components;
99
+ };
100
+
101
+ type Info = {
102
+ title: string;
103
+ summary?: string;
104
+ description?: string;
105
+ termsOfService?: string;
106
+ contact?: Contact;
107
+ license?: License;
108
+ version: string;
109
+ };
110
+
111
+ type Contact = {
112
+ name?: string;
113
+ url?: string;
114
+ email?: string;
115
+ };
116
+
117
+ type License = {
118
+ name: string;
119
+ url?: string;
120
+ identifier?: string;
121
+ };
122
+
123
+ type Server = {
124
+ url: string;
125
+ description?: string;
126
+ variables?: Record<string, ServerVariable>;
127
+ };
128
+
129
+ type ServerVariable = {
130
+ enum?: string[];
131
+ default: string;
132
+ description?: string;
133
+ };
134
+
135
+ type Components = {
136
+ schemas?: Record<string, OasSchema31>;
137
+ responses?: Record<string, Response | Reference>;
138
+ parameters?: Record<string, Parameter | Reference>;
139
+ examples?: Record<string, Example | Reference>;
140
+ requestBodies?: Record<string, RequestBody | Reference>;
141
+ headers?: Record<string, Header | Reference>;
142
+ securitySchemes?: Record<string, SecurityScheme | Reference>;
143
+ links?: Record<string, Link | Reference>;
144
+ callbacks?: Record<string, Callbacks | Reference>;
145
+ pathItems?: Record<string, PathItem | Reference>;
146
+ };
147
+
148
+ type PathItem = {
149
+ summary?: string;
150
+ description?: string;
151
+ servers?: Server[];
152
+ parameters?: (Parameter | Reference)[];
153
+ get?: Operation;
154
+ put?: Operation;
155
+ post?: Operation;
156
+ delete?: Operation;
157
+ options?: Operation;
158
+ head?: Operation;
159
+ patch?: Operation;
160
+ trace?: Operation;
161
+ };
162
+
163
+ type Operation = {
164
+ tags?: string[];
165
+ summary?: string;
166
+ description?: string;
167
+ externalDocs?: ExternalDocumentation;
168
+ operationId?: string;
169
+ parameters?: (Parameter | Reference)[];
170
+ requestBody?: RequestBody | Reference;
171
+ responses?: Record<string, Response | Reference>;
172
+ callbacks?: Record<string, Callbacks | Reference>;
173
+ deprecated?: boolean;
174
+ security?: SecurityRequirement[];
175
+ servers?: Server[];
176
+ };
177
+
178
+ type ExternalDocumentation = {
179
+ description?: string;
180
+ url: string;
181
+ };
182
+
183
+ type Parameter = {
184
+ name: string;
185
+ in: "query" | "header" | "path" | "cookie";
186
+ description?: string;
187
+ required?: boolean;
188
+ deprecated?: boolean;
189
+ allowEmptyValue?: boolean;
190
+ style?: "matrix" | "label" | "form" | "simple" | "spaceDelimited" | "pipeDelimited" | "deepObject";
191
+ explode?: boolean;
192
+ allowReserved?: boolean;
193
+ schema?: OasSchema31;
194
+ content?: Content;
195
+ } & Examples;
196
+
197
+ type RequestBody = {
198
+ description?: string;
199
+ content: Content;
200
+ required?: boolean;
201
+ };
202
+
203
+ type Content = Record<string, MediaType>;
204
+
205
+ type MediaType = {
206
+ schema?: OasSchema31;
207
+ encoding?: Record<string, Encoding>;
208
+ } & Examples;
209
+
210
+ type Encoding = {
211
+ contentType?: string;
212
+ headers?: Record<string, Header | Reference>;
213
+ style?: "form" | "spaceDelimited" | "pipeDelimited" | "deepObject";
214
+ explode?: boolean;
215
+ allowReserved?: boolean;
216
+ };
217
+
218
+ type Response = {
219
+ description: string;
220
+ headers?: Record<string, Header | Reference>;
221
+ content?: Content;
222
+ links?: Record<string, Link | Reference>;
223
+ };
224
+
225
+ type Callbacks = Record<string, PathItem | Reference>;
226
+
227
+ type Example = {
228
+ summary?: string;
229
+ description?: string;
230
+ value?: Json,
231
+ externalValue?: string;
232
+ };
233
+
234
+ type Link = {
235
+ operationRef?: string;
236
+ operationId?: string;
237
+ parameters?: Record<string, string>;
238
+ requestBody?: Json;
239
+ description?: string;
240
+ body?: Server;
241
+ };
242
+
243
+ type Header = {
244
+ description?: string;
245
+ required?: boolean;
246
+ deprecated?: boolean;
247
+ schema?: OpenApi31;
248
+ style?: "simple";
249
+ explode?: boolean;
250
+ content?: Content;
251
+ };
252
+
253
+ type Tag = {
254
+ name: string;
255
+ description?: string;
256
+ externalDocs?: ExternalDocumentation;
257
+ };
258
+
259
+ type Reference = {
260
+ $ref: string;
261
+ summary?: string;
262
+ descriptions?: string;
263
+ };
264
+
265
+ type SecurityScheme = {
266
+ type: "apiKey" | "http" | "mutualTLS" | "oauth2" | "openIdConnect";
267
+ description?: string;
268
+ name?: string;
269
+ in?: "query" | "header" | "cookie";
270
+ scheme?: string;
271
+ bearerFormat?: string;
272
+ flows?: OauthFlows;
273
+ openIdConnectUrl?: string;
274
+ };
275
+
276
+ type OauthFlows = {
277
+ implicit: Implicit;
278
+ Password: Password;
279
+ clientCredentials: ClientCredentials;
280
+ authorizationCode: AuthorizationCode;
281
+ };
282
+
283
+ type Implicit = {
284
+ authorizationUrl: string;
285
+ refreshUrl?: string;
286
+ scopes: Record<string, string>;
287
+ };
288
+
289
+ type Password = {
290
+ tokenUrl: string;
291
+ refreshUrl?: string;
292
+ scopes: Record<string, string>;
293
+ };
294
+
295
+ type ClientCredentials = {
296
+ tokenUrl: string;
297
+ refreshUrl?: string;
298
+ scopes: Record<string, string>;
299
+ };
300
+
301
+ type AuthorizationCode = {
302
+ authorizationUrl: string;
303
+ tokenUrl: string;
304
+ refreshUrl?: string;
305
+ scopes: Record<string, string>;
306
+ };
307
+
308
+ type SecurityRequirement = Record<string, string[]>;
309
+
310
+ type Examples = {
311
+ example?: Json;
312
+ examples?: Record<string, Example | Reference>;
313
+ };
314
+
88
315
  export * from "../lib/index.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyperjump/json-schema",
3
- "version": "1.2.6",
3
+ "version": "1.3.0",
4
4
  "description": "A JSON Schema validator with support for custom keywords, vocabularies, and dialects",
5
5
  "type": "module",
6
6
  "main": "./stable/index.js",