@awesomate/sdk 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 +21 -0
- package/README.md +58 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +50 -0
- package/dist/index.d.ts +182 -0
- package/dist/index.js +134 -0
- package/package.json +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 IT Mooti Pty Ltd t/a Awesomate
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# @awesomate/sdk
|
|
2
|
+
|
|
3
|
+
Read your own Awesomate data from Node. Version 0.1 reads your **contact list**: query it,
|
|
4
|
+
page through it, count it, with TypeScript types generated from your own fields.
|
|
5
|
+
|
|
6
|
+
```bash
|
|
7
|
+
npm install @awesomate/sdk
|
|
8
|
+
npx @awesomate/sdk types --out awesomate.d.ts # uses your Awesomate MCP connection, or AWESOMATE_TOKEN
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { createClient } from '@awesomate/sdk';
|
|
13
|
+
import './awesomate'; // the generated file: every query below is typed
|
|
14
|
+
|
|
15
|
+
const db = createClient({ token: process.env.AWESOMATE_TOKEN! });
|
|
16
|
+
|
|
17
|
+
// People who joined this year, newest first
|
|
18
|
+
const { rows, next, applied } = await db.query('contact', {
|
|
19
|
+
where: { created_at: { gte: '$YEAR_BEGIN' } },
|
|
20
|
+
orderBy: ['created_at', 'desc'],
|
|
21
|
+
limit: 50,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
// Everyone tagged VIP in one suburb, a page at a time
|
|
25
|
+
for await (const person of db.queryAll('contact', { where: { tags: 'VIP', suburb: 'Carindale' }, select: ['first_name', 'email'] })) {
|
|
26
|
+
console.log(person.first_name, person.email);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const one = await db.get('contact', rows[0].id); // null if there is none
|
|
30
|
+
const byPlan = await db.aggregate({ measures: [{ column: 'people', agg: 'count' }], dimensions: ['plan'] });
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## What you can read
|
|
34
|
+
|
|
35
|
+
The built-in contact details (name, email, phone, company, tags, when they were added) and only
|
|
36
|
+
the fields you have marked **readable by AI** under Contacts, Your fields in the hub. A field
|
|
37
|
+
marked sensitive is never readable. `db.schema()` lists the columns and says how many are hidden.
|
|
38
|
+
|
|
39
|
+
## The where grammar
|
|
40
|
+
|
|
41
|
+
`{ column: value }` means equals; `{ column: { op: value } }` uses an operator: `eq`, `neq`, `gt`,
|
|
42
|
+
`gte`, `lt`, `lte`, `in`, `nin`, `contains`, `startsWith`, `like`, `ilike`, `isNull`, and for lists
|
|
43
|
+
(tags) `has`, `hasAny`, `hasAll`, `isEmpty`. Combine with `and: [...]`, `or: [...]`, `not: {...}`.
|
|
44
|
+
A date (`'2026-09-30'`) against a timestamp means that whole day in your time zone; time
|
|
45
|
+
variables `$TODAY`, `$WEEK_BEGIN`, `$MONTH_BEGIN`, `$QUARTER_BEGIN`, `$YEAR_BEGIN` and `$FY_BEGIN`
|
|
46
|
+
(1 July) take an offset in their own unit (`$MONTH_BEGIN-1` is the start of last month).
|
|
47
|
+
|
|
48
|
+
## Errors
|
|
49
|
+
|
|
50
|
+
Every failure is an `AwesomateError` with a `code`: `unauthenticated`, `forbidden`, `not_found`,
|
|
51
|
+
`validation` (with `field`), `consent_blocked`, `rate_limited`, `conflict` or `unavailable`.
|
|
52
|
+
|
|
53
|
+
## Keep the token on a server
|
|
54
|
+
|
|
55
|
+
The token is your account's hosting token. Use it in a server, a script or a scheduled job, never
|
|
56
|
+
in a browser. Sign-in for your own app's users comes in a later version.
|
|
57
|
+
|
|
58
|
+
MIT licence.
|
package/dist/cli.d.ts
ADDED
package/dist/cli.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* npx @awesomate/sdk types [--out awesomate.d.ts] [--profile <name>] [--base <url>]
|
|
4
|
+
*
|
|
5
|
+
* Writes the account's readable kinds as TypeScript. The token comes from AWESOMATE_TOKEN, or
|
|
6
|
+
* else from ~/.awesomate/credentials.json (the file the Awesomate MCP keeps): --profile, then
|
|
7
|
+
* AWESOMATE_ACCOUNT, then its defaultProfile. The account is printed, so a wrong one is obvious.
|
|
8
|
+
*/
|
|
9
|
+
import { readFileSync, writeFileSync } from 'node:fs';
|
|
10
|
+
import { homedir } from 'node:os';
|
|
11
|
+
import { join, resolve } from 'node:path';
|
|
12
|
+
import { AwesomateError, createClient } from './index.js';
|
|
13
|
+
function arg(name) {
|
|
14
|
+
const i = process.argv.indexOf(`--${name}`);
|
|
15
|
+
return i > 0 ? process.argv[i + 1] : undefined;
|
|
16
|
+
}
|
|
17
|
+
function credentials() {
|
|
18
|
+
if (process.env.AWESOMATE_TOKEN) {
|
|
19
|
+
return { token: process.env.AWESOMATE_TOKEN, baseUrl: arg('base') ?? process.env.AWESOMATE_BASE_URL, account: 'from AWESOMATE_TOKEN' };
|
|
20
|
+
}
|
|
21
|
+
let creds;
|
|
22
|
+
try {
|
|
23
|
+
creds = JSON.parse(readFileSync(join(homedir(), '.awesomate', 'credentials.json'), 'utf8'));
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
throw new Error('No token: set AWESOMATE_TOKEN, or connect the Awesomate MCP so ~/.awesomate/credentials.json exists.');
|
|
27
|
+
}
|
|
28
|
+
const name = arg('profile') ?? process.env.AWESOMATE_ACCOUNT ?? creds.defaultProfile;
|
|
29
|
+
const p = name ? creds.profiles?.[name] : undefined;
|
|
30
|
+
if (!p?.pat)
|
|
31
|
+
throw new Error(`No profile ${name ?? '(none)'} with a token in ~/.awesomate/credentials.json.`);
|
|
32
|
+
return { token: p.pat, baseUrl: arg('base') ?? p.apiBase, account: p.slug ?? name };
|
|
33
|
+
}
|
|
34
|
+
async function main() {
|
|
35
|
+
const cmd = process.argv[2];
|
|
36
|
+
if (cmd !== 'types') {
|
|
37
|
+
console.log('Usage: npx @awesomate/sdk types [--out awesomate.d.ts] [--profile <name>] [--base <url>]');
|
|
38
|
+
process.exit(cmd === undefined || cmd === '--help' || cmd === '-h' ? 0 : 1);
|
|
39
|
+
}
|
|
40
|
+
const { token, baseUrl, account } = credentials();
|
|
41
|
+
console.error(`account: ${account}`);
|
|
42
|
+
const r = await createClient({ token, baseUrl }).types();
|
|
43
|
+
const out = resolve(arg('out') ?? r.file);
|
|
44
|
+
writeFileSync(out, r.content);
|
|
45
|
+
console.log(`wrote ${out}`);
|
|
46
|
+
}
|
|
47
|
+
main().catch((err) => {
|
|
48
|
+
console.error(err instanceof AwesomateError ? `${err.code}: ${err.message}` : err.message);
|
|
49
|
+
process.exit(1);
|
|
50
|
+
});
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @awesomate/sdk: read a client's own Awesomate data from Node (v0: the contact list).
|
|
3
|
+
*
|
|
4
|
+
* import { createClient } from '@awesomate/sdk';
|
|
5
|
+
* import './awesomate'; // the file `npx @awesomate/sdk types` wrote: types every query below
|
|
6
|
+
* const db = createClient({ token: process.env.AWESOMATE_TOKEN! });
|
|
7
|
+
* const { rows, next } = await db.query('contact', { where: { created_at: { gte: '$YEAR_BEGIN' } }, limit: 50 });
|
|
8
|
+
*
|
|
9
|
+
* The account's token (a hosting PAT with crm:read) is a secret: use it on a server, never in a
|
|
10
|
+
* browser. End-user sign-in for browser apps comes in a later version.
|
|
11
|
+
*/
|
|
12
|
+
export declare const VERSION = "0.1.0";
|
|
13
|
+
/** Augmented by the generated awesomate.d.ts, so each kind's rows are typed. */
|
|
14
|
+
export interface Kinds {
|
|
15
|
+
}
|
|
16
|
+
export type KindName = [keyof Kinds] extends [never] ? string : Extract<keyof Kinds, string>;
|
|
17
|
+
export type RowOf<K> = K extends keyof Kinds ? Kinds[K] : Record<string, unknown>;
|
|
18
|
+
type TextOps<V extends string> = {
|
|
19
|
+
eq?: V | null;
|
|
20
|
+
neq?: V | null;
|
|
21
|
+
in?: V[];
|
|
22
|
+
nin?: V[];
|
|
23
|
+
contains?: string;
|
|
24
|
+
startsWith?: string;
|
|
25
|
+
like?: string;
|
|
26
|
+
ilike?: string;
|
|
27
|
+
gt?: string;
|
|
28
|
+
gte?: string;
|
|
29
|
+
lt?: string;
|
|
30
|
+
lte?: string;
|
|
31
|
+
isNull?: boolean;
|
|
32
|
+
};
|
|
33
|
+
type NumberOps = {
|
|
34
|
+
eq?: number | null;
|
|
35
|
+
neq?: number | null;
|
|
36
|
+
gt?: number;
|
|
37
|
+
gte?: number;
|
|
38
|
+
lt?: number;
|
|
39
|
+
lte?: number;
|
|
40
|
+
in?: number[];
|
|
41
|
+
nin?: number[];
|
|
42
|
+
isNull?: boolean;
|
|
43
|
+
};
|
|
44
|
+
type BoolOps = {
|
|
45
|
+
eq?: boolean | null;
|
|
46
|
+
neq?: boolean | null;
|
|
47
|
+
isNull?: boolean;
|
|
48
|
+
};
|
|
49
|
+
type ListOps<E> = {
|
|
50
|
+
has?: E;
|
|
51
|
+
hasAny?: E[];
|
|
52
|
+
hasAll?: E[];
|
|
53
|
+
isEmpty?: boolean;
|
|
54
|
+
isNull?: boolean;
|
|
55
|
+
};
|
|
56
|
+
/** What one column accepts in a where clause: a bare value means equals (a list means in, or has all for a list column). */
|
|
57
|
+
export type ColumnFilter<V> = V extends Array<infer E> ? E | E[] | ListOps<E> : V extends number ? V | V[] | null | NumberOps : V extends boolean ? V | null | BoolOps : V extends string ? V | V[] | null | TextOps<V> : unknown;
|
|
58
|
+
export type Where<T> = {
|
|
59
|
+
[C in keyof T]?: ColumnFilter<NonNullable<T[C]>>;
|
|
60
|
+
} & {
|
|
61
|
+
and?: Where<T>[];
|
|
62
|
+
or?: Where<T>[];
|
|
63
|
+
not?: Where<T>;
|
|
64
|
+
};
|
|
65
|
+
/** Columns that can be sorted by: anything but a list. */
|
|
66
|
+
export type SortableColumn<T> = Extract<{
|
|
67
|
+
[C in keyof T]: NonNullable<T[C]> extends unknown[] ? never : C;
|
|
68
|
+
}[keyof T], string>;
|
|
69
|
+
export interface QueryOptions<T, S extends keyof T = keyof T> {
|
|
70
|
+
where?: Where<T>;
|
|
71
|
+
/** One sort column; id breaks ties. Default: created_at, newest first. */
|
|
72
|
+
orderBy?: [SortableColumn<T>, 'asc' | 'desc'];
|
|
73
|
+
/** Rows per page, 1 to 1,000. Default 50. */
|
|
74
|
+
limit?: number;
|
|
75
|
+
/** The previous page's next. Repeat the same orderBy. */
|
|
76
|
+
after?: string;
|
|
77
|
+
select?: S[];
|
|
78
|
+
/** IANA zone for dates and time variables. Default Australia/Sydney. */
|
|
79
|
+
tz?: string;
|
|
80
|
+
}
|
|
81
|
+
export interface Applied {
|
|
82
|
+
order_by: [string, 'asc' | 'desc'];
|
|
83
|
+
limit: number;
|
|
84
|
+
timezone: string;
|
|
85
|
+
time_variables: Record<string, string>;
|
|
86
|
+
defaults: string[];
|
|
87
|
+
}
|
|
88
|
+
export interface Page<R> {
|
|
89
|
+
rows: R[];
|
|
90
|
+
next: string | null;
|
|
91
|
+
count: number;
|
|
92
|
+
/** What the hub assumed; repeat it to whoever asked. */
|
|
93
|
+
applied: Applied;
|
|
94
|
+
hidden: {
|
|
95
|
+
not_readable_by_ai: number;
|
|
96
|
+
sensitive: number;
|
|
97
|
+
};
|
|
98
|
+
asAt: string;
|
|
99
|
+
}
|
|
100
|
+
export type ErrorCode = 'unauthenticated' | 'forbidden' | 'not_found' | 'validation' | 'consent_blocked' | 'rate_limited' | 'conflict' | 'unavailable';
|
|
101
|
+
export declare class AwesomateError extends Error {
|
|
102
|
+
readonly code: ErrorCode;
|
|
103
|
+
readonly status: number;
|
|
104
|
+
/** The column or parameter a validation error is about. */
|
|
105
|
+
readonly field?: string | undefined;
|
|
106
|
+
/** The hub's own code, unmapped. */
|
|
107
|
+
readonly serverCode?: string | undefined;
|
|
108
|
+
constructor(code: ErrorCode, message: string, status: number,
|
|
109
|
+
/** The column or parameter a validation error is about. */
|
|
110
|
+
field?: string | undefined,
|
|
111
|
+
/** The hub's own code, unmapped. */
|
|
112
|
+
serverCode?: string | undefined);
|
|
113
|
+
}
|
|
114
|
+
export interface ClientOptions {
|
|
115
|
+
/** The account's hosting token (amt_pat_...) with crm:read. */
|
|
116
|
+
token: string;
|
|
117
|
+
baseUrl?: string;
|
|
118
|
+
fetch?: typeof fetch;
|
|
119
|
+
}
|
|
120
|
+
export declare class AwesomateClient {
|
|
121
|
+
private readonly opts;
|
|
122
|
+
private readonly base;
|
|
123
|
+
private readonly doFetch;
|
|
124
|
+
constructor(opts: ClientOptions);
|
|
125
|
+
private call;
|
|
126
|
+
/** The kinds this token can read, their columns, operators and examples. */
|
|
127
|
+
schema(): Promise<{
|
|
128
|
+
kinds: Array<{
|
|
129
|
+
kind: string;
|
|
130
|
+
label: string;
|
|
131
|
+
columns: Array<{
|
|
132
|
+
name: string;
|
|
133
|
+
label: string;
|
|
134
|
+
type: string;
|
|
135
|
+
operators: string[];
|
|
136
|
+
}>;
|
|
137
|
+
}>;
|
|
138
|
+
}>;
|
|
139
|
+
/** The readable kinds as a TypeScript file (what the types command writes). */
|
|
140
|
+
types(): Promise<{
|
|
141
|
+
file: string;
|
|
142
|
+
content: string;
|
|
143
|
+
as_at: string;
|
|
144
|
+
}>;
|
|
145
|
+
query<K extends KindName, S extends keyof RowOf<K> = keyof RowOf<K>>(kind: K, options?: QueryOptions<RowOf<K>, S>): Promise<Page<Pick<RowOf<K>, S>>>;
|
|
146
|
+
/** Every matching row, a page at a time. maxRows guards against reading a whole list by accident. */
|
|
147
|
+
queryAll<K extends KindName, S extends keyof RowOf<K> = keyof RowOf<K>>(kind: K, options?: Omit<QueryOptions<RowOf<K>, S>, 'after'> & {
|
|
148
|
+
maxRows?: number;
|
|
149
|
+
}): AsyncGenerator<Pick<RowOf<K>, S>>;
|
|
150
|
+
/** One row by id, or null when there is none this token can read. */
|
|
151
|
+
get<K extends KindName>(kind: K, id: string, options?: {
|
|
152
|
+
tz?: string;
|
|
153
|
+
}): Promise<RowOf<K> | null>;
|
|
154
|
+
/** Grouped numbers (counts, sums) in the Business Data API's shape. */
|
|
155
|
+
aggregate(request: {
|
|
156
|
+
measures: Array<{
|
|
157
|
+
column: string;
|
|
158
|
+
agg?: 'count' | 'sum' | 'avg' | 'min' | 'max';
|
|
159
|
+
}>;
|
|
160
|
+
dimensions?: string[];
|
|
161
|
+
filters?: Array<{
|
|
162
|
+
column: string;
|
|
163
|
+
op?: string;
|
|
164
|
+
value: unknown;
|
|
165
|
+
}>;
|
|
166
|
+
time?: {
|
|
167
|
+
grain?: string;
|
|
168
|
+
preset?: string;
|
|
169
|
+
from?: string;
|
|
170
|
+
to?: string;
|
|
171
|
+
anchor?: string;
|
|
172
|
+
tz?: string;
|
|
173
|
+
};
|
|
174
|
+
order_by?: Array<{
|
|
175
|
+
field: string;
|
|
176
|
+
dir?: 'asc' | 'desc';
|
|
177
|
+
}>;
|
|
178
|
+
limit?: number;
|
|
179
|
+
}): Promise<Record<string, unknown>>;
|
|
180
|
+
}
|
|
181
|
+
export declare function createClient(options: ClientOptions): AwesomateClient;
|
|
182
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @awesomate/sdk: read a client's own Awesomate data from Node (v0: the contact list).
|
|
3
|
+
*
|
|
4
|
+
* import { createClient } from '@awesomate/sdk';
|
|
5
|
+
* import './awesomate'; // the file `npx @awesomate/sdk types` wrote: types every query below
|
|
6
|
+
* const db = createClient({ token: process.env.AWESOMATE_TOKEN! });
|
|
7
|
+
* const { rows, next } = await db.query('contact', { where: { created_at: { gte: '$YEAR_BEGIN' } }, limit: 50 });
|
|
8
|
+
*
|
|
9
|
+
* The account's token (a hosting PAT with crm:read) is a secret: use it on a server, never in a
|
|
10
|
+
* browser. End-user sign-in for browser apps comes in a later version.
|
|
11
|
+
*/
|
|
12
|
+
export const VERSION = '0.1.0';
|
|
13
|
+
const DEFAULT_BASE = 'https://hub.awesomate.ai';
|
|
14
|
+
export class AwesomateError extends Error {
|
|
15
|
+
code;
|
|
16
|
+
status;
|
|
17
|
+
field;
|
|
18
|
+
serverCode;
|
|
19
|
+
constructor(code, message, status,
|
|
20
|
+
/** The column or parameter a validation error is about. */
|
|
21
|
+
field,
|
|
22
|
+
/** The hub's own code, unmapped. */
|
|
23
|
+
serverCode) {
|
|
24
|
+
super(message);
|
|
25
|
+
this.code = code;
|
|
26
|
+
this.status = status;
|
|
27
|
+
this.field = field;
|
|
28
|
+
this.serverCode = serverCode;
|
|
29
|
+
this.name = 'AwesomateError';
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function errorCode(status, server) {
|
|
33
|
+
if (server === 'validation' || server === 'invalid' || status === 422)
|
|
34
|
+
return 'validation';
|
|
35
|
+
if (status === 401)
|
|
36
|
+
return 'unauthenticated';
|
|
37
|
+
if (status === 403)
|
|
38
|
+
return server === 'consent_blocked' ? 'consent_blocked' : 'forbidden';
|
|
39
|
+
if (status === 429)
|
|
40
|
+
return 'rate_limited';
|
|
41
|
+
if (server === 'conflict')
|
|
42
|
+
return 'conflict';
|
|
43
|
+
if (status === 404 && server !== 'not_live')
|
|
44
|
+
return 'not_found';
|
|
45
|
+
return 'unavailable';
|
|
46
|
+
}
|
|
47
|
+
export class AwesomateClient {
|
|
48
|
+
opts;
|
|
49
|
+
base;
|
|
50
|
+
doFetch;
|
|
51
|
+
constructor(opts) {
|
|
52
|
+
this.opts = opts;
|
|
53
|
+
if (!opts?.token)
|
|
54
|
+
throw new Error('createClient needs a token (the account\'s hosting token with crm:read).');
|
|
55
|
+
this.base = (opts.baseUrl ?? DEFAULT_BASE).replace(/\/+$/, '');
|
|
56
|
+
this.doFetch = opts.fetch ?? globalThis.fetch;
|
|
57
|
+
if (!this.doFetch)
|
|
58
|
+
throw new Error('No fetch available: use Node 18 or later, or pass fetch.');
|
|
59
|
+
}
|
|
60
|
+
async call(method, path, body, retry = true) {
|
|
61
|
+
const res = await this.doFetch(`${this.base}${path}`, {
|
|
62
|
+
method,
|
|
63
|
+
headers: {
|
|
64
|
+
authorization: `Bearer ${this.opts.token}`,
|
|
65
|
+
'user-agent': `@awesomate/sdk/${VERSION}`,
|
|
66
|
+
...(body === undefined ? {} : { 'content-type': 'application/json' }),
|
|
67
|
+
},
|
|
68
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
69
|
+
});
|
|
70
|
+
const text = await res.text();
|
|
71
|
+
let json = {};
|
|
72
|
+
try {
|
|
73
|
+
json = text ? JSON.parse(text) : {};
|
|
74
|
+
}
|
|
75
|
+
catch { /* a proxy's HTML error page */ }
|
|
76
|
+
if (res.ok)
|
|
77
|
+
return json;
|
|
78
|
+
const server = typeof json.code === 'string' ? json.code : undefined;
|
|
79
|
+
const code = errorCode(res.status, server);
|
|
80
|
+
// The field list changed while the read ran (the view is rebuilt on every field edit): a read
|
|
81
|
+
// is safe to run again, once.
|
|
82
|
+
if (code === 'conflict' && retry)
|
|
83
|
+
return this.call(method, path, body, false);
|
|
84
|
+
const message = typeof json.error === 'string' ? json.error : `The hub answered ${res.status}.`;
|
|
85
|
+
throw new AwesomateError(code, message, res.status, typeof json.field === 'string' ? json.field : undefined, server);
|
|
86
|
+
}
|
|
87
|
+
/** The kinds this token can read, their columns, operators and examples. */
|
|
88
|
+
schema() {
|
|
89
|
+
return this.call('GET', '/api/my-crm/v1/rows/schema');
|
|
90
|
+
}
|
|
91
|
+
/** The readable kinds as a TypeScript file (what the types command writes). */
|
|
92
|
+
types() {
|
|
93
|
+
return this.call('GET', '/api/my-crm/v1/rows/types');
|
|
94
|
+
}
|
|
95
|
+
async query(kind, options = {}) {
|
|
96
|
+
const r = await this.call('POST', '/api/my-crm/v1/rows/query', { kind, ...options });
|
|
97
|
+
return { rows: r.rows, next: r.next, count: r.count, applied: r.applied, hidden: r.hidden, asAt: r.as_at };
|
|
98
|
+
}
|
|
99
|
+
/** Every matching row, a page at a time. maxRows guards against reading a whole list by accident. */
|
|
100
|
+
async *queryAll(kind, options = {}) {
|
|
101
|
+
const { maxRows = 10_000, ...rest } = options;
|
|
102
|
+
let after;
|
|
103
|
+
let seen = 0;
|
|
104
|
+
do {
|
|
105
|
+
const page = await this.query(kind, { limit: 1000, ...rest, ...(after ? { after } : {}) });
|
|
106
|
+
for (const row of page.rows) {
|
|
107
|
+
if (seen++ >= maxRows)
|
|
108
|
+
return;
|
|
109
|
+
yield row;
|
|
110
|
+
}
|
|
111
|
+
after = page.next ?? undefined;
|
|
112
|
+
} while (after);
|
|
113
|
+
}
|
|
114
|
+
/** One row by id, or null when there is none this token can read. */
|
|
115
|
+
async get(kind, id, options = {}) {
|
|
116
|
+
try {
|
|
117
|
+
const qs = options.tz ? `?tz=${encodeURIComponent(options.tz)}` : '';
|
|
118
|
+
const r = await this.call('GET', `/api/my-crm/v1/rows/${encodeURIComponent(kind)}/${encodeURIComponent(id)}${qs}`);
|
|
119
|
+
return r.row;
|
|
120
|
+
}
|
|
121
|
+
catch (err) {
|
|
122
|
+
if (err instanceof AwesomateError && err.code === 'not_found')
|
|
123
|
+
return null;
|
|
124
|
+
throw err;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
/** Grouped numbers (counts, sums) in the Business Data API's shape. */
|
|
128
|
+
aggregate(request) {
|
|
129
|
+
return this.call('POST', '/api/my-crm/v1/data/query', request);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
export function createClient(options) {
|
|
133
|
+
return new AwesomateClient(options);
|
|
134
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@awesomate/sdk",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Read your own Awesomate data from Node: query and page through your contact list with types generated from your fields",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"engines": {
|
|
11
|
+
"node": ">=18"
|
|
12
|
+
},
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"bin": {
|
|
21
|
+
"awesomate-sdk": "dist/cli.js"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist",
|
|
25
|
+
"README.md",
|
|
26
|
+
"LICENSE"
|
|
27
|
+
],
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "tsc -p tsconfig.json",
|
|
30
|
+
"typecheck": "tsc -p tsconfig.json --noEmit && tsc -p test/typing/tsconfig.json",
|
|
31
|
+
"test": "npm run build && node --test test/*.test.mjs && tsc -p test/typing/tsconfig.json",
|
|
32
|
+
"prepublishOnly": "npm run typecheck && npm test"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@types/node": "^20.17.0",
|
|
36
|
+
"typescript": "^5.7.2"
|
|
37
|
+
}
|
|
38
|
+
}
|