5htp-core 0.3.1 → 0.3.2-2
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/package.json +2 -2
- package/src/client/assets/css/components/button.less +2 -1
- package/src/client/services/router/components/Link.tsx +1 -1
- package/src/client/services/router/components/Page.tsx +6 -4
- package/src/client/services/router/index.tsx +17 -26
- package/src/client/services/router/request/api.ts +12 -4
- package/src/client/services/router/response/page.ts +5 -7
- package/src/common/router/index.ts +2 -1
- package/src/common/router/register.ts +5 -8
- package/src/common/router/response/page.ts +31 -17
- package/src/server/app/index.ts +12 -3
- package/src/server/app/service/container.ts +3 -2
- package/src/server/app/service/index.ts +45 -13
- package/src/server/app.tsconfig.json +1 -1
- package/src/server/services/console/html.ts +21 -20
- package/src/server/services/console/index.ts +112 -138
- package/src/server/services/database/datatypes.ts +14 -2
- package/src/server/services/database/metas.ts +3 -5
- package/src/server/services/disks/index.ts +1 -2
- package/src/server/services/email/index.ts +2 -5
- package/src/server/services/email/transporter.ts +19 -6
- package/src/server/services/router/http/index.ts +2 -2
- package/src/server/services/router/index.ts +14 -13
- package/src/server/services/router/request/api.ts +8 -1
- package/src/server/services/router/response/index.ts +2 -2
- package/src/server/services/router/response/page/index.tsx +4 -5
- package/src/server/services/schema/router/index.ts +1 -2
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "5htp-core",
|
|
3
3
|
"description": "Convenient TypeScript framework designed for Performance and Productivity.",
|
|
4
|
-
"version": "0.3.
|
|
4
|
+
"version": "0.3.2-2",
|
|
5
5
|
"author": "Gaetan Le Gac (https://github.com/gaetanlegac)",
|
|
6
6
|
"repository": "git://github.com/gaetanlegac/5htp-core.git",
|
|
7
7
|
"license": "MIT",
|
|
@@ -61,7 +61,6 @@
|
|
|
61
61
|
"mysql2": "^2.3.0",
|
|
62
62
|
"nodemailer": "^6.6.3",
|
|
63
63
|
"object-sizeof": "^1.6.3",
|
|
64
|
-
"ololog": "^1.1.175",
|
|
65
64
|
"path-to-regexp": "^6.2.0",
|
|
66
65
|
"picomatch": "^2.3.1",
|
|
67
66
|
"preact": "^10.5.15",
|
|
@@ -75,6 +74,7 @@
|
|
|
75
74
|
"request": "^2.88.2",
|
|
76
75
|
"sharp": "^0.29.1",
|
|
77
76
|
"sql-formatter": "^4.0.2",
|
|
77
|
+
"tslog": "^4.9.1",
|
|
78
78
|
"uuid": "^8.3.2",
|
|
79
79
|
"uuid-by-string": "^3.0.4",
|
|
80
80
|
"validator": "^13.7.0",
|
|
@@ -65,6 +65,7 @@
|
|
|
65
65
|
gap: @spacing / 2;
|
|
66
66
|
min-width: 4em;
|
|
67
67
|
font-size: 1rem;
|
|
68
|
+
z-index: 1; // Make the label on top of ::before for example
|
|
68
69
|
|
|
69
70
|
li > & {
|
|
70
71
|
flex: 1;
|
|
@@ -111,7 +112,7 @@
|
|
|
111
112
|
}
|
|
112
113
|
|
|
113
114
|
> i {
|
|
114
|
-
color: inherit
|
|
115
|
+
color: inherit;
|
|
115
116
|
}
|
|
116
117
|
|
|
117
118
|
}
|
|
@@ -19,7 +19,7 @@ export const Link = ({ to, ...props }: {
|
|
|
19
19
|
} & React.HTMLProps<HTMLAnchorElement>) => {
|
|
20
20
|
|
|
21
21
|
// External = open in new tab by default
|
|
22
|
-
if (to[0] !== '/' || to.startsWith('//'))
|
|
22
|
+
if (to && (to[0] !== '/' || to.startsWith('//')))
|
|
23
23
|
props.target = '_blank';
|
|
24
24
|
// Otherwise, propagate to the router
|
|
25
25
|
else
|
|
@@ -22,6 +22,7 @@ export default ({ page, isCurrent }: { page: Page, isCurrent?: boolean }) => {
|
|
|
22
22
|
page.loading ? null : page.data
|
|
23
23
|
);
|
|
24
24
|
page.setAllData = setApiData;
|
|
25
|
+
context.data = apiData;
|
|
25
26
|
|
|
26
27
|
React.useEffect(() => {
|
|
27
28
|
|
|
@@ -47,10 +48,11 @@ export default ({ page, isCurrent }: { page: Page, isCurrent?: boolean }) => {
|
|
|
47
48
|
<page.renderer
|
|
48
49
|
// Services
|
|
49
50
|
{...context}
|
|
50
|
-
// URL params
|
|
51
|
-
{
|
|
52
|
-
|
|
53
|
-
|
|
51
|
+
// API data & URL params
|
|
52
|
+
data={{
|
|
53
|
+
...apiData,
|
|
54
|
+
...context.request.data
|
|
55
|
+
}}
|
|
54
56
|
/>
|
|
55
57
|
|
|
56
58
|
) : null
|
|
@@ -20,7 +20,7 @@ import BaseRouter, {
|
|
|
20
20
|
import { getLayout } from '@common/router/layouts';
|
|
21
21
|
import { getRegisterPageArgs, buildRegex } from '@common/router/register';
|
|
22
22
|
import { TFetcherList } from '@common/router/request/api';
|
|
23
|
-
import type { TFrontRenderer
|
|
23
|
+
import type { TFrontRenderer } from '@common/router/response/page';
|
|
24
24
|
|
|
25
25
|
import App from '@client/app/component';
|
|
26
26
|
import type ClientApplication from '@client/app';
|
|
@@ -61,21 +61,18 @@ export type Response = ClientResponse<ClientRouter> | ServerResponse<ServerRoute
|
|
|
61
61
|
- TYPES: ROUTES LOADING
|
|
62
62
|
----------------------------------*/
|
|
63
63
|
|
|
64
|
-
// WARN: To be updated with the mplemenations list of Router.page
|
|
64
|
+
// WARN: To be updated with the mplemenations list of Router.page AND the routes babel plugin
|
|
65
65
|
// (both server and client side)
|
|
66
|
-
export type TRegisterPageArgs<
|
|
66
|
+
export type TRegisterPageArgs<
|
|
67
|
+
TProvidedData extends TFetcherList = TFetcherList,
|
|
68
|
+
TRouter extends Router = Router
|
|
69
|
+
> = ([
|
|
67
70
|
path: string,
|
|
68
|
-
controller: TDataProvider<TProvidedData> | null,
|
|
69
71
|
renderer: TFrontRenderer<TProvidedData>
|
|
70
72
|
] | [
|
|
71
73
|
path: string,
|
|
72
74
|
options: Partial<TRoute["options"]>,
|
|
73
75
|
renderer: TFrontRenderer<TProvidedData>
|
|
74
|
-
] | [
|
|
75
|
-
path: string,
|
|
76
|
-
options: Partial<TRoute["options"]>,
|
|
77
|
-
controller: TDataProvider<TProvidedData> | null,
|
|
78
|
-
renderer: TFrontRenderer<TProvidedData>
|
|
79
76
|
])
|
|
80
77
|
|
|
81
78
|
// Route definition passed by the server
|
|
@@ -221,28 +218,20 @@ export default class ClientRouter<
|
|
|
221
218
|
return currentRoute;
|
|
222
219
|
}
|
|
223
220
|
|
|
224
|
-
public page(
|
|
221
|
+
public page<TProvidedData extends TFetcherList = TFetcherList>(
|
|
225
222
|
path: string,
|
|
226
|
-
|
|
227
|
-
renderer: TFrontRenderer<{}>
|
|
223
|
+
renderer: TFrontRenderer<TProvidedData>
|
|
228
224
|
): TRoute;
|
|
229
225
|
|
|
230
|
-
public page(
|
|
226
|
+
public page<TProvidedData extends TFetcherList = TFetcherList>(
|
|
231
227
|
path: string,
|
|
232
228
|
options: Partial<TRoute["options"]>,
|
|
233
|
-
renderer: TFrontRenderer<
|
|
234
|
-
): TRoute;
|
|
235
|
-
|
|
236
|
-
public page(
|
|
237
|
-
path: string,
|
|
238
|
-
options: Partial<TRoute["options"]>,
|
|
239
|
-
controller: TDataProvider<{}> | null,
|
|
240
|
-
renderer: TFrontRenderer<{}>
|
|
229
|
+
renderer: TFrontRenderer<TProvidedData>
|
|
241
230
|
): TRoute;
|
|
242
231
|
|
|
243
232
|
public page(...args: TRegisterPageArgs): TRoute {
|
|
244
233
|
|
|
245
|
-
const { path, options,
|
|
234
|
+
const { path, options, renderer, layout } = getRegisterPageArgs(...args);
|
|
246
235
|
|
|
247
236
|
// S'il s'agit d'une page, son id doit avoir été injecté via le plugin babel
|
|
248
237
|
const id = options["id"];
|
|
@@ -260,7 +249,7 @@ export default class ClientRouter<
|
|
|
260
249
|
...defaultOptions,
|
|
261
250
|
...options
|
|
262
251
|
},
|
|
263
|
-
controller: (context: TClientOrServerContext) => new ClientPage(
|
|
252
|
+
controller: (context: TClientOrServerContext) => new ClientPage(route, renderer, context, layout)
|
|
264
253
|
};
|
|
265
254
|
|
|
266
255
|
this.routes.push(route);
|
|
@@ -275,7 +264,7 @@ export default class ClientRouter<
|
|
|
275
264
|
|
|
276
265
|
const route: TErrorRoute = {
|
|
277
266
|
code,
|
|
278
|
-
controller: (context: TClientOrServerContext) => new ClientPage(
|
|
267
|
+
controller: (context: TClientOrServerContext) => new ClientPage(route, renderer, context, layout),
|
|
279
268
|
options
|
|
280
269
|
};
|
|
281
270
|
|
|
@@ -391,7 +380,9 @@ export default class ClientRouter<
|
|
|
391
380
|
|
|
392
381
|
const response = await this.createResponse(route, request, apiData)
|
|
393
382
|
|
|
394
|
-
ReactDOM.hydrate(
|
|
383
|
+
ReactDOM.hydrate((
|
|
384
|
+
<App context={response.context} />
|
|
385
|
+
), document.body, () => {
|
|
395
386
|
|
|
396
387
|
console.log(`Render complete`);
|
|
397
388
|
|
|
@@ -404,7 +395,7 @@ export default class ClientRouter<
|
|
|
404
395
|
pageData: {} = {}
|
|
405
396
|
): Promise<ClientPage> {
|
|
406
397
|
|
|
407
|
-
// Load if not done before
|
|
398
|
+
// Load the route if not done before
|
|
408
399
|
if ('load' in route)
|
|
409
400
|
route = this.routes[route.index] = await this.load(route);
|
|
410
401
|
|
|
@@ -9,7 +9,8 @@ import axios, { AxiosResponse, AxiosError, AxiosRequestConfig } from 'axios';
|
|
|
9
9
|
import type { TApiResponseData } from '@server/services/router';
|
|
10
10
|
import ApiClientService, {
|
|
11
11
|
TPostData,
|
|
12
|
-
TApiFetchOptions, TFetcherList, TFetcherArgs, TFetcher
|
|
12
|
+
TApiFetchOptions, TFetcherList, TFetcherArgs, TFetcher,
|
|
13
|
+
TDataReturnedByFetchers
|
|
13
14
|
} from '@common/router/request/api';
|
|
14
15
|
import { instancierViaCode, NetworkError } from '@common/errors';
|
|
15
16
|
import type ClientApplication from '@client/app';
|
|
@@ -46,6 +47,13 @@ export default class ApiClient implements ApiClientService {
|
|
|
46
47
|
/*----------------------------------
|
|
47
48
|
- HIGH LEVEL
|
|
48
49
|
----------------------------------*/
|
|
50
|
+
|
|
51
|
+
public fetch<TProvidedData extends TFetcherList = TFetcherList>(
|
|
52
|
+
fetchers: TFetcherList
|
|
53
|
+
): TDataReturnedByFetchers<TProvidedData> {
|
|
54
|
+
throw new Error("api.fetch shouldn't be called here.");
|
|
55
|
+
}
|
|
56
|
+
|
|
49
57
|
public get = <TData extends unknown = unknown>(path: string, data?: TPostData, opts?: TApiFetchOptions) =>
|
|
50
58
|
this.createFetcher<TData>('GET', path, data, opts);
|
|
51
59
|
|
|
@@ -120,7 +128,7 @@ export default class ApiClient implements ApiClientService {
|
|
|
120
128
|
/*if (options?.captcha !== undefined)
|
|
121
129
|
await this.gui.captcha.check(options?.captcha);*/
|
|
122
130
|
|
|
123
|
-
return await this.
|
|
131
|
+
return await this.execute<TData>(method, path, data, options).catch((e) => {
|
|
124
132
|
this.app.handleError(e);
|
|
125
133
|
throw e; // Throw to break the loop
|
|
126
134
|
})
|
|
@@ -141,7 +149,7 @@ export default class ApiClient implements ApiClientService {
|
|
|
141
149
|
// Fetch all the api data thanks to one http request
|
|
142
150
|
const fetchedData = fetchersCount === 0
|
|
143
151
|
? 0
|
|
144
|
-
: await this.
|
|
152
|
+
: await this.execute("POST", "/api", {
|
|
145
153
|
fetchers: fetchersToRun
|
|
146
154
|
}).then((res) => {
|
|
147
155
|
|
|
@@ -201,7 +209,7 @@ export default class ApiClient implements ApiClientService {
|
|
|
201
209
|
return config;
|
|
202
210
|
}
|
|
203
211
|
|
|
204
|
-
public
|
|
212
|
+
public execute<TData = unknown>(...args: TFetcherArgs): Promise<TData> {
|
|
205
213
|
|
|
206
214
|
const config = this.configure(...args);
|
|
207
215
|
|
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
import type { ComponentChild } from 'preact';
|
|
7
7
|
|
|
8
8
|
// Core
|
|
9
|
-
import type { TClientOrServerContext, Layout } from '@common/router';
|
|
10
|
-
import PageResponse, {
|
|
9
|
+
import type { TClientOrServerContext, Layout, TRoute, TErrorRoute } from '@common/router';
|
|
10
|
+
import PageResponse, { TFrontRenderer } from "@common/router/response/page";
|
|
11
11
|
|
|
12
12
|
// Specific
|
|
13
13
|
import type ClientRouter from '..';
|
|
@@ -29,15 +29,13 @@ export default class ClientPage<TRouter = ClientRouter> extends PageResponse<TRo
|
|
|
29
29
|
public scrollToId: string;
|
|
30
30
|
|
|
31
31
|
public constructor(
|
|
32
|
-
public
|
|
32
|
+
public route: TRoute | TErrorRoute,
|
|
33
33
|
public component: TFrontRenderer,
|
|
34
34
|
public context: TClientOrServerContext,
|
|
35
|
-
public layout?: Layout
|
|
36
|
-
|
|
37
|
-
public route = context.route
|
|
35
|
+
public layout?: Layout
|
|
38
36
|
) {
|
|
39
37
|
|
|
40
|
-
super(
|
|
38
|
+
super(route, component, context);
|
|
41
39
|
|
|
42
40
|
this.bodyId = context.route.options.bodyId;
|
|
43
41
|
this.scrollToId = context.request.hash;
|
|
@@ -20,7 +20,7 @@ import type { TUserRole } from '@server/services/users';
|
|
|
20
20
|
import type { TAppArrowFunction } from '@common/app';
|
|
21
21
|
|
|
22
22
|
// Specfic
|
|
23
|
-
import type { default as Page, TFrontRenderer } from './response/page';
|
|
23
|
+
import type { default as Page, TFrontRenderer, TDataProvider } from './response/page';
|
|
24
24
|
|
|
25
25
|
/*----------------------------------
|
|
26
26
|
- TYPES: ROUTES
|
|
@@ -81,6 +81,7 @@ export type TRouteOptions = {
|
|
|
81
81
|
|
|
82
82
|
// Injected by the page plugin
|
|
83
83
|
filepath?: string,
|
|
84
|
+
data?: TDataProvider
|
|
84
85
|
|
|
85
86
|
// Indexing
|
|
86
87
|
bodyId?: string,
|
|
@@ -10,7 +10,7 @@ import { getLayout } from './layouts';
|
|
|
10
10
|
|
|
11
11
|
// types
|
|
12
12
|
import type { TRouteOptions } from '.';
|
|
13
|
-
import type {
|
|
13
|
+
import type { TFrontRenderer } from './response/page';
|
|
14
14
|
import type { TRegisterPageArgs } from '@client/services/router';
|
|
15
15
|
|
|
16
16
|
/*----------------------------------
|
|
@@ -21,20 +21,17 @@ export const getRegisterPageArgs = (...args: TRegisterPageArgs) => {
|
|
|
21
21
|
|
|
22
22
|
let path: string;
|
|
23
23
|
let options: Partial<TRouteOptions> = {};
|
|
24
|
-
let controller: TDataProvider|null;
|
|
25
24
|
let renderer: TFrontRenderer;
|
|
26
25
|
|
|
27
|
-
if (args.length ===
|
|
28
|
-
([path,
|
|
29
|
-
else if (typeof args[1] === 'object')
|
|
30
|
-
([path, options, renderer] = args)
|
|
26
|
+
if (args.length === 2)
|
|
27
|
+
([path, renderer] = args)
|
|
31
28
|
else
|
|
32
|
-
([path,
|
|
29
|
+
([path, options, renderer] = args)
|
|
33
30
|
|
|
34
31
|
// Automatic layout form the nearest _layout folder
|
|
35
32
|
const layout = getLayout(path, options);
|
|
36
33
|
|
|
37
|
-
return { path, options,
|
|
34
|
+
return { path, options, renderer, layout }
|
|
38
35
|
|
|
39
36
|
}
|
|
40
37
|
|
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
- DEPENDANCES
|
|
3
3
|
----------------------------------*/
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
// Npm
|
|
6
|
+
import type { VNode } from 'preact';
|
|
6
7
|
|
|
7
8
|
// Core libs
|
|
8
|
-
import { ClientOrServerRouter, TClientOrServerContext } from '@common/router';
|
|
9
|
+
import { ClientOrServerRouter, TClientOrServerContext, TRoute, TErrorRoute } from '@common/router';
|
|
9
10
|
import { TFetcherList, TDataReturnedByFetchers } from '@common/router/request/api';
|
|
10
11
|
import { history } from '@client/services/router/request/history';
|
|
11
12
|
|
|
@@ -14,21 +15,30 @@ import { history } from '@client/services/router/request/history';
|
|
|
14
15
|
----------------------------------*/
|
|
15
16
|
|
|
16
17
|
// The function that fetch data from the api before to pass them as context to the renderer
|
|
17
|
-
export type TDataProvider<TProvidedData extends TFetcherList =
|
|
18
|
-
|
|
18
|
+
export type TDataProvider<TProvidedData extends TFetcherList = TFetcherList> = (
|
|
19
|
+
context: TClientOrServerContext & {
|
|
20
|
+
// URL query parameters
|
|
21
|
+
// TODO: typings
|
|
22
|
+
data: {[key: string]: string | number}
|
|
23
|
+
}
|
|
24
|
+
) => TProvidedData
|
|
19
25
|
|
|
20
26
|
// The function that renders routes
|
|
21
27
|
export type TFrontRenderer<
|
|
22
|
-
TProvidedData extends TFetcherList =
|
|
23
|
-
TAdditionnalData = {},
|
|
28
|
+
TProvidedData extends TFetcherList = TFetcherList,
|
|
29
|
+
TAdditionnalData extends {} = {},
|
|
24
30
|
TRouter = ClientOrServerRouter,
|
|
25
|
-
> =
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
31
|
+
> = (
|
|
32
|
+
context: (
|
|
33
|
+
TClientOrServerContext
|
|
34
|
+
&
|
|
35
|
+
TAdditionnalData
|
|
36
|
+
&
|
|
37
|
+
{
|
|
38
|
+
data: TDataReturnedByFetchers<TProvidedData>
|
|
39
|
+
}
|
|
40
|
+
)
|
|
41
|
+
) => VNode<any> | null
|
|
32
42
|
|
|
33
43
|
// Script or CSS resource
|
|
34
44
|
export type TPageResource = {
|
|
@@ -46,7 +56,7 @@ const debug = false;
|
|
|
46
56
|
/*----------------------------------
|
|
47
57
|
- CLASS
|
|
48
58
|
----------------------------------*/
|
|
49
|
-
export default class PageResponse<TRouter extends ClientOrServerRouter = ClientOrServerRouter> {
|
|
59
|
+
export default abstract class PageResponse<TRouter extends ClientOrServerRouter = ClientOrServerRouter> {
|
|
50
60
|
|
|
51
61
|
// Metadata
|
|
52
62
|
public chunkId: string;
|
|
@@ -65,7 +75,7 @@ export default class PageResponse<TRouter extends ClientOrServerRouter = ClientO
|
|
|
65
75
|
public data: TObjetDonnees = {};
|
|
66
76
|
|
|
67
77
|
public constructor(
|
|
68
|
-
public
|
|
78
|
+
public route: TRoute | TErrorRoute,
|
|
69
79
|
public renderer: TFrontRenderer,
|
|
70
80
|
public context: TClientOrServerContext
|
|
71
81
|
) {
|
|
@@ -77,8 +87,12 @@ export default class PageResponse<TRouter extends ClientOrServerRouter = ClientO
|
|
|
77
87
|
public async fetchData() {
|
|
78
88
|
|
|
79
89
|
// Load the fetchers list to load data if needed
|
|
80
|
-
|
|
81
|
-
|
|
90
|
+
const dataFetcher = this.route.options.data;
|
|
91
|
+
if (dataFetcher)
|
|
92
|
+
this.fetchers = dataFetcher({
|
|
93
|
+
...this.context,
|
|
94
|
+
data: this.context.request.data
|
|
95
|
+
});
|
|
82
96
|
|
|
83
97
|
// Execute the fetchers for missing data
|
|
84
98
|
debug && console.log(`[router][page] Fetching api data:` + Object.keys(this.fetchers));
|
package/src/server/app/index.ts
CHANGED
|
@@ -4,9 +4,13 @@
|
|
|
4
4
|
|
|
5
5
|
// Core
|
|
6
6
|
import AppContainer from './container';
|
|
7
|
-
import ApplicationService, {
|
|
7
|
+
import ApplicationService, { StartedServicesIndex } from './service';
|
|
8
8
|
import CommandsManager from './commands';
|
|
9
|
-
import ServicesContainer, {
|
|
9
|
+
import ServicesContainer, {
|
|
10
|
+
ServicesContainer as ServicesContainerClass,
|
|
11
|
+
TRegisteredService,
|
|
12
|
+
TServiceMetas
|
|
13
|
+
} from './service/container';
|
|
10
14
|
import type { ServerBug } from '../services/console';
|
|
11
15
|
|
|
12
16
|
// Built-in
|
|
@@ -49,7 +53,12 @@ export const Service = ServicesContainer;
|
|
|
49
53
|
/*----------------------------------
|
|
50
54
|
- FUNCTIONS
|
|
51
55
|
----------------------------------*/
|
|
52
|
-
export class Application
|
|
56
|
+
export class Application<
|
|
57
|
+
TServicesContainer extends ServicesContainerClass = ServicesContainerClass
|
|
58
|
+
> extends ApplicationService<Config, Hooks, /* TODO: this ? */Application, {}> {
|
|
59
|
+
|
|
60
|
+
public app!: this;
|
|
61
|
+
public servicesContainer!: TServicesContainer;
|
|
53
62
|
|
|
54
63
|
/*----------------------------------
|
|
55
64
|
- PROPERTIES
|
|
@@ -53,7 +53,7 @@ export class ServicesContainer<
|
|
|
53
53
|
public registered: TRegisteredServicesIndex = {}
|
|
54
54
|
|
|
55
55
|
// All service instances by service id
|
|
56
|
-
public allServices:
|
|
56
|
+
public allServices: TServicesIndex = {} as TServicesIndex
|
|
57
57
|
|
|
58
58
|
public setup<TServiceId extends keyof TServicesIndex>(
|
|
59
59
|
serviceId: keyof TServicesIndex,
|
|
@@ -106,7 +106,8 @@ export class ServicesContainer<
|
|
|
106
106
|
...Object.getOwnPropertyNames( Object.getPrototypeOf( instance )),
|
|
107
107
|
...Object.getOwnPropertyNames( instance ),
|
|
108
108
|
// service.launch() isn't included, maybe because parent abstract class
|
|
109
|
-
'launch'
|
|
109
|
+
'launch',
|
|
110
|
+
'bindServices'
|
|
110
111
|
];
|
|
111
112
|
|
|
112
113
|
for (const method of methods)
|
|
@@ -12,7 +12,8 @@ import ServicesContainer from './container';
|
|
|
12
12
|
- TYPES: OPTIONS
|
|
13
13
|
----------------------------------*/
|
|
14
14
|
|
|
15
|
-
export type AnyService =
|
|
15
|
+
export type AnyService<TSubServices extends StartedServicesIndex = StartedServicesIndex> =
|
|
16
|
+
Service<{}, {}, Application, TSubServices>
|
|
16
17
|
|
|
17
18
|
type TServiceConfig = {
|
|
18
19
|
debug?: boolean
|
|
@@ -82,8 +83,7 @@ export default abstract class Service<
|
|
|
82
83
|
: app
|
|
83
84
|
|
|
84
85
|
// Instanciate subservices
|
|
85
|
-
|
|
86
|
-
this.bindService( localName, services[localName] as unknown as TRegisteredService );
|
|
86
|
+
this.bindServices( services );
|
|
87
87
|
|
|
88
88
|
}
|
|
89
89
|
|
|
@@ -140,10 +140,26 @@ export default abstract class Service<
|
|
|
140
140
|
// this.use immediatly instanciate the subservice for few reasons:
|
|
141
141
|
// - The subservice instance can be accesses from another service in the constructor, no matter the order of loading of the services
|
|
142
142
|
// - Consistency: the subserviuce proprties shouldn't be assogned to two different values according depending on the app lifecycle
|
|
143
|
-
public use<
|
|
143
|
+
public use<
|
|
144
|
+
TInstalledServices extends this["app"]["servicesContainer"]["allServices"],
|
|
145
|
+
TServiceId extends keyof TInstalledServices,
|
|
146
|
+
TServiceClass extends TInstalledServices[TServiceId],
|
|
147
|
+
TSubServices extends TServiceClass["services"],
|
|
148
|
+
>(
|
|
144
149
|
serviceId: TServiceId,
|
|
145
|
-
|
|
146
|
-
|
|
150
|
+
subServices?: TSubServices
|
|
151
|
+
): (
|
|
152
|
+
// We can't pass the services types as a generic to TServiceClass
|
|
153
|
+
// So we overwrite the services property
|
|
154
|
+
ReturnType< TServiceClass["getServiceInstance"] >
|
|
155
|
+
&
|
|
156
|
+
{
|
|
157
|
+
new (...args: any[]): TServiceClass["getServiceInstance"] & { services: TSubServices },
|
|
158
|
+
services: TSubServices
|
|
159
|
+
}
|
|
160
|
+
/*Omit<TServiceClass, 'services'> & {
|
|
161
|
+
services: TSubServices
|
|
162
|
+
}*/
|
|
147
163
|
) {
|
|
148
164
|
|
|
149
165
|
// Check of the service has been configurated
|
|
@@ -152,12 +168,17 @@ export default abstract class Service<
|
|
|
152
168
|
throw new Error(`Unable to use service "${serviceId}": This one hasn't been setup.`);
|
|
153
169
|
|
|
154
170
|
// Bind subservices
|
|
155
|
-
|
|
171
|
+
if (subServices !== undefined)
|
|
172
|
+
registered.subServices = {
|
|
173
|
+
...registered.subServices,
|
|
174
|
+
...subServices
|
|
175
|
+
};
|
|
156
176
|
|
|
157
177
|
// Check if not already instanciated
|
|
158
178
|
const existing = ServicesContainer.allServices[ serviceId ];
|
|
159
179
|
if (existing !== undefined) {
|
|
160
180
|
console.info("Service", serviceId, "already instanciated through another service.");
|
|
181
|
+
existing.bindServices( registered.subServices );
|
|
161
182
|
return existing;
|
|
162
183
|
}
|
|
163
184
|
|
|
@@ -166,17 +187,28 @@ export default abstract class Service<
|
|
|
166
187
|
const ServiceClass = registered.metas.class().default;
|
|
167
188
|
|
|
168
189
|
// Create class instance
|
|
169
|
-
const service = new ServiceClass(
|
|
170
|
-
|
|
190
|
+
const service = new ServiceClass(
|
|
191
|
+
this,
|
|
192
|
+
registered.config,
|
|
193
|
+
registered.subServices,
|
|
194
|
+
this.app || this
|
|
195
|
+
)
|
|
196
|
+
const serviceInstance = service.getServiceInstance();
|
|
171
197
|
|
|
172
198
|
// Bind his own metas
|
|
173
199
|
service.metas = registered.metas;
|
|
174
|
-
ServicesContainer.allServices[ registered.metas.id ] =
|
|
200
|
+
ServicesContainer.allServices[ registered.metas.id ] = serviceInstance;
|
|
201
|
+
|
|
202
|
+
return serviceInstance;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
public bindServices( services: TServicesIndex ) {
|
|
175
206
|
|
|
176
|
-
|
|
207
|
+
for (const localName in services)
|
|
208
|
+
this.bindService( localName, services[localName] as unknown as TRegisteredService );
|
|
177
209
|
}
|
|
178
210
|
|
|
179
|
-
|
|
211
|
+
public bindService( localName: string, service: AnyService ) {
|
|
180
212
|
|
|
181
213
|
const serviceScope = this.constructor.name + '.' + localName;
|
|
182
214
|
|
|
@@ -196,7 +228,7 @@ export default abstract class Service<
|
|
|
196
228
|
|
|
197
229
|
}
|
|
198
230
|
|
|
199
|
-
|
|
231
|
+
public async startService( localName: string, service: AnyService ) {
|
|
200
232
|
// Service already started
|
|
201
233
|
if (!service.started) {
|
|
202
234
|
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
"baseUrl": "..",
|
|
6
6
|
"paths": {
|
|
7
7
|
|
|
8
|
-
"@/server/models": ["./server/.generated/models"],
|
|
8
|
+
"@/server/models": ["./server/.generated/models.d.ts"],
|
|
9
9
|
|
|
10
10
|
"@client/*": ["../node_modules/5htp-core/src/client/*"],
|
|
11
11
|
"@common/*": ["../node_modules/5htp-core/src/common/*"],
|
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
----------------------------------*/
|
|
4
4
|
// Npm
|
|
5
5
|
import Ansi2Html from 'ansi-to-html';
|
|
6
|
-
import {
|
|
7
|
-
import type { Console } from '.';;
|
|
6
|
+
import { formatWithOptions } from 'util';
|
|
7
|
+
import type { default as Console, TJsonLog } from '.';;
|
|
8
8
|
|
|
9
9
|
var ansi2Html = new Ansi2Html({
|
|
10
10
|
newline: true,
|
|
@@ -35,30 +35,31 @@ var ansi2Html = new Ansi2Html({
|
|
|
35
35
|
/*----------------------------------
|
|
36
36
|
- METHOD
|
|
37
37
|
----------------------------------*/
|
|
38
|
-
export default (log:
|
|
38
|
+
export default (log: TJsonLog, c: Console) => {
|
|
39
39
|
|
|
40
|
-
|
|
40
|
+
// Print metas as ANSI
|
|
41
|
+
const logMetaMarkup = c.logger._prettyFormatLogObjMeta({
|
|
42
|
+
date: log.time,
|
|
43
|
+
logLevelId: c.getLogLevelId( log.level ),
|
|
44
|
+
logLevelName: log.level,
|
|
45
|
+
// We consider that having the path is useless in this case
|
|
46
|
+
path: undefined,
|
|
47
|
+
});
|
|
41
48
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// BUG: log.date pas pris encompte, affiche la date actuelle
|
|
53
|
-
// https://github.com/fullstack-build/tslog/blob/master/src/LoggerWithoutCallSite.ts#L509
|
|
54
|
-
|
|
55
|
-
let ansi: string = '';
|
|
56
|
-
const myStd = { write: (message: string) => ansi += message }
|
|
57
|
-
c.logger.printPrettyLog(myStd, log);
|
|
49
|
+
// Print args as ANSI
|
|
50
|
+
const logArgsAndErrorsMarkup = c.logger.runtime.prettyFormatLogObj( log.args, c.logger.settings);
|
|
51
|
+
const logErrors = logArgsAndErrorsMarkup.errors;
|
|
52
|
+
const logArgs = logArgsAndErrorsMarkup.args;
|
|
53
|
+
const logErrorsStr = (logErrors.length > 0 && logArgs.length > 0 ? "\n" : "") + logErrors.join("\n");
|
|
54
|
+
c.logger.settings.prettyInspectOptions.colors = c.logger.settings.stylePrettyLogs;
|
|
55
|
+
let ansi = logMetaMarkup + formatWithOptions(c.logger.settings.prettyInspectOptions, ...logArgs) + logErrorsStr;
|
|
58
56
|
|
|
57
|
+
// Use HTML spaces
|
|
59
58
|
ansi = ansi.replace(/ {2}/g, ' ');
|
|
60
59
|
ansi = ansi.replace(/\t/g, ' '.repeat(8));
|
|
60
|
+
ansi = ansi.replace(/\n/g, '<br/>');
|
|
61
61
|
|
|
62
|
+
// Convert ANSI to HTML
|
|
62
63
|
const html = ansi2Html.toHtml(ansi)
|
|
63
64
|
|
|
64
65
|
return html;
|