@lytical/app 1.0.10 → 1.0.12
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 +52 -34
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ a typescript api server library built for your express project, with dependency
|
|
|
13
13
|
install packages:
|
|
14
14
|
|
|
15
15
|
```bash
|
|
16
|
-
npm install @lytical/app
|
|
16
|
+
npm install @lytical/app express
|
|
17
17
|
```
|
|
18
18
|
|
|
19
19
|
after installing, configure your `tsconfig.json` file to enable decorators.
|
|
@@ -22,8 +22,7 @@ after installing, configure your `tsconfig.json` file to enable decorators.
|
|
|
22
22
|
// tsconfig.json
|
|
23
23
|
{
|
|
24
24
|
"compilerOptions": {
|
|
25
|
-
"experimentalDecorators": true
|
|
26
|
-
"emitDecoratorMetadata": true
|
|
25
|
+
"experimentalDecorators": true
|
|
27
26
|
}
|
|
28
27
|
}
|
|
29
28
|
```
|
|
@@ -55,6 +54,7 @@ for the above project structure:
|
|
|
55
54
|
|
|
56
55
|
```json
|
|
57
56
|
// tsconfig.json
|
|
57
|
+
|
|
58
58
|
{
|
|
59
59
|
"compilerOptions": {
|
|
60
60
|
"rootDir": "src",
|
|
@@ -67,22 +67,29 @@ for the above project structure:
|
|
|
67
67
|
|
|
68
68
|
```json
|
|
69
69
|
// package.json
|
|
70
|
+
|
|
70
71
|
{
|
|
71
72
|
"main": "./{index.js,routes/**/*.js}"
|
|
72
73
|
}
|
|
73
74
|
```
|
|
74
75
|
|
|
75
|
-
|
|
76
|
+
**note:** your route handlers can be place anywhere... just make sure you import them before starting the app.\
|
|
77
|
+
(e.g. `import './contact/routes/index'`)
|
|
78
|
+
|
|
79
|
+
## example app
|
|
80
|
+
a simple project template / example can be found at github\
|
|
81
|
+
(https://github.com/lytical/ts-app-example)
|
|
76
82
|
|
|
77
83
|
## usage
|
|
78
84
|
|
|
79
85
|
create your injectable service class(es) to implement the business logic.
|
|
80
86
|
|
|
81
87
|
```typescript
|
|
88
|
+
// src/services/example-svc.ts
|
|
89
|
+
|
|
82
90
|
import { ioc_injectable } from '@lytical/ioc';
|
|
83
91
|
|
|
84
92
|
@ioc_injectable()
|
|
85
|
-
// src/services/example-svc.ts
|
|
86
93
|
export class example_svc {
|
|
87
94
|
async get_message() {
|
|
88
95
|
return 'Hello from example_svc!';
|
|
@@ -98,6 +105,7 @@ create your middleware classes
|
|
|
98
105
|
|
|
99
106
|
```typescript
|
|
100
107
|
// src/middleware/example-mw.ts
|
|
108
|
+
|
|
101
109
|
import type { Request, Response, NextFunction } from 'express';
|
|
102
110
|
|
|
103
111
|
import { ioc_inject } from '@lytical/ioc';
|
|
@@ -127,6 +135,7 @@ create your route handler(s)
|
|
|
127
135
|
|
|
128
136
|
```typescript
|
|
129
137
|
// src/routes/example.ts
|
|
138
|
+
|
|
130
139
|
import express, {
|
|
131
140
|
type Request,
|
|
132
141
|
type Response,
|
|
@@ -145,7 +154,8 @@ import { example_middleware_class } from '../middleware/example-mw';
|
|
|
145
154
|
|
|
146
155
|
/**
|
|
147
156
|
* Example route class
|
|
148
|
-
* Use for router class(es)
|
|
157
|
+
* Use for router class(es) to auto app.use() registration for
|
|
158
|
+
* routes; dependent middleware; and dependency injection
|
|
149
159
|
*/
|
|
150
160
|
@app_route({ route: '/example' })
|
|
151
161
|
export class example_route_class {
|
|
@@ -173,7 +183,7 @@ export class example_route_class {
|
|
|
173
183
|
],
|
|
174
184
|
// you may indicate an error handler middleware at the route level.
|
|
175
185
|
// the default error handler will be used if not indicated here.
|
|
176
|
-
error_handler: example_error_handler
|
|
186
|
+
error_handler: example_error_handler,
|
|
177
187
|
})
|
|
178
188
|
post_handler(rqs: Request, rsp: Response, nxt: NextFunction) {
|
|
179
189
|
rsp.json({ body: rqs.body, locals: rsp.locals }).end();
|
|
@@ -185,6 +195,7 @@ now just import app and invoke `start()`
|
|
|
185
195
|
|
|
186
196
|
```typescript
|
|
187
197
|
// src/index.ts
|
|
198
|
+
|
|
188
199
|
import app from '@lytical/app';
|
|
189
200
|
|
|
190
201
|
app.start();
|
|
@@ -194,6 +205,7 @@ app.start();
|
|
|
194
205
|
|
|
195
206
|
```typescript
|
|
196
207
|
// src/index.ts
|
|
208
|
+
|
|
197
209
|
import app, { app_evt } from './lib/app';
|
|
198
210
|
|
|
199
211
|
// app events occur in the following order:
|
|
@@ -201,42 +213,52 @@ import app, { app_evt } from './lib/app';
|
|
|
201
213
|
// 2. server_starting
|
|
202
214
|
// 3. server_listening
|
|
203
215
|
|
|
204
|
-
app.once(app_evt.create_server, (
|
|
205
|
-
// set the event parameter (evt.server) property to
|
|
206
|
-
//
|
|
216
|
+
app.once(app_evt.create_server, (evt) => {
|
|
217
|
+
// set the event parameter (evt.server) property to
|
|
218
|
+
// provide the server instance of your choice.
|
|
219
|
+
|
|
207
220
|
// e.g.
|
|
208
221
|
// evt.server = createHttpsServer(evt.express, my_https_options);
|
|
209
|
-
|
|
210
|
-
// a standard http server instance is created by default,
|
|
211
|
-
//
|
|
212
|
-
|
|
222
|
+
|
|
223
|
+
// a standard http server instance is created by default,
|
|
224
|
+
// if no server is provided.
|
|
225
|
+
|
|
226
|
+
// evt.root_route can be modified to change the root route
|
|
227
|
+
// where auto registered routes are mounted.
|
|
228
|
+
|
|
213
229
|
// default is '/api'.
|
|
214
|
-
|
|
215
|
-
// push async operations (Promise) that fetch encryption keys, ...
|
|
216
|
-
//
|
|
217
|
-
|
|
218
|
-
//
|
|
219
|
-
//
|
|
220
|
-
|
|
230
|
+
|
|
231
|
+
// push async operations (Promise) that fetch encryption keys, ...
|
|
232
|
+
// into the event parameter (evt.wait_for.push(...)).
|
|
233
|
+
|
|
234
|
+
// add middleware into the pipeline (evt.express.use(...)),
|
|
235
|
+
// before auto registered routes are added.
|
|
236
|
+
|
|
237
|
+
// this is also the last chance to register dependencies
|
|
238
|
+
// in the ioc collection, before the container is created.
|
|
239
|
+
console.log(`the root route is (${evt.root_route})`);
|
|
221
240
|
});
|
|
222
241
|
|
|
223
|
-
app.once(app_evt.server_starting, (
|
|
242
|
+
app.once(app_evt.server_starting, (evt) => {
|
|
224
243
|
// use to modify the server listening configuration before it is started.
|
|
225
|
-
|
|
244
|
+
|
|
226
245
|
// all auto registered routes have been added at this point.
|
|
227
|
-
|
|
228
|
-
// you may add middleware to the app pipeline (evt.express.use(...)),
|
|
246
|
+
|
|
247
|
+
// you may add middleware to the app pipeline (evt.express.use(...)),
|
|
248
|
+
// after the auto registered routes.
|
|
249
|
+
|
|
229
250
|
// for example, to add error handling middleware, ...
|
|
230
|
-
|
|
231
|
-
// push async operations (Promise) that may fetch data or
|
|
232
|
-
//
|
|
251
|
+
|
|
252
|
+
// push async operations (Promise) that may fetch data or
|
|
253
|
+
// does some kind of i/o, ... into (evt.wait_for.push(...)).
|
|
254
|
+
|
|
233
255
|
// the ioc container is also ready at this point.
|
|
234
|
-
console.log(`the hostname is (${
|
|
256
|
+
console.log(`the hostname is (${evt.hostname})`);
|
|
235
257
|
});
|
|
236
258
|
|
|
237
259
|
app.once(app_evt.server_listening, () => {
|
|
238
260
|
// emitted when the server is listening
|
|
239
|
-
|
|
261
|
+
|
|
240
262
|
// use it to perform operations after the server starts listening.
|
|
241
263
|
// this is the last event from the app, when it's considered started.
|
|
242
264
|
});
|
|
@@ -244,10 +266,6 @@ app.once(app_evt.server_listening, () => {
|
|
|
244
266
|
app.start();
|
|
245
267
|
```
|
|
246
268
|
|
|
247
|
-
## documentation
|
|
248
|
-
|
|
249
|
-
todo: working on this right now...
|
|
250
|
-
|
|
251
269
|
stay tuned! i have more packages to come.`
|
|
252
270
|
|
|
253
271
|
_lytical(r) is a registered trademark of lytical, inc. all rights are reserved._
|
package/package.json
CHANGED