@kalutskii/foundation 2.0.0 → 2.0.6
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 +13 -347
- package/dist/index.d.ts +939 -737
- package/dist/index.js +1009 -678
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
# @kalutskii/foundation
|
|
2
2
|
|
|
3
|
-
Shared TypeScript
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
This repository is not intended to document every exported function through standalone usage snippets.
|
|
7
|
-
Public JSDoc, generated declarations, colocated specifications, and editor inference are the API reference.
|
|
3
|
+
Shared TypeScript foundations for reusable contracts, validation, framework adapters, and utilities.
|
|
4
|
+
Designed for Bun, Node.js, Hono, Drizzle ORM, and Zod applications.
|
|
8
5
|
|
|
9
6
|
## Installation
|
|
10
7
|
|
|
@@ -12,354 +9,23 @@ Public JSDoc, generated declarations, colocated specifications, and editor infer
|
|
|
12
9
|
bun add @kalutskii/foundation
|
|
13
10
|
```
|
|
14
11
|
|
|
15
|
-
|
|
16
|
-
npm install @kalutskii/foundation
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
## Package scope
|
|
20
|
-
|
|
21
|
-
The package contains several deliberately isolated areas:
|
|
22
|
-
|
|
23
|
-
| Module | Responsibility |
|
|
24
|
-
| ---------------- | ----------------------------------------------------------------------------------------- |
|
|
25
|
-
| `utilities` | Framework-independent datetime, enum, execution, generation, logging, and type utilities. |
|
|
26
|
-
| `http` | Shared HTTP result contracts, factories, status constants, and result resolvers. |
|
|
27
|
-
| `upload` | Shared upload presets, file-format metadata, validation, and reusable Zod schemas. |
|
|
28
|
-
| `zod-validation` | Generic Zod parsing, validation, refinement, and related type utilities. |
|
|
29
|
-
| `zod-search` | Reusable search and pagination contracts composed from lower-level Zod primitives. |
|
|
30
|
-
| `zod-bulk` | Include/exclude selection contracts shared by frontend and backend bulk operations. |
|
|
31
|
-
| `hono` | Hono-specific response, file response, error handling, and request logging adapters. |
|
|
32
|
-
| `hmac` | Web Crypto HMAC signing, verification, algorithms, and signature encoding. |
|
|
33
|
-
| `drizzle` | Drizzle-specific SQL composition that must not leak into generic contract modules. |
|
|
34
|
-
| `zod-jwt` | JWT service integration with optional Zod validation of decoded payloads. |
|
|
35
|
-
|
|
36
|
-
The root `src/index.ts` is the only public package entrypoint. Internal file paths are implementation details
|
|
37
|
-
and should not be imported directly by consumers of `@kalutskii/foundation`.
|
|
38
|
-
|
|
39
|
-
## Architecture
|
|
40
|
-
|
|
41
|
-
### Dependency hierarchy
|
|
42
|
-
|
|
43
|
-
Dependencies must flow from specialized components toward smaller and more generic foundations:
|
|
44
|
-
|
|
45
|
-
```text
|
|
46
|
-
Framework adapters
|
|
47
|
-
hono / drizzle / zod-jwt
|
|
48
|
-
│
|
|
49
|
-
▼
|
|
50
|
-
Contract composition
|
|
51
|
-
zod-search / zod-bulk / upload
|
|
52
|
-
│
|
|
53
|
-
▼
|
|
54
|
-
Contract primitives
|
|
55
|
-
http / zod-validation
|
|
56
|
-
│
|
|
57
|
-
▼
|
|
58
|
-
Generic utilities
|
|
59
|
-
utilities
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
This diagram defines direction, not a requirement for every module to depend on the layer below it.
|
|
63
|
-
Independent modules should remain independent instead of introducing artificial shared abstractions.
|
|
64
|
-
|
|
65
|
-
### Dependency rules
|
|
66
|
-
|
|
67
|
-
1. Generic utilities must not depend on Hono, Drizzle, JWT services, or application contracts.
|
|
68
|
-
2. Generic Zod modules must not acquire hidden framework dependencies through imported helper types.
|
|
69
|
-
3. Framework adapters may consume contracts and utilities, but contracts must not import adapters back.
|
|
70
|
-
4. Sibling modules should communicate through public concepts instead of reaching into private internals.
|
|
71
|
-
5. External dependencies should stay inside the narrowest module that genuinely requires their behavior.
|
|
72
|
-
6. Type-only dependencies follow the same architectural rules as runtime dependencies.
|
|
73
|
-
7. A reusable abstraction belongs to the lowest layer that can own it without knowing its consumers.
|
|
74
|
-
|
|
75
|
-
A dependency that appears only in generated declarations is still a real dependency and must be reviewed.
|
|
76
|
-
For example, a generic Zod type importing `Simplify` from Drizzle would violate the hierarchy even with no runtime cost.
|
|
77
|
-
|
|
78
|
-
### Responsibility boundaries
|
|
79
|
-
|
|
80
|
-
Each module owns one coherent domain. A module may contain several files, but every file must have a narrow role.
|
|
81
|
-
Avoid generic dumping grounds such as `common`, `helpers`, `shared`, `misc`, or `stuff` without a concrete domain.
|
|
82
|
-
|
|
83
|
-
A change belongs in an existing module only when that module can explain and enforce its complete semantics.
|
|
84
|
-
Otherwise, create a dedicated module instead of growing an unrelated file with another responsibility.
|
|
85
|
-
|
|
86
|
-
## Project structure
|
|
87
|
-
|
|
88
|
-
```text
|
|
89
|
-
src/
|
|
90
|
-
├── drizzle/
|
|
91
|
-
├── hmac/
|
|
92
|
-
├── hono/
|
|
93
|
-
├── http/
|
|
94
|
-
├── upload/
|
|
95
|
-
├── utilities/
|
|
96
|
-
├── zod-bulk/
|
|
97
|
-
├── zod-jwt/
|
|
98
|
-
├── zod-search/
|
|
99
|
-
├── zod-validation/
|
|
100
|
-
└── index.ts
|
|
101
|
-
```
|
|
102
|
-
|
|
103
|
-
Modules are directories. Files inside them follow the `<module>.<responsibility>.ts` pattern:
|
|
104
|
-
|
|
105
|
-
```text
|
|
106
|
-
zod-search/
|
|
107
|
-
├── zod-search.pagination.schemas.ts
|
|
108
|
-
├── zod-search.schemas.ts
|
|
109
|
-
├── zod-search.types.ts
|
|
110
|
-
└── zod-search.spec.ts
|
|
111
|
-
```
|
|
12
|
+
## Modules
|
|
112
13
|
|
|
113
|
-
|
|
114
|
-
|
|
14
|
+
- **Generic:** `base64`, `datetime`, `execution`, `object`, `random`, `string-enum`, `type`.
|
|
15
|
+
- **Contracts:** `response`, `upload`, `zod`.
|
|
16
|
+
- **Security:** `hmac`, `jwt`.
|
|
17
|
+
- **Adapters:** `drizzle`, `hono`, `logging`.
|
|
115
18
|
|
|
116
|
-
|
|
19
|
+
All supported APIs are exported from the package root. Generated declarations, public JSDoc, and colocated
|
|
20
|
+
specifications are the API reference; internal source paths are not public entrypoints.
|
|
117
21
|
|
|
118
|
-
|
|
119
|
-
| ----------------- | ------------------------------------------------------------------------- |
|
|
120
|
-
| `*.constants.ts` | Immutable configuration values and metadata without behavior. |
|
|
121
|
-
| `*.enums.ts` | Literal collections, derived unions, ergonomic records, and aliases. |
|
|
122
|
-
| `*.schemas.ts` | Runtime Zod schemas and factories whose result is a schema. |
|
|
123
|
-
| `*.types.ts` | Type aliases, generic contracts, and schema-derived output types. |
|
|
124
|
-
| `*.services.ts` | Stateful service classes that coordinate one external capability. |
|
|
125
|
-
| `*.validation.ts` | Ordered validation behavior returning stable domain error keys. |
|
|
126
|
-
| `*.factory.ts` | Functions whose primary responsibility is constructing non-schema values. |
|
|
127
|
-
| `*.presets.ts` | Ready-to-use policies composed from public domain values. |
|
|
128
|
-
| `*.resolvers.ts` | Functions that unwrap, normalize, or translate an existing result. |
|
|
129
|
-
| `*.utilities.ts` | Stateless reusable behavior that has no narrower architectural owner. |
|
|
130
|
-
| `*.parsing.ts` | Input parsing and preprocessing before domain validation. |
|
|
131
|
-
| `*.refiners.ts` | Refinement logic that narrows or safely composes an existing value. |
|
|
132
|
-
| `*.execution.ts` | Framework lifecycle execution and error-boundary behavior. |
|
|
133
|
-
| `*.logging.ts` | Logging formatters, sinks, or middleware behavior. |
|
|
134
|
-
| `*.respond.ts` | Framework response construction and response-specific contracts. |
|
|
135
|
-
| `*.spec.ts` | The single colocated runtime and compile-time specification for a module. |
|
|
136
|
-
|
|
137
|
-
Do not place TypeScript-only contracts in a schema file when they can be separated without creating a circular
|
|
138
|
-
responsibility. Do not split tiny files mechanically either: separation must communicate ownership, not line count.
|
|
139
|
-
|
|
140
|
-
## Naming conventions
|
|
141
|
-
|
|
142
|
-
### Files and directories
|
|
143
|
-
|
|
144
|
-
- Module directories use kebab case: `zod-search`, `zod-validation`.
|
|
145
|
-
- Source files repeat the module name and add a responsibility suffix: `http.resolvers.ts`.
|
|
146
|
-
- Specifications use the module name and `.spec.ts`: `hono.spec.ts`, `zod-bulk.spec.ts`.
|
|
147
|
-
- Avoid names that describe implementation history, temporary state, or vague grouping.
|
|
148
|
-
|
|
149
|
-
### Runtime symbols
|
|
150
|
-
|
|
151
|
-
- Zod schema values use the `zod<Name>Schema` form: `zodPaginationSchema`.
|
|
152
|
-
- Schema factories keep the same form when the project already treats them as schema constructors.
|
|
153
|
-
- Functions use an explicit verb describing their effect: `parseQueryValue`, `generateRandomString`.
|
|
154
|
-
- Predicates begin with `is`, `has`, or `can` and must provide a meaningful type guard when possible.
|
|
155
|
-
- Constants use `UPPER_SNAKE_CASE` when they represent fixed configuration or enumerated values.
|
|
156
|
-
- Boolean options describe capability or state: `queryEnabled`, `paginationEnabled`, `withTime`.
|
|
157
|
-
|
|
158
|
-
### Type symbols
|
|
159
|
-
|
|
160
|
-
- Exported types use PascalCase and describe the domain concept instead of its implementation.
|
|
161
|
-
- Zod-related public types use the established `Zod<Name>` prefix where it clarifies ownership.
|
|
162
|
-
- Option objects end with `Options`; result wrappers end with `Result` when that is their semantic role.
|
|
163
|
-
- Generic parameters use a `T` prefix and a meaningful noun: `TShape`, `TIdentifier`, `TQueryEnabled`.
|
|
164
|
-
- Literal generic flags should remain literal through `const` generics when they change the returned static shape.
|
|
165
|
-
|
|
166
|
-
Names should answer what a symbol represents without requiring a reader to inspect its implementation.
|
|
167
|
-
Prefer a slightly longer precise name over a short generic name that loses domain meaning.
|
|
168
|
-
|
|
169
|
-
## TypeScript and code style
|
|
170
|
-
|
|
171
|
-
The project uses strict TypeScript, ESM, Prettier, and type-aware ESLint. Generated declarations are part of the
|
|
172
|
-
public product, so a solution is incomplete when runtime behavior works but emitted types become broad or unstable.
|
|
173
|
-
|
|
174
|
-
### Imports
|
|
175
|
-
|
|
176
|
-
1. External dependencies come first.
|
|
177
|
-
2. Cross-module project imports use the `@/` alias.
|
|
178
|
-
3. Same-module imports use relative paths.
|
|
179
|
-
4. Type-only dependencies use `import type`.
|
|
180
|
-
5. Import groups are separated by blank lines and left to the configured formatter for sorting.
|
|
181
|
-
|
|
182
|
-
### General code rules
|
|
183
|
-
|
|
184
|
-
- Prefer precise types over `any`, broad records, or type assertions that hide lost inference.
|
|
185
|
-
- Keep assertions close to the compiler limitation they solve and explain why they are safe.
|
|
186
|
-
- Use early returns when they reduce nesting and make exceptional paths visible.
|
|
187
|
-
- Separate logical phases with blank lines instead of compressing unrelated operations together.
|
|
188
|
-
- Reuse existing project dependencies and abstractions before introducing another package.
|
|
189
|
-
- Do not silently swallow unknown fields at public boundaries unless stripping is intentional and documented.
|
|
190
|
-
- Do not manually duplicate a runtime schema as an interface that can drift from validation behavior.
|
|
191
|
-
- Keep transforms, coercion, defaults, and input/output differences visible in both types and specifications.
|
|
192
|
-
|
|
193
|
-
### Zod rules
|
|
194
|
-
|
|
195
|
-
- Runtime schemas are the source of truth; public data types should normally use `z.infer` or `z.output`.
|
|
196
|
-
- Accept `z.ZodObject<TShape>` when object methods or exact keys are required by the implementation.
|
|
197
|
-
- Do not widen an object schema to `z.ZodType` merely to make a generic signature easier to write.
|
|
198
|
-
- Prepared schemas may use explicit escape hatches when transforms or refinements must remain untouched.
|
|
199
|
-
- Literal feature flags must alter both runtime shape and inferred output instead of producing vague optional fields.
|
|
200
|
-
- API boundary objects should usually be strict so stale or misspelled fields fail loudly.
|
|
201
|
-
|
|
202
|
-
## Comments
|
|
203
|
-
|
|
204
|
-
Comments are an intentional part of this codebase. They should make constraints and architectural reasoning easier
|
|
205
|
-
to recover later, especially around type-level behavior, validation order, side effects, and framework integration.
|
|
206
|
-
|
|
207
|
-
### Inline comments
|
|
208
|
-
|
|
209
|
-
Use inline comments to explain why code exists, what invariant it protects, or why a simpler-looking alternative is
|
|
210
|
-
incorrect. Avoid comments that merely translate the following statement into English.
|
|
211
|
-
|
|
212
|
-
Long files and specifications may use wide visual sections:
|
|
213
|
-
|
|
214
|
-
```typescript
|
|
215
|
-
// ==========================================================================================
|
|
216
|
-
// PREPARED SCHEMA COMPOSITION
|
|
217
|
-
// ==========================================================================================
|
|
218
|
-
```
|
|
219
|
-
|
|
220
|
-
Keep separator width consistent within the same file. Section names should describe behavior or responsibility,
|
|
221
|
-
not generic chronology such as `STEP 1` or `OTHER`.
|
|
222
|
-
|
|
223
|
-
For local reasoning, prefer two balanced lines that visually form a compact block:
|
|
224
|
-
|
|
225
|
-
```typescript
|
|
226
|
-
// TypeScript cannot connect the generic conditional type with this runtime branch.
|
|
227
|
-
// The assertion preserves literal inference without replacing the parsed output type.
|
|
228
|
-
```
|
|
229
|
-
|
|
230
|
-
Leave a blank line after a reasoning block when it introduces the next logical phase. Dense comments should improve
|
|
231
|
-
scanning and grouping; they should not turn straightforward code into a narrated transcript.
|
|
232
|
-
|
|
233
|
-
## JSDoc
|
|
234
|
-
|
|
235
|
-
Every public symbol with non-obvious behavior should have JSDoc. Public schema factories, services, result types,
|
|
236
|
-
configuration types, and framework adapters require documentation before being exported from `src/index.ts`.
|
|
237
|
-
|
|
238
|
-
The preferred visual style contains at least two meaningful lines of similar length:
|
|
239
|
-
|
|
240
|
-
```typescript
|
|
241
|
-
/**
|
|
242
|
-
* Builds a strict selection contract for bulk operations across paginated data.
|
|
243
|
-
* The identifier schema is shared by explicit and all-matching selection modes.
|
|
244
|
-
*/
|
|
245
|
-
```
|
|
246
|
-
|
|
247
|
-
The first line summarizes the responsibility. The second line explains behavior, guarantees, or an important
|
|
248
|
-
constraint. Together they should look like a small rectangle instead of one short line followed by a long paragraph.
|
|
249
|
-
|
|
250
|
-
Additional paragraphs should follow the same principle:
|
|
251
|
-
|
|
252
|
-
- keep related lines visually balanced;
|
|
253
|
-
- separate distinct ideas with an empty JSDoc line;
|
|
254
|
-
- document defaults and feature flags next to the affected option;
|
|
255
|
-
- explain input/output transforms when runtime and static shapes differ;
|
|
256
|
-
- add `@example` only when composition is difficult to understand from the signature;
|
|
257
|
-
- avoid copying a full README usage catalog into source comments.
|
|
258
|
-
|
|
259
|
-
Private symbols benefit from JSDoc when they encode a non-trivial type relationship or architectural invariant.
|
|
260
|
-
Simple local values do not need ceremonial comments when their names and implementation are already unambiguous.
|
|
261
|
-
|
|
262
|
-
## Testing
|
|
263
|
-
|
|
264
|
-
Each module owns exactly one colocated specification file. Do not create separate schema, utility, integration,
|
|
265
|
-
or type fixtures for the same module; runtime and compile-time contracts belong together in `<module>.spec.ts`.
|
|
266
|
-
|
|
267
|
-
A module specification should contain:
|
|
268
|
-
|
|
269
|
-
- runtime success paths and meaningful boundary failures;
|
|
270
|
-
- strictness, defaults, coercion, transforms, and error behavior where applicable;
|
|
271
|
-
- compile-time equality checks for exported generic contracts;
|
|
272
|
-
- `@ts-expect-error` assertions for intentionally rejected shapes;
|
|
273
|
-
- wide comment sections separating major responsibilities;
|
|
274
|
-
- tests through public APIs instead of unstable dependency internals.
|
|
275
|
-
|
|
276
|
-
Compile-time assertions should use local helper types inside the specification. They must not be exported or moved
|
|
277
|
-
into fixture files. Invalid runtime expressions should remain inside uninvoked functions when module evaluation could
|
|
278
|
-
otherwise throw before Bun starts the suite.
|
|
279
|
-
|
|
280
|
-
Tests must remain deterministic:
|
|
281
|
-
|
|
282
|
-
- freeze or bound time-dependent behavior;
|
|
283
|
-
- validate random values by shape rather than exact output;
|
|
284
|
-
- avoid relying on terminal-specific ANSI rendering;
|
|
285
|
-
- inspect stable public dependency output instead of private object internals;
|
|
286
|
-
- compare binary responses byte-for-byte through the standard `Response` API.
|
|
287
|
-
|
|
288
|
-
Run the focused specification while implementing, then run the complete suite before finishing the change.
|
|
289
|
-
|
|
290
|
-
## Development workflow
|
|
291
|
-
|
|
292
|
-
### Prerequisites
|
|
293
|
-
|
|
294
|
-
- Bun compatible with the version used in CI.
|
|
295
|
-
- Node.js only when required by publishing or external tooling.
|
|
296
|
-
- `just` is optional but recommended for the project quality-assurance recipe.
|
|
297
|
-
|
|
298
|
-
### Install dependencies
|
|
22
|
+
## Development
|
|
299
23
|
|
|
300
24
|
```bash
|
|
301
25
|
bun install
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
Use the frozen lockfile mode in automation:
|
|
305
|
-
|
|
306
|
-
```bash
|
|
307
|
-
bun install --frozen-lockfile
|
|
308
|
-
```
|
|
309
|
-
|
|
310
|
-
### Focused development
|
|
311
|
-
|
|
312
|
-
```bash
|
|
313
|
-
bun test src/<module>/<module>.spec.ts
|
|
314
|
-
bun run typecheck
|
|
315
|
-
bunx oxlint src/<module>
|
|
316
|
-
```
|
|
317
|
-
|
|
318
|
-
Run focused checks first to shorten feedback loops. Do not claim a command passed unless it was actually executed
|
|
319
|
-
and completed successfully in the current working tree.
|
|
320
|
-
|
|
321
|
-
### Full quality assurance
|
|
322
|
-
|
|
323
|
-
```bash
|
|
324
|
-
just quality-assurance
|
|
26
|
+
just qa
|
|
325
27
|
bun run build
|
|
326
|
-
git diff --check
|
|
327
28
|
```
|
|
328
29
|
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
The build must succeed because `tsup` generates both runtime ESM and public TypeScript declarations. Review declaration
|
|
333
|
-
output whenever a change introduces conditional generics, schema factories, transforms, or new exported type aliases.
|
|
334
|
-
|
|
335
|
-
## Adding or changing a module
|
|
336
|
-
|
|
337
|
-
Before considering a module change complete:
|
|
338
|
-
|
|
339
|
-
1. Confirm the responsibility cannot be owned by a smaller existing abstraction.
|
|
340
|
-
2. Place files under the correct architectural module and use established suffixes.
|
|
341
|
-
3. Keep dependency direction consistent with the hierarchy documented above.
|
|
342
|
-
4. Add or update the single colocated module specification.
|
|
343
|
-
5. Cover runtime behavior and compile-time inference in that same file.
|
|
344
|
-
6. Add balanced JSDoc to every newly exported public symbol.
|
|
345
|
-
7. Export the intended API from `src/index.ts`; keep private helpers private.
|
|
346
|
-
8. Run focused tests, typecheck, full quality assurance, and declaration build.
|
|
347
|
-
9. Inspect `git diff`, `git diff --check`, and generated diagnostics.
|
|
348
|
-
10. Update this README only when architecture or development policy changes.
|
|
349
|
-
|
|
350
|
-
## Public API and releases
|
|
351
|
-
|
|
352
|
-
The package exposes only the root entrypoint declared in `package.json`. Consumers should import from
|
|
353
|
-
`@kalutskii/foundation` and must not depend on `src` paths or generated bundle internals.
|
|
354
|
-
|
|
355
|
-
A new export is a compatibility commitment. Before publishing it, verify:
|
|
356
|
-
|
|
357
|
-
- the name follows project conventions;
|
|
358
|
-
- the owning module is architecturally correct;
|
|
359
|
-
- runtime validation and static inference agree;
|
|
360
|
-
- public JSDoc explains defaults and constraints;
|
|
361
|
-
- declarations preserve concrete keys, literals, and transformed outputs;
|
|
362
|
-
- the module specification protects the intended contract.
|
|
363
|
-
|
|
364
|
-
Breaking public contracts require an intentional version change and migration plan. Internal refactors should keep
|
|
365
|
-
public names and inferred behavior stable unless the release explicitly communicates otherwise.
|
|
30
|
+
`just qa` runs type checking, linting, formatting, and the complete Bun test suite. Repository architecture and
|
|
31
|
+
source conventions are documented in [`AGENTS.md`](./AGENTS.md).
|