Skip to content

Commit

Permalink
feat(typescript): migrated codebase to typescript, fixed possible mem…
Browse files Browse the repository at this point in the history
…ory leaks

BREAKING CHANGE: This might cause flow errors as flow types are removed.
  • Loading branch information
BowlingX committed Mar 2, 2021
1 parent 842af74 commit 9e1cfe0
Show file tree
Hide file tree
Showing 25 changed files with 2,543 additions and 2,334 deletions.
72 changes: 0 additions & 72 deletions .eslintrc

This file was deleted.

23 changes: 23 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint', 'import'],
env: {
browser: true,
node: true,
es6: true,
},
extends: [
'prettier',
'eslint:recommended',
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended'
],
rules: {
'import/order': ['error', { groups: ['builtin', 'external', 'parent', 'sibling', 'index'] }],
'@typescript-eslint/member-delimiter-style': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/ban-ts-comment': 'off',
}
}
1 change: 1 addition & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@

[options]
module.ignore_non_literal_requires=true
sharedmemory.heap_size=3221225472

[strict]
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
5 changes: 5 additions & 0 deletions .prettierrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# .prettierrc or .prettierrc.yaml
tabWidth: 2
semi: false
singleQuote: true
bracketSpacing: true
35 changes: 16 additions & 19 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
const presets = [
[
'@babel/env',
{
targets: {
'node': 'current'
},
useBuiltIns: 'usage'
}
]
]

module.exports = {
presets, plugins: [
require('@babel/plugin-proposal-class-properties'),
require('@babel/plugin-proposal-export-default-from'),
require('@babel/plugin-transform-flow-strip-types')
]
module.exports = {
presets: [
'@babel/typescript',
[
'@babel/preset-env',
{
targets: {
node: process.versions.node,
},
},
]
],
plugins: [
'lodash',
'@babel/proposal-class-properties'
]
}

43 changes: 27 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
"dist/"
],
"devDependencies": {
"@types/ioredis": "^4.22.0",
"@types/iltorb": "^2.3.2",
"@types/express": "^4.17.11",
"@types/lodash": "^4.14.168",
"@babel/cli": "^7.2.3",
"@babel/core": "^7.2.2",
"@babel/node": "^7.5.5",
Expand All @@ -26,23 +30,27 @@
"@semantic-release/release-notes-generator": "^9.0.1",
"babel-eslint": "^10.0.1",
"body-parser": "^1.19.0",
"eslint": "^5.12.0",
"eslint-config-google": "^0.11.0",
"eslint-plugin-flowtype": "^3.2.1",
"eslint-plugin-import": "^2.14.0",
"eslint": "^7.21.0",
"@typescript-eslint/parser": "^4.16.1",
"@typescript-eslint/eslint-plugin": "^4.16.1",
"eslint-plugin-import": "^2.22.1",
"express": "^4.17.1",
"flow-bin": "^0.114.0",
"flow-copy-source": "^2.0.2",
"rimraf": "^2.6.3",
"semantic-release": "^17.3.0"
"semantic-release": "^17.4.0",
"typescript": "^4.2.2",
"@babel/preset-typescript": "^7.10.4",
"prettier": "^2.2.1",
"eslint-config-prettier": "^6.15.0",
"babel-plugin-lodash": "^3.3.4"
},
"scripts": {
"prepare": "yarn run build:clean && yarn run build:lib && yarn run build:flow",
"prepare": "yarn run build:clean && yarn run build:lib && yarn run build:types",
"build:clean": "rimraf dist",
"build:lib": "babel src -d dist",
"build:flow": "flow-copy-source -v src dist",
"test": "yarn eslint src",
"test-server": "babel-node test/index.js"
"build:lib": "babel src -d dist --extensions \".ts\"",
"build:types": "tsc --project tsconfig.types.json",
"test": "yarn eslint src && tsc",
"test-server": "babel-node test/index.js",
"prettify": "prettier \"./src/**/*.{ts,tsx}\" --write"
},
"release": {
"branch": "master",
Expand All @@ -57,14 +65,17 @@
"optionalDependencies": {
"ioredis": "^4"
},
"peerDependencies": {
"express": "^4"
},
"dependencies": {
"@babel/polyfill": "^7.10.4",
"@apollo/client": "^3.1.3",
"@apollo/client": "^3.3.11",
"@babel/polyfill": "^7.12.1",
"apollo-utilities": "^1.3.4",
"graphql": "^14.5.8",
"http-proxy-middleware": "^1.0.5",
"http-proxy-middleware": "^1.0.6",
"iltorb": "^2.4.5",
"lodash": "^4.17.20"
"lodash": "^4.17.21"
},
"engines": {
"node": ">=8"
Expand Down
17 changes: 12 additions & 5 deletions src/caches/inmemory.js → src/caches/inmemory.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
// @flow

import { Cache } from './types'
import { didTimeout } from '../utils-browser-only'
import { Cache } from './types'

// Simple in memory implementation using `Map`

export class InMemoryCache implements Cache<string, Object> {
cache: Map<string, Object> = new Map()
interface CachedData<T> {
data: T
time: number
timeout: number
}

export class InMemoryCache<K extends string = string, V = Record<string, any>>
implements Cache<K, V | null> {
cache: Map<string, CachedData<V>> = new Map()

delete(key: string): Promise<boolean> {
return Promise.resolve(this.cache.delete(key))
}

get(key: string): Promise<?Object> {
get(key: string): Promise<V | null> {
const possibleData = this.cache.get(key)

if (!possibleData) {
Expand All @@ -27,7 +34,7 @@ export class InMemoryCache implements Cache<string, Object> {
return Promise.resolve(data)
}

set(key: string, value: Object, timeout: number): Promise<Cache<string, Object>> {
set(key: K, value: V, timeout: number): Promise<InMemoryCache<K, V>> {
this.cache.set(key, { data: value, time: Number(new Date()), timeout })
return Promise.resolve(this)
}
Expand Down
31 changes: 0 additions & 31 deletions src/caches/redis.js

This file was deleted.

37 changes: 37 additions & 0 deletions src/caches/redis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// @flow

import type { Redis, KeyType } from 'ioredis'
import { Cache } from './types'

export class RedisCache<
K extends KeyType = string,
V = null | Record<string, any>
> implements Cache<K, V | null> {
client: Redis

constructor(client: Redis) {
this.client = client
}

async delete(key: K): Promise<boolean> {
const result = await this.client.del(key)
return result === 1
}

async get(key: K): Promise<V | null> {
const result = await this.client.get(key)
if (result) {
return JSON.parse(result.toString())
}
return null
}

async set(key: K, value: V, timeout: number): Promise<RedisCache<K, V>> {
if (timeout > 0) {
await this.client.set(key, JSON.stringify(value), 'EX', timeout)
return this
}
await this.client.set(key, JSON.stringify(value))
return this
}
}
5 changes: 0 additions & 5 deletions src/caches/types.js

This file was deleted.

5 changes: 5 additions & 0 deletions src/caches/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface Cache<K, V = null | Record<string, any>> {
delete(key: K): Promise<boolean>
get(key: K): Promise<V>
set(key: K, value: V, timeout: number): Promise<Cache<K, V>>
}
File renamed without changes.
Loading

0 comments on commit 9e1cfe0

Please sign in to comment.