@tupik/core 0.0.0-stage → 1.0.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/README.md +93 -2
- package/dist/index.d.mts +929 -0
- package/dist/index.d.ts +929 -0
- package/dist/index.mjs +2008 -0
- package/package.json +51 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Carlos Alberto Barbosa Stenzel / Tupik Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,94 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @tupik/core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Zero-dependency core of **Tupik**, a multi-runtime TypeScript web framework: Radix Tree router,
|
|
4
|
+
fluent modules (no decorators, no reflection), middleware pipeline and an app engine built on
|
|
5
|
+
Web Standards (`Request`/`Response`). It never imports `node:*`, so the same app runs on Node.js,
|
|
6
|
+
Bun and Cloudflare Workers through a driver package. Deno and Fastly are not verified yet (planned).
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @tupik/core
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Requires an ESM project (`"type": "module"`), Node.js 22.12 or later (or Bun, or Cloudflare Workers) and TypeScript 5.4+.
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { createApp, createModule } from '@tupik/core';
|
|
20
|
+
|
|
21
|
+
const users = createModule('/users')
|
|
22
|
+
.get('/:id', { access: 'public', handler: (ctx) => ({ id: ctx.params.id }) })
|
|
23
|
+
.post('/', { access: 'authenticated', handler: (ctx) => ctx.status(201).json({ by: ctx.principal.id }) });
|
|
24
|
+
|
|
25
|
+
const app = createApp({ authenticate: (ctx) => findUser(ctx.req.headers.get('authorization')) ?? null }).register(users);
|
|
26
|
+
|
|
27
|
+
// `app.fetch` is the universal port: drivers, tests and edge runtimes all call it.
|
|
28
|
+
const response = await app.fetch(new Request('http://localhost/users/42'));
|
|
29
|
+
console.log(response.status, await response.json()); // 200 { id: '42' }
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Key API
|
|
33
|
+
|
|
34
|
+
| Export | Purpose |
|
|
35
|
+
| --- | --- |
|
|
36
|
+
| `createModule(prefix?, { access }?)` | Fluent route builder: `get`/`post`/`put`/`patch`/`delete`/`head`/`options`/`route`, `use(middleware)`, `access(rule)` |
|
|
37
|
+
| `createApp(options?)` | App engine: `register(...modules)`, `use(middleware)`, `fetch(request, env?, ctx?)`, `routes`; options `authenticate`, `response`, `schemaConverter`, `dev` |
|
|
38
|
+
| `defineErrors`, `classify`, `inspectRoutes`, `withTags` | Typed error catalog (`ctx.fail`), data classes (`pii`/`health`/`secret`), route metadata |
|
|
39
|
+
| `schemaDataClasses`, `routeDataClasses` | Class paths of a schema or a route, for loggers, error reporters and audits |
|
|
40
|
+
| `RadixRouter` | Standalone router: `insert`, `match`, zero-allocation `matchInto(method, path, state)` |
|
|
41
|
+
| `InferSchemaOutput`, `StandardSchemaV1`, `RouteSchema` | Types for the Standard Schema v1 contract (`schema: { body, query, params, headers, response }`) |
|
|
42
|
+
|
|
43
|
+
Every route declares `access` (`'public'`, `'authenticated'`, a policy or `{ auth, check, owner }`); without it
|
|
44
|
+
the route does not compile and `register` throws. Handlers may return a `Response`, an object (JSON), a string,
|
|
45
|
+
or use `ctx.json`/`ctx.text`/`ctx.html`/`ctx.status`/`ctx.header`/`ctx.redirect`/`ctx.setCookie`/`ctx.fail`.
|
|
46
|
+
|
|
47
|
+
Without a `params` schema, `ctx.params` is a `Record<string, string>` read from the path, not typed from it: under
|
|
48
|
+
`noUncheckedIndexedAccess`, `ctx.params.id` is `string | undefined`. Declare `schema: { params: z.object({ id: z.string() }) }` to get a validated `string`.
|
|
49
|
+
|
|
50
|
+
## Data classes
|
|
51
|
+
|
|
52
|
+
`classify` marks how sensitive a field is. The class lives on the field schema, not on the object, so every schema
|
|
53
|
+
built from it keeps it: `z.array(User)`, `.extend()`, `.pick()`, `.omit()`, `.partial()`, `.optional()`, nesting,
|
|
54
|
+
unions, intersections and pipes (Zod 4 and Valibot). A JSON Schema node may carry `'x-data-class': 'pii'` (also behind
|
|
55
|
+
a local `$ref`).
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
import { classify, schemaDataClasses } from '@tupik/core';
|
|
59
|
+
import { z } from 'zod';
|
|
60
|
+
|
|
61
|
+
const User = classify(z.object({ id: z.string(), email: z.string(), passwordHash: z.string() }), {
|
|
62
|
+
email: 'pii',
|
|
63
|
+
passwordHash: 'secret',
|
|
64
|
+
});
|
|
65
|
+
const PublicUser = User.omit({ passwordHash: true });
|
|
66
|
+
schemaDataClasses(z.array(PublicUser)); // { pii: ['email'] }
|
|
67
|
+
|
|
68
|
+
// One field, marked directly.
|
|
69
|
+
const Contact = z.object({ name: z.string(), phone: classify(z.string(), 'pii') });
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Response shaping (undeclared fields cut, `secret` fields removed), OpenAPI `x-data-class`, the audit, MCP, fuzzing
|
|
73
|
+
and tracing all read the same marks. Limit: a path inside an opaque schema the walker cannot reach stays on that
|
|
74
|
+
schema object only, and a schema derived from it does not see it. A field schema instance shared by two fields
|
|
75
|
+
carries its class in both (fails closed).
|
|
76
|
+
|
|
77
|
+
## The principal in middleware
|
|
78
|
+
|
|
79
|
+
`access` runs after the middleware, so a session or JWT middleware can prepare the principal. A middleware that needs
|
|
80
|
+
the caller earlier (rate limit or idempotency per user, tenant membership) calls `await ctx.getPrincipal()`: it runs
|
|
81
|
+
`createApp({ authenticate })` on demand, once per request, and `access` reuses the cached result. It returns the
|
|
82
|
+
registered principal or `null` and does not set `ctx.principal`.
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
createModule('/users').use(async (ctx, next) => {
|
|
86
|
+
const principal = await ctx.getPrincipal(); // User | null
|
|
87
|
+
ctx.header('x-caller', principal === null ? 'anonymous' : 'user');
|
|
88
|
+
await next();
|
|
89
|
+
});
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## License
|
|
93
|
+
|
|
94
|
+
MIT
|