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,
);
export function getAuthenticateMetadata(
targetClass: Constructor<{}>,
methodName: string,
): AuthenticationMetadata | undefined {
// First check method level
let metadata = MetadataInspector.getMethodMetadata(
AUTHENTICATION_METADATA_METHOD_KEY,
targetClass.prototype,
methodName,
);
if (metadata) return metadata;
// Check if the class level has `@authenticate`
metadata = MetadataInspector.getClassMetadata(
AUTHENTICATION_METADATA_CLASS_KEY,
targetClass,
);
return metadata;
}
): AuthorizationMetadata | undefined {
let targetClass: Function;
if (typeof target === 'function') {
targetClass = target;
target = target.prototype;
} else {
targetClass = target.constructor;
}
const metadata = MetadataInspector.getMethodMetadata(
AUTHORIZATION_METHOD_KEY,
target,
methodName,
);
if (metadata) return metadata;
// Check if the class level has `@authorize`
return MetadataInspector.getClassMetadata(
AUTHORIZATION_CLASS_KEY,
targetClass,
);
}
export function getJsonSchema(
ctor: Function & {prototype: T},
options?: JsonSchemaOptions,
): JSONSchema {
// In the near future the metadata will be an object with
// different titles as keys
const cached = MetadataInspector.getClassMetadata(JSON_SCHEMA_KEY, ctor);
const key = buildModelCacheKey(options);
let schema = cached?.[key];
if (!schema) {
// Create new json schema from model
// if not found in cache for specific key
schema = modelToJsonSchema(ctor, options);
if (cached) {
// Add a new key to the cached schema of the model
cached[key] = schema;
} else {
// Define new metadata and set in cache
MetadataInspector.defineMetadata(
JSON_SCHEMA_KEY.key,
{[key]: schema},
ctor,
it('adds model metadata without name', () => {
const meta = MetadataInspector.getClassMetadata(MODEL_KEY, Receipt);
expect(meta).to.eql({
name: 'Receipt',
properties: {
id: {
type: 'number',
required: true,
},
},
});
});
it('adds model metadata', () => {
const meta = MetadataInspector.getClassMetadata(MODEL_KEY, Order);
expect(meta).to.eql({name: 'order'});
});
function resolveControllerSpec(constructor: Function): ControllerSpec {
debug(`Retrieving OpenAPI specification for controller ${constructor.name}`);
let spec = MetadataInspector.getClassMetadata(
OAI2Keys.CLASS_KEY,
constructor,
);
if (spec) {
debug(' using class-level spec defined via @api()', spec);
spec = DecoratorFactory.cloneDeep(spec);
} else {
spec = {paths: {}};
}
let endpoints =
MetadataInspector.getAllMethodMetadata(
OAI2Keys.METHODS_KEY,
constructor.prototype,
) || {};
function isBindableClass(cls: Constructor) {
if (MetadataInspector.getClassMetadata(BINDING_METADATA_KEY, cls)) {
return true;
}
if (isServiceProvider(cls)) {
debug('Provider class found: %s', cls.name);
return true;
}
debug('Skip class not decorated with @bind: %s', cls.name);
return false;
}
export function getControllerSpec(constructor: Function): ControllerSpec {
let spec = MetadataInspector.getClassMetadata(
OAI2Keys.CONTROLLER_SPEC_KEY,
constructor,
{ownMetadataOnly: true},
);
if (!spec) {
spec = resolveControllerSpec(constructor);
MetadataInspector.defineMetadata(
OAI2Keys.CONTROLLER_SPEC_KEY,
spec,
constructor,
);
}
return spec;
}