@alchemy/common 0.0.0-alpha.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/LICENSE +21 -0
- package/dist/esm/actions/addBreadCrumb.d.ts +14 -0
- package/dist/esm/actions/addBreadCrumb.js +27 -0
- package/dist/esm/actions/addBreadCrumb.js.map +1 -0
- package/dist/esm/chains.d.ts +234 -0
- package/dist/esm/chains.js +113 -0
- package/dist/esm/chains.js.map +1 -0
- package/dist/esm/errors/AccountNotFoundError.d.ts +10 -0
- package/dist/esm/errors/AccountNotFoundError.js +19 -0
- package/dist/esm/errors/AccountNotFoundError.js.map +1 -0
- package/dist/esm/errors/BaseError.d.ts +23 -0
- package/dist/esm/errors/BaseError.js +40 -0
- package/dist/esm/errors/BaseError.js.map +1 -0
- package/dist/esm/errors/ChainNotFoundError.d.ts +11 -0
- package/dist/esm/errors/ChainNotFoundError.js +19 -0
- package/dist/esm/errors/ChainNotFoundError.js.map +1 -0
- package/dist/esm/errors/ConnectionConfigError.d.ts +13 -0
- package/dist/esm/errors/ConnectionConfigError.js +25 -0
- package/dist/esm/errors/ConnectionConfigError.js.map +1 -0
- package/dist/esm/errors/FetchError.d.ts +15 -0
- package/dist/esm/errors/FetchError.js +25 -0
- package/dist/esm/errors/FetchError.js.map +1 -0
- package/dist/esm/errors/InvalidRequestError.d.ts +13 -0
- package/dist/esm/errors/InvalidRequestError.js +22 -0
- package/dist/esm/errors/InvalidRequestError.js.map +1 -0
- package/dist/esm/errors/MethodUnsupportedError.d.ts +13 -0
- package/dist/esm/errors/MethodUnsupportedError.js +21 -0
- package/dist/esm/errors/MethodUnsupportedError.js.map +1 -0
- package/dist/esm/errors/ServerError.d.ts +15 -0
- package/dist/esm/errors/ServerError.js +25 -0
- package/dist/esm/errors/ServerError.js.map +1 -0
- package/dist/esm/index.d.ts +29 -0
- package/dist/esm/index.js +27 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/logging/config.d.ts +190 -0
- package/dist/esm/logging/config.js +279 -0
- package/dist/esm/logging/config.js.map +1 -0
- package/dist/esm/logging/index.d.ts +6 -0
- package/dist/esm/logging/index.js +5 -0
- package/dist/esm/logging/index.js.map +1 -0
- package/dist/esm/logging/local.d.ts +10 -0
- package/dist/esm/logging/local.js +35 -0
- package/dist/esm/logging/local.js.map +1 -0
- package/dist/esm/logging/logger.d.ts +80 -0
- package/dist/esm/logging/logger.js +111 -0
- package/dist/esm/logging/logger.js.map +1 -0
- package/dist/esm/logging/noop.d.ts +6 -0
- package/dist/esm/logging/noop.js +12 -0
- package/dist/esm/logging/noop.js.map +1 -0
- package/dist/esm/logging/sinks.d.ts +90 -0
- package/dist/esm/logging/sinks.js +111 -0
- package/dist/esm/logging/sinks.js.map +1 -0
- package/dist/esm/logging/types.d.ts +96 -0
- package/dist/esm/logging/types.js +2 -0
- package/dist/esm/logging/types.js.map +1 -0
- package/dist/esm/logging/utils.d.ts +7 -0
- package/dist/esm/logging/utils.js +21 -0
- package/dist/esm/logging/utils.js.map +1 -0
- package/dist/esm/rest/restClient.d.ts +34 -0
- package/dist/esm/rest/restClient.js +55 -0
- package/dist/esm/rest/restClient.js.map +1 -0
- package/dist/esm/rest/types.d.ts +24 -0
- package/dist/esm/rest/types.js +2 -0
- package/dist/esm/rest/types.js.map +1 -0
- package/dist/esm/tracing/traceHeader.d.ts +82 -0
- package/dist/esm/tracing/traceHeader.js +145 -0
- package/dist/esm/tracing/traceHeader.js.map +1 -0
- package/dist/esm/tracing/updateHeaders.d.ts +24 -0
- package/dist/esm/tracing/updateHeaders.js +61 -0
- package/dist/esm/tracing/updateHeaders.js.map +1 -0
- package/dist/esm/transport/alchemy.d.ts +110 -0
- package/dist/esm/transport/alchemy.js +164 -0
- package/dist/esm/transport/alchemy.js.map +1 -0
- package/dist/esm/transport/chainRegistry.d.ts +31 -0
- package/dist/esm/transport/chainRegistry.js +95 -0
- package/dist/esm/transport/chainRegistry.js.map +1 -0
- package/dist/esm/transport/connection.d.ts +20 -0
- package/dist/esm/transport/connection.js +2 -0
- package/dist/esm/transport/connection.js.map +1 -0
- package/dist/esm/transport/connectionSchema.d.ts +124 -0
- package/dist/esm/transport/connectionSchema.js +121 -0
- package/dist/esm/transport/connectionSchema.js.map +1 -0
- package/dist/esm/utils/assertNever.d.ts +8 -0
- package/dist/esm/utils/assertNever.js +12 -0
- package/dist/esm/utils/assertNever.js.map +1 -0
- package/dist/esm/utils/bigint.d.ts +24 -0
- package/dist/esm/utils/bigint.js +37 -0
- package/dist/esm/utils/bigint.js.map +1 -0
- package/dist/esm/utils/createEip1193HandlerFactory.d.ts +18 -0
- package/dist/esm/utils/createEip1193HandlerFactory.js +11 -0
- package/dist/esm/utils/createEip1193HandlerFactory.js.map +1 -0
- package/dist/esm/utils/headers.d.ts +7 -0
- package/dist/esm/utils/headers.js +29 -0
- package/dist/esm/utils/headers.js.map +1 -0
- package/dist/esm/utils/lowerAddress.d.ts +8 -0
- package/dist/esm/utils/lowerAddress.js +9 -0
- package/dist/esm/utils/lowerAddress.js.map +1 -0
- package/dist/esm/utils/raise.d.ts +8 -0
- package/dist/esm/utils/raise.js +14 -0
- package/dist/esm/utils/raise.js.map +1 -0
- package/dist/esm/utils/types.d.ts +10 -0
- package/dist/esm/utils/types.js +2 -0
- package/dist/esm/utils/types.js.map +1 -0
- package/dist/esm/version.d.ts +1 -0
- package/dist/esm/version.js +4 -0
- package/dist/esm/version.js.map +1 -0
- package/dist/types/actions/addBreadCrumb.d.ts +15 -0
- package/dist/types/actions/addBreadCrumb.d.ts.map +1 -0
- package/dist/types/chains.d.ts +235 -0
- package/dist/types/chains.d.ts.map +1 -0
- package/dist/types/errors/AccountNotFoundError.d.ts +11 -0
- package/dist/types/errors/AccountNotFoundError.d.ts.map +1 -0
- package/dist/types/errors/BaseError.d.ts +24 -0
- package/dist/types/errors/BaseError.d.ts.map +1 -0
- package/dist/types/errors/ChainNotFoundError.d.ts +12 -0
- package/dist/types/errors/ChainNotFoundError.d.ts.map +1 -0
- package/dist/types/errors/ConnectionConfigError.d.ts +14 -0
- package/dist/types/errors/ConnectionConfigError.d.ts.map +1 -0
- package/dist/types/errors/FetchError.d.ts +16 -0
- package/dist/types/errors/FetchError.d.ts.map +1 -0
- package/dist/types/errors/InvalidRequestError.d.ts +14 -0
- package/dist/types/errors/InvalidRequestError.d.ts.map +1 -0
- package/dist/types/errors/MethodUnsupportedError.d.ts +14 -0
- package/dist/types/errors/MethodUnsupportedError.d.ts.map +1 -0
- package/dist/types/errors/ServerError.d.ts +16 -0
- package/dist/types/errors/ServerError.d.ts.map +1 -0
- package/dist/types/index.d.ts +30 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/logging/config.d.ts +191 -0
- package/dist/types/logging/config.d.ts.map +1 -0
- package/dist/types/logging/index.d.ts +7 -0
- package/dist/types/logging/index.d.ts.map +1 -0
- package/dist/types/logging/local.d.ts +11 -0
- package/dist/types/logging/local.d.ts.map +1 -0
- package/dist/types/logging/logger.d.ts +81 -0
- package/dist/types/logging/logger.d.ts.map +1 -0
- package/dist/types/logging/noop.d.ts +7 -0
- package/dist/types/logging/noop.d.ts.map +1 -0
- package/dist/types/logging/sinks.d.ts +91 -0
- package/dist/types/logging/sinks.d.ts.map +1 -0
- package/dist/types/logging/types.d.ts +97 -0
- package/dist/types/logging/types.d.ts.map +1 -0
- package/dist/types/logging/utils.d.ts +8 -0
- package/dist/types/logging/utils.d.ts.map +1 -0
- package/dist/types/rest/restClient.d.ts +35 -0
- package/dist/types/rest/restClient.d.ts.map +1 -0
- package/dist/types/rest/types.d.ts +25 -0
- package/dist/types/rest/types.d.ts.map +1 -0
- package/dist/types/tracing/traceHeader.d.ts +83 -0
- package/dist/types/tracing/traceHeader.d.ts.map +1 -0
- package/dist/types/tracing/updateHeaders.d.ts +25 -0
- package/dist/types/tracing/updateHeaders.d.ts.map +1 -0
- package/dist/types/transport/alchemy.d.ts +111 -0
- package/dist/types/transport/alchemy.d.ts.map +1 -0
- package/dist/types/transport/chainRegistry.d.ts +32 -0
- package/dist/types/transport/chainRegistry.d.ts.map +1 -0
- package/dist/types/transport/connection.d.ts +21 -0
- package/dist/types/transport/connection.d.ts.map +1 -0
- package/dist/types/transport/connectionSchema.d.ts +125 -0
- package/dist/types/transport/connectionSchema.d.ts.map +1 -0
- package/dist/types/utils/assertNever.d.ts +9 -0
- package/dist/types/utils/assertNever.d.ts.map +1 -0
- package/dist/types/utils/bigint.d.ts +25 -0
- package/dist/types/utils/bigint.d.ts.map +1 -0
- package/dist/types/utils/createEip1193HandlerFactory.d.ts +19 -0
- package/dist/types/utils/createEip1193HandlerFactory.d.ts.map +1 -0
- package/dist/types/utils/headers.d.ts +8 -0
- package/dist/types/utils/headers.d.ts.map +1 -0
- package/dist/types/utils/lowerAddress.d.ts +9 -0
- package/dist/types/utils/lowerAddress.d.ts.map +1 -0
- package/dist/types/utils/raise.d.ts +9 -0
- package/dist/types/utils/raise.d.ts.map +1 -0
- package/dist/types/utils/types.d.ts +11 -0
- package/dist/types/utils/types.d.ts.map +1 -0
- package/dist/types/version.d.ts +2 -0
- package/dist/types/version.d.ts.map +1 -0
- package/package.json +67 -0
- package/src/actions/addBreadCrumb.ts +38 -0
- package/src/chains.ts +118 -0
- package/src/errors/AccountNotFoundError.ts +16 -0
- package/src/errors/BaseError.ts +51 -0
- package/src/errors/ChainNotFoundError.ts +15 -0
- package/src/errors/ConnectionConfigError.ts +22 -0
- package/src/errors/FetchError.ts +21 -0
- package/src/errors/InvalidRequestError.ts +19 -0
- package/src/errors/MethodUnsupportedError.ts +17 -0
- package/src/errors/ServerError.ts +21 -0
- package/src/index.ts +60 -0
- package/src/logging/config.ts +365 -0
- package/src/logging/index.ts +20 -0
- package/src/logging/local.ts +39 -0
- package/src/logging/logger.ts +194 -0
- package/src/logging/noop.ts +13 -0
- package/src/logging/sinks.ts +115 -0
- package/src/logging/types.ts +111 -0
- package/src/logging/utils.ts +31 -0
- package/src/rest/restClient.ts +64 -0
- package/src/rest/types.ts +42 -0
- package/src/tracing/traceHeader.ts +154 -0
- package/src/tracing/updateHeaders.ts +66 -0
- package/src/transport/alchemy.ts +242 -0
- package/src/transport/chainRegistry.ts +115 -0
- package/src/transport/connection.ts +19 -0
- package/src/transport/connectionSchema.ts +145 -0
- package/src/utils/assertNever.ts +12 -0
- package/src/utils/bigint.ts +58 -0
- package/src/utils/createEip1193HandlerFactory.ts +25 -0
- package/src/utils/headers.ts +48 -0
- package/src/utils/lowerAddress.ts +10 -0
- package/src/utils/raise.ts +14 -0
- package/src/utils/types.ts +14 -0
- package/src/version.ts +3 -0
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type { LogEntry } from "./config.js";
|
|
2
|
+
/**
|
|
3
|
+
* In-memory sink for testing purposes.
|
|
4
|
+
* Captures all log entries in an array that can be inspected in tests.
|
|
5
|
+
*
|
|
6
|
+
* **Note:** This is primarily intended for testing SDK integrations.
|
|
7
|
+
* Most applications should use the default console sink.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { InMemorySink, setGlobalLoggerConfig, createLogger } from "@alchemy/common";
|
|
12
|
+
*
|
|
13
|
+
* // In tests
|
|
14
|
+
* const sink = new InMemorySink();
|
|
15
|
+
*
|
|
16
|
+
* setGlobalLoggerConfig({
|
|
17
|
+
* sinks: [sink.sink]
|
|
18
|
+
* });
|
|
19
|
+
*
|
|
20
|
+
* const logger = createLogger({
|
|
21
|
+
* package: "@alchemy/aa-infra",
|
|
22
|
+
* version: "1.0.0",
|
|
23
|
+
* namespace: "aa-infra"
|
|
24
|
+
* });
|
|
25
|
+
*
|
|
26
|
+
* logger.info("test message");
|
|
27
|
+
*
|
|
28
|
+
* // Inspect captured logs
|
|
29
|
+
* expect(sink.count).toBe(1);
|
|
30
|
+
* expect(sink.latest()?.message).toBe("test message");
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export declare class InMemorySink {
|
|
34
|
+
/**
|
|
35
|
+
* Array of captured log entries
|
|
36
|
+
*/
|
|
37
|
+
entries: LogEntry[];
|
|
38
|
+
/**
|
|
39
|
+
* Creates a new InMemorySink instance.
|
|
40
|
+
* Binds the sink method to the instance for use as a callback.
|
|
41
|
+
*/
|
|
42
|
+
constructor();
|
|
43
|
+
/**
|
|
44
|
+
* The sink function to pass to setGlobalLoggerConfig.
|
|
45
|
+
* Captures log entries into the internal array.
|
|
46
|
+
*
|
|
47
|
+
* @param {LogEntry} entry - The log entry to capture
|
|
48
|
+
* @returns {void}
|
|
49
|
+
*/
|
|
50
|
+
sink(entry: LogEntry): void;
|
|
51
|
+
/**
|
|
52
|
+
* Clear all captured entries
|
|
53
|
+
*
|
|
54
|
+
* @returns {void}
|
|
55
|
+
*/
|
|
56
|
+
clear(): void;
|
|
57
|
+
/**
|
|
58
|
+
* Get entries filtered by log level
|
|
59
|
+
*
|
|
60
|
+
* @param {number} level - The log level to filter by (LogLevel.ERROR, LogLevel.INFO, etc.)
|
|
61
|
+
* @returns {LogEntry[]} Array of log entries matching the specified level
|
|
62
|
+
*/
|
|
63
|
+
getByLevel(level: number): LogEntry[];
|
|
64
|
+
/**
|
|
65
|
+
* Get entries filtered by namespace
|
|
66
|
+
*
|
|
67
|
+
* @param {string} namespace - The namespace to filter by (e.g., "aa-infra", "wallet-apis")
|
|
68
|
+
* @returns {LogEntry[]} Array of log entries matching the specified namespace
|
|
69
|
+
*/
|
|
70
|
+
getByNamespace(namespace: string): LogEntry[];
|
|
71
|
+
/**
|
|
72
|
+
* Get entries filtered by message substring
|
|
73
|
+
*
|
|
74
|
+
* @param {string} substring - The substring to search for in log messages
|
|
75
|
+
* @returns {LogEntry[]} Array of log entries containing the substring in their message
|
|
76
|
+
*/
|
|
77
|
+
getByMessage(substring: string): LogEntry[];
|
|
78
|
+
/**
|
|
79
|
+
* Get the most recent log entry
|
|
80
|
+
*
|
|
81
|
+
* @returns {LogEntry | undefined} The latest log entry, or undefined if no entries have been captured
|
|
82
|
+
*/
|
|
83
|
+
latest(): LogEntry | undefined;
|
|
84
|
+
/**
|
|
85
|
+
* Get the number of captured entries
|
|
86
|
+
*
|
|
87
|
+
* @returns {number} The total count of captured log entries
|
|
88
|
+
*/
|
|
89
|
+
get count(): number;
|
|
90
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* In-memory sink for testing purposes.
|
|
3
|
+
* Captures all log entries in an array that can be inspected in tests.
|
|
4
|
+
*
|
|
5
|
+
* **Note:** This is primarily intended for testing SDK integrations.
|
|
6
|
+
* Most applications should use the default console sink.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { InMemorySink, setGlobalLoggerConfig, createLogger } from "@alchemy/common";
|
|
11
|
+
*
|
|
12
|
+
* // In tests
|
|
13
|
+
* const sink = new InMemorySink();
|
|
14
|
+
*
|
|
15
|
+
* setGlobalLoggerConfig({
|
|
16
|
+
* sinks: [sink.sink]
|
|
17
|
+
* });
|
|
18
|
+
*
|
|
19
|
+
* const logger = createLogger({
|
|
20
|
+
* package: "@alchemy/aa-infra",
|
|
21
|
+
* version: "1.0.0",
|
|
22
|
+
* namespace: "aa-infra"
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* logger.info("test message");
|
|
26
|
+
*
|
|
27
|
+
* // Inspect captured logs
|
|
28
|
+
* expect(sink.count).toBe(1);
|
|
29
|
+
* expect(sink.latest()?.message).toBe("test message");
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export class InMemorySink {
|
|
33
|
+
/**
|
|
34
|
+
* Creates a new InMemorySink instance.
|
|
35
|
+
* Binds the sink method to the instance for use as a callback.
|
|
36
|
+
*/
|
|
37
|
+
constructor() {
|
|
38
|
+
/**
|
|
39
|
+
* Array of captured log entries
|
|
40
|
+
*/
|
|
41
|
+
Object.defineProperty(this, "entries", {
|
|
42
|
+
enumerable: true,
|
|
43
|
+
configurable: true,
|
|
44
|
+
writable: true,
|
|
45
|
+
value: []
|
|
46
|
+
});
|
|
47
|
+
this.sink = this.sink.bind(this);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The sink function to pass to setGlobalLoggerConfig.
|
|
51
|
+
* Captures log entries into the internal array.
|
|
52
|
+
*
|
|
53
|
+
* @param {LogEntry} entry - The log entry to capture
|
|
54
|
+
* @returns {void}
|
|
55
|
+
*/
|
|
56
|
+
sink(entry) {
|
|
57
|
+
this.entries.push(entry);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Clear all captured entries
|
|
61
|
+
*
|
|
62
|
+
* @returns {void}
|
|
63
|
+
*/
|
|
64
|
+
clear() {
|
|
65
|
+
this.entries = [];
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Get entries filtered by log level
|
|
69
|
+
*
|
|
70
|
+
* @param {number} level - The log level to filter by (LogLevel.ERROR, LogLevel.INFO, etc.)
|
|
71
|
+
* @returns {LogEntry[]} Array of log entries matching the specified level
|
|
72
|
+
*/
|
|
73
|
+
getByLevel(level) {
|
|
74
|
+
return this.entries.filter((e) => e.level === level);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Get entries filtered by namespace
|
|
78
|
+
*
|
|
79
|
+
* @param {string} namespace - The namespace to filter by (e.g., "aa-infra", "wallet-apis")
|
|
80
|
+
* @returns {LogEntry[]} Array of log entries matching the specified namespace
|
|
81
|
+
*/
|
|
82
|
+
getByNamespace(namespace) {
|
|
83
|
+
return this.entries.filter((e) => e.namespace === namespace);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Get entries filtered by message substring
|
|
87
|
+
*
|
|
88
|
+
* @param {string} substring - The substring to search for in log messages
|
|
89
|
+
* @returns {LogEntry[]} Array of log entries containing the substring in their message
|
|
90
|
+
*/
|
|
91
|
+
getByMessage(substring) {
|
|
92
|
+
return this.entries.filter((e) => e.message.includes(substring));
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Get the most recent log entry
|
|
96
|
+
*
|
|
97
|
+
* @returns {LogEntry | undefined} The latest log entry, or undefined if no entries have been captured
|
|
98
|
+
*/
|
|
99
|
+
latest() {
|
|
100
|
+
return this.entries[this.entries.length - 1];
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Get the number of captured entries
|
|
104
|
+
*
|
|
105
|
+
* @returns {number} The total count of captured log entries
|
|
106
|
+
*/
|
|
107
|
+
get count() {
|
|
108
|
+
return this.entries.length;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
//# sourceMappingURL=sinks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sinks.js","sourceRoot":"","sources":["../../../src/logging/sinks.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,MAAM,OAAO,YAAY;IAMvB;;;OAGG;IACH;QATA;;WAEG;QACI;;;;mBAAsB,EAAE;WAAC;QAO9B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACnC,CAAC;IAED;;;;;;OAMG;IACH,IAAI,CAAC,KAAe;QAClB,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAC3B,CAAC;IAED;;;;OAIG;IACH,KAAK;QACH,IAAI,CAAC,OAAO,GAAG,EAAE,CAAC;IACpB,CAAC;IAED;;;;;OAKG;IACH,UAAU,CAAC,KAAa;QACtB,OAAO,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,KAAK,KAAK,CAAC,CAAC;IACvD,CAAC;IAED;;;;;OAKG;IACH,cAAc,CAAC,SAAiB;QAC9B,OAAO,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,SAAS,KAAK,SAAS,CAAC,CAAC;IAC/D,CAAC;IAED;;;;;OAKG;IACH,YAAY,CAAC,SAAiB;QAC5B,OAAO,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAC;IACnE,CAAC;IAED;;;;OAIG;IACH,MAAM;QACJ,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IAC/C,CAAC;IAED;;;;OAIG;IACH,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC;IAC7B,CAAC;CACF","sourcesContent":["import type { LogEntry } from \"./config.js\";\n\n/**\n * In-memory sink for testing purposes.\n * Captures all log entries in an array that can be inspected in tests.\n *\n * **Note:** This is primarily intended for testing SDK integrations.\n * Most applications should use the default console sink.\n *\n * @example\n * ```ts\n * import { InMemorySink, setGlobalLoggerConfig, createLogger } from \"@alchemy/common\";\n *\n * // In tests\n * const sink = new InMemorySink();\n *\n * setGlobalLoggerConfig({\n * sinks: [sink.sink]\n * });\n *\n * const logger = createLogger({\n * package: \"@alchemy/aa-infra\",\n * version: \"1.0.0\",\n * namespace: \"aa-infra\"\n * });\n *\n * logger.info(\"test message\");\n *\n * // Inspect captured logs\n * expect(sink.count).toBe(1);\n * expect(sink.latest()?.message).toBe(\"test message\");\n * ```\n */\nexport class InMemorySink {\n /**\n * Array of captured log entries\n */\n public entries: LogEntry[] = [];\n\n /**\n * Creates a new InMemorySink instance.\n * Binds the sink method to the instance for use as a callback.\n */\n constructor() {\n this.sink = this.sink.bind(this);\n }\n\n /**\n * The sink function to pass to setGlobalLoggerConfig.\n * Captures log entries into the internal array.\n *\n * @param {LogEntry} entry - The log entry to capture\n * @returns {void}\n */\n sink(entry: LogEntry): void {\n this.entries.push(entry);\n }\n\n /**\n * Clear all captured entries\n *\n * @returns {void}\n */\n clear(): void {\n this.entries = [];\n }\n\n /**\n * Get entries filtered by log level\n *\n * @param {number} level - The log level to filter by (LogLevel.ERROR, LogLevel.INFO, etc.)\n * @returns {LogEntry[]} Array of log entries matching the specified level\n */\n getByLevel(level: number): LogEntry[] {\n return this.entries.filter((e) => e.level === level);\n }\n\n /**\n * Get entries filtered by namespace\n *\n * @param {string} namespace - The namespace to filter by (e.g., \"aa-infra\", \"wallet-apis\")\n * @returns {LogEntry[]} Array of log entries matching the specified namespace\n */\n getByNamespace(namespace: string): LogEntry[] {\n return this.entries.filter((e) => e.namespace === namespace);\n }\n\n /**\n * Get entries filtered by message substring\n *\n * @param {string} substring - The substring to search for in log messages\n * @returns {LogEntry[]} Array of log entries containing the substring in their message\n */\n getByMessage(substring: string): LogEntry[] {\n return this.entries.filter((e) => e.message.includes(substring));\n }\n\n /**\n * Get the most recent log entry\n *\n * @returns {LogEntry | undefined} The latest log entry, or undefined if no entries have been captured\n */\n latest(): LogEntry | undefined {\n return this.entries[this.entries.length - 1];\n }\n\n /**\n * Get the number of captured entries\n *\n * @returns {number} The total count of captured log entries\n */\n get count(): number {\n return this.entries.length;\n }\n}\n"]}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Schema definition for event logging.
|
|
3
|
+
* An array of event definitions with their names and optional data structures.
|
|
4
|
+
*/
|
|
5
|
+
export type EventsSchema = readonly {
|
|
6
|
+
EventName: string;
|
|
7
|
+
EventData?: Record<string, any>;
|
|
8
|
+
}[];
|
|
9
|
+
type Prettify<T> = {
|
|
10
|
+
[K in keyof T]: T[K];
|
|
11
|
+
} & {};
|
|
12
|
+
/**
|
|
13
|
+
* Type-safe parameters for tracking events based on the provided schema.
|
|
14
|
+
* Ensures event names and data structures match the schema definition.
|
|
15
|
+
* When no schema is provided, allows any event with optional data.
|
|
16
|
+
*
|
|
17
|
+
* @template Schema - The events schema to validate against
|
|
18
|
+
*/
|
|
19
|
+
export type TrackEventParameters<Schema extends EventsSchema> = Schema extends readonly [] ? {
|
|
20
|
+
name: string;
|
|
21
|
+
data?: any;
|
|
22
|
+
} : {
|
|
23
|
+
[K in keyof Schema]: Prettify<{
|
|
24
|
+
name: Schema[K]["EventName"];
|
|
25
|
+
} & ([undefined] extends [
|
|
26
|
+
Schema[K]["EventData"]
|
|
27
|
+
] ? {
|
|
28
|
+
data?: undefined;
|
|
29
|
+
} : {
|
|
30
|
+
data: Schema[K]["EventData"];
|
|
31
|
+
})>;
|
|
32
|
+
}[number];
|
|
33
|
+
/**
|
|
34
|
+
* Main event logger interface for type-safe event tracking and performance profiling.
|
|
35
|
+
*
|
|
36
|
+
* @template Schema - The events schema defining allowed events and their data structures
|
|
37
|
+
*/
|
|
38
|
+
export interface EventLogger<Schema extends EventsSchema = []> {
|
|
39
|
+
/**
|
|
40
|
+
* Tracks an event with type-safe validation against the schema.
|
|
41
|
+
*
|
|
42
|
+
* @param params - Event parameters including name and optional data
|
|
43
|
+
* @returns Promise that resolves when the event is tracked
|
|
44
|
+
*/
|
|
45
|
+
trackEvent(params: TrackEventParameters<Schema extends readonly [] ? [] : [...Schema, PerformanceEvent]>): Promise<void>;
|
|
46
|
+
/**
|
|
47
|
+
* Wraps a function to automatically track its execution time as a performance event.
|
|
48
|
+
*
|
|
49
|
+
* @template TArgs - Function argument types
|
|
50
|
+
* @template TRet - Function return type
|
|
51
|
+
* @param name - Name identifier for the profiled function
|
|
52
|
+
* @param func - Function to wrap with performance tracking
|
|
53
|
+
* @returns Wrapped function that tracks execution time
|
|
54
|
+
*/
|
|
55
|
+
profiled<TArgs extends any[], TRet>(name: string, func: (...args: TArgs) => TRet): (...args: TArgs) => TRet;
|
|
56
|
+
/** Internal properties for logger state and configuration */
|
|
57
|
+
_internal: {
|
|
58
|
+
/** Promise that resolves when logger is ready for use */
|
|
59
|
+
ready: Promise<unknown>;
|
|
60
|
+
/** Anonymous identifier for this logger instance */
|
|
61
|
+
anonId: string;
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Internal logger interface without the profiled method.
|
|
66
|
+
* Used internally by different logger implementations.
|
|
67
|
+
*
|
|
68
|
+
* @template Schema - The events schema defining allowed events and their data structures
|
|
69
|
+
*/
|
|
70
|
+
export type InnerLogger<Schema extends EventsSchema> = Omit<EventLogger<Schema>, "profiled">;
|
|
71
|
+
/**
|
|
72
|
+
* Context information attached to all logged events.
|
|
73
|
+
* Provides metadata about the package and version generating events.
|
|
74
|
+
*/
|
|
75
|
+
export type LoggerContext = {
|
|
76
|
+
/** Name of the package generating events */
|
|
77
|
+
package: string;
|
|
78
|
+
/** Version of the package generating events */
|
|
79
|
+
version: string;
|
|
80
|
+
/** Additional context properties as key-value pairs */
|
|
81
|
+
[key: string]: string;
|
|
82
|
+
};
|
|
83
|
+
/**
|
|
84
|
+
* Built-in performance event schema for tracking function execution times.
|
|
85
|
+
* Automatically included in all event schemas for profiled function tracking.
|
|
86
|
+
*/
|
|
87
|
+
export type PerformanceEvent = {
|
|
88
|
+
EventName: "performance";
|
|
89
|
+
EventData: {
|
|
90
|
+
/** Execution time in milliseconds */
|
|
91
|
+
executionTimeMs: number;
|
|
92
|
+
/** Name of the function being profiled */
|
|
93
|
+
functionName: string;
|
|
94
|
+
};
|
|
95
|
+
};
|
|
96
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/logging/types.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * Schema definition for event logging.\n * An array of event definitions with their names and optional data structures.\n */\nexport type EventsSchema = readonly {\n EventName: string;\n EventData?: Record<string, any>;\n}[];\n\ntype Prettify<T> = {\n [K in keyof T]: T[K];\n} & {};\n\n/**\n * Type-safe parameters for tracking events based on the provided schema.\n * Ensures event names and data structures match the schema definition.\n * When no schema is provided, allows any event with optional data.\n *\n * @template Schema - The events schema to validate against\n */\nexport type TrackEventParameters<Schema extends EventsSchema> =\n Schema extends readonly []\n ? { name: string; data?: any }\n : {\n [K in keyof Schema]: Prettify<\n { name: Schema[K][\"EventName\"] } & ([undefined] extends [\n Schema[K][\"EventData\"],\n ]\n ? { data?: undefined }\n : { data: Schema[K][\"EventData\"] })\n >;\n }[number];\n\n/**\n * Main event logger interface for type-safe event tracking and performance profiling.\n *\n * @template Schema - The events schema defining allowed events and their data structures\n */\nexport interface EventLogger<Schema extends EventsSchema = []> {\n /**\n * Tracks an event with type-safe validation against the schema.\n *\n * @param params - Event parameters including name and optional data\n * @returns Promise that resolves when the event is tracked\n */\n trackEvent(\n params: TrackEventParameters<\n Schema extends readonly [] ? [] : [...Schema, PerformanceEvent]\n >,\n ): Promise<void>;\n\n /**\n * Wraps a function to automatically track its execution time as a performance event.\n *\n * @template TArgs - Function argument types\n * @template TRet - Function return type\n * @param name - Name identifier for the profiled function\n * @param func - Function to wrap with performance tracking\n * @returns Wrapped function that tracks execution time\n */\n profiled<TArgs extends any[], TRet>(\n name: string,\n func: (...args: TArgs) => TRet,\n ): (...args: TArgs) => TRet;\n\n /** Internal properties for logger state and configuration */\n _internal: {\n /** Promise that resolves when logger is ready for use */\n ready: Promise<unknown>;\n /** Anonymous identifier for this logger instance */\n anonId: string;\n };\n}\n\n/**\n * Internal logger interface without the profiled method.\n * Used internally by different logger implementations.\n *\n * @template Schema - The events schema defining allowed events and their data structures\n */\nexport type InnerLogger<Schema extends EventsSchema> = Omit<\n EventLogger<Schema>,\n \"profiled\"\n>;\n\n/**\n * Context information attached to all logged events.\n * Provides metadata about the package and version generating events.\n */\nexport type LoggerContext = {\n /** Name of the package generating events */\n package: string;\n /** Version of the package generating events */\n version: string;\n /** Additional context properties as key-value pairs */\n [key: string]: string;\n};\n\n/**\n * Built-in performance event schema for tracking function execution times.\n * Automatically included in all event schemas for profiled function tracking.\n */\nexport type PerformanceEvent = {\n EventName: \"performance\";\n EventData: {\n /** Execution time in milliseconds */\n executionTimeMs: number;\n /** Name of the function being profiled */\n functionName: string;\n };\n};\n"]}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Detects if the current environment is in development mode.
|
|
3
|
+
* Checks multiple common development environment indicators.
|
|
4
|
+
*
|
|
5
|
+
* @returns {boolean} True if running in development mode
|
|
6
|
+
*/
|
|
7
|
+
export function isClientDevMode() {
|
|
8
|
+
if (typeof __DEV__ !== "undefined" && __DEV__) {
|
|
9
|
+
return true;
|
|
10
|
+
}
|
|
11
|
+
if (typeof process !== "undefined" &&
|
|
12
|
+
process.env.NODE_ENV === "development") {
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
if (typeof window !== "undefined" &&
|
|
16
|
+
window.location?.hostname?.includes("localhost")) {
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/logging/utils.ts"],"names":[],"mappings":"AAIA;;;;;GAKG;AACH,MAAM,UAAU,eAAe;IAC7B,IAAI,OAAO,OAAO,KAAK,WAAW,IAAI,OAAO,EAAE,CAAC;QAC9C,OAAO,IAAI,CAAC;IACd,CAAC;IAED,IACE,OAAO,OAAO,KAAK,WAAW;QAC9B,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,aAAa,EACtC,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAED,IACE,OAAO,MAAM,KAAK,WAAW;QAC7B,MAAM,CAAC,QAAQ,EAAE,QAAQ,EAAE,QAAQ,CAAC,WAAW,CAAC,EAChD,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["// just in case we're in a setting that doesn't have these types defined\ndeclare const __DEV__: boolean | undefined;\ndeclare const window: Window | undefined;\n\n/**\n * Detects if the current environment is in development mode.\n * Checks multiple common development environment indicators.\n *\n * @returns {boolean} True if running in development mode\n */\nexport function isClientDevMode() {\n if (typeof __DEV__ !== \"undefined\" && __DEV__) {\n return true;\n }\n\n if (\n typeof process !== \"undefined\" &&\n process.env.NODE_ENV === \"development\"\n ) {\n return true;\n }\n\n if (\n typeof window !== \"undefined\" &&\n window.location?.hostname?.includes(\"localhost\")\n ) {\n return true;\n }\n\n return false;\n}\n"]}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { RestRequestFn, RestRequestSchema } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Parameters for creating an AlchemyRestClient instance.
|
|
4
|
+
*/
|
|
5
|
+
export type AlchemyRestClientParams = {
|
|
6
|
+
/** API key for Alchemy authentication */
|
|
7
|
+
apiKey?: string;
|
|
8
|
+
/** JWT token for Alchemy authentication */
|
|
9
|
+
jwt?: string;
|
|
10
|
+
/** Custom URL (optional - defaults to Alchemy's chain-agnostic URL, but can be used to override it) */
|
|
11
|
+
url?: string;
|
|
12
|
+
/** Custom headers to be sent with requests */
|
|
13
|
+
headers?: HeadersInit;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* A client for making requests to Alchemy's non-JSON-RPC endpoints.
|
|
17
|
+
*/
|
|
18
|
+
export declare class AlchemyRestClient<Schema extends RestRequestSchema> {
|
|
19
|
+
private readonly url;
|
|
20
|
+
private readonly headers;
|
|
21
|
+
/**
|
|
22
|
+
* Creates a new instance of AlchemyRestClient.
|
|
23
|
+
*
|
|
24
|
+
* @param {AlchemyRestClientParams} params - The parameters for configuring the client, including API key, JWT, custom URL, and headers.
|
|
25
|
+
*/
|
|
26
|
+
constructor({ apiKey, jwt, url, headers }: AlchemyRestClientParams);
|
|
27
|
+
/**
|
|
28
|
+
* Makes an HTTP request to an Alchemy non-JSON-RPC endpoint.
|
|
29
|
+
*
|
|
30
|
+
* @param {RestRequestFn<Schema>} params - The parameters for the request
|
|
31
|
+
* @returns {Promise<unknown>} The response from the request
|
|
32
|
+
*/
|
|
33
|
+
request: RestRequestFn<Schema>;
|
|
34
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { FetchError } from "../errors/FetchError.js";
|
|
2
|
+
import { ServerError } from "../errors/ServerError.js";
|
|
3
|
+
import { withAlchemyHeaders } from "../utils/headers.js";
|
|
4
|
+
const ALCHEMY_API_URL = "https://api.g.alchemy.com";
|
|
5
|
+
/**
|
|
6
|
+
* A client for making requests to Alchemy's non-JSON-RPC endpoints.
|
|
7
|
+
*/
|
|
8
|
+
export class AlchemyRestClient {
|
|
9
|
+
/**
|
|
10
|
+
* Creates a new instance of AlchemyRestClient.
|
|
11
|
+
*
|
|
12
|
+
* @param {AlchemyRestClientParams} params - The parameters for configuring the client, including API key, JWT, custom URL, and headers.
|
|
13
|
+
*/
|
|
14
|
+
constructor({ apiKey, jwt, url, headers }) {
|
|
15
|
+
Object.defineProperty(this, "url", {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
configurable: true,
|
|
18
|
+
writable: true,
|
|
19
|
+
value: void 0
|
|
20
|
+
});
|
|
21
|
+
Object.defineProperty(this, "headers", {
|
|
22
|
+
enumerable: true,
|
|
23
|
+
configurable: true,
|
|
24
|
+
writable: true,
|
|
25
|
+
value: void 0
|
|
26
|
+
});
|
|
27
|
+
/**
|
|
28
|
+
* Makes an HTTP request to an Alchemy non-JSON-RPC endpoint.
|
|
29
|
+
*
|
|
30
|
+
* @param {RestRequestFn<Schema>} params - The parameters for the request
|
|
31
|
+
* @returns {Promise<unknown>} The response from the request
|
|
32
|
+
*/
|
|
33
|
+
Object.defineProperty(this, "request", {
|
|
34
|
+
enumerable: true,
|
|
35
|
+
configurable: true,
|
|
36
|
+
writable: true,
|
|
37
|
+
value: async (params) => {
|
|
38
|
+
const response = await fetch(`${this.url}/${params.route}`, {
|
|
39
|
+
method: params.method,
|
|
40
|
+
body: params.body ? JSON.stringify(params.body) : undefined,
|
|
41
|
+
headers: this.headers,
|
|
42
|
+
}).catch((error) => {
|
|
43
|
+
throw new FetchError(params.route, params.method, error);
|
|
44
|
+
});
|
|
45
|
+
if (!response.ok) {
|
|
46
|
+
throw new ServerError(await response.text(), response.status, new Error(response.statusText));
|
|
47
|
+
}
|
|
48
|
+
return response.json();
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
this.url = url ?? ALCHEMY_API_URL;
|
|
52
|
+
this.headers = new Headers(withAlchemyHeaders({ headers, apiKey, jwt }));
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=restClient.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"restClient.js","sourceRoot":"","sources":["../../../src/rest/restClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AACvD,OAAO,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAGzD,MAAM,eAAe,GAAG,2BAA2B,CAAC;AAgBpD;;GAEG;AACH,MAAM,OAAO,iBAAiB;IAI5B;;;;OAIG;IACH,YAAY,EAAE,MAAM,EAAE,GAAG,EAAE,GAAG,EAAE,OAAO,EAA2B;QARjD;;;;;WAAY;QACZ;;;;;WAAiB;QAYlC;;;;;WAKG;QACI;;;;mBAAiC,KAAK,EAAE,MAAM,EAAE,EAAE;gBACvD,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,IAAI,CAAC,GAAG,IAAI,MAAM,CAAC,KAAK,EAAE,EAAE;oBAC1D,MAAM,EAAE,MAAM,CAAC,MAAM;oBACrB,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS;oBAC3D,OAAO,EAAE,IAAI,CAAC,OAAO;iBACtB,CAAC,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;oBACjB,MAAM,IAAI,UAAU,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;gBAC3D,CAAC,CAAC,CAAC;gBAEH,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;oBACjB,MAAM,IAAI,WAAW,CACnB,MAAM,QAAQ,CAAC,IAAI,EAAE,EACrB,QAAQ,CAAC,MAAM,EACf,IAAI,KAAK,CAAC,QAAQ,CAAC,UAAU,CAAC,CAC/B,CAAC;gBACJ,CAAC;gBAED,OAAO,QAAQ,CAAC,IAAI,EAAE,CAAC;YACzB,CAAC;WAAC;QA5BA,IAAI,CAAC,GAAG,GAAG,GAAG,IAAI,eAAe,CAAC;QAClC,IAAI,CAAC,OAAO,GAAG,IAAI,OAAO,CAAC,kBAAkB,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC;IAC3E,CAAC;CA2BF","sourcesContent":["import { FetchError } from \"../errors/FetchError.js\";\nimport { ServerError } from \"../errors/ServerError.js\";\nimport { withAlchemyHeaders } from \"../utils/headers.js\";\nimport type { RestRequestFn, RestRequestSchema } from \"./types.js\";\n\nconst ALCHEMY_API_URL = \"https://api.g.alchemy.com\";\n\n/**\n * Parameters for creating an AlchemyRestClient instance.\n */\nexport type AlchemyRestClientParams = {\n /** API key for Alchemy authentication */\n apiKey?: string;\n /** JWT token for Alchemy authentication */\n jwt?: string;\n /** Custom URL (optional - defaults to Alchemy's chain-agnostic URL, but can be used to override it) */\n url?: string;\n /** Custom headers to be sent with requests */\n headers?: HeadersInit;\n};\n\n/**\n * A client for making requests to Alchemy's non-JSON-RPC endpoints.\n */\nexport class AlchemyRestClient<Schema extends RestRequestSchema> {\n private readonly url: string;\n private readonly headers: Headers;\n\n /**\n * Creates a new instance of AlchemyRestClient.\n *\n * @param {AlchemyRestClientParams} params - The parameters for configuring the client, including API key, JWT, custom URL, and headers.\n */\n constructor({ apiKey, jwt, url, headers }: AlchemyRestClientParams) {\n this.url = url ?? ALCHEMY_API_URL;\n this.headers = new Headers(withAlchemyHeaders({ headers, apiKey, jwt }));\n }\n\n /**\n * Makes an HTTP request to an Alchemy non-JSON-RPC endpoint.\n *\n * @param {RestRequestFn<Schema>} params - The parameters for the request\n * @returns {Promise<unknown>} The response from the request\n */\n public request: RestRequestFn<Schema> = async (params) => {\n const response = await fetch(`${this.url}/${params.route}`, {\n method: params.method,\n body: params.body ? JSON.stringify(params.body) : undefined,\n headers: this.headers,\n }).catch((error) => {\n throw new FetchError(params.route, params.method, error);\n });\n\n if (!response.ok) {\n throw new ServerError(\n await response.text(),\n response.status,\n new Error(response.statusText),\n );\n }\n\n return response.json();\n };\n}\n"]}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Prettify } from "viem";
|
|
2
|
+
export type RestRequestSchema = readonly {
|
|
3
|
+
Route: string;
|
|
4
|
+
Method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "OPTIONS" | "HEAD";
|
|
5
|
+
Body?: unknown | undefined;
|
|
6
|
+
Response: unknown;
|
|
7
|
+
}[];
|
|
8
|
+
export type RestRequestParams<Schema extends RestRequestSchema | undefined = undefined> = Schema extends RestRequestSchema ? {
|
|
9
|
+
[K in keyof Schema]: Prettify<{
|
|
10
|
+
route: Schema[K] extends Schema[number] ? Schema[K]["Route"] : never;
|
|
11
|
+
method: Schema[K] extends Schema[number] ? Schema[K]["Method"] : never;
|
|
12
|
+
} & (Schema[K] extends Schema[number] ? Schema[K]["Body"] extends undefined ? {
|
|
13
|
+
body?: undefined;
|
|
14
|
+
} : {
|
|
15
|
+
body: Schema[K]["Body"];
|
|
16
|
+
} : never)>;
|
|
17
|
+
}[number] : {
|
|
18
|
+
route: string;
|
|
19
|
+
method: RestRequestSchema[number]["Method"];
|
|
20
|
+
body?: unknown | undefined;
|
|
21
|
+
};
|
|
22
|
+
export type RestRequestFn<Schema extends RestRequestSchema | undefined = undefined> = <_parameters extends RestRequestParams<Schema> = RestRequestParams<Schema>, _returnType = Schema extends RestRequestSchema ? Extract<Schema[number], {
|
|
23
|
+
Route: _parameters["route"];
|
|
24
|
+
}>["Response"] : unknown>(params: _parameters) => Promise<_returnType>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/rest/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { Prettify } from \"viem\";\n\nexport type RestRequestSchema = readonly {\n Route: string;\n Method: \"GET\" | \"POST\" | \"PUT\" | \"DELETE\" | \"PATCH\" | \"OPTIONS\" | \"HEAD\";\n Body?: unknown | undefined;\n Response: unknown;\n}[];\n\nexport type RestRequestParams<\n Schema extends RestRequestSchema | undefined = undefined,\n> = Schema extends RestRequestSchema\n ? {\n [K in keyof Schema]: Prettify<\n {\n route: Schema[K] extends Schema[number] ? Schema[K][\"Route\"] : never;\n method: Schema[K] extends Schema[number]\n ? Schema[K][\"Method\"]\n : never;\n } & (Schema[K] extends Schema[number]\n ? Schema[K][\"Body\"] extends undefined\n ? { body?: undefined }\n : { body: Schema[K][\"Body\"] }\n : never)\n >;\n }[number]\n : {\n route: string;\n method: RestRequestSchema[number][\"Method\"];\n body?: unknown | undefined;\n };\n\nexport type RestRequestFn<\n Schema extends RestRequestSchema | undefined = undefined,\n> = <\n _parameters extends RestRequestParams<Schema> = RestRequestParams<Schema>,\n _returnType = Schema extends RestRequestSchema\n ? Extract<Schema[number], { Route: _parameters[\"route\"] }>[\"Response\"]\n : unknown,\n>(\n params: _parameters,\n) => Promise<_returnType>;\n"]}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* These are the headers that are used in the trace headers, could be found in the spec
|
|
3
|
+
*
|
|
4
|
+
* @see https://www.w3.org/TR/trace-context/#design-overview
|
|
5
|
+
*/
|
|
6
|
+
export declare const TRACE_HEADER_NAME = "traceparent";
|
|
7
|
+
/**
|
|
8
|
+
* These are the headers that are used in the trace headers, could be found in the spec
|
|
9
|
+
*
|
|
10
|
+
* @see https://www.w3.org/TR/trace-context/#design-overview
|
|
11
|
+
*/
|
|
12
|
+
export declare const TRACE_HEADER_STATE = "tracestate";
|
|
13
|
+
/**
|
|
14
|
+
* Some tools that are useful when dealing with the values
|
|
15
|
+
* of the trace header. Follows the W3C trace context standard.
|
|
16
|
+
*
|
|
17
|
+
* @see https://www.w3.org/TR/trace-context/
|
|
18
|
+
*/
|
|
19
|
+
export declare class TraceHeader {
|
|
20
|
+
readonly traceId: string;
|
|
21
|
+
readonly parentId: string;
|
|
22
|
+
readonly traceFlags: string;
|
|
23
|
+
readonly traceState: Record<string, string>;
|
|
24
|
+
/**
|
|
25
|
+
* Initializes a new instance with the provided trace identifiers and state information.
|
|
26
|
+
*
|
|
27
|
+
* @param {string} traceId The unique identifier for the trace
|
|
28
|
+
* @param {string} parentId The identifier of the parent trace
|
|
29
|
+
* @param {string} traceFlags Flags containing trace-related options
|
|
30
|
+
* @param {TraceHeader["traceState"]} traceState The trace state information for additional trace context
|
|
31
|
+
*/
|
|
32
|
+
constructor(traceId: string, parentId: string, traceFlags: string, traceState: TraceHeader["traceState"]);
|
|
33
|
+
/**
|
|
34
|
+
* Creating a default trace id that is a random setup for both trace id and parent id
|
|
35
|
+
*
|
|
36
|
+
* @example ```ts
|
|
37
|
+
* const traceHeader = TraceHeader.fromTraceHeader(headers) || TraceHeader.default();
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @returns {TraceHeader} A default trace header
|
|
41
|
+
*/
|
|
42
|
+
static default(): TraceHeader;
|
|
43
|
+
/**
|
|
44
|
+
* Should be able to consume a trace header from the headers of an http request
|
|
45
|
+
*
|
|
46
|
+
* @example ```ts
|
|
47
|
+
* const traceHeader = TraceHeader.fromTraceHeader(headers);
|
|
48
|
+
* ```
|
|
49
|
+
*
|
|
50
|
+
* @param {Record<string,string>} headers The headers from the http request
|
|
51
|
+
* @returns {TraceHeader | undefined} The trace header object, or nothing if not found
|
|
52
|
+
*/
|
|
53
|
+
static fromTraceHeader(headers: Record<string, string>): TraceHeader | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* Should be able to convert the trace header to the format that is used in the headers of an http request
|
|
56
|
+
*
|
|
57
|
+
* @example ```ts
|
|
58
|
+
* const traceHeader = TraceHeader.fromTraceHeader(headers) || TraceHeader.default();
|
|
59
|
+
* const headers = traceHeader.toTraceHeader();
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* @returns {{traceparent: string, tracestate: string}} The trace header in the format of a record, used in our http client
|
|
63
|
+
*/
|
|
64
|
+
toTraceHeader(): {
|
|
65
|
+
readonly traceparent: `00-${string}-${string}-${string}`;
|
|
66
|
+
readonly tracestate: string;
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* Should be able to create a new trace header with a new event in the trace state,
|
|
70
|
+
* as the key of the eventName as breadcrumbs appending onto previous breadcrumbs with the - infix if exists. And the
|
|
71
|
+
* trace parent gets updated as according to the docs
|
|
72
|
+
*
|
|
73
|
+
* @example ```ts
|
|
74
|
+
* const traceHeader = TraceHeader.fromTraceHeader(headers) || TraceHeader.default();
|
|
75
|
+
* const newTraceHeader = traceHeader.withEvent("newEvent");
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* @param {string} eventName The key of the new event
|
|
79
|
+
* @returns {TraceHeader} The new trace header
|
|
80
|
+
*/
|
|
81
|
+
withEvent(eventName: string): TraceHeader;
|
|
82
|
+
}
|