@allisall/sdk 0.1.0 → 0.4.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 +145 -4
- package/dist/client.d.ts +87 -37
- package/dist/client.js +144 -73
- package/dist/generated/openapi.d.ts +9497 -0
- package/dist/generated/openapi.js +4 -0
- package/dist/generated/operations.d.ts +42 -0
- package/dist/generated/operations.js +1167 -0
- package/dist/import-mapping.d.ts +10 -0
- package/dist/import-mapping.js +20 -0
- package/dist/index.d.ts +8 -2
- package/dist/index.js +2 -0
- package/dist/ingest.d.ts +1 -1
- package/dist/ingest.js +2 -2
- package/dist/money.d.ts +57 -0
- package/dist/money.js +284 -0
- package/dist/multipart.d.ts +29 -0
- package/dist/multipart.js +48 -0
- package/dist/options.d.ts +50 -0
- package/dist/options.js +73 -0
- package/dist/pagination.d.ts +55 -0
- package/dist/pagination.js +85 -0
- package/dist/request.d.ts +126 -0
- package/dist/request.js +403 -0
- package/dist/resources/agents.d.ts +58 -0
- package/dist/resources/agents.js +72 -0
- package/dist/resources/analytics.d.ts +42 -0
- package/dist/resources/analytics.js +68 -0
- package/dist/resources/api-keys.d.ts +54 -0
- package/dist/resources/api-keys.js +63 -0
- package/dist/resources/calculations.d.ts +98 -0
- package/dist/resources/calculations.js +133 -0
- package/dist/resources/clients.d.ts +93 -0
- package/dist/resources/clients.js +113 -0
- package/dist/resources/cost.d.ts +76 -0
- package/dist/resources/cost.js +96 -0
- package/dist/resources/dashboard.d.ts +32 -0
- package/dist/resources/dashboard.js +36 -0
- package/dist/resources/events.d.ts +116 -0
- package/dist/resources/events.js +145 -0
- package/dist/resources/health.d.ts +14 -0
- package/dist/resources/health.js +17 -0
- package/dist/resources/imports.d.ts +56 -0
- package/dist/resources/imports.js +63 -0
- package/dist/resources/onboarding.d.ts +29 -0
- package/dist/resources/onboarding.js +34 -0
- package/dist/resources/outcome-types.d.ts +25 -0
- package/dist/resources/outcome-types.js +33 -0
- package/dist/resources/reports.d.ts +92 -0
- package/dist/resources/reports.js +120 -0
- package/dist/resources/value-models.d.ts +99 -0
- package/dist/resources/value-models.js +133 -0
- package/dist/resources/workspace.d.ts +15 -0
- package/dist/resources/workspace.js +18 -0
- package/dist/runtime.d.ts +2 -0
- package/dist/runtime.js +2 -1
- package/dist/share.d.ts +52 -0
- package/dist/share.js +60 -0
- package/dist/transport.d.ts +54 -5
- package/dist/transport.js +81 -32
- package/dist/types.d.ts +163 -3
- package/dist/types.js +18 -3
- package/dist/unicode.d.ts +2 -0
- package/dist/unicode.js +11 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
# @allisall/sdk
|
|
2
2
|
|
|
3
|
-
The allisall SDK for TypeScript and JavaScript
|
|
4
|
-
|
|
3
|
+
The allisall SDK for TypeScript and JavaScript: a typed client for the
|
|
4
|
+
allisall API, covering events, imports, clients, agents, value models,
|
|
5
|
+
costs, analytics, calculations, reports, the dashboard, API keys and
|
|
6
|
+
shared reports. It runs on Node 22 and later, and in Chrome 114, Firefox
|
|
7
|
+
135 and Safari 18.4 and later.
|
|
5
8
|
|
|
6
9
|
## Install
|
|
7
10
|
|
|
@@ -51,8 +54,11 @@ console.log(summary.accepted, summary.duplicates, summary.rejected);
|
|
|
51
54
|
|
|
52
55
|
`ingest` takes any number of events. It sends them in batches, retries a
|
|
53
56
|
failure worth retrying, and returns one summary for the whole list. A
|
|
54
|
-
rejected event is
|
|
55
|
-
|
|
57
|
+
rejected event is counted in `summary.rejected`, its problems are listed in
|
|
58
|
+
`summary.errors`, and the others are still stored. When
|
|
59
|
+
`summary.errors_truncated` is `true`, the list omits some problems, and a
|
|
60
|
+
rejected event may have no entry. Write `cost` with `decimal()` to keep it
|
|
61
|
+
exact.
|
|
56
62
|
|
|
57
63
|
### Set your own `event_id` for at-least-once delivery
|
|
58
64
|
|
|
@@ -63,6 +69,135 @@ for example from a queue that redelivers messages, set `event_id`
|
|
|
63
69
|
yourself, such as to the message's id. The API then stores the event
|
|
64
70
|
once, however often it arrives.
|
|
65
71
|
|
|
72
|
+
## Call the API
|
|
73
|
+
|
|
74
|
+
The client groups its methods by resource: `client.clients`,
|
|
75
|
+
`client.agents`, `client.valueModels`, `client.cost`, `client.analytics`,
|
|
76
|
+
`client.calculations`, `client.reports`, `client.dashboard` and more. Each
|
|
77
|
+
method's documentation names the API operation it calls and the scope your
|
|
78
|
+
key needs.
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
import { Allisall } from "@allisall/sdk";
|
|
82
|
+
|
|
83
|
+
const client = new Allisall();
|
|
84
|
+
|
|
85
|
+
const created = await client.clients.create({
|
|
86
|
+
name: "Acme Logistics",
|
|
87
|
+
status: "active",
|
|
88
|
+
client_key: "acme",
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// An update is a merge patch: a member you leave out keeps its value, and
|
|
92
|
+
// null clears it. This sets the renewal date to one calendar year after
|
|
93
|
+
// today's UTC date; a date before yesterday in UTC is refused.
|
|
94
|
+
const renewal = new Date();
|
|
95
|
+
renewal.setUTCFullYear(renewal.getUTCFullYear() + 1);
|
|
96
|
+
const updated = await client.clients.update(created.id, {
|
|
97
|
+
renewal_date: renewal.toISOString().slice(0, 10),
|
|
98
|
+
});
|
|
99
|
+
console.log(updated.name, updated.renewal_date);
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
A call that creates something other than an API key sends an
|
|
103
|
+
`Idempotency-Key`, and sends the same key again on a retry, so a retry
|
|
104
|
+
never creates a second one. Amounts
|
|
105
|
+
of money come back as a `Decimal`, a string holding the exact amount.
|
|
106
|
+
|
|
107
|
+
## Lists
|
|
108
|
+
|
|
109
|
+
A list method returns every item of every page when you iterate it with
|
|
110
|
+
`for await`. The SDK fetches each page as the loop reaches it:
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
import { Allisall } from "@allisall/sdk";
|
|
114
|
+
|
|
115
|
+
const client = new Allisall();
|
|
116
|
+
|
|
117
|
+
for await (const agent of client.agents.list()) {
|
|
118
|
+
console.log(agent.agent_key, agent.name);
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
To work a page at a time, await the call instead. The page holds its items
|
|
123
|
+
in `data`, and `hasNextPage()` and `getNextPage()` move through the rest:
|
|
124
|
+
|
|
125
|
+
```ts
|
|
126
|
+
import { Allisall } from "@allisall/sdk";
|
|
127
|
+
|
|
128
|
+
const client = new Allisall();
|
|
129
|
+
|
|
130
|
+
const page = await client.agents.list({ limit: 50 });
|
|
131
|
+
console.log(page.data.length, page.hasNextPage());
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
## Errors
|
|
135
|
+
|
|
136
|
+
Every error from the API or the network is an `AllisallError`. An error
|
|
137
|
+
response from the API is an `ApiError`, with the HTTP `status`, the API's
|
|
138
|
+
error `code` and the `requestId` to quote when you report a problem.
|
|
139
|
+
Branch on `code`:
|
|
140
|
+
|
|
141
|
+
```ts
|
|
142
|
+
import { Allisall, ApiError } from "@allisall/sdk";
|
|
143
|
+
|
|
144
|
+
const client = new Allisall();
|
|
145
|
+
|
|
146
|
+
try {
|
|
147
|
+
await client.clients.delete("CLIENT_ID");
|
|
148
|
+
} catch (error) {
|
|
149
|
+
if (error instanceof ApiError && error.code === "client_has_events") {
|
|
150
|
+
// Events carry the client's key, so mark the client churned instead.
|
|
151
|
+
await client.clients.update("CLIENT_ID", { status: "churned" });
|
|
152
|
+
} else {
|
|
153
|
+
throw error;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
A `ConnectionError` means no response arrived, after any retries. A few
|
|
159
|
+
errors are not an `AllisallError`. An argument the SDK cannot send, such
|
|
160
|
+
as an invalid `Date`, throws a `TypeError` before anything is sent. An
|
|
161
|
+
error thrown by your `tokenProvider` reaches you unchanged. A call you
|
|
162
|
+
abort with `signal` rejects with the signal's reason.
|
|
163
|
+
|
|
164
|
+
The SDK retries a failure worth retrying, twice by default; set
|
|
165
|
+
`maxRetries` on the client or on one call to change that. It retries only
|
|
166
|
+
a request that cannot take effect twice: a `GET`, `PUT` or `DELETE`;
|
|
167
|
+
ingest, whose event ids deduplicate; and a call that carries an
|
|
168
|
+
`Idempotency-Key`. The SDK sends that key, the same on every attempt,
|
|
169
|
+
when it creates anything but an API key and when it commits an import. A
|
|
170
|
+
`PATCH`, the calculation and report actions (validate, publish, reopen,
|
|
171
|
+
share and revoke), revoking or creating an API key, an import preview,
|
|
172
|
+
and setting a value model's line items are never retried. Setting line
|
|
173
|
+
items is guarded by the model's `version`, which refuses a repeat of a
|
|
174
|
+
write that already landed.
|
|
175
|
+
|
|
176
|
+
## Sessions
|
|
177
|
+
|
|
178
|
+
Some operations record a person's decision, such as publishing a
|
|
179
|
+
calculation, sharing a report, or managing API keys. They take a signed-in
|
|
180
|
+
user's session token, and refuse any API key with `403`
|
|
181
|
+
`session_required`. Pass a `tokenProvider` that returns the current token.
|
|
182
|
+
The client calls it before every attempt, so a refreshed token is picked
|
|
183
|
+
up:
|
|
184
|
+
|
|
185
|
+
```ts
|
|
186
|
+
import { Allisall } from "@allisall/sdk";
|
|
187
|
+
|
|
188
|
+
// Your sign-in library's way to read the signed-in user's session token.
|
|
189
|
+
declare function currentSessionToken(): Promise<string>;
|
|
190
|
+
|
|
191
|
+
const client = new Allisall({ tokenProvider: currentSessionToken });
|
|
192
|
+
|
|
193
|
+
const calculation = await client.calculations.publish("CALCULATION_ID");
|
|
194
|
+
console.log(calculation.state);
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
`getSharedReport(shareToken)` reads a shared report with its share token.
|
|
198
|
+
It is a function rather than a client method, because it needs no API key
|
|
199
|
+
and no session.
|
|
200
|
+
|
|
66
201
|
## Browsers
|
|
67
202
|
|
|
68
203
|
An API key in a browser can be read by anyone who loads the page. The
|
|
@@ -81,6 +216,12 @@ const client = new Allisall({
|
|
|
81
216
|
|
|
82
217
|
Prefer calling the API from your server.
|
|
83
218
|
|
|
219
|
+
The SDK supports Chrome 114, Firefox 135 and Safari 18.4 and later. They are
|
|
220
|
+
the first releases that read and write JSON numbers exactly, which the SDK
|
|
221
|
+
needs for amounts of money. In an older browser, a call that sends a
|
|
222
|
+
`Decimal` or receives an amount fails with an `AllisallError` that names
|
|
223
|
+
the missing feature.
|
|
224
|
+
|
|
84
225
|
## Documentation
|
|
85
226
|
|
|
86
227
|
See [https://docs.allisai.ai](https://docs.allisai.ai).
|
package/dist/client.d.ts
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
|
+
import { Agents } from "./resources/agents.js";
|
|
2
|
+
import { Analytics } from "./resources/analytics.js";
|
|
3
|
+
import { ApiKeys } from "./resources/api-keys.js";
|
|
4
|
+
import { Calculations } from "./resources/calculations.js";
|
|
5
|
+
import { Clients } from "./resources/clients.js";
|
|
6
|
+
import { Cost } from "./resources/cost.js";
|
|
7
|
+
import { DashboardOperations } from "./resources/dashboard.js";
|
|
8
|
+
import { Events } from "./resources/events.js";
|
|
9
|
+
import { HealthOperations } from "./resources/health.js";
|
|
10
|
+
import { Imports } from "./resources/imports.js";
|
|
11
|
+
import { OnboardingOperations } from "./resources/onboarding.js";
|
|
12
|
+
import { OutcomeTypes } from "./resources/outcome-types.js";
|
|
13
|
+
import { Reports } from "./resources/reports.js";
|
|
14
|
+
import { ValueModels } from "./resources/value-models.js";
|
|
15
|
+
import { WorkspaceOperations } from "./resources/workspace.js";
|
|
1
16
|
import { type FetchLike, type TokenProvider, type TransportConfig } from "./transport.js";
|
|
2
|
-
import type { EventInput, IngestSummary } from "./types.js";
|
|
3
17
|
/** The options of `new Allisall()`. Every one is optional. */
|
|
4
18
|
export interface ClientOptions {
|
|
5
19
|
/**
|
|
@@ -16,6 +30,10 @@ export interface ClientOptions {
|
|
|
16
30
|
/**
|
|
17
31
|
* The API's base URL, absolute, `http` or `https`. Defaults to
|
|
18
32
|
* `AIA_BASE_URL` from the environment, then `https://api.allisai.ai`.
|
|
33
|
+
*
|
|
34
|
+
* In a browser it may also be a path on the page's own origin, starting
|
|
35
|
+
* with a single `/`, such as `/api` for a proxy. It is resolved against
|
|
36
|
+
* the page's address once, when the client is constructed.
|
|
19
37
|
*/
|
|
20
38
|
baseURL?: string | undefined;
|
|
21
39
|
/** The timeout of each attempt, in milliseconds. Defaults to 60,000. */
|
|
@@ -29,49 +47,81 @@ export interface ClientOptions {
|
|
|
29
47
|
* read it. Defaults to `false`.
|
|
30
48
|
*/
|
|
31
49
|
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
50
|
/**
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
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.
|
|
51
|
+
* Headers sent on every request. They may not name `Authorization`,
|
|
52
|
+
* `Content-Type`, `Idempotency-Key` or `X-Request-Id`, which the SDK sets.
|
|
53
|
+
* A call's own `headers` are sent over them.
|
|
61
54
|
*/
|
|
62
|
-
|
|
55
|
+
defaultHeaders?: Readonly<Record<string, string>> | undefined;
|
|
63
56
|
}
|
|
64
|
-
/** The
|
|
57
|
+
/** The options a client and a shared-report call resolve alike. */
|
|
58
|
+
export type TransportOptions = Pick<ClientOptions, "baseURL" | "timeout" | "maxRetries" | "fetch">;
|
|
59
|
+
/**
|
|
60
|
+
* Resolves what every request needs apart from its credential and default
|
|
61
|
+
* headers: the base URL, timeout, retries, `fetch` and `User-Agent`, which
|
|
62
|
+
* a browser does not send.
|
|
63
|
+
*
|
|
64
|
+
* @throws {AllisallError} for a base URL, timeout or `maxRetries` the
|
|
65
|
+
* client refuses.
|
|
66
|
+
*/
|
|
67
|
+
export declare function transportSettings(options: TransportOptions, browser: boolean): Pick<TransportConfig, "baseURL" | "timeout" | "maxRetries" | "fetch" | "userAgent">;
|
|
68
|
+
/**
|
|
69
|
+
* Checks the client's options and fixes everything a request needs.
|
|
70
|
+
*
|
|
71
|
+
* @throws {AllisallError} for an option the client refuses.
|
|
72
|
+
* @throws {TypeError} when `defaultHeaders` is not a plain object of
|
|
73
|
+
* header names and string values, names a header the SDK sets, or holds a
|
|
74
|
+
* name or value the platform cannot send.
|
|
75
|
+
*/
|
|
76
|
+
export declare function transportConfig(options: ClientOptions): TransportConfig;
|
|
77
|
+
/**
|
|
78
|
+
* The allisall API client. Construct one and reuse it.
|
|
79
|
+
*
|
|
80
|
+
* Its operations are grouped by what they act on, one property per group,
|
|
81
|
+
* such as `client.clients.list()`. A failed call rejects with an `ApiError`
|
|
82
|
+
* for an error response, or a `ConnectionError` when no response arrived.
|
|
83
|
+
*/
|
|
65
84
|
export declare class Allisall {
|
|
66
|
-
/**
|
|
85
|
+
/** Events: ingest, and the events behind a client's totals. */
|
|
67
86
|
readonly events: Events;
|
|
87
|
+
/** CSV imports of events. */
|
|
88
|
+
readonly imports: Imports;
|
|
89
|
+
/** Clients, and the agents assigned to each. */
|
|
90
|
+
readonly clients: Clients;
|
|
91
|
+
/** Agents. */
|
|
92
|
+
readonly agents: Agents;
|
|
93
|
+
/** Value models, by their own id or by their client's, and value templates. */
|
|
94
|
+
readonly valueModels: ValueModels;
|
|
95
|
+
/** Outcome types, for the workspace or for one client. */
|
|
96
|
+
readonly outcomeTypes: OutcomeTypes;
|
|
97
|
+
/** Cost line items and the price table. */
|
|
98
|
+
readonly cost: Cost;
|
|
99
|
+
/** A client's value, cost, ROI and anomalies over a period. */
|
|
100
|
+
readonly analytics: Analytics;
|
|
101
|
+
/** Calculations, and the human steps that validate, publish and reopen them. */
|
|
102
|
+
readonly calculations: Calculations;
|
|
103
|
+
/** Reports on calculations, and the human steps that share and revoke them. */
|
|
104
|
+
readonly reports: Reports;
|
|
105
|
+
/** The workspace's month, and renewal readiness. */
|
|
106
|
+
readonly dashboard: DashboardOperations;
|
|
107
|
+
/** API keys, managed with a session token. */
|
|
108
|
+
readonly apiKeys: ApiKeys;
|
|
109
|
+
/** The API's liveness. */
|
|
110
|
+
readonly health: HealthOperations;
|
|
111
|
+
/** The workspace the credential belongs to. */
|
|
112
|
+
readonly workspace: WorkspaceOperations;
|
|
113
|
+
/** The guided setup's progress, and the steps skipped in it. */
|
|
114
|
+
readonly onboarding: OnboardingOperations;
|
|
68
115
|
/**
|
|
69
116
|
* @throws {AllisallError} when there is no credential, or both an API key
|
|
70
|
-
* and a token provider; when the base URL is
|
|
71
|
-
* `https` URL
|
|
72
|
-
*
|
|
73
|
-
* `
|
|
117
|
+
* and a token provider; when the base URL is neither an absolute `http`
|
|
118
|
+
* or `https` URL nor, in a browser, a path starting with a single `/`
|
|
119
|
+
* that resolves to the page's own origin; when `maxRetries` is negative
|
|
120
|
+
* or `timeout` is not positive; or when an API key is used in a browser
|
|
121
|
+
* without `dangerouslyAllowBrowser`.
|
|
122
|
+
* @throws {TypeError} when `defaultHeaders` is not a plain object of
|
|
123
|
+
* header names and string values, names a header the SDK sets, or holds a
|
|
124
|
+
* name or value the platform cannot send.
|
|
74
125
|
*/
|
|
75
126
|
constructor(options?: ClientOptions);
|
|
76
127
|
}
|
|
77
|
-
export {};
|
package/dist/client.js
CHANGED
|
@@ -1,12 +1,25 @@
|
|
|
1
1
|
// The client: its options, and how the credential and the base URL are
|
|
2
2
|
// resolved at construction.
|
|
3
3
|
import { AllisallError } from "./errors.js";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
4
|
+
import { DEFAULT_MAX_RETRIES, DEFAULT_TIMEOUT_MS, resolveHeaders, resolveMaxRetries, resolveTimeout, } from "./options.js";
|
|
5
|
+
import { Agents } from "./resources/agents.js";
|
|
6
|
+
import { Analytics } from "./resources/analytics.js";
|
|
7
|
+
import { ApiKeys } from "./resources/api-keys.js";
|
|
8
|
+
import { Calculations } from "./resources/calculations.js";
|
|
9
|
+
import { Clients } from "./resources/clients.js";
|
|
10
|
+
import { Cost } from "./resources/cost.js";
|
|
11
|
+
import { DashboardOperations } from "./resources/dashboard.js";
|
|
12
|
+
import { Events } from "./resources/events.js";
|
|
13
|
+
import { HealthOperations } from "./resources/health.js";
|
|
14
|
+
import { Imports } from "./resources/imports.js";
|
|
15
|
+
import { OnboardingOperations } from "./resources/onboarding.js";
|
|
16
|
+
import { OutcomeTypes } from "./resources/outcome-types.js";
|
|
17
|
+
import { Reports } from "./resources/reports.js";
|
|
18
|
+
import { ValueModels } from "./resources/value-models.js";
|
|
19
|
+
import { WorkspaceOperations } from "./resources/workspace.js";
|
|
20
|
+
import { globalValue, isBrowser, readEnv, userAgent } from "./runtime.js";
|
|
21
|
+
import { BROWSER_KEY_MESSAGE, isPrintableCredential, } from "./transport.js";
|
|
7
22
|
const DEFAULT_BASE_URL = "https://api.allisai.ai";
|
|
8
|
-
const DEFAULT_TIMEOUT_MS = 60_000;
|
|
9
|
-
const DEFAULT_MAX_RETRIES = 2;
|
|
10
23
|
function resolveCredential(options) {
|
|
11
24
|
const { apiKey, tokenProvider } = options;
|
|
12
25
|
if (apiKey !== undefined && tokenProvider !== undefined) {
|
|
@@ -38,93 +51,151 @@ function resolveCredential(options) {
|
|
|
38
51
|
}
|
|
39
52
|
throw new AllisallError("allisall: no credential; pass apiKey or tokenProvider, or set AIA_API_KEY");
|
|
40
53
|
}
|
|
41
|
-
|
|
54
|
+
/** Reads `globalThis.location.href`, where it exists. */
|
|
55
|
+
function pageAddress() {
|
|
56
|
+
const location = globalValue("location");
|
|
57
|
+
if (typeof location !== "object" || location === null)
|
|
58
|
+
return undefined;
|
|
59
|
+
const href = Reflect.get(location, "href");
|
|
60
|
+
return typeof href === "string" ? href : undefined;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Resolves the base URL: the option, else `AIA_BASE_URL`, else the API's
|
|
64
|
+
* own host. In a browser, a path starting with a single `/` is resolved
|
|
65
|
+
* against the page's address and must keep its origin.
|
|
66
|
+
*
|
|
67
|
+
* @throws {AllisallError} for a base URL the client refuses.
|
|
68
|
+
*/
|
|
69
|
+
function resolveBaseURL(option, browser) {
|
|
42
70
|
const raw = option ?? readEnv("AIA_BASE_URL") ?? DEFAULT_BASE_URL;
|
|
71
|
+
// A path on the page's own origin, such as "/api". A leading "//" names
|
|
72
|
+
// another host, so it is not a path.
|
|
73
|
+
const pathOnly = typeof raw === "string" && raw.startsWith("/") && !raw.startsWith("//");
|
|
74
|
+
const page = browser && pathOnly ? pageAddress() : undefined;
|
|
43
75
|
let url;
|
|
44
76
|
try {
|
|
45
|
-
url = new URL(raw);
|
|
77
|
+
url = page === undefined ? new URL(raw) : new URL(raw, page);
|
|
46
78
|
}
|
|
47
79
|
catch {
|
|
48
|
-
throw new AllisallError(
|
|
80
|
+
throw new AllisallError(pathOnly && browser
|
|
81
|
+
? `allisall: the base URL ${JSON.stringify(raw)} is a path, and the page's address could not be read to resolve it`
|
|
82
|
+
: `allisall: the base URL ${JSON.stringify(raw)} is not an absolute URL`);
|
|
49
83
|
}
|
|
50
84
|
if (url.protocol !== "http:" && url.protocol !== "https:") {
|
|
51
85
|
throw new AllisallError(`allisall: the base URL ${JSON.stringify(raw)} is not http or https`);
|
|
52
86
|
}
|
|
87
|
+
// URL parsing reads a backslash as "/" and drops tabs and newlines, so a
|
|
88
|
+
// value such as "/\other.example/api" passes the "//" check above and
|
|
89
|
+
// still names another host. Only the resolved origin settles it.
|
|
90
|
+
if (page !== undefined && url.origin !== new URL(page).origin) {
|
|
91
|
+
throw new AllisallError(`allisall: the base URL ${JSON.stringify(raw)} is a path, but resolves to another origin, ${url.origin}`);
|
|
92
|
+
}
|
|
53
93
|
return url;
|
|
54
94
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
95
|
+
/**
|
|
96
|
+
* Resolves what every request needs apart from its credential and default
|
|
97
|
+
* headers: the base URL, timeout, retries, `fetch` and `User-Agent`, which
|
|
98
|
+
* a browser does not send.
|
|
99
|
+
*
|
|
100
|
+
* @throws {AllisallError} for a base URL, timeout or `maxRetries` the
|
|
101
|
+
* client refuses.
|
|
102
|
+
*/
|
|
103
|
+
export function transportSettings(options, browser) {
|
|
104
|
+
return {
|
|
105
|
+
baseURL: resolveBaseURL(options.baseURL, browser),
|
|
106
|
+
timeout: resolveTimeout(options.timeout, DEFAULT_TIMEOUT_MS),
|
|
107
|
+
maxRetries: resolveMaxRetries(options.maxRetries, DEFAULT_MAX_RETRIES),
|
|
108
|
+
fetch: options.fetch,
|
|
109
|
+
userAgent: userAgent(),
|
|
110
|
+
};
|
|
63
111
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
112
|
+
/**
|
|
113
|
+
* Checks the client's options and fixes everything a request needs.
|
|
114
|
+
*
|
|
115
|
+
* @throws {AllisallError} for an option the client refuses.
|
|
116
|
+
* @throws {TypeError} when `defaultHeaders` is not a plain object of
|
|
117
|
+
* header names and string values, names a header the SDK sets, or holds a
|
|
118
|
+
* name or value the platform cannot send.
|
|
119
|
+
*/
|
|
120
|
+
export function transportConfig(options) {
|
|
121
|
+
const credential = resolveCredential(options);
|
|
122
|
+
const browser = isBrowser();
|
|
123
|
+
const refuseApiKeys = browser && options.dangerouslyAllowBrowser !== true;
|
|
124
|
+
if (refuseApiKeys && credential.kind === "apiKey") {
|
|
125
|
+
throw new AllisallError(BROWSER_KEY_MESSAGE);
|
|
68
126
|
}
|
|
69
|
-
return
|
|
127
|
+
return {
|
|
128
|
+
...transportSettings(options, browser),
|
|
129
|
+
credential,
|
|
130
|
+
refuseApiKeys,
|
|
131
|
+
defaultHeaders: resolveHeaders(options.defaultHeaders, "defaultHeaders"),
|
|
132
|
+
};
|
|
70
133
|
}
|
|
71
|
-
/**
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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. */
|
|
134
|
+
/**
|
|
135
|
+
* The allisall API client. Construct one and reuse it.
|
|
136
|
+
*
|
|
137
|
+
* Its operations are grouped by what they act on, one property per group,
|
|
138
|
+
* such as `client.clients.list()`. A failed call rejects with an `ApiError`
|
|
139
|
+
* for an error response, or a `ConnectionError` when no response arrived.
|
|
140
|
+
*/
|
|
103
141
|
export class Allisall {
|
|
104
|
-
/**
|
|
142
|
+
/** Events: ingest, and the events behind a client's totals. */
|
|
105
143
|
events;
|
|
144
|
+
/** CSV imports of events. */
|
|
145
|
+
imports;
|
|
146
|
+
/** Clients, and the agents assigned to each. */
|
|
147
|
+
clients;
|
|
148
|
+
/** Agents. */
|
|
149
|
+
agents;
|
|
150
|
+
/** Value models, by their own id or by their client's, and value templates. */
|
|
151
|
+
valueModels;
|
|
152
|
+
/** Outcome types, for the workspace or for one client. */
|
|
153
|
+
outcomeTypes;
|
|
154
|
+
/** Cost line items and the price table. */
|
|
155
|
+
cost;
|
|
156
|
+
/** A client's value, cost, ROI and anomalies over a period. */
|
|
157
|
+
analytics;
|
|
158
|
+
/** Calculations, and the human steps that validate, publish and reopen them. */
|
|
159
|
+
calculations;
|
|
160
|
+
/** Reports on calculations, and the human steps that share and revoke them. */
|
|
161
|
+
reports;
|
|
162
|
+
/** The workspace's month, and renewal readiness. */
|
|
163
|
+
dashboard;
|
|
164
|
+
/** API keys, managed with a session token. */
|
|
165
|
+
apiKeys;
|
|
166
|
+
/** The API's liveness. */
|
|
167
|
+
health;
|
|
168
|
+
/** The workspace the credential belongs to. */
|
|
169
|
+
workspace;
|
|
170
|
+
/** The guided setup's progress, and the steps skipped in it. */
|
|
171
|
+
onboarding;
|
|
106
172
|
/**
|
|
107
173
|
* @throws {AllisallError} when there is no credential, or both an API key
|
|
108
|
-
* and a token provider; when the base URL is
|
|
109
|
-
* `https` URL
|
|
110
|
-
*
|
|
111
|
-
* `
|
|
174
|
+
* and a token provider; when the base URL is neither an absolute `http`
|
|
175
|
+
* or `https` URL nor, in a browser, a path starting with a single `/`
|
|
176
|
+
* that resolves to the page's own origin; when `maxRetries` is negative
|
|
177
|
+
* or `timeout` is not positive; or when an API key is used in a browser
|
|
178
|
+
* without `dangerouslyAllowBrowser`.
|
|
179
|
+
* @throws {TypeError} when `defaultHeaders` is not a plain object of
|
|
180
|
+
* header names and string values, names a header the SDK sets, or holds a
|
|
181
|
+
* name or value the platform cannot send.
|
|
112
182
|
*/
|
|
113
183
|
constructor(options = {}) {
|
|
114
|
-
const
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
this.
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
184
|
+
const config = transportConfig(options);
|
|
185
|
+
this.events = new Events(config);
|
|
186
|
+
this.imports = new Imports(config);
|
|
187
|
+
this.clients = new Clients(config);
|
|
188
|
+
this.agents = new Agents(config);
|
|
189
|
+
this.valueModels = new ValueModels(config);
|
|
190
|
+
this.outcomeTypes = new OutcomeTypes(config);
|
|
191
|
+
this.cost = new Cost(config);
|
|
192
|
+
this.analytics = new Analytics(config);
|
|
193
|
+
this.calculations = new Calculations(config);
|
|
194
|
+
this.reports = new Reports(config);
|
|
195
|
+
this.dashboard = new DashboardOperations(config);
|
|
196
|
+
this.apiKeys = new ApiKeys(config);
|
|
197
|
+
this.health = new HealthOperations(config);
|
|
198
|
+
this.workspace = new WorkspaceOperations(config);
|
|
199
|
+
this.onboarding = new OnboardingOperations(config);
|
|
129
200
|
}
|
|
130
201
|
}
|