Skip to content

Use TypeScript; update deps; refactor exports #58

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

Merged
merged 16 commits into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
34 changes: 33 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,42 @@ module.exports = {
},

{
files: ['*.test.js'],
files: ['*.ts'],
extends: ['@metamask/eslint-config-typescript'],
// TODO: Migrate to our shared config
settings: {
'import/resolver': {
// Try resolving imports using TypeScript's module resolution
// algorithm, then Node's
typescript: {
alwaysTryTypes: true,
},
node: {},
},
jsdoc: {
mode: 'typescript',
},
},
},

{
files: ['*.test.ts', '*.test.js'],
extends: ['@metamask/eslint-config-jest'],
},
],

ignorePatterns: ['!.eslintrc.js', '!.prettierrc.js', 'dist/'],

settings: {
'import/resolver': {
// Try resolving imports using Node's module resolution algorithm,
// then TypeScript's — this allows us to import TypeScript files in
// JavaScript files (e.g. in tests that exercise a TypeScript module where
// we need to pretend we're in a JavaScript-only environment)
node: {},
typescript: {
alwaysTryTypes: true,
},
},
},
};
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ node_modules/
# TypeScript cache
*.tsbuildinfo

# TypeDoc docs
typedocs/

# Optional npm cache directory
.npm

Expand Down
11 changes: 8 additions & 3 deletions jest.config.js → jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
* https://jestjs.io/docs/configuration
*/

module.exports = {
import type { Config } from '@jest/types';

const config: Config.InitialOptions = {
// All imported modules in your tests should be mocked automatically
// automock: false,

Expand All @@ -22,7 +24,7 @@ module.exports = {
collectCoverage: true,

// An array of glob patterns indicating a set of files for which coverage information should be collected
collectCoverageFrom: ['./src/**/*.js'],
collectCoverageFrom: ['./src/**/*.ts'],

// The directory where Jest should output its coverage files
coverageDirectory: 'coverage',
Expand All @@ -39,6 +41,7 @@ module.exports = {
coverageReporters: ['text', 'html'],

// An object that configures minimum threshold enforcement for coverage results
// TODO: Fix lack of coverage and restore to 100%
coverageThreshold: {
global: {
branches: 80,
Expand Down Expand Up @@ -97,7 +100,7 @@ module.exports = {
// notifyMode: "failure-change",

// A preset that is used as a base for Jest's configuration
// preset: undefined,
preset: 'ts-jest',

// Run tests from one or more projects
// projects: undefined,
Expand Down Expand Up @@ -202,3 +205,5 @@ module.exports = {
// Whether to use watchman for file crawling
// watchman: true,
};

export default config;
30 changes: 21 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
"url": "https://github.com/MetaMask/eth-json-rpc-infura.git"
},
"license": "ISC",
"main": "src/index.js",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"src/*.js"
"dist/"
],
"scripts": {
"build": "echo 'nothing to build yet'",
"build": "tsc --project tsconfig.build.json",
"build:clean": "rimraf dist && yarn build",
"lint": "yarn lint:eslint && yarn lint:misc --check",
"lint:eslint": "eslint . --cache --ext js,ts",
Expand All @@ -24,9 +25,9 @@
"test:watch": "jest --watch"
},
"dependencies": {
"eth-json-rpc-middleware": "^6.0.0",
"eth-rpc-errors": "^3.0.0",
"json-rpc-engine": "^5.3.0",
"eth-json-rpc-middleware": "^8.0.1",
"eth-rpc-errors": "^4.0.3",
"json-rpc-engine": "^6.1.0",
"node-fetch": "^2.6.7"
},
"devDependencies": {
Expand All @@ -35,8 +36,15 @@
"@metamask/eslint-config": "^9.0.0",
"@metamask/eslint-config-jest": "^9.0.0",
"@metamask/eslint-config-nodejs": "^9.0.0",
"@metamask/eslint-config-typescript": "^9.0.1",
"@types/jest": "^26.0.13",
"@types/node": "^17.0.23",
"@types/node-fetch": "^2.6.1",
"@typescript-eslint/eslint-plugin": "^4.21.0",
"@typescript-eslint/parser": "^4.21.0",
"eslint": "^7.23.0",
"eslint-config-prettier": "^8.5.0",
"eslint-import-resolver-typescript": "^2.7.0",
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-jest": "^24.3.4",
"eslint-plugin-jsdoc": "^36.1.0",
Expand All @@ -45,7 +53,11 @@
"jest": "^26.4.2",
"prettier": "^2.6.1",
"prettier-plugin-packagejson": "^2.2.17",
"rimraf": "^3.0.2"
"rimraf": "^3.0.2",
"ts-jest": "^26.3.0",
"ts-node": "^10.7.0",
"typedoc": "^0.22.13",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leftover from when typedoc was extracted from this PR I assume

Suggested change
"typedoc": "^0.22.13",

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in e7617b4.

"typescript": "~4.4.0"
},
"engines": {
"node": ">=12.0.0"
Expand All @@ -57,8 +69,8 @@
"lavamoat": {
"allowScripts": {
"@lavamoat/preinstall-always-fail": false,
"eth-json-rpc-middleware>ethereumjs-util>ethereum-cryptography>keccak": true,
"eth-json-rpc-middleware>ethereumjs-util>ethereum-cryptography>secp256k1": true
"eth-json-rpc-middleware>eth-sig-util>ethereumjs-util>ethereum-cryptography>keccak": true,
"eth-json-rpc-middleware>eth-sig-util>ethereumjs-util>ethereum-cryptography>secp256k1": true
}
}
}
39 changes: 39 additions & 0 deletions src/createInfuraMiddleware.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const { createInfuraMiddleware } = require('.');

describe('createInfuraMiddleware (JS-only tests)', () => {
it('throws when the projectId is a number', () => {
expect(() => createInfuraMiddleware({ projectId: 42 })).toThrow(
/Invalid value for 'projectId'/u,
);
});

it('throws when the projectId is undefined', () => {
expect(() => createInfuraMiddleware({ projectId: undefined })).toThrow(
/Invalid value for 'projectId'/u,
);
});

it('throws when the projectId is null', () => {
expect(() => createInfuraMiddleware({ projectId: null })).toThrow(
/Invalid value for 'projectId'/u,
);
});

it('throws when headers is null', () => {
expect(() =>
createInfuraMiddleware({ projectId: 'foo', headers: null }),
).toThrow(/Invalid value for 'headers'/u);
});

it('throws when headers is a number', () => {
expect(() =>
createInfuraMiddleware({ projectId: 'foo', headers: 42 }),
).toThrow(/Invalid value for 'headers'/u);
});

it('throws when headers is an empty string', () => {
expect(() =>
createInfuraMiddleware({ projectId: 'foo', headers: '' }),
).toThrow(/Invalid value for 'headers'/u);
});
});
9 changes: 9 additions & 0 deletions src/createInfuraMiddleware.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { createInfuraMiddleware } from '.';

describe('createInfuraMiddleware', () => {
it('throws when the projectId is an empty string', () => {
expect(() => createInfuraMiddleware({ projectId: '' })).toThrow(
/Invalid value for 'projectId'/u,
);
});
});
Loading