Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('returns cached metadata instead of recreating it', () => {
const classMeta = MetadataInspector.getClassMetadata(
MODEL_KEY,
Phlange,
) as ModelDefinition;
classMeta.properties = {
foo: {
type: String,
},
};
// Intentionally change the metadata to be different from the Phlange
// class metadata
MetadataInspector.defineMetadata(
MODEL_WITH_PROPERTIES_KEY.key,
classMeta,
Phlange,
);
it('returns cached metadata instead of recreating it', () => {
const classMeta = MetadataInspector.getClassMetadata(
MODEL_KEY,
Phlange,
) as ModelDefinition;
classMeta.properties = {
foo: {
type: String,
},
};
// Intentionally change the metadata to be different from the Phlange
// class metadata
MetadataInspector.defineMetadata(
MODEL_WITH_PROPERTIES_KEY.key,
classMeta,
Phlange,
);
const meta = ModelMetadataHelper.getModelMetadata(
Phlange,
) as ModelDefinition;
expect(meta.properties).to.eql(classMeta.properties);
});
});
it('adds belongsTo metadata', () => {
const meta =
MetadataInspector.getAllPropertyMetadata(
RELATIONS_KEY,
Order.prototype,
) ?? /* istanbul ignore next */ {};
const relationDef = meta.customerId;
expect(relationDef).to.containEql({
type: RelationType.belongsTo,
name: 'customer',
target: () => Customer,
keyFrom: 'customerId',
});
expect(relationDef.source).to.be.exactly(Order);
expect(relationDef.target()).to.be.exactly(Customer);
});
it('"@property.array" adds array metadata', () => {
@model()
class TestModel {
@property.array(Product)
items: Product[];
}
const meta =
MetadataInspector.getAllPropertyMetadata(
MODEL_PROPERTIES_KEY,
TestModel.prototype,
) ?? /* istanbul ignore next */ {};
expect(meta.items).to.eql({type: Array, itemType: Product});
});
// License text available at https://opensource.org/licenses/MIT
import {
bind,
Interceptor,
InvocationContext,
InvocationResult,
Provider,
ValueOrPromise,
} from '@loopback/context';
/**
* This class will be bound to the application as a global `Interceptor` during
* `boot`
*/
@bind({tags: {namespace: 'interceptors', name: 'myInterceptor'}})
export class MyInterceptor implements Provider {
/*
constructor() {}
*/
/**
* This method is used by LoopBack context to produce an interceptor function
* for the binding.
*
* @returns An interceptor function
*/
value() {
return this.intercept.bind(this);
}
/**
export async function main() {
const ctx = new Context('request');
// Add EnglishGreeter for now
ctx.add(createBindingFromClass(EnglishGreeter, {namespace: 'greeters'}));
// Add ChineseGreeter
ctx
.bind('greeters.ChineseGreeter')
.toClass(ChineseGreeter)
.tag('greeter');
const enlishGreeterBinding = ctx.getBinding('greeters.EnglishGreeter');
console.log(enlishGreeterBinding.key);
let possibleEnglishGreeters = ctx.find('*.EnglishGreeter');
console.log(possibleEnglishGreeters.map(b => b.key));
export async function main() {
const ctx = new Context('request');
// Add EnglishGreeter for now
ctx.add(createBindingFromClass(EnglishGreeter, {namespace: 'greeters'}));
// Add ChineseGreeter
ctx
.bind('greeters.ChineseGreeter')
.toClass(ChineseGreeter)
.tag('greeter');
const enlishGreeterBinding = ctx.getBinding('greeters.EnglishGreeter');
console.log(enlishGreeterBinding.key);
let possibleEnglishGreeters = ctx.find('*.EnglishGreeter');
console.log(possibleEnglishGreeters.map(b => b.key));
possibleEnglishGreeters = ctx.find(/\w+\.EnglishGreeter$/);
console.log(possibleEnglishGreeters.map(b => b.key));
// Now add ChineseGreeter
ctx
.bind('greeters.ChineseGreeter')
.toClass(ChineseGreeter)
.tag('greeter');
greeting = await invokeMethod(greetingService, 'greet', ctx);
console.log(greeting);
// Change the current language to `en`
ctx.bind('currentLanguage').to('en');
// Change the current user to `Jane`
ctx.bind('currentUser').to('Jane');
greeting = await invokeMethod(greetingService, 'greet', ctx);
console.log(greeting);
}
export async function main() {
const appCtx = new Context('app');
const requestCtx = new RequestContext(appCtx, 'request');
// Observer events from `appCtx`
appCtx.subscribe({
filter: filterByTag('greeter'),
observe: (eventType, binding) => {
console.log('[observer] %s %s', eventType, binding.key);
},
});
// Create a context view on `requestCtx`
const greetersView = requestCtx.createView(filterByKey(/^greeters\./));
greetersView.on('refresh', () => {
console.log(
'[view.refresh] %j',
greetersView.bindings.map(b => b.key),
);
});
// Add EnglishGreeter to `appCtx`
console.log('Adding EnglishGreeter');
constructor(
@inject(CoreBindings.APPLICATION_INSTANCE)
public app: Application,
@inject(BootBindings.PROJECT_ROOT) projectRoot: string,
@config()
public interceptorConfig: ArtifactOptions = {},
) {
super(
projectRoot,
// Set InterceptorProvider Booter Options if passed in via bootConfig
Object.assign({}, InterceptorProviderDefaults, interceptorConfig),
);
}