Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(modules): build (true) esm, (interop) cjs modules; tests/readme #144

Merged
merged 32 commits into from
May 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
dec552f
chore(esm): initial esm work
gadicc Apr 22, 2021
d12790e
chore(esm): don't force engines while we still support CJS
gadicc Apr 22, 2021
00113ed
chore(tests): jest.config.js -> .ts, export, add dev ts-node
gadicc Apr 22, 2021
af07200
chore(tests): update jest.config.ts to ignore /dist/ not /api/
gadicc Apr 22, 2021
769f2cb
chore(env): import, not require deps. add @types/node-fetch
gadicc Apr 22, 2021
bc83053
chore(tests): ts-ignore yahooFinanceFetch partial bind
gadicc Apr 22, 2021
f3eb00e
chore(esm): replace require with import, add .js - breaks stuff
gadicc Apr 28, 2021
dc4dfd7
chore(esm): package/jest/tsconfig bump deps, esm opts
gadicc Apr 30, 2021
ba60463
chore(esm): fix manual conflict resolution for import type Optsions
gadicc Apr 30, 2021
cd4364c
chore(esm): more node/jest config opts for esm, setupTests, setGlobal
gadicc Apr 30, 2021
17b0ea1
chore(esm): import jest global in tests
gadicc Apr 30, 2021
b024e27
use relative paths in package.json for main/exports/types/browser
gadicc Apr 30, 2021
6e6de76
eureka
gadicc Apr 30, 2021
046975d
fetchDevel: use URL paths
gadicc Apr 30, 2021
91f1a66
yahooFinanceFetch: this && this._env check
gadicc Apr 30, 2021
735f96d
yahooFinanceFetch: remove console.log
gadicc Apr 30, 2021
46c55a4
historical.spec.ts ts-ignore
gadicc Apr 30, 2021
6ab2846
schema-tweak.js
gadicc Apr 30, 2021
97a0ced
ci fix, update node image for es2020 for dynamic import
gadicc Apr 30, 2021
0dcc41f
yahoo-finance.js bin
gadicc May 5, 2021
de163c0
package.json: add "module" field, expand "exports"
gadicc May 6, 2021
a3ecd3b
build:cjs - change package.json "type"
gadicc May 6, 2021
fd813d9
yarn.lock: recreate to fix missed conflicts in rebase
gadicc May 8, 2021
e6364f4
update schema
gadicc May 8, 2021
8662e4c
readme note
gadicc May 8, 2021
855d1c9
gitignore: add /api temporarily to make switching branches easier
gadicc May 8, 2021
3571f29
more README notes
gadicc May 8, 2021
f1a10b6
sanity tests
gadicc May 8, 2021
49a58e5
test:build
gadicc May 8, 2021
70cc077
ci: test builds
gadicc May 8, 2021
d45ae4d
separate test root for modules
gadicc May 8, 2021
f4872e7
prettier fix
gadicc May 8, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ jobs:
build:
docker:
# https://circleci.com/developer/images/image/cimg/node
- image: cimg/node:12.22.1
#- image: cimg/node:12.22.1
#- currently we need this for dynamic import in module tests, ONLY.
- image: cimg/node:14.16.1

# Specify service dependencies here if necessary
# CircleCI maintains a library of pre-built images
Expand Down Expand Up @@ -52,4 +54,7 @@ jobs:
- codecov/upload:
file: './coverage/coverage-final.json'

- run: yarn build
- run: yarn test:build

- run: yarn semantic-release
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/node_modules
/coverage
/dist
/api
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ $ yahoo-finance search AAPL '{ "someOption": true }'
**Importing**

