@gobing-ai/ts-utils 0.8.8 → 0.9.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/README.md +45 -9
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @gobing-ai/ts-utils
|
|
2
2
|
|
|
3
|
-
Shared utilities — error types, date helpers, cursor-based pagination, role-based access control, API response builders, and
|
|
3
|
+
Shared utilities — error types, date helpers, cursor-based pagination, role-based access control, API response builders, environment/output/exit seams, object helpers, and event severity types. Zero runtime dependencies; core modules work in Bun, Node, and Cloudflare Workers (`env` is Node/Bun only).
|
|
4
4
|
|
|
5
5
|
## Overview
|
|
6
6
|
|
|
@@ -8,14 +8,17 @@ Shared utilities — error types, date helpers, cursor-based pagination, role-ba
|
|
|
8
8
|
|
|
9
9
|
| Module | Purpose |
|
|
10
10
|
|--------|---------|
|
|
11
|
-
| `errors` | Typed application errors (`AppError`, `NotFoundError`, `ValidationError`,
|
|
12
|
-
| `date` | `nowMs()`, timestamp ↔ Date conversion |
|
|
13
|
-
| `cursor` | Base64url-encoded cursor-based pagination |
|
|
11
|
+
| `errors` | Typed application errors (`AppError`, `NotFoundError`, `ValidationError`, `ConflictError`, `InternalError`, `isAppError`) |
|
|
12
|
+
| `date` | `nowMs()`, timestamp ↔ Date conversion (`toMs`, `fromMs`) |
|
|
13
|
+
| `cursor` | Base64url-encoded cursor-based pagination (`createCursor`, `parseCursor`, `encodeCursorFromItem`, `decodeAndParseCursor`, `buildCursorMeta`) |
|
|
14
14
|
| `access` | Zitadel + generic role-based access control (`hasRole`, `getRoles`) |
|
|
15
|
-
| `api-response` | Standard API response envelope builders |
|
|
16
|
-
| `
|
|
17
|
-
| `
|
|
18
|
-
| `
|
|
15
|
+
| `api-response` | Standard API response envelope builders (`successResponse`, `errorResponse`, `toApiResponse`, etc.) |
|
|
16
|
+
| `env` | Sanctioned `process.env` gateway (`getEnvVar`, `getEnvVars`, `setEnvVar`, `removeEnvVar`, `getAppOptions`) — Node/Bun only |
|
|
17
|
+
| `output` | Sanctioned stdout/stderr & `process.exit` seams (`echo`, `echoError`, `exitProcess`, `createBufferTarget`, `setDefaultOutputTargets`, `setDefaultExitTarget`) |
|
|
18
|
+
| `object` | Prototype-pollution-safe plain-object helpers (`isPlainObject`, `deepMerge`, `flattenKeys`, `deFlattenKeys`) |
|
|
19
|
+
| `event-severity` | Observability severity types (`EventSeverity`, `WithEventSeverity`) |
|
|
20
|
+
| `origin` | Request origin pattern matching & CORS validation (`matchOriginPattern`, `isAllowedOrigin`, `getValidatedOrigin`) |
|
|
21
|
+
| `const` | Shared constants (`LOG_CATEGORY_APP`, `LOG_CATEGORY_CLI`) |
|
|
19
22
|
|
|
20
23
|
## Architecture
|
|
21
24
|
|
|
@@ -175,6 +178,39 @@ fromMs(1704067200000); // → Date('2024-01-01')
|
|
|
175
178
|
fromMs(null); // → null
|
|
176
179
|
```
|
|
177
180
|
|
|
181
|
+
### Environment-variable gateway (`env`)
|
|
182
|
+
|
|
183
|
+
`env` is the sanctioned funnel for `process.env` access across Node/Bun packages (do not use in Cloudflare Workers, where `process` is absent):
|
|
184
|
+
|
|
185
|
+
```ts
|
|
186
|
+
import { getEnvVar, setEnvVar, removeEnvVar, getAppOptions } from '@gobing-ai/ts-utils';
|
|
187
|
+
|
|
188
|
+
const token = getEnvVar('API_TOKEN', 'default');
|
|
189
|
+
setEnvVar('FEATURE_FLAG', '1');
|
|
190
|
+
removeEnvVar('FEATURE_FLAG');
|
|
191
|
+
|
|
192
|
+
// Read a typed option from a bootstrap.options-style config bag:
|
|
193
|
+
const retries = getAppOptions(config, 'retries', 3);
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
### Output & exit seams (`output`)
|
|
197
|
+
|
|
198
|
+
`echo`, `echoError`, and `exitProcess` wrap `process.stdout`, `process.stderr`, and `process.exit` behind injectable targets (`WriteTarget`, `ExitTarget`) so CLI code stays testable with `createBufferTarget()`, `setDefaultOutputTargets()`, and `setDefaultExitTarget()`.
|
|
199
|
+
|
|
200
|
+
```ts
|
|
201
|
+
import { echo, echoError, exitProcess, createBufferTarget, setDefaultOutputTargets } from '@gobing-ai/ts-utils';
|
|
202
|
+
|
|
203
|
+
const buf = createBufferTarget();
|
|
204
|
+
const restore = setDefaultOutputTargets({ stdout: buf });
|
|
205
|
+
echo('hello');
|
|
206
|
+
restore();
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
### Object & event severity helpers
|
|
210
|
+
|
|
211
|
+
- `isPlainObject`, `deepMerge`, `flattenKeys`, `deFlattenKeys` — recursive merge and dot-path flattening with prototype-pollution guards (`__proto__`, `constructor`, `prototype`).
|
|
212
|
+
- `EventSeverity` (`'info' | 'warning' | 'error'`) and `WithEventSeverity` — producer-owned severity mixin for observability events.
|
|
213
|
+
|
|
178
214
|
## Usage
|
|
179
215
|
|
|
180
216
|
### Install
|
|
@@ -214,7 +250,7 @@ async function listUsers(limit: number, cursor?: string) {
|
|
|
214
250
|
// Auth guard
|
|
215
251
|
function requireAdmin(profile: unknown) {
|
|
216
252
|
if (!hasRole(profile as Record<string, unknown>, 'admin')) {
|
|
217
|
-
throw new
|
|
253
|
+
throw new ValidationError('Admin access required');
|
|
218
254
|
}
|
|
219
255
|
}
|
|
220
256
|
```
|