fix: types fix
feat: add publish workflow feat: 添加queryclient fix: 修改路径 feat: 添加手动执行workflows fix: 修改package.json fix: fix publish fix: publish test fix temp
This commit is contained in:
parent
ed178ee4c6
commit
fe5b3ec15b
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",
|
||||
"version": "0.0.3",
|
||||
"version": "0.0.5",
|
||||
"main": "dist/index.js",
|
||||
"module": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
@ -14,8 +14,11 @@
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"keywords": [
|
||||
"kevisual",
|
||||
"query"
|
||||
],
|
||||
"author": "abearxiong",
|
||||
"license": "ISC",
|
||||
"description": "",
|
||||
"devDependencies": {
|
||||
@ -31,6 +34,10 @@
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+ssh://git@github.com/abearxiong/kevisual-query.git"
|
||||
},
|
||||
"exports": {
|
||||
".": {
|
||||
"import": "./dist/index.js",
|
||||
|
45
src/index.ts
45
src/index.ts
@ -1,5 +1,6 @@
|
||||
import { adapter } from './adapter.ts';
|
||||
export {QueryWs} from './ws.ts'
|
||||
import { QueryWs } from './ws.ts';
|
||||
export { QueryOpts };
|
||||
type Fn = (opts: {
|
||||
url?: string;
|
||||
headers?: Record<string, string>;
|
||||
@ -37,8 +38,10 @@ type DataOpts = Partial<QueryOpts> & {
|
||||
* path: 'demo',
|
||||
* key: '1',
|
||||
* });
|
||||
*
|
||||
* U是参数 V是返回值
|
||||
*/
|
||||
export class Query {
|
||||
export class Query<U = any, V = any> {
|
||||
adapter: typeof adapter;
|
||||
url: string;
|
||||
beforeRequest?: Fn;
|
||||
@ -53,10 +56,10 @@ export class Query {
|
||||
};
|
||||
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);
|
||||
}
|
||||
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 headers = { ...this.headers, ...options?.headers };
|
||||
const adapter = options?.adapter || this.adapter;
|
||||
@ -86,5 +89,39 @@ export class Query {
|
||||
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 };
|
||||
|
Loading…
x
Reference in New Issue
Block a user