Skip to content

Commit

Permalink
chore(fetch): convert to typescript; resolveJson; errors index
Browse files Browse the repository at this point in the history
  • Loading branch information
gadicc committed Apr 11, 2021
1 parent e77b557 commit 120fbf2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
12 changes: 12 additions & 0 deletions src/lib/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,15 @@ export class FailedYahooValidationError extends Error {
this.errors = errors;
}
}

interface ErrorsIndex {
[key: string]: any;
}

export default {
BadRequestError,
HTTPError,
InvalidOptionsError,
NoEnvironmentError,
FailedYahooValidationError,
} as ErrorsIndex;
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const _yahooFinanceFetch = require("./yahooFinanceFetch");
const errors = require("./errors");
import _yahooFinanceFetch from "./yahooFinanceFetch";
import errors from "./errors";

const _env = require("../env-node").default;
const _opts = require("./options").default;
import _env from "../env-node";
import _opts from "./options";

describe("yahooFinanceFetch", () => {
const yahooFinanceFetch = _yahooFinanceFetch.bind({ _env, _opts });
Expand All @@ -11,6 +11,7 @@ describe("yahooFinanceFetch", () => {
const fakeConsole = { log: jest.fn(), error: jest.fn(), dir: jest.fn() };
const origConsole = console;

// @ts-ignore
beforeEach(() => (console = fakeConsole));
afterEach(() => (console = origConsole));

Expand All @@ -23,6 +24,7 @@ describe("yahooFinanceFetch", () => {
});

it("throws if no environmennt set", () => {
// @ts-ignore: we're explicityly testing for a bad runtime context
return expect(_yahooFinanceFetch("")).rejects.toBeInstanceOf(
errors.NoEnvironmentError
);
Expand Down
30 changes: 25 additions & 5 deletions src/lib/yahooFinanceFetch.js → src/lib/yahooFinanceFetch.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
const errors = require("./errors");
const pkg = require("../../package.json");
import errors from "./errors";
import pkg from "../../package.json";

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

interface YahooFinanceFetchThisEnv {
[key: string]: any;
URLSearchParams: (init?: any) => any;
fetch: Function;
fetchDevel: Function;
}

interface YahooFinanceFetchThis {
[key: string]: any;
_env: YahooFinanceFetchThisEnv;
_opts: Object;
}

interface YahooFinanceFetchModuleOptions {
devel?: string | boolean;
fetchOptions?: Object;
}

async function yahooFinanceFetch(
urlBase,
this: YahooFinanceFetchThis,
urlBase: string,
params = {},
moduleOpts = {},
moduleOpts: YahooFinanceFetchModuleOptions = {},
func = "json"
) {
if (!this._env)
Expand All @@ -16,6 +35,7 @@ async function yahooFinanceFetch(

const { URLSearchParams, fetch, fetchDevel } = this._env;

// @ts-ignore TODO copy interface? @types lib?
const urlSearchParams = new URLSearchParams(params);
const url = urlBase + "?" + urlSearchParams.toString();

Expand Down Expand Up @@ -69,4 +89,4 @@ async function yahooFinanceFetch(
return result;
}

module.exports = yahooFinanceFetch;
export default yahooFinanceFetch;
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"outDir": "./api",
"allowJs": true,
"preserveConstEnums": true,
"declaration": true
"declaration": true,
"resolveJsonModule": true
},

"include": ["./src/**/*"],
Expand Down

0 comments on commit 120fbf2

Please sign in to comment.