xpref 1.0.0 → 1.0.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 +388 -0
- package/debug.js +1 -1
- package/index.d.ts +2 -2
- package/package.json +10 -3
- package/types.d.ts +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
# API Package
|
|
2
|
+
|
|
3
|
+
A powerful Express.js server setup package that provides a simple interface for creating and configuring Express applications with built-in security, logging, and routing features.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Automatic port management with fallback
|
|
8
|
+
- Built-in security with Helmet
|
|
9
|
+
- CORS support
|
|
10
|
+
- Request logging with log-client integration
|
|
11
|
+
- Request ID tracking
|
|
12
|
+
- Static file serving
|
|
13
|
+
- Route registration
|
|
14
|
+
- Custom middleware support
|
|
15
|
+
- TypeScript support
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
### Basic Server Setup
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import xpref from 'xpref';
|
|
23
|
+
|
|
24
|
+
// Basic server setup
|
|
25
|
+
xpref({
|
|
26
|
+
appName: 'my-app',
|
|
27
|
+
appEnv: 'development',
|
|
28
|
+
port: 3000,
|
|
29
|
+
routes: {
|
|
30
|
+
'/api/users': [
|
|
31
|
+
'users', // route name
|
|
32
|
+
[], // middleware array
|
|
33
|
+
{
|
|
34
|
+
get: (req, res) => {
|
|
35
|
+
res.json({ users: [] });
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
{} // children routes
|
|
39
|
+
]
|
|
40
|
+
}
|
|
41
|
+
}).then(({ port, app }) => {
|
|
42
|
+
console.log(`Server running on port ${port}`);
|
|
43
|
+
});
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### With Request Logging
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
import xpref from 'xpref';
|
|
50
|
+
import { createLogger } from '@core/log-client';
|
|
51
|
+
|
|
52
|
+
// Create a logger instance
|
|
53
|
+
const logger = createLogger({
|
|
54
|
+
appName: 'my-app',
|
|
55
|
+
appEnv: 'development'
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
xpref({
|
|
59
|
+
appName: 'my-app',
|
|
60
|
+
appEnv: 'development',
|
|
61
|
+
port: 3000,
|
|
62
|
+
logger, // Pass the logger instance
|
|
63
|
+
routes: {
|
|
64
|
+
'/api/logs': [
|
|
65
|
+
'logs',
|
|
66
|
+
[],
|
|
67
|
+
{
|
|
68
|
+
get: (req, res) => {
|
|
69
|
+
res.json({ logs: [] });
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
{}
|
|
73
|
+
]
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### With Multiple Routes and Methods
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
xpref({
|
|
82
|
+
appName: 'my-app',
|
|
83
|
+
appEnv: 'development',
|
|
84
|
+
port: 3000,
|
|
85
|
+
routes: {
|
|
86
|
+
'/api/users': [
|
|
87
|
+
'users',
|
|
88
|
+
[], // middleware array
|
|
89
|
+
{
|
|
90
|
+
get: (req, res) => {
|
|
91
|
+
res.json({ users: [] });
|
|
92
|
+
},
|
|
93
|
+
post: (req, res) => {
|
|
94
|
+
res.json({ message: 'User created' });
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
'/create': [
|
|
99
|
+
'create-user',
|
|
100
|
+
[], // middleware array
|
|
101
|
+
{
|
|
102
|
+
post: (req, res) => {
|
|
103
|
+
res.json({ message: 'User created' });
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
{}
|
|
107
|
+
]
|
|
108
|
+
}
|
|
109
|
+
],
|
|
110
|
+
'/api/auth': [
|
|
111
|
+
'auth',
|
|
112
|
+
[], // middleware array
|
|
113
|
+
{
|
|
114
|
+
post: (req, res) => {
|
|
115
|
+
res.json({ token: 'jwt-token' });
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
{}
|
|
119
|
+
]
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### With Static Files
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
xpref({
|
|
128
|
+
appName: 'my-app',
|
|
129
|
+
appEnv: 'development',
|
|
130
|
+
port: 3000,
|
|
131
|
+
staticRoutes: {
|
|
132
|
+
'/public': './public',
|
|
133
|
+
'/uploads': './uploads'
|
|
134
|
+
},
|
|
135
|
+
routes: {
|
|
136
|
+
'/api/files': [
|
|
137
|
+
'files',
|
|
138
|
+
[],
|
|
139
|
+
{
|
|
140
|
+
get: (req, res) => {
|
|
141
|
+
res.json({ files: [] });
|
|
142
|
+
},
|
|
143
|
+
post: (req, res) => {
|
|
144
|
+
res.json({ message: 'File uploaded' });
|
|
145
|
+
},
|
|
146
|
+
put: (req, res) => {
|
|
147
|
+
res.json({ message: 'File updated' });
|
|
148
|
+
},
|
|
149
|
+
delete: (req, res) => {
|
|
150
|
+
res.json({ message: 'File deleted' });
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
{}
|
|
154
|
+
]
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### With Custom Middleware
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
xpref({
|
|
163
|
+
appName: 'my-app',
|
|
164
|
+
appEnv: 'development',
|
|
165
|
+
port: 3000,
|
|
166
|
+
interceptor: (app) => {
|
|
167
|
+
// Add custom middleware
|
|
168
|
+
app.use((req, res, next) => {
|
|
169
|
+
console.log('Custom middleware');
|
|
170
|
+
next();
|
|
171
|
+
});
|
|
172
|
+
},
|
|
173
|
+
routes: {
|
|
174
|
+
'/api/protected': [
|
|
175
|
+
'protected',
|
|
176
|
+
[authMiddleware], // route-specific middleware
|
|
177
|
+
{
|
|
178
|
+
get: (req, res) => {
|
|
179
|
+
res.json({ data: 'Protected route' });
|
|
180
|
+
},
|
|
181
|
+
post: (req, res) => {
|
|
182
|
+
res.json({ data: 'Protected route created' });
|
|
183
|
+
},
|
|
184
|
+
put: (req, res) => {
|
|
185
|
+
res.json({ data: 'Protected route updated' });
|
|
186
|
+
},
|
|
187
|
+
delete: (req, res) => {
|
|
188
|
+
res.json({ data: 'Protected route deleted' });
|
|
189
|
+
}
|
|
190
|
+
},
|
|
191
|
+
{}
|
|
192
|
+
]
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
### With Manual Start Control
|
|
198
|
+
|
|
199
|
+
```typescript
|
|
200
|
+
xpref({
|
|
201
|
+
appName: 'my-app',
|
|
202
|
+
appEnv: 'development',
|
|
203
|
+
port: 3000,
|
|
204
|
+
manuallyStart: ({ app, port }) => {
|
|
205
|
+
// Custom server start logic
|
|
206
|
+
return new Promise((resolve) => {
|
|
207
|
+
const server = app.listen(port, () => {
|
|
208
|
+
resolve({ port, app, server });
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
},
|
|
212
|
+
routes: {
|
|
213
|
+
'/api/health': [
|
|
214
|
+
'health',
|
|
215
|
+
[],
|
|
216
|
+
{
|
|
217
|
+
get: (req, res) => {
|
|
218
|
+
res.json({ status: 'ok' });
|
|
219
|
+
}
|
|
220
|
+
},
|
|
221
|
+
{}
|
|
222
|
+
]
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
## API Reference
|
|
228
|
+
|
|
229
|
+
### `xpref(props: API): Promise<{ port: number, app: Application }>`
|
|
230
|
+
|
|
231
|
+
Creates and configures an Express application.
|
|
232
|
+
|
|
233
|
+
#### Parameters
|
|
234
|
+
|
|
235
|
+
- `props`: Configuration object
|
|
236
|
+
- `appName`: Name of your application
|
|
237
|
+
- `appEnv`: Environment (development, production, etc.)
|
|
238
|
+
- `port`: Port number (default: 3000)
|
|
239
|
+
- `routes`: Object mapping routes to their configurations
|
|
240
|
+
- `staticRoutes`: Object mapping static file paths
|
|
241
|
+
- `manuallyStart`: Function for custom server start logic
|
|
242
|
+
- `interceptor`: Function to add custom middleware
|
|
243
|
+
- `logger`: LogClient instance from @core/log-client package
|
|
244
|
+
|
|
245
|
+
### Route Configuration
|
|
246
|
+
|
|
247
|
+
```typescript
|
|
248
|
+
type Handler = {
|
|
249
|
+
get?: (req: Request, res: Response, next: NextFunction) => void;
|
|
250
|
+
post?: (req: Request, res: Response, next: NextFunction) => void;
|
|
251
|
+
put?: (req: Request, res: Response, next: NextFunction) => void;
|
|
252
|
+
delete?: (req: Request, res: Response, next: NextFunction) => void;
|
|
253
|
+
patch?: (req: Request, res: Response, next: NextFunction) => void;
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
type RouteConfig = [
|
|
257
|
+
string, // route name
|
|
258
|
+
Array<(req: Request, res: Response, next: NextFunction) => void>, // middleware array
|
|
259
|
+
Handler, // handler object with HTTP methods
|
|
260
|
+
Record<string, RouteConfig> // children routes
|
|
261
|
+
];
|
|
262
|
+
|
|
263
|
+
type Routes = {
|
|
264
|
+
[path: string]: RouteConfig;
|
|
265
|
+
};
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
### Static Route Configuration
|
|
269
|
+
|
|
270
|
+
```typescript
|
|
271
|
+
type StaticRoutes = {
|
|
272
|
+
[path: string]: string; // Maps URL path to file system path
|
|
273
|
+
};
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
## Built-in Features
|
|
277
|
+
|
|
278
|
+
### Security
|
|
279
|
+
- Helmet.js for security headers
|
|
280
|
+
- CORS enabled
|
|
281
|
+
- Trust proxy enabled
|
|
282
|
+
- JSON body parsing (8MB limit)
|
|
283
|
+
- URL-encoded body parsing
|
|
284
|
+
|
|
285
|
+
### Request Tracking
|
|
286
|
+
- Unique request ID generation
|
|
287
|
+
- Request logging with log-client integration
|
|
288
|
+
- Request ID middleware
|
|
289
|
+
|
|
290
|
+
### Error Handling
|
|
291
|
+
- Automatic port fallback if port is in use
|
|
292
|
+
- Error handling for server startup
|
|
293
|
+
|
|
294
|
+
## Example Project Structure
|
|
295
|
+
|
|
296
|
+
```
|
|
297
|
+
src/
|
|
298
|
+
├── routes/
|
|
299
|
+
│ ├── users.ts
|
|
300
|
+
│ └── auth.ts
|
|
301
|
+
├── middleware/
|
|
302
|
+
│ └── auth.ts
|
|
303
|
+
├── static/
|
|
304
|
+
│ └── public/
|
|
305
|
+
└── index.ts
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
```typescript
|
|
309
|
+
// src/routes/users.ts
|
|
310
|
+
export default {
|
|
311
|
+
'/api/users': [
|
|
312
|
+
'users',
|
|
313
|
+
[],
|
|
314
|
+
{
|
|
315
|
+
get: (req, res) => {
|
|
316
|
+
res.json({ users: [] });
|
|
317
|
+
},
|
|
318
|
+
post: (req, res) => {
|
|
319
|
+
res.json({ message: 'User created' });
|
|
320
|
+
}
|
|
321
|
+
},
|
|
322
|
+
{
|
|
323
|
+
'/create': [
|
|
324
|
+
'create-user',
|
|
325
|
+
[],
|
|
326
|
+
{
|
|
327
|
+
post: (req, res) => {
|
|
328
|
+
res.json({ message: 'User created' });
|
|
329
|
+
}
|
|
330
|
+
},
|
|
331
|
+
{}
|
|
332
|
+
]
|
|
333
|
+
}
|
|
334
|
+
]
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
// src/routes/auth.ts
|
|
338
|
+
export default {
|
|
339
|
+
'/api/auth': [
|
|
340
|
+
'auth',
|
|
341
|
+
[],
|
|
342
|
+
{
|
|
343
|
+
post: (req, res) => {
|
|
344
|
+
res.json({ token: 'jwt-token' });
|
|
345
|
+
}
|
|
346
|
+
},
|
|
347
|
+
{}
|
|
348
|
+
]
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
// src/index.ts
|
|
352
|
+
import xpref from 'xpref';
|
|
353
|
+
import { createLogger } from '@core/log-client';
|
|
354
|
+
import userRoutes from './routes/users';
|
|
355
|
+
import authRoutes from './routes/auth';
|
|
356
|
+
import authMiddleware from './middleware/auth';
|
|
357
|
+
|
|
358
|
+
// Create logger instance
|
|
359
|
+
const logger = createLogger({
|
|
360
|
+
appName: 'my-api',
|
|
361
|
+
appEnv: process.env.NODE_ENV || 'development'
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
xpref({
|
|
365
|
+
appName: 'my-api',
|
|
366
|
+
appEnv: process.env.NODE_ENV || 'development',
|
|
367
|
+
port: 3000,
|
|
368
|
+
staticRoutes: {
|
|
369
|
+
'/public': './static/public'
|
|
370
|
+
},
|
|
371
|
+
interceptor: (app) => {
|
|
372
|
+
// Add authentication middleware
|
|
373
|
+
app.use('/api/protected', authMiddleware);
|
|
374
|
+
},
|
|
375
|
+
logger, // Pass the logger instance
|
|
376
|
+
routes: {
|
|
377
|
+
...userRoutes,
|
|
378
|
+
...authRoutes
|
|
379
|
+
}
|
|
380
|
+
}).then(({ port }) => {
|
|
381
|
+
console.log(`Server running on port ${port}`);
|
|
382
|
+
});
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
## License
|
|
386
|
+
|
|
387
|
+
MIT
|
|
388
|
+
|
package/debug.js
CHANGED
package/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Xpref } from './types';
|
|
2
2
|
import { getReqId } from './request-log';
|
|
3
|
-
export default function api(pProps:
|
|
3
|
+
export default function api(pProps: Xpref, tried?: number): any;
|
|
4
4
|
export { urlencoded, type Request, type Response, type NextFunction, type Application, } from 'express';
|
|
5
5
|
export declare const getRequestId: typeof getReqId;
|
|
6
6
|
export * from './types';
|
package/package.json
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "xpref",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/yarinnim/xpref.git"
|
|
9
|
+
},
|
|
6
10
|
"scripts": {
|
|
7
11
|
"build": "tsc --build -f ./tsconfig.json",
|
|
8
12
|
"start:dev": "tsc --build -f ./tsconfig.json -w",
|
|
@@ -11,12 +15,15 @@
|
|
|
11
15
|
"jsdoc": "tsc && jsdoc build/**/* -d jsdoc",
|
|
12
16
|
"eslint": "eslint src --ext .ts"
|
|
13
17
|
},
|
|
14
|
-
"author":
|
|
18
|
+
"author": {
|
|
19
|
+
"name": "Yarin NIM <yarin.nim@gmail.com>",
|
|
20
|
+
"web": "https://github.com/yarinnim"
|
|
21
|
+
},
|
|
15
22
|
"license": "ISC",
|
|
23
|
+
"sideEffects": false,
|
|
16
24
|
"publishConfig": {
|
|
17
25
|
"access": "public"
|
|
18
26
|
},
|
|
19
|
-
"sideEffects": false,
|
|
20
27
|
"dependencies": {
|
|
21
28
|
"ajv": "^8.17.1",
|
|
22
29
|
"ajv-errors": "^3.0.0",
|
package/types.d.ts
CHANGED
|
@@ -25,7 +25,7 @@ export type PathMiddleware = Array<any>;
|
|
|
25
25
|
export type PathDetail = [string, PathMiddleware, MethodHandler] | [string, PathMiddleware, MethodHandler, Record<string, PathDetail>];
|
|
26
26
|
export type Route = Record<any, PathDetail>;
|
|
27
27
|
type OnInit = (_app: Application) => void;
|
|
28
|
-
export type
|
|
28
|
+
export type Xpref = {
|
|
29
29
|
appName: string;
|
|
30
30
|
appEnv: string;
|
|
31
31
|
port?: number;
|