-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource.ts
96 lines (84 loc) · 2.36 KB
/
resource.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { Anchor } from "./anchor";
import { File } from "./file";
import { JsonSchema } from "./json-schema";
import { Pointer } from "./pointer";
import { Reference } from "./reference";
import { createURL } from "./url";
/**
* The arguments that can be passed to the `Resource` constructor
*/
export interface ResourceArgs {
file: File;
uri?: string | URL;
data?: unknown;
locationInFile?: string[];
}
/**
* A JSON Schema resource that has a URI
*
* @see http://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.4.3.5
*/
export class Resource {
/**
* The JSON Schema that this resource is part of
*/
public schema: JsonSchema;
/**
* The file that contains this resource
*/
public file: File;
/**
* The base URI of the JSON Schema resource
*
* @see http://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.8.2
*/
public uri: URL;
/**
* A pointer to the resource's location in the file
*/
public locationInFile: Pointer;
/**
* The resource data. This can be *any* JavaScript value, but will usually be one of the following:
*
* - `object` if the resource is a JSON Schema document that has already been parsed
*
* - `string` if the resource is in a text-based file that has not yet been parsed.
* This includes JSON, YAML, HTML, SVG, CSV, plain-text, etc.
*
* - `ArrayBuffer` if the resource contains binary data, such as an image
*/
public data: unknown;
/**
* All anchors in the JSON Schema resource
*
* @see http://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.8.2
*/
public anchors: Anchor[] = [];
/**
* All references in the JSON Schema resource
*
* @see http://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.8.2
*/
public references: Reference[] = [];
public constructor(args: ResourceArgs) {
let { uri } = args;
if (uri === undefined) {
// By default, the resource's URI is its file location
uri = args.file.url;
}
else if (!(uri instanceof URL)) {
uri = createURL(uri);
}
this.schema = args.file.schema;
this.file = args.file;
this.uri = uri;
this.locationInFile = new Pointer(args.locationInFile || []);
this.data = args.data;
}
/**
* Returns the resource URI
*/
public toString(): string {
return `${this.uri || "resource"}`;
}
}