Skip to content

Commit

Permalink
Use TypeScript; update deps; refactor exports (#58)
Browse files Browse the repository at this point in the history
* **(BREAKING)** Change `index` so that it exports all public modules as
  named exports
* **(BREAKING)** Update all dependencies so that we get TypeScript
  versions
* Convert implementation code to TypeScript
* Convert tests to TypeScript, and add tests to ensure that the
  JavaScript-only checks still work, for as much backward compatibility
  as possible
  • Loading branch information
mcmire authored May 2, 2022
1 parent 715e52e commit ec3e88e
Show file tree
Hide file tree
Showing 16 changed files with 887 additions and 616 deletions.
7 changes: 6 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ module.exports = {
},

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

{
files: ['*.test.ts'],
extends: ['@metamask/eslint-config-jest'],
},
],
Expand Down
17 changes: 10 additions & 7 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,15 +24,13 @@ 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',

// An array of regexp pattern strings used to skip coverage collection
// coveragePathIgnorePatterns: [
// "/node_modules/"
// ],
coveragePathIgnorePatterns: ['/src/types\\.ts$'],

// Indicates which provider should be used to instrument code for coverage
coverageProvider: 'v8',
Expand All @@ -39,10 +39,11 @@ 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,
functions: 30,
functions: 27.27,
lines: 55,
statements: 55,
},
Expand Down Expand Up @@ -97,7 +98,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 +203,5 @@ module.exports = {
// Whether to use watchman for file crawling
// watchman: true,
};

export default config;
29 changes: 20 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.1.0",
"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,10 @@
"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",
"typescript": "~4.4.0"
},
"engines": {
"node": ">=12.0.0"
Expand All @@ -57,8 +68,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
}
}
}
51 changes: 51 additions & 0 deletions src/create-infura-middleware.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { createInfuraMiddleware } from '.';

describe('createInfuraMiddleware', () => {
it('throws when an empty set of options is given', () => {
expect(() => createInfuraMiddleware({} as any)).toThrow(
/Invalid value for 'projectId'/u,
);
});

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

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

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

it('throws when the projectId is not a string', () => {
expect(() => createInfuraMiddleware({ projectId: 42 } as any)).toThrow(
/Invalid value for 'projectId'/u,
);
});

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

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

it('throws when headers is not an object', () => {
expect(() =>
createInfuraMiddleware({ projectId: 'foo', headers: 42 } as any),
).toThrow(/Invalid value for 'headers'/u);
});
});
Loading

0 comments on commit ec3e88e

Please sign in to comment.