@neverblink/ohi 0.18.0 → 0.18.1

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
@@ -1,17 +1,27 @@
1
- # ohi SDK
1
+ # ohi SDK for TypeScript
2
2
 
3
- The ohi SDK lets Python and TypeScript applications search publications, inspect metadata, download selected files and save supplied file selections.
3
+ Search ontologies on [ohi.dev](https://ohi.dev), inspect their metadata, download selected files and save file selections.
4
4
 
5
- Install on Python 3.10 or later:
5
+ Works with Node 22 or later and browsers with Fetch. Browsers need CORS access to the catalog, publication and artifact endpoints.
6
6
 
7
7
  ```sh
8
- python -m pip install ohi-sdk
8
+ npm install @neverblink/ohi
9
9
  ```
10
10
 
11
- Install for Node 22 or later, or a browser application:
11
+ Search, inspect the first hit and download its first file:
12
12
 
13
- ```sh
14
- npm install @neverblink/ohi
13
+ ```typescript
14
+ import { CatalogClient } from "@neverblink/ohi";
15
+
16
+ const client = new CatalogClient();
17
+ const page = await client.search({ q: "sensors", page: 1, per_page: 10 });
18
+ if (page.hits.length > 0) {
19
+ const publication = await client.inspect(page.hits[0].base_url);
20
+ const artifact = publication.artifacts[0];
21
+ const content = await client.get(artifact.url);
22
+ }
15
23
  ```
16
24
 
17
- See the [SDK guide](https://github.com/NeverBlink-OSS/ohi/blob/main/ohi/README.md) for usage, browser requirements and API behavior.
25
+ `get` returns the file's bytes as a `Uint8Array`. File reading and writing are in `@neverblink/ohi/node`.
26
+
27
+ **See the [SDK documentation](https://ohi.dev/docs/reference/sdks/)** for configuration, file selections, metadata and errors.
package/dist/catalog.d.ts CHANGED
@@ -2,7 +2,7 @@ import type { Publication } from "./generated.js";
2
2
  import type { CatalogConfig } from "./generated_config.js";
3
3
  import type { CatalogError } from "./generated_error.js";
4
4
  import type { SearchRequest } from "./generated_search_request.js";
5
- import type { SearchResult } from "./generated_search.js";
5
+ import type { SearchHit, SearchResult } from "./generated_search.js";
6
6
  export declare class CatalogException extends Error {
7
7
  readonly error: CatalogError;
8
8
  constructor(error: CatalogError, options?: ErrorOptions);
@@ -11,8 +11,17 @@ export declare class CatalogException extends Error {
11
11
  export declare class CatalogClient {
12
12
  private readonly config;
13
13
  private readonly timeoutMs;
14
+ private readonly base;
15
+ private readonly latestUrl;
14
16
  constructor(config?: CatalogConfig);
15
17
  search(request: SearchRequest): Promise<SearchResult>;
18
+ /** Return the search hit for the latest version of `namespace/name`, or undefined when the catalog has no such
19
+ * ontology.
20
+ */
21
+ latest(reference: string): Promise<SearchHit | undefined>;
22
+ /** Fetch the publication at an absolute HTTP(S) URL, of the release `namespace/name@version`, or of the latest
23
+ * version of `namespace/name`.
24
+ */
16
25
  inspect(baseUrl: string): Promise<Publication>;
17
26
  get(url: string): Promise<Uint8Array>;
18
27
  private request;
package/dist/catalog.js CHANGED
@@ -1,4 +1,6 @@
1
1
  import { decodePublication } from "./publication.js";
2
+ const searchPath = "/search/collections/packages/documents/search";
3
+ const referencePattern = /^([^/@\p{White_Space}`]+)\/([^/@\p{White_Space}`]+)(?:@([^/@\p{White_Space}+`]+))?$/u;
2
4
  export class CatalogException extends Error {
3
5
  error;
4
6
  constructor(error, options) {
@@ -59,6 +61,8 @@ function searchResult(bytes, page, perPage) {
59
61
  export class CatalogClient {
60
62
  config;
61
63
  timeoutMs;
64
+ base;
65
+ latestUrl;
62
66
  constructor(config = {}) {
63
67
  const url = config.search_url;
64
68
  if ((config.search_url === undefined) !== (config.query_by === undefined)) {
@@ -71,6 +75,10 @@ export class CatalogClient {
71
75
  throw new CatalogException({ code: "INVALID_INPUT", operation: "CONFIGURE", url,
72
76
  message: "Supply a search_url without a query or fragment." });
73
77
  }
78
+ if (!url.endsWith(searchPath)) {
79
+ throw new CatalogException({ code: "INVALID_INPUT", operation: "CONFIGURE", url,
80
+ message: `Supply a search_url that ends with ${searchPath}.` });
81
+ }
74
82
  }
75
83
  this.config = {
76
84
  ...config,
@@ -78,9 +86,11 @@ export class CatalogClient {
78
86
  query_by: config.query_by ?? "title,name,description,namespace",
79
87
  };
80
88
  this.timeoutMs = config.timeout_ms ?? 30000;
89
+ this.base = this.config.search_url.slice(0, -searchPath.length);
90
+ this.latestUrl = `${this.base}/search/collections/packages_latest/documents/search`;
81
91
  }
82
92
  async search(request) {
83
- const endpoint = this.config.search_url;
93
+ const endpoint = request.all_releases ? this.config.search_url : this.latestUrl;
84
94
  const page = request.page ?? 1;
85
95
  const perPage = request.per_page ?? 10;
86
96
  const url = new URL(endpoint);
@@ -105,13 +115,52 @@ export class CatalogClient {
105
115
  message: "The search response is invalid." }, { cause });
106
116
  }
107
117
  }
118
+ /** Return the search hit for the latest version of `namespace/name`, or undefined when the catalog has no such
119
+ * ontology.
120
+ */
121
+ async latest(reference) {
122
+ const parts = referencePattern.exec(reference);
123
+ if (parts === null || parts[3] !== undefined) {
124
+ throw new CatalogException({ code: "INVALID_INPUT", operation: "SEARCH", url: reference,
125
+ message: "Supply an ontology reference namespace/name without a version." });
126
+ }
127
+ const [, namespace, name] = parts;
128
+ const filter = `namespace:=\`${namespace}\` && name:=\`${name}\``;
129
+ const result = await this.search({ q: "*", filter_by: filter });
130
+ // The := filter ignores case and separators, so look-alikes such as w3c/DCAT can match.
131
+ return result.hits.find((hit) => hit.namespace === namespace && hit.name === name);
132
+ }
133
+ /** Fetch the publication at an absolute HTTP(S) URL, of the release `namespace/name@version`, or of the latest
134
+ * version of `namespace/name`.
135
+ */
108
136
  async inspect(baseUrl) {
109
- const bytes = await this.request(httpUrl(baseUrl, "INSPECT"), "INSPECT", baseUrl);
137
+ let url = baseUrl;
138
+ const parts = referencePattern.exec(baseUrl);
139
+ if (parts !== null) {
140
+ const [, namespace, name, version] = parts;
141
+ if (version !== undefined) {
142
+ url = `${this.base}/files/packages/${[namespace, name, version].map(encodeURIComponent).join("/")}/`;
143
+ }
144
+ else {
145
+ const hit = await this.latest(baseUrl);
146
+ if (hit === undefined) {
147
+ throw new CatalogException({ code: "HTTP_ERROR", operation: "INSPECT", url: baseUrl, status: 404,
148
+ message: `No ontology matches ${baseUrl}.` });
149
+ }
150
+ url = hit.base_url;
151
+ }
152
+ }
153
+ else if (!/^https?:\/\//i.test(baseUrl)) {
154
+ // new URL repairs "http:/host" and strips tabs, so require the literal prefix before parsing.
155
+ throw new CatalogException({ code: "INVALID_INPUT", operation: "INSPECT", url: baseUrl,
156
+ message: "Supply an absolute HTTP(S) URL or an ontology reference namespace/name[@version]." });
157
+ }
158
+ const bytes = await this.request(httpUrl(url, "INSPECT"), "INSPECT", url);
110
159
  try {
111
160
  return decodePublication(new TextDecoder("utf-8", { fatal: true }).decode(bytes));
112
161
  }
113
162
  catch (cause) {
114
- throw new CatalogException({ code: "INVALID_RESPONSE", operation: "INSPECT", url: baseUrl, status: 200,
163
+ throw new CatalogException({ code: "INVALID_RESPONSE", operation: "INSPECT", url, status: 200,
115
164
  message: "The publication metadata is invalid." }, { cause });
116
165
  }
117
166
  }
@@ -34,7 +34,7 @@ export interface Publication {
34
34
  keywords: string[];
35
35
  /** Reported license names or standard identifiers such as Apache-2.0. Empty means no information supplied. The list specifies neither AND nor OR combination semantics. */
36
36
  licenses: string[];
37
- /** Short catalog identifier, qualified by namespace. The namespace/name pair identifies the same ontology across releases. URLs must not be derived from this identifier. */
37
+ /** Short catalog identifier, qualified by namespace. The namespace/name pair identifies the same ontology across releases. */
38
38
  name: string;
39
39
  /** Namespace qualifying the ontology name, such as w3c in w3c/prov-o. */
40
40
  namespace: string;
@@ -26,7 +26,7 @@ export interface SearchHit {
26
26
  keywords?: string[];
27
27
  /** Reported license names or standard identifiers such as Apache-2.0. Empty means no information supplied. The list specifies neither AND nor OR combination semantics. */
28
28
  licenses: string[];
29
- /** Short catalog identifier, qualified by namespace. The namespace/name pair identifies the same ontology across releases. URLs must not be derived from this identifier. */
29
+ /** Short catalog identifier, qualified by namespace. The namespace/name pair identifies the same ontology across releases. */
30
30
  name: string;
31
31
  /** Namespace qualifying the ontology name, such as w3c in w3c/prov-o. */
32
32
  namespace: string;
@@ -1,5 +1,7 @@
1
1
  /** Query and options for retrieving one page of ontology publications. */
2
2
  export interface SearchRequest {
3
+ /** Search every release instead of each ontology's latest version. */
4
+ all_releases?: boolean;
3
5
  /** Typesense facet expression over facetable fields. */
4
6
  facet_by?: string;
5
7
  /** Typesense filter expression over filterable fields. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@neverblink/ohi",
3
- "version": "0.18.0",
3
+ "version": "0.18.1",
4
4
  "description": "Search ontologies on ohi.dev, inspect their metadata and download their files.",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://ohi.dev/docs/reference/sdks/",