npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

egg-dubbo-ts

v1.0.18

Published

Dubbo plugin for egg

Downloads

15

Readme

egg-dubbo-ts

基于eggjs和node-dubbo-ts实现的一套egg dubbo插件。可以利用装饰器快速构建RPC接口,供消费端调用。

安装步骤:

npm install egg-dubbo-ts
yarn add egg-dubbo-ts

配置方式:

修改eggjs项目中config/plugin.ts文件,开启egg-dubbo-ts插件。

// 示例代码
import { EggPlugin } from 'egg';

const plugin: EggPlugin = {
  dubbo: {
    enable: true,
    package: 'egg-dubbo-ts',
  },
};
export default plugin;

配置config/config.default.ts文件

// 示例代码
config.dubbo = {
  middlewares: [responseHandler],
  serviceDirs: ['app/dubbo/service/**/*.ts', 'app/dubbo/service/**/*.js', '!app/dubbo/service/**/*.d.ts'],
  swagger: {
    enable: false,
  },
  zookeeper: {
    host: '127.0.0.1:2181',
    sessionTimeout: 30000,
    retries: 3,
  },
  provider: {
    application: 'SERVICE_1',
    dubbo_version: '1.0.0',
    port: 8998,
    heartbeat: 60000,
  },
  consumer: {
    application: 'SERVICE_2',
    dubbo_version: '1.0.0',
  },
};
// 中间件示例代码
import { SystemUnknownError } from '../exception';

export async function responseHandler(ctx, next) {
  try {
    await next();
    const { body } = ctx.rpcContext;
    ctx.rpcContext.body = { code: 200, data: body };
  } catch (err) {
    ctx.logger.error(err);
    const error = err.code ? err : new SystemUnknownError({ error: err.message });
    ctx.rpcContext.body = {
      message: error.message,
      code: error.code,
      error: error.error,
    };
  }
}

定义rpc接口

// 示例代码
import { Service, Inject } from 'typedi';
import OAuthService from '@service/auth/oauth';
import { RPCService, Response, Provider, Version, Description, Method, Summary, Param } from 'egg-dubbo-ts';
import { OAuthTokenParams, OAuthTokenResp } from '../dto/oAuth';

@Service()
@Provider('com.jeni.node.service.OAuthService')
@Version('1.0.0')
@Description('第三方授权认证服务')
export default class OAuthRPCService extends RPCService {
  @Inject()
  readonly oAuthService: OAuthService;

  @Method
  @Summary('获取第三方授权Token')
  @Response(OAuthTokenResp)
  async getAccessToken(@Param params: OAuthTokenParams) {
    const { sign, appId, appSecret, school, timestamp } = params;
    const thirdApp = await this.oAuthService.validateOAuth({
      sign,
      appId,
      appSecret,
      school,
      timestamp,
    });
    const accessToken = this.app.jwt.sign(
      {
        appId,
        school,
      },
      {
        expiresIn: thirdApp.tokenExpireTime || 2 * 60 * 60, // 默认有效期2小时
      },
    );
    return {
      token: this.ctx.helper.crypto.encrypt(accessToken),
    };
  }
}
import { IsDefined } from 'class-validator';
import { Type } from 'class-transformer';

export class OAuthTokenParams {
  @IsDefined({ message: '学校不能为空' })
  @Type(() => Number)
  school: number;

  @IsDefined({ message: 'appId不能为空' })
  @Type(() => String)
  appId: string;

  @IsDefined({ message: 'appSecret不能为空' })
  @Type(() => String)
  appSecret: string;

  @IsDefined({ message: '签名不能为空' })
  @Type(() => String)
  sign: string;

  @IsDefined({ message: '时间戳不能为空' })
  @Type(() => Number)
  timestamp: number;
}

export class OAuthTokenResp {
  @IsDefined({ message: 'Token不能为空' })
  @Type(() => String)
  token: string;
}

调用rpc接口

import * as java from 'js-to-java';
async getOAuthAccessToken(data: {
  school: number;
  appId: string;
  appSecret: string;
  sign: string;
  timestamp: number;
}) {
  const invoker = await this.app.dubbo.consumer.get('com.jeni.node.service.OAuthService', '1.0.0');
  return await invoker.invoke('getAccessToken', [java('com.java.Object', { ...data })]);
}