Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions __tests__/cli/index.js → __tests__/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const fs = require('fs')
const path = require('path')
const cp = require('child_process')
const assert = require('assert')
const supertest = require('supertest')
const osTmpdir = require('os-tmpdir')
const tempWrite = require('temp-write')
const mkdirp = require('mkdirp')
const rimraf = require('rimraf')
const serverReady = require('server-ready')
import fs from 'fs'
import path from 'path'
import cp from 'child_process'
import assert from 'assert'
import supertest from 'supertest'
import osTmpdir from 'os-tmpdir'
import tempWrite from 'temp-write'
import mkdirp from 'mkdirp'
import rimraf from 'rimraf'
import serverReady from 'server-ready'
import { Argv } from 'yargs'

let PORT = 3100

Expand All @@ -19,8 +20,8 @@ const middlewareFiles = {

const bin = path.join(__dirname, '../../lib/cli/bin')

function cli(args) {
return cp.spawn('node', ['--', bin, '-p', PORT].concat(args), {
function cli(args: Argv) {
return cp.spawn('node', ['--', bin, '-p', PORT.toString()].concat(args), {
cwd: __dirname,
stdio: ['pipe', process.stdout, process.stderr],
})
Expand Down
8 changes: 4 additions & 4 deletions __tests__/server/mixins.js → __tests__/server/mixins.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const assert = require('assert')
const _ = require('lodash')
const lodashId = require('lodash-id')
const mixins = require('../../src/server/mixins')
import assert from 'assert'
import _ from 'lodash'
import lodashId from 'lodash-id'
import * as mixins from '../../src/server/mixins'

describe('mixins', () => {
let db
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const assert = require('assert')
const request = require('supertest')
const jsonServer = require('../../src/server')
import assert from 'assert'
import request from 'supertest'
import jsonServer from '../../src/server'

describe('Fake server', () => {
let server
let server: any
let router
let db

Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
const assert = require('assert')
const _ = require('lodash')
const request = require('supertest')
const jsonServer = require('../../src/server')
import assert from 'assert'
import _ from 'lodash'
import request from 'supertest'
import jsonServer from '../../src/server'

interface Comment {
id: number,
post_id: number
post: Post
}

interface Post {
id: number,
body: string,
comments: Comment[]
}

describe('Server with custom foreign key', () => {
let server
let server: any
let router
let db
let db: {
posts: Post[],
comments: Comment[]
}

beforeEach(() => {
db = {}

db.posts = [
{ id: 1, body: 'foo' },
{ id: 2, body: 'bar' },
]

db.comments = [
{ id: 1, post_id: 1 },
{ id: 2, post_id: 1 },
{ id: 3, post_id: 2 },
]
const post1 = { id: 1, body: 'foo', comments: [] }
const post2 = { id: 2, body: 'bar', comments: [] }
db = {
posts: [
post1,
post2,
],
comments: [
{ id: 1, post_id: 1, post: post1 },
{ id: 2, post_id: 1, post: post1 },
{ id: 3, post_id: 2, post: post2 },
],
}

server = jsonServer.create()
router = jsonServer.router(db, { foreignKeySuffix: '_id' })
Expand Down
8 changes: 4 additions & 4 deletions __tests__/server/plural.js → __tests__/server/plural.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const assert = require('assert')
const _ = require('lodash')
const request = require('supertest')
const jsonServer = require('../../src/server')
import assert from 'assert'
import _ from 'lodash'
import request from 'supertest'
import jsonServer from '../../src/server'

describe('Server', () => {
let server
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const assert = require('assert')
const request = require('supertest')
const jsonServer = require('../../src/server')
import assert from 'assert'
import request from 'supertest'
import jsonServer from '../../src/server'
import { Express } from "express"

describe('Fake server', () => {
let server
let server: Express
let router
let db

Expand Down
4 changes: 2 additions & 2 deletions __tests__/server/singular.js → __tests__/server/singular.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const request = require('supertest')
const jsonServer = require('../../src/server')
import request from 'supertest'
import jsonServer from '../../src/server'

describe('Server', () => {
let server
Expand Down
4 changes: 2 additions & 2 deletions __tests__/server/utils.js → __tests__/server/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const assert = require('assert')
const utils = require('../../src/server/utils')
import assert from 'assert'
import * as utils from '../../src/server/utils'

describe('utils', () => {
describe('getPage', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const assert = require('assert')
const validateData = require('../../src/server/router/validate-data')
import assert from 'assert'
import { validateData } from '../../src/server/router/validate-data'

describe('validateData', () => {
test('should throw an error if data contains /', () => {
Expand Down
3 changes: 3 additions & 0 deletions custom-typings/connect-pause/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module 'connect-pause' {
export default function pause(delay: number): (req: Request, res: Response, next: () => void) => void
}
3 changes: 3 additions & 0 deletions custom-typings/lodash-id/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module 'lodash-id' {
export default function mixin<TObject>(object: TObject, source: Dictionary<(...args: any[]) => any>, options?: MixinOptions): TObject;
}
3 changes: 3 additions & 0 deletions custom-typings/server-ready/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module 'server-ready' {
export default function serverReady(port: number, done: () => void): void
}
19 changes: 19 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
const {defaults} = require('jest-config');
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
roots: [
"<rootDir>/__tests__"
],
testMatch: [
"**/*.+(ts|tsx|js)"
],
transform: {
"^.+\\.(ts|tsx)$": "ts-jest"
},
collectCoverageFrom: [
"src/**/*.{js,jsx,ts,tsx}",
"!src/**/*.d.ts"
]
};
22 changes: 21 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@
"@babel/core": "^7.12.3",
"@babel/node": "^7.12.6",
"@babel/preset-env": "^7.12.1",
"@types/compression": "^1.0.1",
"@types/cors": "^2.8.6",
"@types/errorhandler": "0.0.32",
"@types/express": "^4.17.2",
"@types/jest": "^27.4.0",
"@types/lodash": "^4.14.149",
"@types/lowdb": "^1.0.9",
"@types/method-override": "0.0.31",
"@types/mkdirp": "^1.0.2",
"@types/morgan": "^1.7.37",
"@types/nanoid": "^2.1.0",
"@types/os-tmpdir": "^1.0.0",
"@types/pluralize": "0.0.29",
"@types/request": "^2.48.4",
"@types/rimraf": "^3.0.2",
"@types/server-destroy": "^1.0.0",
"@types/supertest": "^2.0.11",
"@types/update-notifier": "^2.5.0",
"cross-env": "^7.0.2",
"eslint": "^7.13.0",
"eslint-config-prettier": "^8.3.0",
Expand All @@ -65,7 +83,9 @@
"rimraf": "^3.0.2",
"server-ready": "^0.3.1",
"supertest": "^6.0.1",
"temp-write": "^4.0.0"
"temp-write": "^4.0.0",
"ts-jest": "^27.1.2",
"typescript": "^3.7.5"
},
"repository": {
"type": "git",
Expand Down
File renamed without changes.
10 changes: 5 additions & 5 deletions src/cli/index.js → src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const updateNotifier = require('update-notifier')
const yargs = require('yargs')
const run = require('./run')
const pkg = require('../../package.json')
import updateNotifier from 'update-notifier'
import yargs from 'yargs'
import run from './run'
import pkg from '../../package.json'

module.exports = function () {
export default function() {
updateNotifier({ pkg }).notify()

const argv = yargs
Expand Down
60 changes: 42 additions & 18 deletions src/cli/run.js → src/cli/run.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const fs = require('fs')
const path = require('path')
const jph = require('json-parse-helpfulerror')
const _ = require('lodash')
const chalk = require('chalk')
const enableDestroy = require('server-destroy')
const pause = require('connect-pause')
const is = require('./utils/is')
const load = require('./utils/load')
const jsonServer = require('../server')

function prettyPrint(argv, object, rules) {
import fs from 'fs'
import path from 'path'
import jph from 'json-parse-helpfulerror'
import _ from 'lodash'
import chalk from 'chalk'
import enableDestroy from 'server-destroy'
import pause from 'connect-pause'
import {FILE, JS, URL} from './utils/is'
import load from './utils/load'
import jsonServer from '../server'

function prettyPrint(argv: Argv, object, rules) {
const root = `http://${argv.host}:${argv.port}`

console.log()
Expand All @@ -32,7 +32,7 @@ function prettyPrint(argv, object, rules) {
console.log()
}

function createApp(db, routes, middlewares, argv) {
function createApp(db, routes, middlewares, argv: Argv) {
const app = jsonServer.create()

const { foreignKeySuffix } = argv
Expand All @@ -42,12 +42,13 @@ function createApp(db, routes, middlewares, argv) {
foreignKeySuffix ? { foreignKeySuffix } : undefined
)

const defaultsOpts = {
const defaultsOpts: Argv = {
logger: !argv.quiet,
readOnly: argv.readOnly,
noCors: argv.noCors,
noGzip: argv.noGzip,
bodyParser: true,
static: "",
}

if (argv.static) {
Expand Down Expand Up @@ -77,7 +78,25 @@ function createApp(db, routes, middlewares, argv) {
return app
}

module.exports = function (argv) {
type Argv = {
host: string
port: number
watch: string
routes: string
snapshots: string
quiet: boolean
middlewares: Array<any>
delay: number
static: string
readOnly: boolean
noGzip: boolean
noCors: boolean
logger: boolean
bodyParser: boolean
foreignKeySuffix: string
}

export default function(argv: Argv) {
const source = argv._[0]
let app
let server
Expand Down Expand Up @@ -134,7 +153,7 @@ module.exports = function (argv) {
prettyPrint(argv, db.getState(), routes)

// Catch and handle any error occurring in the server process
process.on('uncaughtException', (error) => {
process.on('uncaughtException', (error: RunError) => {
if (error.errno === 'EADDRINUSE')
console.log(
chalk.red(
Expand Down Expand Up @@ -183,7 +202,7 @@ module.exports = function (argv) {
const source = argv._[0]

// Can't watch URL
if (is.URL(source)) throw new Error("Can't watch URL")
if (URL(source)) throw new Error("Can't watch URL")

// Watch .js or .json file
// Since lowdb uses atomic writing, directory is watched instead of file
Expand All @@ -195,7 +214,7 @@ module.exports = function (argv) {
if (file) {
const watchedFile = path.resolve(watchedDir, file)
if (watchedFile === path.resolve(source)) {
if (is.FILE(watchedFile)) {
if (FILE(watchedFile)) {
let obj
try {
obj = jph.parse(fs.readFileSync(watchedFile))
Expand Down Expand Up @@ -245,3 +264,8 @@ module.exports = function (argv) {
process.exit(1)
})
}

type RunError = {
errno: string
port: number
}
18 changes: 9 additions & 9 deletions src/cli/utils/is.js → src/cli/utils/is.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
module.exports = {
FILE,
JS,
URL,
}

function FILE(s) {
function FILE(s: string) {
return !URL(s) && /\.json$/.test(s)
}

function JS(s) {
function JS(s: string) {
return !URL(s) && /\.js$/.test(s)
}

function URL(s) {
function URL(s: string) {
return /^(http|https):/.test(s)
}

export {
FILE,
JS,
URL
}
Loading