Skip to content

Commit

Permalink
use @eggjs/bin
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 24, 2024
1 parent 4c4d584 commit 4a579ba
Show file tree
Hide file tree
Showing 14 changed files with 130 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/node_modules
node_modules
coverage
*.log
npm-debug.log
Expand Down
34 changes: 34 additions & 0 deletions example/helloworld-typescript/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { ILifecycleBoot, Application } from 'egg';

export default class AppBootHook implements ILifecycleBoot {
private readonly app: Application;

constructor(app: Application) {
this.app = app;
}

async didLoad() {
console.error('didLoad');
// Ready to call configDidLoad,
// Config, plugin files are referred,
// this is the last chance to modify the config.
// throw new Error('Method not implemented.');
}

async willReady() {
// All plugins have started, can do some thing before app ready
}

async didReady() {
// Worker is ready, can do some things
// don't need to block the app boot process
}

async serverDidReady() {
// Server is listening.
}

async beforeClose() {
// Do some thing before app close.
}
}
7 changes: 7 additions & 0 deletions example/helloworld-typescript/app/controller/home.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Controller } from 'egg';

export default class HomeController extends Controller {
async index() {
this.ctx.body = 'Hello EggJS 🥚🥚🥚🥚';
}
}
5 changes: 5 additions & 0 deletions example/helloworld-typescript/app/router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Application } from 'egg';

export default (app: Application) => {
app.get('/', 'home.index');
};
7 changes: 7 additions & 0 deletions example/helloworld-typescript/config/config.default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { EggAppConfig } from 'egg';

export default () => {
return {
keys: '123456',
} as Partial<EggAppConfig>;
};
10 changes: 10 additions & 0 deletions example/helloworld-typescript/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "helloworld-typescript",
"type": "module",
"egg": {
"typescript": true
},
"dependencies": {
"egg": "beta"
}
}
10 changes: 10 additions & 0 deletions example/helloworld-typescript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "@eggjs/tsconfig",
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext"
}
}
18 changes: 11 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "egg",
"version": "4.0.0-beta.6",
"version": "4.0.0-beta.7",
"engines": {
"node": ">= 18.19.0"
},
Expand Down Expand Up @@ -67,7 +67,7 @@
"assert-file": "1",
"coffee": "5",
"cross-env": "7",
"egg-bin": "beta",
"@eggjs/bin": "beta",
"@eggjs/mock": "beta",
"egg-plugin-puml": "^2.4.0",
"egg-tracer": "^2.1.0",
Expand All @@ -83,18 +83,21 @@
"sdk-base": "^4.2.1",
"spy": "^1.0.0",
"supertest": "^7.0.0",
"rimraf": "6",
"tshy": "^3.0.2",
"tshy-after": "1",
"typescript": "5"
},
"scripts": {
"clean": "rimraf dist",
"lint": "eslint src test --ext .ts",
"pretest": "npm run lint -- --fix && npm run prepublishOnly",
"pretest": "npm run clean && npm run lint -- --fix",
"test": "egg-bin test",
"test-local": "egg-bin test",
"test:changed": "egg-bin test --changed",
"preci": "npm run lint && npm run prepublishOnly && attw --pack",
"ci": "egg-bin cov",
"prepublishOnly": "tshy && tshy-after",
"preci": "npm run clean && npm run lint",
"ci": "egg-bin cov && npm run prepublishOnly",
"prepublishOnly": "tshy && tshy-after && attw --pack",
"site:dev": "cross-env NODE_OPTIONS=--openssl-legacy-provider APP_ROOT=./site dumi dev",
"site:devWithNode14-16": "cross-env APP_ROOT=./site dumi dev",
"site:build": "cross-env NODE_OPTIONS=--openssl-legacy-provider APP_ROOT=./site dumi build",
Expand All @@ -115,7 +118,8 @@
"framework": true,
"exports": {
"import": "./dist/esm",
"require": "./dist/commonjs"
"require": "./dist/commonjs",
"typescript": "./src"
}
},
"files": [
Expand Down
14 changes: 10 additions & 4 deletions src/app/extend/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ import {
type ContextDelegation as EggCoreContextDelegation,
} from '@eggjs/core';
import type { Cookies as ContextCookies } from '@eggjs/cookies';
import { EggLogger } from 'egg-logger';
import type { Application } from '../../lib/application.js';
import type {
HttpClientRequestURL, HttpClientRequestOptions, HttpClient,
} from '../../lib/core/httpclient.js';
import type { ContextHttpClient } from '../../lib/core/context_httpclient.js';
import type { BaseContextClass } from '../../lib//core/base_context_class.js';
import Request from './request.js';
import Response from './response.js';
import { EggLogger } from 'egg-logger';
import type Helper from './helper.js';

import './context.types.js';

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

Expand Down Expand Up @@ -114,11 +120,11 @@ export default class Context extends EggCoreContext {
* @member {Helper} Context#helper
* @since 1.0.0
*/
get helper() {
get helper(): Helper {
if (!this[HELPER]) {
this[HELPER] = new this.app.Helper(this as any);
}
return this[HELPER];
return this[HELPER] as Helper;
}

/**
Expand Down
24 changes: 24 additions & 0 deletions src/app/extend/context.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type {
Router,
} from '@eggjs/core';
import type {
HttpClientRequestURL, HttpClientRequestOptions, HttpClient,
} from '../../lib/core/httpclient.js';
import type {
ContextHttpClient,
} from '../../lib/core/context_httpclient.js';
import type Helper from './helper.js';
import type { EggLogger } from 'egg-logger';

declare module '@eggjs/core' {
// add Context overrides types
interface Context {
curl(url: HttpClientRequestURL, options?: HttpClientRequestOptions): ReturnType<HttpClient['request']>;
get router(): Router;
set router(val: Router);
get helper(): Helper;
get httpclient(): ContextHttpClient;
get httpClient(): ContextHttpClient;
getLogger(name: string): EggLogger;
}
}
2 changes: 2 additions & 0 deletions src/lib/egg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ import { BaseHookClass } from './core/base_hook_class.js';
import type { EggApplicationLoader } from './loader/index.js';
import { getSourceDirname } from './utils.js';

import './egg.types.js';

const EGG_PATH = Symbol.for('egg#eggPath');

export interface EggApplicationCoreOptions extends Omit<EggCoreOptions, 'baseDir'> {
Expand Down
6 changes: 6 additions & 0 deletions src/lib/egg.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
declare module '@eggjs/core' {
// add EggApplicationCore overrides types
interface EggCore {
inspect(): any;
}
}
2 changes: 1 addition & 1 deletion test/app/extend/application.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ describe('test/app/extend/application.test.ts', () => {

it('should wait for middleware resolution', async () => {
const ctx = app.createAnonymousContext();
await app.handleRequest(ctx, async (ctx: any) => {
await (app as any).handleRequest(ctx, async (ctx: any) => {
await scheduler.wait(100);
ctx.body = 'middleware resolution';
});
Expand Down
3 changes: 2 additions & 1 deletion test/app/extend/request.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { strict as assert } from 'node:assert';
import { once } from 'node:events';
import type { AddressInfo } from 'node:net';
import urllib from 'urllib';
import { createApp, MockApplication, restore, mm } from '../../utils.js';

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

Expand Down

0 comments on commit 4a579ba

Please sign in to comment.