Skip to content

Commit

Permalink
chore: change to typescript with babel
Browse files Browse the repository at this point in the history
  • Loading branch information
huchenme committed Jun 10, 2019
1 parent 5b27704 commit 9101f9c
Show file tree
Hide file tree
Showing 15 changed files with 489 additions and 187 deletions.
14 changes: 0 additions & 14 deletions .babelrc

This file was deleted.

24 changes: 22 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
},
extends: [
require.resolve('eslint-config-kentcdodds'),
require.resolve('eslint-config-kentcdodds/jest'),
'plugin:@typescript-eslint/recommended',
'eslint-config-kentcdodds',
'eslint-config-kentcdodds/jest',
'prettier/@typescript-eslint',
],
settings: {
'import/resolver': {
node: {
extensions: ['.js', '.jsx', '.ts', '.tsx'],
},
},
},
rules: {
'no-console': 'off',
'consistent-return': 'off',
},
overrides: {
files: ['**/__tests__/**/*.ts'],
rules: {
'@typescript-eslint/explicit-function-return-type': 'off',
},
},
};
20 changes: 20 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module.exports = {
presets: [
[
'@babel/preset-env',
{
targets: {
node: '8',
browsers: '> 0.25%, not dead',
},
},
],
'@babel/preset-typescript',
],
plugins: [
'lodash',
'@babel/plugin-transform-runtime',
'@babel/proposal-class-properties',
'@babel/proposal-object-rest-spread',
],
};
29 changes: 23 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "Get GitHub trending data",
"main": "dist/github-trending.cjs.js",
"module": "dist/github-trending.esm.js",
"types": "dist/index.d.ts",
"engines": {
"node": ">=8.0.0"
},
Expand All @@ -26,6 +27,8 @@
"homepage": "https://github.com/huchenme/github-trending-api#readme",
"dependencies": {
"@babel/runtime": "^7.4.5",
"@typescript-eslint/eslint-plugin": "^1.10.2",
"@typescript-eslint/parser": "^1.10.2",
"cheerio": "^1.0.0-rc.3",
"cors": "^2.8.5",
"express": "^4.17.1",
Expand All @@ -37,9 +40,17 @@
"devDependencies": {
"@babel/core": "^7.4.5",
"@babel/node": "^7.4.5",
"@babel/plugin-proposal-class-properties": "^7.4.4",
"@babel/plugin-proposal-object-rest-spread": "^7.4.4",
"@babel/plugin-transform-runtime": "^7.4.4",
"@babel/preset-env": "^7.4.5",
"babel-core": "^7.0.0-0",
"@babel/preset-typescript": "^7.3.3",
"@types/cors": "^2.8.5",
"@types/express": "^4.17.0",
"@types/jest": "^24.0.13",
"@types/lodash": "^4.14.134",
"@types/memory-cache": "^0.2.0",
"@types/node-fetch": "^2.3.5",
"babel-jest": "^24.8.0",
"babel-plugin-lodash": "^3.3.4",
"doctoc": "^1.4.0",
Expand All @@ -54,17 +65,23 @@
"rimraf": "^2.6.3",
"rollup": "^1.14.6",
"rollup-plugin-babel": "^4.3.2",
"rollup-plugin-node-resolve": "^5.0.1",
"rollup-plugin-terser": "^5.0.0",
"semantic-release": "^15.13.12",
"travis-deploy-once": "^5.0.11"
"travis-deploy-once": "^5.0.11",
"typescript": "^3.5.1"
},
"scripts": {
"dev": "nodemon --exec babel-node ./src/server.js",
"dev": "nodemon --exec \"babel-node --extensions .ts ./src/server.ts\"",
"test": "jest --coverage",
"lint": "eslint ./src",
"validate": "npm-run-all lint test",
"eslint": "eslint . --ext .ts,.js",
"type-check": "tsc --noEmit",
"type-check:watch": "npm run type-check -- --watch",
"validate": "npm-run-all eslint type-check test",
"prebuild": "rimraf dist",
"build": "NODE_ENV=production rollup -c",
"build": "npm-run-all build:*",
"build:types": "tsc --emitDeclarationOnly",
"build:js": "NODE_ENV=production rollup -c",
"semantic-release": "semantic-release",
"travis-deploy-once": "travis-deploy-once",
"start": "node ./dist/server.cjs.js"
Expand Down
35 changes: 20 additions & 15 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
import path from 'path';
import babel from 'rollup-plugin-babel';
import { terser } from 'rollup-plugin-terser';
// eslint-disable-next-line import/extensions
import resolve from 'rollup-plugin-node-resolve';
import pkg from './package.json';

const external = id => !id.startsWith('.') && !path.isAbsolute(id);

const extensions = ['.js', '.jsx', '.ts', '.tsx'];

const resolvePlugin = resolve({
extensions,
});

const babelPlugin = babel({
exclude: 'node_modules/**',
runtimeHelpers: true,
extensions,
});

export default [
{
input: 'src/index.js',
external: id => external(id),
input: 'src/index.ts',
external,
output: [
{ file: pkg.main, format: 'cjs' },
{ file: pkg.module, format: 'es' },
],
plugins: [
babel({
exclude: 'node_modules/**',
runtimeHelpers: true,
}),
resolvePlugin,
babelPlugin,
process.env.NODE_ENV === 'production' && terser(),
],
},
{
input: 'src/server.js',
external: id => external(id),
input: 'src/server.ts',
external,
output: [{ file: 'dist/server.cjs.js', format: 'cjs' }],
plugins: [
babel({
exclude: 'node_modules/**',
runtimeHelpers: true,
}),
],
plugins: [resolvePlugin, babelPlugin],
},
];
File renamed without changes.
8 changes: 5 additions & 3 deletions src/__tests__/fetch.js → src/__tests__/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import {

jest.mock('node-fetch');

const mockedFetch = (fetch as unknown) as jest.Mock;

describe('fetchAllLanguages()', () => {
fetch.mockReturnValueOnce({
mockedFetch.mockReturnValueOnce({
text: jest.fn().mockReturnValue(mockRepositories),
});

Expand All @@ -20,7 +22,7 @@ describe('fetchAllLanguages()', () => {
});

describe('fetchRepositories()', () => {
fetch.mockReturnValueOnce({
mockedFetch.mockReturnValueOnce({
text: jest.fn().mockReturnValue(mockRepositories),
});

Expand All @@ -30,7 +32,7 @@ describe('fetchRepositories()', () => {
});

describe('fetchDevelopers()', () => {
fetch.mockReturnValueOnce({
mockedFetch.mockReturnValueOnce({
text: jest.fn().mockReturnValue(mockDevelopers),
});

Expand Down
8 changes: 4 additions & 4 deletions src/fetch.js → src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function omitNil(object) {
return omitBy(object, isNil);
}

function removeDefaultAvatarSize(src) {
function removeDefaultAvatarSize(src?: string): string {
/* istanbul ignore if */
if (!src) {
return src;
Expand Down Expand Up @@ -131,19 +131,19 @@ export async function fetchRepositories({
$repo
.find(`[href="${relativeUrl}/stargazers"]`)
.text()
.replace(',', '') || /* istanbul ignore next */ 0,
.replace(',', '') || /* istanbul ignore next */ '0',
10
),
forks: parseInt(
$repo
.find(`[href="${relativeUrl}/network/members"]`)
.text()
.replace(',', '') || /* istanbul ignore next */ 0,
.replace(',', '') || /* istanbul ignore next */ '0',
10
),
currentPeriodStars: parseInt(
currentPeriodStarsString.split(' ')[0].replace(',', '') ||
/* istanbul ignore next */ 0,
/* istanbul ignore next */ '0',
10
),
builtBy,
Expand Down
50 changes: 23 additions & 27 deletions src/index.js → src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,56 @@ import { sample, sampleSize } from 'lodash';

const SERVER_URL = 'https://github-trending-api.now.sh';

function buildUrl(baseUrl, params = {}) {
function buildUrl(baseUrl, params = {}): string {
const queryString = Object.keys(params)
.filter(key => params[key])
.map(key => {
return `${key}=${params[key]}`;
})
.filter((key): string | undefined => params[key])
.map((key): string => `${key}=${params[key]}`)
.join('&');

return queryString === '' ? baseUrl : `${baseUrl}?${queryString}`;
}

export async function fetchAllLanguages() {
export async function fetchAllLanguages(): Promise<any> {
const res = await fetch(`${SERVER_URL}/languages`);
if (res.ok) {
return res.json();
} else {
if (!res.ok) {
throw new Error('Something went wrong');
}
return res.json();
}

export async function fetchRepositories(params) {
export async function fetchRepositories(params): Promise<any> {
const res = await fetch(buildUrl(`${SERVER_URL}/repositories`, params));
if (res.ok) {
return res.json();
} else {
if (!res.ok) {
throw new Error('Something went wrong');
}
return res.json();
}

export async function fetchDevelopers(params) {
export async function fetchDevelopers(params): Promise<any> {
const res = await fetch(buildUrl(`${SERVER_URL}/developers`, params));
if (res.ok) {
return res.json();
} else {
if (!res.ok) {
throw new Error('Something went wrong');
}
return res.json();
}

export async function fetchRandomRepository(params) {
export async function fetchRandomRepository(params): Promise<any> {
const res = await fetch(buildUrl(`${SERVER_URL}/repositories`, params));
if (res.ok) {
const json = res.json();
return sample(json);
} else {
if (!res.ok) {
throw new Error('Something went wrong');
}
const json = res.json();
return sample(json);
}

export async function fetchRandomRepositories(size = 1, params) {
export async function fetchRandomRepositories(
size: number = 1,
params
): Promise<any> {
const res = await fetch(buildUrl(`${SERVER_URL}/repositories`, params));
if (res.ok) {
const json = res.json();
return sampleSize(json, size);
} else {
if (!res.ok) {
throw new Error('Something went wrong');
}
const json = res.json();
return sampleSize(json, size);
}
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 9101f9c

Please sign in to comment.