@allisall/sdk 0.0.0-stage → 0.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/LICENSE +202 -0
- package/NOTICE +2 -0
- package/README.md +89 -2
- package/dist/client.d.ts +77 -0
- package/dist/client.js +130 -0
- package/dist/decimal.d.ts +20 -0
- package/dist/decimal.js +23 -0
- package/dist/errors.d.ts +79 -0
- package/dist/errors.js +98 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +4 -0
- package/dist/ingest.d.ts +28 -0
- package/dist/ingest.js +247 -0
- package/dist/retry.d.ts +18 -0
- package/dist/retry.js +105 -0
- package/dist/runtime.d.ts +17 -0
- package/dist/runtime.js +60 -0
- package/dist/transport.d.ts +53 -0
- package/dist/transport.js +220 -0
- package/dist/types.d.ts +135 -0
- package/dist/types.js +80 -0
- package/dist/uuid.d.ts +6 -0
- package/dist/uuid.js +20 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +2 -0
- package/package.json +39 -4
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 [yyyy] [name of copyright owner]
|
|
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/NOTICE
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,90 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @allisall/sdk
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The allisall SDK for TypeScript and JavaScript. Version 0.1 sends events to
|
|
4
|
+
the allisall API. It runs on Node 22 and later, and in current browsers.
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
npm install @allisall/sdk
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Create a client
|
|
13
|
+
|
|
14
|
+
The client reads your API key from `AIA_API_KEY`:
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
export AIA_API_KEY=aia_...
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { Allisall } from "@allisall/sdk";
|
|
22
|
+
|
|
23
|
+
const client = new Allisall();
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
You can also pass the key yourself, as `new Allisall({ apiKey })`. Create
|
|
27
|
+
one client and reuse it.
|
|
28
|
+
|
|
29
|
+
## Ingest events
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import { Allisall, decimal } from "@allisall/sdk";
|
|
33
|
+
|
|
34
|
+
const client = new Allisall();
|
|
35
|
+
|
|
36
|
+
const summary = await client.events.ingest([
|
|
37
|
+
{
|
|
38
|
+
timestamp: new Date(),
|
|
39
|
+
agent_key: "support-bot",
|
|
40
|
+
category: "model",
|
|
41
|
+
event_type: "completion",
|
|
42
|
+
model: "gpt-4o",
|
|
43
|
+
tokens_in: 1200,
|
|
44
|
+
tokens_out: 300,
|
|
45
|
+
cost: decimal("0.0042"),
|
|
46
|
+
},
|
|
47
|
+
]);
|
|
48
|
+
|
|
49
|
+
console.log(summary.accepted, summary.duplicates, summary.rejected);
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
`ingest` takes any number of events. It sends them in batches, retries a
|
|
53
|
+
failure worth retrying, and returns one summary for the whole list. A
|
|
54
|
+
rejected event is listed in `summary.errors`, and the others are still
|
|
55
|
+
stored. Write `cost` with `decimal()` to keep it exact.
|
|
56
|
+
|
|
57
|
+
### Set your own `event_id` for at-least-once delivery
|
|
58
|
+
|
|
59
|
+
The SDK gives every event without an `event_id` a new one. That makes a
|
|
60
|
+
retry within one call safe, but it does not catch the same event sent
|
|
61
|
+
again in a later call. If your events can be delivered more than once,
|
|
62
|
+
for example from a queue that redelivers messages, set `event_id`
|
|
63
|
+
yourself, such as to the message's id. The API then stores the event
|
|
64
|
+
once, however often it arrives.
|
|
65
|
+
|
|
66
|
+
## Browsers
|
|
67
|
+
|
|
68
|
+
An API key in a browser can be read by anyone who loads the page. The
|
|
69
|
+
client therefore refuses an API key in a browser unless you pass
|
|
70
|
+
`dangerouslyAllowBrowser: true`:
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
import { Allisall } from "@allisall/sdk";
|
|
74
|
+
|
|
75
|
+
// Only if you accept that anyone who loads the page can read this key.
|
|
76
|
+
const client = new Allisall({
|
|
77
|
+
apiKey: "aia_...",
|
|
78
|
+
dangerouslyAllowBrowser: true,
|
|
79
|
+
});
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Prefer calling the API from your server.
|
|
83
|
+
|
|
84
|
+
## Documentation
|
|
85
|
+
|
|
86
|
+
See [https://docs.allisai.ai](https://docs.allisai.ai).
|
|
87
|
+
|
|
88
|
+
## License
|
|
89
|
+
|
|
90
|
+
Apache-2.0. See `LICENSE` and `NOTICE`.
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { type FetchLike, type TokenProvider, type TransportConfig } from "./transport.js";
|
|
2
|
+
import type { EventInput, IngestSummary } from "./types.js";
|
|
3
|
+
/** The options of `new Allisall()`. Every one is optional. */
|
|
4
|
+
export interface ClientOptions {
|
|
5
|
+
/**
|
|
6
|
+
* The API key. Defaults to `AIA_API_KEY` from the environment. Pass
|
|
7
|
+
* either this or `tokenProvider`, not both.
|
|
8
|
+
*/
|
|
9
|
+
apiKey?: string | undefined;
|
|
10
|
+
/**
|
|
11
|
+
* A callback returning the bearer token to send, called before every
|
|
12
|
+
* attempt, retries included. Use it to supply an API key fetched at run
|
|
13
|
+
* time, for example from a secret store.
|
|
14
|
+
*/
|
|
15
|
+
tokenProvider?: TokenProvider | undefined;
|
|
16
|
+
/**
|
|
17
|
+
* The API's base URL, absolute, `http` or `https`. Defaults to
|
|
18
|
+
* `AIA_BASE_URL` from the environment, then `https://api.allisai.ai`.
|
|
19
|
+
*/
|
|
20
|
+
baseURL?: string | undefined;
|
|
21
|
+
/** The timeout of each attempt, in milliseconds. Defaults to 60,000. */
|
|
22
|
+
timeout?: number | undefined;
|
|
23
|
+
/** How many times a failure worth repeating is retried. Defaults to 2; 0 turns retries off. */
|
|
24
|
+
maxRetries?: number | undefined;
|
|
25
|
+
/** The `fetch` to send requests with. Defaults to the platform's own. */
|
|
26
|
+
fetch?: FetchLike | undefined;
|
|
27
|
+
/**
|
|
28
|
+
* Allows an API key in a browser, where anyone who loads the page can
|
|
29
|
+
* read it. Defaults to `false`.
|
|
30
|
+
*/
|
|
31
|
+
dangerouslyAllowBrowser?: boolean | undefined;
|
|
32
|
+
}
|
|
33
|
+
/** The options of one call. */
|
|
34
|
+
export interface RequestOptions {
|
|
35
|
+
/** Cancels the call, including any wait between retries. */
|
|
36
|
+
signal?: AbortSignal | undefined;
|
|
37
|
+
}
|
|
38
|
+
/** The `events` operations. */
|
|
39
|
+
declare class Events {
|
|
40
|
+
#private;
|
|
41
|
+
constructor(config: TransportConfig);
|
|
42
|
+
/**
|
|
43
|
+
* Ingests any number of events. They are sent in batches of at most 500
|
|
44
|
+
* events and 5,000,000 bytes, one batch after another, each retried as
|
|
45
|
+
* the client allows. Every event without an `event_id` gets a generated
|
|
46
|
+
* one, so a retry never stores an event twice. Your objects are not
|
|
47
|
+
* modified.
|
|
48
|
+
*
|
|
49
|
+
* An event too large to send even alone is not sent, and is reported as a
|
|
50
|
+
* rejected row with the code `too_large`.
|
|
51
|
+
*
|
|
52
|
+
* @returns the combined outcome over the whole list.
|
|
53
|
+
* @throws {IngestError} when a batch fails; the call stops there, and the
|
|
54
|
+
* error carries the summary so far and every event from the failed batch
|
|
55
|
+
* onward, to send again. A batch fails on an error response, on no
|
|
56
|
+
* response, on the caller's cancellation, or when its token cannot be
|
|
57
|
+
* had: an error from `tokenProvider`, or, in a browser without
|
|
58
|
+
* `dangerouslyAllowBrowser`, a provider's token that is an API key. The
|
|
59
|
+
* error is the `IngestError`'s `cause`.
|
|
60
|
+
* @throws {TypeError} when an event cannot be encoded, before anything is sent.
|
|
61
|
+
*/
|
|
62
|
+
ingest(events: readonly EventInput[], options?: RequestOptions): Promise<IngestSummary>;
|
|
63
|
+
}
|
|
64
|
+
/** The allisall API client. Construct one and reuse it. */
|
|
65
|
+
export declare class Allisall {
|
|
66
|
+
/** The `events` operations. */
|
|
67
|
+
readonly events: Events;
|
|
68
|
+
/**
|
|
69
|
+
* @throws {AllisallError} when there is no credential, or both an API key
|
|
70
|
+
* and a token provider; when the base URL is not an absolute `http` or
|
|
71
|
+
* `https` URL; when `maxRetries` is negative or `timeout` is not
|
|
72
|
+
* positive; or when an API key is used in a browser without
|
|
73
|
+
* `dangerouslyAllowBrowser`.
|
|
74
|
+
*/
|
|
75
|
+
constructor(options?: ClientOptions);
|
|
76
|
+
}
|
|
77
|
+
export {};
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
// The client: its options, and how the credential and the base URL are
|
|
2
|
+
// resolved at construction.
|
|
3
|
+
import { AllisallError } from "./errors.js";
|
|
4
|
+
import { ingest, readIngestResult } from "./ingest.js";
|
|
5
|
+
import { isBrowser, readEnv, userAgent } from "./runtime.js";
|
|
6
|
+
import { BROWSER_KEY_MESSAGE, isPrintableCredential, post, } from "./transport.js";
|
|
7
|
+
const DEFAULT_BASE_URL = "https://api.allisai.ai";
|
|
8
|
+
const DEFAULT_TIMEOUT_MS = 60_000;
|
|
9
|
+
const DEFAULT_MAX_RETRIES = 2;
|
|
10
|
+
function resolveCredential(options) {
|
|
11
|
+
const { apiKey, tokenProvider } = options;
|
|
12
|
+
if (apiKey !== undefined && tokenProvider !== undefined) {
|
|
13
|
+
throw new AllisallError("allisall: pass either apiKey or tokenProvider, not both");
|
|
14
|
+
}
|
|
15
|
+
if (tokenProvider !== undefined) {
|
|
16
|
+
if (typeof tokenProvider !== "function") {
|
|
17
|
+
throw new AllisallError("allisall: tokenProvider must be a function");
|
|
18
|
+
}
|
|
19
|
+
return { kind: "provider", provider: tokenProvider };
|
|
20
|
+
}
|
|
21
|
+
if (apiKey !== undefined) {
|
|
22
|
+
const key = typeof apiKey === "string" ? apiKey.trim() : "";
|
|
23
|
+
if (key === "") {
|
|
24
|
+
throw new AllisallError("allisall: apiKey is empty");
|
|
25
|
+
}
|
|
26
|
+
if (!isPrintableCredential(key)) {
|
|
27
|
+
throw new AllisallError("allisall: apiKey holds a character other than printable ASCII, which cannot be sent");
|
|
28
|
+
}
|
|
29
|
+
return { kind: "apiKey", apiKey: key };
|
|
30
|
+
}
|
|
31
|
+
// A variable of white space alone counts as unset.
|
|
32
|
+
const fromEnv = readEnv("AIA_API_KEY")?.trim();
|
|
33
|
+
if (fromEnv !== undefined && fromEnv !== "") {
|
|
34
|
+
if (!isPrintableCredential(fromEnv)) {
|
|
35
|
+
throw new AllisallError("allisall: AIA_API_KEY holds a character other than printable ASCII, which cannot be sent");
|
|
36
|
+
}
|
|
37
|
+
return { kind: "apiKey", apiKey: fromEnv };
|
|
38
|
+
}
|
|
39
|
+
throw new AllisallError("allisall: no credential; pass apiKey or tokenProvider, or set AIA_API_KEY");
|
|
40
|
+
}
|
|
41
|
+
function resolveBaseURL(option) {
|
|
42
|
+
const raw = option ?? readEnv("AIA_BASE_URL") ?? DEFAULT_BASE_URL;
|
|
43
|
+
let url;
|
|
44
|
+
try {
|
|
45
|
+
url = new URL(raw);
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
throw new AllisallError(`allisall: the base URL ${JSON.stringify(raw)} is not an absolute URL`);
|
|
49
|
+
}
|
|
50
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") {
|
|
51
|
+
throw new AllisallError(`allisall: the base URL ${JSON.stringify(raw)} is not http or https`);
|
|
52
|
+
}
|
|
53
|
+
return url;
|
|
54
|
+
}
|
|
55
|
+
function resolveTimeout(option) {
|
|
56
|
+
const timeout = option ?? DEFAULT_TIMEOUT_MS;
|
|
57
|
+
if (typeof timeout !== "number" ||
|
|
58
|
+
!Number.isFinite(timeout) ||
|
|
59
|
+
timeout <= 0) {
|
|
60
|
+
throw new AllisallError(`allisall: timeout must be a positive number of milliseconds, not ${String(timeout)}`);
|
|
61
|
+
}
|
|
62
|
+
return timeout;
|
|
63
|
+
}
|
|
64
|
+
function resolveMaxRetries(option) {
|
|
65
|
+
const maxRetries = option ?? DEFAULT_MAX_RETRIES;
|
|
66
|
+
if (!Number.isInteger(maxRetries) || maxRetries < 0) {
|
|
67
|
+
throw new AllisallError(`allisall: maxRetries must be a whole number, 0 or more, not ${String(maxRetries)}`);
|
|
68
|
+
}
|
|
69
|
+
return maxRetries;
|
|
70
|
+
}
|
|
71
|
+
/** The `events` operations. */
|
|
72
|
+
class Events {
|
|
73
|
+
#config;
|
|
74
|
+
constructor(config) {
|
|
75
|
+
this.#config = config;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Ingests any number of events. They are sent in batches of at most 500
|
|
79
|
+
* events and 5,000,000 bytes, one batch after another, each retried as
|
|
80
|
+
* the client allows. Every event without an `event_id` gets a generated
|
|
81
|
+
* one, so a retry never stores an event twice. Your objects are not
|
|
82
|
+
* modified.
|
|
83
|
+
*
|
|
84
|
+
* An event too large to send even alone is not sent, and is reported as a
|
|
85
|
+
* rejected row with the code `too_large`.
|
|
86
|
+
*
|
|
87
|
+
* @returns the combined outcome over the whole list.
|
|
88
|
+
* @throws {IngestError} when a batch fails; the call stops there, and the
|
|
89
|
+
* error carries the summary so far and every event from the failed batch
|
|
90
|
+
* onward, to send again. A batch fails on an error response, on no
|
|
91
|
+
* response, on the caller's cancellation, or when its token cannot be
|
|
92
|
+
* had: an error from `tokenProvider`, or, in a browser without
|
|
93
|
+
* `dangerouslyAllowBrowser`, a provider's token that is an API key. The
|
|
94
|
+
* error is the `IngestError`'s `cause`.
|
|
95
|
+
* @throws {TypeError} when an event cannot be encoded, before anything is sent.
|
|
96
|
+
*/
|
|
97
|
+
ingest(events, options = {}) {
|
|
98
|
+
const config = this.#config;
|
|
99
|
+
return ingest(events, (body, count, signal) => post(config, "/v1/events", body, (value) => readIngestResult(value, count), signal), options.signal);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/** The allisall API client. Construct one and reuse it. */
|
|
103
|
+
export class Allisall {
|
|
104
|
+
/** The `events` operations. */
|
|
105
|
+
events;
|
|
106
|
+
/**
|
|
107
|
+
* @throws {AllisallError} when there is no credential, or both an API key
|
|
108
|
+
* and a token provider; when the base URL is not an absolute `http` or
|
|
109
|
+
* `https` URL; when `maxRetries` is negative or `timeout` is not
|
|
110
|
+
* positive; or when an API key is used in a browser without
|
|
111
|
+
* `dangerouslyAllowBrowser`.
|
|
112
|
+
*/
|
|
113
|
+
constructor(options = {}) {
|
|
114
|
+
const credential = resolveCredential(options);
|
|
115
|
+
const browser = isBrowser();
|
|
116
|
+
const refuseApiKeys = browser && options.dangerouslyAllowBrowser !== true;
|
|
117
|
+
if (refuseApiKeys && credential.kind === "apiKey") {
|
|
118
|
+
throw new AllisallError(BROWSER_KEY_MESSAGE);
|
|
119
|
+
}
|
|
120
|
+
this.events = new Events({
|
|
121
|
+
baseURL: resolveBaseURL(options.baseURL),
|
|
122
|
+
credential,
|
|
123
|
+
timeout: resolveTimeout(options.timeout),
|
|
124
|
+
maxRetries: resolveMaxRetries(options.maxRetries),
|
|
125
|
+
fetch: options.fetch,
|
|
126
|
+
refuseApiKeys,
|
|
127
|
+
userAgent: userAgent(),
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
declare const decimalBrand: unique symbol;
|
|
2
|
+
/**
|
|
3
|
+
* An exact decimal amount, such as a `cost`. It is a string at run time,
|
|
4
|
+
* branded so that only `decimal()` produces one. The SDK writes its text
|
|
5
|
+
* into the request unchanged, as a JSON number, so no binary float ever
|
|
6
|
+
* rounds it.
|
|
7
|
+
*/
|
|
8
|
+
export type Decimal = string & {
|
|
9
|
+
readonly [decimalBrand]: true;
|
|
10
|
+
};
|
|
11
|
+
/** Reports whether `text` has the syntax of a `Decimal`. */
|
|
12
|
+
export declare function isDecimalText(text: string): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Makes a `Decimal` from its text, such as `decimal("0.3683")`.
|
|
15
|
+
*
|
|
16
|
+
* @throws {TypeError} when `text` is not an optional `-`, digits without a
|
|
17
|
+
* leading zero (or a single `0`), and optional decimal places after a `.`.
|
|
18
|
+
*/
|
|
19
|
+
export declare function decimal(text: string): Decimal;
|
|
20
|
+
export {};
|
package/dist/decimal.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The syntax a `Decimal` accepts: an optional `-`, digits without a leading
|
|
3
|
+
* zero (or a single `0`), and optional decimal places after a `.`. Every
|
|
4
|
+
* string it accepts is a valid JSON number.
|
|
5
|
+
*/
|
|
6
|
+
const DECIMAL_SYNTAX = /^-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?$/;
|
|
7
|
+
/** Reports whether `text` has the syntax of a `Decimal`. */
|
|
8
|
+
export function isDecimalText(text) {
|
|
9
|
+
return DECIMAL_SYNTAX.test(text);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Makes a `Decimal` from its text, such as `decimal("0.3683")`.
|
|
13
|
+
*
|
|
14
|
+
* @throws {TypeError} when `text` is not an optional `-`, digits without a
|
|
15
|
+
* leading zero (or a single `0`), and optional decimal places after a `.`.
|
|
16
|
+
*/
|
|
17
|
+
export function decimal(text) {
|
|
18
|
+
if (typeof text !== "string" || !isDecimalText(text)) {
|
|
19
|
+
throw new TypeError(`allisall: ${JSON.stringify(text)} is not a decimal; write it as digits with an optional "-" and decimal places, such as "12.5"`);
|
|
20
|
+
}
|
|
21
|
+
// The syntax check above is the invariant the brand stands for.
|
|
22
|
+
return text;
|
|
23
|
+
}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { ErrorCode, EventInput, IngestSummary, Problem } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* The base of every error the SDK throws. Catch it to handle any SDK
|
|
4
|
+
* failure; catch `ApiError`, `ConnectionError` or `IngestError` to handle
|
|
5
|
+
* one condition.
|
|
6
|
+
*/
|
|
7
|
+
export declare class AllisallError extends Error {
|
|
8
|
+
name: string;
|
|
9
|
+
}
|
|
10
|
+
/** The members of an `ApiError`. */
|
|
11
|
+
export interface ApiErrorInit {
|
|
12
|
+
status: number;
|
|
13
|
+
problem?: Problem | undefined;
|
|
14
|
+
requestId?: string | undefined;
|
|
15
|
+
retryAfter?: number | undefined;
|
|
16
|
+
headers: Headers;
|
|
17
|
+
message?: string | undefined;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* An error response from the API, or a successful response whose body was
|
|
21
|
+
* not the expected shape. Branch on `code`; for a code you do not
|
|
22
|
+
* recognise, fall back on `status`.
|
|
23
|
+
*/
|
|
24
|
+
export declare class ApiError extends AllisallError {
|
|
25
|
+
name: string;
|
|
26
|
+
/** The HTTP status. */
|
|
27
|
+
readonly status: number;
|
|
28
|
+
/** The problem's `code`. Absent when the body was not a problem document. */
|
|
29
|
+
readonly code: ErrorCode | undefined;
|
|
30
|
+
/** The whole problem document. Absent when the body was not one. */
|
|
31
|
+
readonly problem: Problem | undefined;
|
|
32
|
+
/** The problem's `request_id`, or else the response's `X-Request-Id`. */
|
|
33
|
+
readonly requestId: string | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* The response's `Retry-After`, in milliseconds from when the response
|
|
36
|
+
* arrived. Absent when the response had none, or one that did not parse
|
|
37
|
+
* or named a time in the past.
|
|
38
|
+
*/
|
|
39
|
+
readonly retryAfter: number | undefined;
|
|
40
|
+
/** The response headers. */
|
|
41
|
+
readonly headers: Headers;
|
|
42
|
+
constructor(init: ApiErrorInit);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* A request that got no response: the connection failed, was reset, or the
|
|
46
|
+
* attempt timed out. `cause` holds the underlying error.
|
|
47
|
+
*/
|
|
48
|
+
export declare class ConnectionError extends AllisallError {
|
|
49
|
+
name: string;
|
|
50
|
+
/** The `X-Request-Id` the SDK sent on the last attempt, to quote when reporting it. */
|
|
51
|
+
readonly requestId: string;
|
|
52
|
+
constructor(requestId: string, cause: unknown);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* A batch of an `ingest` call failed, so the call stopped and sent nothing
|
|
56
|
+
* after it. `cause` holds what failed the batch: an `ApiError`, a
|
|
57
|
+
* `ConnectionError`, the caller's cancellation, or the error from getting
|
|
58
|
+
* the token: the token provider's own error, unchanged, or the
|
|
59
|
+
* `AllisallError` refusing a provider's API key in a browser. To recover,
|
|
60
|
+
* send `remaining` again: every event in it keeps the `event_id` it was
|
|
61
|
+
* sent with, so any the failed batch stored come back as duplicates.
|
|
62
|
+
*/
|
|
63
|
+
export declare class IngestError extends AllisallError {
|
|
64
|
+
name: string;
|
|
65
|
+
/** The combined outcome of the batches that succeeded before the failed one. */
|
|
66
|
+
readonly summary: IngestSummary;
|
|
67
|
+
/**
|
|
68
|
+
* Copies of every event from the failed batch onward, each with its
|
|
69
|
+
* `event_id`, less any event too large to send: those are rows of
|
|
70
|
+
* `summary.errors` and never in `remaining`. To map `remaining` back to
|
|
71
|
+
* your list, walk it from `remainingFrom` and skip the `index` of every
|
|
72
|
+
* `summary.errors` row at or past `remainingFrom`. With no such row,
|
|
73
|
+
* `remaining[i]` is your event `remainingFrom + i`.
|
|
74
|
+
*/
|
|
75
|
+
readonly remaining: EventInput[];
|
|
76
|
+
/** The index, in the list passed to `ingest`, of the first event in `remaining`. */
|
|
77
|
+
readonly remainingFrom: number;
|
|
78
|
+
constructor(cause: unknown, summary: IngestSummary, remaining: EventInput[], remainingFrom: number);
|
|
79
|
+
}
|