Compare commits

...

3 Commits
main ... v0.0.5

Author SHA1 Message Date
742a562109 feat: 添加queryclient 2024-09-28 22:22:17 +08:00
61a68fbd41 feat: add publish workflow 2024-09-28 19:37:35 +08:00
9ba7fb2f10 fix: types fix 2024-09-28 19:22:00 +08:00
4 changed files with 78 additions and 5 deletions

35
.github/workflow/publish.yml vendored Normal file
View File

@ -0,0 +1,35 @@
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

View File

@ -1,6 +1,6 @@
{ {
"name": "@kevisual/query", "name": "@kevisual/query",
"version": "0.0.3", "version": "0.0.5",
"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",

View File

@ -1,5 +1,6 @@
import { adapter } from './adapter.ts'; import { adapter } from './adapter.ts';
export {QueryWs} from './ws.ts' import { QueryWs } from './ws.ts';
export { QueryOpts };
type Fn = (opts: { type Fn = (opts: {
url?: string; url?: string;
headers?: Record<string, string>; headers?: Record<string, string>;
@ -37,8 +38,10 @@ type DataOpts = Partial<QueryOpts> & {
* path: 'demo', * path: 'demo',
* key: '1', * key: '1',
* }); * });
*
* U是参数 V是返回值
*/ */
export class Query { export class Query<U = any, V = any> {
adapter: typeof adapter; adapter: typeof adapter;
url: string; url: string;
beforeRequest?: Fn; beforeRequest?: Fn;
@ -53,10 +56,10 @@ export class Query {
}; };
this.timeout = opts?.timeout || 60000; // 默认超时时间为 60s this.timeout = opts?.timeout || 60000; // 默认超时时间为 60s
} }
async get<T, S>(params: Record<string, any> & Data & T, options?: DataOpts): Promise<Result<S>> { async get<T = any, S = any>(params: Record<string, any> & Data & U & T, options?: DataOpts): Promise<Result<V & S>> {
return this.post(params, options); return this.post(params, options);
} }
async post<T, S>(body: Record<string, any> & Data & T, options?: DataOpts): Promise<Result<S>> { async post<T = any, S = any>(body: Record<string, any> & Data & T, options?: DataOpts): Promise<Result<S>> {
const url = options?.url || this.url; const url = options?.url || this.url;
const headers = { ...this.headers, ...options?.headers }; const headers = { ...this.headers, ...options?.headers };
const adapter = options?.adapter || this.adapter; const adapter = options?.adapter || this.adapter;
@ -86,5 +89,39 @@ export class Query {
this.afterResponse = fn; this.afterResponse = fn;
} }
} }
/**
* QueryRouter
*/
export class QueryClient<U = any, V = any> extends Query<U, V> {
tokenName: string;
storage: Storage;
token: string;
qws: QueryWs;
constructor(opts?: QueryOpts & { tokenName?: string; storage?: Storage }) {
super(opts);
this.tokenName = opts?.tokenName || 'token';
this.storage = opts?.storage || localStorage;
this.beforeRequest = async (opts) => {
const token = this.token || this.getToken();
if (token) {
opts.headers = {
...opts.headers,
Authorization: `Bearer ${token}`,
};
}
return opts;
};
this.qws = new QueryWs({ url: opts?.url });
}
getToken() {
return this.storage.getItem(this.tokenName);
}
saveToken(token: string) {
this.storage.setItem(this.tokenName, token);
}
removeToken() {
this.storage.removeItem(this.tokenName);
}
}
export { adapter }; export { adapter };

View File

@ -58,6 +58,7 @@ export class QueryWs {
}; };
ws.onclose = () => { ws.onclose = () => {
store.getState().setConnected(false); store.getState().setConnected(false);
store.getState().setStatus('disconnected');
this.ws = null; this.ws = null;
}; };
} }