8000 Add Request Configuration Options (#125) · polygon-io/client-js@c77b6a3 · GitHub
[go: up one dir, main page]

Skip to content

Commit c77b6a3

Browse files
Add Request Configuration Options (#125)
* Support request configuration globally or per-call, updated examples, added tests * 7.0.0 * Move apiKey to auth header and update tests, add tests for optional query params, update build command with local tsc, and update readme with example asked in github issues * ES Module Support (#127) * Transpile as ES modules instead of commonjs * Package as an ES Module instead of a CommonJS Module * Add node target to package.json * Update node support to be >= 16 instead of > 16 * Release notes and link to upgrade guide in README * Wording and formatting changes to changelog
1 parent fbfb2b5 commit c77b6a3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+1577
-1207
lines changed

.mocharc.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"$schema": "https://json.schemastore.org/mocharc",
3+
"extension": ["ts"],
4+
"node-option": ["experimental-specifier-resolution=node", "loader=ts-node/esm"],
5+
"spec": ["src/**/*.test.ts"]
6+
}

CHANGELOG.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
## [7.0.0](https://github.com/polygon-io/client-js/v6.2.0...v7.0.0) (2023-01-25)
2+
3+
> Description
4+
5+
### Upgrade Steps
6+
7+
#### Node Version
8+
To use this version in a node project you will need to use node 16 or above. This change was made to support exporting ES Modules instead of CommonJS Modules. The ES Modules exported by this package should be fully supported in Node 16+.
9+
10+
#### Headers and Request Options
11+
The headers parameter has been replaced with a request options parameter which accepts headers. If you were passing headers to an endpoint, your code should change from
12+
13+
```javascript
14+
rest.forex.previousClose("C:EURUSD", {}, myHeadersObject)
15+
```
16+
17+
to
18+
19+
```javascript
20+
rest.forex.previousClose("C:EURUSD", {}, { headers: myHeadersObject })
21+
```
22+
23+
This change was made to allow additional request options to be passed to the request. You can pass any of the [available request options for fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#supplying_request_options) in the request options object, and they will be passed through to the fetch request.
24+
25+
You can also pass request options when initializing the client like:
26+
27+
```javascript
28+
const rest = restClient("API KEY", "https://api.polygon.io", { headers: myHeaders });
29+
```
30+
31+
These will be applied to each request unless overridden by options passed directly to the endpoint function.
32+
33+
You can find more examples in the [configuration examples file](./examples/rest/configuration.js).
34+
35+
### Breaking Changes
36+
* Node 16+ compilation target
37+
* Pass entire request options as optional parameter instead of just headers
38+
39+
### New Features
40+
* Allow request options to be passed globally to the client, or individually to endpoint functions
41+
42+
### Bug Fixes
43+
* Pass api key in header instead of query parameter to improve security
44+
45+
### Other Changes
46+
* Improved test coverage
47+
* Additional examples

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
44

5+
## Upgrading to Version 7
6+
7+
See the [Release Notes](./CHANGELOG.md) for instructions on upgrading to Version 7.
8+
59
## Install
610

711
```bash
@@ -28,12 +32,12 @@ rest.forex
2832

2933
### [REST API](https://polygon.io/docs/stocks/getting-started)
3034

31-
- import all the rest submodule
35+
- import the rest submodule
3236

3337
```typescript
3438
import { restClient } from "@polygon.io/client-js";
3539

36-
const rest = restClient("api key");
40+
const rest = restClient("API KEY");
3741

3842
rest.forex.previousClose("C:EURUSD").then(/* your success handler */);
3943
```
@@ -43,11 +47,13 @@ rest.forex.previousClose("C:EURUSD").then(/* your success handler */);
4347
```typescript
4448
import { referenceClient } from "@polygon.io/client-js";
4549

46-
const reference = referenceClient("api key");
50+
const reference = referenceClient("API KEY");
4751

4852
reference.tickers().then(/* your success handler */);
53+
reference.conditions({ asset_class: 'stocks', data_type: 'trades', sort: 'id' }).then(/* your success handler */)
4954
```
5055

56+
5157
#### [For Launchpad Examples and Usage, see Polygon Launchpad Examples](examples/rest/launchpad/README.md)
5258

5359
### [Websocket](https://polygon.io/docs/stocks/ws_getting-started)
@@ -57,7 +63,7 @@ You can get preauthenticated [websocket clients](https://www.npmjs.com/package/w
5 10000 763
```typescript
5864
import { websocketClient } from "@polygon.io/client-js";
5965

60-
const stocksWS = websocketClient("api key").stocks();
66+
const stocksWS = websocketClient("API KEY").stocks();
6167

6268
stocksWS.onmessage = ({data}) => {
6369
const [message] = JSON.parse(data);

examples/rest/configuration.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const { restClient } = require('@polygon.io/client-js');
2+
3+
// You can pass global options to fetch to add headers or configure requests
4+
const globalFetchOptions = {
5+
method: 'HEAD'
6+
}
7+
const rest = restClient("API KEY", "https://api.polygon.io", globalFetchOptions);
8+
9+
// You can also pass options to each individual call, each key will override keys from the options also set globally
10+
11+
// The signal option configures a timeout of 8 seconds for this call.
12+
// The method option overrides the one we set globally.
13+
const controller = new AbortController();
14+
const id = setTimeout(() => controller.abort(), 8000);
15+
const overrideFetchOptions = {
16+
method: 'GET',
17+
signal: controller.signal
18+
};
19+
rest.forex.previousClose("C:EURUSD", {}, overrideFetchOptions).then((data) => {
20+
clearTimeout(id);
21+
console.log(data);
22+
}).catch(e => {
23+
console.error('An error happened:', e.message);
24+
});

examples/rest/launchpad/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ const edgeHeaders = {
1818
'X-Polygon-Edge-User-Agent': useragent
1919
}
2020

21-
const rest = restClient("api key", "https://api.polygon.io", edgeHeaders);
22-
rest.forex.previousClose("C:EURUSD").then(/* your success handler */);
21+
const rest = restClient("API KEY", "https://api.polygon.io");
22+
rest.forex.previousClose("C:EURUSD", {}, { headers: edgeHeaders }).then(/* your success handler */);
2323

24-
const reference = referenceClient("api key", "https://api.polygon.io", edgeHeaders);
25-
reference.tickers().then(/* your success handler */);
24+
const reference = referenceClient("API KEY", "https://api.polygon.io");
25+
reference.tickers({}, { headers: edgeHeaders }).then(/* your success handler */);
2626
```

examples/rest/launchpad/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ const edgeHeaders = {
1111
'X-Polygon-Edge-User-Agent': 'useragent'
1212
}
1313

14-
const rest = restClient("api key", "https://api.polygon.io", edgeHeaders);
15-
rest.forex.previousClose("C:EURUSD").then(/* your success handler */);
14+
const rest = restClient("API KEY", "https://api.polygon.io");
15+
rest.forex.previousClose("C:EURUSD", {}, { headers: edgeHeaders }).then(/* your success handler */);
1616

17-
const reference = referenceClient("api key", "https://api.polygon.io", edgeHeaders);
18-
reference.tickers().then(/* your success handler */);
17+
const reference = referenceClient("API KEY", "https://api.polygon.io");
18+
reference.tickers({}, { headers: edgeHeaders }).then(/* your success handler */);

package-lock.json

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
{
22
"name": "@polygon.io/client-js",
3-
"version": "6.2.0",
3+
"version": "7.0.0",
44
"description": "Isomorphic Javascript client for Polygon.io Stocks, Forex, and Crypto APIs",
55
"main": "lib/main.js",
66
"types": "lib/main.d.ts",
7+
"type": "module",
78
"scripts": {
8-
"test": "mocha --require ts-node/register '**/*.test.ts'",
9-
"test:watch": "mocha --require ts-node/register --watch '**/*.test.ts'",
9+
"test": "mocha",
10+
"test:watch": "mocha --watch",
1011
"format": "prettier --write '**/*.ts'",
11-
"build": "tsc",
12+
"build": "rm -rf ./lib && ./node_modules/.bin/tsc",
1213
"generate-doc": "typedoc src/main.ts"
1314
},
1415
"repository": {
@@ -76,5 +77,8 @@
7677
"workspaces": [
7778
"./lib/*",
7879
"./sandbox"
79-
]
80+
],
81+
"engines": {
82+
"node": ">=16"
83+
}
8084
}

src/main.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
export * from "./rest";
2-
export * from "./websockets";
1+
export * from "./rest/index.js";
2+
export * from "./websockets/index.js";
33

4-
import restClient, { IRestClient } from "./rest";
5-
import websocketClient, { IWebsocketClient } from "./websockets";
4+
import restClient, { IRestClient } from "./rest/index.js";
5+
import websocketClient, { IWebsocketClient } from "./websockets/index.js";
66

77
export interface IPolygonClient {
88
rest: IRestClient;

src/rest/crypto/aggregates.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
// CF: https://polygon.io/docs/crypto/get_v2_aggs_ticker__cryptoTicker__range__multiplier___timespan___from___to
22

3-
import { get, IHeaders } from "../transport/request";
4-
import { IAggsQuery, IAggs } from "../stocks/aggregates";
3+
import { IGet, IRequestOptions } from "../transport/request.js";
4+
import { IAggsQuery, IAggs } from "../stocks/aggregates.js";
55

66
export const aggregates = async (
7-
apikey: string,
8-
apiBase: string,
7+
get: IGet,
98
ticker: string,
109
multiplier: number,
1110
timespan: string,
1211
from: string,
1312
to: string,
1413
query?: IAggsQuery,
15-
headers?: IHeaders,
14+
options?: IRequestOptions,
1615
): Promise<IAggs> =>
17-
get(
18-
`/v2/aggs/ticker/${ticker}/range/${multiplier}/${timespan}/${from}/${to}`,
19-
apikey,
20-
apiBase,
16+
get(`/v2/aggs/ticker/${ticker}/range/${multiplier}/${timespan}/${from}/${to}`,
2117
query,
22-
headers
18+
options
2319
);

0 commit comments

Comments
 (0)
0