mirror of
https://github.com/zhangxiangliang/stock-api.git
synced 2025-01-23 06:00:24 +08:00
feat: 修改 数据源导出类为对象
This commit is contained in:
parent
b8969bd2e0
commit
a371dbc714
@ -5,14 +5,14 @@ import StockApi from "types/stocks/index";
|
||||
/**
|
||||
* 基础股票代码接口
|
||||
*/
|
||||
class Base implements StockApi {
|
||||
const Base: StockApi = {
|
||||
/**
|
||||
* 获取股票数据
|
||||
* @param code 股票代码
|
||||
*/
|
||||
async getStock(code: string): Promise<Stock> {
|
||||
throw new Error("未实现获取股票数据");
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 获取股票数据组
|
||||
@ -21,6 +21,6 @@ class Base implements StockApi {
|
||||
async getStocks(codes: string[]): Promise<Stock[]> {
|
||||
throw new Error("未实现获取股票数据组");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default Base;
|
||||
|
@ -8,11 +8,12 @@ import fetch from "@utils/fetch";
|
||||
|
||||
// Types
|
||||
import Stock from "types/utils/stock";
|
||||
import StockApi from "types/stocks/index";
|
||||
|
||||
/**
|
||||
* 网易股票代码接口
|
||||
*/
|
||||
class Netease extends Base {
|
||||
const Netease: StockApi = {
|
||||
/**
|
||||
* 获取股票数据
|
||||
* @param code 股票代码
|
||||
@ -37,7 +38,7 @@ class Netease extends Base {
|
||||
high: 0,
|
||||
yesterday: 0,
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 获取股票数据组
|
||||
|
@ -9,11 +9,12 @@ import iconv from "@utils/iconv";
|
||||
|
||||
// Types
|
||||
import Stock from "types/utils/stock";
|
||||
import StockApi from "types/stocks/index";
|
||||
|
||||
/**
|
||||
* 新浪股票代码接口
|
||||
*/
|
||||
class Sina extends Base {
|
||||
const Sina: StockApi = {
|
||||
/**
|
||||
* 获取股票数据
|
||||
* @param code 股票代码
|
||||
@ -35,7 +36,7 @@ class Sina extends Base {
|
||||
const data = (new SinaStockTransform(code, params));
|
||||
|
||||
return data.getStock();
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 获取股票数据组
|
||||
|
@ -9,18 +9,12 @@ import iconv from "@utils/iconv";
|
||||
|
||||
// Types
|
||||
import Stock from "types/utils/stock";
|
||||
import StockApi from "types/stocks/index";
|
||||
|
||||
/**
|
||||
* 腾讯股票代码接口
|
||||
*/
|
||||
class Tencent extends Base {
|
||||
/**
|
||||
* 构造函数
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
const Tencent: StockApi = {
|
||||
/**
|
||||
* 获取股票数据
|
||||
* @param code 股票代码
|
||||
@ -42,7 +36,7 @@ class Tencent extends Base {
|
||||
const data = (new TencentStockTransform(code, params));
|
||||
|
||||
return data.getStock();
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 获取股票数据组
|
||||
|
@ -8,28 +8,29 @@ import fetch from "@utils/fetch";
|
||||
|
||||
// Types
|
||||
import Stock from "types/utils/stock";
|
||||
import StockApi from "types/stocks/index";
|
||||
import Dictionary from "types/utils/dictionary";
|
||||
|
||||
let token: string = '';
|
||||
|
||||
/**
|
||||
* 雪球股票代码接口
|
||||
*/
|
||||
class Xueqiu extends Base {
|
||||
public token: string = '';
|
||||
|
||||
const Xueqiu: StockApi & { getToken(): Promise<string> } = {
|
||||
/**
|
||||
* 获取 Token
|
||||
*/
|
||||
async getToken() {
|
||||
if (this.token !== '') return this.token;
|
||||
async getToken(): Promise<string> {
|
||||
if (token !== '') return token;
|
||||
|
||||
const res = await fetch.get('https://xueqiu.com/');
|
||||
const cookies: string[] = res.headers['set-cookie'];
|
||||
|
||||
const param: string = cookies.filter(key => key.includes('xq_a_token'))[0] || '';
|
||||
this.token = param.split(';')[0] || '';
|
||||
token = param.split(';')[0] || '';
|
||||
|
||||
return this.token;
|
||||
}
|
||||
return token;
|
||||
},
|
||||
|
||||
/**
|
||||
* 获取股票数据
|
||||
@ -51,7 +52,7 @@ class Xueqiu extends Base {
|
||||
const data = (new XueqiuStockTransform(code, params));
|
||||
|
||||
return data.getStock();
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 获取股票数据组
|
||||
|
@ -3,13 +3,13 @@ const Base = require("stocks/base").default;
|
||||
|
||||
describe("【基础】股票代码接口", () => {
|
||||
it("需要获取的股票代码", async () => {
|
||||
await expect(new Base().getStock("SZ000000"))
|
||||
await expect(Base.getStock("SZ000000"))
|
||||
.rejects
|
||||
.toThrow(new Error("未实现获取股票数据"));
|
||||
});
|
||||
|
||||
it("需要获取的股票代码组", async () => {
|
||||
await expect(new Base().getStocks(["SZ000000"]))
|
||||
await expect(Base.getStocks(["SZ000000"]))
|
||||
.rejects
|
||||
.toThrow(new Error("未实现获取股票数据组"));
|
||||
});
|
||||
|
@ -3,13 +3,13 @@ const Netease = require("stocks/netease").default;
|
||||
|
||||
describe("【网易】股票代码接口", () => {
|
||||
it("需要获取的股票代码", async () => {
|
||||
await expect(new Netease().getStock("SH510500"))
|
||||
await expect(Netease.getStock("SH510500"))
|
||||
.resolves
|
||||
.toMatchObject({ code: "SH510500", name: "500ETF" });
|
||||
});
|
||||
|
||||
it("需要获取的股票代码组", async () => {
|
||||
await expect((new Netease()).getStocks(["SH510500"]))
|
||||
await expect(Netease.getStocks(["SH510500"]))
|
||||
.resolves
|
||||
.toMatchObject([{ code: "SH510500", name: "500ETF" }]);
|
||||
});
|
||||
|
@ -3,13 +3,13 @@ const Sina = require("stocks/sina").default;
|
||||
|
||||
describe("【新浪】股票代码接口", () => {
|
||||
it("需要获取的股票代码", async () => {
|
||||
await expect(new Sina().getStock("SH510500"))
|
||||
await expect(Sina.getStock("SH510500"))
|
||||
.resolves
|
||||
.toMatchObject({ code: "SH510500", name: "500ETF" });
|
||||
});
|
||||
|
||||
it("需要获取的股票代码组", async () => {
|
||||
await expect((new Sina()).getStocks(["SH510500"]))
|
||||
await expect(Sina.getStocks(["SH510500"]))
|
||||
.resolves
|
||||
.toMatchObject([{ code: "SH510500", name: "500ETF" }]);
|
||||
});
|
||||
|
@ -3,13 +3,13 @@ const Tencent = require("stocks/tencent").default;
|
||||
|
||||
describe("【腾讯】股票代码接口", () => {
|
||||
it("需要获取的股票代码", async () => {
|
||||
await expect(new Tencent().getStock("SH510500"))
|
||||
await expect(Tencent.getStock("SH510500"))
|
||||
.resolves
|
||||
.toMatchObject({ code: "SH510500", name: "500ETF" });
|
||||
});
|
||||
|
||||
it("需要获取的股票代码组", async () => {
|
||||
await expect((new Tencent()).getStocks(["SH510500"]))
|
||||
await expect(Tencent.getStocks(["SH510500"]))
|
||||
.resolves
|
||||
.toMatchObject([{ code: "SH510500", name: "500ETF" }]);
|
||||
});
|
||||
|
@ -3,19 +3,19 @@ const Xueqiu = require("stocks/xueqiu").default;
|
||||
|
||||
describe("【雪球】股票代码接口", () => {
|
||||
it("获取 Token", async () => {
|
||||
await expect(new Xueqiu().getToken())
|
||||
await expect(Xueqiu.getToken())
|
||||
.resolves
|
||||
.toContain('xq_a_token');
|
||||
});
|
||||
|
||||
it("需要获取的股票代码", async () => {
|
||||
await expect(new Xueqiu().getStock("SH510500"))
|
||||
await expect(Xueqiu.getStock("SH510500"))
|
||||
.resolves
|
||||
.toMatchObject({ code: "SH510500", name: "中证500ETF" });
|
||||
});
|
||||
|
||||
it("需要获取的股票代码组", async () => {
|
||||
await expect((new Xueqiu()).getStocks(["SH510500"]))
|
||||
await expect(Xueqiu.getStocks(["SH510500"]))
|
||||
.resolves
|
||||
.toMatchObject([{ code: "SH510500", name: "中证500ETF" }]);
|
||||
});
|
||||
|
2
types/stocks/index.d.ts
vendored
2
types/stocks/index.d.ts
vendored
@ -1,6 +1,6 @@
|
||||
import Stock from '../utils/stock';
|
||||
|
||||
declare class StockApi {
|
||||
export interface StockApi {
|
||||
/**
|
||||
* 获取股票数据
|
||||
* @param code 股票代码
|
||||
|
Loading…
Reference in New Issue
Block a user