@loopback/core 4.0.0-alpha.9 → 4.0.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.
Files changed (121) hide show
  1. package/LICENSE +25 -0
  2. package/README.md +77 -2
  3. package/dist/application.d.ts +341 -0
  4. package/dist/application.js +554 -0
  5. package/dist/application.js.map +1 -0
  6. package/dist/component.d.ts +80 -0
  7. package/dist/component.js +59 -0
  8. package/dist/component.js.map +1 -0
  9. package/dist/extension-point.d.ts +121 -0
  10. package/dist/extension-point.js +227 -0
  11. package/dist/extension-point.js.map +1 -0
  12. package/dist/index.d.ts +21 -0
  13. package/dist/index.js +31 -0
  14. package/dist/index.js.map +1 -0
  15. package/dist/keys.d.ts +97 -0
  16. package/dist/keys.js +109 -0
  17. package/dist/keys.js.map +1 -0
  18. package/dist/lifecycle-registry.d.ts +91 -0
  19. package/dist/lifecycle-registry.js +191 -0
  20. package/dist/lifecycle-registry.js.map +1 -0
  21. package/dist/lifecycle.d.ts +47 -0
  22. package/dist/lifecycle.js +56 -0
  23. package/dist/lifecycle.js.map +1 -0
  24. package/dist/mixin-target.d.ts +60 -0
  25. package/{lib6/internal-types.js → dist/mixin-target.js} +2 -3
  26. package/dist/mixin-target.js.map +1 -0
  27. package/dist/server.d.ts +16 -0
  28. package/{lib6/component.js → dist/server.js} +2 -2
  29. package/dist/server.js.map +1 -0
  30. package/dist/service.d.ts +63 -0
  31. package/dist/service.js +151 -0
  32. package/dist/service.js.map +1 -0
  33. package/package.json +39 -37
  34. package/src/application.ts +719 -0
  35. package/src/component.ts +155 -0
  36. package/src/extension-point.ts +312 -0
  37. package/src/index.ts +29 -0
  38. package/src/keys.ts +144 -0
  39. package/src/lifecycle-registry.ts +268 -0
  40. package/src/lifecycle.ts +90 -0
  41. package/src/mixin-target.ts +69 -0
  42. package/src/server.ts +22 -0
  43. package/src/service.ts +211 -0
  44. package/index.d.ts +0 -6
  45. package/index.js +0 -9
  46. package/lib/application.d.ts +0 -54
  47. package/lib/application.js +0 -81
  48. package/lib/application.js.map +0 -1
  49. package/lib/component.d.ts +0 -2
  50. package/lib/component.js +0 -7
  51. package/lib/component.js.map +0 -1
  52. package/lib/http-handler.d.ts +0 -16
  53. package/lib/http-handler.js +0 -62
  54. package/lib/http-handler.js.map +0 -1
  55. package/lib/index.d.ts +0 -17
  56. package/lib/index.js +0 -37
  57. package/lib/index.js.map +0 -1
  58. package/lib/internal-types.d.ts +0 -29
  59. package/lib/internal-types.js +0 -8
  60. package/lib/internal-types.js.map +0 -1
  61. package/lib/keys.d.ts +0 -7
  62. package/lib/keys.js +0 -16
  63. package/lib/keys.js.map +0 -1
  64. package/lib/parser.d.ts +0 -11
  65. package/lib/parser.js +0 -96
  66. package/lib/parser.js.map +0 -1
  67. package/lib/promisify.d.ts +0 -3
  68. package/lib/promisify.js +0 -34
  69. package/lib/promisify.js.map +0 -1
  70. package/lib/router/metadata.d.ts +0 -12
  71. package/lib/router/metadata.js +0 -30
  72. package/lib/router/metadata.js.map +0 -1
  73. package/lib/router/routing-table.d.ts +0 -16
  74. package/lib/router/routing-table.js +0 -97
  75. package/lib/router/routing-table.js.map +0 -1
  76. package/lib/sequence.d.ts +0 -55
  77. package/lib/sequence.js +0 -99
  78. package/lib/sequence.js.map +0 -1
  79. package/lib/server.d.ts +0 -23
  80. package/lib/server.js +0 -64
  81. package/lib/server.js.map +0 -1
  82. package/lib/writer.d.ts +0 -11
  83. package/lib/writer.js +0 -34
  84. package/lib/writer.js.map +0 -1
  85. package/lib6/application.d.ts +0 -54
  86. package/lib6/application.js +0 -81
  87. package/lib6/application.js.map +0 -1
  88. package/lib6/component.d.ts +0 -2
  89. package/lib6/component.js.map +0 -1
  90. package/lib6/http-handler.d.ts +0 -16
  91. package/lib6/http-handler.js +0 -72
  92. package/lib6/http-handler.js.map +0 -1
  93. package/lib6/index.d.ts +0 -17
  94. package/lib6/index.js +0 -37
  95. package/lib6/index.js.map +0 -1
  96. package/lib6/internal-types.d.ts +0 -29
  97. package/lib6/internal-types.js.map +0 -1
  98. package/lib6/keys.d.ts +0 -7
  99. package/lib6/keys.js +0 -16
  100. package/lib6/keys.js.map +0 -1
  101. package/lib6/parser.d.ts +0 -11
  102. package/lib6/parser.js +0 -106
  103. package/lib6/parser.js.map +0 -1
  104. package/lib6/promisify.d.ts +0 -3
  105. package/lib6/promisify.js +0 -34
  106. package/lib6/promisify.js.map +0 -1
  107. package/lib6/router/metadata.d.ts +0 -12
  108. package/lib6/router/metadata.js +0 -30
  109. package/lib6/router/metadata.js.map +0 -1
  110. package/lib6/router/routing-table.d.ts +0 -16
  111. package/lib6/router/routing-table.js +0 -97
  112. package/lib6/router/routing-table.js.map +0 -1
  113. package/lib6/sequence.d.ts +0 -55
  114. package/lib6/sequence.js +0 -109
  115. package/lib6/sequence.js.map +0 -1
  116. package/lib6/server.d.ts +0 -23
  117. package/lib6/server.js +0 -74
  118. package/lib6/server.js.map +0 -1
  119. package/lib6/writer.d.ts +0 -11
  120. package/lib6/writer.js +0 -34
  121. package/lib6/writer.js.map +0 -1
