@kalyptos/bifrost 0.0.0-stage → 5.0.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/LICENSE +202 -0
- package/README.md +58 -2
- package/dist/client.d.ts +29 -0
- package/dist/client.js +57 -0
- package/dist/compose.d.ts +73 -0
- package/dist/compose.js +110 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +1 -0
- package/dist/types.d.ts +157 -0
- package/dist/types.js +2 -0
- package/package.json +29 -5
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2026 Kalyptos
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,59 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @kalyptos/bifrost
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
TypeScript client for the Bifrost API. Full reference: <https://bifrost.kalyptos.io/docs>.
|
|
4
|
+
|
|
5
|
+
ESM with no runtime dependencies. It needs a global `fetch` (Node 18 or later, or a browser).
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @kalyptos/bifrost
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Calls
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { Bifrost } from '@kalyptos/bifrost';
|
|
15
|
+
|
|
16
|
+
const bifrost = new Bifrost({ baseUrl: 'https://bifrost.example.com' });
|
|
17
|
+
|
|
18
|
+
const { matches } = await bifrost.resolve({ input: 'Rådhuspladsen 1, København' });
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
`resolve`, `search` and `project` take one request and return one result, or take an array and
|
|
22
|
+
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`.
|
|
25
|
+
|
|
26
|
+
## Compose
|
|
27
|
+
|
|
28
|
+
`compose` sends several calls as one `POST /compose`. Later calls read earlier outputs, and the
|
|
29
|
+
result has the shape the callback returns:
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
const rows = await bifrost.compose((q) =>
|
|
33
|
+
q.search({ input: 'Vestergade', target: 'street', limit: 8 }).matches.map((s) => ({
|
|
34
|
+
street: s.result,
|
|
35
|
+
kommune: q.project({ input: s.ref, target: 'kommune' }).matches[0].result
|
|
36
|
+
}))
|
|
37
|
+
);
|
|
38
|
+
// rows: { street: string; kommune: string | undefined }[]
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
The callback runs once, synchronously, and only records. `q` calls and the values you reach from
|
|
42
|
+
them are handles: paths into outputs that do not exist yet, not data.
|
|
43
|
+
|
|
44
|
+
- Return a step, any path into one, a `map`, or a record of these, nested freely. A path that finds
|
|
45
|
+
nothing gives `undefined`, so its type adds `undefined` once it indexes an array or passes an
|
|
46
|
+
optional or null field.
|
|
47
|
+
- A handle fits a plain string field (`input`, a `components` value, a ref `id`), not `target`,
|
|
48
|
+
`kind` or a number. A handle to a number fits only a search `input` and a ref `id`. A project
|
|
49
|
+
`input` also takes a whole ref, a match's `m.ref` or a project output's `input`, and
|
|
50
|
+
`m.refs.<kind>`, which sends that kind with the id.
|
|
51
|
+
- Every match's `ref` is a `Ref`, except from a `stednavn` search: a named place is no project
|
|
52
|
+
input. With a `target` that is not a literal, narrow on `ref.kind` first.
|
|
53
|
+
- `map` runs its callback once for each element on the server. The callback can make `q` calls and
|
|
54
|
+
return anything from the element or from those calls, but one call cannot feed another. If it
|
|
55
|
+
returns one of its own calls, `map` gives that step over the whole array, and you can `map` it
|
|
56
|
+
again. Returning a step made outside the callback throws. A `map` cannot nest.
|
|
57
|
+
- A failed call is an empty result with `error`, for example `"unresolved reference"` when a handle
|
|
58
|
+
found nothing to send. A `map` over a path that is not an array gives `[]`.
|
|
59
|
+
- `geometry: true` in a batch or inside a `map` throws when the callback runs.
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type Decoded, type Recorder, type Shape } from './compose.js';
|
|
2
|
+
import type { MatchResult, ProjectRequest, ProjectResult, ResolveRequest, SearchMatch, SearchRequest, SearchTarget } from './types.js';
|
|
3
|
+
export type Token = string | (() => string | Promise<string>);
|
|
4
|
+
export interface BifrostOptions {
|
|
5
|
+
baseUrl: string;
|
|
6
|
+
token?: Token;
|
|
7
|
+
}
|
|
8
|
+
export interface ValidationIssue {
|
|
9
|
+
loc: (string | number)[];
|
|
10
|
+
msg: string;
|
|
11
|
+
type: string;
|
|
12
|
+
}
|
|
13
|
+
export type ErrorDetail = string | ValidationIssue[];
|
|
14
|
+
export declare class BifrostError extends Error {
|
|
15
|
+
readonly status: number;
|
|
16
|
+
readonly detail: ErrorDetail;
|
|
17
|
+
constructor(status: number, detail: ErrorDetail);
|
|
18
|
+
}
|
|
19
|
+
export declare class Bifrost {
|
|
20
|
+
#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>>;
|
|
29
|
+
}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { record } from './compose.js';
|
|
2
|
+
export class BifrostError extends Error {
|
|
3
|
+
status;
|
|
4
|
+
detail;
|
|
5
|
+
constructor(status, detail) {
|
|
6
|
+
super(`${status}: ${typeof detail === 'string' ? detail : JSON.stringify(detail)}`);
|
|
7
|
+
this.name = 'BifrostError';
|
|
8
|
+
this.status = status;
|
|
9
|
+
this.detail = detail;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export class Bifrost {
|
|
13
|
+
#baseUrl;
|
|
14
|
+
#token;
|
|
15
|
+
constructor({ baseUrl, token }) {
|
|
16
|
+
this.#baseUrl = baseUrl.replace(/\/+$/, '');
|
|
17
|
+
this.#token = token;
|
|
18
|
+
}
|
|
19
|
+
resolve(body) {
|
|
20
|
+
return this.#post('/resolve', body);
|
|
21
|
+
}
|
|
22
|
+
search(body) {
|
|
23
|
+
return this.#post('/search', body);
|
|
24
|
+
}
|
|
25
|
+
project(body) {
|
|
26
|
+
return this.#post('/project', body);
|
|
27
|
+
}
|
|
28
|
+
async compose(fn) {
|
|
29
|
+
const { steps, decode } = record(fn);
|
|
30
|
+
const outputs = (await this.#post('/compose', { steps }));
|
|
31
|
+
return decode(outputs);
|
|
32
|
+
}
|
|
33
|
+
async #post(path, body) {
|
|
34
|
+
const headers = { 'content-type': 'application/json' };
|
|
35
|
+
const token = typeof this.#token === 'function' ? await this.#token() : this.#token;
|
|
36
|
+
if (token)
|
|
37
|
+
headers.authorization = `Bearer ${token}`;
|
|
38
|
+
const res = await fetch(this.#baseUrl + path, {
|
|
39
|
+
method: 'POST',
|
|
40
|
+
headers,
|
|
41
|
+
body: JSON.stringify(body)
|
|
42
|
+
});
|
|
43
|
+
if (!res.ok)
|
|
44
|
+
throw new BifrostError(res.status, await detailOf(res));
|
|
45
|
+
return res.json();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
async function detailOf(res) {
|
|
49
|
+
const text = await res.text();
|
|
50
|
+
try {
|
|
51
|
+
const { detail } = JSON.parse(text);
|
|
52
|
+
return detail ?? text;
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
return text;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { MatchResult, ProjectRequest, ProjectResult, ProjectRoot, Ref, ResolveRequest, SearchMatch, SearchRequest, SearchTarget } from './types.js';
|
|
2
|
+
declare const value: unique symbol;
|
|
3
|
+
declare const step: unique symbol;
|
|
4
|
+
declare const kind: unique symbol;
|
|
5
|
+
export type Handle<T, U = never> = {
|
|
6
|
+
readonly [value]: T | U;
|
|
7
|
+
} & Navigate<T, U | Gap<T>>;
|
|
8
|
+
export type Step<T> = Handle<T> & {
|
|
9
|
+
readonly [step]: T;
|
|
10
|
+
};
|
|
11
|
+
export type Mapped<T> = {
|
|
12
|
+
readonly [value]: T[];
|
|
13
|
+
};
|
|
14
|
+
export type Shape = {
|
|
15
|
+
readonly [value]: unknown;
|
|
16
|
+
} | {
|
|
17
|
+
readonly [key: string]: Shape;
|
|
18
|
+
};
|
|
19
|
+
export type Decoded<S> = S extends {
|
|
20
|
+
readonly [value]: infer T;
|
|
21
|
+
} ? T : {
|
|
22
|
+
[K in keyof S]: Decoded<S[K]>;
|
|
23
|
+
};
|
|
24
|
+
export type Slot = string | Handle<string | null, undefined>;
|
|
25
|
+
export type CodeSlot = string | Handle<string | number | null, undefined>;
|
|
26
|
+
export type RefHandle = {
|
|
27
|
+
readonly [value]: string | undefined;
|
|
28
|
+
readonly [kind]: ProjectRoot;
|
|
29
|
+
};
|
|
30
|
+
export interface ProjectBody extends Omit<ProjectRequest<Slot>, 'input'> {
|
|
31
|
+
input: Ref<CodeSlot> | RefHandle | Handle<Ref | null, undefined>;
|
|
32
|
+
}
|
|
33
|
+
type Gap<T> = [Extract<T, null | undefined>] extends [never] ? never : undefined;
|
|
34
|
+
type Optional<T, K extends keyof T> = {} extends Pick<T, K> ? undefined : never;
|
|
35
|
+
type Navigate<T, U> = NonNullable<T> extends readonly (infer E)[] ? Items<E, U> : NonNullable<T> extends object ? Fields<NonNullable<T>, U> : unknown;
|
|
36
|
+
type Fields<T, U> = {
|
|
37
|
+
readonly [K in keyof T]-?: K extends 'refs' ? Kinds<Required<T>[K], U | Optional<T, K>> : Handle<Required<T>[K], U | Optional<T, K>>;
|
|
38
|
+
};
|
|
39
|
+
type Kinds<R, U> = R extends readonly unknown[] ? Handle<R, U> : Handle<R, U> & {
|
|
40
|
+
readonly [K in keyof R]-?: {
|
|
41
|
+
readonly [kind]: K;
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
type Digit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
|
|
45
|
+
type Index = Digit | `${Exclude<Digit, '0'>}${Digit}` | `${Exclude<Digit, '0'>}${Digit}${Digit}`;
|
|
46
|
+
type Items<E, U> = {
|
|
47
|
+
readonly [I in Index]: Handle<E, U | undefined>;
|
|
48
|
+
} & {
|
|
49
|
+
readonly [n: number]: Handle<E, U | undefined>;
|
|
50
|
+
map<R extends Shape>(fn: (element: Handle<E>) => R): R extends {
|
|
51
|
+
readonly [step]: infer T;
|
|
52
|
+
} ? Step<T[]> : Mapped<Decoded<R>>;
|
|
53
|
+
};
|
|
54
|
+
export interface Recorder {
|
|
55
|
+
resolve(body: ResolveRequest<Slot>): Step<MatchResult>;
|
|
56
|
+
resolve(body: ResolveRequest<Slot>[]): Step<MatchResult[]>;
|
|
57
|
+
search<T extends SearchTarget>(body: SearchRequest<CodeSlot, T>): Step<MatchResult<SearchMatch<T>>>;
|
|
58
|
+
search<T extends SearchTarget>(body: SearchRequest<CodeSlot, T>[]): Step<MatchResult<SearchMatch<T>>[]>;
|
|
59
|
+
project(body: ProjectBody): Step<ProjectResult<Ref | null>>;
|
|
60
|
+
project(body: ProjectBody[]): Step<ProjectResult<Ref | null>[]>;
|
|
61
|
+
}
|
|
62
|
+
type Op = 'resolve' | 'search' | 'project';
|
|
63
|
+
interface WireStep {
|
|
64
|
+
id: string;
|
|
65
|
+
op: Op;
|
|
66
|
+
body: unknown;
|
|
67
|
+
each?: string;
|
|
68
|
+
}
|
|
69
|
+
export declare function record(fn: (q: Recorder) => Shape): {
|
|
70
|
+
steps: WireStep[];
|
|
71
|
+
decode: (outputs: Record<string, unknown>) => unknown;
|
|
72
|
+
};
|
|
73
|
+
export {};
|
package/dist/compose.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
const isRecord = (v) => typeof v === 'object' && v !== null && Object.getPrototypeOf(v) === Object.prototype;
|
|
2
|
+
// mirrors the server's walk; a missing value reads undefined
|
|
3
|
+
const walk = (v, path) => path.reduce((v, seg) => typeof seg === 'number'
|
|
4
|
+
? Array.isArray(v) ? v[seg] : undefined
|
|
5
|
+
: isRecord(v) && Object.hasOwn(v, seg) ? v[seg] : undefined, v);
|
|
6
|
+
const text = ({ root, path }) => root + path.map((seg) => (typeof seg === 'number' ? `[${seg}]` : `.${seg}`)).join('');
|
|
7
|
+
export function record(fn) {
|
|
8
|
+
const steps = [];
|
|
9
|
+
const nodes = new Map();
|
|
10
|
+
const maps = new Map();
|
|
11
|
+
let mapping;
|
|
12
|
+
const live = (node, scope = mapping) => {
|
|
13
|
+
if (node.scope && node.scope !== scope)
|
|
14
|
+
throw new Error('handle used outside its map callback');
|
|
15
|
+
};
|
|
16
|
+
const handle = (node) => {
|
|
17
|
+
const proxy = new Proxy({}, {
|
|
18
|
+
get(_, key) {
|
|
19
|
+
// then must read undefined, or an accidental await never settles
|
|
20
|
+
if (typeof key === 'symbol' || key === 'then')
|
|
21
|
+
return undefined;
|
|
22
|
+
if (key === 'toJSON')
|
|
23
|
+
return () => ({ from: wire(node) });
|
|
24
|
+
if (key === 'map')
|
|
25
|
+
return (fn) => map(node, fn);
|
|
26
|
+
const seg = /^\d+$/.test(key) ? Number(key) : key;
|
|
27
|
+
return handle({ ...node, path: [...node.path, seg] });
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
nodes.set(proxy, node);
|
|
31
|
+
return proxy;
|
|
32
|
+
};
|
|
33
|
+
const wire = (node) => {
|
|
34
|
+
live(node);
|
|
35
|
+
// an each step sees only its own element, never a sibling step's output for it
|
|
36
|
+
if (node.scope && node.root !== '@')
|
|
37
|
+
throw new Error('a step made in map can only be returned');
|
|
38
|
+
return text(node);
|
|
39
|
+
};
|
|
40
|
+
// one decoder per shape, built at record time so a bad shape throws before any request
|
|
41
|
+
const compile = (shape, scope) => {
|
|
42
|
+
const node = nodes.get(shape);
|
|
43
|
+
if (node) {
|
|
44
|
+
live(node, scope);
|
|
45
|
+
return (read) => walk(read(node.root), node.path);
|
|
46
|
+
}
|
|
47
|
+
const mapped = maps.get(shape);
|
|
48
|
+
if (mapped)
|
|
49
|
+
return mapped;
|
|
50
|
+
if (!isRecord(shape))
|
|
51
|
+
throw new Error('compose returns handles, maps and records of them');
|
|
52
|
+
const fields = Object.entries(shape).map(([key, v]) => [key, compile(v, scope)]);
|
|
53
|
+
return (read) => Object.fromEntries(fields.map(([key, f]) => [key, f(read)]));
|
|
54
|
+
};
|
|
55
|
+
const map = (source, fn) => {
|
|
56
|
+
live(source);
|
|
57
|
+
if (mapping)
|
|
58
|
+
throw new Error('map cannot nest');
|
|
59
|
+
const first = steps.length;
|
|
60
|
+
const element = {};
|
|
61
|
+
mapping = element;
|
|
62
|
+
let returned;
|
|
63
|
+
try {
|
|
64
|
+
returned = fn(handle({ root: '@', path: [], scope: element }));
|
|
65
|
+
}
|
|
66
|
+
finally {
|
|
67
|
+
mapping = undefined;
|
|
68
|
+
}
|
|
69
|
+
const made = steps.slice(first);
|
|
70
|
+
for (const s of made)
|
|
71
|
+
s.each = text(source);
|
|
72
|
+
const inner = new Set(made.map((s) => s.id));
|
|
73
|
+
const own = nodes.get(returned);
|
|
74
|
+
// typed as a step to chain from, but no each step runs it
|
|
75
|
+
if (own?.path.length === 0 && own.root !== '@') {
|
|
76
|
+
if (!inner.has(own.root))
|
|
77
|
+
throw new Error('map returns a step made outside its callback');
|
|
78
|
+
return handle({ root: own.root, path: [], scope: undefined });
|
|
79
|
+
}
|
|
80
|
+
const body = compile(returned, element);
|
|
81
|
+
const token = {};
|
|
82
|
+
maps.set(token, (read) => {
|
|
83
|
+
const items = walk(read(source.root), source.path);
|
|
84
|
+
if (!Array.isArray(items))
|
|
85
|
+
return [];
|
|
86
|
+
return items.map((el, i) => body((root) => (root === '@' ? el : inner.has(root) ? walk(read(root), [i]) : read(root))));
|
|
87
|
+
});
|
|
88
|
+
return token;
|
|
89
|
+
};
|
|
90
|
+
const call = (op) => (body) => {
|
|
91
|
+
// the server runs an each step as a batch, so both refuse geometry with one message
|
|
92
|
+
if ((mapping || Array.isArray(body)) && [body].flat().some((item) => item.geometry))
|
|
93
|
+
throw new Error('set geometry=false for every batch item');
|
|
94
|
+
const expand = (item) => {
|
|
95
|
+
const node = nodes.get(item.input);
|
|
96
|
+
const [parent, name] = node?.path.slice(-2) ?? [];
|
|
97
|
+
return parent === 'refs' && typeof name === 'string'
|
|
98
|
+
? { ...item, input: { kind: name, id: item.input } }
|
|
99
|
+
: item;
|
|
100
|
+
};
|
|
101
|
+
const wired = op !== 'project' ? body : Array.isArray(body) ? body.map(expand) : expand(body);
|
|
102
|
+
const id = `s${steps.length}`;
|
|
103
|
+
// serialize now: a misused handle throws here, and later mutation cannot leak in
|
|
104
|
+
steps.push({ id, op, body: JSON.parse(JSON.stringify(wired)) });
|
|
105
|
+
return handle({ root: id, path: [], scope: mapping });
|
|
106
|
+
};
|
|
107
|
+
// the proxies are untyped; Recorder carries the types
|
|
108
|
+
const decode = compile(fn({ resolve: call('resolve'), search: call('search'), project: call('project') }), undefined);
|
|
109
|
+
return { steps, decode: (outputs) => decode((root) => outputs[root]) };
|
|
110
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { Bifrost, BifrostError } from './client.js';
|
|
2
|
+
export type { BifrostOptions, ErrorDetail, Token, ValidationIssue } from './client.js';
|
|
3
|
+
export type { CodeSlot, Decoded, Handle, Mapped, ProjectBody, Recorder, RefHandle, Shape, Slot, Step } from './compose.js';
|
|
4
|
+
export type * from './types.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Bifrost, BifrostError } from './client.js';
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
export type ProjectTarget = 'unit' | 'entrance' | 'street' | 'postcode' | 'city' | 'ejendom' | 'kommune' | 'sogn' | 'region' | 'retskreds' | 'politikreds' | 'opstillingskreds' | 'ejerlav';
|
|
2
|
+
export type ProjectRoot = ProjectTarget;
|
|
3
|
+
export type SearchTarget = Exclude<ProjectTarget, 'unit' | 'entrance'> | 'stednavn';
|
|
4
|
+
export type FeatureKind = ProjectTarget | 'stednavn';
|
|
5
|
+
export type Lifecycle = 'current' | 'preliminary' | 'retired' | 'abandoned';
|
|
6
|
+
export type Confidence = 'A' | 'B' | 'C';
|
|
7
|
+
export type EjendomType = 'samlet_fast_ejendom' | 'ejerlejlighed' | 'bygning_paa_fremmed_grund';
|
|
8
|
+
export type ComponentField = 'street' | 'house_number' | 'house_letter' | 'floor' | 'door' | 'postcode' | 'city' | 'sub_locality';
|
|
9
|
+
export type PinField = ComponentField | 'kommune' | 'sogn' | 'region';
|
|
10
|
+
export type Pins<S = string> = {
|
|
11
|
+
[K in PinField]?: S;
|
|
12
|
+
};
|
|
13
|
+
export interface ResolveRequest<S = string> {
|
|
14
|
+
input?: S;
|
|
15
|
+
components?: Pins<S>;
|
|
16
|
+
lifecycle?: Lifecycle[];
|
|
17
|
+
limit?: number;
|
|
18
|
+
geometry?: boolean;
|
|
19
|
+
}
|
|
20
|
+
export interface SearchRequest<S = string, T extends SearchTarget = SearchTarget> {
|
|
21
|
+
input: S;
|
|
22
|
+
target: T;
|
|
23
|
+
limit?: number;
|
|
24
|
+
lifecycle?: Lifecycle[];
|
|
25
|
+
geometry?: boolean;
|
|
26
|
+
}
|
|
27
|
+
export interface Ref<S = string> {
|
|
28
|
+
kind: ProjectRoot;
|
|
29
|
+
id: S;
|
|
30
|
+
}
|
|
31
|
+
export interface ProjectRequest<S = string> {
|
|
32
|
+
input: Ref<S>;
|
|
33
|
+
target: ProjectTarget;
|
|
34
|
+
limit?: number;
|
|
35
|
+
cursor?: string;
|
|
36
|
+
lifecycle?: Lifecycle[];
|
|
37
|
+
geometry?: boolean;
|
|
38
|
+
}
|
|
39
|
+
export type Position = [number, number];
|
|
40
|
+
export interface Point {
|
|
41
|
+
type: 'Point';
|
|
42
|
+
coordinates: Position;
|
|
43
|
+
}
|
|
44
|
+
export type GeoJsonGeometry = Point | {
|
|
45
|
+
type: 'MultiPoint';
|
|
46
|
+
coordinates: Position[];
|
|
47
|
+
} | {
|
|
48
|
+
type: 'LineString';
|
|
49
|
+
coordinates: Position[];
|
|
50
|
+
} | {
|
|
51
|
+
type: 'MultiLineString';
|
|
52
|
+
coordinates: Position[][];
|
|
53
|
+
} | {
|
|
54
|
+
type: 'Polygon';
|
|
55
|
+
coordinates: Position[][];
|
|
56
|
+
} | {
|
|
57
|
+
type: 'MultiPolygon';
|
|
58
|
+
coordinates: Position[][][];
|
|
59
|
+
} | {
|
|
60
|
+
type: 'GeometryCollection';
|
|
61
|
+
geometries: GeoJsonGeometry[];
|
|
62
|
+
};
|
|
63
|
+
export interface Geometry {
|
|
64
|
+
srid: number;
|
|
65
|
+
geojson: GeoJsonGeometry;
|
|
66
|
+
vejpunkt: Position | null;
|
|
67
|
+
}
|
|
68
|
+
export interface Upstream {
|
|
69
|
+
source: string;
|
|
70
|
+
id: string | number;
|
|
71
|
+
}
|
|
72
|
+
export interface Meta {
|
|
73
|
+
score: number;
|
|
74
|
+
confidence: Confidence;
|
|
75
|
+
upstream: Partial<Record<FeatureKind, Upstream>>;
|
|
76
|
+
}
|
|
77
|
+
export interface PropertyRef {
|
|
78
|
+
id: string;
|
|
79
|
+
type: EjendomType;
|
|
80
|
+
ejerlejlighedsnummer?: string;
|
|
81
|
+
lifecycle: Lifecycle | null;
|
|
82
|
+
upstream: Upstream;
|
|
83
|
+
}
|
|
84
|
+
export interface PropertyRefs {
|
|
85
|
+
refs: PropertyRef[];
|
|
86
|
+
complete: boolean;
|
|
87
|
+
}
|
|
88
|
+
export interface Parcel {
|
|
89
|
+
matrikelnummer: string | null;
|
|
90
|
+
ejerlavskode: number | null;
|
|
91
|
+
ejerlav: string | null;
|
|
92
|
+
kommunekode: string | null;
|
|
93
|
+
kommune: string | null;
|
|
94
|
+
lifecycle: Lifecycle;
|
|
95
|
+
upstream: Upstream;
|
|
96
|
+
}
|
|
97
|
+
export interface Ejendom {
|
|
98
|
+
type: EjendomType;
|
|
99
|
+
ejerlejlighedsnummer?: string;
|
|
100
|
+
relations: {
|
|
101
|
+
parents: PropertyRefs;
|
|
102
|
+
children: PropertyRefs;
|
|
103
|
+
};
|
|
104
|
+
parcels: {
|
|
105
|
+
items: Parcel[];
|
|
106
|
+
complete: boolean;
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
type Area<Code extends string, Name extends string> = Record<Code, string> & Record<Name, string | null>;
|
|
110
|
+
export interface Components {
|
|
111
|
+
street?: string;
|
|
112
|
+
house_number?: string;
|
|
113
|
+
house_letter?: string;
|
|
114
|
+
floor?: string;
|
|
115
|
+
door?: string;
|
|
116
|
+
sub_locality?: string;
|
|
117
|
+
stednavn?: string;
|
|
118
|
+
type?: string;
|
|
119
|
+
matrikelnummer?: string;
|
|
120
|
+
ejerlavskode?: number;
|
|
121
|
+
ejerlav?: string;
|
|
122
|
+
postcodes?: Area<'postcode', 'city'>[];
|
|
123
|
+
kommuner?: Area<'kommunekode', 'kommune'>[];
|
|
124
|
+
sogne?: Area<'sognekode', 'sogn'>[];
|
|
125
|
+
regioner?: Area<'regionskode', 'region'>[];
|
|
126
|
+
retskredse?: Area<'retskredsnummer', 'retskreds'>[];
|
|
127
|
+
politikredse?: Area<'politikredsnummer', 'politikreds'>[];
|
|
128
|
+
opstillingskredse?: Area<'opstillingskredsnummer', 'opstillingskreds'>[];
|
|
129
|
+
}
|
|
130
|
+
export interface StednavnRef {
|
|
131
|
+
kind: 'stednavn';
|
|
132
|
+
id: string;
|
|
133
|
+
}
|
|
134
|
+
export interface Match<R extends Ref | StednavnRef = Ref> {
|
|
135
|
+
ref: R;
|
|
136
|
+
refs: Partial<Record<FeatureKind, string>>;
|
|
137
|
+
result: string;
|
|
138
|
+
lifecycle: Lifecycle | null;
|
|
139
|
+
components: Components;
|
|
140
|
+
ejendom?: Ejendom;
|
|
141
|
+
point: Point | null;
|
|
142
|
+
geometry: Geometry | null;
|
|
143
|
+
meta: Meta;
|
|
144
|
+
}
|
|
145
|
+
export interface MatchResult<M extends Match<Ref | StednavnRef> = Match> {
|
|
146
|
+
input: string | null;
|
|
147
|
+
matches: M[];
|
|
148
|
+
error?: string;
|
|
149
|
+
}
|
|
150
|
+
export type SearchMatch<T extends SearchTarget> = T extends 'stednavn' ? Match<StednavnRef> : Match;
|
|
151
|
+
export interface ProjectResult<I extends Ref | null = Ref> {
|
|
152
|
+
input: I;
|
|
153
|
+
matches: Match[];
|
|
154
|
+
next?: string;
|
|
155
|
+
error?: string;
|
|
156
|
+
}
|
|
157
|
+
export {};
|
package/dist/types.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,30 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@kalyptos/bifrost",
|
|
3
|
+
"version": "5.0.0",
|
|
4
|
+
"description": "TypeScript client for the Bifrost Danish address API",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/kalyptos-io/bifrost.git",
|
|
9
|
+
"directory": "sdk"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"exports": "./dist/index.js",
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"publishConfig": {
|
|
18
|
+
"access": "public"
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"check": "tsc --noEmit",
|
|
22
|
+
"test": "vitest run",
|
|
23
|
+
"build": "tsc -p tsconfig.build.json",
|
|
24
|
+
"prepack": "npm run build"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"typescript": "^6.0.3",
|
|
28
|
+
"vitest": "^4.1.11"
|
|
29
|
+
}
|
|
30
|
+
}
|