mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-08-12 21:10:23 +00:00
feat(koa): default state + default context (#37901)
This commit is contained in:
Vendored
+26
-4
@@ -3,6 +3,7 @@
|
||||
// Definitions by: DavidCai1993 <https://github.com/DavidCai1993>
|
||||
// jKey Lu <https://github.com/jkeylu>
|
||||
// Brice Bernard <https://github.com/brikou>
|
||||
// harryparkdotio <https://github.com/harryparkdotio>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.3
|
||||
|
||||
@@ -431,7 +432,10 @@ declare interface ContextDelegatedResponse {
|
||||
flushHeaders(): void;
|
||||
}
|
||||
|
||||
declare class Application<StateT = any, CustomT = {}> extends EventEmitter {
|
||||
declare class Application<
|
||||
StateT = Application.DefaultState,
|
||||
CustomT = Application.DefaultContext
|
||||
> extends EventEmitter {
|
||||
proxy: boolean;
|
||||
middleware: Application.Middleware<StateT, CustomT>[];
|
||||
subdomainOffset: number;
|
||||
@@ -512,7 +516,7 @@ declare class Application<StateT = any, CustomT = {}> extends EventEmitter {
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
createContext<StateT = any>(
|
||||
createContext<StateT = Application.DefaultState>(
|
||||
req: IncomingMessage,
|
||||
res: ServerResponse,
|
||||
): Application.ParameterizedContext<StateT>;
|
||||
@@ -526,7 +530,22 @@ declare class Application<StateT = any, CustomT = {}> extends EventEmitter {
|
||||
}
|
||||
|
||||
declare namespace Application {
|
||||
type Middleware<StateT = any, CustomT = {}> = compose.Middleware<ParameterizedContext<StateT, CustomT>>;
|
||||
type DefaultStateExtends = any;
|
||||
/**
|
||||
* This interface can be augmented by users to add types to Koa's default state
|
||||
*/
|
||||
interface DefaultState extends DefaultStateExtends {}
|
||||
|
||||
type DefaultContextExtends = {};
|
||||
/**
|
||||
* This interface can be augmented by users to add types to Koa's default context
|
||||
*/
|
||||
interface DefaultContext extends DefaultContextExtends {}
|
||||
|
||||
type Middleware<
|
||||
StateT = DefaultState,
|
||||
CustomT = DefaultContext
|
||||
> = compose.Middleware<ParameterizedContext<StateT, CustomT>>;
|
||||
|
||||
interface BaseRequest extends ContextDelegatedRequest {
|
||||
/**
|
||||
@@ -701,7 +720,10 @@ declare namespace Application {
|
||||
respond?: boolean;
|
||||
}
|
||||
|
||||
type ParameterizedContext<StateT = any, CustomT = {}> = ExtendableContext & {
|
||||
type ParameterizedContext<
|
||||
StateT = DefaultState,
|
||||
CustomT = DefaultContext
|
||||
> = ExtendableContext & {
|
||||
state: StateT;
|
||||
} & CustomT;
|
||||
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
import Koa = require('koa');
|
||||
|
||||
declare module 'koa' {
|
||||
interface DefaultState {
|
||||
stateProperty: boolean;
|
||||
}
|
||||
|
||||
interface DefaultContext {
|
||||
logger: {
|
||||
info: Function;
|
||||
log: Function;
|
||||
error: Function;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const app = new Koa();
|
||||
|
||||
app.context.logger = {
|
||||
info: () => {},
|
||||
log: () => {},
|
||||
error: () => {},
|
||||
};
|
||||
|
||||
app.use((ctx, next) => {
|
||||
ctx.state.stateProperty = false;
|
||||
return next();
|
||||
})
|
||||
|
||||
app.use<{ a: boolean }>(async (ctx, next) => {
|
||||
ctx.state.a = true;
|
||||
ctx.state.b = ''; // undeclared property
|
||||
await next();
|
||||
});
|
||||
|
||||
app.use((ctx, next) => {
|
||||
const start: any = new Date();
|
||||
return next().then(() => {
|
||||
const end: any = new Date();
|
||||
const ms = end - start;
|
||||
ctx.logger.info(`${ctx.method} ${ctx.url} - ${ms}ms`);
|
||||
ctx.user = {}; // undeclared property
|
||||
ctx.assert(true, 404, 'Yep!');
|
||||
});
|
||||
});
|
||||
|
||||
// response
|
||||
app.use(ctx => {
|
||||
ctx.body = 'Hello World';
|
||||
ctx.body = ctx.URL.toString();
|
||||
});
|
||||
|
||||
app.listen(3000);
|
||||
|
||||
const server = app.listen();
|
||||
@@ -1,7 +1,8 @@
|
||||
{
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"koa-tests.ts"
|
||||
"test/index.ts",
|
||||
"test/default.ts"
|
||||
],
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
|
||||
Reference in New Issue
Block a user