@biblioteksentralen/bmdb-search 0.0.0-beta.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/CHANGELOG.md +3 -0
- package/README.md +83 -0
- package/dist/index.cjs +30 -0
- package/dist/index.d.cts +1040 -0
- package/dist/index.d.ts +1040 -0
- package/dist/index.js +22 -0
- package/package.json +57 -0
package/CHANGELOG.md
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# @biblioteksentralen/bmdb-search
|
|
2
|
+
|
|
3
|
+
TypeScript client for searching Bibliotekenes metadatabrønn (BMDB).
|
|
4
|
+
|
|
5
|
+
## Setup
|
|
6
|
+
|
|
7
|
+
### Server context
|
|
8
|
+
|
|
9
|
+
When used in a non-browser context, the `baseUrl` can be supplied directly, e.g.
|
|
10
|
+
|
|
11
|
+
```typescript
|
|
12
|
+
import { createBmdbSearchClient } from "@biblioteksentralen/bmdb-search";
|
|
13
|
+
|
|
14
|
+
const bmdbSearchClient = createBmdbSearchClient({
|
|
15
|
+
clientIdentifier: "client-unique-description",
|
|
16
|
+
baseUrl: "https://search.data.bs.no",
|
|
17
|
+
});
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
### Browser context
|
|
21
|
+
|
|
22
|
+
In browsers, a content security policy typically has to be taken into account. One approach is to use URL rewrites. If for example using Next.js, this can be achieved using the config file:
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
// next.config.js or similar
|
|
26
|
+
const searchApiHost = "https://search.data.bs.no";
|
|
27
|
+
|
|
28
|
+
const config = {
|
|
29
|
+
// ... other config ...
|
|
30
|
+
rewrites() {
|
|
31
|
+
return [
|
|
32
|
+
{
|
|
33
|
+
source: "/bmdb/api/:path*",
|
|
34
|
+
destination: `${searchApiHost}/bmdb/api/:path*`,
|
|
35
|
+
},
|
|
36
|
+
];
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// File utilizing search client
|
|
41
|
+
import { createBmdbSearchClient } from "@biblioteksentralen/bmdb-search";
|
|
42
|
+
|
|
43
|
+
const bmdbSearchClient = createBmdbSearchClient({ clientIdentifier: "client-unique-description" });
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Usage examples
|
|
47
|
+
|
|
48
|
+
### Search works using client
|
|
49
|
+
|
|
50
|
+
Uses [openapi-fetch](https://openapi-ts.dev/openapi-fetch/) to generate a typed search client.
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
import { createBmdbSearchClient } from "@biblioteksentralen/bmdb-search";
|
|
54
|
+
|
|
55
|
+
const bmdbSearchClient = createBmdbSearchClient({ clientIdentifier: "client-unique-description" });
|
|
56
|
+
const { data, error } = await bmdbSearchClient.GET("/works/search", { query: { query: "test" } });
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
The arguments and response are fully typed.
|
|
60
|
+
|
|
61
|
+
### Search works using react hooks
|
|
62
|
+
|
|
63
|
+
Uses [tanstack](https://tanstack.com/query/latest) for fetch management and [openapi-react-query](https://openapi-ts.dev/openapi-react-query/) for typing.
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
import { createBmdbSearchHooks } from "@biblioteksentralen/bmdb-search";
|
|
67
|
+
|
|
68
|
+
const { useQuery, useInfiniteQuery, useSuspenseQuery, queryOptions } = createBmdbSearchHooks({
|
|
69
|
+
clientIdentifier: "client-unique-description",
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const Component = (props) => {
|
|
73
|
+
const { data, error, isLoading } = useQuery("/works/search", { query: { query: "test" } });
|
|
74
|
+
return <div>Hello</div>
|
|
75
|
+
};
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Options
|
|
79
|
+
|
|
80
|
+
### Client identification policy
|
|
81
|
+
|
|
82
|
+
The API does not require authentication, but clients should identify themselves using a descriptive
|
|
83
|
+
name and a contact address in the `clientIdentifier` string. We will only contact you about usage of the API.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var createFetchClient = require('openapi-fetch');
|
|
4
|
+
var createReactQueryClient = require('openapi-react-query');
|
|
5
|
+
|
|
6
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
7
|
+
|
|
8
|
+
var createFetchClient__default = /*#__PURE__*/_interopDefault(createFetchClient);
|
|
9
|
+
var createReactQueryClient__default = /*#__PURE__*/_interopDefault(createReactQueryClient);
|
|
10
|
+
|
|
11
|
+
// src/client.ts
|
|
12
|
+
var createBmdbSearchClient = ({
|
|
13
|
+
clientIdentifier,
|
|
14
|
+
version = "v2",
|
|
15
|
+
...options
|
|
16
|
+
}) => {
|
|
17
|
+
const baseUrl = `${options.baseUrl?.replace(/(.*)\/$/, "$1") ?? ""}/bmdb/api/${version}/`;
|
|
18
|
+
const client = createFetchClient__default.default({ ...options, baseUrl });
|
|
19
|
+
client.use({
|
|
20
|
+
onRequest({ request }) {
|
|
21
|
+
request.headers.set("client-identifier", clientIdentifier);
|
|
22
|
+
return request;
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
return client;
|
|
26
|
+
};
|
|
27
|
+
var createBmdbSearchHooks = (args) => createReactQueryClient__default.default("searchClient" in args ? args.searchClient : createBmdbSearchClient(args));
|
|
28
|
+
|
|
29
|
+
exports.createBmdbSearchClient = createBmdbSearchClient;
|
|
30
|
+
exports.createBmdbSearchHooks = createBmdbSearchHooks;
|