Skip to content

Commit

Permalink
chore(docs): use @loopback/core for imports of @loopback/context
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Jun 1, 2020
1 parent 15c0f32 commit 6b2a54c
Show file tree
Hide file tree
Showing 31 changed files with 124 additions and 66 deletions.
2 changes: 1 addition & 1 deletion docs/site/BelongsTo-relation.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ The following code snippet shows how it would look like:
content="/src/repositories/order.repository.ts" %}

```ts
import {Getter, inject} from '@loopback/context';
import {Getter, inject} from '@loopback/core';
import {
BelongsToAccessor,
DefaultCrudRepository,
Expand Down
26 changes: 21 additions & 5 deletions docs/site/Binding.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ There are a few ways to create a binding:
- Use `Binding` constructor:

```ts
import {Context, Binding} from '@loopback/core';
const context = new Context();
const binding = new Binding('my-key');
ctx.add(binding);
Expand All @@ -40,6 +41,7 @@ There are a few ways to create a binding:
- Use `Binding.bind()`

```ts
import {Context, Binding} from '@loopback/core';
const context = new Context();
const binding = Binding.bind('my-key');
ctx.add(binding);
Expand All @@ -48,10 +50,24 @@ There are a few ways to create a binding:
- Use `context.bind()`

```ts
import {Context, Binding} from '@loopback/core';
const context = new Context();
context.bind('my-key');
```

{% include note.html content="The `@loopback/core` package re-exports all
public APIs of `@loopback/context`. For consistency, we recommend the usage of
`@loopback/core` for imports in LoopBack modules and applications unless they
depend on `@loopback/context` explicitly. The two statements below are
equivalent:

```ts
import {inject} from '@loopback/context';
import {inject} from '@loopback/core';
```

" %}

## How to set up a binding?

The `Binding` class provides a set of fluent APIs to create and configure a
Expand Down Expand Up @@ -87,7 +103,7 @@ The factory function can receive extra information about the context, binding,
and resolution options.

```ts
import {ValueFactory} from '@loopback/context';
import {ValueFactory} from '@loopback/core';

