Skip to content

Commit

Permalink
fix: context.state property override when context typings merge (#3402)
Browse files Browse the repository at this point in the history
* fix: override state typings to context

* fix: koa missing
  • Loading branch information
czy88840616 authored Nov 12, 2023
1 parent 7e4fd5a commit f742125
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 16 deletions.
5 changes: 5 additions & 0 deletions packages/faas/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,10 @@ export interface FaaSHTTPContext
* FaaS Cookies Object
*/
cookies: Cookies;
/**
* FaaS Context State
*/
state: State;

/**
* Throw an error with `msg` and optional `status`
Expand Down Expand Up @@ -515,3 +519,4 @@ export interface HttpResponseFormat<T = unknown> {
}

export interface wrapHttpRequestOptions extends HttpResponseOptions {}
export interface State {}
18 changes: 9 additions & 9 deletions packages/passport/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ declare module '@midwayjs/core/dist/interface' {
}

declare module '@midwayjs/koa/dist/interface' {
interface State {
user?: any;
}
interface Context {
state: {
user?: any;
};
isAuthenticated(): boolean;
isUnauthenticated(): boolean;
login(): Promise<void>;
Expand All @@ -21,10 +21,10 @@ declare module '@midwayjs/koa/dist/interface' {
}

declare module '@midwayjs/web/dist/interface' {
interface State {
user?: any;
}
interface Context {
state: {
user?: any;
};
isAuthenticated(): boolean;
isUnauthenticated(): boolean;
login(): Promise<void>;
Expand All @@ -33,10 +33,10 @@ declare module '@midwayjs/web/dist/interface' {
}

declare module '@midwayjs/faas/dist/interface' {
interface State {
user?: any;
}
interface Context {
state: {
user?: any;
};
isAuthenticated(): boolean;
isUnauthenticated(): boolean;
login(): Promise<void>;
Expand Down
2 changes: 2 additions & 0 deletions packages/web-koa/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
IMidwayKoaConfigurationOptions,
Context as KoaContext,
BodyParserOptions,
State,
} from './dist';
import { CookieSetOptions, Cookies } from '@midwayjs/cookies';
import '@midwayjs/session';
Expand Down Expand Up @@ -41,5 +42,6 @@ declare module 'koa' {
cookies: Cookies;
app: Application;
forward: (url: string) => void;
state: State;
}
}
12 changes: 8 additions & 4 deletions packages/web-koa/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ import * as koa from 'koa';
import { Context as KoaContext, DefaultState, Middleware, Next } from 'koa';
import { RouterParamValue } from '@midwayjs/core';

export interface State extends DefaultState {}

export type IMidwayKoaContext = IMidwayContext<KoaContext>;
export type IMidwayKoaApplication = IMidwayApplication<IMidwayKoaContext, koa<DefaultState, IMidwayKoaContext> & {
export type IMidwayKoaApplication = IMidwayApplication<IMidwayKoaContext, koa<State, IMidwayKoaContext> & {
generateController(
controllerMapping: string,
routeArgsInfo?: RouterParamValue[],
routerResponseData?: any []
): Middleware<DefaultState, IMidwayKoaContext>;
): Middleware<State, IMidwayKoaContext>;
/**
* @deprecated
* @param middlewareId
*/
generateMiddleware(middlewareId: any): Promise<Middleware<DefaultState, IMidwayKoaContext>>;
generateMiddleware(middlewareId: any): Promise<Middleware<State, IMidwayKoaContext>>;
}>;

/**
Expand Down Expand Up @@ -92,7 +94,9 @@ export interface IWebMiddleware {

export type Application = IMidwayKoaApplication;

export interface Context extends IMidwayKoaContext {}
export interface Context extends IMidwayKoaContext {
state: State;
}

export interface BodyParserOptions {
enable?: boolean;
Expand Down
2 changes: 2 additions & 0 deletions packages/web/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
IMidwayWebBaseApplication,
IMidwayWebConfigurationOptions,
Context as EggContext,
State,
} from './dist';
import { ILogger, LoggerOptions } from '@midwayjs/logger';
import { EggAppConfig } from 'egg';
Expand All @@ -30,6 +31,7 @@ declare module 'egg' {
interface Context<ResponseBodyT = any> extends IMidwayBaseContext {
getLogger(name?: string): ILogger;
forward: (url: string) => void;
state: State;
}
}

Expand Down
9 changes: 6 additions & 3 deletions packages/web/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import { ILogger, LoggerOptions } from '@midwayjs/logger';
export const RUN_IN_AGENT_KEY = 'egg:run_in_agent';
export const EGG_AGENT_APP_KEY = 'egg_agent_app';

export interface State extends DefaultState {}

export interface IMidwayWebBaseApplication {
applicationContext: IMidwayContainer;
getLogger(name?: string): ILogger;
getCoreLogger(): ILogger;
generateMiddleware?(middlewareId: any): Promise<Middleware<DefaultState, EggContext>>;
generateMiddleware?(middlewareId: any): Promise<Middleware<State, EggContext>>;
createLogger(name: string, options: LoggerOptions): ILogger;
}

Expand Down Expand Up @@ -62,7 +64,8 @@ export interface Context <ResponseBodyT = unknown> extends IMidwayWebContext <Re
* allow to put any value on session object
*/
[_: string]: any;
}
};
state: State;
}
/**
* @deprecated since version 3.0.0
Expand Down Expand Up @@ -121,7 +124,7 @@ export interface IMidwayWebConfigurationOptions extends IConfigurationOptions {
* @deprecated since version 3.0.0
* Please use IMiddleware from '@midwayjs/core'
*/
export type MidwayWebMiddleware = Middleware<DefaultState, Context>;
export type MidwayWebMiddleware = Middleware<State, Context>;

/**
* @deprecated since version 3.0.0
Expand Down
21 changes: 21 additions & 0 deletions site/docs/extensions/egg.md
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,27 @@ Copy



## State 类型定义

在 egg 底层的 koa 的 Context 中有一个特殊的 State 属性,通过和 Context 类似的方式可以扩展 State 定义。

```typescript
// src/interface.ts

declare module '@midwayjs/web/dist/interface' {
interface Context {
abc: string;
}

interface State{
bbb: string;
ccc: number;
}
}
```



## 配置

### 默认配置
Expand Down
21 changes: 21 additions & 0 deletions site/docs/extensions/koa.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,27 @@ export class MainConfiguration {



## State 类型定义

在 koa 的 Context 中有一个特殊的 State 属性,通过和 Context 类似的方式可以扩展 State 定义。

```typescript
// src/interface.ts

declare module '@midwayjs/koa/dist/interface' {
interface Context {
abc: string;
}

interface State{
bbb: string;
ccc: number;
}
}
```





## 配置
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,29 @@ The original egg uses `EGG_SERVER_ENV` as an environmental sign, please use `MID



## State type definition

There is a special State attribute in the Context of koa at the bottom of egg. The State definition can be extended in a similar way to Context.

```typescript
// src/interface.ts

declare module '@midwayjs/web/dist/interface' {
interface Context {
abc: string;
}

interface State{
bbb: string;
ccc: number;
}
}
```





## Configuration

### Default configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,29 @@ export class MainConfiguration {



## State type definition

There is a special State attribute in koa's Context, and the State definition can be extended in a similar way to Context.

```typescript
// src/interface.ts

declare module '@midwayjs/koa/dist/interface' {
interface Context {
abc: string;
}

interface State{
bbb: string;
ccc: number;
}
}
```





## Configuration


Expand Down

0 comments on commit f742125

Please sign in to comment.