@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,291 @@
|
|
|
1
|
+
/**
|
|
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 {
|
|
10
|
+
apiKeySchema,
|
|
11
|
+
createApiKeyResultSchema,
|
|
12
|
+
createApiKeySchema,
|
|
13
|
+
loginResultSchema,
|
|
14
|
+
loginSchema,
|
|
15
|
+
principalSchema,
|
|
16
|
+
revokeApiKeySchema,
|
|
17
|
+
successSchema,
|
|
18
|
+
whoamiSchema,
|
|
19
|
+
} from "./auth";
|
|
20
|
+
// Engram schemas
|
|
21
|
+
import {
|
|
22
|
+
batchCreateEngramSchema,
|
|
23
|
+
batchCreateResultSchema,
|
|
24
|
+
createEngramSchema,
|
|
25
|
+
deleteEngramSchema,
|
|
26
|
+
deleteResultSchema,
|
|
27
|
+
engramSchema,
|
|
28
|
+
getEngramSchema,
|
|
29
|
+
mvEngramResultSchema,
|
|
30
|
+
mvEngramSchema,
|
|
31
|
+
searchEngramResultSchema,
|
|
32
|
+
searchEngramSchema,
|
|
33
|
+
updateEngramSchema,
|
|
34
|
+
} from "./engram";
|
|
35
|
+
|
|
36
|
+
// Principal schemas
|
|
37
|
+
import {
|
|
38
|
+
addRoleMemberSchema,
|
|
39
|
+
checkTreeAccessSchema,
|
|
40
|
+
createPrincipalSchema,
|
|
41
|
+
deletePrincipalSchema,
|
|
42
|
+
getPrincipalSchema,
|
|
43
|
+
getTreeOwnerSchema,
|
|
44
|
+
grantTreeAccessSchema,
|
|
45
|
+
listRoleMembersSchema,
|
|
46
|
+
listRolesForPrincipalSchema,
|
|
47
|
+
listTreeGrantsSchema,
|
|
48
|
+
listTreeOwnersSchema,
|
|
49
|
+
principalDetailSchema,
|
|
50
|
+
principalListItemSchema,
|
|
51
|
+
principalRoleSchema,
|
|
52
|
+
removeRoleMemberSchema,
|
|
53
|
+
removeTreeOwnerSchema,
|
|
54
|
+
revokeTreeAccessSchema,
|
|
55
|
+
roleMemberSchema,
|
|
56
|
+
setPasswordSchema,
|
|
57
|
+
setTreeOwnerSchema,
|
|
58
|
+
treeGrantSchema,
|
|
59
|
+
treeOwnerSchema,
|
|
60
|
+
} from "./principal";
|
|
61
|
+
|
|
62
|
+
// ===== RPC Method Definition =====
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Define a method with its input schema and output type.
|
|
66
|
+
* The output type is specified as a generic since DB results
|
|
67
|
+
* aren't validated at runtime (trusted internal data).
|
|
68
|
+
*
|
|
69
|
+
* Uses z.input<> for input types to preserve optionality of fields
|
|
70
|
+
* with defaults (e.g., limit?: number with .default(100) stays optional
|
|
71
|
+
* for callers, but the server gets the default after parsing).
|
|
72
|
+
*/
|
|
73
|
+
function method<TInput extends z.ZodType, TOutput>(
|
|
74
|
+
input: TInput,
|
|
75
|
+
_output?: TOutput,
|
|
76
|
+
) {
|
|
77
|
+
return {
|
|
78
|
+
input,
|
|
79
|
+
_types: {} as {
|
|
80
|
+
input: z.input<TInput>;
|
|
81
|
+
output: TOutput;
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** For methods with no input params */
|
|
87
|
+
const emptySchema = z.object({});
|
|
88
|
+
|
|
89
|
+
// ===== Auth Methods Contract =====
|
|
90
|
+
|
|
91
|
+
export const authMethods = {
|
|
92
|
+
"auth.login": method(loginSchema, {} as z.infer<typeof loginResultSchema>),
|
|
93
|
+
"auth.whoami": method(emptySchema, {} as z.infer<typeof whoamiSchema>),
|
|
94
|
+
"auth.createApiKey": method(
|
|
95
|
+
createApiKeySchema,
|
|
96
|
+
{} as z.infer<typeof createApiKeyResultSchema>,
|
|
97
|
+
),
|
|
98
|
+
"auth.listApiKeys": method(emptySchema, {} as z.infer<typeof apiKeySchema>[]),
|
|
99
|
+
"auth.revokeApiKey": method(
|
|
100
|
+
revokeApiKeySchema,
|
|
101
|
+
{} as z.infer<typeof successSchema>,
|
|
102
|
+
),
|
|
103
|
+
} as const;
|
|
104
|
+
|
|
105
|
+
// ===== Principal Methods Contract =====
|
|
106
|
+
|
|
107
|
+
export const principalMethods = {
|
|
108
|
+
"principal.list": method(
|
|
109
|
+
emptySchema,
|
|
110
|
+
{} as z.infer<typeof principalListItemSchema>[],
|
|
111
|
+
),
|
|
112
|
+
"principal.get": method(
|
|
113
|
+
getPrincipalSchema,
|
|
114
|
+
{} as z.infer<typeof principalDetailSchema>,
|
|
115
|
+
),
|
|
116
|
+
"principal.create": method(
|
|
117
|
+
createPrincipalSchema,
|
|
118
|
+
{} as z.infer<typeof principalSchema>,
|
|
119
|
+
),
|
|
120
|
+
"principal.delete": method(
|
|
121
|
+
deletePrincipalSchema,
|
|
122
|
+
{} as z.infer<typeof successSchema>,
|
|
123
|
+
),
|
|
124
|
+
"principal.setPassword": method(
|
|
125
|
+
setPasswordSchema,
|
|
126
|
+
{} as z.infer<typeof successSchema>,
|
|
127
|
+
),
|
|
128
|
+
} as const;
|
|
129
|
+
|
|
130
|
+
// ===== Grant Methods Contract =====
|
|
131
|
+
|
|
132
|
+
export const grantMethods = {
|
|
133
|
+
"grant.create": method(
|
|
134
|
+
grantTreeAccessSchema,
|
|
135
|
+
{} as z.infer<typeof successSchema>,
|
|
136
|
+
),
|
|
137
|
+
"grant.revoke": method(
|
|
138
|
+
revokeTreeAccessSchema,
|
|
139
|
+
{} as z.infer<typeof successSchema>,
|
|
140
|
+
),
|
|
141
|
+
"grant.list": method(
|
|
142
|
+
listTreeGrantsSchema,
|
|
143
|
+
{} as z.infer<typeof treeGrantSchema>[],
|
|
144
|
+
),
|
|
145
|
+
"grant.check": method(checkTreeAccessSchema, {} as { allowed: boolean }),
|
|
146
|
+
} as const;
|
|
147
|
+
|
|
148
|
+
// ===== Owner Methods Contract =====
|
|
149
|
+
|
|
150
|
+
export const ownerMethods = {
|
|
151
|
+
"owner.set": method(setTreeOwnerSchema, {} as z.infer<typeof successSchema>),
|
|
152
|
+
"owner.remove": method(
|
|
153
|
+
removeTreeOwnerSchema,
|
|
154
|
+
{} as z.infer<typeof successSchema>,
|
|
155
|
+
),
|
|
156
|
+
"owner.get": method(
|
|
157
|
+
getTreeOwnerSchema,
|
|
158
|
+
{} as z.infer<typeof treeOwnerSchema> | null,
|
|
159
|
+
),
|
|
160
|
+
"owner.list": method(
|
|
161
|
+
listTreeOwnersSchema,
|
|
162
|
+
{} as z.infer<typeof treeOwnerSchema>[],
|
|
163
|
+
),
|
|
164
|
+
} as const;
|
|
165
|
+
|
|
166
|
+
// ===== Role Methods Contract =====
|
|
167
|
+
|
|
168
|
+
export const roleMethods = {
|
|
169
|
+
"role.addMember": method(
|
|
170
|
+
addRoleMemberSchema,
|
|
171
|
+
{} as z.infer<typeof successSchema>,
|
|
172
|
+
),
|
|
173
|
+
"role.removeMember": method(
|
|
174
|
+
removeRoleMemberSchema,
|
|
175
|
+
{} as z.infer<typeof successSchema>,
|
|
176
|
+
),
|
|
177
|
+
"role.listMembers": method(
|
|
178
|
+
listRoleMembersSchema,
|
|
179
|
+
{} as z.infer<typeof roleMemberSchema>[],
|
|
180
|
+
),
|
|
181
|
+
"role.listForPrincipal": method(
|
|
182
|
+
listRolesForPrincipalSchema,
|
|
183
|
+
{} as z.infer<typeof principalRoleSchema>[],
|
|
184
|
+
),
|
|
185
|
+
} as const;
|
|
186
|
+
|
|
187
|
+
// ===== Engram Methods Contract =====
|
|
188
|
+
|
|
189
|
+
export const engramMethods = {
|
|
190
|
+
"engram.create": method(
|
|
191
|
+
createEngramSchema,
|
|
192
|
+
{} as z.infer<typeof engramSchema>,
|
|
193
|
+
),
|
|
194
|
+
"engram.batchCreate": method(
|
|
195
|
+
batchCreateEngramSchema,
|
|
196
|
+
{} as z.infer<typeof batchCreateResultSchema>,
|
|
197
|
+
),
|
|
198
|
+
"engram.get": method(getEngramSchema, {} as z.infer<typeof engramSchema>),
|
|
199
|
+
"engram.update": method(
|
|
200
|
+
updateEngramSchema,
|
|
201
|
+
{} as z.infer<typeof engramSchema>,
|
|
202
|
+
),
|
|
203
|
+
"engram.delete": method(
|
|
204
|
+
deleteEngramSchema,
|
|
205
|
+
{} as z.infer<typeof deleteResultSchema>,
|
|
206
|
+
),
|
|
207
|
+
"engram.search": method(
|
|
208
|
+
searchEngramSchema,
|
|
209
|
+
{} as z.infer<typeof searchEngramResultSchema>,
|
|
210
|
+
),
|
|
211
|
+
"engram.mv": method(
|
|
212
|
+
mvEngramSchema,
|
|
213
|
+
{} as z.infer<typeof mvEngramResultSchema>,
|
|
214
|
+
),
|
|
215
|
+
} as const;
|
|
216
|
+
|
|
217
|
+
// ===== Combined RPC Contract =====
|
|
218
|
+
|
|
219
|
+
/** All registered RPC methods */
|
|
220
|
+
export const rpcMethods = {
|
|
221
|
+
...authMethods,
|
|
222
|
+
...principalMethods,
|
|
223
|
+
...grantMethods,
|
|
224
|
+
...ownerMethods,
|
|
225
|
+
...roleMethods,
|
|
226
|
+
...engramMethods,
|
|
227
|
+
} as const;
|
|
228
|
+
|
|
229
|
+
/** Union of all method names */
|
|
230
|
+
export type RpcMethodName = keyof typeof rpcMethods;
|
|
231
|
+
|
|
232
|
+
/** Extract input type for a method */
|
|
233
|
+
export type RpcInput<M extends RpcMethodName> =
|
|
234
|
+
(typeof rpcMethods)[M]["_types"]["input"];
|
|
235
|
+
|
|
236
|
+
/** Extract output type for a method */
|
|
237
|
+
export type RpcOutput<M extends RpcMethodName> =
|
|
238
|
+
(typeof rpcMethods)[M]["_types"]["output"];
|
|
239
|
+
|
|
240
|
+
/** Get the input schema for runtime validation */
|
|
241
|
+
export function getInputSchema<M extends RpcMethodName>(method: M) {
|
|
242
|
+
return rpcMethods[method].input;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// ===== Output Schema Map =====
|
|
246
|
+
|
|
247
|
+
const checkAccessOutputSchema = z.object({ allowed: z.boolean() });
|
|
248
|
+
|
|
249
|
+
/** Runtime output schemas for response validation */
|
|
250
|
+
const outputSchemas: Record<string, z.ZodType> = {
|
|
251
|
+
// Auth
|
|
252
|
+
"auth.login": loginResultSchema,
|
|
253
|
+
"auth.whoami": whoamiSchema,
|
|
254
|
+
"auth.createApiKey": createApiKeyResultSchema,
|
|
255
|
+
"auth.listApiKeys": z.array(apiKeySchema),
|
|
256
|
+
"auth.revokeApiKey": successSchema,
|
|
257
|
+
// Principal
|
|
258
|
+
"principal.list": z.array(principalListItemSchema),
|
|
259
|
+
"principal.get": principalDetailSchema,
|
|
260
|
+
"principal.create": principalSchema,
|
|
261
|
+
"principal.delete": successSchema,
|
|
262
|
+
"principal.setPassword": successSchema,
|
|
263
|
+
// Grant
|
|
264
|
+
"grant.create": successSchema,
|
|
265
|
+
"grant.revoke": successSchema,
|
|
266
|
+
"grant.list": z.array(treeGrantSchema),
|
|
267
|
+
"grant.check": checkAccessOutputSchema,
|
|
268
|
+
// Owner
|
|
269
|
+
"owner.set": successSchema,
|
|
270
|
+
"owner.remove": successSchema,
|
|
271
|
+
"owner.get": treeOwnerSchema.nullable(),
|
|
272
|
+
"owner.list": z.array(treeOwnerSchema),
|
|
273
|
+
// Role
|
|
274
|
+
"role.addMember": successSchema,
|
|
275
|
+
"role.removeMember": successSchema,
|
|
276
|
+
"role.listMembers": z.array(roleMemberSchema),
|
|
277
|
+
"role.listForPrincipal": z.array(principalRoleSchema),
|
|
278
|
+
// Engram
|
|
279
|
+
"engram.create": engramSchema,
|
|
280
|
+
"engram.batchCreate": batchCreateResultSchema,
|
|
281
|
+
"engram.get": engramSchema,
|
|
282
|
+
"engram.update": engramSchema,
|
|
283
|
+
"engram.delete": deleteResultSchema,
|
|
284
|
+
"engram.search": searchEngramResultSchema,
|
|
285
|
+
"engram.mv": mvEngramResultSchema,
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
/** Get the output schema for runtime response validation */
|
|
289
|
+
export function getOutputSchema(method: string): z.ZodType | undefined {
|
|
290
|
+
return outputSchemas[method];
|
|
291
|
+
}
|