```js
// import syntax (recommended)
import yahooFinance from 'yahoo-finance2';

// require syntax (if your code base does not support imports)
const yahooFinance = require('yahoo-finance2').default; // NOTE the .default

const results = await yahooFinance.search('AAPL');
const results = await yahooFInance.search('AAPL', { someOption: true, etc });
```
Expand Down Expand Up @@ -81,6 +85,39 @@ const quote = await yahooFinance.quote('AAPL');
const { regularMarketPrice as price, currency } = quote;
```

## NB: CommonJS / ES modules

This package is shipped as **both an ES Module and a CommonJS module**. Node will
*automatically* load the ES module if:

* *Your* `package.json` contains a `{ type: module }` entry
* You're running at least Node 12.
* You `import` the module (`require` function does not exist in ES modules)

otherwise the traditional CommonJS module will be loaded. Note, for ES modules,
and depending on your node version, you may need some extra flags:

```bash
# As environment variables
NODE_OPTIONS="--experimental-vm-modules --experimental-json-modules"

# Or as command line options
node --experimental-vm-modules --experimental-json-modules [...]
```

ES Modules are "relatively" new. They got a big boost in April 2021 when
Node 10, which did not support them, reached end-of-life. However, support
varies by build tool and configuration, and there are some edge cases which
can be tricky. Please open an issue if you run into any trouble.

**require (CommonJS)**

If you use load the library with `require`, make sure to add `.default`:

```js
const yahooFinance = require('yahoo-finance2').default; // NOTE the .default
```

## (Optional) TypeScript Love

Working with `yahoo-finance2` is a joy if you're using TypeScript (but you
Expand Down
4 changes: 2 additions & 2 deletions bin/schema-tweak.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

const fs = require("fs");
const schemaWalker = require("oas-schema-walker");
import fs from "fs";
import schemaWalker from "oas-schema-walker";

const chunks = [];
process.stdin.on("readable", () => {
Expand Down
4 changes: 2 additions & 2 deletions bin/yahoo-finance.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node
#!/usr/bin/env -S node --experimental-json-modules --experimental-vm-modules

const yahooFinance = require("../api/index-node.js").default;
import yahooFinance from "../dist/esm/src/index-node.js";
const moduleNames = Object.keys(yahooFinance).filter((n) => !n.startsWith("_"));

const node = process.argv[0];
Expand Down
12 changes: 0 additions & 12 deletions jest.config.js

This file was deleted.

30 changes: 30 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { Config } from "@jest/types";

const config: Config.InitialOptions = {
preset: "ts-jest/presets/default-esm",
setupFilesAfterEnv: ["<rootDir>/tests/setupTests.js"],
testEnvironment: "node",
testPathIgnorePatterns: [
"/node_modules/",
"/dist/",
"/api/",
"/tests-modules/",
],
extensionsToTreatAsEsm: [".ts"],
globals: {
"ts-jest": {
useESM: true,
},
},
moduleNameMapper: {
"(.*)\\.js$": "$1",
},
/*
reporters: [
'<rootDir>/tests/reporter.js',
'<rootDir>/tests/summary-reporter.js',
],
*/
};

export default config;
38 changes: 29 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@
"name": "yahoo-finance2",
"version": "0.0.1",
"description": "JS API for Yahoo Finance",
"main": "api/index-node.js",
"types": "api/index-node.d.ts",
"browser": "api/index-browser.js",
"type": "module",
"module": "./dist/esm/src/index-node.js",
"main": "./dist/cjs/src/index-node.js",
"exports": {
"import": "./dist/esm/src/index-node.js",
"default": "./dist/cjs/src/index-node.js"
},
"types": "./dist/esm/src/index-node.d.ts",
"browser": "./dist/esm/src/index-browser.js",
"repository": "https://github.com/gadicc/node-yahoo-finance2",
"author": "Gadi Cohen <[email protected]>",
"license": "MIT",
Expand All @@ -22,20 +28,32 @@
"client",
"library"
],
"//engines//UNCOMMENT_WHEN_WE_DROP_CJS_SUPPORT": {
"node": ">=12.17.0",
"//node2": "we need 14.x for dynamic imports -- JUST FOR FETCH_DEVEL()! think about this.",
"node2": ">=14.0.0"
},
"bin": {
"yahoo-finance": "bin/yahoo-finance.js"
},
"scripts": {
"coverage": "jest --coverage",
"lint": "eslint . --ext .js,.ts",
"schema": "ts-json-schema-generator -f tsconfig.json -p 'src/{modules,typings}/**/*.ts' -t '*' | node bin/schema-tweak.js > schema.json",
"build": "yarn run build:esm && yarn run build:cjs",
"build:esm": "tsc --module es2020 --outDir dist/esm",
"build:cjs": "tsc --module commonjs --outDir dist/cjs && sed -i 's/\"type\": \"module\",/\"type:\": \"commonjs\",/' dist/cjs/package.json",
"generateSchema": "yarn schema",
"prepublishOnly": "tsc && yarn generateSchema",
"test": "jest",
"test:ts": "tsc --noEmit"
"prepublishOnly": "yarn build && yarn generateSchema",
"test": "node --experimental-vm-modules node_modules/.bin/jest",
"test:ts": "tsc --noEmit",
"test:esm": "node --experimental-vm-modules node_modules/.bin/jest -c tests-modules/esm/jest.config.js tests-modules/esm/tests/*",
"test:cjs": "node node_modules/.bin/jest -c tests-modules/cjs/jest.config.js tests-modules/cjs/tests/*",
"test:modules": "yarn test:esm && yarn test:cjs",
"test:build": "yarn test:modules"
},
"files": [
"api",
"dist",
"schema.json"
],
"dependencies": {
Expand All @@ -52,18 +70,20 @@
"@semantic-release/release-notes-generator": "9.0.2",
"@tsconfig/node12": "1.0.7",
"@types/jest": "26.0.23",
"@types/node-fetch": "^2.5.10",
"@typescript-eslint/eslint-plugin": "4.22.1",
"@typescript-eslint/parser": "4.22.1",
"eslint": "7.26.0",
"eslint-config-prettier": "8.3.0",
"globby": "11.0.3",
"jest": "26.6.3",
"jest": "v27.0.0-next.8",
"jest-tobetype": "1.2.3",
"oas-schema-walker": "1.1.5",
"prettier": "2.2.1",
"semantic-release": "17.4.2",
"ts-jest": "26.5.6",
"ts-jest": "27.0.0-next.11",
"ts-json-schema-generator": "0.92.0",
"ts-node": "^9.1.1",
"typescript": "4.2.4"
}
}
78 changes: 73 additions & 5 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@
"$schema": "http://json-schema.org/draft-07/schema#",
"$comment": "DO NOT EDIT THIS FILE. It is generated automatically from typescript interfaces in the project. To update, run `yarn schema`.",
"definitions": {
"*": {
"additionalProperties": false,
"properties": {
"moduleOptions": {
"$ref": "#/definitions/ModuleOptions"
},
"query": {
"type": "string"
},
"queryOptionsOverrides": {
"$ref": "#/definitions/AutocOptions"
},
"this": {
"$ref": "#/definitions/ModuleThis"
}
},
"required": [
"this",
"query"
],
"type": "object"
},
"Action": {
"enum": [
"down",
Expand Down Expand Up @@ -1992,6 +2014,52 @@
],
"type": "object"
},
"ModuleOptions": {
"additionalProperties": false,
"properties": {
"devel": {
"type": [
"boolean",
"string"
]
},
"fetchOptions": {
"type": "object"
},
"validateResult": {
"type": "boolean"
}
},
"type": "object"
},
"ModuleThis": {
"properties": {
"_moduleExec": {
"additionalProperties": false,
"properties": {
"arguments": {},
"caller": {
"$ref": "#/definitions/interface-731470504-9814-11278-731470504-0-212312"
},
"length": {
"yahooFinanceType": "number"
},
"prototype": {}
},
"required": [
"prototype",
"length",
"arguments",
"caller"
],
"type": "object"
}
},
"required": [
"_moduleExec"
],
"type": "object"
},
"NetSharePurchaseActivity": {
"additionalProperties": false,
"properties": {
Expand Down Expand Up @@ -2416,7 +2484,7 @@
"properties": {
"arguments": {},
"caller": {
"$ref": "#/definitions/interface-2073358172-9814-11278-2073358172-0-212312"
"$ref": "#/definitions/interface-731470504-9814-11278-731470504-0-212312"
},
"length": {
"yahooFinanceType": "number"
Expand All @@ -2436,7 +2504,7 @@
"properties": {
"arguments": {},
"caller": {
"$ref": "#/definitions/interface-2073358172-9814-11278-2073358172-0-212312"
"$ref": "#/definitions/interface-731470504-9814-11278-731470504-0-212312"
},
"length": {
"yahooFinanceType": "number"
Expand All @@ -2456,7 +2524,7 @@
"properties": {
"arguments": {},
"caller": {
"$ref": "#/definitions/interface-2073358172-9814-11278-2073358172-0-212312"
"$ref": "#/definitions/interface-731470504-9814-11278-731470504-0-212312"
},
"length": {
"yahooFinanceType": "number"
Expand Down Expand Up @@ -6224,12 +6292,12 @@
],
"type": "object"
},
"interface-2073358172-9814-11278-2073358172-0-212312": {
"interface-731470504-9814-11278-731470504-0-212312": {
"additionalProperties": false,
"properties": {
"arguments": {},
"caller": {
"$ref": "#/definitions/interface-2073358172-9814-11278-2073358172-0-212312"
"$ref": "#/definitions/interface-731470504-9814-11278-731470504-0-212312"
},
"length": {
"yahooFinanceType": "number"
Expand Down
11 changes: 6 additions & 5 deletions src/env-node.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const { URLSearchParams } = require("url");
const fetch = require("node-fetch");
import { URLSearchParams } from "url";
import fetch from "node-fetch";

function fetchDevel() {
// This let's us still only require the file if we need it, at runtime.
return require("./lib/fetchDevel");
// This let's us still only require the file if we need it, at runtime.
async function fetchDevel() {
const module = await import("./lib/fetchDevel.js");
return module.default;
}

export default {
Expand Down
4 changes: 2 additions & 2 deletions src/index-browser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import yahooFinance from "./index-common";
import browserEnvironment from "./env-browser";
import yahooFinance from "./index-common.js";
import browserEnvironment from "./env-browser.js";

yahooFinance._env = browserEnvironment;

Expand Down
24 changes: 12 additions & 12 deletions src/index-common.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
// libs
import yahooFinanceFetch from "./lib/yahooFinanceFetch";
import moduleExec from "./lib/moduleExec";
import options from "./lib/options";
import yahooFinanceFetch from "./lib/yahooFinanceFetch.js";
import moduleExec from "./lib/moduleExec.js";
import options from "./lib/options.js";

// modules
import autoc from "./modules/autoc";
import historical from "./modules/historical";
import quote from "./modules/quote";
import quoteSummary from "./modules/quoteSummary";
import search from "./modules/search";
import recommendationsBySymbol from "./modules/recommendationsBySymbol";
import trendingSymbols from "./modules/trendingSymbols";
import optionsModule from "./modules/options";
import autoc from "./modules/autoc.js";
import historical from "./modules/historical.js";
import quote from "./modules/quote.js";
import quoteSummary from "./modules/quoteSummary.js";
import search from "./modules/search.js";
import recommendationsBySymbol from "./modules/recommendationsBySymbol.js";
import trendingSymbols from "./modules/trendingSymbols.js";
import optionsModule from "./modules/options.js";

// other
import quoteCombine from "./other/quoteCombine";
import quoteCombine from "./other/quoteCombine.js";

export default {
// internal
Expand Down
Loading