package/LICENSE ADDED
@@ -0,0 +1,25 @@
1
+ Copyright (c) IBM Corp. and LoopBack contributors 2017,2019.
2
+ Node module: @loopback/core
3
+ This project is licensed under the MIT License, full text below.
4
+
5
+ --------
6
+
7
+ MIT license
8
+
9
+ Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ of this software and associated documentation files (the "Software"), to deal
11
+ in the Software without restriction, including without limitation the rights
12
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ copies of the Software, and to permit persons to whom the Software is
14
+ furnished to do so, subject to the following conditions:
15
+
16
+ The above copyright notice and this permission notice shall be included in
17
+ all copies or substantial portions of the Software.
18
+
19
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
+ THE SOFTWARE.
package/README.md CHANGED
@@ -1,13 +1,88 @@
1
- # @loopback.core
1
+ # @loopback/core
2
2
 
3
3
  LoopBack makes it easy to build modern applications that require complex
4
4
  integrations.
5
5
 
6
+ ## Overview
7
+
6
8
  - Fast, small, powerful, extensible core
7
9
  - Generate real APIs with a single command
8
10
  - Define your data and endpoints with OpenAPI
9
11
  - No maintenance of generated code
10
12
 
11
- # License
13
+ ## Installation
14
+
15
+ ```shell
16
+ $ npm install --save @loopback/core
17
+ ```
18
+
19
+ ## Basic Use
20
+
21
+ `@loopback/core` provides the foundation for your LoopBack app, but unlike
22
+ previous versions, it no longer contains the implementation for listening
23
+ servers.
24
+
25
+ For a typical example of how to create a REST server with your application, see
26
+ the
27
+ [@loopback/rest package.](https://github.com/loopbackio/loopback-next/tree/master/packages/rest)
28
+
29
+ ## Advanced Use
30
+
31
+ Since `@loopback/core` is decoupled from the listening server implementation,
32
+ LoopBack applications are now able to work with any component that provides this
33
+ functionality.
34
+
35
+ ```ts
36
+ // index.ts
37
+ import {Application} from '@loopback/core';
38
+ import {RestComponent} from '@loopback/rest';
39
+ import {GrpcComponent} from '@loopback/grpc';
40
+
41
+ const app = new Application({
42
+ rest: {
43
+ port: 3000,
44
+ },
45
+ grpc: {
46
+ port: 3001,
47
+ },
48
+ });
49
+ app.component(RestComponent); // REST Server
50
+ app.component(GrpcComponent)(
51
+ // GRPC Server
52
+
53
+ async function start() {
54
+ // Let's retrieve the bound instances of our servers.
55
+ const rest = await app.getServer<RestServer>('RestServer');
56
+ const grpc = await app.getServer<GrpcServer>('GrpcServer');
57
+
58
+ // Define all sorts of bindings here to pass configuration or data
59
+ // between your server instances, define controllers and datasources for them,
60
+ // etc...
61
+ await app.start(); // This automatically spins up all your servers, too!
62
+ console.log(`REST server running on port: ${rest.getSync('rest.port')}`);
63
+ console.log(`GRPC server running on port: ${grpc.getSync('grpc.port')}`);
64
+ },
65
+ )();
66
+ ```
67
+
68
+ In the above example, having a GRPC server mounted on your Application could
69
+ enable communication with other GRPC-enabled microservices, allowing things like
70
+ dynamic configuration updates.
71
+
72
+ ## Contributions
73
+
74
+ - [Guidelines](https://github.com/loopbackio/loopback-next/blob/master/docs/CONTRIBUTING.md)
75
+ - [Join the team](https://github.com/loopbackio/loopback-next/issues/110)
76
+
77
+ ## Tests
78
+
79
+ Run `npm test` from the root folder.
80
+
81
+ ## Contributors
82
+
83
+ See
84
+ [all contributors](https://github.com/loopbackio/loopback-next/graphs/contributors).
85
+
86
+ ## License
12
87
 
13
88
  MIT
@@ -0,0 +1,341 @@
1
+ /// <reference types="node" />
2
+ import { Binding, BindingFromClassOptions, Constructor, Context, DynamicValueProviderClass, Interceptor, InterceptorBindingOptions, JSONObject, Provider, ValueOrPromise } from '@loopback/context';
3
+ import { Component } from './component';
4
+ import { LifeCycleObserver } from './lifecycle';
5
+ import { Server } from './server';
6
+ import { ServiceOptions } from './service';
7
+ /**
8
+ * Application is the container for various types of artifacts, such as
9
+ * components, servers, controllers, repositories, datasources, connectors,
10
+ * and models.
11
+ */
12
+ export declare class Application extends Context implements LifeCycleObserver {
13
+ readonly options: ApplicationConfig;
14
+ /**
15
+ * A flag to indicate that the application is being shut down
16
+ */
17
+ private _isShuttingDown;
18
+ private _shutdownOptions;
19
+ private _signalListener;
20
+ private _initialized;
21
+ /**
22
+ * State of the application
23
+ */
24
+ private _state;
25
+ /**
26
+ * Get the state of the application. The initial state is `created` and it can
27
+ * transition as follows by `start` and `stop`:
28
+ *
29
+ * 1. start
30
+ * - !started -> starting -> started
31
+ * - started -> started (no-op)
32
+ * 2. stop
33
+ * - (started | initialized) -> stopping -> stopped
34
+ * - ! (started || initialized) -> stopped (no-op)
35
+ *
36
+ * Two types of states are expected:
37
+ * - stable, such as `started` and `stopped`
38
+ * - in process, such as `booting` and `starting`
39
+ *
40
+ * Operations such as `start` and `stop` can only be called at a stable state.
41
+ * The logic should immediately set the state to a new one indicating work in
42
+ * process, such as `starting` and `stopping`.
43
+ */
44
+ get state(): string;
45
+ /**
46
+ * Create an application with the given parent context
47
+ * @param parent - Parent context
48
+ */
49
+ constructor(parent: Context);
50
+ /**
51
+ * Create an application with the given configuration and parent context
52
+ * @param config - Application configuration
53
+ * @param parent - Parent context
54
+ */
55
+ constructor(config?: ApplicationConfig, parent?: Context);
56
+ /**
57
+ * Register a controller class with this application.
58
+ *
59
+ * @param controllerCtor - The controller class
60
+ * (constructor function).
61
+ * @param name - Optional controller name, default to the class name
62
+ * @returns The newly created binding, you can use the reference to
63
+ * further modify the binding, e.g. lock the value to prevent further
64
+ * modifications.
65
+ *
66
+ * @example
67
+ * ```ts
68
+ * class MyController {
69
+ * }
70
+ * app.controller(MyController).lock();
71
+ * ```
72
+ */
73
+ controller<T>(controllerCtor: ControllerClass<T>, nameOrOptions?: string | BindingFromClassOptions): Binding<T>;
74
+ /**
75
+ * Bind a Server constructor to the Application's master context.
76
+ * Each server constructor added in this way must provide a unique prefix
77
+ * to prevent binding overlap.
78
+ *
79
+ * @example
80
+ * ```ts
81
+ * app.server(RestServer);
82
+ * // This server constructor will be bound under "servers.RestServer".
83
+ * app.server(RestServer, "v1API");
84
+ * // This server instance will be bound under "servers.v1API".
85
+ * ```
86
+ *
87
+ * @param server - The server constructor.
88
+ * @param nameOrOptions - Optional override for name or options.
89
+ * @returns Binding for the server class
90
+ *
91
+ */
92
+ server<T extends Server>(ctor: Constructor<T>, nameOrOptions?: string | BindingFromClassOptions): Binding<T>;
93
+ /**
94
+ * Bind an array of Server constructors to the Application's master
95
+ * context.
96
+ * Each server added in this way will automatically be named based on the
97
+ * class constructor name with the "servers." prefix.
98
+ *
99
+ * @remarks
100
+ * If you wish to control the binding keys for particular server instances,
101
+ * use the app.server function instead.
102
+ * ```ts
103
+ * app.servers([
104
+ * RestServer,
105
+ * GRPCServer,
106
+ * ]);
107
+ * // Creates a binding for "servers.RestServer" and a binding for
108
+ * // "servers.GRPCServer";
109
+ * ```
110
+ *
111
+ * @param ctors - An array of Server constructors.
112
+ * @returns An array of bindings for the registered server classes
113
+ *
114
+ */
115
+ servers<T extends Server>(ctors: Constructor<T>[]): Binding[];
116
+ /**
117
+ * Retrieve the singleton instance for a bound server.
118
+ *
119
+ * @typeParam T - Server type
120
+ * @param ctor - The constructor that was used to make the
121
+ * binding.
122
+ * @returns A Promise of server instance
123
+ *
124
+ */
125
+ getServer<T extends Server>(target: Constructor<T> | string): Promise<T>;
126
+ /**
127
+ * Assert there is no other operation is in progress, i.e., the state is not
128
+ * `*ing`, such as `starting` or `stopping`.
129
+ *
130
+ * @param op - The operation name, such as 'boot', 'start', or 'stop'
131
+ */
132
+ protected assertNotInProcess(op: string): void;
133
+ /**
134
+ * Assert current state of the application to be one of the expected values
135
+ * @param op - The operation name, such as 'boot', 'start', or 'stop'
136
+ * @param states - Valid states
137
+ */
138
+ protected assertInStates(op: string, ...states: string[]): void;
139
+ /**
140
+ * Transition the application to a new state and emit an event
141
+ * @param state - The new state
142
+ */
143
+ protected setState(state: string): void;
144
+ protected awaitState(state: string): Promise<void>;
145
+ /**
146
+ * Initialize the application, and all of its registered observers. The
147
+ * application state is checked to ensure the integrity of `initialize`.
148
+ *
149
+ * If the application is already initialized, no operation is performed.
150
+ *
151
+ * This method is automatically invoked by `start()` if the application is not
152
+ * initialized.
153
+ */
154
+ init(): Promise<void>;
155
+ /**
156
+ * Register a function to be called when the application initializes.
157
+ *
158
+ * This is a shortcut for adding a binding for a LifeCycleObserver
159
+ * implementing a `init()` method.
160
+ *
161
+ * @param fn The function to invoke, it can be synchronous (returning `void`)
162
+ * or asynchronous (returning `Promise<void>`).
163
+ * @returns The LifeCycleObserver binding created.
164
+ */
165
+ onInit(fn: () => ValueOrPromise<void>): Binding<LifeCycleObserver>;
166
+ /**
167
+ * Start the application, and all of its registered observers. The application
168
+ * state is checked to ensure the integrity of `start`.
169
+ *
170
+ * If the application is not initialized, it calls first `init()` to
171
+ * initialize the application. This only happens if `start()` is called for
172
+ * the first time.
173
+ *
174
+ * If the application is already started, no operation is performed.
175
+ */
176
+ start(): Promise<void>;
177
+ /**
178
+ * Register a function to be called when the application starts.
179
+ *
180
+ * This is a shortcut for adding a binding for a LifeCycleObserver
181
+ * implementing a `start()` method.
182
+ *
183
+ * @param fn The function to invoke, it can be synchronous (returning `void`)
184
+ * or asynchronous (returning `Promise<void>`).
185
+ * @returns The LifeCycleObserver binding created.
186
+ */
187
+ onStart(fn: () => ValueOrPromise<void>): Binding<LifeCycleObserver>;
188
+ /**
189
+ * Stop the application instance and all of its registered observers. The
190
+ * application state is checked to ensure the integrity of `stop`.
191
+ *
192
+ * If the application is already stopped or not started, no operation is
193
+ * performed.
194
+ */
195
+ stop(): Promise<void>;
196
+ /**
197
+ * Register a function to be called when the application starts.
198
+ *
199
+ * This is a shortcut for adding a binding for a LifeCycleObserver
200
+ * implementing a `start()` method.
201
+ *
202
+ * @param fn The function to invoke, it can be synchronous (returning `void`)
203
+ * or asynchronous (returning `Promise<void>`).
204
+ * @returns The LifeCycleObserver binding created.
205
+ */
206
+ onStop(fn: () => ValueOrPromise<void>): Binding<LifeCycleObserver>;
207
+ private getLifeCycleObserverRegistry;
208
+ /**
209
+ * Add a component to this application and register extensions such as
210
+ * controllers, providers, and servers from the component.
211
+ *
212
+ * @param componentCtor - The component class to add.
213
+ * @param nameOrOptions - Optional component name or options, default to the
214
+ * class name
215
+ *
216
+ * @example
217
+ * ```ts
218
+ *
219
+ * export class ProductComponent {
220
+ * controllers = [ProductController];
221
+ * repositories = [ProductRepo, UserRepo];
222
+ * providers = {
223
+ * [AUTHENTICATION_STRATEGY]: AuthStrategy,
224
+ * [AUTHORIZATION_ROLE]: Role,
225
+ * };
226
+ * };
227
+ *
228
+ * app.component(ProductComponent);
229
+ * ```
230
+ */
231
+ component<T extends Component = Component>(componentCtor: Constructor<T>, nameOrOptions?: string | BindingFromClassOptions): Binding<T>;
232
+ /**
233
+ * Set application metadata. `@loopback/boot` calls this method to populate
234
+ * the metadata from `package.json`.
235
+ *
236
+ * @param metadata - Application metadata
237
+ */
238
+ setMetadata(metadata: ApplicationMetadata): void;
239
+ /**
240
+ * Register a life cycle observer class
241
+ * @param ctor - A class implements LifeCycleObserver
242
+ * @param nameOrOptions - Optional name or options for the life cycle observer
243
+ */
244
+ lifeCycleObserver<T extends LifeCycleObserver>(ctor: Constructor<T>, nameOrOptions?: string | BindingFromClassOptions): Binding<T>;
245
+ /**
246
+ * Add a service to this application.
247
+ *
248
+ * @param cls - The service or provider class
249
+ *
250
+ * @example
251
+ *
252
+ * ```ts
253
+ * // Define a class to be bound via ctx.toClass()
254
+ * @injectable({scope: BindingScope.SINGLETON})
255
+ * export class LogService {
256
+ * log(msg: string) {
257
+ * console.log(msg);
258
+ * }
259
+ * }
260
+ *
261
+ * // Define a class to be bound via ctx.toProvider()
262
+ * import {v4 as uuidv4} from 'uuid';
263
+ * export class UuidProvider implements Provider<string> {
264
+ * value() {
265
+ * return uuidv4();
266
+ * }
267
+ * }
268
+ *
269
+ * // Register the local services
270
+ * app.service(LogService);
271
+ * app.service(UuidProvider, 'uuid');
272
+ *
273
+ * export class MyController {
274
+ * constructor(
275
+ * @inject('services.uuid') private uuid: string,
276
+ * @inject('services.LogService') private log: LogService,
277
+ * ) {
278
+ * }
279
+ *
280
+ * greet(name: string) {
281
+ * this.log(`Greet request ${this.uuid} received: ${name}`);
282
+ * return `${this.uuid}: ${name}`;
283
+ * }
284
+ * }
285
+ * ```
286
+ */
287
+ service<S>(cls: ServiceOrProviderClass<S>, nameOrOptions?: string | ServiceOptions): Binding<S>;
288
+ /**
289
+ * Register an interceptor
290
+ * @param interceptor - An interceptor function or provider class
291
+ * @param nameOrOptions - Binding name or options
292
+ */
293
+ interceptor(interceptor: Interceptor | Constructor<Provider<Interceptor>>, nameOrOptions?: string | InterceptorBindingOptions): Binding<Interceptor>;
294
+ /**
295
+ * Set up signals that are captured to shutdown the application
296
+ */
297
+ protected setupShutdown(): (signal: string) => Promise<void>;
298
+ private registerSignalListener;
299
+ private removeSignalListener;
300
+ }
301
+ /**
302
+ * Options to set up application shutdown
303
+ */
304
+ export declare type ShutdownOptions = {
305
+ /**
306
+ * An array of signals to be trapped for graceful shutdown
307
+ */
308
+ signals?: NodeJS.Signals[];
309
+ /**
310
+ * Period in milliseconds to wait for the grace shutdown to finish before
311
+ * exiting the process
312
+ */
313
+ gracePeriod?: number;
314
+ };
315
+ /**
316
+ * Configuration for application
317
+ */
318
+ export interface ApplicationConfig {
319
+ /**
320
+ * Name of the application context
321
+ */
322
+ name?: string;
323
+ /**
324
+ * Configuration for signals that shut down the application
325
+ */
326
+ shutdown?: ShutdownOptions;
327
+ /**
328
+ * Other properties
329
+ */
330
+ [prop: string]: any;
331
+ }
332
+ export declare type ControllerClass<T = any> = Constructor<T>;
333
+ export declare type ServiceOrProviderClass<T = any> = Constructor<T | Provider<T>> | DynamicValueProviderClass<T>;
334
+ /**
335
+ * Type description for `package.json`
336
+ */
337
+ export interface ApplicationMetadata extends JSONObject {
338
+ name: string;
339
+ version: string;
340
+ description: string;
341
+ }