@ubuligan/shared 0.1.0 → 0.1.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/README.md +67 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# @ubuligan/shared
|
|
2
|
+
|
|
3
|
+
Shared, **dependency-free** types and utilities for the [MCP Toolkit](https://github.com/jsznpm/create-mcp-toolkit).
|
|
4
|
+
Browser-safe — no MCP SDK import — so it can be used from both Node and the browser.
|
|
5
|
+
|
|
6
|
+
It is the base layer that [`@ubuligan/client`](https://www.npmjs.com/package/@ubuligan/client),
|
|
7
|
+
[`@ubuligan/server`](https://www.npmjs.com/package/@ubuligan/server) and
|
|
8
|
+
[`@ubuligan/react`](https://www.npmjs.com/package/@ubuligan/react) build on. You rarely install it
|
|
9
|
+
directly — it comes in as a transitive dependency — but its types and helpers are useful on their own.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install @ubuligan/shared
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## What's inside
|
|
18
|
+
|
|
19
|
+
| Export | Kind | Purpose |
|
|
20
|
+
| --- | --- | --- |
|
|
21
|
+
| `createConsoleLogger(level)` | fn | A leveled console logger (`debug` / `info` / `warn` / `error`). |
|
|
22
|
+
| `noopLogger` | const | A logger that discards everything (default when none is given). |
|
|
23
|
+
| `Logger`, `LogLevel` | type | The logger interface and the level union. |
|
|
24
|
+
| `withRetry(fn, opts, logger, label)` | fn | Run an async fn with exponential-backoff + jitter retries. |
|
|
25
|
+
| `RetryOptions` | type | `{ retries, minDelayMs, maxDelayMs, shouldRetry }`. |
|
|
26
|
+
| `resolveAuthHeaders(auth)` | fn | Turn an `AuthConfig` into HTTP headers (async). |
|
|
27
|
+
| `AuthConfig` | type | `{ type: "bearer" }` or `{ type: "headers" }` (values may be async getters). |
|
|
28
|
+
| `TransportConfig` | type | Union of `StdioTransportConfig` and `HttpTransportConfig`. |
|
|
29
|
+
|
|
30
|
+
## Usage
|
|
31
|
+
|
|
32
|
+
### Logging
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import { createConsoleLogger } from "@ubuligan/shared";
|
|
36
|
+
|
|
37
|
+
const log = createConsoleLogger("info"); // logs info+ to the console
|
|
38
|
+
log.debug("hidden"); // below level → dropped
|
|
39
|
+
log.info("connecting", { url });
|
|
40
|
+
log.error("failed", err);
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Retry
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
import { withRetry, createConsoleLogger } from "@ubuligan/shared";
|
|
47
|
+
|
|
48
|
+
const data = await withRetry(
|
|
49
|
+
() => fetch(url).then((r) => r.json()),
|
|
50
|
+
{ retries: 3, minDelayMs: 200, maxDelayMs: 5000 },
|
|
51
|
+
createConsoleLogger("warn"),
|
|
52
|
+
"fetch-data", // label shown in retry logs
|
|
53
|
+
);
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Auth headers
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
import { resolveAuthHeaders } from "@ubuligan/shared";
|
|
60
|
+
|
|
61
|
+
const headers = resolveAuthHeaders({ type: "bearer", token: process.env.TOKEN! });
|
|
62
|
+
// → { Authorization: "Bearer ..." }
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## License
|
|
66
|
+
|
|
67
|
+
MIT
|