// The factory function now have access extra metadata about the resolution
const factory: ValueFactory<string> = resolutionCtx => {
Expand All @@ -113,7 +129,7 @@ An advanced form of value factory is a class that has a static `value` method
that allows parameter injection.

```ts
import {inject} from '@loopback/context';
import {inject} from '@loopback/core';

class GreetingProvider {
static value(@inject('user') user: string) {
Expand Down Expand Up @@ -458,7 +474,7 @@ match/find bindings by tag. The search criteria can be one of the followings:
filterByTag,
includesTagValue, // Match tag value as an array that includes the item
TagValueMatcher,
} from '@loopback/context';
} from '@loopback/core';
// Match a binding with a named service
ctx.find(filterByTag({name: ANY_TAG_VALUE, service: 'service'}));

Expand Down Expand Up @@ -504,7 +520,7 @@ When the class is bound, these attributes are honored to create a binding. You
can use `@bind` decorator to configure how to bind a class.
```ts
import {bind, BindingScope} from '@loopback/context';
import {bind, BindingScope} from '@loopback/core';

// @bind() accepts scope and tags
@bind({
Expand Down Expand Up @@ -538,7 +554,7 @@ export class YourController {}
Then a binding can be created by inspecting the class,
```ts
import {createBindingFromClass} from '@loopback/context';
import {createBindingFromClass} from '@loopback/core';

const ctx = new Context();
const binding = createBindingFromClass(MyService);
Expand Down
20 changes: 16 additions & 4 deletions docs/site/Context.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,26 @@ can be chained using the `parent` to form a hierarchy. For example, the code
below creates a chain of three contexts: `reqCtx -> serverCtx -> rootCtx`.

```ts
import {Context} from '@loopback/context';
import {Context} from '@loopback/core';

const rootCtx = new Context('root-ctx'); // No parent
const serverCtx = new Context(rootCtx, 'server-ctx'); // rootCtx as the parent
const reqCtx = new Context(serverCtx); // No explicit name, a UUID will be generated
```

{% include note.html content="The `@loopback/core` package re-exports all public
APIs of `@loopback/context`. For consistency, we recommend the usage of
`@loopback/core` for imports in LoopBack modules and applications unless they
depend on `@loopback/context` explicitly. The two statements below are
equivalent:

```ts
import {inject} from '@loopback/context';
import {inject} from '@loopback/core';
```

" %}

LoopBack's context system allows an unlimited amount of Context instances, each
of which may have a parent Context.

Expand Down Expand Up @@ -180,8 +193,7 @@ However, when using classes, LoopBack provides a better way to get at stuff in
the context via the `@inject` decorator:

```ts
import {inject} from '@loopback/context';
import {Application} from '@loopback/core';
import {inject, Application} from '@loopback/core';

const app = new Application();
app.bind('defaultName').to('John');
Expand Down Expand Up @@ -489,7 +501,7 @@ be used to watch a list of bindings matching certain criteria depicted by a
matched bindings.

```ts
import {Context, ContextView} from '@loopback/context';
import {Context, ContextView} from '@loopback/core';

// Set up a context chain
const appCtx = new Context('app');
Expand Down
2 changes: 1 addition & 1 deletion docs/site/Controller-generator.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ based on the given name:
```ts
// Uncomment these imports to begin using these cool features!

// import {inject} from '@loopback/context';
// import {inject} from '@loopback/core';

export class FooController {
constructor() {}
Expand Down
6 changes: 3 additions & 3 deletions docs/site/Creating-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function called by [Context](Context.md) when another entity requests a value to
be injected.

```ts
import {Provider} from '@loopback/context';
import {Provider} from '@loopback/core';

export class MyValueProvider implements Provider<string> {
value() {
Expand Down Expand Up @@ -168,7 +168,7 @@ the list of keys reserved for the framework use.
Provider's `value()` method can be asynchronous too:

```ts
import {Provider} from '@loopback/context';
import {Provider} from '@loopback/core';
const request = require('request-promise-native');
const weatherUrl =
'http://samples.openweathermap.org/data/2.5/weather?appid=b1b15e88fa797225412429c1c50c122a1';
Expand All @@ -192,7 +192,7 @@ dependencies annotated with `@inject` keyword, so that LoopBack runtime can
resolve them automatically.

```ts
import {Provider} from '@loopback/context';
import {Provider} from '@loopback/core';
import {Request, RestBindings} from '@loopback/rest';
import {v4 as uuid} from 'uuid';

Expand Down
19 changes: 17 additions & 2 deletions docs/site/Dependency-injection.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ the caller specify which strategy to use.
The implementation of the `authenticate` action is shown below.

```ts
import {inject, Provider} from '@loopback/core';

export class AuthenticateActionProvider implements Provider<AuthenticateFn> {
constructor(
// The provider is instantiated for Sequence constructor,
Expand Down Expand Up @@ -73,6 +75,19 @@ export class AuthenticateActionProvider implements Provider<AuthenticateFn> {
}
```

{% include note.html content="The `@loopback/core` package re-exports all public
APIs of `@loopback/context`. For consistency, we recommend the usage of
`@loopback/core` for imports in LoopBack modules and applications unless they
depend on `@loopback/context` explicitly. The two statements below are
equivalent:

```ts
import {inject} from '@loopback/context';
import {inject} from '@loopback/core';
```

" %}

Dependency Injection makes the code easier to extend and customize, because the
dependencies can be easily rewired by the application developer. It makes the
code easier to test in isolation (in a pure unit test), because the test can
Expand Down Expand Up @@ -296,7 +311,7 @@ problem.
Consider the following example:

```ts
import {Context, inject} from '@loopback/context';
import {Context, inject} from '@loopback/core';

interface Developer {
// Each developer belongs to a team
Expand Down Expand Up @@ -363,7 +378,7 @@ Let's take a look at the following example:
The corresponding code is:

```ts
import {inject, Context, BindingScope} from '@loopback/context';
import {inject, Context, BindingScope} from '@loopback/core';
import {RestBindings} from '@loopback/rest';

interface Logger() {
Expand Down
2 changes: 1 addition & 1 deletion docs/site/Express-middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ Alternatively, we can create a subclass of
`ExpressMiddlewareInterceptorProvider`.

```ts
import {config} from '@loopback/context';
import {config} from '@loopback/core';
import {
ExpressMiddlewareInterceptorProvider,
createMiddlewareInterceptorBinding,
Expand Down
2 changes: 1 addition & 1 deletion docs/site/Extending-LoopBack-4.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ that the service provider can be injected into the consumer class. The code
snippet below shows the usage of `@inject` for dependency injection.

```ts
import {inject, Context} from '@loopback/context';
import {inject, Context} from '@loopback/core';

/**
* A UserController implementation that depends on UserRepository and PasswordHasher
Expand Down
4 changes: 2 additions & 2 deletions docs/site/Extension-point-and-extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ decorators and functions are provided to ensure consistency and convention.
1. Inject a getter function for extensions

```ts
import {Getter} from '@loopback/context';
import {Getter} from '@loopback/core';
import {extensionPoint, extensions} from '@loopback/core';

@extensionPoint('greeters')
Expand All @@ -81,7 +81,7 @@ decorators and functions are provided to ensure consistency and convention.
2. Inject a context view for extensions

```ts
import {ContextView} from '@loopback/context';
import {ContextView} from '@loopback/core';
import {extensionPoint, extensions} from '@loopback/core';

@extensionPoint('greeters')
Expand Down
2 changes: 1 addition & 1 deletion docs/site/FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ headers). This can be accomplished by injecting the `Response` object into the
controller:

```ts
import {inject} from '@loopback/context';
import {inject} from '@loopback/core';
import {get, Response, RestBindings} from '@loopback/rest';

export class PingController {
Expand Down
4 changes: 2 additions & 2 deletions docs/site/File-upload-download.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ A few steps are involved to create an endpoint for file upload.
[`FileUploadController`](https://github.com/strongloop/loopback-next/blob/master/examples/file-transfer/src/controllers/file-upload.controller.ts)

```ts
import {inject} from '@loopback/context';
import {inject} from '@loopback/core';
import {
post,
Request,
Expand Down Expand Up @@ -110,7 +110,7 @@ To download files from the backend, please follow the following steps.
[`FileDownloadController`](https://github.com/strongloop/loopback-next/blob/master/examples/file-transfer/src/controllers/file-download.controller.ts)

```ts
import {inject} from '@loopback/context';
import {inject} from '@loopback/core';
import {
get,
HttpErrors,
Expand Down
4 changes: 2 additions & 2 deletions docs/site/Interceptor-generator.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ import {
bind,
Interceptor,
Provider,
} from '@loopback/context';
} from '@loopback/core';

/**
* This class will be bound to the application as a global `Interceptor` during
Expand Down Expand Up @@ -115,7 +115,7 @@ import {
bind,
Interceptor,
Provider,
} from '@loopback/context';
} from '@loopback/core';

/**
* This class will be bound to the application as a global `Interceptor` during
Expand Down
14 changes: 7 additions & 7 deletions docs/site/Interceptors.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Controller methods decorated with `@intercept` are invoked with applied
interceptors for corresponding routes upon API requests.

```ts
import {intercept} from '@loopback/context';
import {intercept} from '@loopback/core';

@intercept(log) // `log` is an interceptor function
export class OrderController {
Expand Down Expand Up @@ -74,7 +74,7 @@ services and would like to allow repository or service methods to be
intercepted.

```ts
import {createProxyWithInterceptors} from '@loopback/context';
import {createProxyWithInterceptors} from '@loopback/core';

const proxy = createProxyWithInterceptors(controllerInstance, ctx);
const msg = await proxy.greet('John');
Expand Down Expand Up @@ -148,7 +148,7 @@ To explicitly invoke a method with interceptors, use `invokeMethod` from
`RestServer` for controller methods.

```ts
import {Context, invokeMethod} from '@loopback/context';
import {Context, invokeMethod} from '@loopback/core';

const ctx: Context = new Context();

Expand Down Expand Up @@ -336,7 +336,7 @@ Global interceptors are discovered from the `InvocationContext`. They are
registered as bindings with `globalInterceptor` tag. For example,

```ts
import {asGlobalInterceptor} from '@loopback/context';
import {asGlobalInterceptor} from '@loopback/core';

app
.bind('globalInterceptors.MetricsInterceptor')
Expand Down Expand Up @@ -747,7 +747,7 @@ Sometimes we want to apply more than one interceptors together as a whole. It
can be done by `composeInterceptors`:

```ts
import {composeInterceptors} from '@loopback/context';
import {composeInterceptors} from '@loopback/core';

const interceptor = composeInterceptors(
interceptorFn1,
Expand All @@ -768,7 +768,7 @@ is the base class that can be extended to create your own flavor of interceptors
and chains. For example,

```ts
import {GenericInvocationChain, GenericInterceptor} from '@loopback/context';
import {GenericInvocationChain, GenericInterceptor} from '@loopback/core';
import {RequestContext} from '@loopback/rest';

export interface RequestInterceptor
Expand All @@ -795,7 +795,7 @@ await chain.invokeInterceptors();
It's also possible to pass in a final handler:

```ts
import {Next} from '@loopback/context';
import {Next} from '@loopback/core';
const finalHandler: Next = async () => {
// return ...;
};
Expand Down
2 changes: 1 addition & 1 deletion docs/site/LB3-vs-LB4-request-response-cycle.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ available to them via [dependency injection](./Dependency-injection.md).
Example of accesssing the request and response object in a Controller:

```ts
import {inject} from '@loopback/context';
import {inject} from '@loopback/core';
import {Request, Response, RestBindings, get} from '@loopback/rest';

export class ExampleController {
Expand Down
Loading

0 comments on commit 6b2a54c

Please sign in to comment.