@biblioteksentralen/bmdb-search 0.0.0-beta.32 → 0.0.0-beta.34

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
@@ -2,18 +2,104 @@
2
2
 
3
3
  TypeScript client for searching Bibliotekenes metadatabrønn (BMDB) using [openapi-fetch](https://openapi-ts.dev/openapi-fetch/).
4
4
 
5
- ## Usage examples
5
+ ## Setup
6
6
 
7
- ### Setup client
7
+ ### Setup for server-side usage
8
+
9
+ If the client is running in an environment that can keep secrets, i.e. server-side, the bundled
10
+ `clientCredentialsAccessTokenGetter` helper can be used to exchange your long-lived OAuth 2.0
11
+ client credentials for short-lived access tokens:
12
+
13
+ ```typescript
14
+ import { createBmdbFetchClient } from "@biblioteksentralen/bmdb-search/v1";
15
+ import { clientCredentialsAccessTokenGetter } from "@biblioteksentralen/bmdb-search/server";
16
+
17
+ const bmdbSearchClient = createBmdbFetchClient({
18
+ host: "https://search.bmdb.no",
19
+ auth: clientCredentialsAccessTokenGetter({
20
+ clientId: process.env.BMDB_CORDATA_API_CLIENT_ID,
21
+ clientSecret: process.env.BMDB_CORDATA_API_CLIENT_SECRET,
22
+ }),
23
+ });
24
+ ```
25
+
26
+ Note: If you are planning to access pre-production environments, see notes below on how to
27
+ connect to those below.
28
+
29
+ ### Setup for client-side (frontend) usage
30
+
31
+ To use the BMDB search client in a client-side environment, i.e. one that cannot keep secrets,
32
+ the client needs to fetch access tokens from a server-side route that you manage.
33
+
34
+ Example:
35
+
36
+ ```typescript
37
+ // clients/bmdbClient.ts
38
+ import { createBmdbFetchClient } from "@biblioteksentralen/bmdb-search/v1";
39
+
40
+ export const bmdbBrowserClient = createBmdbFetchClient({
41
+ host: "https://search.bmdb.no",
42
+ auth: async () => {
43
+ // Fetches access token from an endpoint you manage
44
+ const response = await fetch("/api/bmdb/access-token");
45
+
46
+ if (!response.ok) {
47
+ throw new Error("Failed to fetch BMDB access token");
48
+ }
49
+
50
+ const { accessToken } = await response.json();
51
+ return accessToken;
52
+ },
53
+ });
54
+ ```
55
+
56
+ Note that the client's auth middleware caches tokens in memory, only calling the endpoint when a new
57
+ token is needed. For this reason, it's recommended to not create a new client for each request, but
58
+ reuse it.
59
+
60
+ The implementation of the server-side API route depends on the programming language and framework
61
+ you use. Below is a simple example for Next.js with App Router:
8
62
 
9
63
  ```typescript
10
- import { createBmdbFetchClient } from "@biblioteksentralen/bmdb-search";
64
+ // app/api/bmdb/access-token/route.ts
65
+ import { clientCredentialsAccessTokenGetter } from "@biblioteksentralen/bmdb-search/server";
66
+
67
+ const getAccessToken = clientCredentialsAccessTokenGetter({
68
+ clientId: process.env.BMDB_CORDATA_API_CLIENT_ID,
69
+ clientSecret: process.env.BMDB_CORDATA_API_CLIENT_SECRET,
70
+ });
71
+
72
+ export async function GET() {
73
+ try {
74
+ // Ask yourself if you want to restrict clients before returning a token
75
+ const accessToken = await getAccessToken();
76
+ return Response.json({ accessToken });
77
+ } catch (err) {
78
+ console.error(`Failed to fetch access token`, err);
79
+ return Response.json({ error: "Failed to fetch access token" }, { status: 500 });
80
+ }
81
+ }
82
+ ```
83
+
84
+ ### Connecting to pre-production environments (dev/test/staging)
85
+
86
+ When connecting to pre-production environments, the OAuth 2.0 Authorization Server Token URL must be
87
+ be specified manually through the `tokenUrl` parameter. Example for connecting to the test/staging
88
+ environment:
11
89
 
90
+ ```typescript
12
91
  const bmdbSearchClient = createBmdbFetchClient({
13
- host: "https://search.bmdb.no/",
92
+ host: "https://search-staging.bmdb.no",
93
+ auth: clientCredentialsAccessTokenGetter({
94
+ clientId: process.env.BMDB_CORDATA_API_CLIENT_ID,
95
+ clientSecret: process.env.BMDB_CORDATA_API_CLIENT_SECRET,
96
+ tokenUrl: "https://biblioteksentralen-uat.eu.kinde.com/oauth2/token",
97
+ }),
14
98
  });
15
99
  ```
16
100
 
101
+ ## Usage
102
+
17
103
  ### Search works
18
104
 
19
105
  ```typescript
@@ -53,26 +139,9 @@ const { data, error } = await bmdbSearchClient.GET("/{searchProfile}/agents/sear
53
139
  const { data, error } = await bmdbSearchClient.GET("/{searchProfile}/agents/search", {
54
140
  params: {
55
141
  path: { searchProfile: "global" },
56
- query: { facet: "role", size: 0, facet_terms_size: 20 },
142
+ query: { facet: ["role"], size: 0, facet_terms_size: 20 },
57
143
  },
58
144
  });
59
145
  ```
60
146
 
61
147
  The arguments and response are fully typed.
62
-
63
- ## Usage with Next.js
64
-
65
- The queries have to happen in a server context. For this purpose we can create a server client:
66
-
67
- ```typescript
68
- "server only";
69
-
70
- import { createBmdbFetchClient } from "@biblioteksentralen/bmdb-search";
71
- import { headers } from "next/headers";
72
-
73
- export const getBmdbServerFetchClient = async () =>
74
- createBmdbFetchClient({
75
- host: "https://search.bmdb.no/",
76
- headers: await headers(),
77
- });
78
- ```