Compare commits
5 Commits
v0.0.5
...
v0.0.7-alp
| Author | SHA1 | Date | |
|---|---|---|---|
| 77e6394266 | |||
| 4e38c3a74e | |||
| 3f8971350e | |||
| 81afd2965e | |||
| fe5b3ec15b |
35
.github/workflow/publish.yml
vendored
35
.github/workflow/publish.yml
vendored
@@ -1,35 +0,0 @@
|
|||||||
name: Publish to npm
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
tags:
|
|
||||||
- 'v*.*.*' # 当推送带有版本号的 tag 时触发,例如 v1.0.0
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
publish:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
# Step 1: Clone current Git repository
|
|
||||||
- name: Checkout this repository
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
# Setop 2: list files in current directory
|
|
||||||
- name: List files in current directory
|
|
||||||
run: ls -al
|
|
||||||
# Step 3: Setup Node.js and install dependencies
|
|
||||||
- name: Setup Node.js
|
|
||||||
uses: actions/setup-node@v2
|
|
||||||
with:
|
|
||||||
node-version: '20.6'
|
|
||||||
registry-url: 'https://registry.npmjs.org/'
|
|
||||||
- name: npm whoami
|
|
||||||
run: |
|
|
||||||
npm whoami
|
|
||||||
echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" > .npmrc
|
|
||||||
# Step 4: Install dependencies
|
|
||||||
- name: Install dependencies
|
|
||||||
run: npm install
|
|
||||||
# Step 5: Publish to npm
|
|
||||||
- name: Publish package
|
|
||||||
run: npm publish
|
|
||||||
env:
|
|
||||||
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} # 使用 GitHub Secrets 来安全存储 npm token
|
|
||||||
NPM_TOKEN: ${{ secrets.NPM_TOKEN }} # 使用 GitHub Secrets 来安全存储 npm token
|
|
||||||
38
.github/workflows/publish.yml
vendored
Normal file
38
.github/workflows/publish.yml
vendored
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
name: Publish to npm
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- 'v*.*.*' # 当推送带有版本号的 tag 时触发,例如 v1.0.0
|
||||||
|
workflow_dispatch: # 添加手动触发器
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
publish:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
# Step 1: Clone current Git repository
|
||||||
|
- name: Checkout this repository
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
# Step 3: Setup Node.js and install dependencies
|
||||||
|
- name: Setup Node.js
|
||||||
|
uses: actions/setup-node@v3
|
||||||
|
with:
|
||||||
|
node-version: '20.6'
|
||||||
|
registry-url: 'https://registry.npmjs.org/'
|
||||||
|
cache: 'npm' # 启用 npm 缓存,提高安装速度
|
||||||
|
- name: Configure npm authentication
|
||||||
|
run: npm config set //registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}
|
||||||
|
- name: Install dependencies
|
||||||
|
run: npm install
|
||||||
|
- name: Build project
|
||||||
|
run: npm run build
|
||||||
|
# Step 6: 发布到 npm
|
||||||
|
- name: Publish package
|
||||||
|
run: npm publish
|
||||||
|
env:
|
||||||
|
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
||||||
|
# Step 7: 发布成功后,更新版本标签
|
||||||
|
# - name: Create Git tag
|
||||||
|
# run: |
|
||||||
|
# TAG="v$(node -p -e "require('./package.json').version")"
|
||||||
|
# git tag $TAG
|
||||||
|
# git push origin $TAG
|
||||||
13
package.json
13
package.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@kevisual/query",
|
"name": "@kevisual/query",
|
||||||
"version": "0.0.5",
|
"version": "0.0.7-alpha.1",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"module": "dist/index.js",
|
"module": "dist/index.js",
|
||||||
"types": "dist/index.d.ts",
|
"types": "dist/index.d.ts",
|
||||||
@@ -14,8 +14,11 @@
|
|||||||
"files": [
|
"files": [
|
||||||
"dist"
|
"dist"
|
||||||
],
|
],
|
||||||
"keywords": [],
|
"keywords": [
|
||||||
"author": "",
|
"kevisual",
|
||||||
|
"query"
|
||||||
|
],
|
||||||
|
"author": "abearxiong",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"description": "",
|
"description": "",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
@@ -31,6 +34,10 @@
|
|||||||
"publishConfig": {
|
"publishConfig": {
|
||||||
"access": "public"
|
"access": "public"
|
||||||
},
|
},
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "git+ssh://git@github.com/abearxiong/kevisual-query.git"
|
||||||
|
},
|
||||||
"exports": {
|
"exports": {
|
||||||
".": {
|
".": {
|
||||||
"import": "./dist/index.js",
|
"import": "./dist/index.js",
|
||||||
|
|||||||
@@ -18,23 +18,24 @@ export default [
|
|||||||
typescript({
|
typescript({
|
||||||
allowImportingTsExtensions: true,
|
allowImportingTsExtensions: true,
|
||||||
noEmit: true,
|
noEmit: true,
|
||||||
|
exclude: ['src/node-adapter.ts'],
|
||||||
}), // 使用 @rollup/plugin-typescript 处理 TypeScript 文件
|
}), // 使用 @rollup/plugin-typescript 处理 TypeScript 文件
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
{
|
// {
|
||||||
input: 'src/node-adapter.ts', // TypeScript 入口文件
|
// input: 'src/node-adapter.ts', // TypeScript 入口文件
|
||||||
output: {
|
// output: {
|
||||||
file: 'dist/node-adapter.js', // 输出文件
|
// file: 'dist/node-adapter.js', // 输出文件
|
||||||
format: 'es', // 输出格式设置为 ES 模块
|
// format: 'es', // 输出格式设置为 ES 模块
|
||||||
},
|
// },
|
||||||
plugins: [
|
// plugins: [
|
||||||
resolve(), // 使用 @rollup/plugin-node-resolve 解析 node_modules 中的模块
|
// resolve(), // 使用 @rollup/plugin-node-resolve 解析 node_modules 中的模块
|
||||||
typescript({
|
// typescript({
|
||||||
allowImportingTsExtensions: true,
|
// allowImportingTsExtensions: true,
|
||||||
noEmit: true,
|
// noEmit: true,
|
||||||
}), // 使用 @rollup/plugin-typescript 处理 TypeScript 文件
|
// }), // 使用 @rollup/plugin-typescript 处理 TypeScript 文件
|
||||||
],
|
// ],
|
||||||
},
|
// },
|
||||||
{
|
{
|
||||||
input: 'src/ws.ts', // TypeScript 入口文件
|
input: 'src/ws.ts', // TypeScript 入口文件
|
||||||
output: {
|
output: {
|
||||||
@@ -46,6 +47,7 @@ export default [
|
|||||||
typescript({
|
typescript({
|
||||||
allowImportingTsExtensions: true,
|
allowImportingTsExtensions: true,
|
||||||
noEmit: true,
|
noEmit: true,
|
||||||
|
declaration: false,
|
||||||
}), // 使用 @rollup/plugin-typescript 处理 TypeScript 文件
|
}), // 使用 @rollup/plugin-typescript 处理 TypeScript 文件
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|||||||
18
src/index.ts
18
src/index.ts
@@ -1,6 +1,6 @@
|
|||||||
import { adapter } from './adapter.ts';
|
import { adapter } from './adapter.ts';
|
||||||
import { QueryWs } from './ws.ts';
|
import { QueryWs, QueryWsOpts } from './ws.ts';
|
||||||
export { QueryOpts };
|
export { QueryOpts, QueryWs };
|
||||||
type Fn = (opts: {
|
type Fn = (opts: {
|
||||||
url?: string;
|
url?: string;
|
||||||
headers?: Record<string, string>;
|
headers?: Record<string, string>;
|
||||||
@@ -64,6 +64,7 @@ export class Query<U = any, V = any> {
|
|||||||
const headers = { ...this.headers, ...options?.headers };
|
const headers = { ...this.headers, ...options?.headers };
|
||||||
const adapter = options?.adapter || this.adapter;
|
const adapter = options?.adapter || this.adapter;
|
||||||
const beforeRequest = options?.beforeRequest || this.beforeRequest;
|
const beforeRequest = options?.beforeRequest || this.beforeRequest;
|
||||||
|
const afterResponse = options?.afterResponse || this.afterResponse;
|
||||||
const timeout = options?.timeout || this.timeout;
|
const timeout = options?.timeout || this.timeout;
|
||||||
const req = {
|
const req = {
|
||||||
url: url,
|
url: url,
|
||||||
@@ -76,8 +77,8 @@ export class Query<U = any, V = any> {
|
|||||||
}
|
}
|
||||||
return adapter(req).then(async (res) => {
|
return adapter(req).then(async (res) => {
|
||||||
res.success = res.code === 200;
|
res.success = res.code === 200;
|
||||||
if (options?.afterResponse) {
|
if (afterResponse) {
|
||||||
return await options.afterResponse(res);
|
return await afterResponse(res);
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
});
|
});
|
||||||
@@ -97,7 +98,7 @@ export class QueryClient<U = any, V = any> extends Query<U, V> {
|
|||||||
storage: Storage;
|
storage: Storage;
|
||||||
token: string;
|
token: string;
|
||||||
qws: QueryWs;
|
qws: QueryWs;
|
||||||
constructor(opts?: QueryOpts & { tokenName?: string; storage?: Storage }) {
|
constructor(opts?: QueryOpts & { tokenName?: string; storage?: Storage; io?: boolean }) {
|
||||||
super(opts);
|
super(opts);
|
||||||
this.tokenName = opts?.tokenName || 'token';
|
this.tokenName = opts?.tokenName || 'token';
|
||||||
this.storage = opts?.storage || localStorage;
|
this.storage = opts?.storage || localStorage;
|
||||||
@@ -111,7 +112,12 @@ export class QueryClient<U = any, V = any> extends Query<U, V> {
|
|||||||
}
|
}
|
||||||
return opts;
|
return opts;
|
||||||
};
|
};
|
||||||
this.qws = new QueryWs({ url: opts?.url });
|
if (opts?.io) {
|
||||||
|
this.createWs();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
createWs(opts?: QueryWsOpts) {
|
||||||
|
this.qws = new QueryWs({ url: this.url });
|
||||||
}
|
}
|
||||||
getToken() {
|
getToken() {
|
||||||
return this.storage.getItem(this.tokenName);
|
return this.storage.getItem(this.tokenName);
|
||||||
|
|||||||
41
src/ws.ts
41
src/ws.ts
@@ -9,7 +9,7 @@ type QueryWsStore = {
|
|||||||
};
|
};
|
||||||
export type QuerySelectState = 'connecting' | 'connected' | 'disconnected';
|
export type QuerySelectState = 'connecting' | 'connected' | 'disconnected';
|
||||||
export type QueryWsStoreListener = (newState: QueryWsStore, oldState: QueryWsStore) => void;
|
export type QueryWsStoreListener = (newState: QueryWsStore, oldState: QueryWsStore) => void;
|
||||||
type QueryWsOpts = {
|
export type QueryWsOpts = {
|
||||||
url?: string;
|
url?: string;
|
||||||
store?: StoreApi<QueryWsStore>;
|
store?: StoreApi<QueryWsStore>;
|
||||||
ws?: WebSocket;
|
ws?: WebSocket;
|
||||||
@@ -45,22 +45,31 @@ export class QueryWs {
|
|||||||
/**
|
/**
|
||||||
* 连接 WebSocket
|
* 连接 WebSocket
|
||||||
*/
|
*/
|
||||||
connect() {
|
async connect(opts?: { timeout?: number }) {
|
||||||
const store = this.store;
|
const store = this.store;
|
||||||
const connected = store.getState().connected;
|
const connected = store.getState().connected;
|
||||||
if (connected) {
|
if (connected) {
|
||||||
return;
|
return Promise.resolve(true);
|
||||||
}
|
}
|
||||||
const ws = this.ws || new WebSocket(this.url);
|
return new Promise((resolve, reject) => {
|
||||||
ws.onopen = () => {
|
const ws = this.ws || new WebSocket(this.url);
|
||||||
store.getState().setConnected(true);
|
const timeout = opts?.timeout || 5 * 60 * 1000; // 默认 2 分钟
|
||||||
store.getState().setStatus('connected');
|
let timer = setTimeout(() => {
|
||||||
};
|
console.error('WebSocket 连接超时');
|
||||||
ws.onclose = () => {
|
reject('timeout');
|
||||||
store.getState().setConnected(false);
|
}, timeout);
|
||||||
store.getState().setStatus('disconnected');
|
ws.onopen = () => {
|
||||||
this.ws = null;
|
store.getState().setConnected(true);
|
||||||
};
|
store.getState().setStatus('connected');
|
||||||
|
resolve(true);
|
||||||
|
clearTimeout(timer);
|
||||||
|
};
|
||||||
|
ws.onclose = () => {
|
||||||
|
store.getState().setConnected(false);
|
||||||
|
store.getState().setStatus('disconnected');
|
||||||
|
this.ws = null;
|
||||||
|
};
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
listenConnect(callback: () => void) {
|
listenConnect(callback: () => void) {
|
||||||
@@ -141,4 +150,10 @@ export class QueryWs {
|
|||||||
ws.send(data as string);
|
ws.send(data as string);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
getOpen() {
|
||||||
|
if (!this.ws) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return this.ws.readyState === WebSocket.OPEN;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user