Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ By [@USERNAME](https://github.com/USERNAME) in https://github.com/apollographql/

# [x.x.x] (unreleased) - 2022-mm-dd

## 🚀 Features

### Add support for setting multi-value header keys to rhai ([Issue #2211](https://github.com/apollographql/router/issues/2211))

Adds support for setting a header map key with an array. This causes the HeaderMap key/values to be appended() to the map, rather than inserted().

Example use from rhai as:

```
response.headers["set-cookie"] = [
"foo=bar; Domain=localhost; Path=/; Expires=Wed, 04 Jan 2023 17:25:27 GMT; HttpOnly; Secure; SameSite=None",
"foo2=bar2; Domain=localhost; Path=/; Expires=Wed, 04 Jan 2023 17:25:27 GMT; HttpOnly; Secure; SameSite=None",
];
```

By [@garypen](https://github.com/garypen) in https://github.com/apollographql/router/pull/2219

## 🐛 Fixes

### Filter nullified deferred responses ([Issue #2213](https://github.com/apollographql/router/issues/2168))
Expand Down
12 changes: 12 additions & 0 deletions apollo-router/src/plugins/rhai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,18 @@ impl Rhai {
);
Ok(())
})
// Register an additional setter which allows us to set multiple values for the same
// key
.register_indexer_set(|x: &mut HeaderMap, key: &str, value: rhai::Array| {
let h_key = HeaderName::from_str(key).map_err(|e| e.to_string())?;
for v in value {
x.append(
h_key.clone(),
HeaderValue::from_str(&v.into_string()?).map_err(|e| e.to_string())?,
);
}
Ok(())
})
// Register a Context indexer so we can get/set context
.register_indexer_get(
|x: &mut Context, key: &str| -> Result<Dynamic, Box<EvalAltResult>> {
Expand Down
6 changes: 6 additions & 0 deletions docs/source/customizations/rhai-api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,12 @@ The headers of a request are accessible as a read/write indexed variable. The ke
// You can interact with request.headers as an indexed variable
request.headers["x-my-new-header"] = 42.to_string(); // Inserts a new header "x-my-new-header" with value "42"
print(`${request.headers["x-my-new-header"]}`); // Writes "42" into the router log at info level
// You can also set an header value from an array. Useful with the "set-cookie" header,
// Note: It's probably more useful to do this on response headers. Simply illustrating the syntax here.
request.headers["set-cookie"] = [
"foo=bar; Domain=localhost; Path=/; Expires=Wed, 04 Jan 2023 17:25:27 GMT; HttpOnly; Secure; SameSite=None",
"foo2=bar2; Domain=localhost; Path=/; Expires=Wed, 04 Jan 2023 17:25:27 GMT; HttpOnly; Secure; SameSite=None",
];
// Rhai also supports extended dot notation for indexed variables, so this is equivalent
request.headers.x-my-new-header = 42.to_string();
```
Expand Down