Skip to content

Commit a015b75

Browse files
borodayevValeriy Borodayev
and
Valeriy Borodayev
authored
Migrating codebase to ts (#40)
* refactor: moving to ts, wip resolving errors * feat: resolve all ts errors * refactor: fix test cases, remove babel * build: set up tsc compiler properly * refactor: fix eslint issue * chore(package.json): update keywords Co-authored-by: Valeriy Borodayev <[email protected]>
1 parent 5ed1c08 commit a015b75

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1046
-10865
lines changed

.babelrc.js

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

.eslintignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
lib/*
2-
flow-typed

.eslintrc.js

Lines changed: 24 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,29 @@
11
module.exports = {
2-
"extends": ["airbnb-base", "prettier"],
3-
"parser": "babel-eslint",
4-
"rules": {
5-
"no-underscore-dangle": 0,
6-
"arrow-body-style": 0,
7-
"no-unused-expressions": 0,
8-
"no-plusplus": 0,
9-
"lines-between-class-members": 0,
10-
"import/no-extraneous-dependencies": [
11-
"error",
12-
{
13-
"devDependencies": [
14-
"**/*-test.js",
15-
"**/__mocks__/**",
16-
"**/__fixtures__/**",
17-
"**/demo/**"
18-
]
19-
}
20-
],
21-
"no-prototype-builtins": 0,
22-
"no-restricted-syntax": 0,
23-
"no-mixed-operators": 0,
24-
"comma-dangle": [
25-
"error",
26-
{
27-
"arrays": "always-multiline",
28-
"objects": "always-multiline",
29-
"imports": "always-multiline",
30-
"exports": "always-multiline",
31-
"functions": "ignore"
32-
}
33-
],
34-
"prettier/prettier": [
35-
"error",
36-
{
37-
"printWidth": 100,
38-
"singleQuote": true,
39-
"trailingComma": "es5"
40-
}
41-
],
42-
"import/prefer-default-export": 0,
43-
"arrow-parens": 0,
44-
"prefer-destructuring": 0,
45-
"no-use-before-define": 0
2+
root: true,
3+
parser: '@typescript-eslint/parser',
4+
parserOptions: {
5+
project: ['./tsconfig.eslint.json'],
466
},
47-
"env": {
48-
"jasmine": true,
49-
"jest": true
7+
extends: [
8+
'airbnb-typescript/base',
9+
'prettier/@typescript-eslint',
10+
'plugin:import/typescript',
11+
'plugin:jest/all',
12+
'plugin:prettier/recommended',
13+
],
14+
plugins: [
15+
'@typescript-eslint',
16+
'jest',
17+
'prettier',
18+
],
19+
env: {
20+
'jest/globals': true,
21+
'jasmine': true,
22+
'jest': true
5023
},
51-
"plugins": ["flowtype", "prettier"],
52-
"globals": {
53-
"Class": true,
54-
"Iterator": true,
55-
"$Shape": true,
56-
"$Keys": true,
57-
"$FlowFixMe": true,
58-
"fetch": true
24+
rules: {
25+
'prettier/prettier': ['error', { 'singleQuote': true }],
26+
'lines-between-class-members': 0,
27+
'no-underscore-dangle': 0,
5928
}
6029
}

.flowconfig

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

__fixtures__/Post.js renamed to __fixtures__/Post.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// @flow
22
/* eslint-disable no-param-reassign, func-names */
33

4-
import mongoose, { type MongooseSchema } from 'mongoose';
5-
import DiffPlugin from '../src/index';
4+
import mongoose, { Schema, Document, Model } from 'mongoose';
5+
import DiffPlugin, { IDiffModel } from '../src/index';
66
import DB from './db';
77

88
DB.init();
99

10-
export const PostSchema: MongooseSchema<PostDoc> = new mongoose.Schema(
10+
export const PostSchema: Schema<IPostDoc> = new mongoose.Schema(
1111
{
1212
title: {
1313
type: String,
@@ -34,15 +34,15 @@ export const PostSchema: MongooseSchema<PostDoc> = new mongoose.Schema(
3434
}
3535
);
3636

37-
export class PostDoc /* :: extends Mongoose$Document */ {
37+
export interface IPostDoc extends Document {
3838
title: string;
3939
subjects: Array<{ name: string }>;
40+
}
4041

41-
// TODO: find out solution for flow to use `DiffModelT` instead of `any`
42-
/* :: static diffModel(): any {} */
42+
interface IPostModel extends Model<IPostDoc> {
43+
diffModel(): IDiffModel;
4344
}
4445

4546
PostSchema.plugin(DiffPlugin);
46-
PostSchema.loadClass(PostDoc);
4747

48-
export const Post = DB.data.model('Post', PostSchema);
48+
export const Post = DB.data.model<IPostDoc, IPostModel>('Post', PostSchema);

__fixtures__/__mocks__/db.js renamed to __fixtures__/__mocks__/db.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1+
/* eslint-disable no-param-reassign, jest/require-top-level-describe, jest/no-hooks */
12
/* eslint-disable no-param-reassign */
2-
/* @flow */
33

4-
import MongoMemoryServer from 'mongodb-memory-server';
4+
import * as m from 'mongodb-memory-server';
55

6-
// $FlowFixMe
76
const DB = require.requireActual('../db').default;
87

9-
const mongod = new MongoMemoryServer({
10-
// debug: true,
11-
});
8+
const mongod = new m.MongoMemoryServer({});
129

13-
DB.autoIndexOnceInDev = opts => {
10+
DB.autoIndexOnceInDev = (opts: any) => {
1411
opts.config.autoIndex = false;
1512
};
1613
DB.consoleErr = () => {};

__fixtures__/db.js renamed to __fixtures__/db.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
// @flow
21
/* eslint-disable no-console */
32

4-
import mongoose from 'mongoose';
5-
import type { MongooseConnection } from 'mongoose';
3+
import mongoose, { Connection } from 'mongoose';
64

75
mongoose.Promise = global.Promise;
86

@@ -14,7 +12,7 @@ export default class DB {
1412
static consoleLog = console.log;
1513
static _connectionStr: string;
1614

17-
static data: MongooseConnection = mongoose.createConnection();
15+
static data: Connection = mongoose.createConnection();
1816

1917
static init(connectionStr?: string) {
2018
if (connectionStr) this._connectionStr = connectionStr;
@@ -25,24 +23,23 @@ export default class DB {
2523
return Promise.all([DB.closeDB('data')]);
2624
}
2725

28-
static openDB(name: DBNames = 'data'): Promise<MongooseConnection> {
26+
static openDB(name: DBNames = 'data'): Promise<Connection> {
2927
return new Promise((resolve, reject) => {
3028
const uri = process.env.MONGO_CONNECTION_STRING || this._connectionStr;
31-
const opts = {};
29+
const opts: any = {};
3230

3331
opts.promiseLibrary = global.Promise;
3432
opts.autoReconnect = true;
3533
opts.reconnectTries = Number.MAX_VALUE;
3634
opts.reconnectInterval = 1000;
3735
opts.useNewUrlParser = true;
3836

39-
// $FlowFixMe
4037
const db: any = DB[name];
4138

4239
db.consoleErr = DB.consoleErr;
4340
db.consoleLog = DB.consoleLog;
4441

45-
db.on('error', e => {
42+
db.on('error', (e: any) => {
4643
if (e.message.code === 'ETIMEDOUT') {
4744
db.consoleErr(Date.now(), e);
4845
db.connect(uri, opts);
@@ -64,7 +61,6 @@ export default class DB {
6461
}
6562

6663
static closeDB(name: DBNames = 'data'): Promise<any> {
67-
// $FlowFixMe
6864
if (DB[name]) {
6965
return DB[name].close();
7066
}

0 commit comments

Comments
 (0)