@memosyne/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/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # @memosyne/sdk
2
+
3
+ Official TypeScript client for the [Memosyne](https://usememosyne.com) API.
4
+
5
+ ## Install
6
+
7
+ ```
8
+ npm install @memosyne/sdk
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```ts
14
+ import { Memosyne } from "@memosyne/sdk";
15
+
16
+ const memosyne = new Memosyne({
17
+ apiKey: process.env.MEMOSYNE_API_KEY!,
18
+ baseUrl: "https://api.usememosyne.com",
19
+ });
20
+
21
+ await memosyne.logEvent({
22
+ action: "user.login",
23
+ actor: "user_123",
24
+ resourceType: "session",
25
+ resourceId: "sess_abc123",
26
+ });
27
+ ```
28
+
29
+ ## API
30
+
31
+ ### `logEvent(input)`
32
+
33
+ Logs an action without a before/after diff.
34
+
35
+ ### `logChange(input)`
36
+
37
+ Computes and stores the diff between two object snapshots.
38
+
39
+ ```ts
40
+ await memosyne.logChange({
41
+ before: { status: "active" },
42
+ after: { status: "archived" },
43
+ actor: "user_123",
44
+ resourceType: "facility",
45
+ resourceId: "fac_1",
46
+ });
47
+ ```
48
+
49
+ ### `getEvents(options)`
50
+
51
+ Fetches logged events for a resource.
52
+
53
+ ```ts
54
+ const { events, nextCursor } = await memosyne.getEvents({
55
+ resourceType: "facility",
56
+ resourceId: "fac_1",
57
+ limit: 25,
58
+ });
59
+ ```
60
+
61
+ ## Errors
62
+
63
+ Failed requests throw `MemosyneError`, which carries the HTTP `status` and a `code`
64
+ (`UNAUTHORIZED`, `INVALID_REQUEST`, `NOT_FOUND`, `RATE_LIMITED`, or `SERVER_ERROR`).
@@ -0,0 +1,70 @@
1
+ type ChangeType = "added" | "updated" | "removed";
2
+ interface FieldChange {
3
+ path: string;
4
+ type: ChangeType;
5
+ from: unknown;
6
+ to: unknown;
7
+ }
8
+ interface Actor {
9
+ id: string;
10
+ name?: string;
11
+ email?: string;
12
+ }
13
+ interface ChangeEvent {
14
+ id: string;
15
+ resourceType: string;
16
+ resourceId: string;
17
+ actor: Actor;
18
+ action?: string;
19
+ changes: FieldChange[];
20
+ metadata: Record<string, unknown>;
21
+ timestamp: string;
22
+ }
23
+ interface LogChangeInput {
24
+ before: Record<string, unknown>;
25
+ after: Record<string, unknown>;
26
+ actor: string | Actor;
27
+ resourceType: string;
28
+ resourceId: string;
29
+ metadata?: Record<string, unknown>;
30
+ redact?: string[];
31
+ }
32
+ interface LogEventInput {
33
+ action: string;
34
+ actor: string | Actor;
35
+ resourceType: string;
36
+ resourceId: string;
37
+ metadata?: Record<string, unknown>;
38
+ }
39
+ interface QueryOptions {
40
+ resourceType: string;
41
+ resourceId: string;
42
+ limit?: number;
43
+ cursor?: string;
44
+ }
45
+ interface QueryResult {
46
+ events: ChangeEvent[];
47
+ nextCursor: string | null;
48
+ }
49
+ interface MemosyneConfig {
50
+ apiKey: string;
51
+ baseUrl: string;
52
+ }
53
+
54
+ declare class Memosyne {
55
+ private readonly apiKey;
56
+ private readonly baseUrl;
57
+ constructor(config: MemosyneConfig);
58
+ logChange(input: LogChangeInput): Promise<ChangeEvent>;
59
+ logEvent(input: LogEventInput): Promise<ChangeEvent>;
60
+ getEvents(options: QueryOptions): Promise<QueryResult>;
61
+ private request;
62
+ }
63
+
64
+ declare class MemosyneError extends Error {
65
+ readonly status: number;
66
+ readonly code: string;
67
+ constructor(message: string, status: number, code: string);
68
+ }
69
+
70
+ export { type Actor, type ChangeEvent, type ChangeType, type FieldChange, type LogChangeInput, type LogEventInput, Memosyne, type MemosyneConfig, MemosyneError, type QueryOptions, type QueryResult };
package/dist/index.js ADDED
@@ -0,0 +1,67 @@
1
+ // src/errors.ts
2
+ var MemosyneError = class extends Error {
3
+ constructor(message, status, code) {
4
+ super(message);
5
+ this.status = status;
6
+ this.code = code;
7
+ this.name = "MemosyneError";
8
+ }
9
+ status;
10
+ code;
11
+ };
12
+
13
+ // src/client.ts
14
+ var Memosyne = class {
15
+ apiKey;
16
+ baseUrl;
17
+ constructor(config) {
18
+ this.apiKey = config.apiKey;
19
+ this.baseUrl = config.baseUrl.replace(/\/$/, "");
20
+ }
21
+ async logChange(input) {
22
+ const res = await this.request("POST", "/v1/events", input);
23
+ return res;
24
+ }
25
+ async logEvent(input) {
26
+ const res = await this.request("POST", "/v1/events/log", input);
27
+ return res;
28
+ }
29
+ async getEvents(options) {
30
+ const params = new URLSearchParams({
31
+ resourceType: options.resourceType,
32
+ resourceId: options.resourceId,
33
+ ...options.limit !== void 0 ? { limit: String(options.limit) } : {},
34
+ ...options.cursor !== void 0 ? { cursor: options.cursor } : {}
35
+ });
36
+ const res = await this.request("GET", `/v1/events?${params}`);
37
+ return res;
38
+ }
39
+ async request(method, path, body) {
40
+ const res = await fetch(`${this.baseUrl}${path}`, {
41
+ method,
42
+ headers: {
43
+ Authorization: `Bearer ${this.apiKey}`,
44
+ ...body !== void 0 ? { "Content-Type": "application/json" } : {}
45
+ },
46
+ ...body !== void 0 ? { body: JSON.stringify(body) } : {}
47
+ });
48
+ const json = await res.json().catch(() => ({}));
49
+ if (!res.ok) {
50
+ const message = json.error ?? "Request failed";
51
+ throw new MemosyneError(message, res.status, toErrorCode(res.status));
52
+ }
53
+ return json;
54
+ }
55
+ };
56
+ function toErrorCode(status) {
57
+ if (status === 401) return "UNAUTHORIZED";
58
+ if (status === 400) return "INVALID_REQUEST";
59
+ if (status === 404) return "NOT_FOUND";
60
+ if (status === 429) return "RATE_LIMITED";
61
+ return "SERVER_ERROR";
62
+ }
63
+ export {
64
+ Memosyne,
65
+ MemosyneError
66
+ };
67
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/errors.ts","../src/client.ts"],"sourcesContent":["export class MemosyneError extends Error {\n constructor(\n message: string,\n public readonly status: number,\n public readonly code: string,\n ) {\n super(message);\n this.name = \"MemosyneError\";\n }\n}\n","import { MemosyneError } from \"./errors.js\";\nimport type {\n MemosyneConfig,\n ChangeEvent,\n QueryOptions,\n QueryResult,\n LogChangeInput,\n LogEventInput,\n} from \"./types.js\";\n\nexport class Memosyne {\n private readonly apiKey: string;\n private readonly baseUrl: string;\n\n constructor(config: MemosyneConfig) {\n this.apiKey = config.apiKey;\n this.baseUrl = config.baseUrl.replace(/\\/$/, \"\");\n }\n\n async logChange(input: LogChangeInput): Promise<ChangeEvent> {\n const res = await this.request(\"POST\", \"/v1/events\", input);\n return res as ChangeEvent;\n }\n\n async logEvent(input: LogEventInput): Promise<ChangeEvent> {\n const res = await this.request(\"POST\", \"/v1/events/log\", input);\n return res as ChangeEvent;\n }\n\n async getEvents(options: QueryOptions): Promise<QueryResult> {\n const params = new URLSearchParams({\n resourceType: options.resourceType,\n resourceId: options.resourceId,\n ...(options.limit !== undefined ? { limit: String(options.limit) } : {}),\n ...(options.cursor !== undefined ? { cursor: options.cursor } : {}),\n });\n\n const res = await this.request(\"GET\", `/v1/events?${params}`);\n return res as QueryResult;\n }\n\n private async request(method: string, path: string, body?: unknown): Promise<unknown> {\n const res = await fetch(`${this.baseUrl}${path}`, {\n method,\n headers: {\n Authorization: `Bearer ${this.apiKey}`,\n ...(body !== undefined ? { \"Content-Type\": \"application/json\" } : {}),\n },\n ...(body !== undefined ? { body: JSON.stringify(body) } : {}),\n });\n\n const json = await res.json().catch(() => ({}));\n\n if (!res.ok) {\n const message = (json as { error?: string }).error ?? \"Request failed\";\n throw new MemosyneError(message, res.status, toErrorCode(res.status));\n }\n\n return json;\n }\n}\n\nfunction toErrorCode(status: number): string {\n if (status === 401) return \"UNAUTHORIZED\";\n if (status === 400) return \"INVALID_REQUEST\";\n if (status === 404) return \"NOT_FOUND\";\n if (status === 429) return \"RATE_LIMITED\";\n return \"SERVER_ERROR\";\n}\n"],"mappings":";AAAO,IAAM,gBAAN,cAA4B,MAAM;AAAA,EACvC,YACE,SACgB,QACA,MAChB;AACA,UAAM,OAAO;AAHG;AACA;AAGhB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AAE;AAE9C,QAAI,CAAC,IAAI,IAAI;AACX,YAAM,UAAW,KAA4B,SAAS;AACtD,YAAM,IAAI,cAAc,SAAS,IAAI,QAAQ,YAAY,IAAI,MAAM,CAAC;AAAA,IACtE;AAEA,WAAO;AAAA,EACT;AACF;AAEA,SAAS,YAAY,QAAwB;AAC3C,MAAI,WAAW,IAAK,QAAO;AAC3B,MAAI,WAAW,IAAK,QAAO;AAC3B,MAAI,WAAW,IAAK,QAAO;AAC3B,MAAI,WAAW,IAAK,QAAO;AAC3B,SAAO;AACT;","names":[]}
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@memosyne/sdk",
3
+ "version": "0.1.0",
4
+ "description": "Official TypeScript client for the Memosyne API.",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": ["dist"],
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/comandel/memosyne.git",
19
+ "directory": "sdks/typescript"
20
+ },
21
+ "publishConfig": {
22
+ "access": "public"
23
+ },
24
+ "scripts": {
25
+ "build": "tsup",
26
+ "test": "vitest run",
27
+ "typecheck": "tsc --noEmit"
28
+ },
29
+ "devDependencies": {
30
+ "@types/node": "^26.0.0",
31
+ "tsup": "^8.5.1",
32
+ "typescript": "^6.0.3",
33
+ "vitest": "^4.1.9"
34
+ }
35
+ }