feat: 修改 数据源导出类为对象

This commit is contained in:
zhangxiangliang 2020-08-20 02:20:10 +08:00
parent b8969bd2e0
commit a371dbc714
11 changed files with 34 additions and 37 deletions

View File

@ -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;

View File

@ -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,
};
}
},
/**
*

View File

@ -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();
}
},
/**
*

View File

@ -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();
}
},
/**
*

View File

@ -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();
}
},
/**
*

View File

@ -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("未实现获取股票数据组"));
});

View File

@ -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" }]);
});

View File

@ -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" }]);
});

View File

@ -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" }]);
});

View File

@ -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" }]);
});

View File

@ -1,6 +1,6 @@
import Stock from '../utils/stock';
declare class StockApi {
export interface StockApi {
/**
*
* @param code