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

refactor: convert more tests to typescript #5379

Open
wants to merge 17 commits into
base: next
Choose a base branch
from
58 changes: 0 additions & 58 deletions index-old.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,30 +218,6 @@

// export type LoggerLevel = EggLoggerLevel;


// /**
// * egg app info
// * @example
// * ```js
// * // config/config.default.ts
// * import { EggAppInfo } from 'egg';
// *
// * export default (appInfo: EggAppInfo) => {
// * return {
// * keys: appInfo.name + '123456',
// * };
// * }
// * ```
// */
// export interface EggAppInfo {
// pkg: any; // package.json
// name: string; // the application name from package.json
// baseDir: string; // current directory of application
// env: EggEnvType; // equals to serverEnv
// HOME: string; // home directory of the OS
// root: string; // baseDir when local and unittest, HOME when other environment
// }

// type IgnoreItem = string | RegExp | ((ctx: Context) => boolean);
// type IgnoreOrMatch = IgnoreItem | IgnoreItem[];

Expand Down Expand Up @@ -1082,40 +1058,6 @@
// urlFor(name: string, params?: PlainObject): string;
// }

// // egg env type
// export type EggEnvType = 'local' | 'unittest' | 'prod' | string;

// /**
// * plugin config item interface
// */
// export interface IEggPluginItem {
// env?: EggEnvType[];
// path?: string;
// package?: string;
// enable?: boolean;
// }

// export type EggPluginItem = IEggPluginItem | boolean;

// /**
// * build-in plugin list
// */
// export interface EggPlugin {
// [key: string]: EggPluginItem | undefined;
// onerror?: EggPluginItem;
// session?: EggPluginItem;
// i18n?: EggPluginItem;
// watcher?: EggPluginItem;
// multipart?: EggPluginItem;
// security?: EggPluginItem;
// development?: EggPluginItem;
// logrotator?: EggPluginItem;
// schedule?: EggPluginItem;
// static?: EggPluginItem;
// jsonp?: EggPluginItem;
// view?: EggPluginItem;
// }

