@kalyptos/bifrost 5.0.0 → 5.1.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
@@ -1,8 +1,9 @@
1
1
  # @kalyptos/bifrost
2
2
 
3
- TypeScript client for the Bifrost API. Full reference: <https://bifrost.kalyptos.io/docs>.
3
+ TypeScript client for the Bifrost API.
4
4
 
5
- ESM with no runtime dependencies. It needs a global `fetch` (Node 18 or later, or a browser).
5
+ ESM with no runtime dependencies. It uses the global `fetch` (Node 18 or later, or a browser) unless
6
+ you pass your own.
6
7
 
7
8
  ```sh
8
9
  npm install @kalyptos/bifrost
@@ -20,8 +21,10 @@ const { matches } = await bifrost.resolve({ input: 'Rådhuspladsen 1, København
20
21
 
21
22
  `resolve`, `search` and `project` take one request and return one result, or take an array and
22
23
  return a batch. A batch item that fails is still a result: `{ input, matches: [], error }`.
23
- `token` is optional: a string, or a function that gives the token for each request. A response that
24
- is not 2xx throws `BifrostError` with `status` and `detail`.
24
+ `token` is optional: a string, or a function that gives the token for each request. `fetch` is
25
+ optional too: the client sends every request through it, for retries, logging or tests. Every call,
26
+ `compose` too, takes `{ signal }` as its last argument, to cancel it. A response that is not 2xx
27
+ throws `BifrostError` with `status` and `detail`.
25
28
 
26
29
  ## Compose
27
30
 
package/dist/client.d.ts CHANGED
@@ -4,6 +4,10 @@ export type Token = string | (() => string | Promise<string>);
4
4
  export interface BifrostOptions {
5
5
  baseUrl: string;
6
6
  token?: Token;
7
+ fetch?: typeof fetch;
8
+ }
9
+ export interface CallOptions {
10
+ signal?: AbortSignal;
7
11
  }
8
12
  export interface ValidationIssue {
9
13
  loc: (string | number)[];
@@ -18,12 +22,12 @@ export declare class BifrostError extends Error {
18
22
  }
19
23
  export declare class Bifrost {
20
24
  #private;
21
- constructor({ baseUrl, token }: BifrostOptions);
22
- resolve(body: ResolveRequest): Promise<MatchResult>;
23
- resolve(body: ResolveRequest[]): Promise<MatchResult[]>;
24
- search<T extends SearchTarget>(body: SearchRequest<string, T>): Promise<MatchResult<SearchMatch<T>>>;
25
- search<T extends SearchTarget>(body: SearchRequest<string, T>[]): Promise<MatchResult<SearchMatch<T>>[]>;
26
- project(body: ProjectRequest): Promise<ProjectResult>;
27
- project(body: ProjectRequest[]): Promise<ProjectResult[]>;
28
- compose<S extends Shape>(fn: (q: Recorder) => S): Promise<Decoded<S>>;
25
+ constructor({ baseUrl, token, fetch }: BifrostOptions);
26
+ resolve(body: ResolveRequest, options?: CallOptions): Promise<MatchResult>;
27
+ resolve(body: ResolveRequest[], options?: CallOptions): Promise<MatchResult[]>;
28
+ search<T extends SearchTarget>(body: SearchRequest<string, T>, options?: CallOptions): Promise<MatchResult<SearchMatch<T>>>;
29
+ search<T extends SearchTarget>(body: SearchRequest<string, T>[], options?: CallOptions): Promise<MatchResult<SearchMatch<T>>[]>;
30
+ project(body: ProjectRequest, options?: CallOptions): Promise<ProjectResult>;
31
+ project(body: ProjectRequest[], options?: CallOptions): Promise<ProjectResult[]>;
32
+ compose<S extends Shape>(fn: (q: Recorder) => S, options?: CallOptions): Promise<Decoded<S>>;
29
33
  }
package/dist/client.js CHANGED
@@ -12,33 +12,38 @@ export class BifrostError extends Error {
12
12
  export class Bifrost {
13
13
  #baseUrl;
14
14
  #token;
15
- constructor({ baseUrl, token }) {
15
+ #fetch;
16
+ constructor({ baseUrl, token, fetch }) {
16
17
  this.#baseUrl = baseUrl.replace(/\/+$/, '');
17
18
  this.#token = token;
19
+ this.#fetch = fetch;
18
20
  }
19
- resolve(body) {
20
- return this.#post('/resolve', body);
21
+ resolve(body, options) {
22
+ return this.#post('/resolve', body, options);
21
23
  }
22
- search(body) {
23
- return this.#post('/search', body);
24
+ search(body, options) {
25
+ return this.#post('/search', body, options);
24
26
  }
25
- project(body) {
26
- return this.#post('/project', body);
27
+ project(body, options) {
28
+ return this.#post('/project', body, options);
27
29
  }
28
- async compose(fn) {
30
+ async compose(fn, options) {
29
31
  const { steps, decode } = record(fn);
30
- const outputs = (await this.#post('/compose', { steps }));
32
+ const outputs = (await this.#post('/compose', { steps }, options));
31
33
  return decode(outputs);
32
34
  }
33
- async #post(path, body) {
35
+ async #post(path, body, { signal } = {}) {
34
36
  const headers = { 'content-type': 'application/json' };
35
37
  const token = typeof this.#token === 'function' ? await this.#token() : this.#token;
36
38
  if (token)
37
39
  headers.authorization = `Bearer ${token}`;
38
- const res = await fetch(this.#baseUrl + path, {
40
+ // called unbound and looked up per call: a browser rejects fetch with a foreign this
41
+ const send = this.#fetch ?? globalThis.fetch;
42
+ const res = await send(this.#baseUrl + path, {
39
43
  method: 'POST',
40
44
  headers,
41
- body: JSON.stringify(body)
45
+ body: JSON.stringify(body),
46
+ ...(signal && { signal })
42
47
  });
43
48
  if (!res.ok)
44
49
  throw new BifrostError(res.status, await detailOf(res));
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  export { Bifrost, BifrostError } from './client.js';
2
- export type { BifrostOptions, ErrorDetail, Token, ValidationIssue } from './client.js';
2
+ export type { BifrostOptions, CallOptions, ErrorDetail, Token, ValidationIssue } from './client.js';
3
3
  export type { CodeSlot, Decoded, Handle, Mapped, ProjectBody, Recorder, RefHandle, Shape, Slot, Step } from './compose.js';
4
4
  export type * from './types.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kalyptos/bifrost",
3
- "version": "5.0.0",
3
+ "version": "5.1.0",
4
4
  "description": "TypeScript client for the Bifrost Danish address API",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {