ec-print
v1.0.6
Published
print toolkit for e-commerce ,integrated cainiao,pinduouo,jingdong,kuaishou,douyin ...
Downloads
43
Maintainers
Readme
ec-print
All in one print toolkit for e-commerce ,integrated cainiao,pinduouo,jingdong,kuaishou,douyin ...
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
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')
})