Code Monkey home page Code Monkey logo

ec-print's Introduction

ec-print

All in one print toolkit for e-commerce ,integrated cainiao,pinduouo,jingdong,kuaishou,douyin ...

Commitizen friendly Continuous Deployment FOSSA Status GitHub license


Platform supports & Docs

dependences

Quickstart

install

yarn add ec-print

use in your project

import Printer,{Providers} from 'ec-print';

const cainiao = new Providers.Cainiao(undefined, {
  WebSocket,
  // maxRetries: 2,
  debug: true,
});
const doudian = new Providers.Doudian(undefined, {
  WebSocket,
  debug: true,
});
const printer = new Printer();
printer.register([
  { key: cainiao.providerKey, provider: cainiao },
  { key: doudian.providerKey, provider: doudian },
]);
async run(){
  const connected = await printer.connect();
  console.log('connected', connected);
  const version = await printer.getAgentInfo('cainiao');
  console.log('version', version);
  const res1 = await printer.print<Response>(cainiao_print_task);
  console.log('cainiao print response', res1);
  const res2 = await printer.print(douyin_print_task, 'doudian');
  console.log('douyin print response', res2);
}

features

Auto reconnect

see reconnecting-websocket

options

type SocketOptions = {
    WebSocket?: any; // WebSocket constructor, if none provided, defaults to global WebSocket
    maxReconnectionDelay?: number; // max delay in ms between reconnections
    minReconnectionDelay?: number; // min delay in ms between reconnections
    reconnectionDelayGrowFactor?: number; // how fast the reconnection delay grows
    minUptime?: number; // min time in ms to consider connection as stable
    connectionTimeout?: number; // retry connect if not connected after this time, in ms
    maxRetries?: number; // maximum number of retries
    maxEnqueuedMessages?: number; // maximum number of messages to buffer until reconnection
    startClosed?: boolean; // start websocket in CLOSED state, call `.reconnect()` to connect
    debug?: boolean; // enables debug output
    autoReconnect?: boolean; //if true attempt to reconnect when send message to ws
};

custom printer provider

import { PrinterProvider, PrinterProps} from 'ec-printer';
class CustomPrinter extends PrinterProvider {
  readonly providerKey: string = 'yourKey';
  private version: string;
  constructor(props: PrinterProps = { url: 'ws://localhost:1111' }) {
    const url = props.url;
    super({ url, options: props.options });
    this.version = props.version || '1.0';
  }
  // implement abstract methods handleResponseMessage and handleRequestMessage
  handleResponseMessage<T extends Response>(event: MessageEvent<any>): T {
    const res = JSON.parse(event.data);
    let success = true;
    if (res.status) {
      success = res.status === 'success';
    }
    return { ...res, success } as unknown as T;
  }
  handleRequestMessage<T extends JsonObject>(jsonData: Request): T {
    return jsonData as unknown as T;
  }
  protected getRequestHeader(cmd: CMD): { cmd: CMD; version: string } {
    return {
      cmd,
      version: this.version || '1.0',
    };
  }
}

then

const printer = new Printer();
printer.register([
  { key: cainiao.providerKey, provider: cainiao },
  { key: 'providerKey', provider: new CustomPrinter() },
]);

printer.connect().then(res=>{
  printer.getPrinter('providerKey')
})

Demos

nodejs-example vue3-example

License

FOSSA Status

Analytics

Alt

ec-print's People

Contributors

gavin-hao avatar semantic-release-bot avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.