Skip to content

Commit

Permalink
feat: convert to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
spence-s committed Nov 12, 2024
1 parent 71902b1 commit 0b457c4
Show file tree
Hide file tree
Showing 94 changed files with 11,311 additions and 4,395 deletions.
29 changes: 29 additions & 0 deletions .xo-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"prettier": true,
"parserOptions": {
"project": "./tsconfig.json"
},
"ignore": "**/*.js",
"rules": {
"@typescript-eslint/prefer-nullish-coalescing": "off",
"@typescript-eslint/naming-convention": "off",
"@typescript-eslint/consistent-type-definitions": "off",
"@typescript-eslint/promise-function-async": "off",
"@typescript-eslint/no-floating-promises": "off",
"@typescript-eslint/no-empty-interface": "off",
"@typescript-eslint/ban-types": "off",
"@typescript-eslint/no-namespace": "off",
"unicorn/prevent-abbreviations": "off",
"unicorn/prefer-module": "off",
"unicorn/prefer-ternary": "off",
"capitalized-comments": "off",
"unicorn/no-array-reduce": "off",
"no-warning-comments": "off",
"max-depth": "off",
"complexity": "off",
"no-multi-assign": "off",
"accessor-pairs": "error",
"prefer-destructuring": "off",
"logical-assignment-operators": "off"
}
}
8 changes: 4 additions & 4 deletions __tests__/application/compose.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict'

const { describe, it } = require('node:test')
const request = require('supertest')
const assert = require('assert')
const Koa = require('../..')
import { describe, it } from 'node:test'
import request from 'supertest'
import assert from 'assert'
import Koa from '../../dist/application.js'

