@kalera/munin-sdk 0.1.0 → 1.0.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.
@@ -1,4 +1,4 @@
1
1
 
2
- > @kalera/munin-sdk@0.1.0 build /home/runner/work/munin-for-agents/munin-for-agents/packages/ts-sdk
2
+ > @kalera/munin-sdk@1.0.0 build /home/runner/work/munin-for-agents/munin-for-agents/packages/ts-sdk
3
3
  > tsc -p tsconfig.json
4
4
 
package/dist/client.d.ts CHANGED
@@ -2,19 +2,18 @@ import type { MuninAction, MuninCapabilities, MuninClientConfig, MuninResponse }
2
2
  export declare class MuninClient {
3
3
  private readonly baseUrl;
4
4
  private readonly apiKey?;
5
- private readonly project;
6
5
  private readonly timeoutMs;
7
6
  private readonly fetchImpl;
8
7
  private capabilitiesCache?;
9
8
  constructor(config: MuninClientConfig);
10
9
  capabilities(forceRefresh?: boolean): Promise<MuninCapabilities>;
11
- invoke<TPayload extends Record<string, unknown>, TData = unknown>(action: MuninAction, payload: TPayload, options?: {
10
+ invoke<TPayload extends Record<string, unknown>, TData = unknown>(projectId: string, action: MuninAction, payload: TPayload, options?: {
12
11
  requestId?: string;
13
12
  ensureCapability?: boolean;
14
13
  }): Promise<MuninResponse<TData>>;
15
- store(payload: Record<string, unknown>): Promise<MuninResponse<unknown>>;
16
- retrieve(payload: Record<string, unknown>): Promise<MuninResponse<unknown>>;
17
- search(payload: Record<string, unknown>): Promise<MuninResponse<unknown>>;
18
- list(payload?: Record<string, unknown>): Promise<MuninResponse<unknown>>;
19
- recent(payload?: Record<string, unknown>): Promise<MuninResponse<unknown>>;
14
+ store(projectId: string, payload: Record<string, unknown>): Promise<MuninResponse<unknown>>;
15
+ retrieve(projectId: string, payload: Record<string, unknown>): Promise<MuninResponse<unknown>>;
16
+ search(projectId: string, payload: Record<string, unknown>): Promise<MuninResponse<unknown>>;
17
+ list(projectId: string, payload?: Record<string, unknown>): Promise<MuninResponse<unknown>>;
18
+ recent(projectId: string, payload?: Record<string, unknown>): Promise<MuninResponse<unknown>>;
20
19
  }
package/dist/client.js CHANGED
@@ -4,14 +4,12 @@ const DEFAULT_TIMEOUT_MS = 15_000;
4
4
  export class MuninClient {
5
5
  baseUrl;
6
6
  apiKey;
7
- project;
8
7
  timeoutMs;
9
8
  fetchImpl;
10
9
  capabilitiesCache;
11
10
  constructor(config) {
12
11
  this.baseUrl = config.baseUrl.replace(/\/$/, "");
13
12
  this.apiKey = config.apiKey;
14
- this.project = config.project;
15
13
  this.timeoutMs = config.timeoutMs ?? DEFAULT_TIMEOUT_MS;
16
14
  this.fetchImpl = config.fetchImpl ?? fetch;
17
15
  }
@@ -23,7 +21,7 @@ export class MuninClient {
23
21
  this.capabilitiesCache = caps;
24
22
  return caps;
25
23
  }
26
- async invoke(action, payload, options) {
24
+ async invoke(projectId, action, payload, options) {
27
25
  if (options?.ensureCapability) {
28
26
  const caps = await this.capabilities();
29
27
  if (!isActionSupported(caps, action)) {
@@ -35,13 +33,14 @@ export class MuninClient {
35
33
  }
36
34
  const request = {
37
35
  apiKey: this.apiKey,
38
- projectId: this.project,
36
+ project: projectId,
37
+ projectId: projectId, // Fallback for un-restarted server
39
38
  action,
40
39
  payload,
41
40
  requestId: options?.requestId,
42
41
  client: {
43
42
  name: "@kalera/munin-sdk",
44
- version: "0.1.0",
43
+ version: "1.0.0",
45
44
  },
46
45
  };
47
46
  const controller = new AbortController();
@@ -59,26 +58,30 @@ export class MuninClient {
59
58
  clearTimeout(timeout);
60
59
  const body = (await response.json());
61
60
  if (!response.ok || (body.ok === false) || (body.success === false)) {
62
- throw new MuninSdkError(body.error ?? {
61
+ let errObj = body.error;
62
+ if (typeof errObj === 'string') {
63
+ errObj = { code: "INTERNAL_ERROR", message: errObj };
64
+ }
65
+ throw new MuninSdkError(errObj ?? {
63
66
  code: "INTERNAL_ERROR",
64
67
  message: `Unexpected failure invoking action '${action}'`,
65
68
  });
66
69
  }
67
70
  return body;
68
71
  }
69
- async store(payload) {
70
- return this.invoke("store", payload, { ensureCapability: true });
72
+ async store(projectId, payload) {
73
+ return this.invoke(projectId, "store", payload, { ensureCapability: true });
71
74
  }
72
- async retrieve(payload) {
73
- return this.invoke("retrieve", payload, { ensureCapability: true });
75
+ async retrieve(projectId, payload) {
76
+ return this.invoke(projectId, "retrieve", payload, { ensureCapability: true });
74
77
  }
75
- async search(payload) {
76
- return this.invoke("search", payload, { ensureCapability: true });
78
+ async search(projectId, payload) {
79
+ return this.invoke(projectId, "search", payload, { ensureCapability: true });
77
80
  }
78
- async list(payload = {}) {
79
- return this.invoke("list", payload, { ensureCapability: true });
81
+ async list(projectId, payload = {}) {
82
+ return this.invoke(projectId, "list", payload, { ensureCapability: true });
80
83
  }
81
- async recent(payload = {}) {
82
- return this.invoke("recent", payload, { ensureCapability: true });
84
+ async recent(projectId, payload = {}) {
85
+ return this.invoke(projectId, "recent", payload, { ensureCapability: true });
83
86
  }
84
87
  }
package/dist/types.d.ts CHANGED
@@ -39,7 +39,6 @@ export interface MuninResponse<TData = unknown> {
39
39
  export interface MuninClientConfig {
40
40
  baseUrl: string;
41
41
  apiKey?: string;
42
- project: string;
43
42
  timeoutMs?: number;
44
43
  fetchImpl?: typeof fetch;
45
44
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kalera/munin-sdk",
3
- "version": "0.1.0",
3
+ "version": "1.0.0",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/client.ts CHANGED
@@ -16,7 +16,6 @@ const DEFAULT_TIMEOUT_MS = 15_000;
16
16
  export class MuninClient {
17
17
  private readonly baseUrl: string;
18
18
  private readonly apiKey?: string;
19
- private readonly project: string;
20
19
  private readonly timeoutMs: number;
21
20
  private readonly fetchImpl: typeof fetch;
22
21
  private capabilitiesCache?: MuninCapabilities;
@@ -24,7 +23,6 @@ export class MuninClient {
24
23
  constructor(config: MuninClientConfig) {
25
24
  this.baseUrl = config.baseUrl.replace(/\/$/, "");
26
25
  this.apiKey = config.apiKey;
27
- this.project = config.project;
28
26
  this.timeoutMs = config.timeoutMs ?? DEFAULT_TIMEOUT_MS;
29
27
  this.fetchImpl = config.fetchImpl ?? fetch;
30
28
  }
@@ -44,6 +42,7 @@ export class MuninClient {
44
42
  }
45
43
 
46
44
  async invoke<TPayload extends Record<string, unknown>, TData = unknown>(
45
+ projectId: string,
47
46
  action: MuninAction,
48
47
  payload: TPayload,
49
48
  options?: { requestId?: string; ensureCapability?: boolean },
@@ -60,13 +59,14 @@ export class MuninClient {
60
59
 
61
60
  const request = {
62
61
  apiKey: this.apiKey,
63
- projectId: this.project,
62
+ project: projectId,
63
+ projectId: projectId, // Fallback for un-restarted server
64
64
  action,
65
65
  payload,
66
66
  requestId: options?.requestId,
67
67
  client: {
68
68
  name: "@kalera/munin-sdk",
69
- version: "0.1.0",
69
+ version: "1.0.0",
70
70
  },
71
71
  };
72
72
 
@@ -91,8 +91,13 @@ export class MuninClient {
91
91
  const body = (await response.json()) as any;
92
92
 
93
93
  if (!response.ok || (body.ok === false) || (body.success === false)) {
94
+ let errObj = body.error;
95
+ if (typeof errObj === 'string') {
96
+ errObj = { code: "INTERNAL_ERROR", message: errObj };
97
+ }
98
+
94
99
  throw new MuninSdkError(
95
- body.error ?? {
100
+ errObj ?? {
96
101
  code: "INTERNAL_ERROR",
97
102
  message: `Unexpected failure invoking action '${action}'`,
98
103
  },
@@ -102,23 +107,23 @@ export class MuninClient {
102
107
  return body;
103
108
  }
104
109
 
105
- async store(payload: Record<string, unknown>) {
106
- return this.invoke("store", payload, { ensureCapability: true });
110
+ async store(projectId: string, payload: Record<string, unknown>) {
111
+ return this.invoke(projectId, "store", payload, { ensureCapability: true });
107
112
  }
108
113
 
109
- async retrieve(payload: Record<string, unknown>) {
110
- return this.invoke("retrieve", payload, { ensureCapability: true });
114
+ async retrieve(projectId: string, payload: Record<string, unknown>) {
115
+ return this.invoke(projectId, "retrieve", payload, { ensureCapability: true });
111
116
  }
112
117
 
113
- async search(payload: Record<string, unknown>) {
114
- return this.invoke("search", payload, { ensureCapability: true });
118
+ async search(projectId: string, payload: Record<string, unknown>) {
119
+ return this.invoke(projectId, "search", payload, { ensureCapability: true });
115
120
  }
116
121
 
117
- async list(payload: Record<string, unknown> = {}) {
118
- return this.invoke("list", payload, { ensureCapability: true });
122
+ async list(projectId: string, payload: Record<string, unknown> = {}) {
123
+ return this.invoke(projectId, "list", payload, { ensureCapability: true });
119
124
  }
120
125
 
121
- async recent(payload: Record<string, unknown> = {}) {
122
- return this.invoke("recent", payload, { ensureCapability: true });
126
+ async recent(projectId: string, payload: Record<string, unknown> = {}) {
127
+ return this.invoke(projectId, "recent", payload, { ensureCapability: true });
123
128
  }
124
129
  }
package/src/types.ts CHANGED
@@ -57,7 +57,6 @@ export interface MuninResponse<TData = unknown> {
57
57
  export interface MuninClientConfig {
58
58
  baseUrl: string;
59
59
  apiKey?: string;
60
- project: string;
61
60
  timeoutMs?: number;
62
61
  fetchImpl?: typeof fetch;
63
62
  }
@@ -47,14 +47,13 @@ async function run() {
47
47
 
48
48
  const client = new MuninClient({
49
49
  baseUrl: "http://localhost:4000",
50
- project: "default",
51
50
  fetchImpl: fakeFetch,
52
51
  });
53
52
 
54
53
  const capabilities = await client.capabilities();
55
54
  assert.equal(capabilities.specVersion, "v1.0.0");
56
55
 
57
- const result = await client.store({ key: "hello", content: "world" });
56
+ const result = await client.store("default", { key: "hello", content: "world" });
58
57
  assert.equal(result.ok, true);
59
58
  assert.equal((result.data as any).echoedAction, "store");
60
59