// /**
// * Singleton instance in Agent Worker, extend {@link EggApplication}
// */
Expand Down
16 changes: 7 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "egg",
"version": "4.0.0-beta.11",
"version": "4.0.0-beta.15",
"engines": {
"node": ">= 18.19.0"
},
Expand All @@ -21,13 +21,12 @@
"dependencies": {
"@eggjs/cluster": "^3.0.0",
"@eggjs/cookies": "^3.0.0",
"@eggjs/core": "^6.2.5",
"@eggjs/core": "^6.2.13",
"@eggjs/schedule": "^5.0.2",
"@eggjs/utils": "^4.1.5",
"@eggjs/watcher": "^4.0.1",
"@eggjs/utils": "^4.2.4",
"@eggjs/watcher": "^4.0.3",
"circular-json-for-egg": "^1.0.0",
"cluster-client": "^3.7.0",
"delegates": "^1.0.0",
"egg-development": "^3.0.0",
"egg-errors": "^2.3.1",
"egg-i18n": "^2.1.1",
Expand All @@ -46,7 +45,7 @@
"is-type-of": "^2.1.0",
"koa-bodyparser": "^4.4.1",
"koa-override": "^4.0.0",
"onelogger": "^1.0.0",
"onelogger": "^1.0.1",
"performance-ms": "^1.1.0",
"sendmessage": "^3.0.1",
"urllib": "^4.6.11",
Expand All @@ -57,10 +56,9 @@
"@arethetypeswrong/cli": "^0.17.1",
"@eggjs/bin": "^7.0.0",
"@eggjs/koa": "^2.19.1",
"@eggjs/mock": "^6.0.3",
"@eggjs/mock": "^6.0.5",
"@eggjs/supertest": "^8.1.1",
"@eggjs/tsconfig": "1",
"@types/delegates": "^1.0.3",
"@types/koa-bodyparser": "^4.3.12",
"@types/mocha": "^10.0.7",
"@types/ms": "^0.7.34",
Expand All @@ -78,7 +76,7 @@
"formstream": "^1.5.1",
"koa-static": "^5.0.0",
"mm": "^3.4.0",
"pedding": "^1.1.0",
"pedding": "^2.0.1",
"prettier": "^2.7.1",
"rimraf": "6",
"runscript": "^2.0.1",
Expand Down
60 changes: 33 additions & 27 deletions src/app/extend/context.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import delegate from 'delegates';
import { assign } from 'utility';
import { now, diff } from 'performance-ms';
import {
utils, Context as EggCoreContext, Router,
type ContextDelegation as EggCoreContextDelegation,
} from '@eggjs/core';
import type { Cookies as ContextCookies } from '@eggjs/cookies';
import { EggLogger } from 'egg-logger';
import type { EggLogger } from 'egg-logger';
import type { Application } from '../../lib/application.js';
import type {
HttpClientRequestURL, HttpClientRequestOptions, HttpClient,
} from '../../lib/core/httpclient.js';
import type { BaseContextClass } from '../../lib//core/base_context_class.js';
import Request from './request.js';
import Response from './response.js';
import type Request from './request.js';
import type Response from './response.js';
import type Helper from './helper.js';

import './context.types.js';
Expand All @@ -33,7 +31,9 @@
export default class Context extends EggCoreContext {
declare app: Application;
declare request: Request;
declare response: Response;

Check warning on line 34 in src/app/extend/context.ts

View check run for this annotation

Codecov / codecov/patch

src/app/extend/context.ts#L34

Added line #L34 was not covered by tests
declare service: BaseContextClass;
declare proxy: any;

Check warning on line 36 in src/app/extend/context.ts

View check run for this annotation

Codecov / codecov/patch

src/app/extend/context.ts#L36

Added line #L36 was not covered by tests

/**
* Request start time
Expand Down Expand Up @@ -230,7 +230,7 @@
* });
* ```
*/
runInBackground(scope: (ctx: ContextDelegation) => Promise<void>, taskName?: string): void {
runInBackground(scope: (ctx: Context) => Promise<void>, taskName?: string): void {
// try to use custom function name first
if (!taskName) {
taskName = Reflect.get(scope, '_name') || scope.name || utils.getCalleeFromStack(true);
Expand All @@ -243,7 +243,7 @@

// let plugins or frameworks to reuse _runInBackground in some cases.
// e.g.: https://github.com/eggjs/egg-mock/pull/78
async _runInBackground(scope: (ctx: ContextDelegation) => Promise<void>, taskName: string) {
async _runInBackground(scope: (ctx: Context) => Promise<void>, taskName: string) {
const startTime = now();
try {
await scope(this as any);
Expand All @@ -257,46 +257,52 @@
this.app.emit('error', err, this);
}
}
}

/**
* Context delegation.
*/

delegate(Context.prototype, 'request')
/**
* @member {Boolean} Context#acceptJSON
* @see Request#acceptJSON
* @since 1.0.0
*/
.getter('acceptJSON')
get acceptJSON(): boolean {
return this.request.acceptJSON;
}

Check warning on line 269 in src/app/extend/context.ts

View check run for this annotation

Codecov / codecov/patch

src/app/extend/context.ts#L269

Added line #L269 was not covered by tests
get query(): Record<string, string> {
return this.request.query;
}

Check warning on line 273 in src/app/extend/context.ts

View check run for this annotation

Codecov / codecov/patch

src/app/extend/context.ts#L273

Added line #L273 was not covered by tests
/**
* @member {Array} Context#queries
* @see Request#queries
* @since 1.0.0
*/
.getter('queries')
/**
* @member {Boolean} Context#accept
* @see Request#accept
* @since 1.0.0
*/
.getter('accept')
get queries(): Record<string, string[]> {
return this.request.queries;
}

Check warning on line 282 in src/app/extend/context.ts

View check run for this annotation

Codecov / codecov/patch

src/app/extend/context.ts#L282

Added line #L282 was not covered by tests
/**
* @member {string} Context#ip
* @see Request#ip
* @since 1.0.0
*/
.access('ip');
get ip(): string {
return this.request.ip;
}

Check warning on line 291 in src/app/extend/context.ts

View check run for this annotation

Codecov / codecov/patch

src/app/extend/context.ts#L291

Added line #L291 was not covered by tests
set ip(val: string) {
this.request.ip = val;
}

delegate(Context.prototype, 'response')
/**
* @member {Number} Context#realStatus
* @see Response#realStatus
* @since 1.0.0
*/
.access('realStatus');
get realStatus(): number {
return this.response.realStatus;
}

export type ContextDelegation = EggCoreContextDelegation & Context
& Pick<Request, 'acceptJSON' | 'queries' | 'accept' | 'ip'>
& Pick<Response, 'realStatus'>;
set realStatus(val: number) {
this.response.realStatus = val;
}
}

Check warning on line 308 in src/app/extend/context.ts

View check run for this annotation

Codecov / codecov/patch

src/app/extend/context.ts#L308

Added line #L308 was not covered by tests
5 changes: 4 additions & 1 deletion src/app/extend/context.types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type {
Router,
} from '@eggjs/core';
import type { EggLogger } from 'egg-logger';
import type {
HttpClientRequestURL, HttpClientRequestOptions, HttpClient,
} from '../../lib/core/httpclient.js';
import type Helper from './helper.js';
import type { EggLogger } from 'egg-logger';

declare module '@eggjs/core' {
// add Context overrides types
Expand All @@ -17,5 +17,8 @@ declare module '@eggjs/core' {
get httpclient(): HttpClient;
get httpClient(): HttpClient;
getLogger(name: string): EggLogger;
get logger(): EggLogger;
get coreLogger(): EggLogger;
get locals(): Record<string, any>;
}
}
11 changes: 9 additions & 2 deletions src/app/extend/request.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import querystring from 'node:querystring';
import { Request as EggCoreRequest } from '@eggjs/core';
import type { Application } from '../../lib/application.js';
import type { ContextDelegation } from './context.js';
import type Context from './context.js';
import Response from './response.js';

const QUERY_CACHE = Symbol('request query cache');
Expand All @@ -11,11 +11,18 @@ const HOST = Symbol('request host');
const IPS = Symbol('request ips');
const RE_ARRAY_KEY = /[^\[\]]+\[\]$/;

import './request.types.js';

export default class Request extends EggCoreRequest {
declare app: Application;
declare ctx: ContextDelegation;
declare ctx: Context;
declare response: Response;

/**
* Request body, parsed from koa-bodyparser or egg-multipart
*/
declare body: any;

/**
* Parse the "Host" header field host
* and support X-Forwarded-Host when a
Expand Down
10 changes: 10 additions & 0 deletions src/app/extend/request.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
declare module '@eggjs/core' {
// add Request overrides types
interface Request {
body: any;
get acceptJSON(): boolean;
get query(): Record<string, string>;
set query(obj: Record<string, string>);
get queries(): Record<string, string[]>;
}
}
2 changes: 2 additions & 0 deletions src/app/extend/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Response as KoaResponse } from '@eggjs/core';

const REAL_STATUS = Symbol('response realStatus');

import './response.types.js';

export default class Response extends KoaResponse {
/**
* Get or set a real status code.
Expand Down
7 changes: 7 additions & 0 deletions src/app/extend/response.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
declare module '@eggjs/core' {
// add Response overrides types
interface Response {
get realStatus(): number;
set realStatus(status: number);
}
}
4 changes: 2 additions & 2 deletions src/app/middleware/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
*/

import { performance } from 'node:perf_hooks';
import type { ContextDelegation, Next } from '../../lib/egg.js';
import type { Context, Next } from '../../lib/egg.js';

export interface MetaMiddlewareOptions {
enable: boolean;
logging: boolean;
}

export default (options: MetaMiddlewareOptions) => {
return async function meta(ctx: ContextDelegation, next: Next) {
return async function meta(ctx: Context, next: Next) {
if (options.logging) {
ctx.coreLogger.info('[meta] request started, host: %s, user-agent: %s',
ctx.host, ctx.header['user-agent']);
Expand Down
4 changes: 2 additions & 2 deletions src/app/middleware/notfound.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { Next, ContextDelegation } from '../../lib/egg.js';
import type { Next, Context } from '../../lib/egg.js';

export interface NotFoundMiddlewareOptions {
enable: boolean;
pageUrl: string;
}

export default (options: NotFoundMiddlewareOptions) => {
return async function notfound(ctx: ContextDelegation, next: Next) {
return async function notfound(ctx: Context, next: Next) {
await next();

if (ctx.status !== 404 || ctx.body) {
Expand Down
6 changes: 3 additions & 3 deletions src/app/middleware/site_file.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { readFile } from 'node:fs/promises';
import type { Next, ContextDelegation } from '../../lib/egg.js';
import type { Next, Context } from '../../lib/egg.js';

export type SiteFileContentFun = (ctx: ContextDelegation) => Promise<Buffer | string>;
export type SiteFileContentFun = (ctx: Context) => Promise<Buffer | string>;

export interface SiteFileMiddlewareOptions {
enable: boolean;
Expand All @@ -14,7 +14,7 @@ export interface SiteFileMiddlewareOptions {
const BUFFER_CACHE = Symbol('siteFile URL buffer cache');

export default (options: SiteFileMiddlewareOptions) => {
return async function siteFile(ctx: ContextDelegation, next: Next) {
return async function siteFile(ctx: Context, next: Next) {
if (ctx.method !== 'HEAD' && ctx.method !== 'GET') {
return next();
}
Expand Down
2 changes: 1 addition & 1 deletion src/config/config.default.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'node:path';
import { pathToFileURL } from 'node:url';
import type { EggAppInfo } from '@eggjs/core';
import type { EggAppConfig } from '../lib/type.js';
import type { EggAppConfig } from '../lib/types.js';
import { getSourceFile } from '../lib/utils.js';

/**
Expand Down
2 changes: 1 addition & 1 deletion src/config/config.local.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { EggAppConfig } from '../lib/type.js';
import type { EggAppConfig } from '../lib/types.js';

export default () => {
return {
Expand Down
Loading
Loading