@nodora/client 0.0.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/LICENSE +21 -0
- package/README.md +31 -0
- package/lib/client.d.ts +203 -0
- package/lib/client.js +247 -0
- package/lib/metrics.js +41 -0
- package/lib/utils.js +10 -0
- package/package.json +26 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nodora
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# @nodora/client
|
|
2
|
+
|
|
3
|
+
A lightweight client for the [Nodora](https://app.nodora.net) platform. Evaluate business rules in your application with built-in caching, event batching, and multi-environment support.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @nodora/client
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick start
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { NodoraClient } from "@nodora/client";
|
|
15
|
+
|
|
16
|
+
const client = new NodoraClient({
|
|
17
|
+
apiKey: "apikey_abc123...",
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const checkout = client.ruleset("Checkout");
|
|
21
|
+
const result = await checkout.evaluate("IsDiscountEligible", {
|
|
22
|
+
total: 100,
|
|
23
|
+
pastCustomer: true,
|
|
24
|
+
});
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
For configuration options, caching strategies, and detailed usage, see the official [documentation](https://docs.nodora.org).
|
|
28
|
+
|
|
29
|
+
## License
|
|
30
|
+
|
|
31
|
+
MIT
|
package/lib/client.d.ts
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import type { EvaluationResult } from "@nodora/js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The deployment environment the client is running in.
|
|
5
|
+
*
|
|
6
|
+
* @example "production"
|
|
7
|
+
*/
|
|
8
|
+
export type Environment = "staging" | "production";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Caching strategy configuration.
|
|
12
|
+
*
|
|
13
|
+
* Currently supports a stale-while-revalidate approach:
|
|
14
|
+
* - Returns cached data immediately (even if stale)
|
|
15
|
+
* - Fetches fresh data in the background and updates cache
|
|
16
|
+
*/
|
|
17
|
+
export type Strategy = {
|
|
18
|
+
/**
|
|
19
|
+
* Strategy type.
|
|
20
|
+
*/
|
|
21
|
+
type: "stale-while-revalidate";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Time-to-live for cached data (in milliseconds).
|
|
25
|
+
* After this duration, cached data is considered stale and will revalidate.
|
|
26
|
+
*
|
|
27
|
+
* @example 60000 // 1 minute
|
|
28
|
+
*/
|
|
29
|
+
ttl: number;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export interface ClientOptions {
|
|
33
|
+
/**
|
|
34
|
+
* Base URL of the Nodora instance.
|
|
35
|
+
*
|
|
36
|
+
* @default "https://app.nodora.net"
|
|
37
|
+
*/
|
|
38
|
+
baseUrl?: string;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Public API key used to authenticate requests.
|
|
42
|
+
*
|
|
43
|
+
* @example "apikey_abc123..."
|
|
44
|
+
*/
|
|
45
|
+
apiKey: string;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Target environment for the client.
|
|
49
|
+
*
|
|
50
|
+
* @default "production"
|
|
51
|
+
* @example "staging"
|
|
52
|
+
*/
|
|
53
|
+
env?: Environment;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Optional caching strategy.
|
|
57
|
+
*
|
|
58
|
+
* If omitted, caching is disabled.
|
|
59
|
+
*
|
|
60
|
+
* @example { type: "stale-while-revalidate", ttl: 30000 }
|
|
61
|
+
*/
|
|
62
|
+
strategy?: Strategy;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Maximum number of events to batch before sending.
|
|
66
|
+
*
|
|
67
|
+
* Helps reduce network overhead by grouping events.
|
|
68
|
+
*
|
|
69
|
+
* @default 60
|
|
70
|
+
*/
|
|
71
|
+
flushThreshold?: number;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Time interval (in ms) to flush events even if the threshold is not reached.
|
|
75
|
+
*
|
|
76
|
+
* Ensures events are sent during low activity periods.
|
|
77
|
+
*
|
|
78
|
+
* @default 10000
|
|
79
|
+
*/
|
|
80
|
+
flushInterval?: number;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Maximum number of events to keep in the buffer.
|
|
84
|
+
*
|
|
85
|
+
* When the buffer exceeds this size (e.g. due to persistent flush failures),
|
|
86
|
+
* the oldest events are dropped to prevent unbounded memory growth.
|
|
87
|
+
*
|
|
88
|
+
* @default 300
|
|
89
|
+
*/
|
|
90
|
+
maxBufferSize?: number;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Timeout (in ms) for HTTP requests to the Nodora API.
|
|
94
|
+
*
|
|
95
|
+
* Applies to both data fetching and event ingestion requests.
|
|
96
|
+
*
|
|
97
|
+
* @default 10000
|
|
98
|
+
*/
|
|
99
|
+
timeout?: number;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Called when a non-fatal background error occurs,
|
|
103
|
+
* such as a failed event flush or background refresh.
|
|
104
|
+
*
|
|
105
|
+
* @param error - The error that occurred.
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* onError: (err) => logger.warn(err)
|
|
109
|
+
*/
|
|
110
|
+
onError?: (error: Error) => void;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* A handle for interacting with a specific ruleset.
|
|
115
|
+
* Provides methods to evaluate rules against input data.
|
|
116
|
+
*/
|
|
117
|
+
export declare class RulesetHandle {
|
|
118
|
+
/**
|
|
119
|
+
* Evaluates a rule within the ruleset.
|
|
120
|
+
*
|
|
121
|
+
* @param rule - The name or identifier of the rule to evaluate.
|
|
122
|
+
* @param input - Input data used during evaluation.
|
|
123
|
+
*
|
|
124
|
+
* @returns A promise that resolves to the evaluation result.
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* const result = await ruleset.evaluate("IsEligible", { age: 25 });
|
|
128
|
+
*/
|
|
129
|
+
evaluate(
|
|
130
|
+
rule: string,
|
|
131
|
+
input?: Record<string, any>,
|
|
132
|
+
): Promise<EvaluationResult>;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Main client for interacting with the Nodora API.
|
|
137
|
+
* Handles configuration, batching, caching, and ruleset evaluation.
|
|
138
|
+
*/
|
|
139
|
+
export declare class NodoraClient {
|
|
140
|
+
/**
|
|
141
|
+
* Creates a new Nodora client instance.
|
|
142
|
+
*
|
|
143
|
+
* @param options - Configuration options for the client.
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* const client = new NodoraClient({
|
|
147
|
+
* apiKey: "apikey_abc123...",
|
|
148
|
+
* environment: "production",
|
|
149
|
+
* });
|
|
150
|
+
*/
|
|
151
|
+
constructor(options: ClientOptions);
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Prefetches all rulesets and required data for evaluation.
|
|
155
|
+
*
|
|
156
|
+
* Typically used to warm up cache or reduce latency
|
|
157
|
+
* before performing evaluations.
|
|
158
|
+
*
|
|
159
|
+
* @returns A promise that resolves when prefetching is complete.
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* await client.prefetch();
|
|
163
|
+
*/
|
|
164
|
+
prefetch(): Promise<void>;
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Returns a handle for a specific ruleset.
|
|
168
|
+
*
|
|
169
|
+
* @param ruleset - The name or identifier of the ruleset.
|
|
170
|
+
*
|
|
171
|
+
* @returns A RulesetHandle instance used to evaluate rules.
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* const rules = client.ruleset("Checkout");
|
|
175
|
+
* const result = await rules.evaluate("IsDiscountEligible", { total: 100 });
|
|
176
|
+
*/
|
|
177
|
+
ruleset(ruleset: string): RulesetHandle;
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Flushes any buffered events to the server immediately.
|
|
181
|
+
*
|
|
182
|
+
* Call this before `destroy()` if you need to ensure
|
|
183
|
+
* all pending events are sent.
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* await client.flush();
|
|
187
|
+
* await client.destroy();
|
|
188
|
+
*/
|
|
189
|
+
flush(): Promise<void>;
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Cleans up internal resources used by the client.
|
|
193
|
+
*
|
|
194
|
+
* Should be called when the client is no longer needed
|
|
195
|
+
* to prevent memory leaks, timers, or pending network activity.
|
|
196
|
+
* Buffered events are discarded — call `flush()` first if needed.
|
|
197
|
+
*
|
|
198
|
+
* @example
|
|
199
|
+
* await client.flush();
|
|
200
|
+
* await client.destroy();
|
|
201
|
+
*/
|
|
202
|
+
destroy(): Promise<void>;
|
|
203
|
+
}
|
package/lib/client.js
ADDED
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import { createEvaluator } from "@nodora/js";
|
|
2
|
+
import { MetricAccumulator } from "./metrics";
|
|
3
|
+
import { asyncGate } from "./utils";
|
|
4
|
+
|
|
5
|
+
export class NodoraClient {
|
|
6
|
+
constructor(options) {
|
|
7
|
+
if (!options || !options.apiKey) {
|
|
8
|
+
throw new Error("apiKey is required");
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const defaultOptions = {
|
|
12
|
+
baseUrl: "https://app.nodora.net",
|
|
13
|
+
env: "production",
|
|
14
|
+
flushThreshold: 60,
|
|
15
|
+
flushInterval: 10000,
|
|
16
|
+
maxBufferSize: 300,
|
|
17
|
+
timeout: 10000,
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
this._options = { ...defaultOptions, ...options };
|
|
21
|
+
this._onError = options.onError || (() => {});
|
|
22
|
+
this._cache = new Map();
|
|
23
|
+
this._lastFetchedAt = 0;
|
|
24
|
+
this._flushTimer = null;
|
|
25
|
+
this._eventBuffer = [];
|
|
26
|
+
this._initPromise = null;
|
|
27
|
+
this._refreshGate = asyncGate();
|
|
28
|
+
this._flushGate = asyncGate();
|
|
29
|
+
this._abortController = new AbortController();
|
|
30
|
+
this._metrics = new MetricAccumulator();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async prefetch() {
|
|
34
|
+
if (!this._initPromise) {
|
|
35
|
+
this._initPromise = this._init();
|
|
36
|
+
}
|
|
37
|
+
return this._initPromise;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async flush() {
|
|
41
|
+
return this._flushEvents();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
ruleset(rulesetName) {
|
|
45
|
+
return new RulesetHandle(this, rulesetName);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
_evaluate(rulesetName, ruleName, input) {
|
|
49
|
+
if (this._abortController.signal.aborted) return;
|
|
50
|
+
|
|
51
|
+
const entry = this._cache.get(rulesetName);
|
|
52
|
+
if (!entry) {
|
|
53
|
+
throw new Error(`Ruleset "${rulesetName}" not found`);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
let result;
|
|
57
|
+
let succeeded = false;
|
|
58
|
+
|
|
59
|
+
try {
|
|
60
|
+
result = entry.evaluator.evaluate(ruleName, input);
|
|
61
|
+
succeeded = true;
|
|
62
|
+
} catch (err) {
|
|
63
|
+
throw err;
|
|
64
|
+
} finally {
|
|
65
|
+
this._metrics.record(entry.rulesetId, "evaluations", ruleName, succeeded);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (result.emitted_signals && result.emitted_signals.length > 0) {
|
|
69
|
+
for (const signal of result.emitted_signals) {
|
|
70
|
+
this._bufferEvent({
|
|
71
|
+
type: "signal",
|
|
72
|
+
payload: {
|
|
73
|
+
rulesetId: entry.rulesetId,
|
|
74
|
+
env: this._options.env,
|
|
75
|
+
payload: {
|
|
76
|
+
name: signal.name,
|
|
77
|
+
args: signal.args || [],
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// trigger background refresh if TTL expired
|
|
85
|
+
const strategy = this._options.strategy;
|
|
86
|
+
if (
|
|
87
|
+
strategy &&
|
|
88
|
+
strategy.type === "stale-while-revalidate" &&
|
|
89
|
+
Date.now() - this._lastFetchedAt > strategy.ttl
|
|
90
|
+
) {
|
|
91
|
+
this._refreshGate(() => this._refresh()).catch((err) =>
|
|
92
|
+
this._onError(err)
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return result;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
_bufferEvent(event) {
|
|
100
|
+
this._eventBuffer.push(event);
|
|
101
|
+
if (this._eventBuffer.length >= this._options.flushThreshold) {
|
|
102
|
+
this._flushEvents();
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async _init() {
|
|
107
|
+
await this._refresh();
|
|
108
|
+
|
|
109
|
+
if (this._flushTimer) return;
|
|
110
|
+
this._flushTimer = setInterval(() => {
|
|
111
|
+
if (this._abortController.signal.aborted) {
|
|
112
|
+
clearInterval(this._flushTimer);
|
|
113
|
+
this._flushTimer = null;
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
this._flushEvents();
|
|
117
|
+
}, this._options.flushInterval);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async _fetch() {
|
|
121
|
+
const { baseUrl, apiKey, env, timeout } = this._options;
|
|
122
|
+
const url = `${baseUrl}/api/releases/latest?env=${env}`;
|
|
123
|
+
const res = await fetch(url, {
|
|
124
|
+
headers: { "x-api-key": apiKey },
|
|
125
|
+
signal: AbortSignal.any([
|
|
126
|
+
AbortSignal.timeout(timeout),
|
|
127
|
+
this._abortController.signal,
|
|
128
|
+
]),
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
if (!res.ok) {
|
|
132
|
+
throw new Error(`Failed to fetch releases: HTTP ${res.status}`);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return res.json();
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async _syncEvaluators(data) {
|
|
139
|
+
const entries = Object.entries(data);
|
|
140
|
+
await Promise.all(
|
|
141
|
+
entries.map(async ([rulesetName, release]) => {
|
|
142
|
+
const existing = this._cache.get(rulesetName);
|
|
143
|
+
|
|
144
|
+
if (existing && existing.checksum === release.checksum) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (existing && existing.evaluator) {
|
|
149
|
+
existing.evaluator.destroy();
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const evaluator = await createEvaluator(release.compiled);
|
|
153
|
+
this._cache.set(rulesetName, {
|
|
154
|
+
rulesetId: release.rulesetId,
|
|
155
|
+
checksum: release.checksum,
|
|
156
|
+
evaluator,
|
|
157
|
+
});
|
|
158
|
+
})
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
// clean up deleted rulesets
|
|
162
|
+
for (const rulesetName of this._cache.keys()) {
|
|
163
|
+
if (!(rulesetName in data)) {
|
|
164
|
+
const entry = this._cache.get(rulesetName);
|
|
165
|
+
if (entry.evaluator) entry.evaluator.destroy();
|
|
166
|
+
this._cache.delete(rulesetName);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
async _refresh() {
|
|
172
|
+
try {
|
|
173
|
+
const data = await this._fetch();
|
|
174
|
+
this._lastFetchedAt = Date.now();
|
|
175
|
+
await this._syncEvaluators(data);
|
|
176
|
+
} catch (err) {
|
|
177
|
+
if (this._abortController.signal.aborted) return;
|
|
178
|
+
throw err;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
_flushEvents() {
|
|
183
|
+
for (const event of this._metrics.drain()) {
|
|
184
|
+
this._eventBuffer.push(event);
|
|
185
|
+
}
|
|
186
|
+
if (this._eventBuffer.length === 0) return Promise.resolve();
|
|
187
|
+
|
|
188
|
+
return this._flushGate(async () => {
|
|
189
|
+
const batch = this._eventBuffer.splice(0);
|
|
190
|
+
const { baseUrl, apiKey, timeout } = this._options;
|
|
191
|
+
|
|
192
|
+
try {
|
|
193
|
+
return await fetch(`${baseUrl}/api/ingest`, {
|
|
194
|
+
method: "POST",
|
|
195
|
+
headers: {
|
|
196
|
+
"Content-Type": "application/json",
|
|
197
|
+
"x-api-key": apiKey,
|
|
198
|
+
},
|
|
199
|
+
body: JSON.stringify(batch),
|
|
200
|
+
signal: AbortSignal.any([
|
|
201
|
+
AbortSignal.timeout(timeout),
|
|
202
|
+
this._abortController.signal,
|
|
203
|
+
]),
|
|
204
|
+
});
|
|
205
|
+
} catch (err) {
|
|
206
|
+
this._eventBuffer.unshift(...batch);
|
|
207
|
+
const max = this._options.maxBufferSize;
|
|
208
|
+
if (this._eventBuffer.length > max) {
|
|
209
|
+
this._eventBuffer.length = max;
|
|
210
|
+
}
|
|
211
|
+
this._onError(err);
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
async destroy() {
|
|
217
|
+
this._abortController.abort();
|
|
218
|
+
|
|
219
|
+
if (this._flushTimer) {
|
|
220
|
+
clearInterval(this._flushTimer);
|
|
221
|
+
this._flushTimer = null;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
this._eventBuffer.length = 0;
|
|
225
|
+
this._metrics.clear();
|
|
226
|
+
|
|
227
|
+
for (const entry of this._cache.values()) {
|
|
228
|
+
if (entry.evaluator) {
|
|
229
|
+
entry.evaluator.destroy();
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
this._cache.clear();
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
class RulesetHandle {
|
|
238
|
+
constructor(client, ruleset) {
|
|
239
|
+
this._client = client;
|
|
240
|
+
this._ruleset = ruleset;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
async evaluate(rule, input = {}) {
|
|
244
|
+
await this._client.prefetch();
|
|
245
|
+
return this._client._evaluate(this._ruleset, rule, input);
|
|
246
|
+
}
|
|
247
|
+
}
|
package/lib/metrics.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export class MetricAccumulator {
|
|
2
|
+
constructor() {
|
|
3
|
+
this._counters = new Map();
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
record(rulesetId, type, bucket, succeeded) {
|
|
7
|
+
const key = `${rulesetId}:${type}:${bucket}`;
|
|
8
|
+
const counter = this._counters.get(key);
|
|
9
|
+
if (counter) {
|
|
10
|
+
counter.count++;
|
|
11
|
+
if (succeeded) counter.succeeded++;
|
|
12
|
+
else counter.failed++;
|
|
13
|
+
} else {
|
|
14
|
+
this._counters.set(key, {
|
|
15
|
+
rulesetId,
|
|
16
|
+
type,
|
|
17
|
+
bucket,
|
|
18
|
+
count: 1,
|
|
19
|
+
succeeded: succeeded ? 1 : 0,
|
|
20
|
+
failed: succeeded ? 0 : 1,
|
|
21
|
+
timestamp: Date.now(),
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
drain() {
|
|
27
|
+
const events = [];
|
|
28
|
+
for (const payload of this._counters.values()) {
|
|
29
|
+
events.push({
|
|
30
|
+
type: "metric",
|
|
31
|
+
payload,
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
this._counters.clear();
|
|
35
|
+
return events;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
clear() {
|
|
39
|
+
this._counters.clear();
|
|
40
|
+
}
|
|
41
|
+
}
|
package/lib/utils.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nodora/client",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Nodora platform client for ruleset evaluation",
|
|
5
|
+
"main": "lib/client.js",
|
|
6
|
+
"types": "lib/client.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"lib"
|
|
9
|
+
],
|
|
10
|
+
"keywords": [
|
|
11
|
+
"nodora",
|
|
12
|
+
"client"
|
|
13
|
+
],
|
|
14
|
+
"author": "Nodora",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@nodora/js": "^0.0.3"
|
|
18
|
+
},
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"import": "./lib/client.js",
|
|
22
|
+
"require": "./lib/client.js",
|
|
23
|
+
"types": "./lib/client.d.ts"
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|