Skip to content

Commit

Permalink
feat(env): multiple environments, browser entry point
Browse files Browse the repository at this point in the history
  • Loading branch information
gadicc committed Feb 2, 2021
1 parent 69983a3 commit e18aa67
Show file tree
Hide file tree
Showing 18 changed files with 132 additions and 59 deletions.
4 changes: 2 additions & 2 deletions bin/yahoo-finance.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

const yahooFinance = require('../api/index.js');
const moduleNames = Object.keys(yahooFinance);
const yahooFinance = require('../api/index-node.js').default;
const moduleNames = Object.keys(yahooFinance).filter(n => !n.startsWith('_'));

const node = process.argv[0];
const script = process.argv[1];
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
"name": "yahoo-finance2",
"version": "0.0.1",
"description": "JS API for Yahoo Finance",
"main": "api/index.js",
"types": "api/index.d.ts",
"main": "api/index-node.js",
"types": "api/index-node.d.ts",
"browser": "api/index-browser.js",
"repository": "https://github.com/gadicc/node-yahoo-finance2",
"author": "Gadi Cohen <[email protected]>",
"license": "MIT",
Expand Down
6 changes: 6 additions & 0 deletions src/env-browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @ts-nocheck

export default {
fetch,
URLSearchParams,
}
7 changes: 7 additions & 0 deletions src/env-node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { URLSearchParams } = require('url');
const fetch = require('node-fetch');

export default {
fetch,
URLSearchParams,
}
6 changes: 6 additions & 0 deletions src/index-browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import yahooFinance from './index-common';
import browserEnvironment from './env-browser';

yahooFinance._env = browserEnvironment;

export default yahooFinance;
12 changes: 6 additions & 6 deletions src/index.ts → src/index-common.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// libs
import yahooFinanceFetch from './lib/yahooFinanceFetch';

// modules
import autoc from './modules/autoc';
import historical from './modules/historical';
import quoteSummary from './modules/quoteSummary';
import search from './modules/search';

export default {
autoc,
historical,
quoteSummary,
search,
}
_env: {},
_fetch: yahooFinanceFetch,

export {
autoc,
historical,
quoteSummary,
Expand Down
6 changes: 6 additions & 0 deletions src/index-node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import yahooFinance from './index-common';
import nodeEnvironment from './env-node';

yahooFinance._env = nodeEnvironment;

export default yahooFinance;
5 changes: 5 additions & 0 deletions src/lib/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ class InvalidOptionsError extends Error {
constructor(...args) { super(...args); this.name = 'InvalidOptionsError' }
}

class NoEnvironmentError extends Error {
constructor(...args) { super(...args); this.name = 'NoEnvironmentError' }
}

module.exports = {
BadRequestError,
HTTPError,
FailedYahooValidationError,
InvalidOptionsError,
NoEnvironmentError,
};
14 changes: 8 additions & 6 deletions src/lib/yahooFinanceFetch.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
const { URLSearchParams} = require('url');
const nodeFetch = require('node-fetch');

const errors = require('./errors');
const pkg = require('../../package.json');

const userAgent = `${pkg.name}/${pkg.version} (+${pkg.repository})`;

async function yahooFinanceFetch(urlBase, params={}, fetchOptionsOverrides={}, func='json') {
if (!this._env)
throw new errors.NoEnvironmentError("yahooFinanceFetch called without this._env set");

const { URLSearchParams, fetch } = this._env;

const urlSearchParams = new URLSearchParams(params);
const url = urlBase + '?' + urlSearchParams.toString();

const fetch = fetchOptionsOverrides.devel
const fetchFunc = fetchOptionsOverrides.devel
? require('./fetchDevel')
: nodeFetch;
: fetch;

const fetchOptions = {
"User-Agent": userAgent,
...fetchOptionsOverrides
};

const res = await fetch(url, fetchOptions);
const res = await fetchFunc(url, fetchOptions);
const result = await res[func]();

/*
Expand Down
6 changes: 5 additions & 1 deletion src/lib/yahooFinanceFetch.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
const yahooFinanceFetch = require('./yahooFinanceFetch');
const _yahooFinanceFetch = require('./yahooFinanceFetch');
const errors = require('./errors');

const _env = require('../env-node').default;

describe('yahooFinanceFetch', () => {

const yahooFinanceFetch = _yahooFinanceFetch.bind({ _env });

it('catches errors', () => {
const url = 'https://query2.finance.yahoo.com/v1/finance/search';

Expand Down
11 changes: 10 additions & 1 deletion src/modules/autoc.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import autoc from './autoc';
const { InvalidOptionsError } = require('../lib/errors');

import _env from '../env-node';
import _fetch from '../lib/yahooFinanceFetch';

const yf = {
_env,
_fetch,
autoc
};

describe('autoc', () => {

it('throws InvalidOptions on invalid options', async () => {
const rwo = (options:any) => autoc('symbol', options);
const rwo = (options:any) => yf.autoc('symbol', options);
await expect(rwo({ invalid: true })).rejects.toThrow(InvalidOptionsError)
});

Expand Down
4 changes: 2 additions & 2 deletions src/modules/autoc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import yahooFinanceFetch from '../lib/yahooFinanceFetch';
import validateAndCoerceTypes from '../lib/validateAndCoerceTypes';

const QUERY_URL = 'https://autoc.finance.yahoo.com/autoc';
Expand Down Expand Up @@ -30,6 +29,7 @@ const queryOptionsDefaults = {
};

async function autoc(
this: { [key:string]: any, _fetch: Function },
query: string,
queryOptionsOverrides: AutocOptions = {},
fetchOptions?: object
Expand All @@ -42,7 +42,7 @@ async function autoc(
...queryOptionsOverrides
};

const result = await yahooFinanceFetch(QUERY_URL, queryOptions, fetchOptions);
const result = await this._fetch(QUERY_URL, queryOptions, fetchOptions);

if (result.ResultSet) {
validateAndCoerceTypes(result.ResultSet, QUERY_RESULT_SCHEMA_KEY);
Expand Down
11 changes: 10 additions & 1 deletion src/modules/historical.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import historical from './historical';
const { InvalidOptionsError } = require('../lib/errors');

import _env from '../env-node';
import _fetch from '../lib/yahooFinanceFetch';

const yf = {
_env,
_fetch,
historical
};

describe('historical', () => {

it('throws InvalidOptions on invalid options', async () => {
const rwo = (options:any) => historical('symbol', options);
const rwo = (options:any) => yf.historical('symbol', options);
await expect(rwo({ invalid: true })).rejects.toThrow(InvalidOptionsError)
});

Expand Down
4 changes: 2 additions & 2 deletions src/modules/historical.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import yahooFinanceFetch = require('../lib/yahooFinanceFetch');
import validateAndCoerceTypes from '../lib/validateAndCoerceTypes';
import csv2json from '../lib/csv2json';

Expand Down Expand Up @@ -33,6 +32,7 @@ const queryOptionsDefaults: Omit<HistoricalOptions,'period1'> = {
};

export default async function historical(
this: { [key:string]: any, _fetch: Function },
symbol: string,
queryOptionsOverrides: HistoricalOptions,
fetchOptions?: object
Expand All @@ -57,7 +57,7 @@ export default async function historical(
}

const url = QUERY_URL + '/' + symbol;
const csv = await yahooFinanceFetch(url, queryOptions, fetchOptions, 'text');
const csv = await this._fetch(url, queryOptions, fetchOptions, 'text');
const result = csv2json(csv);

// this can't handle Dates. let's decide where/when/how to validate.
Expand Down
Loading

0 comments on commit e18aa67

Please sign in to comment.