@edge-base/admin 0.1.1
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 +213 -0
- package/dist/admin-auth.d.ts +65 -0
- package/dist/admin-auth.d.ts.map +1 -0
- package/dist/admin-auth.js +67 -0
- package/dist/admin-auth.js.map +1 -0
- package/dist/analytics.d.ts +200 -0
- package/dist/analytics.d.ts.map +1 -0
- package/dist/analytics.js +134 -0
- package/dist/analytics.js.map +1 -0
- package/dist/client.d.ts +96 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +153 -0
- package/dist/client.js.map +1 -0
- package/dist/d1.d.ts +28 -0
- package/dist/d1.d.ts.map +1 -0
- package/dist/d1.js +33 -0
- package/dist/d1.js.map +1 -0
- package/dist/generated/admin-api-core.d.ts +309 -0
- package/dist/generated/admin-api-core.d.ts.map +1 -0
- package/dist/generated/admin-api-core.js +303 -0
- package/dist/generated/admin-api-core.js.map +1 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/kv.d.ts +32 -0
- package/dist/kv.d.ts.map +1 -0
- package/dist/kv.js +42 -0
- package/dist/kv.js.map +1 -0
- package/dist/push.d.ts +73 -0
- package/dist/push.d.ts.map +1 -0
- package/dist/push.js +44 -0
- package/dist/push.js.map +1 -0
- package/dist/vectorize.d.ts +75 -0
- package/dist/vectorize.d.ts.map +1 -0
- package/dist/vectorize.js +70 -0
- package/dist/vectorize.js.map +1 -0
- package/llms.txt +118 -0
- package/package.json +52 -0
package/README.md
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
<h1 align="center">@edge-base/admin</h1>
|
|
2
|
+
|
|
3
|
+
<p align="center">
|
|
4
|
+
<b>Trusted server-side SDK for EdgeBase</b><br>
|
|
5
|
+
Database admin, user management, SQL, analytics, push, storage, and function calls from secure environments
|
|
6
|
+
</p>
|
|
7
|
+
|
|
8
|
+
<p align="center">
|
|
9
|
+
<a href="https://www.npmjs.com/package/@edge-base/admin"><img src="https://img.shields.io/npm/v/%40edge-base%2Fadmin?color=brightgreen" alt="npm"></a>
|
|
10
|
+
<a href="https://edgebase.fun/docs/database/admin-sdk"><img src="https://img.shields.io/badge/docs-admin_sdk-blue" alt="Docs"></a>
|
|
11
|
+
<a href="https://github.com/edge-base/edgebase/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="MIT License"></a>
|
|
12
|
+
</p>
|
|
13
|
+
|
|
14
|
+
<p align="center">
|
|
15
|
+
Node.js · Edge runtimes · Server actions · Background jobs · Trusted workers
|
|
16
|
+
</p>
|
|
17
|
+
|
|
18
|
+
<p align="center">
|
|
19
|
+
<a href="https://edgebase.fun/docs/database/admin-sdk"><b>Database Admin SDK</b></a> ·
|
|
20
|
+
<a href="https://edgebase.fun/docs/authentication/admin-users"><b>Admin Users</b></a> ·
|
|
21
|
+
<a href="https://edgebase.fun/docs/push/admin-sdk"><b>Push Admin SDK</b></a> ·
|
|
22
|
+
<a href="https://edgebase.fun/docs/analytics/admin-sdk"><b>Analytics Admin SDK</b></a>
|
|
23
|
+
</p>
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
`@edge-base/admin` is the trusted server-side SDK for EdgeBase.
|
|
28
|
+
|
|
29
|
+
Use it when you need:
|
|
30
|
+
|
|
31
|
+
- Service Key authenticated access
|
|
32
|
+
- server-side database operations that bypass access rules
|
|
33
|
+
- admin user management
|
|
34
|
+
- raw SQL
|
|
35
|
+
- push and analytics from secure environments
|
|
36
|
+
- server-to-server function calls
|
|
37
|
+
|
|
38
|
+
If code runs in a browser, use [`@edge-base/web`](https://www.npmjs.com/package/@edge-base/web) instead. If code runs on the server but should act as the current signed-in user through cookies, use [`@edge-base/ssr`](https://www.npmjs.com/package/@edge-base/ssr).
|
|
39
|
+
|
|
40
|
+
> Beta: the package is already usable, but some APIs may still evolve before general availability.
|
|
41
|
+
|
|
42
|
+
## Documentation Map
|
|
43
|
+
|
|
44
|
+
- [Database Admin SDK](https://edgebase.fun/docs/database/admin-sdk)
|
|
45
|
+
Server-side database access with a Service Key
|
|
46
|
+
- [Admin Users](https://edgebase.fun/docs/authentication/admin-users)
|
|
47
|
+
Create, update, list, revoke, and manage users
|
|
48
|
+
- [Push Admin SDK](https://edgebase.fun/docs/push/admin-sdk)
|
|
49
|
+
Send push notifications from secure environments
|
|
50
|
+
- [Analytics Admin SDK](https://edgebase.fun/docs/analytics/admin-sdk)
|
|
51
|
+
Query request analytics and track server-side events
|
|
52
|
+
- [Admin SDK Reference](https://edgebase.fun/docs/admin-sdk/reference)
|
|
53
|
+
Public admin SDK surface by category
|
|
54
|
+
|
|
55
|
+
## For AI Coding Assistants
|
|
56
|
+
|
|
57
|
+
This package ships with an `llms.txt` file for AI-assisted server SDK usage.
|
|
58
|
+
|
|
59
|
+
You can find it:
|
|
60
|
+
|
|
61
|
+
- after install: `node_modules/@edge-base/admin/llms.txt`
|
|
62
|
+
- in the repository: [llms.txt](https://github.com/edge-base/edgebase/blob/main/packages/sdk/js/packages/admin/llms.txt)
|
|
63
|
+
|
|
64
|
+
Use it when you want an agent to:
|
|
65
|
+
|
|
66
|
+
- keep Service Key logic on the server
|
|
67
|
+
- choose between `web`, `ssr`, and `admin`
|
|
68
|
+
- use the correct `db(namespace, id?)` and `sql(...)` signatures
|
|
69
|
+
- avoid shipping privileged SDK code to the browser
|
|
70
|
+
|
|
71
|
+
## Installation
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
npm install @edge-base/admin
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Quick Start
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
import { createAdminClient } from '@edge-base/admin';
|
|
81
|
+
|
|
82
|
+
const admin = createAdminClient(process.env.EDGEBASE_URL!, {
|
|
83
|
+
serviceKey: process.env.EDGEBASE_SERVICE_KEY,
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
const { items: posts } = await admin
|
|
87
|
+
.db('app')
|
|
88
|
+
.table('posts')
|
|
89
|
+
.orderBy('createdAt', 'desc')
|
|
90
|
+
.limit(20)
|
|
91
|
+
.getList();
|
|
92
|
+
|
|
93
|
+
console.log(posts);
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Inside EdgeBase App Functions, you can also rely on environment detection:
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
import { createAdminClient } from '@edge-base/admin';
|
|
100
|
+
|
|
101
|
+
const admin = createAdminClient();
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Core API
|
|
105
|
+
|
|
106
|
+
Once you create an admin client, these are the main surfaces you will use:
|
|
107
|
+
|
|
108
|
+
- `admin.db(namespace, id?)`
|
|
109
|
+
Server-side database access
|
|
110
|
+
- `admin.auth`
|
|
111
|
+
Admin user management
|
|
112
|
+
- `admin.sql(...)`
|
|
113
|
+
Raw SQL execution
|
|
114
|
+
- `admin.storage`
|
|
115
|
+
Server-side storage access
|
|
116
|
+
- `admin.functions`
|
|
117
|
+
Call app functions from trusted code
|
|
118
|
+
- `admin.push`
|
|
119
|
+
Send push notifications
|
|
120
|
+
- `admin.analytics`
|
|
121
|
+
Query analytics and track server-side events
|
|
122
|
+
- `admin.kv(namespace)`, `admin.d1(database)`, `admin.vector(index)`
|
|
123
|
+
Access platform resources from trusted code
|
|
124
|
+
|
|
125
|
+
## Database Access
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
type Post = {
|
|
129
|
+
id: string;
|
|
130
|
+
title: string;
|
|
131
|
+
status: 'draft' | 'published';
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const posts = admin.db('app').table<Post>('posts');
|
|
135
|
+
|
|
136
|
+
const result = await posts
|
|
137
|
+
.where('status', '==', 'published')
|
|
138
|
+
.orderBy('title', 'asc')
|
|
139
|
+
.limit(50)
|
|
140
|
+
.getList();
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
For instance databases, pass the instance id positionally:
|
|
144
|
+
|
|
145
|
+
```ts
|
|
146
|
+
admin.db('workspace', 'ws-123');
|
|
147
|
+
admin.db('user', 'user-123');
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
Read more: [Database Admin SDK](https://edgebase.fun/docs/database/admin-sdk)
|
|
151
|
+
|
|
152
|
+
## Admin Auth
|
|
153
|
+
|
|
154
|
+
```ts
|
|
155
|
+
const created = await admin.auth.createUser({
|
|
156
|
+
email: 'june@example.com',
|
|
157
|
+
password: 'secure-pass-123',
|
|
158
|
+
displayName: 'June',
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
await admin.auth.setCustomClaims(created.id, {
|
|
162
|
+
role: 'moderator',
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
const users = await admin.auth.listUsers({ limit: 20 });
|
|
166
|
+
console.log(users.users);
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
Read more: [Admin Users](https://edgebase.fun/docs/authentication/admin-users)
|
|
170
|
+
|
|
171
|
+
## Raw SQL
|
|
172
|
+
|
|
173
|
+
`sql()` supports two forms:
|
|
174
|
+
|
|
175
|
+
```ts
|
|
176
|
+
const health = await admin.sql('select 1 as ok');
|
|
177
|
+
|
|
178
|
+
const docs = await admin.sql(
|
|
179
|
+
'workspace',
|
|
180
|
+
'ws-123',
|
|
181
|
+
'select * from documents where status = ?',
|
|
182
|
+
['published'],
|
|
183
|
+
);
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## Push And Analytics
|
|
187
|
+
|
|
188
|
+
```ts
|
|
189
|
+
await admin.push.send('user-123', {
|
|
190
|
+
title: 'Deployment finished',
|
|
191
|
+
body: 'Your content is live.',
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
const overview = await admin.analytics.overview({ range: '7d' });
|
|
195
|
+
console.log(overview.summary.totalRequests);
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
Read more:
|
|
199
|
+
|
|
200
|
+
- [Push Admin SDK](https://edgebase.fun/docs/push/admin-sdk)
|
|
201
|
+
- [Analytics Admin SDK](https://edgebase.fun/docs/analytics/admin-sdk)
|
|
202
|
+
|
|
203
|
+
## Choose The Right Package
|
|
204
|
+
|
|
205
|
+
| Package | Use it for |
|
|
206
|
+
| --- | --- |
|
|
207
|
+
| `@edge-base/web` | Browser and untrusted client code |
|
|
208
|
+
| `@edge-base/ssr` | Server-side code acting as the current cookie-authenticated user |
|
|
209
|
+
| `@edge-base/admin` | Trusted server-side code with Service Key access |
|
|
210
|
+
|
|
211
|
+
## License
|
|
212
|
+
|
|
213
|
+
MIT
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Admin Auth client for server-side user management
|
|
3
|
+
*: admin.auth context — Service Key required
|
|
4
|
+
*: Service Key
|
|
5
|
+
*/
|
|
6
|
+
import type { HttpClient } from '@edge-base/core';
|
|
7
|
+
export interface UserRecord {
|
|
8
|
+
id: string;
|
|
9
|
+
email?: string;
|
|
10
|
+
displayName?: string;
|
|
11
|
+
avatarUrl?: string;
|
|
12
|
+
role?: string;
|
|
13
|
+
emailVisibility?: string;
|
|
14
|
+
isAnonymous?: boolean;
|
|
15
|
+
customClaims?: Record<string, unknown>;
|
|
16
|
+
createdAt: string;
|
|
17
|
+
updatedAt: string;
|
|
18
|
+
}
|
|
19
|
+
export interface ListUsersResult {
|
|
20
|
+
users: UserRecord[];
|
|
21
|
+
cursor?: string;
|
|
22
|
+
}
|
|
23
|
+
export interface CreateUserOptions {
|
|
24
|
+
email: string;
|
|
25
|
+
password: string;
|
|
26
|
+
displayName?: string;
|
|
27
|
+
role?: string;
|
|
28
|
+
/** Preferred locale for this user (e.g. 'ko', 'ja'). Default: 'en' */
|
|
29
|
+
locale?: string;
|
|
30
|
+
}
|
|
31
|
+
export interface UpdateUserOptions {
|
|
32
|
+
email?: string;
|
|
33
|
+
displayName?: string;
|
|
34
|
+
avatarUrl?: string;
|
|
35
|
+
role?: string;
|
|
36
|
+
emailVisibility?: string;
|
|
37
|
+
/** Preferred locale for this user (e.g. 'ko', 'ja'). */
|
|
38
|
+
locale?: string;
|
|
39
|
+
}
|
|
40
|
+
export declare class AdminAuthClient {
|
|
41
|
+
private client;
|
|
42
|
+
private hasServiceKey;
|
|
43
|
+
private adminCore;
|
|
44
|
+
constructor(client: HttpClient, hasServiceKey: boolean);
|
|
45
|
+
/** Ensure Service Key is configured */
|
|
46
|
+
private requireServiceKey;
|
|
47
|
+
/** Get a user by ID */
|
|
48
|
+
getUser(userId: string): Promise<UserRecord>;
|
|
49
|
+
/** List users with pagination */
|
|
50
|
+
listUsers(options?: {
|
|
51
|
+
limit?: number;
|
|
52
|
+
cursor?: string;
|
|
53
|
+
}): Promise<ListUsersResult>;
|
|
54
|
+
/** Create a new user (server-side registration) */
|
|
55
|
+
createUser(data: CreateUserOptions): Promise<UserRecord>;
|
|
56
|
+
/** Update a user */
|
|
57
|
+
updateUser(userId: string, data: UpdateUserOptions): Promise<UserRecord>;
|
|
58
|
+
/** Delete a user */
|
|
59
|
+
deleteUser(userId: string): Promise<void>;
|
|
60
|
+
/** Set custom claims for a user (reflected in JWT on next token refresh) */
|
|
61
|
+
setCustomClaims(userId: string, claims: Record<string, unknown>): Promise<void>;
|
|
62
|
+
/** Revoke all sessions for a user (force re-authentication) */
|
|
63
|
+
revokeAllSessions(userId: string): Promise<void>;
|
|
64
|
+
}
|
|
65
|
+
//# sourceMappingURL=admin-auth.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"admin-auth.d.ts","sourceRoot":"","sources":["../src/admin-auth.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAIlD,MAAM,WAAW,UAAU;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,SAAS,CAAC,EA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AC;KACjB,GAAG,OAAO,CAAC,eAAe,CAAC;IAQ5B,mDAAmD;IAC7C,UAAU,CAAC,IAAI,EAAE,iBAAiB,GAAG,OAAO,CAAC,UAAU,CAAC;IAM9D,oBAAoB;IACd,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,iBAAiB,GAAG,OAAO,CAAC,UAAU,CAAC;IAM9E,oBAAoB;IACd,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAK/C,4EAA4E;IACtE,eAAe,CACnB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC9B,OAAO,CAAC,IAAI,CAAC;IAKhB,+DAA+D;IACzD,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAIvD"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Admin Auth client for server-side user management
|
|
3
|
+
*: admin.auth context — Service Key required
|
|
4
|
+
*: Service Key
|
|
5
|
+
*/
|
|
6
|
+
import { EdgeBaseError, HttpClientAdapter } from '@edge-base/core';
|
|
7
|
+
import { DefaultAdminApi } from './generated/admin-api-core.js';
|
|
8
|
+
export class AdminAuthClient {
|
|
9
|
+
client;
|
|
10
|
+
hasServiceKey;
|
|
11
|
+
adminCore;
|
|
12
|
+
constructor(client, hasServiceKey) {
|
|
13
|
+
this.client = client;
|
|
14
|
+
this.hasServiceKey = hasServiceKey;
|
|
15
|
+
this.adminCore = new DefaultAdminApi(new HttpClientAdapter(client));
|
|
16
|
+
}
|
|
17
|
+
/** Ensure Service Key is configured */
|
|
18
|
+
requireServiceKey() {
|
|
19
|
+
if (!this.hasServiceKey) {
|
|
20
|
+
throw new EdgeBaseError(403, 'admin.auth requires serviceKey. Initialize EdgeBase with { serviceKey: "..." } option.');
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/** Get a user by ID */
|
|
24
|
+
async getUser(userId) {
|
|
25
|
+
this.requireServiceKey();
|
|
26
|
+
const result = await this.adminCore.adminAuthGetUser(userId);
|
|
27
|
+
return result.user ?? result;
|
|
28
|
+
}
|
|
29
|
+
/** List users with pagination */
|
|
30
|
+
async listUsers(options) {
|
|
31
|
+
this.requireServiceKey();
|
|
32
|
+
const query = {};
|
|
33
|
+
if (options?.limit)
|
|
34
|
+
query.limit = String(options.limit);
|
|
35
|
+
if (options?.cursor)
|
|
36
|
+
query.cursor = options.cursor;
|
|
37
|
+
return this.adminCore.adminAuthListUsers(query);
|
|
38
|
+
}
|
|
39
|
+
/** Create a new user (server-side registration) */
|
|
40
|
+
async createUser(data) {
|
|
41
|
+
this.requireServiceKey();
|
|
42
|
+
const result = await this.adminCore.adminAuthCreateUser(data);
|
|
43
|
+
return result.user ?? result;
|
|
44
|
+
}
|
|
45
|
+
/** Update a user */
|
|
46
|
+
async updateUser(userId, data) {
|
|
47
|
+
this.requireServiceKey();
|
|
48
|
+
const result = await this.adminCore.adminAuthUpdateUser(userId, data);
|
|
49
|
+
return result.user ?? result;
|
|
50
|
+
}
|
|
51
|
+
/** Delete a user */
|
|
52
|
+
async deleteUser(userId) {
|
|
53
|
+
this.requireServiceKey();
|
|
54
|
+
await this.adminCore.adminAuthDeleteUser(userId);
|
|
55
|
+
}
|
|
56
|
+
/** Set custom claims for a user (reflected in JWT on next token refresh) */
|
|
57
|
+
async setCustomClaims(userId, claims) {
|
|
58
|
+
this.requireServiceKey();
|
|
59
|
+
await this.adminCore.adminAuthSetClaims(userId, claims);
|
|
60
|
+
}
|
|
61
|
+
/** Revoke all sessions for a user (force re-authentication) */
|
|
62
|
+
async revokeAllSessions(userId) {
|
|
63
|
+
this.requireServiceKey();
|
|
64
|
+
await this.adminCore.adminAuthRevokeUserSessions(userId);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=admin-auth.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":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,KAAK,CAA6B,CAAC;IAC9E,CAAC;IAED,mDAAmD;IACnD,KAAK,CAAC,UAAU,CAAC,IAAuB;QACtC,IAAI,CAAC,iBAAiB,EAAE,CAAC;QACzB,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,mBAAmB,CAAC,IAAI,CAAyB,CAAC;QACtF,OAAQ,MAAc,CAAC,IAAI,IAAI,MAAM,CAAC;IACxC,CAAC;IAED,oBAAoB;IACpB,KAAK,CAAC,UAAU,CAAC,MAAc,EAAE,IAAuB;QACtD,IAAI,CAAC,iBAAiB,EAAE,CAAC;QACzB,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,mBAAmB,CAAC,MAAM,EAAE,IAAI,CAAyB,CAAC;QAC9F,OAAQ,MAAc,CAAC,IAAI,IAAI,MAAM,CAAC;IACxC,CAAC;IAED,oBAAoB;IACpB,KAAK,CAAC,UAAU,CAAC,MAAc;QAC7B,IAAI,CAAC,iBAAiB,EAAE,CAAC;QACzB,MAAM,IAAI,CAAC,SAAS,CAAC,mBAAmB,CAAC,MAAM,CAAC,CAAC;IACnD,CAAC;IAED,4EAA4E;IAC5E,KAAK,CAAC,eAAe,CACnB,MAAc,EACd,MAA+B;QAE/B,IAAI,CAAC,iBAAiB,EAAE,CAAC;QACzB,MAAM,IAAI,CAAC,SAAS,CAAC,kBAAkB,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC1D,CAAC;IAED,+DAA+D;IAC/D,KAAK,CAAC,iBAAiB,CAAC,MAAc;QACpC,IAAI,CAAC,iBAAiB,EAAE,CAAC;QACzB,MAAM,IAAI,CAAC,SAAS,CAAC,2BAA2B,CAAC,MAAM,CAAC,CAAC;IAC3D,CAAC;CACF"}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AnalyticsClient — Analytics query + custom event tracking for Admin/Server SDK
|
|
3
|
+
*
|
|
4
|
+
* Feature 1: Request log metrics query (same data as admin dashboard)
|
|
5
|
+
* Feature 2: Custom event tracking (server-side) + query
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* const admin = createAdminClient(url, { serviceKey });
|
|
9
|
+
*
|
|
10
|
+
* // Request log metrics
|
|
11
|
+
* const overview = await admin.analytics.overview({ range: '7d' });
|
|
12
|
+
* const ts = await admin.analytics.timeSeries({ range: '24h', category: 'db' });
|
|
13
|
+
*
|
|
14
|
+
* // Custom events
|
|
15
|
+
* await admin.analytics.track('user_upgraded', { plan: 'pro', amount: 29.99 }, 'user-123');
|
|
16
|
+
* const events = await admin.analytics.queryEvents({ event: 'user_upgraded', metric: 'count' });
|
|
17
|
+
*/
|
|
18
|
+
import type { HttpClient } from '@edge-base/core';
|
|
19
|
+
/** Options for querying request log metrics. */
|
|
20
|
+
export interface AnalyticsQueryOptions {
|
|
21
|
+
/** Time range for the query. */
|
|
22
|
+
range?: '1h' | '6h' | '24h' | '7d' | '30d' | '90d';
|
|
23
|
+
/** Filter by route category (e.g. 'db', 'auth', 'storage'). */
|
|
24
|
+
category?: string;
|
|
25
|
+
/** Grouping interval for time series data. */
|
|
26
|
+
groupBy?: 'minute' | 'hour' | 'day';
|
|
27
|
+
}
|
|
28
|
+
/** A single point in a time series. */
|
|
29
|
+
export interface TimeSeriesPoint {
|
|
30
|
+
timestamp: number;
|
|
31
|
+
requests: number;
|
|
32
|
+
errors: number;
|
|
33
|
+
avgLatency: number;
|
|
34
|
+
uniqueUsers: number;
|
|
35
|
+
}
|
|
36
|
+
/** Aggregate summary of request metrics. */
|
|
37
|
+
export interface AnalyticsSummary {
|
|
38
|
+
totalRequests: number;
|
|
39
|
+
totalErrors: number;
|
|
40
|
+
avgLatency: number;
|
|
41
|
+
uniqueUsers: number;
|
|
42
|
+
}
|
|
43
|
+
/** Breakdown item (e.g. by category or status code). */
|
|
44
|
+
export interface BreakdownItem {
|
|
45
|
+
label: string;
|
|
46
|
+
count: number;
|
|
47
|
+
percentage: number;
|
|
48
|
+
avgLatency?: number;
|
|
49
|
+
errorRate?: number;
|
|
50
|
+
}
|
|
51
|
+
/** Top endpoint/item entry. */
|
|
52
|
+
export interface TopItem {
|
|
53
|
+
label: string;
|
|
54
|
+
count: number;
|
|
55
|
+
avgLatency: number;
|
|
56
|
+
errorRate: number;
|
|
57
|
+
}
|
|
58
|
+
/** Full overview response from the analytics query endpoint. */
|
|
59
|
+
export interface AnalyticsOverview {
|
|
60
|
+
timeSeries: TimeSeriesPoint[];
|
|
61
|
+
summary: AnalyticsSummary;
|
|
62
|
+
breakdown: BreakdownItem[];
|
|
63
|
+
topItems: TopItem[];
|
|
64
|
+
}
|
|
65
|
+
/** Data for tracking a single custom event. */
|
|
66
|
+
export interface TrackEventData {
|
|
67
|
+
/** Event name (required). */
|
|
68
|
+
name: string;
|
|
69
|
+
/** Arbitrary properties (max 50 keys, max 4KB JSON). */
|
|
70
|
+
properties?: Record<string, string | number | boolean>;
|
|
71
|
+
/** Unix timestamp in ms (default: now). */
|
|
72
|
+
timestamp?: number;
|
|
73
|
+
/** User ID override (Service Key only — ignored with JWT). */
|
|
74
|
+
userId?: string;
|
|
75
|
+
}
|
|
76
|
+
/** Options for querying custom events. */
|
|
77
|
+
export interface EventQueryOptions {
|
|
78
|
+
/** Time range. */
|
|
79
|
+
range?: '1h' | '6h' | '24h' | '7d' | '30d' | '90d';
|
|
80
|
+
/** Filter by event name. */
|
|
81
|
+
event?: string;
|
|
82
|
+
/** Filter by user ID. */
|
|
83
|
+
userId?: string;
|
|
84
|
+
/** Query metric type. */
|
|
85
|
+
metric?: 'list' | 'count' | 'timeSeries' | 'topEvents';
|
|
86
|
+
/** Grouping interval for time series. */
|
|
87
|
+
groupBy?: 'minute' | 'hour' | 'day';
|
|
88
|
+
/** Max results (for list metric). */
|
|
89
|
+
limit?: number;
|
|
90
|
+
/** Cursor for pagination (for list metric). */
|
|
91
|
+
cursor?: string;
|
|
92
|
+
}
|
|
93
|
+
/** A single custom event record. */
|
|
94
|
+
export interface EventItem {
|
|
95
|
+
id: number;
|
|
96
|
+
timestamp: number;
|
|
97
|
+
userId: string | null;
|
|
98
|
+
eventName: string;
|
|
99
|
+
properties: Record<string, unknown> | null;
|
|
100
|
+
}
|
|
101
|
+
/** Result for metric='list'. */
|
|
102
|
+
export interface EventListResult {
|
|
103
|
+
events: EventItem[];
|
|
104
|
+
cursor?: string;
|
|
105
|
+
hasMore: boolean;
|
|
106
|
+
}
|
|
107
|
+
/** Result for metric='count'. */
|
|
108
|
+
export interface EventCountResult {
|
|
109
|
+
totalEvents: number;
|
|
110
|
+
uniqueUsers: number;
|
|
111
|
+
}
|
|
112
|
+
/** Result for metric='timeSeries'. */
|
|
113
|
+
export interface EventTimeSeriesResult {
|
|
114
|
+
timeSeries: Array<{
|
|
115
|
+
timestamp: number;
|
|
116
|
+
count: number;
|
|
117
|
+
}>;
|
|
118
|
+
}
|
|
119
|
+
/** Result for metric='topEvents'. */
|
|
120
|
+
export interface EventTopResult {
|
|
121
|
+
topEvents: Array<{
|
|
122
|
+
eventName: string;
|
|
123
|
+
count: number;
|
|
124
|
+
uniqueUsers: number;
|
|
125
|
+
}>;
|
|
126
|
+
}
|
|
127
|
+
export declare class AnalyticsClient {
|
|
128
|
+
private core;
|
|
129
|
+
private adminCore;
|
|
130
|
+
constructor(httpClient: HttpClient);
|
|
131
|
+
/**
|
|
132
|
+
* Get a full analytics overview (time series + summary + breakdown + top endpoints).
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* const overview = await admin.analytics.overview({ range: '7d' });
|
|
136
|
+
* console.log(overview.summary.totalRequests);
|
|
137
|
+
*/
|
|
138
|
+
overview(options?: AnalyticsQueryOptions): Promise<AnalyticsOverview>;
|
|
139
|
+
/**
|
|
140
|
+
* Get time series data only.
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* const ts = await admin.analytics.timeSeries({ range: '24h', category: 'db' });
|
|
144
|
+
*/
|
|
145
|
+
timeSeries(options?: AnalyticsQueryOptions): Promise<TimeSeriesPoint[]>;
|
|
146
|
+
/**
|
|
147
|
+
* Get breakdown data (by category, status code, etc.).
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* const breakdown = await admin.analytics.breakdown({ range: '30d' });
|
|
151
|
+
*/
|
|
152
|
+
breakdown(options?: AnalyticsQueryOptions): Promise<BreakdownItem[]>;
|
|
153
|
+
/**
|
|
154
|
+
* Get top endpoints by request count.
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* const top = await admin.analytics.topEndpoints({ range: '7d', category: 'auth' });
|
|
158
|
+
*/
|
|
159
|
+
topEndpoints(options?: AnalyticsQueryOptions): Promise<TopItem[]>;
|
|
160
|
+
/**
|
|
161
|
+
* Track a single custom event.
|
|
162
|
+
*
|
|
163
|
+
* @param name Event name (e.g. 'user_upgraded')
|
|
164
|
+
* @param properties Arbitrary key-value data (max 50 keys, max 4KB)
|
|
165
|
+
* @param userId User ID to associate (Service Key callers only)
|
|
166
|
+
*
|
|
167
|
+
* @example
|
|
168
|
+
* await admin.analytics.track('user_upgraded', { plan: 'pro', amount: 29.99 }, 'user-123');
|
|
169
|
+
*/
|
|
170
|
+
track(name: string, properties?: Record<string, string | number | boolean>, userId?: string): Promise<void>;
|
|
171
|
+
/**
|
|
172
|
+
* Track multiple custom events in a single request (max 100).
|
|
173
|
+
*
|
|
174
|
+
* @example
|
|
175
|
+
* await admin.analytics.trackBatch([
|
|
176
|
+
* { name: 'page_view', properties: { path: '/pricing' } },
|
|
177
|
+
* { name: 'page_view', properties: { path: '/docs' } },
|
|
178
|
+
* ]);
|
|
179
|
+
*/
|
|
180
|
+
trackBatch(events: TrackEventData[]): Promise<void>;
|
|
181
|
+
/**
|
|
182
|
+
* Query custom events. Returns different shapes based on `metric`.
|
|
183
|
+
*
|
|
184
|
+
* @example
|
|
185
|
+
* // List events
|
|
186
|
+
* const list = await admin.analytics.queryEvents({ event: 'purchase', metric: 'list', limit: 20 });
|
|
187
|
+
*
|
|
188
|
+
* // Count
|
|
189
|
+
* const count = await admin.analytics.queryEvents({ event: 'purchase', metric: 'count' });
|
|
190
|
+
*
|
|
191
|
+
* // Time series
|
|
192
|
+
* const ts = await admin.analytics.queryEvents({ metric: 'timeSeries', groupBy: 'day' });
|
|
193
|
+
*
|
|
194
|
+
* // Top events
|
|
195
|
+
* const top = await admin.analytics.queryEvents({ metric: 'topEvents' });
|
|
196
|
+
*/
|
|
197
|
+
queryEvents<T = EventListResult | EventCountResult | EventTimeSeriesResult | EventTopResult>(options?: EventQueryOptions): Promise<T>;
|
|
198
|
+
private buildQuery;
|
|
199
|
+
}
|
|
200
|
+
//# sourceMappingURL=analytics.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"analytics.d.ts","sourceRoot":"","sources":["../src/analytics.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAMlD,gDAAgD;AAChD,MAAM,WAAW,qBAAqB;IACpC,gCAAgC;IAChC,KAAK,CAAC,EAAE,IAAI,GAAG,IAAI,GAAG,KAAK,GAAG,IAAI,GAAG,KAAK,GAAG,KAAK,CAAC;IACnD,+DAA+D;IAC/D,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,8CAA8C;IAC9C,OAAO,CAAC,EAAE,QAAQ,GAAG,MAAM,GAAG,KAAK,CAAC;CACrC;AAED,uCAAuC;AACvC,MAAM,WAAW,eAAe;IAC9B,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,4CAA4C;AAC5C,MAAM,WAAW,gBAAgB;IAC/B,aAAa,EAAE,MAAM,CAAC;IACtB,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,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