Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('honors expressSettings', () => {
const app = new Application();
const server = new TestRestServer(app, {
expressSettings: {
'x-powered-by': false,
env: 'production',
},
});
const expressApp = server.expressApp;
expect(expressApp.get('x-powered-by')).to.equal(false);
expect(expressApp.get('env')).to.equal('production');
// `extended` is the default setting by Express
expect(expressApp.get('query parser')).to.equal('extended');
expect(expressApp.get('not set')).to.equal(undefined);
});
it('honors a newly added/removed greeter binding', async () => {
/**
* A greeter implementation for French
*/
@bind(asGreeter)
class FrenchGreeter implements Greeter {
language = 'fr';
greet(name: string) {
return `Bonjour, ${name}!`;
}
}
// Add a new greeter for French
const binding = createBindingFromClass(FrenchGreeter);
app.add(binding);
let msg = await greetingService.greet('fr', 'Raymond');
expect(msg).to.eql('Bonjour, Raymond!');
// Remove the French greeter
app.unbind(binding.key);
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: @loopback/boot
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {bind, BindingScope, Provider} from '@loopback/core';
@bind({
tags: {serviceType: 'local'},
scope: BindingScope.SINGLETON,
})
export class BindableGreetingService {
greet(whom = 'world') {
return Promise.resolve(`Hello ${whom}`);
}
}
@bind({tags: {serviceType: 'local', name: 'CurrentDate'}})
export class DateProvider implements Provider {
value(): Promise {
return Promise.resolve(new Date());
}
}
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {bind, BindingScope, Provider} from '@loopback/core';
@bind({
tags: {serviceType: 'local'},
scope: BindingScope.SINGLETON,
})
export class BindableGreetingService {
greet(whom = 'world') {
return Promise.resolve(`Hello ${whom}`);
}
}
@bind({tags: {serviceType: 'local', name: 'CurrentDate'}})
export class DateProvider implements Provider {
value(): Promise {
return Promise.resolve(new Date());
}
}
export class NotBindableGreetingService {
greet(whom = 'world') {
return Promise.resolve(`Hello ${whom}`);
}
}
export class NotBindableDateProvider implements Provider {
value(): Promise {
return Promise.resolve(new Date());
}
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: @loopback/boot
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {bind, BindingScope, Provider} from '@loopback/core';
@bind({
tags: {serviceType: 'local'},
scope: BindingScope.SINGLETON,
})
export class BindableGreetingService {
greet(whom = 'world') {
return Promise.resolve(`Hello ${whom}`);
}
}
@bind({tags: {serviceType: 'local', name: 'CurrentDate'}})
export class DateProvider implements Provider {
value(): Promise {
return Promise.resolve(new Date());
}
}
export class NotBindableGreetingService {
greet(whom = 'world') {
import debugFactory from 'debug';
import {Message} from './types';
const debug = debugFactory('greeter-extension');
/**
* Configuration for CachingService
*/
export interface CachingServiceOptions {
// The time-to-live setting for a cache entry
ttl: number;
}
/**
* Message caching service
*/
@bind({scope: BindingScope.SINGLETON})
export class CachingService {
private timer: NodeJS.Timer;
private store: Map = new Map();
constructor(
@config.view()
private optionsView: ContextView,
) {
// Use a view so that we can listen on `refresh` events, which are emitted
// when the configuration binding is updated in the context.
optionsView.on('refresh', () => {
debug('Restarting the service as configuration changes...');
this.restart().catch(err => {
console.error('Cannot restart the caching service.', err);
process.exit(1);
});
// Node module: @loopback/v3compat
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {inject} from '@loopback/core';
import {OperationArgs, Request, Response, RestBindings} from '@loopback/rest';
import * as debugFactory from 'debug';
import {SharedMethod} from './shared-method';
const debug = debugFactory('loopback:v3compat:rest-adapter');
export class Lb3ModelController {
@inject(RestBindings.Http.REQUEST)
protected request: Request;
@inject(RestBindings.Http.RESPONSE)
protected response: Response;
// TODO: a property for strong-remoting's HttpContext
[key: string]: Function | Request | Response;
protected buildMethodArguments(
sharedMethod: SharedMethod,
inputArgs: OperationArgs,
) {
const finalArgs: OperationArgs = [];
for (const argSpec of sharedMethod.accepts) {
const source = argSpec.http && argSpec.http.source;
switch (source) {
case 'req':
finalArgs.push(this.request);
constructor(
@inject(CoreBindings.APPLICATION_INSTANCE)
public app: ApplicationWithServices,
@inject(BootBindings.PROJECT_ROOT) projectRoot: string,
@config()
public serviceConfig: ArtifactOptions = {},
) {
super(
projectRoot,
// Set Service Booter Options if passed in via bootConfig
Object.assign({}, ServiceDefaults, serviceConfig),
);
}
constructor(
@inject(CoreBindings.APPLICATION_INSTANCE)
private app: Application & Bootable,
@inject(BootBindings.PROJECT_ROOT) private projectRoot: string,
@inject(BootBindings.BOOT_OPTIONS, {optional: true})
private bootOptions: BootOptions = {},
) {
// Resolve path to projectRoot and re-bind
this.projectRoot = resolve(this.projectRoot);
app.bind(BootBindings.PROJECT_ROOT).to(this.projectRoot);
// This is re-bound for testing reasons where this value may be passed directly
// and needs to be propagated to the Booters via DI
app.bind(BootBindings.BOOT_OPTIONS).to(this.bootOptions);
}
constructor(
@inject(CoreBindings.APPLICATION_INSTANCE)
public app: Application,
@inject(BootBindings.PROJECT_ROOT) projectRoot: string,
@config()
public observerConfig: ArtifactOptions = {},
) {
super(
projectRoot,
// Set LifeCycleObserver Booter Options if passed in via bootConfig
Object.assign({}, LifeCycleObserverDefaults, observerConfig),
);
}