Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: convert koa to ts #1848

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
30 changes: 17 additions & 13 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,31 @@ name: Node.js CI

on:
push:
branch: master
branches: [master]
pull_request:
branch: master
branches: [master]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18.x, 20.x, 22.x]

steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run lint
- run: npm run test:coverage
- run: npx codecov
continue-on-error: true
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run lint
- run: npm run build
# https://github.com/nodejs/node/issues/55054 multiple issues with experimental code coverage
# with source maps make test coverage unusable.
# TODO: use code coverage when it is fixed by node core teams
- run: npm run test
# - run: npm run test:coverage
# - run: npx codecov
# continue-on-error: true
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
22 changes: 12 additions & 10 deletions __tests__/application/respond.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
'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 'node:path'
import { fileURLToPath } from 'node:url'

const __dirname = path.dirname(fileURLToPath(import.meta.url))

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

describe('app.respond', () => {
describe('when ctx.respond === false', () => {
Expand Down Expand Up @@ -616,7 +622,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 +639,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 +656,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 +676,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
Loading
Loading