From eab150557751df90cdf4922d385dedc9b51e1a87 Mon Sep 17 00:00:00 2001 From: dcs3spp Date: Fri, 23 Aug 2024 16:04:05 +0100 Subject: [PATCH] [DX-1409] Fix virtual endpoints Javascript request object documentation (#5132) fix virtual endpoints request object content --------- Co-authored-by: Simon Pears Co-authored-by: Tit Petric --- .../middleware-scripting-guide.md | 76 ++++++++++++++++++- 1 file changed, 73 insertions(+), 3 deletions(-) diff --git a/tyk-docs/content/plugins/supported-languages/javascript-middleware/middleware-scripting-guide.md b/tyk-docs/content/plugins/supported-languages/javascript-middleware/middleware-scripting-guide.md index a84748c3fd..1941b924a7 100755 --- a/tyk-docs/content/plugins/supported-languages/javascript-middleware/middleware-scripting-guide.md +++ b/tyk-docs/content/plugins/supported-languages/javascript-middleware/middleware-scripting-guide.md @@ -94,12 +94,36 @@ The system API provides access to resources outside of the JavaScript Virtual Ma ### The `request` object -The `request` object provides a set of arrays that describe the API request. These can be manipulated and, when changed, will affect the request as it passes through the middleware pipeline. +The `request` object provides a set of arrays that describe the API request. These can be manipulated and, when changed, will affect the request as it passes through the middleware pipeline. For [virtual endpoints]({{< ref "advanced-configuration/compose-apis/virtual-endpoints" >}}) the request object has a [different structure](#VirtualEndpoint-Request). The structure of the `request` object is: +```typesecript +class ReturnOverrides { + ResponseCode: number = 200; + ResponseBody: string = ""; + ResponseHeaders: string[] = []; +} + +class Request { + Headers: { [key: string]: string[] } = {}; + SetHeaders: { [key: string]: string } = {}; + DeleteHeaders: string[] = []; + Body: string = ""; + URL: string = ""; + AddParams: { [key: string]: string } = {}; + DeleteParams: string[] = []; + ReturnOverrides: ReturnOverrides = new ReturnOverrides(); + IgnoreBody: boolean = false; + Method: string = ""; + RequestURI: string = ""; + Scheme: string = ""; +} +``` + + - `Headers`: this is an object of string arrays, and represents the current state of the request header; this object cannot be modified directly, but can be used to read header data - `SetHeaders`: this is a key-value map that will be set in the header when the middleware returns the object; existing headers will be overwritten and new headers will be added @@ -132,6 +156,7 @@ The structure of the `request` object is: - `RequestURI`: contains the request URI, including the query string, e.g. `/path?key=value` - `Scheme`: contains the URL scheme, e.g. `http`, `https` + #### Using `ReturnOverrides` If you configure values in `request.ReturnOverrides` then Tyk will terminate the request and provide a response to the client when the function completes. The request will not be proxied to the upstream. @@ -159,6 +184,51 @@ testJSVMData.NewProcessRequest(function(request, session, config) { }); ``` +#### The virtual endpoint `request` object {#VirtualEndpoint-Request} + +For [virtual endpoint]({{< ref "advanced-configuration/compose-apis/virtual-endpoints" >}}) functions the structure of a Javascript `request` object is: + +```typescript +class VirtualEndpointRequest { + Body: string = ""; + Headers: { [key: string]: string[] } = {}; + Params: { [key: string]: string[] } = {}; + Scheme: string = ""; + URL: string = ""; +} +``` + +- `Body`: HTTP request body, e.g. `""` +- `Headers`: HTTP request headers, e.g. `"Accept": ["*/*"]` +- `Params`: Decoded query and form parameters, e.g. `{ "confirm": ["true"], "userId": ["123"] }` +- `Scheme`: The scheme of the URL ( e.g. `http` or `https`) +- `URL`: The full URL of the request, e.g `/vendpoint/anything?user_id=123\u0026confirm=true` + +
+ +{{< note success >}} +**Note** + +Each query and form parameter within the request is stored as an array field in the `Params` field of the request object. + +Repeated parameter assignments are appended to the corresponding array. For example, a request against `/vendpoint/anything?user_id[]=123&user_id[]=234` would result in a Javascript request object similar to that shown below: + +```javascript +const httpRequest = { + Headers: { + "Accept": ["*/*"], + "User-Agent": ["curl/8.1.2"] + }, + Body: "", + URL: "/vendpoint/anything?user_id[]=123\u0026user_id[]=234", + Params: { + "user_id[]": ["123", "234"] + }, + Scheme: "http" +}; +``` +{{< /note >}} + ### The `session` object Tyk uses an internal [session object]({{< ref "getting-started/key-concepts/what-is-a-session-object" >}}) to handle the quota, rate limits, access allowances and auth data of a specific key. JS middleware can be granted access to the session object but there is also the option to disable it as deserialising it into the JSVM is computationally expensive and can add latency. Other than the `meta_data` field, the session object itself cannot be directly edited as it is crucial to the correct functioning of Tyk.