describe('app.compose', () => {
it('should work with default compose ', async () => {
Expand Down
8 changes: 4 additions & 4 deletions __tests__/application/context.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict'

const { describe, it } = require('node:test')
const request = require('supertest')
const assert = require('assert')
const Koa = require('../..')
import { describe, it } from 'node:test'
import request from 'supertest'
import assert from 'assert'
import Koa from '../../dist/application.js'

describe('app.context', () => {
const app1 = new Koa()
Expand Down
16 changes: 4 additions & 12 deletions __tests__/application/currentContext.test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
'use strict'

const { describe, it } = require('node:test')
const request = require('supertest')
const assert = require('assert')
const Koa = require('../..')
import { describe, it } from 'node:test'
import request from 'supertest'
import assert from 'assert'
import Koa from '../../dist/application.js'

describe('app.currentContext', () => {
it('should throw error if AsyncLocalStorage not support', () => {
if (require('async_hooks').AsyncLocalStorage) return
assert.throws(() => new Koa({ asyncLocalStorage: true }),
/Requires node 12\.17\.0 or higher to enable asyncLocalStorage/)
})

it('should get currentContext return context when asyncLocalStorage enable', async () => {
if (!require('async_hooks').AsyncLocalStorage) return

const app = new Koa({ asyncLocalStorage: true })

app.use(async ctx => {
Expand Down
11 changes: 5 additions & 6 deletions __tests__/application/index.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict'

const { describe, it } = require('node:test')
const request = require('supertest')
const assert = require('assert')
const Koa = require('../..')
import { describe, it } from 'node:test'
import request from 'supertest'
import assert from 'assert'
import Koa from '../../dist/application.js'
import CreateError from 'http-errors'

describe('app', () => {
// ignore test on Node.js v18
Expand Down Expand Up @@ -64,8 +65,6 @@ describe('app', () => {
})

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)
Expand Down
8 changes: 4 additions & 4 deletions __tests__/application/inspect.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict'

const { describe, it } = require('node:test')
const assert = require('assert')
const util = require('util')
const Koa = require('../..')
import { describe, it } from 'node:test'
import assert from 'assert'
import util from 'util'
import Koa from '../../dist/application.js'

process.env.NODE_ENV = 'test'
const app = new Koa()
Expand Down
9 changes: 5 additions & 4 deletions __tests__/application/onerror.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
'use strict'

const { describe, it, mock } = require('node:test')
const assert = require('assert')
const Koa = require('../..')
import { describe, it, mock } from 'node:test'
import assert from 'assert'
import Koa from '../../dist/application.js'
import vm from 'node:vm'

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

it('should accept errors coming from other scopes', () => {
const ExternError = require('vm').runInNewContext('Error')
const ExternError = vm.runInNewContext('Error')

const app = new Koa()
const error = Object.assign(new ExternError('boom'), {
Expand Down
8 changes: 4 additions & 4 deletions __tests__/application/request.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict'

const { describe, it } = require('node:test')
const request = require('supertest')
const assert = require('assert')
const Koa = require('../..')
import { describe, it } from 'node:test'
import request from 'supertest'
import assert from 'assert'
import Koa from '../../dist/application.js'

describe('app.request', () => {
const app1 = new Koa()
Expand Down
19 changes: 9 additions & 10 deletions __tests__/application/respond.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
'use strict'

const { describe, it } = require('node:test')
const request = require('supertest')
const statuses = require('statuses')
const assert = require('assert')
const Koa = require('../..')
const fs = require('fs')
import { describe, it } from 'node:test'
import request from 'supertest'
import statuses from 'statuses'
import assert from 'assert'
import Koa from '../../dist/application.js'
import fs from 'fs'
import path from 'path'

const pkg = JSON.parse(fs.readFileSync(path.join(import.meta.dirname, '..', '..', 'package.json')))

describe('app.respond', () => {
describe('when ctx.respond === false', () => {
Expand Down Expand Up @@ -616,7 +619,6 @@ describe('app.respond', () => {
.get('/')
.expect('Content-Type', 'application/json; charset=utf-8')

const pkg = require('../../package')
assert.strictEqual(Object.prototype.hasOwnProperty.call(res.headers, 'content-length'), false)
assert.deepStrictEqual(res.body, pkg)
})
Expand All @@ -634,7 +636,6 @@ describe('app.respond', () => {
.get('/')
.expect('Content-Type', 'application/json; charset=utf-8')

const pkg = require('../../package')
assert.strictEqual(Object.prototype.hasOwnProperty.call(res.headers, 'content-length'), false)
assert.deepStrictEqual(res.body, pkg)
})
Expand All @@ -652,7 +653,6 @@ describe('app.respond', () => {
.get('/')
.expect('Content-Type', 'application/json; charset=utf-8')

const pkg = require('../../package')
assert.strictEqual(Object.prototype.hasOwnProperty.call(res.headers, 'content-length'), true)
assert.deepStrictEqual(res.body, pkg)
})
Expand All @@ -673,7 +673,6 @@ describe('app.respond', () => {
.get('/')
.expect('Content-Type', 'application/json; charset=utf-8')

const pkg = require('../../package')
assert.strictEqual(Object.prototype.hasOwnProperty.call(res.headers, 'content-length'), true)
assert.deepStrictEqual(res.body, pkg)
})
Expand Down
8 changes: 4 additions & 4 deletions __tests__/application/response.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict'

const { describe, it } = require('node:test')
const request = require('supertest')
const assert = require('assert')
const Koa = require('../..')
import { describe, it } from 'node:test'
import request from 'supertest'
import assert from 'assert'
import Koa from '../../dist/application.js'

describe('app.response', () => {
const app1 = new Koa()
Expand Down
6 changes: 3 additions & 3 deletions __tests__/application/toJSON.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict'

const { describe, it } = require('node:test')
const assert = require('assert')
const Koa = require('../..')
import { describe, it } from 'node:test'
import assert from 'assert'
import Koa from '../../dist/application.js'

describe('app.toJSON()', () => {
it('should work', () => {
Expand Down
8 changes: 4 additions & 4 deletions __tests__/application/use.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict'

const { describe, it } = require('node:test')
const request = require('supertest')
const assert = require('assert')
const Koa = require('../..')
import { describe, it } from 'node:test'
import request from 'supertest'
import assert from 'assert'
import Koa from '../../dist/application.js'

describe('app.use(fn)', () => {
it('should compose middleware', async () => {
Expand Down
6 changes: 3 additions & 3 deletions __tests__/context/assert.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict'

const { describe, it } = require('node:test')
const context = require('../../test-helpers/context')
const assert = require('assert')
import { describe, it } from 'node:test'
import context from '../../test-helpers/context.js'
import assert from 'assert'

describe('ctx.assert(value, status)', () => {
it('should throw an error', () => {
Expand Down
8 changes: 4 additions & 4 deletions __tests__/context/cookies.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict'

const { describe, it } = require('node:test')
const assert = require('assert')
const request = require('supertest')
const Koa = require('../..')
import { describe, it } from 'node:test'
import assert from 'assert'
import request from 'supertest'
import Koa from '../../dist/application.js'

describe('ctx.cookies', () => {
describe('ctx.cookies.set()', () => {
Expand Down
10 changes: 5 additions & 5 deletions __tests__/context/inspect.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict'

const { describe, it } = require('node:test')
const prototype = require('../../lib/context')
const assert = require('assert')
const util = require('util')
const context = require('../../test-helpers/context')
import { describe, it } from 'node:test'
import prototype from '../../dist/context.js'
import assert from 'assert'
import util from 'util'
import context from '../../test-helpers/context.js'

describe('ctx.inspect()', () => {
it('should return a json representation', () => {
Expand Down
15 changes: 7 additions & 8 deletions __tests__/context/onerror.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict'

const { describe, it } = require('node:test')
const assert = require('assert')
const request = require('supertest')
const Koa = require('../..')
const context = require('../../test-helpers/context')

import { describe, it } from 'node:test'
import assert from 'assert'
import request from 'supertest'
import Koa from '../../dist/application.js'
import context from '../../test-helpers/context.js'
import vm from 'node:vm'
describe('ctx.onerror(err)', () => {
it('should respond', () => {
const app = new Koa()
Expand Down Expand Up @@ -192,8 +192,7 @@ describe('ctx.onerror(err)', () => {

describe('when error from another scope thrown', () => {
it('should handle it like a normal error', async () => {
const ExternError = require('vm').runInNewContext('Error')

const ExternError = vm.runInNewContext('Error')
const app = new Koa()
const error = Object.assign(new ExternError('boom'), {
status: 418,
Expand Down
8 changes: 4 additions & 4 deletions __tests__/context/state.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict'

const { describe, it } = require('node:test')
const request = require('supertest')
const assert = require('assert')
const Koa = require('../..')
import { describe, it } from 'node:test'
import request from 'supertest'
import assert from 'assert'
import Koa from '../../dist/application.js'

describe('ctx.state', () => {
it('should provide a ctx.state namespace', () => {
Expand Down
6 changes: 3 additions & 3 deletions __tests__/context/throw.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict'

const { describe, it } = require('node:test')
const context = require('../../test-helpers/context')
const assert = require('assert')
import { describe, it } from 'node:test'
import context from '../../test-helpers/context.js'
import assert from 'assert'

describe('ctx.throw(msg)', () => {
it('should set .status to 500', () => {
Expand Down
6 changes: 3 additions & 3 deletions __tests__/context/toJSON.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict'

const { describe, it } = require('node:test')
const assert = require('assert')
const context = require('../../test-helpers/context')
import { describe, it } from 'node:test'
import assert from 'assert'
import context from '../../test-helpers/context.js'

describe('ctx.toJSON()', () => {
it('should return a json representation', () => {
Expand Down
6 changes: 3 additions & 3 deletions __tests__/lib/search-params.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { describe, it } = require('node:test')
const sp = require('../../lib/search-params')
const assert = require('assert')
import { describe, it } from 'node:test'
import sp from '../../dist/search-params.js'
import assert from 'assert'

describe('search-params', () => {
describe('stringify', () => {
Expand Down
Loading

0 comments on commit 0b457c4

Please sign in to comment.