Skip to content

Commit 4a579ba

Browse files
committed
use @eggjs/bin
1 parent 4c4d584 commit 4a579ba

File tree

14 files changed

+130
-14
lines changed

14 files changed

+130
-14
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/node_modules
1+
node_modules
22
coverage
33
*.log
44
npm-debug.log

example/helloworld-typescript/app.ts

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { ILifecycleBoot, Application } from 'egg';
2+
3+
export default class AppBootHook implements ILifecycleBoot {
4+
private readonly app: Application;
5+
6+
constructor(app: Application) {
7+
this.app = app;
8+
}
9+
10+
async didLoad() {
11+
console.error('didLoad');
12+
// Ready to call configDidLoad,
13+
// Config, plugin files are referred,
14+
// this is the last chance to modify the config.
15+
// throw new Error('Method not implemented.');
16+
}
17+
18+
async willReady() {
19+
// All plugins have started, can do some thing before app ready
20+
}
21+
22+
async didReady() {
23+
// Worker is ready, can do some things
24+
// don't need to block the app boot process
25+
}
26+
27+
async serverDidReady() {
28+
// Server is listening.
29+
}
30+
31+
async beforeClose() {
32+
// Do some thing before app close.
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Controller } from 'egg';
2+
3+
export default class HomeController extends Controller {
4+
async index() {
5+
this.ctx.body = 'Hello EggJS 🥚🥚🥚🥚';
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Application } from 'egg';
2+
3+
export default (app: Application) => {
4+
app.get('/', 'home.index');
5+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { EggAppConfig } from 'egg';
2+
3+
export default () => {
4+
return {
5+
keys: '123456',
6+
} as Partial<EggAppConfig>;
7+
};
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "helloworld-typescript",
3+
"type": "module",
4+
"egg": {
5+
"typescript": true
6+
},
7+
"dependencies": {
8+
"egg": "beta"
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "@eggjs/tsconfig",
3+
"compilerOptions": {
4+
"strict": true,
5+
"noImplicitAny": true,
6+
"target": "ES2022",
7+
"module": "NodeNext",
8+
"moduleResolution": "NodeNext"
9+
}
10+
}

package.json

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "egg",
3-
"version": "4.0.0-beta.6",
3+
"version": "4.0.0-beta.7",
44
"engines": {
55
"node": ">= 18.19.0"
66
},
@@ -67,7 +67,7 @@
6767
"assert-file": "1",
6868
"coffee": "5",
6969
"cross-env": "7",
70-
"egg-bin": "beta",
70+
"@eggjs/bin": "beta",
7171
"@eggjs/mock": "beta",
7272
"egg-plugin-puml": "^2.4.0",
7373
"egg-tracer": "^2.1.0",
@@ -83,18 +83,21 @@
8383
"sdk-base": "^4.2.1",
8484
"spy": "^1.0.0",
8585
"supertest": "^7.0.0",
86+
"rimraf": "6",
8687
"tshy": "^3.0.2",
8788
"tshy-after": "1",
8889
"typescript": "5"
8990
},
9091
"scripts": {
92+
"clean": "rimraf dist",
9193
"lint": "eslint src test --ext .ts",
92-
"pretest": "npm run lint -- --fix && npm run prepublishOnly",
94+
"pretest": "npm run clean && npm run lint -- --fix",
9395
"test": "egg-bin test",
96+
"test-local": "egg-bin test",
9497
"test:changed": "egg-bin test --changed",
95-
"preci": "npm run lint && npm run prepublishOnly && attw --pack",
96-
"ci": "egg-bin cov",
97-
"prepublishOnly": "tshy && tshy-after",
98+
"preci": "npm run clean && npm run lint",
99+
"ci": "egg-bin cov && npm run prepublishOnly",
100+
"prepublishOnly": "tshy && tshy-after && attw --pack",
98101
"site:dev": "cross-env NODE_OPTIONS=--openssl-legacy-provider APP_ROOT=./site dumi dev",
99102
"site:devWithNode14-16": "cross-env APP_ROOT=./site dumi dev",
100103
"site:build": "cross-env NODE_OPTIONS=--openssl-legacy-provider APP_ROOT=./site dumi build",
@@ -115,7 +118,8 @@
115118
"framework": true,
116119
"exports": {
117120
"import": "./dist/esm",
118-
"require": "./dist/commonjs"
121+
"require": "./dist/commonjs",
122+
"typescript": "./src"
119123
}
120124
},
121125
"files": [

src/app/extend/context.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@ import {
66
type ContextDelegation as EggCoreContextDelegation,
77
} from '@eggjs/core';
88
import type { Cookies as ContextCookies } from '@eggjs/cookies';
9+
import { EggLogger } from 'egg-logger';
910
import type { Application } from '../../lib/application.js';
11+
import type {
12+
HttpClientRequestURL, HttpClientRequestOptions, HttpClient,
13+
} from '../../lib/core/httpclient.js';
1014
import type { ContextHttpClient } from '../../lib/core/context_httpclient.js';
1115
import type { BaseContextClass } from '../../lib//core/base_context_class.js';
1216
import Request from './request.js';
1317
import Response from './response.js';
14-
import { EggLogger } from 'egg-logger';
18+
import type Helper from './helper.js';
19+
20+
import './context.types.js';
1521

1622
const HELPER = Symbol('ctx helper');
1723
const LOCALS = Symbol('ctx locals');
@@ -79,7 +85,7 @@ export default class Context extends EggCoreContext {
7985
* @param {Object} [options] - options for request.
8086
* @return {Object} see {@link ContextHttpClient#curl}
8187
*/
82-
async curl(url: string, options?: object): ReturnType<ContextHttpClient['curl']> {
88+
async curl(url: HttpClientRequestURL, options?: HttpClientRequestOptions): ReturnType<HttpClient['request']> {
8389
return await this.httpclient.curl(url, options);
8490
}
8591

@@ -114,11 +120,11 @@ export default class Context extends EggCoreContext {
114120
* @member {Helper} Context#helper
115121
* @since 1.0.0
116122
*/
117-
get helper() {
123+
get helper(): Helper {
118124
if (!this[HELPER]) {
119125
this[HELPER] = new this.app.Helper(this as any);
120126
}
121-
return this[HELPER];
127+
return this[HELPER] as Helper;
122128
}
123129

124130
/**

src/app/extend/context.types.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type {
2+
Router,
3+
} from '@eggjs/core';
4+
import type {
5+
HttpClientRequestURL, HttpClientRequestOptions, HttpClient,
6+
} from '../../lib/core/httpclient.js';
7+
import type {
8+
ContextHttpClient,
9+
} from '../../lib/core/context_httpclient.js';
10+
import type Helper from './helper.js';
11+
import type { EggLogger } from 'egg-logger';
12+
13+
declare module '@eggjs/core' {
14+
// add Context overrides types
15+
interface Context {
16+
curl(url: HttpClientRequestURL, options?: HttpClientRequestOptions): ReturnType<HttpClient['request']>;
17+
get router(): Router;
18+
set router(val: Router);
19+
get helper(): Helper;
20+
get httpclient(): ContextHttpClient;
21+
get httpClient(): ContextHttpClient;
22+
getLogger(name: string): EggLogger;
23+
}
24+
}

src/lib/egg.ts

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ import { BaseHookClass } from './core/base_hook_class.js';
4343
import type { EggApplicationLoader } from './loader/index.js';
4444
import { getSourceDirname } from './utils.js';
4545

46+
import './egg.types.js';
47+
4648
const EGG_PATH = Symbol.for('egg#eggPath');
4749

4850
export interface EggApplicationCoreOptions extends Omit<EggCoreOptions, 'baseDir'> {

src/lib/egg.types.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
declare module '@eggjs/core' {
2+
// add EggApplicationCore overrides types
3+
interface EggCore {
4+
inspect(): any;
5+
}
6+
}

test/app/extend/application.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ describe('test/app/extend/application.test.ts', () => {
234234

235235
it('should wait for middleware resolution', async () => {
236236
const ctx = app.createAnonymousContext();
237-
await app.handleRequest(ctx, async (ctx: any) => {
237+
await (app as any).handleRequest(ctx, async (ctx: any) => {
238238
await scheduler.wait(100);
239239
ctx.body = 'middleware resolution';
240240
});

test/app/extend/request.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { strict as assert } from 'node:assert';
22
import { once } from 'node:events';
3+
import type { AddressInfo } from 'node:net';
34
import urllib from 'urllib';
45
import { createApp, MockApplication, restore, mm } from '../../utils.js';
56

@@ -377,7 +378,7 @@ describe('test/app/extend/request.test.ts', () => {
377378
before(async () => {
378379
const server = app.listen(0);
379380
await once(server, 'listening');
380-
host = `http://127.0.0.1:${server.address().port}`;
381+
host = `http://127.0.0.1:${(server.address() as AddressInfo).port}`;
381382
});
382383
after(() => app.close());
383384

0 commit comments

Comments
 (0)