Skip to content
Merged
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
31 changes: 30 additions & 1 deletion docs/docs/features/prerequest-scripts.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,33 @@ const sessid = altair.helpers.getCookie('sessid');
```js
const res = await altair.helpers.request('GET', 'https://api.agify.io/?name=michael');
// res => {"name":"michael","age":60,"count":41938}
```
```

### Persisting data between requests

Since altair pretty much lives in a browser environment, it does support the `LocalStorage` feature. This is useful when you need an authentication token before each request but only requesting the token when your authentication expired.

Assuming you have this header in the `Set Headers window`:

| Key | Value |
| ------------- | -------------------- |
| Authorization | Bearer {{token_env}} |

Below is a working example of persisting data between requests (token, token_expiry):

```js
const nowInSeconds = () => Date.now() / 1000;
const tokenExpiry = localStorage.getItem("token_expiry") || 0;

if (nowInSeconds() >= tokenExpiry) {
const res = await altair.helpers.request('POST', 'https://auth.example.com', { /* auth payload */ });
// res => {"token":"abcd","expiry":3600}

localStorage.setItem("token", res.token);
localStorage.setItem("token_expiry", nowInSeconds() + res.expiry);
}

const token = localStorage.getItem("token");

altair.helpers.setEnvironment('token_env', token);
```