Skip to content

Commit e2be3a8

Browse files
Kevin Delislekjdelisle
Kevin Delisle
authored andcommitted
docs: move documentation into monorepo
Migrate the existing lb4 files under the docs folder of the monorepo
1 parent dbad75a commit e2be3a8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+6766
-1
lines changed

docs/Application-generator.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
---
2+
lang: en
3+
title: 'Application generator'
4+
keywords: LoopBack 4.0, LoopBack 4
5+
tags:
6+
sidebar: lb4_sidebar
7+
permalink: /doc/en/lb4/Application-generator.html
8+
summary:
9+
---
10+
11+
### Synopsis
12+
13+
Creates a new LoopBack4 application using REST API.
14+
15+
```
16+
lb4 [app] [options] [<name>]
17+
```
18+
19+
### Options
20+
21+
`--applicationName`
22+
: Application name.
23+
24+
`--description`
25+
: Description of the application.
26+
27+
`--outDir`
28+
: Project root directory for the application.
29+
30+
`--tslint`
31+
: Add TSLint to LoopBack4 application project.
32+
33+
`--prettier`
34+
: Add Prettier to LoopBack4 application project.
35+
36+
`--mocha`
37+
: Add Mocha to LoopBack4 application project.
38+
39+
`--loopbackBuild`
40+
: Add @loopback/build module's script set to LoopBack4 application project.
41+
42+
{% include_relative includes/CLI-std-options.md %}
43+
44+
### Arguments
45+
46+
`<name>` - Optional name of the application given as an argument to the command. 
47+
If provided, the tool will use that as the default when prompting for the name.
48+
49+
### Interactive Prompts
50+
51+
The tool will prompt you for:
52+
53+
- Name of the application as will be shown in `package.json`.
54+
If the name had been supplied from the command-line, the prompt is skipped and the application is built with the name from the command-line argument.
55+
Must follow npm naming conventions.
56+
57+
- Description of the application as will be shown in `package.json`.
58+
59+
- Name of the directory in which to create your application.
60+
Defaults to the name of the application previously entered.
61+
62+
- Name of the Application class in `application.ts`.
63+
Defaults to <code><i>name</i>Application</code>.
64+
65+
- Optional modules to add to the application. These modules are helpful tools to help format, test, and build a LoopBack4 application.
66+
Defaults to `true` for all of the modules.
67+
The prompted modules are:
68+
69+
- [`tslint`](https://www.npmjs.com/package/tslint)
70+
- [`prettier`](https://www.npmjs.com/package/prettier)
71+
- [`mocha`](https://www.npmjs.com/package/mocha)
72+
- [`@loopback/build`](https://www.npmjs.com/package/@loopback/build)
73+
74+
### Output
75+
76+
The core scaffold of a LoopBack4 application generated by the CLI consists of the following files and directories:
77+
78+
```
79+
.
80+
├── src/
81+
| ├── controllers/
82+
| | └── ping.controller.ts
83+
| ├── datasources/
84+
| ├── models/
85+
| ├── repositories/
86+
| ├── application.ts
87+
| ├── index.ts
88+
| └── sequence.ts
89+
├── test/
90+
└── package.json
91+
```
92+
93+
`ping.controller.ts` is a file used to provide the application with a responsive endpoint.
94+
It contains logic to respond with a greeting message when the appliaction receives a `GET` request from endpoint `/ping`.
95+
96+
`cd` to the application's newly created directory and run `npm start` to see the application running at `localhost:3000`.
97+
Go to `localhost:3000/ping` to be greeted with a message.
98+
99+
Once the application has been created, additional generators such as [controller generator](Controller-generator.md) can be run from the application's root directory to further scaffold the application.

docs/Application.md

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
---
2+
lang: en
3+
title: 'Application'
4+
keywords: LoopBack 4.0, LoopBack 4
5+
tags:
6+
sidebar: lb4_sidebar
7+
permalink: /doc/en/lb4/Application.html
8+
summary:
9+
---
10+
11+
## What is an Application?
12+
13+
In LoopBack 4, the [`Application`](http://apidocs.strongloop.com/@loopback%2fcore/#Application)
14+
class is the central class for setting up all of your module's components,
15+
controllers, servers and bindings. The `Application` class extends
16+
[Context](Context.md), and provides the controls for starting and stopping
17+
itself and its associated servers.
18+
19+
When using LoopBack 4, we strongly encourage you to create your own subclass
20+
of `Application` to better organize your configuration and setup.
21+
22+
## Making your own application class
23+
24+
By making your own application class, you can perform several additional
25+
tasks as a part of your setup:
26+
- Pass configuration into the base class constructor
27+
- Perform some asynchronous wireup before application start
28+
- Perform some graceful cleanup on application stop
29+
30+
{% include code-caption.html content="src/widget-application.ts" %}
31+
```ts
32+
import {Application} from '@loopback/core';
33+
import {RestComponent, RestServer} from '@loopback/rest';
34+
import {SamoflangeController, DoohickeyController} from './controllers';
35+
import {WidgetApi} from './apidef/';
36+
37+
export class WidgetApplication extends Application {
38+
constructor() {
39+
// This is where you would pass configuration to the base constructor
40+
// (as well as handle your own!)
41+
super();
42+
const app = this; // For clarity.
43+
// You can bind to the Application-level context here.
44+
// app.bind('foo').to(bar);
45+
app.component(RestComponent);
46+
app.controller(SamoflangeController);
47+
app.controller(DoohickeyController);
48+
}
49+
50+
async start() {
51+
// This is where you would asynchronously retrieve servers, providers and
52+
// other components to configure them before launch.
53+
const server = await app.getServer(RestServer);
54+
server.bind('rest.port').to(8080);
55+
server.api(WidgetApi);
56+
// The superclass start method will call start on all servers that are
57+
// bound to the application.
58+
return await super.start();
59+
}
60+
61+
async stop() {
62+
// This is where you would do whatever is necessary before stopping your
63+
// app (graceful closing of connections, flushing buffers, etc)
64+
console.log('Widget application is shutting down...')
65+
// The superclass stop method will call stop on all servers that are
66+
// bound to the application.
67+
await super.stop();
68+
}
69+
}
70+
71+
```
72+
73+
## Configuring your application
74+
Your application can be configured with constructor arguments, bindings, or
75+
a combination of both.
76+
77+
### Binding configuration
78+
Binding is the most commonly-demonstrated form of application configuration
79+
throughout our examples, and is the recommended method for setting up your
80+
application.
81+
82+
In addition to the binding functions provided by [Context](Context.md),
83+
the `Application` class also provides some sugar functions for commonly used
84+
bindings, like `component`, `server` and `controller`:
85+
86+
```ts
87+
export class MyApplication extends Application {
88+
constructor() {
89+
super();
90+
this.component(MagicSuite);
91+
this.server(RestServer, 'public');
92+
this.server(RestServer, 'private');
93+
94+
this.controller(FooController);
95+
this.controller(BarController);
96+
this.controller(BazController);
97+
}
98+
}
99+
```
100+
101+
You can find a complete list of these functions on the
102+
[`Application`](http://apidocs.loopback.io/@loopback%2fcore/#Application) API
103+
docs page.
104+
105+
Additionally, you can use more advanced forms of binding to fine-tune your
106+
application's configuration:
107+
108+
```ts
109+
export class MyApplication extends Application {
110+
constructor() {
111+
super();
112+
this.server(RestServer);
113+
this.controller(FooController);
114+
this.bind('fooCorp.logger').toProvider(LogProvider);
115+
this.bind('repositories.widget')
116+
.toClass(WidgetRepository)
117+
.inScope(BindingScope.SINGLETON);
118+
}
119+
}
120+
```
121+
In the above example:
122+
- injection calls for `fooCorp.logger` will be handled by the `LogProvider`
123+
class.
124+
- injection calls for `repositories.widget` will be handled by a singleton
125+
instance of the `WidgetRepository` class.
126+
127+
#### Components
128+
```ts
129+
app.component(MyComponent);
130+
app.component(RestComponent);
131+
```
132+
The `component` function allows binding of component constructors within
133+
your `Application` instance's context.
134+
135+
For more information on how to make use of components,
136+
see [Using Components](Using-components.md).
137+
138+
#### Controllers
139+
```ts
140+
app.controller(FooController);
141+
app.controller(BarController);
142+
```
143+
Much like the component function, the `controller` function allows
144+
binding of [Controllers](Controllers.md) to the `Application` context.
145+
146+
#### Servers
147+
```ts
148+
app.server(RestServer);
149+
app.servers([MyServer, GrpcServer]);
150+
```
151+
The `server` function is much like the previous functions, but
152+
with [Servers](server.md) bulk bindings are possible through the function
153+
`servers`.
154+
155+
```ts
156+
const app = new Application();
157+
app.server(RestServer, 'public'); // {'public': RestServer}
158+
app.server(RestServer, 'private'); // {'private': RestServer}
159+
```
160+
In the above example, the two server instances would be bound to the Application
161+
context under the keys `servers.public`, and `servers.private` respectively.
162+
163+
### Constructor configuration
164+
165+
The `Application` class constructor also accepts an
166+
[`ApplicationConfig`](http://apidocs.strongloop.com/@loopback%2fcore/#ApplicationConfig)
167+
object which contains component-level configurations such as
168+
[`RestServerConfig`](http://apidocs.strongloop.com/@loopback%2frest/#RestServerConfig).
169+
It will automatically create bindings for these configurations and later be injected
170+
through dependency injections. Visit [Dependency Injection](Dependency-injection.md)
171+
for more details.
172+
173+
{% include note.html content="
174+
Binding configuration such as component binding, provider binding, or binding scopes
175+
are not possible with the constructor-based configuration approach.
176+
" %}
177+
178+
```ts
179+
export class MyApplication extends RestApplication {
180+
constructor() {
181+
super({
182+
rest: {
183+
port: 4000,
184+
host: 'my-host'
185+
}
186+
})
187+
}
188+
}
189+
```
190+
191+
## Tips for application setup
192+
Here are some tips to help avoid common pitfalls and mistakes.
193+
194+
### Use unique bindings
195+
Use binding names that are prefixed with a unique string that does not overlap
196+
with loopback's bindings. As an example, if your application is built for
197+
your employer FooCorp, you can prefix your bindings with `fooCorp`.
198+
```ts
199+
// This is unlikely to conflict with keys used by other component developers
200+
// or within loopback itself!
201+
app.bind('fooCorp.widgetServer.config').to(widgetServerConfig);
202+
```
203+
204+
### Avoid use of `getSync`
205+
We provide the [`getSync`](http://apidocs.loopback.io/@loopback%2fcontext/#getSync)
206+
function for scenarios where you cannot asynchronously retrieve your bindings,
207+
such as in constructor bodies.
208+
209+
However, the number of scenarios in which you must do this are limited, and you
210+
should avoid potential race conditions and retrieve your bindings asynchronously
211+
using the [`get`](http://apidocs.loopback.io/@loopback%2fcontext/#get) function
212+
whenever possible.
213+
214+
### Use caution with singleton binding scopes
215+
By default, bindings for controllers will instantiate a new instance whenever
216+
they are injected or retrieved from their binding. Your application should only
217+
set singleton binding scopes on controllers when it makes sense to do so.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
lang: en
3+
title: 'Best practices with Loopback 4'
4+
keywords: LoopBack 4.0, LoopBack 4
5+
tags:
6+
sidebar: lb4_sidebar
7+
permalink: /doc/en/lb4/Best-practices-with-Loopback-4.html
8+
summary:
9+
---
10+
11+
LoopBack 4 is more than just a framework: It’s an ecosystem that encourages developers to follow best practices through predefined standards. This section will walk through some important guidelines by building an example API for a catalog of products.
12+
13+
Our best practice follows an "API first" and test-driven development approach:
14+
15+
1. [**Defining and validating the API**](./Defining-and-validating-the-API.md): This section guides you through constructing your API first before any internal logic is added.
16+
2. [**Testing the API**](./Testing-the-API.md): This section describes the process of writing smoke test for your API and its spec.
17+
3. [**Defining your testing strategy**](./Defining-your-testing-strategy.md): This section discusses the advantages and the process of building a strong testing suite.
18+
4. [**Implementing features**](./Implementing-features.md): This section demonstrates how the tests for each feature of your application should be written, and how to write the logic to make these tests pass. In the example, the tests for the controller, model, repository, data source, and sequence are written and then implemented.
19+
5. [**Preparing the API for consumption**](./Preparing-the-API-for-consumption.md): This section shows how the endpoints can be physically tested using the Swagger UI.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
lang: en
3+
title: 'Calling other APIs and web services'
4+
keywords: LoopBack 4.0, LoopBack 4
5+
tags:
6+
sidebar: lb4_sidebar
7+
permalink: /doc/en/lb4/Calling-other-APIs-and-web-services.html
8+
summary:
9+
---
10+
Your API implementation often needs to interact with REST APIs, SOAP Web Services or other forms of APIs.
11+
12+
How to:
13+
- Set up a Service
14+
- Use services with controllers
15+
- Reuse models between services and controllers
16+
17+
{% include content/tbd.html %}

docs/Command-line-interface.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
lang: en
3+
title: 'Command-line interface'
4+
keywords: LoopBack 4.0, LoopBack 4
5+
tags:
6+
sidebar: lb4_sidebar
7+
permalink: /doc/en/lb4/Command-line-interface.html
8+
summary:
9+
---
10+
11+
LoopBack 4 provides command-line tools to help you get started quickly. The command line tools generate application and extension projects and install their dependencies for you.
12+
The CLI can also help you generate artifacts, such as controllers, for your projects.
13+
Once generated, the scaffold can be expanded with users' own code as needed.
14+
15+
To use LoopBack 4's CLI, run this command:
16+
17+
```
18+
npm install -g @loopback/cli
19+
```
20+
21+
## Generating LoopBack projects
22+
23+
{% include content/lb4-project-commands.html %}
24+
25+
## Generating LoopBack artifacts
26+
27+
{% include content/lb4-artifact-commands.html %}

0 commit comments

Comments
 (0)