transit-kit 0.3.0 → 0.3.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 +341 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,3 +2,344 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://github.com/D4rkr34lm/transit-kit/actions/workflows/ci.yml)
|
|
4
4
|
[](https://coveralls.io/github/D4rkr34lm/transit-kit?branch=main)
|
|
5
|
+
|
|
6
|
+
A declarative TypeScript framework for building type-safe Express.js APIs with automatic OpenAPI generation.
|
|
7
|
+
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
- 🔒 **Type-Safe**: End-to-end type safety with TypeScript and Zod validation
|
|
11
|
+
- 📝 **Declarative API Definition**: Define your endpoints with clear, declarative syntax
|
|
12
|
+
- 🔄 **Automatic Validation**: Request body and query parameter validation using Zod schemas
|
|
13
|
+
- 📚 **OpenAPI Generation**: Automatically generate OpenAPI documentation from your endpoint definitions
|
|
14
|
+
- ⚡ **Express.js Powered**: Built on top of the battle-tested Express.js framework
|
|
15
|
+
- 🪵 **Built-in Logging**: Request and response logging out of the box
|
|
16
|
+
- 🎯 **Path Parameters**: Full support for path parameters with type safety
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install transit-kit
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Quick Start
|
|
25
|
+
|
|
26
|
+
### 1. Create a Server
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import { createServer } from "transit-kit/server";
|
|
30
|
+
|
|
31
|
+
const server = createServer({
|
|
32
|
+
port: 3000,
|
|
33
|
+
inDevMode: true,
|
|
34
|
+
logger: true, // or pass a custom logger (console-like interface)
|
|
35
|
+
});
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### 2. Define an API Endpoint
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
import { createApiEndpointHandler } from "transit-kit/server";
|
|
42
|
+
import z from "zod";
|
|
43
|
+
|
|
44
|
+
// Define a simple GET endpoint
|
|
45
|
+
const getUserEndpoint = createApiEndpointHandler(
|
|
46
|
+
{
|
|
47
|
+
meta: {
|
|
48
|
+
name: "Get User",
|
|
49
|
+
description: "Retrieves a user by ID",
|
|
50
|
+
group: "Users",
|
|
51
|
+
},
|
|
52
|
+
method: "get",
|
|
53
|
+
path: "/users/:userId",
|
|
54
|
+
responseSchemas: {
|
|
55
|
+
200: {
|
|
56
|
+
dataType: "application/json",
|
|
57
|
+
dataSchema: z.object({
|
|
58
|
+
id: z.string(),
|
|
59
|
+
name: z.string(),
|
|
60
|
+
email: z.string().email(),
|
|
61
|
+
}),
|
|
62
|
+
},
|
|
63
|
+
404: {}, // Empty response
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
async (request) => {
|
|
67
|
+
const { userId } = request.params;
|
|
68
|
+
|
|
69
|
+
// Simulate fetching user
|
|
70
|
+
const user = await fetchUser(userId);
|
|
71
|
+
|
|
72
|
+
if (!user) {
|
|
73
|
+
return {
|
|
74
|
+
code: 404,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
code: 200,
|
|
80
|
+
dataType: "application/json",
|
|
81
|
+
json: user,
|
|
82
|
+
};
|
|
83
|
+
},
|
|
84
|
+
);
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### 3. Define an Endpoint with Request Body and Query Parameters
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
const createUserEndpoint = createApiEndpointHandler(
|
|
91
|
+
{
|
|
92
|
+
meta: {
|
|
93
|
+
name: "Create User",
|
|
94
|
+
description: "Creates a new user",
|
|
95
|
+
group: "Users",
|
|
96
|
+
},
|
|
97
|
+
method: "post",
|
|
98
|
+
path: "/users",
|
|
99
|
+
requestBodySchema: z.object({
|
|
100
|
+
name: z.string().min(1),
|
|
101
|
+
email: z.string().email(),
|
|
102
|
+
age: z.number().min(18),
|
|
103
|
+
}),
|
|
104
|
+
querySchema: z.object({
|
|
105
|
+
notify: z.boolean().optional(),
|
|
106
|
+
}),
|
|
107
|
+
responseSchemas: {
|
|
108
|
+
201: {
|
|
109
|
+
dataType: "application/json",
|
|
110
|
+
dataSchema: z.object({
|
|
111
|
+
id: z.string(),
|
|
112
|
+
name: z.string(),
|
|
113
|
+
email: z.string().email(),
|
|
114
|
+
}),
|
|
115
|
+
},
|
|
116
|
+
400: {
|
|
117
|
+
dataType: "application/json",
|
|
118
|
+
dataSchema: z.object({
|
|
119
|
+
error: z.string(),
|
|
120
|
+
}),
|
|
121
|
+
},
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
async (request) => {
|
|
125
|
+
// Request body is automatically validated and typed
|
|
126
|
+
const { name, email, age } = request.body;
|
|
127
|
+
const { notify } = request.query;
|
|
128
|
+
|
|
129
|
+
try {
|
|
130
|
+
const newUser = await createUser({ name, email, age });
|
|
131
|
+
|
|
132
|
+
if (notify) {
|
|
133
|
+
await sendNotification(email);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return {
|
|
137
|
+
code: 201,
|
|
138
|
+
dataType: "application/json",
|
|
139
|
+
json: newUser,
|
|
140
|
+
};
|
|
141
|
+
} catch (error) {
|
|
142
|
+
return {
|
|
143
|
+
code: 400,
|
|
144
|
+
dataType: "application/json",
|
|
145
|
+
json: { error: error.message },
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
},
|
|
149
|
+
);
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### 4. Register Endpoints and Start Server
|
|
153
|
+
|
|
154
|
+
```typescript
|
|
155
|
+
server.registerApiEndpoint(getUserEndpoint);
|
|
156
|
+
server.registerApiEndpoint(createUserEndpoint);
|
|
157
|
+
|
|
158
|
+
server.start();
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## Response Types
|
|
162
|
+
|
|
163
|
+
### JSON Response
|
|
164
|
+
|
|
165
|
+
For endpoints that return JSON data:
|
|
166
|
+
|
|
167
|
+
```typescript
|
|
168
|
+
return {
|
|
169
|
+
code: 200,
|
|
170
|
+
dataType: "application/json",
|
|
171
|
+
json: { message: "Success", data: myData },
|
|
172
|
+
};
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### Empty Response
|
|
176
|
+
|
|
177
|
+
For endpoints that return no content (e.g., 204 No Content):
|
|
178
|
+
|
|
179
|
+
```typescript
|
|
180
|
+
return {
|
|
181
|
+
code: 204,
|
|
182
|
+
};
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## OpenAPI Generation
|
|
186
|
+
|
|
187
|
+
Transit-kit can automatically generate OpenAPI documentation from your endpoint definitions.
|
|
188
|
+
|
|
189
|
+
### Using the CLI
|
|
190
|
+
|
|
191
|
+
```bash
|
|
192
|
+
npx transit-kit generate-openapi --output openapi.json --target ./src
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
Options:
|
|
196
|
+
|
|
197
|
+
- `-o, --output <path>`: Output path for the generated OpenAPI document (default: `openapi.json`)
|
|
198
|
+
- `-t, --target <path>`: Target path to search for endpoint definitions (default: `.`)
|
|
199
|
+
|
|
200
|
+
### Programmatic Usage
|
|
201
|
+
|
|
202
|
+
```typescript
|
|
203
|
+
import { generateOpenApiDoc } from "transit-kit/cli";
|
|
204
|
+
|
|
205
|
+
const openApiDoc = await generateOpenApiDoc("./src");
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
The generated OpenAPI document will include:
|
|
209
|
+
|
|
210
|
+
- All registered endpoints
|
|
211
|
+
- Request/response schemas
|
|
212
|
+
- Path parameters
|
|
213
|
+
- Query parameters
|
|
214
|
+
- Request body schemas
|
|
215
|
+
- Response schemas for all status codes
|
|
216
|
+
|
|
217
|
+
## Configuration
|
|
218
|
+
|
|
219
|
+
### Server Configuration
|
|
220
|
+
|
|
221
|
+
```typescript
|
|
222
|
+
interface ServerConfig {
|
|
223
|
+
inDevMode: boolean; // Enable development mode features
|
|
224
|
+
port: number; // Port to listen on
|
|
225
|
+
logger: Logger | boolean; // Logger instance or boolean to enable/disable
|
|
226
|
+
}
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
### Custom Logger
|
|
230
|
+
|
|
231
|
+
You can provide a custom logger with a console-like interface:
|
|
232
|
+
|
|
233
|
+
```typescript
|
|
234
|
+
const customLogger = {
|
|
235
|
+
log: (message: string) => {
|
|
236
|
+
/* custom logging */
|
|
237
|
+
},
|
|
238
|
+
error: (message: string) => {
|
|
239
|
+
/* custom error logging */
|
|
240
|
+
},
|
|
241
|
+
// ... other console methods
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
const server = createServer({
|
|
245
|
+
port: 3000,
|
|
246
|
+
inDevMode: false,
|
|
247
|
+
logger: customLogger,
|
|
248
|
+
});
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
## API Reference
|
|
252
|
+
|
|
253
|
+
### `createServer(config: ServerConfig): Server`
|
|
254
|
+
|
|
255
|
+
Creates a new server instance with the specified configuration.
|
|
256
|
+
|
|
257
|
+
### `createApiEndpointHandler(definition, handler)`
|
|
258
|
+
|
|
259
|
+
Creates an API endpoint handler with type-safe request/response handling.
|
|
260
|
+
|
|
261
|
+
**Definition properties:**
|
|
262
|
+
|
|
263
|
+
- `meta`: Metadata about the endpoint (name, description, group)
|
|
264
|
+
- `method`: HTTP method (`get`, `post`, `put`, `patch`, `delete`)
|
|
265
|
+
- `path`: Express-style path with optional parameters (e.g., `/users/:userId`)
|
|
266
|
+
- `requestBodySchema`: (Optional) Zod schema for request body validation
|
|
267
|
+
- `querySchema`: (Optional) Zod schema for query parameter validation
|
|
268
|
+
- `responseSchemas`: Map of status codes to response schemas
|
|
269
|
+
|
|
270
|
+
### `server.registerApiEndpoint(endpoint)`
|
|
271
|
+
|
|
272
|
+
Registers an endpoint with the server.
|
|
273
|
+
|
|
274
|
+
### `server.start()`
|
|
275
|
+
|
|
276
|
+
Starts the Express server on the configured port.
|
|
277
|
+
|
|
278
|
+
## Examples
|
|
279
|
+
|
|
280
|
+
### Complete Example
|
|
281
|
+
|
|
282
|
+
```typescript
|
|
283
|
+
import { createServer, createApiEndpointHandler } from "transit-kit/server";
|
|
284
|
+
import z from "zod";
|
|
285
|
+
|
|
286
|
+
// Create server
|
|
287
|
+
const server = createServer({
|
|
288
|
+
port: 3000,
|
|
289
|
+
inDevMode: true,
|
|
290
|
+
logger: true,
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
// Define endpoints
|
|
294
|
+
const listUsersEndpoint = createApiEndpointHandler(
|
|
295
|
+
{
|
|
296
|
+
meta: {
|
|
297
|
+
name: "List Users",
|
|
298
|
+
description: "Get a list of all users",
|
|
299
|
+
group: "Users",
|
|
300
|
+
},
|
|
301
|
+
method: "get",
|
|
302
|
+
path: "/users",
|
|
303
|
+
querySchema: z.object({
|
|
304
|
+
page: z.number().optional(),
|
|
305
|
+
limit: z.number().optional(),
|
|
306
|
+
}),
|
|
307
|
+
responseSchemas: {
|
|
308
|
+
200: {
|
|
309
|
+
dataType: "application/json",
|
|
310
|
+
dataSchema: z.array(
|
|
311
|
+
z.object({
|
|
312
|
+
id: z.string(),
|
|
313
|
+
name: z.string(),
|
|
314
|
+
}),
|
|
315
|
+
),
|
|
316
|
+
},
|
|
317
|
+
},
|
|
318
|
+
},
|
|
319
|
+
async (request) => {
|
|
320
|
+
const { page = 1, limit = 10 } = request.query;
|
|
321
|
+
const users = await fetchUsers(page, limit);
|
|
322
|
+
|
|
323
|
+
return {
|
|
324
|
+
code: 200,
|
|
325
|
+
dataType: "application/json",
|
|
326
|
+
json: users,
|
|
327
|
+
};
|
|
328
|
+
},
|
|
329
|
+
);
|
|
330
|
+
|
|
331
|
+
server.registerApiEndpoint(listUsersEndpoint);
|
|
332
|
+
server.start();
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
## License
|
|
336
|
+
|
|
337
|
+
MIT
|
|
338
|
+
|
|
339
|
+
## Contributing
|
|
340
|
+
|
|
341
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
342
|
+
|
|
343
|
+
## Author
|
|
344
|
+
|
|
345
|
+
D4rkr34lm
|