@2muchcoffee/nestjs-context 中文文档教程
@2muchcoffee/nestjs-context
允许您获取 NestJs 请求范围之外的请求上下文
Installation
npm install @2muchcoffee/nestjs-context --save
Usage
Import ContextModule
到 AppModule
或 CoreModule
一次
import { ContextModule } from '@2muchcoffee/nestjs-context';
@Module({
imports: [
...
ContextModule,
...
]
})
export class AppModule {
}
After ContextModule
import 您可以在代码中使用 Context
及其静态函数
import { Context } from '@2muchcoffee/nestjs-context';
const {user} = Context.getHttpReq<Request>();
注意:在大多数情况下,作者使用 @2muchcoffee/nestjs-context 在自定义验证器中获取上下文。 有关详细信息,请参阅示例。
GraphQl
你应该安装 @2muchcoffee/nestjs-gql-context
以获得框架使用的正确上下文
npm install @2muchcoffee/nestjs-gql-context --save
如果你想要 graphql 上下文,你可以使用 GqlContext
来获取它Context
import { GqlContext } from '@2muchcoffee/nestjs-gql-context';
GqlContext.getGqlContext<{req: Request}>()
Static Methods
Method | Description |
---|---|
Context | import { Context } from '@2muchcoffee/nestjs-context' |
getContext | Returns the current request pipeline |
getHttpArgs | Returns an array of arguments being passed to the handler |
getHttpReq | Returns a request object |
getHttpRes | Returns a response object |
getType | Returns the current execution context type (string) |
isHttp | Returns true if the current execution context type is http |
isGraphql | Returns true if the current execution context type is graphql |
GqlContext | import { GqlContext } from '@2muchcoffee/nestjs-gql-context';Context extends from GqlContext and has the same methods |
getExecutionContext | Returns the current gql execution context |
getGqlRoot | The previous object sent from the previous Query type |
getGqlArgs | Arguments provided in the GraphQL query |
getGqlContext | A value which is provided to every resolver and holds important contextual information |
getGqlInfo | A value which holds field-specific information relevant to the current query |
Samples
查看./sample中的示例使用例子。
@2muchcoffee/nestjs-context
Allows you to get the request context outside of the NestJs request scope
Installation
npm install @2muchcoffee/nestjs-context --save
Usage
Import ContextModule
to AppModule
or CoreModule
for once
import { ContextModule } from '@2muchcoffee/nestjs-context';
@Module({
imports: [
...
ContextModule,
...
]
})
export class AppModule {
}
After ContextModule
import you can use Context
and its static functions in your code
import { Context } from '@2muchcoffee/nestjs-context';
const {user} = Context.getHttpReq<Request>();
Note: In most cases author uses @2muchcoffee/nestjs-context to get context in custom validators. See samples for more details.
GraphQl
You should install @2muchcoffee/nestjs-gql-context
in order to get the correct context on the framework usage
npm install @2muchcoffee/nestjs-gql-context --save
If you want the graphql context, you can get it using GqlContext
instead of Context
import { GqlContext } from '@2muchcoffee/nestjs-gql-context';
GqlContext.getGqlContext<{req: Request}>()
Static Methods
Method | Description |
---|---|
Context | import { Context } from '@2muchcoffee/nestjs-context' |
getContext | Returns the current request pipeline |
getHttpArgs | Returns an array of arguments being passed to the handler |
getHttpReq | Returns a request object |
getHttpRes | Returns a response object |
getType | Returns the current execution context type (string) |
isHttp | Returns true if the current execution context type is http |
isGraphql | Returns true if the current execution context type is graphql |
GqlContext | import { GqlContext } from '@2muchcoffee/nestjs-gql-context';Context extends from GqlContext and has the same methods |
getExecutionContext | Returns the current gql execution context |
getGqlRoot | The previous object sent from the previous Query type |
getGqlArgs | Arguments provided in the GraphQL query |
getGqlContext | A value which is provided to every resolver and holds important contextual information |
getGqlInfo | A value which holds field-specific information relevant to the current query |
Samples
Take a look at samples in ./sample for usage examples.