Skip to content

Commit

Permalink
[DX-1409] Fix virtual endpoints Javascript request object documentati…
Browse files Browse the repository at this point in the history
…on (#5132)

fix virtual endpoints request object content

---------

Co-authored-by: Simon Pears <[email protected]>
Co-authored-by: Tit Petric <[email protected]>
  • Loading branch information
3 people authored Aug 23, 2024
1 parent 20f8f96 commit eab1505
Showing 1 changed file with 73 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
}
```

<!--
```go
{
struct {
Headers map[string][]string
SetHeaders map[string]string
DeleteHeaders []string
Expand All @@ -117,7 +141,7 @@ The structure of the `request` object is:
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
Expand All @@ -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.
Expand Down Expand Up @@ -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`

</br>

{{< 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.
Expand Down

0 comments on commit eab1505

Please sign in to comment.