Skip to content

Commit 225926a

Browse files
committed
tests + code to ts
1 parent ff872cc commit 225926a

30 files changed

+327
-185
lines changed

.flowconfig

Lines changed: 0 additions & 11 deletions
This file was deleted.

jest.config.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ module.exports = {
77
name: packageJSON.name,
88
displayName: packageJSON.name,
99
transform: {
10-
'\\.[jt]sx?$': 'babel-jest',
10+
'^.+\\.[jt]sx?$': 'ts-jest',
1111
},
12+
preset: 'ts-jest',
1213
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
1314
testURL: 'http://localhost',
1415
transformIgnorePatterns: ['[/\\\\]node_modules[/\\\\].+\\.(js|jsx)$'],
15-
testMatch: ['**/*.(spec|test).js'],
16+
testMatch: ['**/*.(spec|test).[jt]s'],
1617
collectCoverage: true,
1718
coverageDirectory: './coverage/',
1819
};

package-lock.json

Lines changed: 94 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@
1717
"engines": {
1818
"node": ">=6.0.0"
1919
},
20+
"eslintConfig": {
21+
"extends": [
22+
"plugin:@typescript-eslint/recommended",
23+
"prettier",
24+
"prettier/@typescript-eslint"
25+
],
26+
"parser": "@typescript-eslint/parser",
27+
"parserOptions": {
28+
"project": "./tsconfig.eslint.json"
29+
}
30+
},
2031
"browserify": {
2132
"transform": [
2233
"babelify"
@@ -29,18 +40,17 @@
2940
"postinstall": "npm run download && npm run build",
3041
"start": "node lib/server",
3142
"watch": "babel scripts/watch.js | node",
32-
"test": "npm run lint && npm run check && npm run test:only",
43+
"test": "npm run lint && npm run test:only",
3344
"test:only": "jest",
3445
"lint": "eslint src handler",
3546
"lint:fix": "eslint --fix src handler",
36-
"check": "flow check",
3747
"cover": "babel-node node_modules/.bin/isparta cover --root src --report html node_modules/.bin/_mocha -- $npm_package_options_mocha",
3848
"coveralls": "babel-node node_modules/.bin/isparta cover --root src --report lcovonly node_modules/.bin/_mocha -- $npm_package_options_mocha && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js",
3949
"build": "rimraf lib && babel src --ignore __tests__,public --out-dir lib/ && npm run build:lambda",
4050
"build:lambda": "NODE_ENV=development netlify-lambda build handler",
4151
"download": "babel-node scripts/download.js cache/data.json",
4252
"serve-public": "babel-node scripts/serve-public",
43-
"prettier": "prettier --write 'src/**/*.js'",
53+
"prettier": "prettier --write 'src/**/*.ts'",
4454
"print-schema": "babel-node scripts/print-schema.js",
4555
"store-schema": "babel-node scripts/store-schema.js"
4656
},
@@ -52,10 +62,12 @@
5262
"express": "^4.17.1",
5363
"express-graphql": "^0.9.0",
5464
"graphql": "14.5.8",
55-
"graphql-relay": "0.6.0"
65+
"graphql-relay": "^0.6.0"
5666
},
5767
"devDependencies": {
5868
"@babel/core": "^7.12.3",
69+
"@types/graphql-relay": "^0.6.0",
70+
"@types/jest": "^26.0.15",
5971
"babel-cli": "^6.26.0",
6072
"babel-core": "^6.26.3",
6173
"babel-eslint": "^10.0.3",
@@ -77,6 +89,8 @@
7789
"jest": "^26.6.3",
7890
"netlify-lambda": "^1.6.3",
7991
"prettier": "^1.18.2",
80-
"sane": "^4.1.0"
92+
"sane": "^4.1.0",
93+
"ts-jest": "^26.4.4",
94+
"typescript": "^4.1.2"
8195
}
8296
}

scripts/download.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ async function cacheResources() {
3939
for (const name of resources) {
4040
let url = `https://swapi.dev/api/${name}/`;
4141

42-
while (url != null) {
42+
while (url !== null) {
4343
console.error(url);
4444
const response = await fetch(url, { agent });
4545
const text = await response.text();

scripts/store-schema.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-console */
12
/**
23
* Copyright (c) 2020, GraphQL Contributors
34
* All rights reserved.
@@ -12,7 +13,7 @@ import { printSchema } from 'graphql/utilities';
1213
import { writeFileSync } from 'fs';
1314

1415
try {
15-
var output = printSchema(swapiSchema);
16+
const output = printSchema(swapiSchema);
1617
writeFileSync('schema.graphql', output, 'utf8');
1718
} catch (error) {
1819
console.error(error);
File renamed without changes.
File renamed without changes.

src/api/local.js renamed to src/api/local.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,23 @@
88
* @flow strict
99
*/
1010

11-
import swapiData from '../../cache/data';
11+
import swapiData from '../../cache/data.json';
1212

1313
/**
1414
* Given a URL of an object in the SWAPI, return the data
1515
* from our local cache.
1616
*/
17+
// casting json file to a Record with Index.
18+
const typedSwapiData = swapiData as typeof swapiData & { [key: string]: any }
1719
export async function getFromLocalUrl(
18-
url: string,
19-
): Promise<{ [key: string]: any }> {
20-
const text = swapiData[url];
20+
url: unknown,
21+
): Promise<Record<string, any>> {
22+
23+
if (!(typeof url === 'string')) {
24+
throw new Error('Url provided is not a string');
25+
}
26+
27+
const text = typedSwapiData[url];
2128
if (!text) {
2229
throw new Error(`No entry in local cache for ${url}`);
2330
}

0 commit comments

Comments
 (0)