Skip to content

Commit

Permalink
Use TypeScript; update deps; refactor exports
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
* Add a working TypeDoc configuration file in the event that we want to
  generate documentation
  • Loading branch information
mcmire committed Apr 1, 2022
1 parent 061a7e3 commit af9e092
Show file tree
Hide file tree
Showing 20 changed files with 990 additions and 624 deletions.
39 changes: 38 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,47 @@ module.exports = {
},

{
files: ['*.test.js'],
files: ['*.ts'],
extends: ['@metamask/eslint-config-typescript'],
// TODO: Migrate to shared config
settings: {
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'],
rules: {
'import/unambiguous': 'off',
},
},
],

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
22 changes: 11 additions & 11 deletions jest.config.js → jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*
* For a detailed explanation regarding each configuration property, visit:
* https://jestjs.io/docs/en/configuration.html
* For a detailed explanation regarding each configuration property and type check, visit:
* https://jestjs.io/docs/configuration
*/

module.exports = {
export default {
// All imported modules in your tests should be mocked automatically
// automock: false,

Expand All @@ -13,11 +13,11 @@ module.exports = {
// The directory where Jest should store its cached dependency information
// cacheDirectory: "/private/var/folders/fk/c3y07g0576j8_2s9m01pk4qw0000gn/T/jest_dx",

// Automatically clear mock calls and instances between every test
// Automatically clear mock calls, instances and results before every test
clearMocks: true,

// Indicates whether the coverage information should be collected while executing the test
// collectCoverage: false,
collectCoverage: true,

// An array of glob patterns indicating a set of files for which coverage information should be collected
// collectCoverageFrom: undefined,
Expand Down Expand Up @@ -73,10 +73,10 @@ module.exports = {
// An array of file extensions your modules use
// moduleFileExtensions: [
// "js",
// "json",
// "jsx",
// "ts",
// "tsx",
// "json",
// "node"
// ],

Expand All @@ -93,15 +93,15 @@ 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,

// Use this configuration option to add custom reporters to Jest
// reporters: undefined,

// Automatically reset mock state between every test
// Automatically reset mock state before every test
// resetMocks: false,

// Reset the module registry before running each individual test
Expand All @@ -110,7 +110,7 @@ module.exports = {
// A path to a custom resolver
// resolver: undefined,

// Automatically restore mock state between every test
// Automatically restore mock state and implementation before every test
// restoreMocks: false,

// The root directory that Jest should scan for tests and modules within
Expand All @@ -137,7 +137,7 @@ module.exports = {
// snapshotSerializers: [],

// The test environment that will be used for testing
testEnvironment: 'node',
// testEnvironment: "jest-environment-node",

// Options that will be passed to the testEnvironment
// testEnvironmentOptions: {},
Expand All @@ -163,7 +163,7 @@ module.exports = {
// testResultsProcessor: undefined,

// This option allows use of a custom test runner
// testRunner: "jasmine2",
// testRunner: "jest-circus/runner",

// This option sets the URL for the jsdom environment. It is reflected in properties such as location.href
// testURL: "http://localhost",
Expand Down
30 changes: 21 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
"type": "git",
"url": "https://github.com/MetaMask/eth-json-rpc-infura.git"
},
"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 @@ -23,9 +24,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.1"
},
"devDependencies": {
Expand All @@ -34,8 +35,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 @@ -44,7 +52,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",
"typescript": "~4.4.0"
},
"engines": {
"node": ">=12.0.0"
Expand All @@ -56,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
}
}
}
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

0 comments on commit af9e092

Please sign in to comment.