From e1f272b203d33ad9979b072486f9d33a5aa109b1 Mon Sep 17 00:00:00 2001 From: Alexandre Bodin Date: Thu, 15 Sep 2022 20:25:50 +0200 Subject: [PATCH 1/2] Add documentation for the request context feature --- .../requests-responses.md | 54 ++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/docs/developer-docs/latest/development/backend-customization/requests-responses.md b/docs/developer-docs/latest/development/backend-customization/requests-responses.md index b1847af79f..ecf76cd73e 100644 --- a/docs/developer-docs/latest/development/backend-customization/requests-responses.md +++ b/docs/developer-docs/latest/development/backend-customization/requests-responses.md @@ -1,5 +1,5 @@ --- -title: Requests and Responses - Strapi Developer Docs +title: Requests and Responses - Strapi Developer Docs description: Learn more about requests and responses for Strapi, the most popular headless CMS. canonicalUrl: https://docs.strapi.io/developer-docs/latest/development/backend-customization/requests-responses.html --- @@ -19,3 +19,55 @@ For more information, please refer to the [Koa request documentation](http://koa The context object (`ctx`) contains a list of values and functions useful to manage server responses. They are accessible through `ctx.response`, from [controllers](/developer-docs/latest/development/backend-customization/controllers.md) and [policies](/developer-docs/latest/development/backend-customization/policies.md). For more information, please refer to the [Koa response documentation](http://koajs.com/#response). + +## Accessing the request context anywhere + +_**Added in:** v4.3.9_ + +Strapi exposes a way to access the current request context from anywhere in the code. (e.g lifecycle functions) + +You can access the request as follows: + +```js +const ctx = strapi.requestContext.get(); +``` + +You should only use this inside of functions that will be called in the context of an HTTP request. + +```js +// correct + +const service = { + myFunction() { + const ctx = strapi.requestContext.get(); + console.log(ctx.state.user); + }, +}; + +// incorrect +const ctx = strapi.requestContext.get(); + +const service = { + myFunction() { + console.log(ctx.state.user); + }, +}; +``` + +**Example:** + +```js +//path: ./api/test/content-types/article/lifecycles.js + +module.exports = { + beforeUpdate() { + const ctx = strapi.requestContext.get(); + + console.log('User info in service: ', ctx.state.user); + }, +}; +``` + +::: tip +Strapi uses a Node.js feature called [AsyncLocalStorage](https://nodejs.org/docs/latest-v16.x/api/async_context.html#class-asynclocalstorage) to make the context available anywhere. +::: From 2e0def54ccb02b08f94b585717d1987d14f1cff5 Mon Sep 17 00:00:00 2001 From: Alexandre BODIN Date: Fri, 16 Sep 2022 09:52:10 +0200 Subject: [PATCH 2/2] Update docs/developer-docs/latest/development/backend-customization/requests-responses.md Co-authored-by: Pierre Wizla --- .../backend-customization/requests-responses.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/developer-docs/latest/development/backend-customization/requests-responses.md b/docs/developer-docs/latest/development/backend-customization/requests-responses.md index ecf76cd73e..b8ccade529 100644 --- a/docs/developer-docs/latest/development/backend-customization/requests-responses.md +++ b/docs/developer-docs/latest/development/backend-customization/requests-responses.md @@ -22,9 +22,11 @@ For more information, please refer to the [Koa response documentation](http://ko ## Accessing the request context anywhere -_**Added in:** v4.3.9_ +::: callout ✨ New in v4.3.9 +The `strapi.requestContext` works with Strapi v4.3.9+. +::: -Strapi exposes a way to access the current request context from anywhere in the code. (e.g lifecycle functions) +Strapi exposes a way to access the current request context from anywhere in the code (e.g. lifecycle functions). You can access the request as follows: @@ -68,6 +70,6 @@ module.exports = { }; ``` -::: tip +::: note Strapi uses a Node.js feature called [AsyncLocalStorage](https://nodejs.org/docs/latest-v16.x/api/async_context.html#class-asynclocalstorage) to make the context available anywhere. :::