@dreamkit/app 0.0.3 → 0.0.5

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/lib/App.d.ts CHANGED
@@ -1,7 +1,15 @@
1
1
  import { Route } from "./builders/RouteBuilder.js";
2
+ import { MiddlewareConstructor } from "./objects/middleware.js";
3
+ import { AppService, ServiceConstructor } from "./objects/service.js";
4
+ import { IocContext } from "@dreamkit/ioc";
2
5
  export declare class App {
6
+ static instanceKey: string;
7
+ readonly started = false;
8
+ readonly context: IocContext;
3
9
  readonly objects: Map<string, any>;
4
10
  readonly routes: Set<Route>;
11
+ readonly services: Set<AppService>;
12
+ readonly middlewares: Set<MiddlewareConstructor>;
5
13
  protected listeners: {
6
14
  add: Set<(data: {
7
15
  id: string;
@@ -18,6 +26,8 @@ export declare class App {
18
26
  }) => any>;
19
27
  };
20
28
  constructor();
29
+ static instance(): App;
30
+ static saveInstance(instance: App): void;
21
31
  on(event: "add" | "remove", cb: (data: {
22
32
  id: string;
23
33
  value: unknown;
@@ -30,6 +40,13 @@ export declare class App {
30
40
  off(event: "add" | "remove" | "change", cb: any): this;
31
41
  getObjectId(value: unknown): string;
32
42
  remove(ids: string[]): Promise<void>;
43
+ protected addService(service: ServiceConstructor): AppService;
44
+ protected startService(item: AppService): Promise<void>;
45
+ protected stopService(item: AppService): Promise<void>;
33
46
  add(objects: Record<string, any>): Promise<void>;
47
+ createRequestContext(request: Request): IocContext;
48
+ request(request: Request): Promise<Response | undefined>;
49
+ start(): Promise<void>;
50
+ stop(): Promise<void>;
34
51
  }
35
52
  //# sourceMappingURL=App.d.ts.map
package/lib/App.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"App.d.ts","sourceRoot":"","sources":["../src/App.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,4BAA4B,CAAC;AAGnD,qBAAa,GAAG;IACd,QAAQ,CAAC,OAAO,mBAA0B;IAC1C,QAA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AAO;IAMpB,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE;IAmB1B,SAAS,CAAC,UAAU,CAAC,OAAO,EAAE,kBAAkB,GAAG,UAAU;cAU7C,YAAY,CAAC,IAAI,EAAE,UAAU;cAU7B,WAAW,CAAC,IAAI,EAAE,UAAU;IAQtC,GAAG,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IAsBtC,oBAAoB,CAAC,OAAO,EAAE,OAAO;IAS/B,OAAO,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,GAAG,SAAS,CAAC;IASxD,KAAK;IAKL,IAAI;CAiBX"}
package/lib/App.js CHANGED
@@ -1,13 +1,36 @@
1
+ import { RequestUrl } from "./RequestUrl.js";
2
+ import { isMiddleware } from "./objects/middleware.js";
3
+ import { isService, } from "./objects/service.js";
1
4
  import { isRoute } from "./utils/kind.js";
5
+ import { log } from "./utils/log.js";
6
+ import { context } from "@dreamkit/ioc";
2
7
  export class App {
8
+ static instanceKey = "dk:app";
9
+ started = false;
10
+ context;
3
11
  objects = new Map();
4
12
  routes = new Set();
13
+ services = new Set();
14
+ middlewares = new Set();
5
15
  listeners = {
6
16
  add: new Set(),
7
17
  remove: new Set(),
8
18
  change: new Set(),
9
19
  };
10
- constructor() { }
20
+ constructor() {
21
+ this.context = context.fork().register(App, {
22
+ value: this,
23
+ });
24
+ }
25
+ static instance() {
26
+ const value = globalThis[App.instanceKey];
27
+ if (!value)
28
+ throw new Error("App instance not found");
29
+ return value;
30
+ }
31
+ static saveInstance(instance) {
32
+ globalThis[App.instanceKey] = instance;
33
+ }
11
34
  on(event, cb) {
12
35
  this.listeners[event].add(cb);
13
36
  return this;
@@ -31,6 +54,15 @@ export class App {
31
54
  if (isRoute(value)) {
32
55
  this.routes.delete(value);
33
56
  }
57
+ else if (isService(value)) {
58
+ const item = [...this.services].find((item) => item.service === value);
59
+ if (!item)
60
+ throw new Error(`Service not found: ${id}`);
61
+ await this.stopService(item);
62
+ }
63
+ else if (isMiddleware(value)) {
64
+ this.middlewares.delete(value);
65
+ }
34
66
  for (const cb of this.listeners.remove)
35
67
  await cb({ id, value });
36
68
  for (const cb of this.listeners.change)
@@ -38,16 +70,96 @@ export class App {
38
70
  this.objects.delete(id);
39
71
  }
40
72
  }
73
+ addService(service) {
74
+ const item = {
75
+ name: service.prototype.name ?? service.name ?? "anonymous",
76
+ service,
77
+ started: false,
78
+ };
79
+ this.services.add(item);
80
+ return item;
81
+ }
82
+ async startService(item) {
83
+ const name = item.name;
84
+ log("starting service", { name });
85
+ const cb = await this.context.resolve(item.service).onStart();
86
+ const shutdown = typeof cb === "function";
87
+ item.started = true;
88
+ log("service started", { name, ...(shutdown && { shutdown: true }) });
89
+ if (shutdown)
90
+ item.shutdown = cb;
91
+ }
92
+ async stopService(item) {
93
+ const name = item.name;
94
+ log("stopping service", { name });
95
+ await item.shutdown?.();
96
+ item.started = false;
97
+ log("service stopped", { name });
98
+ }
41
99
  async add(objects) {
100
+ const services = [];
42
101
  for (const [id, value] of Object.entries(objects)) {
43
102
  if (isRoute(value)) {
44
103
  this.routes.add(value);
45
104
  }
105
+ else if (isService(value)) {
106
+ const item = this.addService(value);
107
+ services.push(item);
108
+ this.services.add(item);
109
+ }
110
+ else if (isMiddleware(value)) {
111
+ this.middlewares.add(value);
112
+ }
46
113
  this.objects.set(id, value);
47
114
  for (const cb of this.listeners.add)
48
115
  await cb({ id, value });
49
116
  for (const cb of this.listeners.change)
50
117
  await cb({ id, value, action: "add" });
51
118
  }
119
+ if (this.started) {
120
+ for (const service of services)
121
+ await this.startService(service);
122
+ }
123
+ }
124
+ createRequestContext(request) {
125
+ const url = new RequestUrl(request.url, "http://localhost");
126
+ return this.context
127
+ .fork()
128
+ .register(Request, { value: request })
129
+ .register(Headers, { value: request.headers })
130
+ .register(RequestUrl, { value: url });
131
+ }
132
+ async request(request) {
133
+ const context = this.createRequestContext(request);
134
+ log("request", context.resolve(RequestUrl).pathname);
135
+ for (const middleware of this.middlewares) {
136
+ const $md = context.resolve(middleware);
137
+ const response = await $md.onRequest();
138
+ if (response)
139
+ return response;
140
+ }
141
+ }
142
+ async start() {
143
+ log("starting app");
144
+ this.started = true;
145
+ for (const item of this.services)
146
+ await this.startService(item);
147
+ }
148
+ async stop() {
149
+ const errors = [];
150
+ const services = [...this.services].filter((item) => item.started && typeof item.shutdown === "function");
151
+ log("stopping services", { services: services.length });
152
+ for (const item of services) {
153
+ try {
154
+ await this.stopService(item);
155
+ }
156
+ catch (error) {
157
+ log("service stopped", { name, error });
158
+ errors.push(error);
159
+ }
160
+ }
161
+ if (errors.length)
162
+ throw new AggregateError(errors, "App stop failed");
163
+ this.started = false;
52
164
  }
53
165
  }
@@ -0,0 +1,4 @@
1
+ export declare class RequestUrl<T = string> extends URL {
2
+ is(...paths: (keyof T)[]): boolean;
3
+ }
4
+ //# sourceMappingURL=RequestUrl.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"RequestUrl.d.ts","sourceRoot":"","sources":["../src/RequestUrl.ts"],"names":[],"mappings":"AAEA,qBAAa,UAAU,CAAC,CAAC,GAAG,MAAM,CAAE,SAAQ,GAAG;IAI7C,EAAE,CAAC,GAAG,KAAK,EAAE,CAAC,MAAM,CAAC,CAAC,EAAE,GAAG,OAAO;CAInC"}
@@ -0,0 +1,10 @@
1
+ import { kindApp } from "./utils/kind.js";
2
+ export class RequestUrl extends URL {
3
+ static {
4
+ kindApp(this, "RequestUrl");
5
+ }
6
+ is(...paths) {
7
+ // [review]
8
+ return paths.includes(this.pathname);
9
+ }
10
+ }
@@ -0,0 +1,6 @@
1
+ import { Func, FuncBuilder, FuncData } from "@dreamkit/func";
2
+ export declare const kindApi: (input: object, name?: string) => void, isApi: (input: unknown) => input is Func;
3
+ export declare class ApiBuilder<T extends FuncData> extends FuncBuilder<T> {
4
+ protected onCreate(func: Func): Func;
5
+ }
6
+ //# sourceMappingURL=ApiBuilder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ApiBuilder.d.ts","sourceRoot":"","sources":["../../src/builders/ApiBuilder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAG7D,eAAO,MAAO,OAAO,0CAAE,KAAK,mCAAyC,CAAC;AAEtE,qBAAa,UAAU,CAAC,CAAC,SAAS,QAAQ,CAAE,SAAQ,WAAW,CAAC,CAAC,CAAC;cAC7C,QAAQ,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI;CAI9C"}
@@ -0,0 +1,9 @@
1
+ import { FuncBuilder } from "@dreamkit/func";
2
+ import { createKind } from "@dreamkit/kind";
3
+ export const [kindApi, isApi] = createKind("@dreamkit/app/api");
4
+ export class ApiBuilder extends FuncBuilder {
5
+ onCreate(func) {
6
+ kindApi(func);
7
+ return func;
8
+ }
9
+ }
@@ -1,11 +1,16 @@
1
+ import { Func } from "@dreamkit/func";
1
2
  import { InferType, MinimalObjectType, ObjectType, ObjectTypeProps } from "@dreamkit/schema";
2
3
  import type { Merge } from "@dreamkit/utils/ts.js";
3
4
  export type RoutePathParams<P extends string | number | symbol> = {
4
5
  [K in P]: K extends string ? `:${K}` : never;
5
6
  };
6
7
  export type RouteParams = MinimalObjectType | undefined;
7
- export type RouteData<TParams extends RouteParams = RouteParams> = {
8
+ export type RouteApi = {
9
+ [key: string]: (...args: any[]) => any;
10
+ };
11
+ export type RouteData<TParams extends RouteParams = RouteParams, TApi extends RouteApi = RouteApi> = {
8
12
  params?: TParams;
13
+ api?: TApi;
9
14
  };
10
15
  export type RouteOptions<T extends RouteData = RouteData> = T & {
11
16
  title?: string;
@@ -26,6 +31,9 @@ export type InferRouteParams<T extends RouteData> = [undefined] extends [
26
31
  export type RouteProps<T extends RouteData = RouteData> = {
27
32
  setParams: [undefined] extends [T["params"]] ? never : (params: InferRouteParams<T>) => void;
28
33
  params: InferRouteParams<T>;
34
+ api: {
35
+ [K in keyof T["api"]]: T["api"][K] extends Func<infer TData, infer TResult> ? Func<Omit<TData, "self">, Promise<Awaited<TResult>>> : never;
36
+ };
29
37
  };
30
38
  export type Route<T extends RouteData = RouteData> = ((props: any) => any) & {
31
39
  $options: RouteOptions<T>;
@@ -37,6 +45,9 @@ export declare class RouteBuilder<T extends RouteData = RouteData> {
37
45
  constructor(options: RouteOptions<T>);
38
46
  protected clone(input?: Partial<RouteOptions> | RouteBuilder): this;
39
47
  title(value: string | undefined): this;
48
+ api<TApi extends RouteApi>(api: TApi): RouteBuilder<MergeFuncData<T, {
49
+ api: T["api"] & TApi;
50
+ }>>;
40
51
  params<TParams extends RouteParams>(type: TParams): RouteBuilder<MergeFuncData<T, {
41
52
  params: TParams;
42
53
  }>>;
@@ -1 +1 @@
1
- {"version":3,"file":"RouteBuilder.d.ts","sourceRoot":"","sources":["../../src/builders/RouteBuilder.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,SAAS,EACT,iBAAiB,EACjB,UAAU,EACV,eAAe,EAEhB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAEnD,MAAM,MAAM,eAAe,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,GAAG,MAAM,IAAI;KAC/D,CAAC,IAAI,CAAC,GAAG,CAAC,SAAS,MAAM,GAAG,IAAI,CAAC,EAAE,GAAG,KAAK;CAC7C,CAAC;AAEF,MAAM,MAAM,WAAW,GAAG,iBAAiB,GAAG,SAAS,CAAC;AACxD,MAAM,MAAM,SAAS,CAAC,OAAO,SAAS,WAAW,GAAG,WAAW,IAAI;IACjE,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB,CAAC;AAEF,MAAM,MAAM,YAAY,CAAC,CAAC,SAAS,SAAS,GAAG,SAAS,IAAI,CAAC,GAAG;IAC9D,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,aAAa,CAAC,EAAE;QAAE,KAAK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
31
  }
32
+ api(api) {
33
+ return this.clone({ api });
34
+ }
32
35
  params(params) {
33
36
  return this.clone({ params });
34
37
  }
package/lib/index.d.ts CHANGED
@@ -1,6 +1,11 @@
1
+ export { RequestUrl } from "./RequestUrl.js";
1
2
  export { isRoute, isApp } from "./utils/kind.js";
3
+ export { isApi } from "./builders/ApiBuilder.js";
2
4
  export { App } from "./App.js";
3
- export { $route } from "./$route.js";
4
5
  export { RouteBuilder, type RouteOptions, type Route, type RouteData, type RouteProps, } from "./builders/RouteBuilder.js";
5
6
  export { defineRoutePath, routePath } from "./routePath.js";
7
+ export { $route } from "./objects/$route.js";
8
+ export { $api } from "./objects/$api.js";
9
+ export { Middleware, MiddlewareClass, isMiddleware, } from "./objects/middleware.js";
10
+ export { Service, ServiceClass, type ServiceStop, isService, } from "./objects/service.js";
6
11
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAC/B,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EACL,YAAY,EACZ,KAAK,YAAY,EACjB,KAAK,KAAK,EACV,KAAK,SAAS,EACd,KAAK,UAAU,GAChB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,eAAe,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,KAAK,EAAE,MAAM,0BAA0B,CAAC;AACjD,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAC/B,OAAO,EACL,YAAY,EACZ,KAAK,YAAY,EACjB,KAAK,KAAK,EACV,KAAK,SAAS,EACd,KAAK,UAAU,GAChB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,eAAe,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE5D,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAC7C,OAAO,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AACzC,OAAO,EACL,UAAU,EACV,eAAe,EACf,YAAY,GACb,MAAM,yBAAyB,CAAC;AACjC,OAAO,EACL,OAAO,EACP,YAAY,EACZ,KAAK,WAAW,EAChB,SAAS,GACV,MAAM,sBAAsB,CAAC"}
package/lib/index.js CHANGED
@@ -1,5 +1,11 @@
1
+ export { RequestUrl } from "./RequestUrl.js";
1
2
  export { isRoute, isApp } from "./utils/kind.js";
3
+ export { isApi } from "./builders/ApiBuilder.js";
2
4
  export { App } from "./App.js";
3
- export { $route } from "./$route.js";
4
5
  export { RouteBuilder, } from "./builders/RouteBuilder.js";
5
6
  export { defineRoutePath, routePath } from "./routePath.js";
7
+ // objects
8
+ export { $route } from "./objects/$route.js";
9
+ export { $api } from "./objects/$api.js";
10
+ export { Middleware, MiddlewareClass, isMiddleware, } from "./objects/middleware.js";
11
+ export { Service, ServiceClass, isService, } from "./objects/service.js";
@@ -0,0 +1,3 @@
1
+ import { ApiBuilder } from "../builders/ApiBuilder.js";
2
+ export declare const $api: ApiBuilder<{}>;
3
+ //# sourceMappingURL=$api.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"$api.d.ts","sourceRoot":"","sources":["../../src/objects/$api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAEvD,eAAO,MAAM,IAAI,gBAAmC,CAAC"}
@@ -0,0 +1,2 @@
1
+ import { ApiBuilder } from "../builders/ApiBuilder.js";
2
+ export const $api = /*#__PURE__*/ new ApiBuilder({});
@@ -0,0 +1,3 @@
1
+ import { RouteBuilder } from "../builders/RouteBuilder.js";
2
+ export declare const $route: RouteBuilder<{}>;
3
+ //# sourceMappingURL=$route.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"$route.d.ts","sourceRoot":"","sources":["../../src/objects/$route.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAE3D,eAAO,MAAM,MAAM,kBAAqC,CAAC"}
@@ -0,0 +1,2 @@
1
+ import { RouteBuilder } from "../builders/RouteBuilder.js";
2
+ export const $route = /*#__PURE__*/ new RouteBuilder({});
@@ -0,0 +1,8 @@
1
+ import { Constructor } from "@dreamkit/utils/ts.js";
2
+ export declare const kindMiddleware: (input: object, name?: string) => void, isMiddleware: (input: unknown) => input is MiddlewareConstructor;
3
+ export type MiddlewareConstructor = Constructor<Middleware>;
4
+ export declare abstract class Middleware {
5
+ abstract onRequest(): any;
6
+ }
7
+ export declare const MiddlewareClass: <P extends import("@dreamkit/ioc").IocParamsUserConfig = {}>(params: P) => import("@dreamkit/ioc").IocClass<P, Middleware, never, import("@dreamkit/ioc").IocParams<P>>;
8
+ //# sourceMappingURL=middleware.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../../src/objects/middleware.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAEpD,eAAO,MAAO,cAAc,0CAAE,YAAY,oDAEzC,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG,WAAW,CAAC,UAAU,CAAC,CAAC;AAC5D,8BAAsB,UAAU;IAI9B,QAAQ,CAAC,SAAS,IAAI,GAAG;CAC1B;AAED,eAAO,MAAM,eAAe,yKAA6B,CAAC"}
@@ -0,0 +1,9 @@
1
+ import { createIocClass } from "@dreamkit/ioc";
2
+ import { createKind } from "@dreamkit/kind";
3
+ export const [kindMiddleware, isMiddleware] = createKind("@dreamkit/app/middleware");
4
+ export class Middleware {
5
+ static {
6
+ kindMiddleware(this);
7
+ }
8
+ }
9
+ export const MiddlewareClass = createIocClass(Middleware);
@@ -0,0 +1,20 @@
1
+ import { Constructor } from "@dreamkit/utils/ts.js";
2
+ export declare const kindService: (input: object, name?: string) => void, isService: (input: unknown) => input is ServiceConstructor;
3
+ type SyncOrAsync<T> = T | Promise<T>;
4
+ export type Shutdown = () => SyncOrAsync<void>;
5
+ export type AppService = {
6
+ name: string;
7
+ service: ServiceConstructor;
8
+ started: boolean;
9
+ shutdown?: Shutdown;
10
+ };
11
+ export type ServiceStop = SyncOrAsync<void> | Shutdown;
12
+ export type ServiceConstructor = Constructor<Service>;
13
+ export declare abstract class Service {
14
+ get name(): string | undefined;
15
+ abstract onStart(): ServiceStop;
16
+ onStop(): any;
17
+ }
18
+ export declare const ServiceClass: <P extends import("@dreamkit/ioc").IocParamsUserConfig = {}>(params: P) => import("@dreamkit/ioc").IocClass<P, Service, never, import("@dreamkit/ioc").IocParams<P>>;
19
+ export {};
20
+ //# sourceMappingURL=service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../../src/objects/service.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAEpD,eAAO,MAAO,WAAW,0CAAE,SAAS,iDAEnC,CAAC;AAEF,KAAK,WAAW,CAAC,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;AACrC,MAAM,MAAM,QAAQ,GAAG,MAAM,WAAW,CAAC,IAAI,CAAC,CAAC;AAC/C,MAAM,MAAM,UAAU,GAAG;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,kBAAkB,CAAC;IAC5B,OAAO,EAAE,OAAO,CAAC;IACjB,QAAQ,CAAC,EAAE,QAAQ,CAAC;CACrB,CAAC;AACF,MAAM,MAAM,WAAW,GAAG,WAAW,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC;AACvD,MAAM,MAAM,kBAAkB,GAAG,WAAW,CAAC,OAAO,CAAC,CAAC;AACtD,8BAAsB,OAAO;IAI3B,IAAI,IAAI,IAAI,MAAM,GAAG,SAAS,CAE7B;IACD,QAAQ,CAAC,OAAO,IAAI,WAAW;IAC/B,MAAM,IAAI,GAAG;CACd;AAED,eAAO,MAAM,YAAY,sKAA0B,CAAC"}
@@ -0,0 +1,13 @@
1
+ import { createIocClass } from "@dreamkit/ioc";
2
+ import { createKind } from "@dreamkit/kind";
3
+ export const [kindService, isService] = createKind("@dreamkit/app/service");
4
+ export class Service {
5
+ static {
6
+ kindService(this);
7
+ }
8
+ get name() {
9
+ return;
10
+ }
11
+ onStop() { }
12
+ }
13
+ export const ServiceClass = createIocClass(Service);
@@ -2,5 +2,5 @@ import type { App } from "../App.js";
2
2
  import type { Route, RouteBuilder } from "../builders/RouteBuilder.js";
3
3
  export declare const kindApp: (input: object, name?: string) => void, isApp: (input: unknown) => input is App;
4
4
  export declare const kindRoute: (input: object, name?: string) => void, isRoute: (input: unknown) => input is Route;
5
- export declare const kindRouteBuilder: (input: object, name?: string) => void, isRouteBuilder: (input: unknown) => input is RouteBuilder<import("../builders/RouteBuilder.js").RouteData<import("../builders/RouteBuilder.js").RouteParams>>;
5
+ export declare const kindRouteBuilder: (input: object, name?: string) => void, isRouteBuilder: (input: unknown) => input is RouteBuilder<import("../builders/RouteBuilder.js").RouteData<import("../builders/RouteBuilder.js").RouteParams, import("../builders/RouteBuilder.js").RouteApi>>;
6
6
  //# sourceMappingURL=kind.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"kind.d.ts","sourceRoot":"","sources":["../../src/utils/kind.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,WAAW,CAAC;AACrC,OAAO,KAAK,EAAE,KAAK,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAGvE,eAAO,MAAO,OAAO,0CAAE,KAAK,kCAAoC,CAAC;AACjE,eAAO,MAAO,SAAS,0CAAE,OAAO,oCAA4C,CAAC;AAC7E,eAAO,MAAO,gBAAgB,0CAAE,cAAc,+IAE7C,CAAC"}
1
+ {"version":3,"file":"kind.d.ts","sourceRoot":"","sources":["../../src/utils/kind.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,WAAW,CAAC;AACrC,OAAO,KAAK,EAAE,KAAK,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAGvE,eAAO,MAAO,OAAO,0CAAE,KAAK,kCAAoC,CAAC;AACjE,eAAO,MAAO,SAAS,0CAAE,OAAO,oCAA4C,CAAC;AAC7E,eAAO,MAAO,gBAAgB,0CAAE,cAAc,+LAE7C,CAAC"}
@@ -0,0 +1,3 @@
1
+ import debug from "debug";
2
+ export declare const log: debug.Debugger;
3
+ //# sourceMappingURL=log.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"log.d.ts","sourceRoot":"","sources":["../../src/utils/log.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,eAAO,MAAM,GAAG,gBAAkB,CAAC"}
@@ -0,0 +1,2 @@
1
+ import debug from "debug";
2
+ export const log = debug("dk:app");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dreamkit/app",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "Set of utils to create applications.",
5
5
  "homepage": "https://dreamkit.dev",
6
6
  "repository": {
@@ -26,11 +26,15 @@
26
26
  "lib"
27
27
  ],
28
28
  "dependencies": {
29
+ "debug": "^4.3.7",
30
+ "@dreamkit/func": "0.0.2",
31
+ "@dreamkit/ioc": "0.0.1",
29
32
  "@dreamkit/kind": "0.0.2",
30
- "@dreamkit/schema": "0.0.2",
31
- "@dreamkit/utils": "0.0.2"
33
+ "@dreamkit/schema": "0.0.4",
34
+ "@dreamkit/utils": "0.0.4"
32
35
  },
33
36
  "devDependencies": {
37
+ "@types/debug": "^4.1.12",
34
38
  "@dreamkit/tsconfig": "0.0.1"
35
39
  },
36
40
  "x-dreamkit": {
package/lib/$route.d.ts DELETED
@@ -1,3 +0,0 @@
1
- import { RouteBuilder } from "./builders/RouteBuilder.js";
2
- export declare const $route: RouteBuilder<{}>;
3
- //# sourceMappingURL=$route.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"$route.d.ts","sourceRoot":"","sources":["../src/$route.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAE1D,eAAO,MAAM,MAAM,kBAAqC,CAAC"}
package/lib/$route.js DELETED
@@ -1,2 +0,0 @@
1
- import { RouteBuilder } from "./builders/RouteBuilder.js";
2
- export const $route = /*#__PURE__*/ new RouteBuilder({});