@memoryengine/client 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/dist/index.d.ts +71 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +213 -0
- package/dist/index.js.map +1 -0
- package/dist/schemas/auth.d.ts +69 -0
- package/dist/schemas/auth.d.ts.map +1 -0
- package/dist/schemas/auth.js +55 -0
- package/dist/schemas/auth.js.map +1 -0
- package/dist/schemas/engram.d.ts +141 -0
- package/dist/schemas/engram.d.ts.map +1 -0
- package/dist/schemas/engram.js +140 -0
- package/dist/schemas/engram.js.map +1 -0
- package/dist/schemas/fields.d.ts +18 -0
- package/dist/schemas/fields.d.ts.map +1 -0
- package/dist/schemas/fields.js +28 -0
- package/dist/schemas/fields.js.map +1 -0
- package/dist/schemas/index.d.ts +9 -0
- package/dist/schemas/index.d.ts.map +1 -0
- package/dist/schemas/index.js +14 -0
- package/dist/schemas/index.js.map +1 -0
- package/dist/schemas/principal.d.ts +150 -0
- package/dist/schemas/principal.d.ts.map +1 -0
- package/dist/schemas/principal.js +119 -0
- package/dist/schemas/principal.js.map +1 -0
- package/dist/schemas/rpc.d.ts +1312 -0
- package/dist/schemas/rpc.d.ts.map +1 -0
- package/dist/schemas/rpc.js +137 -0
- package/dist/schemas/rpc.js.map +1 -0
- package/package.json +38 -0
- package/src/index.ts +415 -0
- package/src/schemas/auth.ts +79 -0
- package/src/schemas/engram.test.ts +59 -0
- package/src/schemas/engram.ts +194 -0
- package/src/schemas/fields.test.ts +38 -0
- package/src/schemas/fields.ts +35 -0
- package/src/schemas/index.ts +15 -0
- package/src/schemas/principal.ts +178 -0
- package/src/schemas/rpc.ts +291 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rpc.d.ts","sourceRoot":"","sources":["../../src/schemas/rpc.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAA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|
+
/**
|
|
2
|
+
* RPC Contract - maps method names to input/output types
|
|
3
|
+
*
|
|
4
|
+
* This is the key to type inference: define the contract once,
|
|
5
|
+
* and both server validation and client types are derived from it.
|
|
6
|
+
*/
|
|
7
|
+
import { z } from "zod";
|
|
8
|
+
// Auth schemas
|
|
9
|
+
import { apiKeySchema, createApiKeyResultSchema, createApiKeySchema, loginResultSchema, loginSchema, principalSchema, revokeApiKeySchema, successSchema, whoamiSchema, } from "./auth";
|
|
10
|
+
// Engram schemas
|
|
11
|
+
import { batchCreateEngramSchema, batchCreateResultSchema, createEngramSchema, deleteEngramSchema, deleteResultSchema, engramSchema, getEngramSchema, mvEngramResultSchema, mvEngramSchema, searchEngramResultSchema, searchEngramSchema, updateEngramSchema, } from "./engram";
|
|
12
|
+
// Principal schemas
|
|
13
|
+
import { addRoleMemberSchema, checkTreeAccessSchema, createPrincipalSchema, deletePrincipalSchema, getPrincipalSchema, getTreeOwnerSchema, grantTreeAccessSchema, listRoleMembersSchema, listRolesForPrincipalSchema, listTreeGrantsSchema, listTreeOwnersSchema, principalDetailSchema, principalListItemSchema, principalRoleSchema, removeRoleMemberSchema, removeTreeOwnerSchema, revokeTreeAccessSchema, roleMemberSchema, setPasswordSchema, setTreeOwnerSchema, treeGrantSchema, treeOwnerSchema, } from "./principal";
|
|
14
|
+
// ===== RPC Method Definition =====
|
|
15
|
+
/**
|
|
16
|
+
* Define a method with its input schema and output type.
|
|
17
|
+
* The output type is specified as a generic since DB results
|
|
18
|
+
* aren't validated at runtime (trusted internal data).
|
|
19
|
+
*
|
|
20
|
+
* Uses z.input<> for input types to preserve optionality of fields
|
|
21
|
+
* with defaults (e.g., limit?: number with .default(100) stays optional
|
|
22
|
+
* for callers, but the server gets the default after parsing).
|
|
23
|
+
*/
|
|
24
|
+
function method(input, _output) {
|
|
25
|
+
return {
|
|
26
|
+
input,
|
|
27
|
+
_types: {},
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/** For methods with no input params */
|
|
31
|
+
const emptySchema = z.object({});
|
|
32
|
+
// ===== Auth Methods Contract =====
|
|
33
|
+
export const authMethods = {
|
|
34
|
+
"auth.login": method(loginSchema, {}),
|
|
35
|
+
"auth.whoami": method(emptySchema, {}),
|
|
36
|
+
"auth.createApiKey": method(createApiKeySchema, {}),
|
|
37
|
+
"auth.listApiKeys": method(emptySchema, {}),
|
|
38
|
+
"auth.revokeApiKey": method(revokeApiKeySchema, {}),
|
|
39
|
+
};
|
|
40
|
+
// ===== Principal Methods Contract =====
|
|
41
|
+
export const principalMethods = {
|
|
42
|
+
"principal.list": method(emptySchema, {}),
|
|
43
|
+
"principal.get": method(getPrincipalSchema, {}),
|
|
44
|
+
"principal.create": method(createPrincipalSchema, {}),
|
|
45
|
+
"principal.delete": method(deletePrincipalSchema, {}),
|
|
46
|
+
"principal.setPassword": method(setPasswordSchema, {}),
|
|
47
|
+
};
|
|
48
|
+
// ===== Grant Methods Contract =====
|
|
49
|
+
export const grantMethods = {
|
|
50
|
+
"grant.create": method(grantTreeAccessSchema, {}),
|
|
51
|
+
"grant.revoke": method(revokeTreeAccessSchema, {}),
|
|
52
|
+
"grant.list": method(listTreeGrantsSchema, {}),
|
|
53
|
+
"grant.check": method(checkTreeAccessSchema, {}),
|
|
54
|
+
};
|
|
55
|
+
// ===== Owner Methods Contract =====
|
|
56
|
+
export const ownerMethods = {
|
|
57
|
+
"owner.set": method(setTreeOwnerSchema, {}),
|
|
58
|
+
"owner.remove": method(removeTreeOwnerSchema, {}),
|
|
59
|
+
"owner.get": method(getTreeOwnerSchema, {}),
|
|
60
|
+
"owner.list": method(listTreeOwnersSchema, {}),
|
|
61
|
+
};
|
|
62
|
+
// ===== Role Methods Contract =====
|
|
63
|
+
export const roleMethods = {
|
|
64
|
+
"role.addMember": method(addRoleMemberSchema, {}),
|
|
65
|
+
"role.removeMember": method(removeRoleMemberSchema, {}),
|
|
66
|
+
"role.listMembers": method(listRoleMembersSchema, {}),
|
|
67
|
+
"role.listForPrincipal": method(listRolesForPrincipalSchema, {}),
|
|
68
|
+
};
|
|
69
|
+
// ===== Engram Methods Contract =====
|
|
70
|
+
export const engramMethods = {
|
|
71
|
+
"engram.create": method(createEngramSchema, {}),
|
|
72
|
+
"engram.batchCreate": method(batchCreateEngramSchema, {}),
|
|
73
|
+
"engram.get": method(getEngramSchema, {}),
|
|
74
|
+
"engram.update": method(updateEngramSchema, {}),
|
|
75
|
+
"engram.delete": method(deleteEngramSchema, {}),
|
|
76
|
+
"engram.search": method(searchEngramSchema, {}),
|
|
77
|
+
"engram.mv": method(mvEngramSchema, {}),
|
|
78
|
+
};
|
|
79
|
+
// ===== Combined RPC Contract =====
|
|
80
|
+
/** All registered RPC methods */
|
|
81
|
+
export const rpcMethods = {
|
|
82
|
+
...authMethods,
|
|
83
|
+
...principalMethods,
|
|
84
|
+
...grantMethods,
|
|
85
|
+
...ownerMethods,
|
|
86
|
+
...roleMethods,
|
|
87
|
+
...engramMethods,
|
|
88
|
+
};
|
|
89
|
+
/** Get the input schema for runtime validation */
|
|
90
|
+
export function getInputSchema(method) {
|
|
91
|
+
return rpcMethods[method].input;
|
|
92
|
+
}
|
|
93
|
+
// ===== Output Schema Map =====
|
|
94
|
+
const checkAccessOutputSchema = z.object({ allowed: z.boolean() });
|
|
95
|
+
/** Runtime output schemas for response validation */
|
|
96
|
+
const outputSchemas = {
|
|
97
|
+
// Auth
|
|
98
|
+
"auth.login": loginResultSchema,
|
|
99
|
+
"auth.whoami": whoamiSchema,
|
|
100
|
+
"auth.createApiKey": createApiKeyResultSchema,
|
|
101
|
+
"auth.listApiKeys": z.array(apiKeySchema),
|
|
102
|
+
"auth.revokeApiKey": successSchema,
|
|
103
|
+
// Principal
|
|
104
|
+
"principal.list": z.array(principalListItemSchema),
|
|
105
|
+
"principal.get": principalDetailSchema,
|
|
106
|
+
"principal.create": principalSchema,
|
|
107
|
+
"principal.delete": successSchema,
|
|
108
|
+
"principal.setPassword": successSchema,
|
|
109
|
+
// Grant
|
|
110
|
+
"grant.create": successSchema,
|
|
111
|
+
"grant.revoke": successSchema,
|
|
112
|
+
"grant.list": z.array(treeGrantSchema),
|
|
113
|
+
"grant.check": checkAccessOutputSchema,
|
|
114
|
+
// Owner
|
|
115
|
+
"owner.set": successSchema,
|
|
116
|
+
"owner.remove": successSchema,
|
|
117
|
+
"owner.get": treeOwnerSchema.nullable(),
|
|
118
|
+
"owner.list": z.array(treeOwnerSchema),
|
|
119
|
+
// Role
|
|
120
|
+
"role.addMember": successSchema,
|
|
121
|
+
"role.removeMember": successSchema,
|
|
122
|
+
"role.listMembers": z.array(roleMemberSchema),
|
|
123
|
+
"role.listForPrincipal": z.array(principalRoleSchema),
|
|
124
|
+
// Engram
|
|
125
|
+
"engram.create": engramSchema,
|
|
126
|
+
"engram.batchCreate": batchCreateResultSchema,
|
|
127
|
+
"engram.get": engramSchema,
|
|
128
|
+
"engram.update": engramSchema,
|
|
129
|
+
"engram.delete": deleteResultSchema,
|
|
130
|
+
"engram.search": searchEngramResultSchema,
|
|
131
|
+
"engram.mv": mvEngramResultSchema,
|
|
132
|
+
};
|
|
133
|
+
/** Get the output schema for runtime response validation */
|
|
134
|
+
export function getOutputSchema(method) {
|
|
135
|
+
return outputSchemas[method];
|
|
136
|
+
}
|
|
137
|
+
//# sourceMappingURL=rpc.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rpc.js","sourceRoot":"","sources":["../../src/schemas/rpc.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,eAAe;AACf,OAAO,EACL,YAAY,EACZ,wBAAwB,EACxB,kBAAkB,EAClB,iBAAiB,EACjB,WAAW,EACX,eAAe,EACf,kBAAkB,EAClB,aAAa,EACb,YAAY,GACb,MAAM,QAAQ,CAAC;AAChB,iBAAiB;AACjB,OAAO,EACL,uBAAuB,EACvB,uBAAuB,EACvB,kBAAkB,EAClB,kBAAkB,EAClB,kBA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AC,eAAe,CAAC;IACtC,aAAa,EAAE,uBAAuB;IACtC,QAAQ;IACR,WAAW,EAAE,aAAa;IAC1B,cAAc,EAAE,aAAa;IAC7B,WAAW,EAAE,eAAe,CAAC,QAAQ,EAAE;IACvC,YAAY,EAAE,CAAC,CAAC,KAAK,CAAC,eAAe,CAAC;IACtC,OAAO;IACP,gBAAgB,EAAE,aAAa;IAC/B,mBAAmB,EAAE,aAAa;IAClC,kBAAkB,EAAE,CAAC,CAAC,KAAK,CAAC,gBAAgB,CAAC;IAC7C,uBAAuB,EAAE,CAAC,CAAC,KAAK,CAAC,mBAAmB,CAAC;IACrD,SAAS;IACT,eAAe,EAAE,YAAY;IAC7B,oBAAoB,EAAE,uBAAuB;IAC7C,YAAY,EAAE,YAAY;IAC1B,eAAe,EAAE,YAAY;IAC7B,eAAe,EAAE,kBAAkB;IACnC,eAAe,EAAE,wBAAwB;IACzC,WAAW,EAAE,oBAAoB;CAClC,CAAC;AAEF,4DAA4D;AAC5D,MAAM,UAAU,eAAe,CAAC,MAAc;IAC5C,OAAO,aAAa,CAAC,MAAM,CAAC,CAAC;AAC/B,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@memoryengine/client",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Client library for memory engine",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"module": "src/index.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"bun": "./src/index.ts",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./schemas": {
|
|
14
|
+
"bun": "./src/schemas/index.ts",
|
|
15
|
+
"types": "./dist/schemas/index.d.ts",
|
|
16
|
+
"import": "./dist/schemas/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist",
|
|
21
|
+
"src"
|
|
22
|
+
],
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "tsc -p tsconfig.build.json",
|
|
25
|
+
"prepublishOnly": "bun run build"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"zod": "^4.0.0"
|
|
29
|
+
},
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"typescript": "^5.0.0"
|
|
32
|
+
},
|
|
33
|
+
"peerDependenciesMeta": {
|
|
34
|
+
"typescript": {
|
|
35
|
+
"optional": true
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,415 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client library for the me JSON-RPC API
|
|
3
|
+
* HTTP-based client with full type inference
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
getOutputSchema,
|
|
8
|
+
type RpcInput,
|
|
9
|
+
type RpcMethodName,
|
|
10
|
+
type RpcOutput,
|
|
11
|
+
} from "./schemas";
|
|
12
|
+
|
|
13
|
+
// ===== Namespace Types =====
|
|
14
|
+
|
|
15
|
+
type EngramNamespace = {
|
|
16
|
+
create: (
|
|
17
|
+
params: RpcInput<"engram.create">,
|
|
18
|
+
) => Promise<RpcOutput<"engram.create">>;
|
|
19
|
+
get: (params: RpcInput<"engram.get">) => Promise<RpcOutput<"engram.get">>;
|
|
20
|
+
search: (
|
|
21
|
+
params: RpcInput<"engram.search">,
|
|
22
|
+
) => Promise<RpcOutput<"engram.search">>;
|
|
23
|
+
update: (
|
|
24
|
+
params: RpcInput<"engram.update">,
|
|
25
|
+
) => Promise<RpcOutput<"engram.update">>;
|
|
26
|
+
delete: (
|
|
27
|
+
params: RpcInput<"engram.delete">,
|
|
28
|
+
) => Promise<RpcOutput<"engram.delete">>;
|
|
29
|
+
batchCreate: (
|
|
30
|
+
params: RpcInput<"engram.batchCreate">,
|
|
31
|
+
) => Promise<RpcOutput<"engram.batchCreate">>;
|
|
32
|
+
mv: (params: RpcInput<"engram.mv">) => Promise<RpcOutput<"engram.mv">>;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
type AuthNamespace = {
|
|
36
|
+
login: (params: RpcInput<"auth.login">) => Promise<RpcOutput<"auth.login">>;
|
|
37
|
+
whoami: () => Promise<RpcOutput<"auth.whoami">>;
|
|
38
|
+
createApiKey: (
|
|
39
|
+
params: RpcInput<"auth.createApiKey">,
|
|
40
|
+
) => Promise<RpcOutput<"auth.createApiKey">>;
|
|
41
|
+
listApiKeys: () => Promise<RpcOutput<"auth.listApiKeys">>;
|
|
42
|
+
revokeApiKey: (
|
|
43
|
+
params: RpcInput<"auth.revokeApiKey">,
|
|
44
|
+
) => Promise<RpcOutput<"auth.revokeApiKey">>;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
type PrincipalNamespace = {
|
|
48
|
+
list: () => Promise<RpcOutput<"principal.list">>;
|
|
49
|
+
get: (
|
|
50
|
+
params: RpcInput<"principal.get">,
|
|
51
|
+
) => Promise<RpcOutput<"principal.get">>;
|
|
52
|
+
create: (
|
|
53
|
+
params: RpcInput<"principal.create">,
|
|
54
|
+
) => Promise<RpcOutput<"principal.create">>;
|
|
55
|
+
delete: (
|
|
56
|
+
params: RpcInput<"principal.delete">,
|
|
57
|
+
) => Promise<RpcOutput<"principal.delete">>;
|
|
58
|
+
setPassword: (
|
|
59
|
+
params: RpcInput<"principal.setPassword">,
|
|
60
|
+
) => Promise<RpcOutput<"principal.setPassword">>;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
type GrantNamespace = {
|
|
64
|
+
create: (
|
|
65
|
+
params: RpcInput<"grant.create">,
|
|
66
|
+
) => Promise<RpcOutput<"grant.create">>;
|
|
67
|
+
revoke: (
|
|
68
|
+
params: RpcInput<"grant.revoke">,
|
|
69
|
+
) => Promise<RpcOutput<"grant.revoke">>;
|
|
70
|
+
list: (params?: RpcInput<"grant.list">) => Promise<RpcOutput<"grant.list">>;
|
|
71
|
+
check: (params: RpcInput<"grant.check">) => Promise<RpcOutput<"grant.check">>;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
type OwnerNamespace = {
|
|
75
|
+
set: (params: RpcInput<"owner.set">) => Promise<RpcOutput<"owner.set">>;
|
|
76
|
+
remove: (
|
|
77
|
+
params: RpcInput<"owner.remove">,
|
|
78
|
+
) => Promise<RpcOutput<"owner.remove">>;
|
|
79
|
+
get: (params: RpcInput<"owner.get">) => Promise<RpcOutput<"owner.get">>;
|
|
80
|
+
list: (params?: RpcInput<"owner.list">) => Promise<RpcOutput<"owner.list">>;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
type RoleNamespace = {
|
|
84
|
+
addMember: (
|
|
85
|
+
params: RpcInput<"role.addMember">,
|
|
86
|
+
) => Promise<RpcOutput<"role.addMember">>;
|
|
87
|
+
removeMember: (
|
|
88
|
+
params: RpcInput<"role.removeMember">,
|
|
89
|
+
) => Promise<RpcOutput<"role.removeMember">>;
|
|
90
|
+
listMembers: (
|
|
91
|
+
params: RpcInput<"role.listMembers">,
|
|
92
|
+
) => Promise<RpcOutput<"role.listMembers">>;
|
|
93
|
+
listForPrincipal: (
|
|
94
|
+
params?: RpcInput<"role.listForPrincipal">,
|
|
95
|
+
) => Promise<RpcOutput<"role.listForPrincipal">>;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
// ===== JSON-RPC Types =====
|
|
99
|
+
|
|
100
|
+
interface JsonRpcRequest {
|
|
101
|
+
jsonrpc: "2.0";
|
|
102
|
+
method: string;
|
|
103
|
+
params?: unknown;
|
|
104
|
+
id: string | number;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
interface JsonRpcResponse<T = unknown> {
|
|
108
|
+
jsonrpc: "2.0";
|
|
109
|
+
result?: T;
|
|
110
|
+
error?: JsonRpcError;
|
|
111
|
+
id: string | number | null;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
interface JsonRpcError {
|
|
115
|
+
code: number;
|
|
116
|
+
message: string;
|
|
117
|
+
data?: unknown;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// ===== Client Options =====
|
|
121
|
+
|
|
122
|
+
export interface MeClientOptions {
|
|
123
|
+
server?: string;
|
|
124
|
+
apiKey?: string;
|
|
125
|
+
timeout?: number;
|
|
126
|
+
retries?: number;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function sleep(ms: number): Promise<void> {
|
|
130
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const RETRYABLE_STATUS_CODES = new Set([429, 500, 502, 503, 504]);
|
|
134
|
+
const MAX_RETRY_DELAY = 30_000;
|
|
135
|
+
const BASE_DELAY = 500;
|
|
136
|
+
|
|
137
|
+
export function createClient(options: MeClientOptions = {}) {
|
|
138
|
+
const server = options.server ?? "http://localhost:3000";
|
|
139
|
+
const timeout = options.timeout ?? 30000;
|
|
140
|
+
const maxRetries = options.retries ?? 3;
|
|
141
|
+
|
|
142
|
+
let apiKey = options.apiKey;
|
|
143
|
+
let requestId = 0;
|
|
144
|
+
|
|
145
|
+
const rpcUrl = `${server}/rpc`;
|
|
146
|
+
|
|
147
|
+
function setApiKey(key: string | undefined): void {
|
|
148
|
+
apiKey = key;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function getApiKey(): string | undefined {
|
|
152
|
+
return apiKey;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
async function fetchRpc<T>(
|
|
156
|
+
body: JsonRpcRequest | JsonRpcRequest[],
|
|
157
|
+
label: string,
|
|
158
|
+
): Promise<T> {
|
|
159
|
+
for (let attempt = 0; ; attempt++) {
|
|
160
|
+
const controller = new AbortController();
|
|
161
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
162
|
+
try {
|
|
163
|
+
const headers: Record<string, string> = {
|
|
164
|
+
"Content-Type": "application/json",
|
|
165
|
+
};
|
|
166
|
+
if (apiKey) {
|
|
167
|
+
headers.Authorization = `Bearer ${apiKey}`;
|
|
168
|
+
}
|
|
169
|
+
const res = await fetch(rpcUrl, {
|
|
170
|
+
method: "POST",
|
|
171
|
+
headers,
|
|
172
|
+
body: JSON.stringify(body),
|
|
173
|
+
signal: controller.signal,
|
|
174
|
+
});
|
|
175
|
+
if (!res.ok) {
|
|
176
|
+
if (attempt < maxRetries && RETRYABLE_STATUS_CODES.has(res.status)) {
|
|
177
|
+
const retryAfter = res.headers.get("Retry-After");
|
|
178
|
+
const delayMs = retryAfter
|
|
179
|
+
? Math.min(Number(retryAfter) * 1000, MAX_RETRY_DELAY)
|
|
180
|
+
: Math.min(
|
|
181
|
+
BASE_DELAY * 2 ** attempt + Math.random() * BASE_DELAY,
|
|
182
|
+
MAX_RETRY_DELAY,
|
|
183
|
+
);
|
|
184
|
+
await sleep(delayMs);
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
throw new Error(`HTTP error: ${res.status} ${res.statusText}`);
|
|
188
|
+
}
|
|
189
|
+
return (await res.json()) as T;
|
|
190
|
+
} catch (error) {
|
|
191
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
192
|
+
if (attempt < maxRetries) {
|
|
193
|
+
const delayMs = Math.min(
|
|
194
|
+
BASE_DELAY * 2 ** attempt + Math.random() * BASE_DELAY,
|
|
195
|
+
MAX_RETRY_DELAY,
|
|
196
|
+
);
|
|
197
|
+
await sleep(delayMs);
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
throw new Error(`Request timeout: ${label}`);
|
|
201
|
+
}
|
|
202
|
+
// Retry on network errors (TypeError from fetch)
|
|
203
|
+
if (error instanceof TypeError && attempt < maxRetries) {
|
|
204
|
+
const delayMs = Math.min(
|
|
205
|
+
BASE_DELAY * 2 ** attempt + Math.random() * BASE_DELAY,
|
|
206
|
+
MAX_RETRY_DELAY,
|
|
207
|
+
);
|
|
208
|
+
await sleep(delayMs);
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
throw error;
|
|
212
|
+
} finally {
|
|
213
|
+
clearTimeout(timeoutId);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
async function call<M extends RpcMethodName>(
|
|
219
|
+
method: M,
|
|
220
|
+
...args: RpcInput<M> extends Record<string, never>
|
|
221
|
+
? []
|
|
222
|
+
: [params: RpcInput<M>]
|
|
223
|
+
): Promise<RpcOutput<M>> {
|
|
224
|
+
const request: JsonRpcRequest = {
|
|
225
|
+
jsonrpc: "2.0",
|
|
226
|
+
method,
|
|
227
|
+
params: args[0],
|
|
228
|
+
id: ++requestId,
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
const response = await fetchRpc<JsonRpcResponse<RpcOutput<M>>>(
|
|
232
|
+
request,
|
|
233
|
+
method,
|
|
234
|
+
);
|
|
235
|
+
|
|
236
|
+
if (response.error) {
|
|
237
|
+
throw Object.assign(new Error(response.error.message), {
|
|
238
|
+
code: response.error.code,
|
|
239
|
+
data: response.error.data,
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const schema = getOutputSchema(method);
|
|
244
|
+
if (schema) {
|
|
245
|
+
const parsed = schema.safeParse(response.result);
|
|
246
|
+
if (!parsed.success) {
|
|
247
|
+
throw new Error(
|
|
248
|
+
`Invalid response for ${method}: ${parsed.error.message}`,
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
return parsed.data as RpcOutput<M>;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return response.result as RpcOutput<M>;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
async function callRaw<T>(method: string, params?: unknown): Promise<T> {
|
|
258
|
+
const request: JsonRpcRequest = {
|
|
259
|
+
jsonrpc: "2.0",
|
|
260
|
+
method,
|
|
261
|
+
params,
|
|
262
|
+
id: ++requestId,
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
const response = await fetchRpc<JsonRpcResponse<T>>(request, method);
|
|
266
|
+
|
|
267
|
+
if (response.error) {
|
|
268
|
+
throw Object.assign(new Error(response.error.message), {
|
|
269
|
+
code: response.error.code,
|
|
270
|
+
data: response.error.data,
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
return response.result as T;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
async function batch<T extends unknown[]>(
|
|
278
|
+
requests: { method: string; params?: unknown }[],
|
|
279
|
+
): Promise<T> {
|
|
280
|
+
if (requests.length === 0) {
|
|
281
|
+
return [] as unknown as T;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
const batchRequests: JsonRpcRequest[] = requests.map((req) => ({
|
|
285
|
+
jsonrpc: "2.0",
|
|
286
|
+
method: req.method,
|
|
287
|
+
params: req.params,
|
|
288
|
+
id: ++requestId,
|
|
289
|
+
}));
|
|
290
|
+
|
|
291
|
+
const idToIndex = new Map<string | number, number>();
|
|
292
|
+
batchRequests.forEach((req, index) => {
|
|
293
|
+
idToIndex.set(req.id, index);
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
const responses = await fetchRpc<JsonRpcResponse[]>(batchRequests, "batch");
|
|
297
|
+
|
|
298
|
+
const results: unknown[] = new Array(requests.length);
|
|
299
|
+
for (const response of responses) {
|
|
300
|
+
if (response.id == null) continue;
|
|
301
|
+
const index = idToIndex.get(response.id);
|
|
302
|
+
if (index !== undefined) {
|
|
303
|
+
if (response.error) {
|
|
304
|
+
results[index] = Object.assign(new Error(response.error.message), {
|
|
305
|
+
code: response.error.code,
|
|
306
|
+
data: response.error.data,
|
|
307
|
+
});
|
|
308
|
+
} else {
|
|
309
|
+
results[index] = response.result;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// Fill any missing responses (sparse array holes) with errors
|
|
315
|
+
for (let i = 0; i < results.length; i++) {
|
|
316
|
+
if (results[i] === undefined) {
|
|
317
|
+
results[i] = new Error("No response received for batch request");
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
return results as T;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// Build namespace objects
|
|
325
|
+
const engram: EngramNamespace = {
|
|
326
|
+
create: (params) => call("engram.create", params),
|
|
327
|
+
get: (params) => call("engram.get", params),
|
|
328
|
+
search: (params) => call("engram.search", params),
|
|
329
|
+
update: (params) => call("engram.update", params),
|
|
330
|
+
delete: (params) => call("engram.delete", params),
|
|
331
|
+
batchCreate: (params) => call("engram.batchCreate", params),
|
|
332
|
+
mv: (params) => call("engram.mv", params),
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
const auth: AuthNamespace = {
|
|
336
|
+
login: (params) => call("auth.login", params),
|
|
337
|
+
whoami: () => call("auth.whoami"),
|
|
338
|
+
createApiKey: (params) => call("auth.createApiKey", params),
|
|
339
|
+
listApiKeys: () => call("auth.listApiKeys"),
|
|
340
|
+
revokeApiKey: (params) => call("auth.revokeApiKey", params),
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
const principal: PrincipalNamespace = {
|
|
344
|
+
list: () => call("principal.list"),
|
|
345
|
+
get: (params) => call("principal.get", params),
|
|
346
|
+
create: (params) => call("principal.create", params),
|
|
347
|
+
delete: (params) => call("principal.delete", params),
|
|
348
|
+
setPassword: (params) => call("principal.setPassword", params),
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
const grant: GrantNamespace = {
|
|
352
|
+
create: (params) => call("grant.create", params),
|
|
353
|
+
revoke: (params) => call("grant.revoke", params),
|
|
354
|
+
list: (params) => call("grant.list", params ?? {}),
|
|
355
|
+
check: (params) => call("grant.check", params),
|
|
356
|
+
};
|
|
357
|
+
|
|
358
|
+
const owner: OwnerNamespace = {
|
|
359
|
+
set: (params) => call("owner.set", params),
|
|
360
|
+
remove: (params) => call("owner.remove", params),
|
|
361
|
+
get: (params) => call("owner.get", params),
|
|
362
|
+
list: (params) => call("owner.list", params ?? {}),
|
|
363
|
+
};
|
|
364
|
+
|
|
365
|
+
const role: RoleNamespace = {
|
|
366
|
+
addMember: (params) => call("role.addMember", params),
|
|
367
|
+
removeMember: (params) => call("role.removeMember", params),
|
|
368
|
+
listMembers: (params) => call("role.listMembers", params),
|
|
369
|
+
listForPrincipal: (params) => call("role.listForPrincipal", params ?? {}),
|
|
370
|
+
};
|
|
371
|
+
|
|
372
|
+
return {
|
|
373
|
+
call,
|
|
374
|
+
callRaw,
|
|
375
|
+
batch,
|
|
376
|
+
setApiKey,
|
|
377
|
+
getApiKey,
|
|
378
|
+
|
|
379
|
+
engram,
|
|
380
|
+
auth,
|
|
381
|
+
principal,
|
|
382
|
+
grant,
|
|
383
|
+
owner,
|
|
384
|
+
role,
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
export type MeClient = ReturnType<typeof createClient>;
|
|
389
|
+
|
|
390
|
+
// Re-export types from shared schemas for convenience
|
|
391
|
+
export type {
|
|
392
|
+
ApiKey,
|
|
393
|
+
CreateApiKeyResult,
|
|
394
|
+
CreateEngramParams,
|
|
395
|
+
CreatePrincipalParams,
|
|
396
|
+
Engram,
|
|
397
|
+
EngramWithScore,
|
|
398
|
+
LoginResult,
|
|
399
|
+
MvEngramParams,
|
|
400
|
+
MvEngramResult,
|
|
401
|
+
Principal,
|
|
402
|
+
PrincipalDetail,
|
|
403
|
+
PrincipalListItem,
|
|
404
|
+
PrincipalRole,
|
|
405
|
+
RoleMember,
|
|
406
|
+
RpcInput,
|
|
407
|
+
RpcMethodName,
|
|
408
|
+
RpcOutput,
|
|
409
|
+
SearchEngramParams,
|
|
410
|
+
SearchEngramResult,
|
|
411
|
+
TreeGrant,
|
|
412
|
+
TreeOwner,
|
|
413
|
+
UpdateEngramParams,
|
|
414
|
+
Whoami,
|
|
415
|
+
} from "./schemas";
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Auth schemas - shared between server and client
|
|
3
|
+
*/
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
|
|
6
|
+
// ===== Input Schemas =====
|
|
7
|
+
|
|
8
|
+
export const registerSchema = z.object({
|
|
9
|
+
email: z.email(),
|
|
10
|
+
password: z.string().min(8),
|
|
11
|
+
name: z.string().min(1),
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
export const loginSchema = z.object({
|
|
15
|
+
identifier: z.string().min(1),
|
|
16
|
+
password: z.string(),
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
export const createApiKeySchema = z.object({
|
|
20
|
+
name: z.string().min(1),
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
export const revokeApiKeySchema = z.object({
|
|
24
|
+
keyId: z.string(),
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
// ===== Output Schemas =====
|
|
28
|
+
|
|
29
|
+
export const principalSchema = z.object({
|
|
30
|
+
id: z.uuidv7(),
|
|
31
|
+
email: z.email().nullable(),
|
|
32
|
+
name: z.string(),
|
|
33
|
+
superuser: z.boolean(),
|
|
34
|
+
createrole: z.boolean(),
|
|
35
|
+
can_login: z.boolean(),
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
export const loginResultSchema = z.object({
|
|
39
|
+
key: z.string(),
|
|
40
|
+
principal: principalSchema,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
export const whoamiSchema = z.object({
|
|
44
|
+
principalId: z.uuidv7(),
|
|
45
|
+
name: z.string(),
|
|
46
|
+
email: z.email().nullable(),
|
|
47
|
+
superuser: z.boolean(),
|
|
48
|
+
createrole: z.boolean(),
|
|
49
|
+
can_login: z.boolean(),
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
export const apiKeySchema = z.object({
|
|
53
|
+
id: z.string(),
|
|
54
|
+
name: z.string(),
|
|
55
|
+
expires_at: z.string(),
|
|
56
|
+
created_at: z.string(),
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
export const createApiKeyResultSchema = z.object({
|
|
60
|
+
key: z.string(),
|
|
61
|
+
id: z.string(),
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
export const successSchema = z.object({
|
|
65
|
+
success: z.literal(true),
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// ===== Inferred Types =====
|
|
69
|
+
|
|
70
|
+
export type LoginParams = z.infer<typeof loginSchema>;
|
|
71
|
+
export type CreateApiKeyParams = z.infer<typeof createApiKeySchema>;
|
|
72
|
+
export type RevokeApiKeyParams = z.infer<typeof revokeApiKeySchema>;
|
|
73
|
+
|
|
74
|
+
export type Principal = z.infer<typeof principalSchema>;
|
|
75
|
+
export type LoginResult = z.infer<typeof loginResultSchema>;
|
|
76
|
+
export type Whoami = z.infer<typeof whoamiSchema>;
|
|
77
|
+
export type ApiKey = z.infer<typeof apiKeySchema>;
|
|
78
|
+
export type CreateApiKeyResult = z.infer<typeof createApiKeyResultSchema>;
|
|
79
|
+
export type Success = z.infer<typeof successSchema>;
|