Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions packages/authentication/src/decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@
import {Reflector, Constructor} from '@loopback/context';
import {BindingKeys} from './keys';

/* class to hold authentication metadata
/**
* Authentication metadata stored via Reflection API
*/
export interface AuthenticationMetadata {
strategy: string;
options?: Object;
}

/**
* decorator to add authentication metadata to controller methods
* @param strategyName
* @param options
* Mark a controller method as requiring authenticated user.
*
* @param strategyName The name of the authentication strategy to use.
* @param options Additional options to configure the authentication.
*/
export function authenticate(strategyName: string, options?: Object) {
return function(controllerClass: Object, methodName: string) {
Expand All @@ -34,9 +36,10 @@ export function authenticate(strategyName: string, options?: Object) {
}

/**
* function to get stored authentication metadata in a controller method
* @param controllerObj
* @param methodName
* Fetch authentication metadata stored by `@authenticate` decorator.
*
* @param controllerClass Target controller
* @param methodName Target method
*/
export function getAuthenticateMetadata(
controllerClass: Constructor<{}>,
Expand Down
3 changes: 3 additions & 0 deletions packages/authentication/src/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

/**
* Binding keys used by this component.
*/
export namespace BindingKeys {
export namespace Authentication {
export const STRATEGY = 'authentication.strategy';
Expand Down
15 changes: 14 additions & 1 deletion packages/context/src/binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,20 @@ export class Binding {
}

/**
* Bind the key to a BindingProvider
* Bind the key to a value computed by a Provider.
*
* * @example
*
* ```ts
* export class DateProvider implements Provider<Date> {
* constructor(@inject('stringDate') private param: String){}
* value(): Date {
* return new Date(param);
* }
* }
* ```
*
* @param provider The value provider to use.
*/
public toProvider<T>(providerClass: Constructor<Provider<T>>): this {
this._getValue = ctx => {
Expand Down
28 changes: 14 additions & 14 deletions packages/context/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,32 @@
import {ValueOrPromise} from './binding';

/**
* @exports Provider<T> : interface definition for a provider of a value
* of type T
* @summary Providers allow binding of a value provider class instead of the
* value itself
* @example:
* Providers allow developers to compute injected values dynamically,
* with any dependencies required by the value getter injected automatically
* from the Context.
*
* @example
*
* ```ts
* export class DateProvider implements Provider<Date> {
* constructor(@inject('stringDate') private param: String){}
* value(): Date {
* return new Date(param);
* }
* }
* ```
* @example: Binding a context
* ```ts
*
* ctx.bind('stringDate').to('2017-01-01')
* ctx.bind('provider_key').toProvider(MyProvider);
* ```
* @example: getting a value dynamically
* ```ts
* ctx.get('provider_key');
* ctx.getBinding('provider_key').getValue();
*
* const value = ctx.getAsync('provider_key');
* // value is a Date instance
* ```
*/
export interface Provider<T> {
/**
* @returns a value or a promise
* @returns The value to inject to dependents.
* This method can return a promise too, in which case the IoC framework
* will resolve this promise to obtain the value to inject.
*/
value(): ValueOrPromise<T>;
}