Skip to content

Commit

Permalink
chore: migrate main code base from js -> ts
Browse files Browse the repository at this point in the history
  • Loading branch information
etroynov committed Jun 30, 2024
1 parent 0176f81 commit c53c354
Show file tree
Hide file tree
Showing 36 changed files with 392 additions and 291 deletions.
15 changes: 8 additions & 7 deletions __tests__/application/compose.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import request from 'supertest'
import assert from 'node:assert'
import Koa from '../..'
import Koa from '../../src/application'

import { describe, it } from '@jest/globals'
import { KoaContext } from '@/context'

describe('app.compose', () => {
it('should work with default compose ', async () => {
const app = new Koa()
const calls = []
const calls: number[] = []

app.use((ctx, next) => {
app.use((_ctx: KoaContext, next) => {
calls.push(1)
return next().then(() => {
calls.push(4)
})
})

app.use((ctx, next) => {
app.use((_ctx: KoaContext, next) => {
calls.push(2)
return next().then(() => {
calls.push(3)
Expand All @@ -37,7 +38,7 @@ describe('app.compose', () => {
let count = 0
const app = new Koa({
compose (fns) {
return async (ctx) => {
return async (ctx: KoaContext) => {
const dispatch = async () => {
count++
const fn = fns.shift()
Expand All @@ -48,12 +49,12 @@ describe('app.compose', () => {
}
})

app.use((ctx, next) => {
app.use((_ctx: KoaContext, next) => {
calls.push(1)
next()
calls.push(4)
})
app.use((ctx, next) => {
app.use((_ctx: KoaContext, next) => {
calls.push(2)
next()
calls.push(3)
Expand Down
6 changes: 3 additions & 3 deletions __tests__/application/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import request from 'supertest'
import assert from 'node:assert'
import Koa from '../..'
import Koa from '../../src/application'

import { describe, it } from '@jest/globals'

Expand All @@ -10,7 +10,7 @@ describe('app.context', () => {
const app2 = new Koa()

it('should merge properties', () => {
app1.use((ctx, next) => {
app1.use(ctx => {
assert.strictEqual(ctx.msg, 'hello')
ctx.status = 204
})
Expand All @@ -21,7 +21,7 @@ describe('app.context', () => {
})

it('should not affect the original prototype', () => {
app2.use((ctx, next) => {
app2.use(ctx => {
assert.strictEqual(ctx.msg, undefined)
ctx.status = 204
})
Expand Down
12 changes: 6 additions & 6 deletions __tests__/application/currentContext.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import request from 'supertest'
import assert from 'node:assert'
import Koa from '../..'
import Koa from '../../src/application'

import { describe, it } from '@jest/globals'

Expand All @@ -18,13 +18,13 @@ describe('app.currentContext', () => {

app.use(async ctx => {
assert(ctx === app.currentContext)
await new Promise(resolve => {
await new Promise<void>(resolve => {
setTimeout(() => {
assert(ctx === app.currentContext)
resolve()
}, 1)
})
await new Promise(resolve => {
await new Promise<void>(resolve => {
assert(ctx === app.currentContext)
setImmediate(() => {
assert(ctx === app.currentContext)
Expand Down Expand Up @@ -68,7 +68,7 @@ describe('app.currentContext', () => {
throw new Error('error message')
})

const handleError = new Promise((resolve, reject) => {
const handleError = new Promise<void>((resolve, reject) => {
app.on('error', (err, ctx) => {
try {
assert.strictEqual(err.message, 'error message')
Expand All @@ -91,8 +91,8 @@ describe('app.currentContext', () => {
throw new Error('error message')
})

const handleError = new Promise((resolve, reject) => {
app.on('error', (err, ctx) => {
const handleError = new Promise<void>((resolve, reject) => {
app.on('error', err => {
try {
assert.strictEqual(err.message, 'error message')
assert.strictEqual(app.currentContext, undefined)
Expand Down
13 changes: 6 additions & 7 deletions __tests__/application/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import request from 'supertest'
import assert from 'node:assert'
import Koa from '../..'
import Koa from '../../src/application'
import CreateError from 'http-errors'

import { describe, it } from '@jest/globals'

Expand All @@ -9,7 +10,7 @@ describe('app', () => {
(/^v18\./.test(process.version) ? it.skip : it)('should handle socket errors', done => {
const app = new Koa()

app.use((ctx, next) => {
app.use(ctx => {
// triggers ctx.socket.writable == false
ctx.socket.emit('error', new Error('boom'))
})
Expand All @@ -27,7 +28,7 @@ describe('app', () => {
it('should not .writeHead when !socket.writable', done => {
const app = new Koa()

app.use((ctx, next) => {
app.use(ctx => {
// set .writable to false
ctx.socket.writable = false
ctx.status = 204
Expand Down Expand Up @@ -79,16 +80,14 @@ describe('app', () => {
})

it('should set compose from the constructor', () => {
const compose = () => (ctx) => {}
const compose = () => () => {}
const app = new Koa({ compose })
assert.strictEqual(app.compose, compose)
})

it('should have a static property exporting `HttpError` from http-errors library', () => {
const CreateError = require('http-errors')

assert.notEqual(Koa.HttpError, undefined)
assert.deepStrictEqual(Koa.HttpError, CreateError.HttpError)
assert.throws(() => { throw new CreateError(500, 'test error') }, Koa.HttpError)
assert.throws(() => { throw CreateError(500, 'test error') }, Koa.HttpError)
})
})
2 changes: 1 addition & 1 deletion __tests__/application/inspect.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import util from 'node:util'
import assert from 'node:assert'
import Koa from '../..'
import Koa from '../../src/application'

import { describe, it } from '@jest/globals'

Expand Down
6 changes: 3 additions & 3 deletions __tests__/application/onerror.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import assert from 'node:assert'
import Koa from '../..'
import Koa from '../../src/application'

import { describe, it } from '@jest/globals'
import { describe, expect, it, jest } from '@jest/globals'

describe('app.onerror(err)', () => {
it('should throw an error if a non-error is given', () => {
Expand All @@ -26,7 +26,7 @@ describe('app.onerror(err)', () => {

it('should do nothing if status is 404', () => {
const app = new Koa()
const err = new Error()
const err: any = new Error()

err.status = 404

Expand Down
6 changes: 3 additions & 3 deletions __tests__/application/request.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import request from 'supertest'
import assert from 'node:assert'
import Koa from '../..'
import Koa from '../../src/application'

import { describe, it } from '@jest/globals'

Expand All @@ -10,7 +10,7 @@ describe('app.request', () => {
const app2 = new Koa()

it('should merge properties', () => {
app1.use((ctx, next) => {
app1.use(ctx => {
assert.strictEqual(ctx.request.message, 'hello')
ctx.status = 204
})
Expand All @@ -21,7 +21,7 @@ describe('app.request', () => {
})

it('should not affect the original prototype', () => {
app2.use((ctx, next) => {
app2.use(ctx => {
assert.strictEqual(ctx.request.message, undefined)
ctx.status = 204
})
Expand Down
16 changes: 9 additions & 7 deletions __tests__/application/respond.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import statuses from 'statuses'
import request from 'supertest'
import assert from 'node:assert'
import fs from 'node:fs'
import Koa from '../..'
import Koa from '../../src/application'

import { describe, it } from '@jest/globals'

Expand Down Expand Up @@ -272,12 +272,12 @@ describe('app.respond', () => {
it('should send the right body', () => {
const app = new Koa()

app.use((ctx, next) => {
app.use(ctx => {
const res = ctx.res
ctx.status = 200
res.setHeader('Content-Type', 'text/html')
res.write('Hello')
return new Promise(resolve => {
return new Promise<void>(resolve => {
setTimeout(() => {
res.end('Goodbye')
resolve()
Expand Down Expand Up @@ -386,6 +386,7 @@ describe('app.respond', () => {
.expect(700)
.expect('custom status')

// @ts-ignore
assert.strictEqual(res.res.statusMessage, 'custom status')
})
})
Expand All @@ -406,6 +407,7 @@ describe('app.respond', () => {
.expect(200)
.expect('ok')

// @ts-ignore
assert.strictEqual(res.res.statusMessage, 'ok')
})
})
Expand Down Expand Up @@ -764,8 +766,8 @@ describe('app.respond', () => {
it('should expose the message', () => {
const app = new Koa()

app.use(ctx => {
const err = new Error('sorry!')
app.use(() => {
const err: any = new Error('sorry!')
err.status = 403
err.expose = true
throw err
Expand All @@ -781,8 +783,8 @@ describe('app.respond', () => {
it('should respond with .status', () => {
const app = new Koa()

app.use(ctx => {
const err = new Error('s3 explodes')
app.use(() => {
const err: any = new Error('s3 explodes')
err.status = 403
throw err
})
Expand Down
16 changes: 8 additions & 8 deletions __tests__/application/response.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import request from 'supertest'
import assert from 'node:assert'
import Koa from '../..'
import Koa from '../../src/application'

import { describe, it } from '@jest/globals'

Expand All @@ -15,7 +15,7 @@ describe('app.response', () => {
const app7 = new Koa()

it('should merge properties', () => {
app1.use((ctx, next) => {
app1.use(ctx => {
assert.strictEqual(ctx.response.msg, 'hello')
ctx.status = 204
})
Expand All @@ -26,7 +26,7 @@ describe('app.response', () => {
})

it('should not affect the original prototype', () => {
app2.use((ctx, next) => {
app2.use(ctx => {
assert.strictEqual(ctx.response.msg, undefined)
ctx.status = 204
})
Expand All @@ -37,7 +37,7 @@ describe('app.response', () => {
})

it('should not include status message in body for http2', async () => {
app3.use((ctx, next) => {
app3.use(ctx => {
ctx.req.httpVersionMajor = 2
ctx.status = 404
})
Expand All @@ -48,7 +48,7 @@ describe('app.response', () => {
})

it('should set ._explicitNullBody correctly', async () => {
app4.use((ctx, next) => {
app4.use(ctx => {
ctx.body = null
assert.strictEqual(ctx.response._explicitNullBody, true)
})
Expand All @@ -59,7 +59,7 @@ describe('app.response', () => {
})

it('should not set ._explicitNullBody incorrectly', async () => {
app5.use((ctx, next) => {
app5.use(ctx => {
ctx.body = undefined
assert.strictEqual(ctx.response._explicitNullBody, undefined)
ctx.body = ''
Expand All @@ -74,7 +74,7 @@ describe('app.response', () => {
})

it('should add Content-Length when Transfer-Encoding is not defined', () => {
app6.use((ctx, next) => {
app6.use(ctx => {
ctx.body = 'hello world'
})

Expand All @@ -85,7 +85,7 @@ describe('app.response', () => {
})

it('should not add Content-Length when Transfer-Encoding is defined', () => {
app7.use((ctx, next) => {
app7.use(ctx => {
ctx.set('Transfer-Encoding', 'chunked')
ctx.body = 'hello world'
assert.strictEqual(ctx.response.get('Content-Length'), undefined)
Expand Down
2 changes: 1 addition & 1 deletion __tests__/application/toJSON.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from 'node:assert'
import Koa from '../..'
import Koa from '../../src/application'

import { describe, it } from '@jest/globals'

Expand Down
2 changes: 1 addition & 1 deletion __tests__/application/use.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import request from 'supertest'
import assert from 'node:assert'
import Koa from '../..'
import Koa from '../../src/application'

import { describe, it } from '@jest/globals'

Expand Down
2 changes: 1 addition & 1 deletion __tests__/context/cookies.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import request from 'supertest'
import assert from 'node:assert'
import Koa from '../..'
import Koa from '../../src/application'

import { describe, it } from '@jest/globals'

Expand Down
2 changes: 1 addition & 1 deletion __tests__/context/inspect.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import assert from 'node:assert'
import util from 'node:util'
import createContext from '../../test-helpers/context'
import context from '../../lib/context'
import context from '../../src/context'

import { describe, it } from '@jest/globals'

Expand Down
2 changes: 1 addition & 1 deletion __tests__/context/onerror.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import request from 'supertest'
import assert from 'node:assert'
import createContext from '../../test-helpers/context'
import Koa from '../..'
import Koa from '../../src/application'

import { describe, it } from '@jest/globals'

Expand Down
Loading

0 comments on commit c53c354

Please sign in to comment.