How to use the @loopback/context.MetadataInspector.getClassMetadata function in @loopback/context

To help you get started, we’ve selected a few @loopback/context examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github strongloop / loopback-next / packages / repository / src / __tests__ / unit / decorator / metadata.unit.ts View on Github external
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,
      );
github strongloop / loopback-next / packages / authentication / src / decorators / authenticate.decorator.ts View on Github external
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;
}
github strongloop / loopback-next / packages / authorization / src / decorators / authorize.ts View on Github external
): 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,
  );
}
github strongloop / loopback-next / packages / repository-json-schema / src / build-schema.ts View on Github external
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,
github strongloop / loopback-next / packages / repository / src / __tests__ / unit / decorator / model-and-relation.decorator.unit.ts View on Github external
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,
        },
      },
    });
  });
github strongloop / loopback-next / packages / repository / src / __tests__ / unit / decorator / model-and-relation.decorator.unit.ts View on Github external
it('adds model metadata', () => {
    const meta = MetadataInspector.getClassMetadata(MODEL_KEY, Order);
    expect(meta).to.eql({name: 'order'});
  });
github strongloop / loopback-next / packages / openapi-v2 / src / controller-spec.ts View on Github external
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,
    ) || {};
github strongloop / loopback-next / packages / boot / src / booters / service.booter.ts View on Github external
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;
}
github strongloop / loopback-next / packages / openapi-v2 / src / controller-spec.ts View on Github external
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;
}