Skip to content

Commit

Permalink
feat(demo): add demos
Browse files Browse the repository at this point in the history
  • Loading branch information
haozhigang committed Oct 6, 2022
1 parent 8c8b1d8 commit 8a5b1b6
Show file tree
Hide file tree
Showing 32 changed files with 1,852 additions and 81 deletions.
103 changes: 102 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,109 @@ All in one print toolkit for e-commerce ,integrated cainiao,pinduouo,jingdong,
```bash
yarn add ec-print
```
#### use in your project

## License
```typescript
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](https://github.com/pladaria/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

``` typescript
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

```typescript
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](./example/src/index.ts)
[vue3-example](./example-vue/src/main.ts)
# License

[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fgavin-hao%2Fec-print.svg?type=large)](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fgavin-hao%2Fec-print.svg?type=badge_large)
# Analytics
Expand Down
26 changes: 26 additions & 0 deletions example-vue/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
auto-imports.d.ts
components.d.ts
3 changes: 3 additions & 0 deletions example-vue/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"recommendations": ["Vue.volar"]
}
16 changes: 16 additions & 0 deletions example-vue/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Vue 3 + TypeScript + Vite

This template should help get you started developing with Vue 3 and TypeScript in Vite. The template uses Vue 3 `<script setup>` SFCs, check out the [script setup docs](https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup) to learn more.

## Recommended IDE Setup

- [VS Code](https://code.visualstudio.com/) + [Volar](https://marketplace.visualstudio.com/items?itemName=Vue.volar)

## Type Support For `.vue` Imports in TS

Since TypeScript cannot handle type information for `.vue` imports, they are shimmed to be a generic Vue component type by default. In most cases this is fine if you don't really care about component prop types outside of templates. However, if you wish to get actual prop types in `.vue` imports (for example to get props validation when using manual `h(...)` calls), you can enable Volar's Take Over mode by following these steps:

1. Run `Extensions: Show Built-in Extensions` from VS Code's command palette, look for `TypeScript and JavaScript Language Features`, then right click and select `Disable (Workspace)`. By default, Take Over mode will enable itself if the default TypeScript extension is disabled.
2. Reload the VS Code window by running `Developer: Reload Window` from the command palette.

You can learn more about Take Over mode [here](https://github.com/johnsoncodehk/volar/discussions/471).
13 changes: 13 additions & 0 deletions example-vue/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + Vue + TS</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
24 changes: 24 additions & 0 deletions example-vue/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "example-vue",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vue-tsc --noEmit && vite build",
"preview": "vite preview"
},
"dependencies": {
"element-plus": "^2.2.17",
"pinia": "^2.0.22",
"vue": "^3.2.37"
},
"devDependencies": {
"@vitejs/plugin-vue": "^3.1.0",
"typescript": "^4.6.4",
"unplugin-auto-import": "^0.11.2",
"unplugin-vue-components": "^0.22.8",
"vite": "^3.1.0",
"vue-tsc": "^0.40.4"
}
}
1 change: 1 addition & 0 deletions example-vue/public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 8a5b1b6

Please sign in to comment.