@nextrush/types 3.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 +400 -0
- package/dist/index.d.ts +915 -0
- package/dist/index.js +78 -0
- package/dist/index.js.map +1 -0
- package/package.json +58 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Tanzim (NextRush)
|
|
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
ADDED
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
# @nextrush/types
|
|
2
|
+
|
|
3
|
+
> Shared TypeScript type definitions for the NextRush framework.
|
|
4
|
+
|
|
5
|
+
## The Problem
|
|
6
|
+
|
|
7
|
+
Backend frameworks often have types scattered across packages. This creates:
|
|
8
|
+
|
|
9
|
+
- Circular dependencies between packages
|
|
10
|
+
- Inconsistent interfaces across the ecosystem
|
|
11
|
+
- Difficulty extending or augmenting types
|
|
12
|
+
|
|
13
|
+
## How NextRush Approaches This
|
|
14
|
+
|
|
15
|
+
`@nextrush/types` is the **single source of truth** for all NextRush types:
|
|
16
|
+
|
|
17
|
+
- **Zero dependencies**: Pure TypeScript definitions
|
|
18
|
+
- **Zero runtime code**: Only exports types and constants
|
|
19
|
+
- **Foundation package**: All NextRush packages depend on this
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
pnpm add @nextrush/types
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Quick Start
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
import type { Context, Middleware, Plugin } from '@nextrush/types';
|
|
31
|
+
import { HttpStatus, ContentType } from '@nextrush/types';
|
|
32
|
+
|
|
33
|
+
const middleware: Middleware = async (ctx, next) => {
|
|
34
|
+
ctx.status = HttpStatus.OK;
|
|
35
|
+
await next();
|
|
36
|
+
};
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Context Types
|
|
40
|
+
|
|
41
|
+
The `Context` interface is the heart of NextRush:
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
import type { Context, ContextState, RouteParams, QueryParams } from '@nextrush/types';
|
|
45
|
+
|
|
46
|
+
const handler = async (ctx: Context) => {
|
|
47
|
+
// Request (read-only)
|
|
48
|
+
ctx.method; // HttpMethod
|
|
49
|
+
ctx.url; // Full URL with query
|
|
50
|
+
ctx.path; // Path without query
|
|
51
|
+
ctx.query; // QueryParams
|
|
52
|
+
ctx.headers; // IncomingHeaders
|
|
53
|
+
ctx.ip; // Client IP
|
|
54
|
+
ctx.params; // RouteParams (from router)
|
|
55
|
+
ctx.body; // unknown (from body parser)
|
|
56
|
+
|
|
57
|
+
// Response
|
|
58
|
+
ctx.status = 201;
|
|
59
|
+
ctx.json({ created: true });
|
|
60
|
+
ctx.send('text');
|
|
61
|
+
ctx.html('<h1>Hello</h1>');
|
|
62
|
+
ctx.redirect('/new-url');
|
|
63
|
+
|
|
64
|
+
// Headers
|
|
65
|
+
ctx.set('X-Custom', 'value');
|
|
66
|
+
ctx.get('Authorization');
|
|
67
|
+
|
|
68
|
+
// Error helpers
|
|
69
|
+
ctx.throw(404, 'Not found');
|
|
70
|
+
ctx.assert(user, 404, 'User not found');
|
|
71
|
+
|
|
72
|
+
// State (for middleware data)
|
|
73
|
+
ctx.state.user = { id: '123' };
|
|
74
|
+
|
|
75
|
+
// Middleware flow
|
|
76
|
+
await ctx.next();
|
|
77
|
+
|
|
78
|
+
// Raw access (platform-specific)
|
|
79
|
+
ctx.raw.req; // Raw request
|
|
80
|
+
ctx.raw.res; // Raw response
|
|
81
|
+
ctx.runtime; // 'node' | 'bun' | 'deno' | 'edge'
|
|
82
|
+
ctx.bodySource; // BodySource for parsers
|
|
83
|
+
};
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Context Options
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
import type { ContextOptions } from '@nextrush/types';
|
|
90
|
+
|
|
91
|
+
// Used by adapters to create contexts
|
|
92
|
+
const options: ContextOptions = {
|
|
93
|
+
method: 'GET',
|
|
94
|
+
url: '/users?page=1',
|
|
95
|
+
headers: { 'content-type': 'application/json' },
|
|
96
|
+
ip: '127.0.0.1',
|
|
97
|
+
raw: { req, res },
|
|
98
|
+
};
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## HTTP Types
|
|
102
|
+
|
|
103
|
+
### Methods
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
import type { HttpMethod, CommonHttpMethod } from '@nextrush/types';
|
|
107
|
+
import { HTTP_METHODS } from '@nextrush/types';
|
|
108
|
+
|
|
109
|
+
const method: HttpMethod = 'GET';
|
|
110
|
+
// 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS' | 'TRACE' | 'CONNECT'
|
|
111
|
+
|
|
112
|
+
const common: CommonHttpMethod = 'POST';
|
|
113
|
+
// 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'
|
|
114
|
+
|
|
115
|
+
// Iterate over methods
|
|
116
|
+
for (const m of HTTP_METHODS) {
|
|
117
|
+
console.log(m);
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### Status Codes
|
|
122
|
+
|
|
123
|
+
```typescript
|
|
124
|
+
import { HttpStatus } from '@nextrush/types';
|
|
125
|
+
import type { HttpStatusCode } from '@nextrush/types';
|
|
126
|
+
|
|
127
|
+
ctx.status = HttpStatus.OK; // 200
|
|
128
|
+
ctx.status = HttpStatus.CREATED; // 201
|
|
129
|
+
ctx.status = HttpStatus.BAD_REQUEST; // 400
|
|
130
|
+
ctx.status = HttpStatus.UNAUTHORIZED; // 401
|
|
131
|
+
ctx.status = HttpStatus.FORBIDDEN; // 403
|
|
132
|
+
ctx.status = HttpStatus.NOT_FOUND; // 404
|
|
133
|
+
ctx.status = HttpStatus.INTERNAL_SERVER_ERROR; // 500
|
|
134
|
+
|
|
135
|
+
// Type for status code values
|
|
136
|
+
const status: HttpStatusCode = 200;
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### Content Types
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
import { ContentType } from '@nextrush/types';
|
|
143
|
+
import type { ContentTypeValue } from '@nextrush/types';
|
|
144
|
+
|
|
145
|
+
ctx.set('Content-Type', ContentType.JSON); // 'application/json'
|
|
146
|
+
ctx.set('Content-Type', ContentType.HTML); // 'text/html'
|
|
147
|
+
ctx.set('Content-Type', ContentType.TEXT); // 'text/plain'
|
|
148
|
+
ctx.set('Content-Type', ContentType.FORM); // 'application/x-www-form-urlencoded'
|
|
149
|
+
ctx.set('Content-Type', ContentType.MULTIPART); // 'multipart/form-data'
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### Headers
|
|
153
|
+
|
|
154
|
+
```typescript
|
|
155
|
+
import type { IncomingHeaders, OutgoingHeaders } from '@nextrush/types';
|
|
156
|
+
|
|
157
|
+
// Request headers (read-only)
|
|
158
|
+
const incoming: IncomingHeaders = ctx.headers;
|
|
159
|
+
|
|
160
|
+
// Response headers (writable)
|
|
161
|
+
const outgoing: OutgoingHeaders = {
|
|
162
|
+
'Content-Type': 'application/json',
|
|
163
|
+
'X-Request-Id': '123',
|
|
164
|
+
};
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### Body Types
|
|
168
|
+
|
|
169
|
+
```typescript
|
|
170
|
+
import type { ParsedBody, ResponseBody } from '@nextrush/types';
|
|
171
|
+
|
|
172
|
+
// Request body after parsing
|
|
173
|
+
const body: ParsedBody = ctx.body;
|
|
174
|
+
// string | Uint8Array | Record<string, unknown> | unknown[] | null | undefined
|
|
175
|
+
|
|
176
|
+
// Response body types
|
|
177
|
+
const response: ResponseBody = { data: 'value' };
|
|
178
|
+
// string | Uint8Array | ArrayBuffer | NodeStreamLike | WebStreamLike | Record<string, unknown> | unknown[] | null | undefined
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## Middleware Types
|
|
182
|
+
|
|
183
|
+
```typescript
|
|
184
|
+
import type { Middleware, Next, RouteHandler } from '@nextrush/types';
|
|
185
|
+
|
|
186
|
+
// Middleware function
|
|
187
|
+
const middleware: Middleware = async (ctx, next) => {
|
|
188
|
+
console.log('Before');
|
|
189
|
+
await next();
|
|
190
|
+
console.log('After');
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
// Next function type
|
|
194
|
+
const callNext: Next = async () => {
|
|
195
|
+
// ...
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
// Route handler (alias for Middleware)
|
|
199
|
+
const handler: RouteHandler = async (ctx) => {
|
|
200
|
+
ctx.json({ ok: true });
|
|
201
|
+
};
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
## Plugin Types
|
|
205
|
+
|
|
206
|
+
```typescript
|
|
207
|
+
import type {
|
|
208
|
+
Plugin,
|
|
209
|
+
PluginFactory,
|
|
210
|
+
PluginWithHooks,
|
|
211
|
+
PluginMeta,
|
|
212
|
+
ApplicationLike,
|
|
213
|
+
} from '@nextrush/types';
|
|
214
|
+
|
|
215
|
+
// Basic plugin
|
|
216
|
+
const plugin: Plugin = {
|
|
217
|
+
name: 'my-plugin',
|
|
218
|
+
version: '1.0.0',
|
|
219
|
+
|
|
220
|
+
install(app: ApplicationLike) {
|
|
221
|
+
app.use(async (ctx, next) => {
|
|
222
|
+
await next();
|
|
223
|
+
});
|
|
224
|
+
},
|
|
225
|
+
|
|
226
|
+
destroy() {
|
|
227
|
+
// Cleanup on shutdown
|
|
228
|
+
},
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
// Plugin with lifecycle hooks
|
|
232
|
+
const advancedPlugin: PluginWithHooks = {
|
|
233
|
+
name: 'advanced',
|
|
234
|
+
install(app) {},
|
|
235
|
+
|
|
236
|
+
onRequest(ctx) {},
|
|
237
|
+
onResponse(ctx) {},
|
|
238
|
+
onError(error, ctx) {},
|
|
239
|
+
extendContext(ctx) {},
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
// Plugin factory pattern
|
|
243
|
+
const createPlugin: PluginFactory<{ debug: boolean }> = (options) => ({
|
|
244
|
+
name: 'configurable',
|
|
245
|
+
install(app) {
|
|
246
|
+
if (options?.debug) {
|
|
247
|
+
// Debug mode
|
|
248
|
+
}
|
|
249
|
+
},
|
|
250
|
+
});
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
## Router Types
|
|
254
|
+
|
|
255
|
+
```typescript
|
|
256
|
+
import type {
|
|
257
|
+
Router,
|
|
258
|
+
Route,
|
|
259
|
+
RouteMatch,
|
|
260
|
+
RouterOptions,
|
|
261
|
+
RoutePattern,
|
|
262
|
+
RouteParam,
|
|
263
|
+
} from '@nextrush/types';
|
|
264
|
+
|
|
265
|
+
// Route definition
|
|
266
|
+
const route: Route = {
|
|
267
|
+
method: 'GET',
|
|
268
|
+
path: '/users/:id',
|
|
269
|
+
handler: async (ctx) => ctx.json({ id: ctx.params.id }),
|
|
270
|
+
middleware: [],
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
// Route match result
|
|
274
|
+
const match: RouteMatch = {
|
|
275
|
+
handler: route.handler,
|
|
276
|
+
params: { id: '123' },
|
|
277
|
+
middleware: [],
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
// Router options
|
|
281
|
+
const options: RouterOptions = {
|
|
282
|
+
prefix: '/api/v1',
|
|
283
|
+
caseSensitive: false,
|
|
284
|
+
strict: false,
|
|
285
|
+
};
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
## Runtime Types
|
|
289
|
+
|
|
290
|
+
```typescript
|
|
291
|
+
import type { Runtime, RuntimeInfo, RuntimeCapabilities, BodySource } from '@nextrush/types';
|
|
292
|
+
|
|
293
|
+
// Supported runtimes
|
|
294
|
+
const runtime: Runtime = 'node';
|
|
295
|
+
// 'node' | 'bun' | 'deno' | 'deno-deploy' | 'cloudflare-workers' | 'vercel-edge' | 'edge' | 'unknown'
|
|
296
|
+
|
|
297
|
+
// Runtime capabilities
|
|
298
|
+
const caps: RuntimeCapabilities = {
|
|
299
|
+
nodeStreams: true,
|
|
300
|
+
webStreams: true,
|
|
301
|
+
fileSystem: true,
|
|
302
|
+
webSocket: true,
|
|
303
|
+
fetch: true,
|
|
304
|
+
cryptoSubtle: true,
|
|
305
|
+
workers: true,
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
// Body source for parsers
|
|
309
|
+
const bodySource: BodySource = ctx.bodySource;
|
|
310
|
+
await bodySource.text(); // Read as string
|
|
311
|
+
await bodySource.buffer(); // Read as Uint8Array
|
|
312
|
+
await bodySource.json(); // Read as JSON
|
|
313
|
+
bodySource.stream(); // Get underlying stream
|
|
314
|
+
bodySource.consumed; // Check if already read
|
|
315
|
+
bodySource.contentLength; // Content-Length header
|
|
316
|
+
bodySource.contentType; // Content-Type header
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
## Raw HTTP Types
|
|
320
|
+
|
|
321
|
+
```typescript
|
|
322
|
+
import type { RawHttp } from '@nextrush/types';
|
|
323
|
+
|
|
324
|
+
// Generic raw access
|
|
325
|
+
const raw: RawHttp = ctx.raw;
|
|
326
|
+
raw.req; // Platform request
|
|
327
|
+
raw.res; // Platform response
|
|
328
|
+
|
|
329
|
+
// Type with generics for platform-specific typing
|
|
330
|
+
import type { IncomingMessage, ServerResponse } from 'http';
|
|
331
|
+
const nodeRaw: RawHttp<IncomingMessage, ServerResponse> = ctx.raw;
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
## API Reference
|
|
335
|
+
|
|
336
|
+
### Exports
|
|
337
|
+
|
|
338
|
+
```typescript
|
|
339
|
+
import {
|
|
340
|
+
// Constants (runtime values)
|
|
341
|
+
HttpStatus,
|
|
342
|
+
HTTP_METHODS,
|
|
343
|
+
ContentType,
|
|
344
|
+
} from '@nextrush/types';
|
|
345
|
+
|
|
346
|
+
import type {
|
|
347
|
+
// Context
|
|
348
|
+
Context,
|
|
349
|
+
ContextOptions,
|
|
350
|
+
ContextState,
|
|
351
|
+
RouteParams,
|
|
352
|
+
QueryParams,
|
|
353
|
+
Middleware,
|
|
354
|
+
Next,
|
|
355
|
+
RouteHandler,
|
|
356
|
+
|
|
357
|
+
// HTTP
|
|
358
|
+
HttpMethod,
|
|
359
|
+
CommonHttpMethod,
|
|
360
|
+
HttpStatusCode,
|
|
361
|
+
ContentTypeValue,
|
|
362
|
+
IncomingHeaders,
|
|
363
|
+
OutgoingHeaders,
|
|
364
|
+
ParsedBody,
|
|
365
|
+
ResponseBody,
|
|
366
|
+
RawHttp,
|
|
367
|
+
|
|
368
|
+
// Plugin
|
|
369
|
+
Plugin,
|
|
370
|
+
PluginWithHooks,
|
|
371
|
+
PluginFactory,
|
|
372
|
+
PluginMeta,
|
|
373
|
+
ApplicationLike,
|
|
374
|
+
|
|
375
|
+
// Router
|
|
376
|
+
Router,
|
|
377
|
+
Route,
|
|
378
|
+
RouteMatch,
|
|
379
|
+
RouterOptions,
|
|
380
|
+
RoutePattern,
|
|
381
|
+
RouteParam,
|
|
382
|
+
|
|
383
|
+
// Runtime
|
|
384
|
+
Runtime,
|
|
385
|
+
RuntimeInfo,
|
|
386
|
+
RuntimeCapabilities,
|
|
387
|
+
BodySource,
|
|
388
|
+
BodySourceOptions,
|
|
389
|
+
} from '@nextrush/types';
|
|
390
|
+
```
|
|
391
|
+
|
|
392
|
+
## Package Size
|
|
393
|
+
|
|
394
|
+
- **Bundle**: ~1 KB (mostly constants)
|
|
395
|
+
- **Types**: ~22 KB
|
|
396
|
+
- **Dependencies**: None
|
|
397
|
+
|
|
398
|
+
## License
|
|
399
|
+
|
|
400
|
+
MIT
|