8000 Fixes the response format, also adds tests to make sure changes to th… · polygon-io/client-js@507a889 · GitHub
[go: up one dir, main page]

Skip to content

Commit 507a889

Browse files
Fixes the response format, also adds tests to make sure changes to this have coverage (#179)
* Fix hole in testing, also maybe fixed the request format * 7.3.1 * Release notes
1 parent 75aa6ed commit 507a889

File tree< 8000 /h2>

10 files changed

+128
-10
lines changed

10 files changed

+128
-10
lines changed

CHANGELOG.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
## [7.3.0](https://github.com/polygon-io/client-js/compare/v7.2.6...v7.3.0) (2023-08-11)
1+
## [7.3.1](https://github.com/polygon-io/client-js/compare/v7.3.0...v7.3.1) (2023-09-15)
2+
### Bug Fixes
3+
* Fix unintended change to top level response object that returned results array instead of results object
4+
5+
### Other Changes
6+
* Improved test coverage for response object
7+
8+
## [7.3.0](https://github.com/polygon-io/client-js/compare/v7.2.6...v7.3.0) (2023-09-11)
29
### New Feature
310
* Update README with pagination and trace modes
411
* Added request tracing and API pagination support

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@polygon.io/client-js",
3-
"version": "7.3.0",
3+
"version": "7.3.1",
44
"description": "Isomorphic Javascript client for Polygon.io Stocks, Forex, and Crypto APIs",
55
"main": "dist/main.js",
66
"types": "dist/main.d.ts",

src/rest/crypto/index.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ describe("[REST] Crypto", () => {
3737
sandbox.restore();
3838
});
3939

40+
it("should have appropriate results", async () => {
41+
setStub({ results: [{ o: 1}] });
42+
const response = await crypto.aggregates("AAPL", 1, "day", "2019-01-01", "2019-02-01", mocks.query, mocks.overrideOptions);
43+
response.results![0].t?.should.eql(1);
44+
});
45+
4046
it("aggregates call /v2/aggs/ticker/{ticker}/range/{multiplier}/{timespan}/{from}/{to}", async () => {
4147
setStub({ results: [] });
4248
await crypto.aggregates("BTC", 1, "day", "2019-01-01", "2019-02-01", mocks.query, mocks.overrideOptions);

src/rest/forex/index.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ describe("[REST] Forex / Currencies", () => {
3737
sandbox.restore();
3838
});
3939

40+
it("should have appropriate results", async () => {
41+
setStub({ results: [{ o: 1}] });
42+
const response = await fx.aggregates("AAPL", 1, "day", "2019-01-01", "2019-02-01", mocks.query, mocks.overrideOptions);
43+
response.results![0].t?.should.eql(1);
44+
});
45+
4046
it("aggregates call /v2/aggs/ticker/{ticker}/range/{multiplier}/{timespan}/{from}/{to}", async () => {
4147
setStub({ results: [] });
4248
await fx.aggregates("EURCHF", 1, "day", "2019-01-01", "2019-02-01", mocks.query, mocks.overrideOptions);

src/rest/indices/index.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ describe("[REST] Indices", () => {
3838
sandbox.restore();
3939
});
4040

41+
it("should have appropriate results", async () => {
42+
setStub({ results: [{ o: 1}] });
43+
const response = await indices.aggregates("AAPL", 1, "day", "2019-01-01", "2019-02-01", mocks.query, mocks.overrideOptions);
44+
response.results![0].t?.should.eql(1);
45+
});
46+
4147
it("aggregates call /v2/aggs/ticker/{ticker}/range/{multiplier}/{timespan}/{from}/{to}", async () => {
4248
setStub({ results: [] });
4349
await indices.aggregates("AAPL", 1, "day", "2019-01-01", "2019-02-01", mocks.query, mocks.overrideOptions);

src/rest/options/index.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ describe("[REST] Options", () => {
3838
sandbox.restore();
3939
});
4040

41+
it("should have appropriate results", async () => {
42+
setStub({ results: [{ o: 1}] });
43+
const response = await options.aggregates("AAPL", 1, "day", "2019-01-01", "2019-02-01", mocks.query, mocks.overrideOptions);
44+
response.results![0].t?.should.eql(1);
45+
});
46+
4147
it("aggregates call /v2/aggs/ticker/{ticker}/range/{multiplier}/{timespan}/{from}/{to}", async () => {
4248
setStub({ results: [] });
4349
await options.aggregates("AAPL", 1, "day", "2019-01-01", "2019-02-01", mocks.query, mocks.overrideOptions);

src/rest/stocks/index.test.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,20 @@ describe("[REST] Stocks", () => {
3838
sandbox.restore();
3939
});
4040

41+
it("should have appropriate results", async () => {
42+
setStub({ results: [{ o: 1}] });
43+
const response = await stocks.aggregates("AAPL", 1, "day", "2019-01-01", "2019-02-01", mocks.query, mocks.overrideOptions);
44+
response.results![0].t?.should.eql(1);
45+
});
46+
4147
it("aggregates call /v2/aggs/ticker/{ticker}/range/{multiplier}/{timespan}/{from}/{to}", async () => {
42-
setStub({ results: [] });
43-
await stocks.aggregates("AAPL", 1, "day", "2019-01-01", "2019-02-01", mocks.query, mocks.overrideOptions);
48+
setStub({ results: [{ o: 1}] });
49+
const response = await stocks.aggregates("AAPL", 1, "day", "2019-01-01", "2019-02-01", mocks.query, mocks.overrideOptions);
4450
fetchStub.callCount.should.eql(1);
4551
getPath(fetchStub.getCalls()[0].args[0]).should.eql(
4652
"/v2/aggs/ticker/AAPL/range/1/day/2019-01-01/2019-02-01"
4753
);
54+
response.results?.[0].o?.should.eql(1);
4855
fetchStub.getCalls()[0].args[0].indexOf(mocks.query.query1).should.be.gt(-1);
4956
fetchStub.getCalls()[0].args[1].referrer.should.eql(mocks.overrideOptions.referrer);
5057
fetchStub.getCalls()[0].args[1].headers.header1.should.eql(mocks.globalOptions.headers.header1);

src/rest/transport/index.test.ts

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import * as sinon from "sinon";
2+
import * as chai from "chai";
3+
import fetchModule from '../transport/fetch';
4+
import { getWithGlobals } from '../transport/request';
5+
6+
describe("[REST] Stocks", () => {
7+
chai.should();
8+
const mocks = {
9+
key: 'invalid',
10+
base: 'https://test.api.polygon.io',
11+
query: { query1: 'queryVal' },
12+
overrideOptions: {
13+
referrer: 'overrideVal'
14+
},
15+
globalOptions: {
16+
referrer: 'globalVal1',
17+
headers: {
18+
header1: 'headerVal1'
19+
}
20+
}
21+
};
22+
let fetchStub;
23+
const sandbox = sinon.createSandbox();
24+
const getPath = (url) => url.slice(mocks.base.length, url.indexOf('?'));
25+
const setStub = (returnVal, status = 200) => {
26+
fetchStub?.restore();
27+
fetchStub = sandbox.stub(fetchModule, 'fetch').returns(
28+
Promise.resolve({ json: () => Promise.resolve(returnVal), status } as Response)
29+
);
30+
}
31+
const setStubs = (returnVal1, returnVal2, status = 200) => {
32+
fetchStub?.restore();
33+
fetchStub = sandbox.stub(fetchModule, 'fetch').onCall(0).returns(
34+
Promise.resolve({ json: () => Promise.resolve(returnVal1), status } as Response)
35+
).onCall(1).returns(
36+
Promise.resolve({ json: () => Promise.resolve(returnVal2), status } as Response)
37+
);
38+
}
39+
beforeEach(async () => {
40+
setStub({ ticks: [], results: [], tickers: [] });
41+
});
42+
afterEach(() => {
43+
sandbox.restore();
44+
});
45+
46+
it("Returns the expected result when pagination is not enabled", async () => {
47+
setStub({ results: [{ a: 1 }] });
48+
const requestFn = getWithGlobals(mocks.key, mocks.base, mocks.globalOptions);
49+
const response = await requestFn("/v2/aggs/ticker/AAPL/range/1/day/2019-01-01/2019-02-01", mocks.query, mocks.overrideOptions);
50+
response.results[0].a.should.eql(1);
51+
fetchStub.callCount.should.eql(1);
52+
getPath(fetchStub.getCalls()[0].args[0]).should.eql(
53+
"/v2/aggs/ticker/AAPL/range/1/day/2019-01-01/2019-02-01"
54+
);
55+
fetchStub.getCalls()[0].args[0].indexOf(mocks.query.query1).should.be.gt(-1);
56+
fetchStub.getCalls()[0].args[1].referrer.should.eql(mocks.overrideOptions.referrer);
57+
fetchStub.getCalls()[0].args[1].headers.header1.should.eql(mocks.globalOptions.headers.header1);
58+
});
59+
60+
61+
it("Returns the expected result when pagination is enabled", async () => {
62+
setStubs({ results: [{ a: 1 }], next_url: "/v2/aggs/ticker/AAPL/range/1/day/2019-01-01/2019-02-02" }, { results: [{ b: 2 }], });
63+
const requestFn = getWithGlobals(mocks.key, mocks.base, {...mocks.globalOptions, pagination: true});
64+
const response = await requestFn("/v2/aggs/ticker/AAPL/range/1/day/2019-01-01/2019-02-01", mocks.query, mocks.overrideOptions);
65+
response.results.length.should.eql(2);
66+
response.results[0].a.should.eql(1);
67+
response.results[1].b.should.eql(2);
68+
fetchStub.callCount.should.eql(2);
69+
getPath(fetchStub.getCalls()[0].args[0]).should.eql(
70+
"/v2/aggs/ticker/AAPL/range/1/day/2019-01-01/2019-02-01"
71+
);
72+
fetchStub.getCalls()[0].args[0].indexOf(mocks.query.query1).should.be.gt(-1);
73+
fetchStub.getCalls()[0].args[1].referrer.should.eql(mocks.overrideOptions.referrer);
74+
fetchStub.getCalls()[0].args[1].headers.header1.should.eql(mocks.globalOptions.headers.header1);
75+
});
76+
});

src/rest/transport/request.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,18 @@ export const getWithGlobals: ICurriedGet = (apiKey, apiBase, globalOptions = {})
103103
}
104104

105105
const json = await response.json();
106-
const newData = allData.concat(json.results);
106+
const newData = json.results instanceof Array ? allData.concat(json.results) : allData;
107107

108-
// check if there is a next page, pagination is enabled, and fetch it recursively
109-
if(globalOptions.pagination && json.next_url) {
108+
if(globalOptions?.pagination && json.next_url) {
109+
// check if there is a next page, pagination is enabled, and fetch it recursively
110110
const nextPath = json.next_url.replace(apiBase, "");
111111
return fetchPage(nextPath, {}, options, newData);
112+
} else if (globalOptions?.pagination) {
113+
// check if there is a next page, pagination is enabled, and fetch it recursively
114+
return { ...json, results: newData, count: newData.length, next_url: null }
112115
} else {
113-
return newData;
116+
// just return the response
117+
return json;
114118
}
115119

116120
} catch (e) {

0 commit comments

Comments
 (0)
0