# pj-ws-api-client

> 品杰WS API 客户端

Latest version **1.0.6** (published 2024-10-30) · ISC license · 0 weekly downloads

## Install

```sh
npm install pj-ws-api-client
pnpm add pj-ws-api-client
yarn add pj-ws-api-client
bun add pj-ws-api-client
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.6 |
| Published | 2024-10-30 |
| First published | 2023-10-13 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 47.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | 四川品杰科技有限公司 |
| Maintainers | cxwl3sxl |

## Links

- npm: https://www.npmjs.com/package/pj-ws-api-client
- npm.io page: https://npm.io/package/pj-ws-api-client

## Recent versions

- 1.0.6 (latest) — 2024-10-30
- 1.0.5 — 2023-10-13
- 1.0.4 — 2023-10-13
- 1.0.3 — 2023-10-13
- 1.0.2 — 2023-10-13
- 1.0.1 — 2023-10-13
- 1.0.0 — 2023-10-13

## README

### 定义API(可通过工具生成)
```
// @ts-nocheck
import {BaseServerApi} from "pj-ws-api-client";

export class Account extends BaseServerApi {
    constructor() {
        super("Account");
        this
            ._RegMethod("login", ["userName", "password", "verifyCode"])
            ._RegMethod("logout", ["token"])
            ._RegMethod("verifyToken", ["token"])
    }

    login(name: string, pwd: string, verifyCode: string): Promise<any> {
        return Promise.reject(new Error("Method not implemented."));
    }

    logout(token: string): Promise<boolean> {
        return Promise.reject(new Error("Method not implemented."));
    }

    verifyToken(token: string): Promise<any> {
        return Promise.reject(new Error("Method not implemented."));
    }
}
```

### 使用
```
<script setup lang="ts">
import {onMounted, ref} from "vue";
import {ApiClient} from "pj-ws-api-client";
import {Account} from "./Api/Account.ts";
import {Echo} from "./Api/Echo.ts";

const client = new ApiClient("ws://192.168.0.84:8604");
const myInfo = ref<string>("");

function hello(name: string): string {
  console.log("收到服务器调用");
  return `hello ${name}, i'm js`;
}

function helloAsync(name: string): Promise<string> {
  return new Promise<string>((acc, rej) => {
    console.log("收到服务器调用Async");
    acc(`hello ${name}, i'm js via promise`);
  })
}

onMounted(async () => {
  client.start();
  try {
    client.register(hello, this);
    client.register(helloAsync, this);

    client.addEventHandler("onpush", (sender: any, args: any[]) => {
      console.log("收到服务器消息", args);
    });
    client.addEventHandler("connected", (sender: any) => {
      console.log("已经登录成功");
    })
    await client.waitConnected();

    let a = await client.getProxy<Account>(Account).login("admin", "e10adc3949ba59abbe56e057f20f883e", "xxx");
    console.log(`login result`, a);
    client.addHeader("A-Token", a.AccessToken);

    myInfo.value = await client.getProxy<Echo>(Echo).myInfo();

    let c = await client.getProxy<Account>(Account).verifyToken(a.AccessToken);
    console.log(`verify token result`, c);

    let b = await client.getProxy<Account>(Account).logout(a.AccessToken);
    console.log(`logout result`, b);
  } catch (e) {
    console.warn(e);
  }
})
</script>
```

---
_Source: https://npm.io/package/pj-ws-api-client · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
