@reactionary/core 0.2.5 → 0.2.10
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,5 @@
|
|
|
1
1
|
import { trace } from "@opentelemetry/api";
|
|
2
|
+
import { z } from "zod";
|
|
2
3
|
import { getReactionaryMeter } from "../metrics/metrics.js";
|
|
3
4
|
import { error, success } from "../schemas/result.js";
|
|
4
5
|
const TRACER_NAME = "@reactionary";
|
|
@@ -33,6 +34,14 @@ class ReactionaryDecoratorOptions {
|
|
|
33
34
|
* given query.
|
|
34
35
|
*/
|
|
35
36
|
this.cacheTimeToLiveInSeconds = 60;
|
|
37
|
+
/**
|
|
38
|
+
* The schema for the input (query or mutation) type, for validation purposes
|
|
39
|
+
*/
|
|
40
|
+
this.inputSchema = z.never();
|
|
41
|
+
/**
|
|
42
|
+
* The schema for the primary output type, for validation purposes
|
|
43
|
+
*/
|
|
44
|
+
this.outputSchema = z.never();
|
|
36
45
|
}
|
|
37
46
|
}
|
|
38
47
|
function Reactionary(options) {
|
|
@@ -61,37 +70,44 @@ function Reactionary(options) {
|
|
|
61
70
|
return input;
|
|
62
71
|
}
|
|
63
72
|
const cacheKey = this.generateCacheKeyForQuery(scope, input.value);
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
73
|
+
let fromCache = null;
|
|
74
|
+
if (options.cache) {
|
|
75
|
+
fromCache = await this.cache.get(
|
|
76
|
+
cacheKey,
|
|
77
|
+
options.outputSchema
|
|
78
|
+
);
|
|
79
|
+
}
|
|
68
80
|
let result;
|
|
69
81
|
if (!fromCache) {
|
|
70
82
|
result = await original.apply(this, [input.value]);
|
|
71
83
|
const r = result;
|
|
72
84
|
if (r.success) {
|
|
73
85
|
const dependencyIds = this.generateDependencyIdsForModel(r.value);
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
86
|
+
if (options.cache) {
|
|
87
|
+
this.cache.put(cacheKey, r.value, {
|
|
88
|
+
ttlSeconds: configuration.cacheTimeToLiveInSeconds,
|
|
89
|
+
dependencyIds
|
|
90
|
+
});
|
|
91
|
+
}
|
|
78
92
|
}
|
|
79
93
|
} else {
|
|
80
94
|
result = success(fromCache);
|
|
81
95
|
cacheStatus = "hit";
|
|
82
96
|
}
|
|
83
|
-
const validatedResult = validateOutput(
|
|
97
|
+
const validatedResult = validateOutput(
|
|
98
|
+
result,
|
|
99
|
+
configuration.outputSchema
|
|
100
|
+
);
|
|
84
101
|
if (validatedResult.success) {
|
|
85
102
|
validatedResult.meta.cache.hit = !!fromCache;
|
|
86
103
|
validatedResult.meta.cache.key = cacheKey;
|
|
87
104
|
}
|
|
88
105
|
return result;
|
|
89
106
|
} catch (err) {
|
|
90
|
-
console.error(err);
|
|
91
107
|
status = "error";
|
|
92
108
|
const result = error({
|
|
93
109
|
type: "Generic",
|
|
94
|
-
message:
|
|
110
|
+
message: errorToString(err)
|
|
95
111
|
});
|
|
96
112
|
return result;
|
|
97
113
|
} finally {
|
|
@@ -159,9 +175,23 @@ async function traceSpan(name, fn) {
|
|
|
159
175
|
}
|
|
160
176
|
});
|
|
161
177
|
}
|
|
178
|
+
function errorToString(err) {
|
|
179
|
+
if (err instanceof Error) {
|
|
180
|
+
return err.stack ?? err.message;
|
|
181
|
+
}
|
|
182
|
+
if (typeof err === "string") {
|
|
183
|
+
return err;
|
|
184
|
+
}
|
|
185
|
+
try {
|
|
186
|
+
return JSON.stringify(err);
|
|
187
|
+
} catch {
|
|
188
|
+
return String(err);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
162
191
|
export {
|
|
163
192
|
Reactionary,
|
|
164
193
|
ReactionaryDecoratorOptions,
|
|
194
|
+
errorToString,
|
|
165
195
|
getTracer,
|
|
166
196
|
traceSpan,
|
|
167
197
|
validateInput,
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Tracer } from '@opentelemetry/api';
|
|
2
|
-
import
|
|
2
|
+
import { z } from 'zod';
|
|
3
3
|
import type { BaseProvider } from '../providers/index.js';
|
|
4
4
|
import { type Result } from '../schemas/result.js';
|
|
5
5
|
export declare function getTracer(): Tracer;
|
|
@@ -32,11 +32,11 @@ export declare class ReactionaryDecoratorOptions {
|
|
|
32
32
|
/**
|
|
33
33
|
* The schema for the input (query or mutation) type, for validation purposes
|
|
34
34
|
*/
|
|
35
|
-
inputSchema
|
|
35
|
+
inputSchema: z.ZodType;
|
|
36
36
|
/**
|
|
37
37
|
* The schema for the primary output type, for validation purposes
|
|
38
38
|
*/
|
|
39
|
-
outputSchema
|
|
39
|
+
outputSchema: z.ZodType;
|
|
40
40
|
}
|
|
41
41
|
/**
|
|
42
42
|
* Decorator for provider functions to provide functionality such as caching, tracing and type-checked
|
|
@@ -57,3 +57,8 @@ export declare function validateOutput(output: Result<unknown>, schema: z.ZodTyp
|
|
|
57
57
|
* traced span for OTEL handling.
|
|
58
58
|
*/
|
|
59
59
|
export declare function traceSpan<T>(name: string, fn: () => Promise<T> | T): Promise<T>;
|
|
60
|
+
/**
|
|
61
|
+
* Utility function for converting a caught error to a plain
|
|
62
|
+
* string for reporting back.
|
|
63
|
+
*/
|
|
64
|
+
export declare function errorToString(err: unknown): string;
|