diff --git a/src/stocks/base/index.ts b/src/stocks/base/index.ts index 6364629..bb56d1c 100644 --- a/src/stocks/base/index.ts +++ b/src/stocks/base/index.ts @@ -5,14 +5,14 @@ import StockApi from "types/stocks/index"; /** * 基础股票代码接口 */ -class Base implements StockApi { +const Base: StockApi = { /** * 获取股票数据 * @param code 股票代码 */ async getStock(code: string): Promise { throw new Error("未实现获取股票数据"); - } + }, /** * 获取股票数据组 @@ -21,6 +21,6 @@ class Base implements StockApi { async getStocks(codes: string[]): Promise { throw new Error("未实现获取股票数据组"); } -} +}; export default Base; diff --git a/src/stocks/netease/index.ts b/src/stocks/netease/index.ts index b7aa852..f0b044c 100644 --- a/src/stocks/netease/index.ts +++ b/src/stocks/netease/index.ts @@ -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, }; - } + }, /** * 获取股票数据组 diff --git a/src/stocks/sina/index.ts b/src/stocks/sina/index.ts index 05caf16..78d10a6 100644 --- a/src/stocks/sina/index.ts +++ b/src/stocks/sina/index.ts @@ -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(); - } + }, /** * 获取股票数据组 diff --git a/src/stocks/tencent/index.ts b/src/stocks/tencent/index.ts index a38d4a8..0bdd5c6 100644 --- a/src/stocks/tencent/index.ts +++ b/src/stocks/tencent/index.ts @@ -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(); - } + }, /** * 获取股票数据组 diff --git a/src/stocks/xueqiu/index.ts b/src/stocks/xueqiu/index.ts index ae5cfe1..034cb9a 100644 --- a/src/stocks/xueqiu/index.ts +++ b/src/stocks/xueqiu/index.ts @@ -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 } = { /** * 获取 Token */ - async getToken() { - if (this.token !== '') return this.token; + async getToken(): Promise { + 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(); - } + }, /** * 获取股票数据组 diff --git a/test/stocks/base/index.test.ts b/test/stocks/base/index.test.ts index e876078..1d10890 100644 --- a/test/stocks/base/index.test.ts +++ b/test/stocks/base/index.test.ts @@ -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("未实现获取股票数据组")); }); diff --git a/test/stocks/netease/index.test.ts b/test/stocks/netease/index.test.ts index 8997ca6..8f8c10d 100644 --- a/test/stocks/netease/index.test.ts +++ b/test/stocks/netease/index.test.ts @@ -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" }]); }); diff --git a/test/stocks/sina/index.test.ts b/test/stocks/sina/index.test.ts index d05d465..01fdfe1 100644 --- a/test/stocks/sina/index.test.ts +++ b/test/stocks/sina/index.test.ts @@ -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" }]); }); diff --git a/test/stocks/tencent/index.test.ts b/test/stocks/tencent/index.test.ts index 8e5e960..e390ce7 100644 --- a/test/stocks/tencent/index.test.ts +++ b/test/stocks/tencent/index.test.ts @@ -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" }]); }); diff --git a/test/stocks/xueqiu/index.test.ts b/test/stocks/xueqiu/index.test.ts index 5604f30..36e720d 100644 --- a/test/stocks/xueqiu/index.test.ts +++ b/test/stocks/xueqiu/index.test.ts @@ -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" }]); }); diff --git a/types/stocks/index.d.ts b/types/stocks/index.d.ts index ccdff95..16006af 100644 --- a/types/stocks/index.d.ts +++ b/types/stocks/index.d.ts @@ -1,6 +1,6 @@ import Stock from '../utils/stock'; -declare class StockApi { +export interface StockApi { /** * 获取股票数据 * @param code 股票代码