Skip to content

Commit

Permalink
Update Creating-components.md
Browse files Browse the repository at this point in the history
Copy edits, grammar, usage, etc.
  • Loading branch information
crandmck authored Nov 9, 2017
1 parent a5ccfe0 commit f9320d4
Showing 1 changed file with 32 additions and 31 deletions.
63 changes: 32 additions & 31 deletions pages/en/lb4/Creating-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ export class MyComponent {
}
```

When a component is mounted to an application, a new instance of the component class is created and then
- each Controller class is registered via `app.controller()`,
- each Provider is bound to its key in `providers` object.
When a component is mounted to an application, a new instance of the component class is created and then:
- Each Controller class is registered via `app.controller()`,
- Each Provider is bound to its key in `providers` object.

The example `MyComponent` above will add `MyController` to application's API and create a new binding `my.value` that will be resolved using `MyValueProvider`.

## Providers

Providers are the mechanism allowing components to export values that can be used by the target application or other components. A provider is a class that provides `value()` function, this function is called by [Context](Context.html) whenever another entity requests a value to be injected.
Providers enable components to export values that can be used by the target application or other components. The `Provider` class provides a `value()` function called by [Context](Context.html) when another entity requests a value to be injected.

```js
import {Provider} from '@loopback/context';
Expand Down Expand Up @@ -63,7 +63,7 @@ export class MyComponent {

### Accessing values from Providers

Applications should use `@inject` decorators to access the value provided by our exported provider.
Applications can use `@inject` decorators to access the value of an exported Provider.
If you’re not familiar with decorators in TypeScript, see [Key Concepts: Decorators](Decorators.html)

```js
Expand All @@ -84,7 +84,8 @@ class MyController {
}
```

A note on binding names: In order to avoid name conflicts, always add a unique prefix to your binding key (`my-component.` in the example above). See [Reserved binding keys](Reserved-binding-keys.html) for the list of keys reserved for the framework use.
{% include note.html title="A note on binding names" content="To avoid name conflicts, add a unique prefix to your binding key (for example, `my-component.` in the example above). See [Reserved binding keys](Reserved-binding-keys.html) for the list of keys reserved for the framework use.
" %}

### Asynchronous providers

Expand All @@ -107,7 +108,7 @@ In this case, LoopBack will wait until the promise returned by `value()` is reso
### Working with HTTP request/response
In some cases, the provider may depend on other parts of LoopBack, for example the current `request` object. Such dependencies should be listed in provider's constructor and annotated with `@inject` keyword, so that LoopBack runtime can resolve them automatically for you.
In some cases, the Provider may depend on other parts of LoopBack; for example the current `request` object. The Provider's constructor should list such dependencies annotated with `@inject` keyword, so that LoopBack runtime can resolve them automatically.
```js
const uuid = require('uuid/v4');
Expand All @@ -125,7 +126,7 @@ class CorrelationIdProvider {
## Modifying request handling logic
A frequent use case for components is to modify the way how all requests are handled. For example, the authentication component needs to verify user credentials before the actual handler can be invoked, a logger component needs to record start time and write a log entry when the request has been handled.
A frequent use case for components is to modify the way requests are handled. For example, the authentication component needs to verify user credentials before the actual handler can be invoked; or a logger component needs to record start time and write a log entry when the request has been handled.
The idiomatic solution has two parts:
Expand Down Expand Up @@ -179,7 +180,7 @@ The idiomatic solution has two parts:
### Accessing Elements contributed by other Sequence Actions
When writing a custom sequence action, you need to access Elements contributed by other actions run in the sequence. For example, `authenticate()` action needs information about the invoked route in order to decide whether and how to authenticate the request.
When writing a custom sequence action, you need to access Elements contributed by other actions run in the sequence. For example, `authenticate()` action needs information about the invoked route to decide whether and how to authenticate the request.
Because all Actions are resolved before the Sequence `handle` function is run, Elements contributed by Actions are not available for injection yet. To solve this problem, use `@inject.getter` decorator to obtain a getter function instead of the actual value. This allows you to defer resolution of your dependency only until the sequence action contributing this value has already finished.
Expand Down Expand Up @@ -232,14 +233,13 @@ export class AuthenticationProvider {
## Extends Application with Mixin
When binding a component to an app, you may want to extend the app with the component's
properties and methods.
This can be achieved by using mixins.
properties and methods by using mixins.
If you are not familiar with the mixin concept, check [Mixin](Mixin.html) to learn more.
For an overview of mixins, see [Mixin](Mixin.html).
An example of how a mixin leverages component would be `RepositoryMixin`:
Suppose an app has multiple components with repositories bound to each of them,
you can use function `RepositoryMixin` to mount those repositories to application level context.
An example of how a mixin leverages a component is `RepositoryMixin`.
Suppose an app has multiple components with repositories bound to each of them.
You can use function `RepositoryMixin()` to mount those repositories to application level context.
The following snippet is an abbreviated function
[`RepositoryMixin`](https://github.com/strongloop/loopback-next/blob/master/packages/repository/src/repository-mixin.ts):
Expand Down Expand Up @@ -291,7 +291,7 @@ app.find('repositories.*');
## Configuring components
More often than not, the component may want to offer different value providers depending on the configuration. For example, a component providing Email API may offer different transports (stub, SMTP, etc.).
More often than not, the component may want to offer different value providers depending on the configuration. For example, a component providing an email API may offer different transports (stub, SMTP, and so on).
Components should use constructor-level [Dependency Injection](Context.html#dependency-injection) to receive the configuration from the application.
Expand All @@ -311,8 +311,8 @@ class EmailComponent {
## Creating your own servers
LoopBack 4 has the concept of a Server, which you can use to create your own
implementations of REST, SOAP, gRPC, MQTT and more. For an overview, see the
[Server](#Server.html) section.
implementations of REST, SOAP, gRPC, MQTT and more. For an overview, see
[Server](Server.html).
### A Basic Example
The concept of servers in LoopBack doesn't actually require listening on ports;
Expand Down Expand Up @@ -353,7 +353,7 @@ export class ReportProcessingServer implements Server {
}
```
In this example, the `ReportProcessingServer` will load new reports every two
minutes from the database (using the provided repository), process those entries
minutes from the database (using the provided repository), process those entries,
and then update the database.
### Using your server
Expand All @@ -376,17 +376,17 @@ concept to leverage for creating your custom servers.
### Controllers and routing
LoopBack 4 developers are strongly encouraged to use controllers for their
modules, and this naturally leads into the concept of routing.
modules, and this naturally leads to the concept of routing.
No matter what protocol you intend to use for your custom server, you'll need
to use some algorithm to determine _which_ controller and function to send
request data to, and that means you need a router.
To demonstrate this concept, we'll make a toy protocol similar to the JSON RPC
For example, consider a "toy protocol" similar to the JSON RPC
specification (but nowhere near as complete or robust).
Our protocol will require a JSON payload with three properties: `controller`,
`method` and `input`.
The toy protocol will require a JSON payload with three properties: `controller`,
`method`, and `input`.
An example request would look something like this:
```json
Expand All @@ -399,8 +399,8 @@ An example request would look something like this:
}
```
### Defining our controller
Let's create our GreetController, as well as a Person type:
### Defining the controller
Here is the `GreetController` and the `Person` type:
{% include code-caption.html content="/src/controllers/greet.controller.ts" %}
Expand All @@ -427,8 +427,8 @@ export type Person = {
```
### Defining our router and server
Now that we have a controller to test with, let's create our Router and Server.
For this example, we'll use [express](https://expressjs.com/) and the
Now that there is a controller to test with, the following code creates a `Router` and `Server`.
This example uses [express](https://expressjs.com/) and the
`@types/express` package.
{% include code-caption.html content="/src/server/rpc-router.ts" %}
Expand Down Expand Up @@ -475,8 +475,8 @@ export class RPCRouter {
}
```
In addition to the server, we'll also include a small type definition for
convenience with our configuration.
In addition to the server, the example will include a small type definition for
convenience.
{% include code-caption.html content="/src/server/rpc-server.ts" %}
```ts
Expand Down Expand Up @@ -522,7 +522,7 @@ export type RPCServerConfig = {
```
### Putting it all together
Now we'll create our Application subclass and `index.ts` entrypoint to
Now here is the Application subclass and `index.ts` entry point to
make it go!
{% include code-caption.html content="/src/application.ts" %}
Expand Down Expand Up @@ -569,7 +569,8 @@ process.on('SIGINT', shutdown);
```
### Trying it out
Let's start our server up and run a few REST requests. Feel free to use
Now, try it out: start the server and run a few REST requests. Feel free to use
whatever REST client you'd prefer (this example will use `curl`).
```sh
# Basic Greeting Calls
Expand Down

0 comments on commit f9320d4

Please sign in to comment.