diff --git a/__tests__/application/compose.ts b/__tests__/application/compose.ts index 7a1877606..4fb1f62a9 100644 --- a/__tests__/application/compose.ts +++ b/__tests__/application/compose.ts @@ -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) @@ -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() @@ -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) diff --git a/__tests__/application/context.ts b/__tests__/application/context.ts index b985a8fda..4820aad87 100644 --- a/__tests__/application/context.ts +++ b/__tests__/application/context.ts @@ -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' @@ -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 }) @@ -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 }) diff --git a/__tests__/application/currentContext.ts b/__tests__/application/currentContext.ts index c277e4783..d65189610 100644 --- a/__tests__/application/currentContext.ts +++ b/__tests__/application/currentContext.ts @@ -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' @@ -18,13 +18,13 @@ describe('app.currentContext', () => { app.use(async ctx => { assert(ctx === app.currentContext) - await new Promise(resolve => { + await new Promise(resolve => { setTimeout(() => { assert(ctx === app.currentContext) resolve() }, 1) }) - await new Promise(resolve => { + await new Promise(resolve => { assert(ctx === app.currentContext) setImmediate(() => { assert(ctx === app.currentContext) @@ -68,7 +68,7 @@ describe('app.currentContext', () => { throw new Error('error message') }) - const handleError = new Promise((resolve, reject) => { + const handleError = new Promise((resolve, reject) => { app.on('error', (err, ctx) => { try { assert.strictEqual(err.message, 'error message') @@ -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((resolve, reject) => { + app.on('error', err => { try { assert.strictEqual(err.message, 'error message') assert.strictEqual(app.currentContext, undefined) diff --git a/__tests__/application/index.ts b/__tests__/application/index.ts index c16ac6dde..361772672 100644 --- a/__tests__/application/index.ts +++ b/__tests__/application/index.ts @@ -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' @@ -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')) }) @@ -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 @@ -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) }) }) diff --git a/__tests__/application/inspect.ts b/__tests__/application/inspect.ts index 2feb96e36..f2c532cbf 100644 --- a/__tests__/application/inspect.ts +++ b/__tests__/application/inspect.ts @@ -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' diff --git a/__tests__/application/onerror.ts b/__tests__/application/onerror.ts index 819f15855..68730b698 100644 --- a/__tests__/application/onerror.ts +++ b/__tests__/application/onerror.ts @@ -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', () => { @@ -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 diff --git a/__tests__/application/request.ts b/__tests__/application/request.ts index a79460f91..a9dcd4595 100644 --- a/__tests__/application/request.ts +++ b/__tests__/application/request.ts @@ -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' @@ -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 }) @@ -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 }) diff --git a/__tests__/application/respond.ts b/__tests__/application/respond.ts index baea1f0ac..921813449 100644 --- a/__tests__/application/respond.ts +++ b/__tests__/application/respond.ts @@ -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' @@ -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(resolve => { setTimeout(() => { res.end('Goodbye') resolve() @@ -386,6 +386,7 @@ describe('app.respond', () => { .expect(700) .expect('custom status') + // @ts-ignore assert.strictEqual(res.res.statusMessage, 'custom status') }) }) @@ -406,6 +407,7 @@ describe('app.respond', () => { .expect(200) .expect('ok') + // @ts-ignore assert.strictEqual(res.res.statusMessage, 'ok') }) }) @@ -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 @@ -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 }) diff --git a/__tests__/application/response.ts b/__tests__/application/response.ts index 61da8f8c7..a24661ee5 100644 --- a/__tests__/application/response.ts +++ b/__tests__/application/response.ts @@ -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' @@ -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 }) @@ -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 }) @@ -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 }) @@ -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) }) @@ -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 = '' @@ -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' }) @@ -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) diff --git a/__tests__/application/toJSON.ts b/__tests__/application/toJSON.ts index e4ff3cdc3..ffd0b3e2d 100644 --- a/__tests__/application/toJSON.ts +++ b/__tests__/application/toJSON.ts @@ -1,5 +1,5 @@ import assert from 'node:assert' -import Koa from '../..' +import Koa from '../../src/application' import { describe, it } from '@jest/globals' diff --git a/__tests__/application/use.ts b/__tests__/application/use.ts index 3833abb7e..fad445af6 100644 --- a/__tests__/application/use.ts +++ b/__tests__/application/use.ts @@ -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' diff --git a/__tests__/context/cookies.ts b/__tests__/context/cookies.ts index ff9edf7bd..3e3539d1e 100644 --- a/__tests__/context/cookies.ts +++ b/__tests__/context/cookies.ts @@ -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' diff --git a/__tests__/context/inspect.ts b/__tests__/context/inspect.ts index 20dffdca4..504d47fef 100644 --- a/__tests__/context/inspect.ts +++ b/__tests__/context/inspect.ts @@ -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' diff --git a/__tests__/context/onerror.ts b/__tests__/context/onerror.ts index 1a41b609a..b65a85213 100644 --- a/__tests__/context/onerror.ts +++ b/__tests__/context/onerror.ts @@ -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' diff --git a/__tests__/context/state.ts b/__tests__/context/state.ts index 78aa955f0..0b82753a6 100644 --- a/__tests__/context/state.ts +++ b/__tests__/context/state.ts @@ -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' diff --git a/__tests__/request/href.ts b/__tests__/request/href.ts index 984d99c9f..6b52735ef 100644 --- a/__tests__/request/href.ts +++ b/__tests__/request/href.ts @@ -2,7 +2,7 @@ import Stream from 'node:stream' import assert from 'node:assert' import http from 'node:http' import createContext from '../../test-helpers/context' -import Koa from '../..' +import Koa from '../../src/application' import { describe, it } from '@jest/globals' diff --git a/__tests__/request/inspect.ts b/__tests__/request/inspect.ts index 984f8f3a5..18fdfe158 100644 --- a/__tests__/request/inspect.ts +++ b/__tests__/request/inspect.ts @@ -4,12 +4,10 @@ import createContext from '../../test-helpers/context' import { describe, it } from '@jest/globals' -const { request } = createContext - describe('req.inspect()', () => { describe('with no request.req present', () => { it('should return null', () => { - const req = request() + const req = createContext.request() req.method = 'GET' delete req.req assert(undefined === req.inspect()) @@ -18,7 +16,7 @@ describe('req.inspect()', () => { }) it('should return a json representation', () => { - const req = request() + const req = createContext.request() req.method = 'GET' req.url = 'example.com' req.header.host = 'example.com' diff --git a/__tests__/request/ip.ts b/__tests__/request/ip.ts index 2e926f99e..533f35a0a 100644 --- a/__tests__/request/ip.ts +++ b/__tests__/request/ip.ts @@ -1,6 +1,6 @@ import Stream from 'node:stream' import assert from 'node:assert' -import Koa from '../..' +import Koa from '../../src/application' import createContext from '../../test-helpers/context' import { describe, it } from '@jest/globals' diff --git a/__tests__/response/attachment.ts b/__tests__/response/attachment.ts index 0b60d34ed..7f8ee5aee 100644 --- a/__tests__/response/attachment.ts +++ b/__tests__/response/attachment.ts @@ -1,6 +1,6 @@ import request from 'supertest' import assert from 'node:assert' -import Koa from '../..' +import Koa from '../../src/application' import createContext from '../../test-helpers/context' import { describe, it } from '@jest/globals' diff --git a/__tests__/response/flushHeaders.ts b/__tests__/response/flushHeaders.ts index fe0a0bcbe..dead2a8e1 100644 --- a/__tests__/response/flushHeaders.ts +++ b/__tests__/response/flushHeaders.ts @@ -1,7 +1,7 @@ import request from 'supertest' import assert from 'node:assert' import http from 'node:http' -import Koa from '../..' +import Koa from '../../src/application' import { describe, it } from '@jest/globals' diff --git a/__tests__/response/header.ts b/__tests__/response/header.ts index 73b8c8e41..364d57318 100644 --- a/__tests__/response/header.ts +++ b/__tests__/response/header.ts @@ -1,6 +1,6 @@ import request from 'supertest' import assert from 'node:assert' -import Koa from '../..' +import Koa from '../../src/application' import createContext from '../../test-helpers/context' import { describe, it } from '@jest/globals' diff --git a/__tests__/response/redirect.ts b/__tests__/response/redirect.ts index 6377d69fa..2915fe2cb 100644 --- a/__tests__/response/redirect.ts +++ b/__tests__/response/redirect.ts @@ -1,6 +1,6 @@ import request from 'supertest' import assert from 'node:assert' -import Koa from '../..' +import Koa from '../../src/application' import createContext from '../../test-helpers/context' import { describe, it } from '@jest/globals' diff --git a/__tests__/response/status.ts b/__tests__/response/status.ts index e7eb0c6e4..a33fe595a 100644 --- a/__tests__/response/status.ts +++ b/__tests__/response/status.ts @@ -1,7 +1,7 @@ import assert from 'node:assert' import request from 'supertest' import statuses from 'statuses' -import Koa from '../..' +import Koa from '../../src/application' import createContext from '../../test-helpers/context' import { describe, it, beforeEach } from '@jest/globals' @@ -47,7 +47,7 @@ describe('res.status=', () => { const res = createContext.response({ httpVersionMajor: 2, httpVersion: '2.0' - }) + } as any) res.status = 200 assert(!res.res.statusMessage) }) diff --git a/__tests__/response/writable.ts b/__tests__/response/writable.ts index 74506bb37..382e8b73a 100644 --- a/__tests__/response/writable.ts +++ b/__tests__/response/writable.ts @@ -1,6 +1,6 @@ import assert from 'node:assert' import net from 'node:net' -import Koa from '../..' +import Koa from '../../src/application' import { describe, it } from '@jest/globals' diff --git a/package-lock.json b/package-lock.json index b62221aca..6c1c09e64 100644 --- a/package-lock.json +++ b/package-lock.json @@ -33,13 +33,15 @@ "devDependencies": { "@jest/globals": "29.7.0", "@types/accepts": "^1.3.7", + "@types/http-errors": "^2.0.4", + "@types/koa-compose": "3.2.8", "@types/node": "^20.14.9", + "@types/on-finished": "^2.3.4", "@types/parseurl": "^1.3.3", "@types/statuses": "2.0.5", "@types/supertest": "^6.0.2", "esbuild": "^0.21.5", "esbuild-jest2": "^0.6.7", - "gen-esm-wrapper": "^1.0.6", "jest": "^29.7.0", "snazzy": "^9.0.0", "standard": "^17.1.0", @@ -1933,18 +1935,79 @@ "@babel/types": "^7.20.7" } }, + "node_modules/@types/body-parser": { + "version": "1.19.5", + "resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.5.tgz", + "integrity": "sha512-fB3Zu92ucau0iQ0JMCFQE7b/dv8Ot07NI3KaZIkIUNXq82k4eBAqUaneXfleGY9JWskeS9y+u0nXMyspcuQrCg==", + "dev": true, + "dependencies": { + "@types/connect": "*", + "@types/node": "*" + } + }, + "node_modules/@types/connect": { + "version": "3.4.38", + "resolved": "https://registry.npmjs.org/@types/connect/-/connect-3.4.38.tgz", + "integrity": "sha512-K6uROf1LD88uDQqJCktA4yzL1YYAK6NgfsI0v/mTgyPKWsX1CnJ0XPSDhViejru1GcRkLWb8RlzFYJRqGUbaug==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, + "node_modules/@types/content-disposition": { + "version": "0.5.8", + "resolved": "https://registry.npmjs.org/@types/content-disposition/-/content-disposition-0.5.8.tgz", + "integrity": "sha512-QVSSvno3dE0MgO76pJhmv4Qyi/j0Yk9pBp0Y7TJ2Tlj+KCgJWY6qX7nnxCOLkZ3VYRSIk1WTxCvwUSdx6CCLdg==", + "dev": true + }, "node_modules/@types/cookiejar": { "version": "2.1.5", "resolved": "https://registry.npmjs.org/@types/cookiejar/-/cookiejar-2.1.5.tgz", "integrity": "sha512-he+DHOWReW0nghN24E1WUqM0efK4kI9oTqDm6XmK8ZPe2djZ90BSNdGnIyCLzCPw7/pogPlGbzI2wHGGmi4O/Q==", "dev": true }, + "node_modules/@types/cookies": { + "version": "0.9.0", + "resolved": "https://registry.npmjs.org/@types/cookies/-/cookies-0.9.0.tgz", + "integrity": "sha512-40Zk8qR147RABiQ7NQnBzWzDcjKzNrntB5BAmeGCb2p/MIyOE+4BVvc17wumsUqUw00bJYqoXFHYygQnEFh4/Q==", + "dev": true, + "dependencies": { + "@types/connect": "*", + "@types/express": "*", + "@types/keygrip": "*", + "@types/node": "*" + } + }, "node_modules/@types/estree": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.5.tgz", "integrity": "sha512-/kYRxGDLWzHOB7q+wtSUQlFrtcdUccpfy+X+9iMBpHK8QLLhx2wIPYuS5DYtR9Wa/YlZAbIovy7qVdB1Aq6Lyw==", "dev": true }, + "node_modules/@types/express": { + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.21.tgz", + "integrity": "sha512-ejlPM315qwLpaQlQDTjPdsUFSc6ZsP4AN6AlWnogPjQ7CVi7PYF3YVz+CY3jE2pwYf7E/7HlDAN0rV2GxTG0HQ==", + "dev": true, + "dependencies": { + "@types/body-parser": "*", + "@types/express-serve-static-core": "^4.17.33", + "@types/qs": "*", + "@types/serve-static": "*" + } + }, + "node_modules/@types/express-serve-static-core": { + "version": "4.19.5", + "resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-4.19.5.tgz", + "integrity": "sha512-y6W03tvrACO72aijJ5uF02FRq5cgDR9lUxddQ8vyF+GvmjJQqbzDcJngEjURc+ZsG31VI3hODNZJ2URj86pzmg==", + "dev": true, + "dependencies": { + "@types/node": "*", + "@types/qs": "*", + "@types/range-parser": "*", + "@types/send": "*" + } + }, "node_modules/@types/graceful-fs": { "version": "4.1.9", "resolved": "https://registry.npmjs.org/@types/graceful-fs/-/graceful-fs-4.1.9.tgz", @@ -1954,6 +2017,18 @@ "@types/node": "*" } }, + "node_modules/@types/http-assert": { + "version": "1.5.5", + "resolved": "https://registry.npmjs.org/@types/http-assert/-/http-assert-1.5.5.tgz", + "integrity": "sha512-4+tE/lwdAahgZT1g30Jkdm9PzFRde0xwxBNUyRsCitRvCQB90iuA2uJYdUnhnANRcqGXaWOGY4FEoxeElNAK2g==", + "dev": true + }, + "node_modules/@types/http-errors": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/@types/http-errors/-/http-errors-2.0.4.tgz", + "integrity": "sha512-D0CFMMtydbJAegzOyHjtiKPLlvnm3iTZyZRSZoLq2mRhDdmLfIWOCYPfQJ4cu2erKghU++QvjcUjp/5h7hESpA==", + "dev": true + }, "node_modules/@types/istanbul-lib-coverage": { "version": "2.0.6", "resolved": "https://registry.npmjs.org/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.6.tgz", @@ -1984,12 +2059,49 @@ "integrity": "sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ==", "dev": true }, + "node_modules/@types/keygrip": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/@types/keygrip/-/keygrip-1.0.6.tgz", + "integrity": "sha512-lZuNAY9xeJt7Bx4t4dx0rYCDqGPW8RXhQZK1td7d4H6E9zYbLoOtjBvfwdTKpsyxQI/2jv+armjX/RW+ZNpXOQ==", + "dev": true + }, + "node_modules/@types/koa": { + "version": "2.15.0", + "resolved": "https://registry.npmjs.org/@types/koa/-/koa-2.15.0.tgz", + "integrity": "sha512-7QFsywoE5URbuVnG3loe03QXuGajrnotr3gQkXcEBShORai23MePfFYdhz90FEtBBpkyIYQbVD+evKtloCgX3g==", + "dev": true, + "dependencies": { + "@types/accepts": "*", + "@types/content-disposition": "*", + "@types/cookies": "*", + "@types/http-assert": "*", + "@types/http-errors": "*", + "@types/keygrip": "*", + "@types/koa-compose": "*", + "@types/node": "*" + } + }, + "node_modules/@types/koa-compose": { + "version": "3.2.8", + "resolved": "https://registry.npmjs.org/@types/koa-compose/-/koa-compose-3.2.8.tgz", + "integrity": "sha512-4Olc63RY+MKvxMwVknCUDhRQX1pFQoBZ/lXcRLP69PQkEpze/0cr8LNqJQe5NFb/b19DWi2a5bTi2VAlQzhJuA==", + "dev": true, + "dependencies": { + "@types/koa": "*" + } + }, "node_modules/@types/methods": { "version": "1.1.4", "resolved": "https://registry.npmjs.org/@types/methods/-/methods-1.1.4.tgz", "integrity": "sha512-ymXWVrDiCxTBE3+RIrrP533E70eA+9qu7zdWoHuOmGujkYtzf4HQF96b8nwHLqhuf4ykX61IGRIB38CC6/sImQ==", "dev": true }, + "node_modules/@types/mime": { + "version": "1.3.5", + "resolved": "https://registry.npmjs.org/@types/mime/-/mime-1.3.5.tgz", + "integrity": "sha512-/pyBZWSLD2n0dcHE3hq8s8ZvcETHtEuF+3E7XVt0Ig2nvsVQXdghHVcEkIWjy9A0wKfTn97a/PSDYohKIlnP/w==", + "dev": true + }, "node_modules/@types/node": { "version": "20.14.9", "resolved": "https://registry.npmjs.org/@types/node/-/node-20.14.9.tgz", @@ -1999,6 +2111,15 @@ "undici-types": "~5.26.4" } }, + "node_modules/@types/on-finished": { + "version": "2.3.4", + "resolved": "https://registry.npmjs.org/@types/on-finished/-/on-finished-2.3.4.tgz", + "integrity": "sha512-Ld4UQD3udYcKPaAWlI1EYXKhefkZcTlpqOLkQRmN3u5Ml/tUypMivUHbNH8LweP4H4FlhGGO+uBjJI1Y1dkE1g==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/parseurl": { "version": "1.3.3", "resolved": "https://registry.npmjs.org/@types/parseurl/-/parseurl-1.3.3.tgz", @@ -2008,6 +2129,39 @@ "@types/node": "*" } }, + "node_modules/@types/qs": { + "version": "6.9.15", + "resolved": "https://registry.npmjs.org/@types/qs/-/qs-6.9.15.tgz", + "integrity": "sha512-uXHQKES6DQKKCLh441Xv/dwxOq1TVS3JPUMlEqoEglvlhR6Mxnlew/Xq/LRVHpLyk7iK3zODe1qYHIMltO7XGg==", + "dev": true + }, + "node_modules/@types/range-parser": { + "version": "1.2.7", + "resolved": "https://registry.npmjs.org/@types/range-parser/-/range-parser-1.2.7.tgz", + "integrity": "sha512-hKormJbkJqzQGhziax5PItDUTMAM9uE2XXQmM37dyd4hVM+5aVl7oVxMVUiVQn2oCQFN/LKCZdvSM0pFRqbSmQ==", + "dev": true + }, + "node_modules/@types/send": { + "version": "0.17.4", + "resolved": "https://registry.npmjs.org/@types/send/-/send-0.17.4.tgz", + "integrity": "sha512-x2EM6TJOybec7c52BX0ZspPodMsQUd5L6PRwOunVyVUhXiBSKf3AezDL8Dgvgt5o0UfKNfuA0eMLr2wLT4AiBA==", + "dev": true, + "dependencies": { + "@types/mime": "^1", + "@types/node": "*" + } + }, + "node_modules/@types/serve-static": { + "version": "1.15.7", + "resolved": "https://registry.npmjs.org/@types/serve-static/-/serve-static-1.15.7.tgz", + "integrity": "sha512-W8Ym+h8nhuRwaKPaDw34QUkwsGi6Rc4yYqvKFo5rm2FUEhCFbzVWrxXUxuKK8TASjWsysJY0nsmNCGhCOIsrOw==", + "dev": true, + "dependencies": { + "@types/http-errors": "*", + "@types/node": "*", + "@types/send": "*" + } + }, "node_modules/@types/stack-utils": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/@types/stack-utils/-/stack-utils-2.0.3.tgz", @@ -2368,16 +2522,6 @@ "integrity": "sha512-BSHWgDSAiKs50o2Re8ppvp3seVHXSRM44cdSsT9FfNEUUZLOGWVCsiWaRPWM1Znn+mqZ1OfVZ3z3DWEzSp7hRA==", "dev": true }, - "node_modules/assert": { - "version": "1.5.0", - "resolved": "https://registry.npmjs.org/assert/-/assert-1.5.0.tgz", - "integrity": "sha512-EDsgawzwoun2CZkCgtxJbv392v4nbk9XDD06zI+kQYoBM/3RBWLlEyJARDOmhAAosBjWACEkKL6S+lIZtcAubA==", - "dev": true, - "dependencies": { - "object-assign": "^4.1.1", - "util": "0.10.3" - } - }, "node_modules/astral-regex": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/astral-regex/-/astral-regex-2.0.0.tgz", @@ -4380,18 +4524,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/gen-esm-wrapper": { - "version": "1.1.3", - "resolved": "https://registry.npmjs.org/gen-esm-wrapper/-/gen-esm-wrapper-1.1.3.tgz", - "integrity": "sha512-LNHZ+QpaCW/0VhABIbXn45V+P8kFvjjwuue9hbV23eOjuFVz6c0FE3z1XpLX9pSjLW7UmtCkXo5F9vhZWVs8oQ==", - "dev": true, - "dependencies": { - "is-valid-identifier": "^2.0.2" - }, - "bin": { - "gen-esm-wrapper": "gen-esm-wrapper.js" - } - }, "node_modules/gensync": { "version": "1.0.0-beta.2", "resolved": "https://registry.npmjs.org/gensync/-/gensync-1.0.0-beta.2.tgz", @@ -5170,15 +5302,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/is-valid-identifier": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/is-valid-identifier/-/is-valid-identifier-2.0.2.tgz", - "integrity": "sha512-mpS5EGqXOwzXtKAg6I44jIAqeBfntFLxpAth1rrKbxtKyI6LPktyDYpHBI+tHlduhhX/SF26mFXmxQu995QVqg==", - "dev": true, - "dependencies": { - "assert": "^1.4.1" - } - }, "node_modules/is-weakmap": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/is-weakmap/-/is-weakmap-2.0.2.tgz", @@ -8714,27 +8837,12 @@ "punycode": "^2.1.0" } }, - "node_modules/util": { - "version": "0.10.3", - "resolved": "https://registry.npmjs.org/util/-/util-0.10.3.tgz", - "integrity": "sha1-evsa/lCAUkZInj23/g7TeTNqwPk=", - "dev": true, - "dependencies": { - "inherits": "2.0.1" - } - }, "node_modules/util-deprecate": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==", "dev": true }, - "node_modules/util/node_modules/inherits": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.1.tgz", - "integrity": "sha1-sX0I0ya0Qj5Wjv9xn5GwscvfafE=", - "dev": true - }, "node_modules/v8-compile-cache": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/v8-compile-cache/-/v8-compile-cache-2.3.0.tgz", diff --git a/package.json b/package.json index 65385b74a..461ee9ef3 100644 --- a/package.json +++ b/package.json @@ -5,14 +5,12 @@ "tag": "experimental" }, "description": "Koa web app framework", - "main": "lib/application.js", + "main": "dist/application.mjs", "exports": { ".": { - "require": "./lib/application.js", - "import": "./dist/koa.mjs" + "require": "./dist/application.js", + "import": "./dist/application.mjs" }, - "./*": "./*.js", - "./*.js": "./*.js", "./package": "./package.json", "./package.json": "./package.json" }, @@ -21,7 +19,7 @@ "lint": "standard", "lint:pretty": "standard | snazzy", "authors": "git log --format='%aN <%aE>' | sort -u > AUTHORS", - "build": "gen-esm-wrapper . ./dist/koa.mjs", + "build": "tsup", "prepare": "npm run build" }, "repository": "koajs/koa", @@ -39,7 +37,7 @@ "accepts": "^1.3.8", "cache-content-type": "^1.0.1", "content-disposition": "~0.5.4", - "content-type": "^1.0.4", + "content-type": "^1.0.5", "cookies": "~0.9.1", "debug": "^4.3.5", "delegates": "^1.0.0", @@ -63,9 +61,11 @@ "@types/accepts": "^1.3.7", "@types/parseurl": "^1.3.3", "@types/statuses": "2.0.5", + "@types/http-errors": "^2.0.4", + "@types/on-finished": "^2.3.4", + "@types/koa-compose": "3.2.8", "esbuild": "^0.21.5", "esbuild-jest2": "^0.6.7", - "gen-esm-wrapper": "^1.0.6", "@jest/globals": "29.7.0", "jest": "^29.7.0", "snazzy": "^9.0.0", diff --git a/lib/application.js b/src/application.ts similarity index 69% rename from lib/application.js rename to src/application.ts index bbee69067..dbd477305 100644 --- a/lib/application.js +++ b/src/application.ts @@ -1,75 +1,83 @@ -'use strict' +export { HttpError } from 'http-errors' -/** - * Module dependencies. - */ +import util from 'node:util' +import http from 'node:http' +import assert from 'node:assert' +import Emitter from 'node:events' +import Stream from 'node:stream' +import { AsyncLocalStorage } from 'node:async_hooks' + +import debug from 'debug'; +import onFinished from 'on-finished' +import compose from 'koa-compose' +import statuses from 'statuses' + +import response, { type KoaResponse } from './response' +import request, { type KoaRequest } from './request' +import context, { type KoaContext } from './context' + +import { only } from './utils' -const debug = require('debug')('koa:application') -const assert = require('assert') -const onFinished = require('on-finished') -const response = require('./response') -const compose = require('koa-compose') -const context = require('./context') -const request = require('./request') -const statuses = require('statuses') -const Emitter = require('events') -const util = require('util') -const Stream = require('stream') -const http = require('http') -const only = require('only') -const { HttpError } = require('http-errors') - -/** @typedef {typeof import ('./context') & { - * app: Application - * req: import('http').IncomingMessage - * res: import('http').ServerResponse - * request: KoaRequest - * response: KoaResponse - * state: any - * originalUrl: string - * }} Context */ - -/** @typedef {typeof import('./request')} KoaRequest */ - -/** @typedef {typeof import('./response')} KoaResponse */ +const httpDebug = debug('koa:application') /** * Expose `Application` class. * Inherits from `Emitter.prototype`. */ -module.exports = class Application extends Emitter { +export type KoaOptions = { + /** Environment */ + env: string; + /** Signed cookie keys */ + keys: string[]; + /** Trust proxy headers */ + proxy: boolean; + /** Subdomain offset */ + subdomainOffset: number; + /** Proxy IP header, defaults to X-Forwarded-For */ + proxyIpHeader: string; + /** Max IPs read from proxy IP header, default to 0 (means infinity) */ + maxIpsCount: number; + /** No data */ + asyncLocalStorage: boolean; + + /** Function to handle middleware composition */ + compose(args: any): void; +} + +export default class Application extends Emitter { /** * Initialize a new `Application`. * * @api public */ - /** - * - * @param {object} [options] Application options - * @param {string} [options.env='development'] Environment - * @param {string[]} [options.keys] Signed cookie keys - * @param {boolean} [options.proxy] Trust proxy headers - * @param {number} [options.subdomainOffset] Subdomain offset - * @param {string} [options.proxyIpHeader] Proxy IP header, defaults to X-Forwarded-For - * @param {number} [options.maxIpsCount] Max IPs read from proxy IP header, default to 0 (means infinity) - * @param {function} [options.compose] Function to handle middleware composition - * @param {boolean} [options.asyncLocalStorage] Max IPs read from proxy IP header, default to 0 (means infinity) - * - */ - - constructor (options) { + public proxy: boolean; + public subdomainOffset: number; + public proxyIpHeader: string; + public maxIpsCount: number; + public env: string; + public keys: string[]; + public silent: boolean; + public ctxStorage: AsyncLocalStorage; + + public compose: any; + public context: KoaContext; + public request: KoaRequest; + public response: KoaResponse; + + public middleware = []; + + constructor (options?: KoaOptions) { super() - options = options || {} - this.proxy = options.proxy || false - this.subdomainOffset = options.subdomainOffset || 2 - this.proxyIpHeader = options.proxyIpHeader || 'X-Forwarded-For' - this.maxIpsCount = options.maxIpsCount || 0 - this.env = options.env || process.env.NODE_ENV || 'development' - this.compose = options.compose || compose - if (options.keys) this.keys = options.keys - this.middleware = [] + + this.proxy = options?.proxy || false + this.subdomainOffset = options?.subdomainOffset || 2 + this.proxyIpHeader = options?.proxyIpHeader || 'X-Forwarded-For' + this.maxIpsCount = options?.maxIpsCount || 0 + this.env = options?.env || process.env.NODE_ENV || 'development' + this.compose = options?.compose || compose + if (options?.keys) this.keys = options.keys this.context = Object.create(context) this.request = Object.create(request) this.response = Object.create(response) @@ -78,8 +86,7 @@ module.exports = class Application extends Emitter { if (util.inspect.custom) { this[util.inspect.custom] = this.inspect } - if (options.asyncLocalStorage) { - const { AsyncLocalStorage } = require('async_hooks') + if (options?.asyncLocalStorage) { assert(AsyncLocalStorage, 'Requires node 12.17.0 or higher to enable asyncLocalStorage') this.ctxStorage = new AsyncLocalStorage() } @@ -96,7 +103,7 @@ module.exports = class Application extends Emitter { */ listen (...args) { - debug('listen') + httpDebug('listen') const server = http.createServer(this.callback()) return server.listen(...args) } @@ -140,7 +147,7 @@ module.exports = class Application extends Emitter { use (fn) { if (typeof fn !== 'function') throw new TypeError('middleware must be a function!') - debug('use %s', fn._name || fn.name || '-') + httpDebug('use %s', fn._name || fn.name || '-') this.middleware.push(fn) return this } @@ -250,6 +257,8 @@ module.exports = class Application extends Emitter { } } +export type KoaApplication = typeof Application + /** * Response helper. */ @@ -311,10 +320,3 @@ function respond (ctx) { } res.end(body) } - -/** - * Make HttpError available to consumers of the library so that consumers don't - * have a direct dependency upon `http-errors` - */ - -module.exports.HttpError = HttpError diff --git a/lib/context.js b/src/context.ts similarity index 88% rename from lib/context.js rename to src/context.ts index 83b4d8f9b..a3ffd8286 100644 --- a/lib/context.js +++ b/src/context.ts @@ -1,15 +1,9 @@ -'use strict' - -/** - * Module dependencies. - */ - -const util = require('util') -const createError = require('http-errors') -const httpAssert = require('http-assert') -const delegate = require('delegates') -const statuses = require('statuses') -const Cookies = require('cookies') +import util from 'node:util' +import createError from 'http-errors' +import httpAssert from 'http-assert' +import delegate from 'delegates' +import statuses from 'statuses' +import Cookies from 'cookies' const COOKIES = Symbol('context#cookies') @@ -17,7 +11,7 @@ const COOKIES = Symbol('context#cookies') * Context prototype. */ -const proto = module.exports = { +const context = { /** * util.inspect() implementation, which @@ -28,7 +22,7 @@ const proto = module.exports = { */ inspect () { - if (this === proto) return this + if (this === context) return this return this.toJSON() }, @@ -179,23 +173,11 @@ const proto = module.exports = { } } -/** - * Custom inspection implementation for newer Node.js versions. - * - * @return {Object} - * @api public - */ - -/* istanbul ignore else */ -if (util.inspect.custom) { - module.exports[util.inspect.custom] = module.exports.inspect -} - /** * Response delegation. */ -delegate(proto, 'response') +delegate(context, 'response') .method('attachment') .method('redirect') .method('remove') @@ -218,7 +200,7 @@ delegate(proto, 'response') * Request delegation. */ -delegate(proto, 'request') +delegate(context, 'request') .method('acceptsLanguages') .method('acceptsEncodings') .method('acceptsCharsets') @@ -248,3 +230,6 @@ delegate(proto, 'request') .getter('fresh') .getter('ips') .getter('ip') + +export type KoaContext = typeof context +export default context diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 000000000..e69de29bb diff --git a/lib/request.js b/src/request.ts similarity index 94% rename from lib/request.js rename to src/request.ts index 7a1a07c12..4e6b0e3c8 100644 --- a/lib/request.js +++ b/src/request.ts @@ -1,20 +1,14 @@ -'use strict' +import net from 'node:net' +import qs from 'node:querystring' +import { URL, format } from 'node:url' -/** - * Module dependencies. - */ +import accepts from 'accepts' +import contentType from 'content-type' +import parse from 'parseurl' +import fresh from 'fresh' +import typeis from 'type-is' -const URL = require('url').URL -const net = require('net') -const accepts = require('accepts') -const contentType = require('content-type') -const stringify = require('url').format -const parse = require('parseurl') -const qs = require('querystring') -const typeis = require('type-is') -const fresh = require('fresh') -const only = require('only') -const util = require('util') +import { only } from './utils' const IP = Symbol('context#ip') @@ -22,7 +16,7 @@ const IP = Symbol('context#ip') * Prototype. */ -module.exports = { +const request = { /** * Return request header. @@ -158,7 +152,7 @@ module.exports = { url.pathname = path url.path = null - this.url = stringify(url) + this.url = format(url) }, /** @@ -208,10 +202,11 @@ module.exports = { const url = parse(this.req) if (url.search === `?${str}`) return + // @ts-ignore url.search = str url.path = null - this.url = stringify(url) + this.url = format(url) }, /** @@ -712,14 +707,5 @@ module.exports = { } } -/** - * Custom inspection implementation for newer Node.js versions. - * - * @return {Object} - * @api public - */ - -/* istanbul ignore else */ -if (util.inspect.custom) { - module.exports[util.inspect.custom] = module.exports.inspect -} +export type KoaRequest = typeof request +export default request diff --git a/lib/response.js b/src/response.ts similarity index 92% rename from lib/response.js rename to src/response.ts index cbe587142..b696adda2 100644 --- a/lib/response.js +++ b/src/response.ts @@ -1,29 +1,24 @@ -'use strict' - -/** - * Module dependencies. - */ - -const contentDisposition = require('content-disposition') -const getType = require('cache-content-type') -const onFinish = require('on-finished') -const escape = require('escape-html') -const typeis = require('type-is').is -const statuses = require('statuses') -const destroy = require('destroy') -const assert = require('assert') -const extname = require('path').extname -const vary = require('vary') -const only = require('only') -const util = require('util') -const encodeUrl = require('encodeurl') -const Stream = require('stream') +import Stream from 'node:stream' +import assert from 'node:assert' +import { extname } from 'node:path' + +import vary from 'vary' +import typeis from 'type-is' +import onFinished from 'on-finished' +import statuses from 'statuses' +import escape from 'escape-html' +import getType from 'cache-content-type' +import encodeUrl from 'encodeurl' +import destroy from 'destroy' +import contentDisposition from 'content-disposition' + +import { only } from './utils' /** * Prototype. */ -module.exports = { +const response = { /** * Return the request socket. @@ -172,7 +167,7 @@ module.exports = { // stream if (val instanceof Stream) { - onFinish(this.res, destroy.bind(null, val)) + onFinished(this.res, destroy.bind(null, val)) if (original !== val) { val.once('error', err => this.ctx.onerror(err)) // overwriting @@ -408,7 +403,7 @@ module.exports = { */ is (type, ...types) { - return typeis(this.type, type, ...types) + return typeis.is(this.type, type, ...types) }, /** @@ -587,14 +582,5 @@ module.exports = { } } -/** - * Custom inspection implementation for node 6+. - * - * @return {Object} - * @api public - */ - -/* istanbul ignore else */ -if (util.inspect.custom) { - module.exports[util.inspect.custom] = module.exports.inspect -} +export type KoaResponse = typeof response +export default response diff --git a/src/utils/index.ts b/src/utils/index.ts new file mode 100644 index 000000000..d14f0b03d --- /dev/null +++ b/src/utils/index.ts @@ -0,0 +1 @@ +export * from './only' diff --git a/src/utils/only.ts b/src/utils/only.ts new file mode 100644 index 000000000..497042ff8 --- /dev/null +++ b/src/utils/only.ts @@ -0,0 +1,10 @@ +export function only(obj: object, keys){ + obj = obj || {}; + if ('string' == typeof keys) keys = keys.split(/ +/); + + return keys.reduce(function(ret, key){ + if (null == obj[key]) return ret; + ret[key] = obj[key]; + return ret; + }, {}); +}; diff --git a/test-helpers/context.ts b/test-helpers/context.ts index f6503bce9..86b66e2a4 100644 --- a/test-helpers/context.ts +++ b/test-helpers/context.ts @@ -1,18 +1,30 @@ import Stream from 'node:stream' -import Koa from '../lib/application' +import Koa, { type KoaApplication } from '../src/application' +import type { IncomingMessage, ServerResponse } from 'node:http' -const createContext = (req, res, app) => { +const createContext = (req?: IncomingMessage, res?: ServerResponse, app?: KoaApplication) => { const socket = new Stream.Duplex() + req = Object.assign({ headers: {}, socket }, Stream.Readable.prototype, req) res = Object.assign({ _headers: {}, socket }, Stream.Writable.prototype, res) + // @ts-ignore req.socket.remoteAddress = req.socket.remoteAddress || '127.0.0.1' + + // @ts-ignore app = app || new Koa() - res.getHeader = k => res._headers[k.toLowerCase()] - res.setHeader = (k, v) => { res._headers[k.toLowerCase()] = v } - res.removeHeader = (k, v) => delete res._headers[k.toLowerCase()] + + // @ts-ignore + res.getHeader = (k: string) => res._headers[k.toLowerCase()] + // @ts-ignore + res.setHeader = (k: string, v: string) => { res._headers[k.toLowerCase()] = v } + // @ts-ignore + res.removeHeader = (k: string) => delete res._headers[k.toLowerCase()] + + // @ts-ignore return app.createContext(req, res) } -module.exports = createContext -module.exports.request = (req, res, app) => createContext(req, res, app).request -module.exports.response = (req, res, app) => createContext(req, res, app).response +createContext.request = (req?: IncomingMessage, res?: ServerResponse, app?: KoaApplication) => createContext(req, res, app).request +createContext.response = (req?: IncomingMessage, res?: ServerResponse, app?: KoaApplication) => createContext(req, res, app).response + +export default createContext diff --git a/tsconfig.json b/tsconfig.json index e075f973c..315a86a86 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,7 +11,7 @@ // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ /* Language and Environment */ - "target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ + "target": "es2022", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ // "jsx": "preserve", /* Specify what JSX code is generated. */ // "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */ @@ -25,11 +25,13 @@ // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ /* Modules */ - "module": "commonjs", /* Specify what module code is generated. */ + "module": "NodeNext", /* Specify what module code is generated. */ // "rootDir": "./", /* Specify the root folder within your source files. */ // "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */ - // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ - // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ + "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ + "paths": { + "@/*": ["src/*"] + }, /* Specify a set of entries that re-map imports to additional lookup locations. */ // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ // "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */ // "types": [], /* Specify type package names to be included without being referenced in a source file. */ @@ -82,7 +84,7 @@ "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */ /* Type Checking */ - "strict": true, /* Enable all strict type-checking options. */ + "strict": false, /* Enable all strict type-checking options. */ // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */ // "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */ // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */ diff --git a/tsup.config.ts b/tsup.config.ts new file mode 100644 index 000000000..80c5505e8 --- /dev/null +++ b/tsup.config.ts @@ -0,0 +1,9 @@ +import { defineConfig } from 'tsup' + +export default defineConfig({ + entry: ['src/application.ts'], + format: ['cjs', 'esm'], + target: 'es2022', + dts: true, + clean: true +})