0.2.22 • Published 4 years ago

@ltv/moleculer-core v0.2.22

Weekly downloads
4
License
UNLICENSED
Repository
-
Last release
4 years ago

@ltv/moleculer-services-core

Dependencies

nameversiondescriptionrequires install
moleculer0.14.8moleculerYES

Modules

errors

  • BaseError
  • AppError

Usage

import { BaseError, AppError } from '@ltv/moleculer-core';

return AppError.invalidRequest().reject();

Built-in Errors

  • invalidRequest
  • invalidRequestError
  • invalidResponseType
  • forbidden
  • badRequest
  • notFound
  • rateLimitExceeded

Extends Error

import { BaseError, IError } from '@ltv/moleculer-core';

const DbErrorMap: { [key: string]: IError } = {
  NOT_FOUND_ENTITY: {
    type: 'NOT_FOUND_ENTITY',
    message: 'Could not found entity with provided id',
  },
};

export class DatabaseError extends BaseError {
  constructor(message: string, code: number, type: string) {
    super(message, code, type);
    this.name = 'DatabaseError';
  }

  public static notFoundEntity(message?: IError): DatabaseError {
    return this.createError(DbErrorMap.NOT_FOUND_ENTITY, message);
  }
}

middlewares

check-permissions

Used for checking permissions of each action Ex: permissions: ['post.create']

  • Apply middlewares in moleculer.config.(ts|js)
import { CheckPermissionsMiddleware } from '@ltv/moleculer-core';

const brokerConfig: BrokerOptions = {
  // ...
  // Register custom middlewares
  const options = { acl, roles }; // options is optional
  middlewares: [CheckPermissionsMiddleware(options)],
  // ...
};

export = brokerConfig;
  • Use in actions
module.exports = {
  name: 'post',
  actions: {
    create: {
      permissions: ['post.create']
      handler(ctx) {}
    }
  }
}

find-entity

Used for pre-fetch entity by id

  • Apply middlewares in moleculer.config.(ts|js)
import { FindEntityMiddleware } from '@ltv/moleculer-core';

const brokerConfig: BrokerOptions = {
  // ...
  // Register custom middlewares
  middlewares: [FindEntityMiddleware()],
  // ...
};

export = brokerConfig;
  • Use in actions
module.exports = {
  name: 'post',
  actions: {
    update: {
      needEntity: true,
      params: {
        id: 'string',
        toBeUpdate: 'object'
      }
      handler(ctx) {
        const { entity } = ctx.locals;
        const { id, toBeUpdate } = ctx.params;
        entity = { ...toBeUpdate };
        return this.adapter.updateById(id, entity);
      }
    }
  }
}

heath-check

Used for checking heath of service

  • Apply middlewares in moleculer.config.(ts|js)
import { CreateHealthCheckMiddleware } from '@ltv/moleculer-core';

const {
  HEALTH_CHECK_READINESS_PATH,
  HEALTH_CHECK_LIVENESS_PATH,
  HEALTH_CHECK_PORT,
} = process.env;
const healthCheckOpts = {
  port: +HEALTH_CHECK_PORT || 3001,
  readiness: {
    path: HEALTH_CHECK_READINESS_PATH || '/ready',
  },
  liveness: {
    path: HEALTH_CHECK_LIVENESS_PATH || '/live',
  },
};

const brokerConfig: BrokerOptions = {
  // ...
  // Register custom middlewares
  middlewares: [CreateHealthCheckMiddleware(healthCheckOpts)],
  // ...
};

export = brokerConfig;

mixins

// TODO: Update later

utils

// TODO: Update later

0.2.22

4 years ago

0.2.21

4 years ago

0.2.20

4 years ago

0.2.19

4 years ago

0.2.18

4 years ago

0.2.17

4 years ago

0.2.16

4 years ago

0.2.15

4 years ago

0.2.14

4 years ago

0.2.13

4 years ago

0.2.12

4 years ago

0.2.11

4 years ago

0.2.10

4 years ago

0.2.9

4 years ago

0.2.8

4 years ago

0.2.7

4 years ago

0.2.6

4 years ago

0.2.5

4 years ago

0.2.4

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago