Skip to content

Commit 77182be

Browse files
mpywclaude
andcommitted
docs: add request/response/headers API documentation and E2E tests
- Add Request Object section with method, url, headers properties - Add Response Object section with status, statusText, headers, ok properties - Add Headers API section with get/has/set/append/delete/keys/values/entries/forEach - Add comprehensive E2E tests for all request/response/headers methods - Update transform variable lists to include request/response Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent a3279d8 commit 77182be

3 files changed

Lines changed: 619 additions & 0 deletions

File tree

SCHEMA.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ This document describes all configuration options for sql-http-proxy. For usage
3030
- [Path Parameters](#path-parameters)
3131
- [Named Placeholders](#named-placeholders)
3232
- [Accepts](#accepts)
33+
- [Request Object](#request-object)
34+
- [Response Object](#response-object)
35+
- [Headers API](#headers-api)
3336

3437
---
3538

@@ -379,6 +382,7 @@ For `object_js` and `array_js`:
379382
- `input` (parameter): Request parameters
380383
- `ctx` (free variable): Shared state
381384
- `sql` (free variable): SQL string (read-only)
385+
- `request` (free variable): HTTP [request object](#request-object) (read-only)
382386

383387
## Transform
384388

@@ -400,6 +404,7 @@ Validates and transforms input before SQL/[mock](#mock) execution.
400404
- `input` (parameter): Request parameters
401405
- `ctx` (free variable): Shared state (persists to [post](#post-transform))
402406
- `sql` (free variable): SQL string (modifiable, only meaningful when using sql)
407+
- `request` (free variable): HTTP [request object](#request-object) (read-only)
403408

404409
**Returns:** Object with parameters for SQL/mock
405410

@@ -420,6 +425,8 @@ Transforms the result after SQL/[mock](#mock) execution.
420425
- `ctx` (free variable): Shared state from [pre](#pre-transform)
421426
- `ctx.lastInsertId`: Auto-increment ID ([MySQL mutations](#mysql-specific-behavior) only)
422427
- `ctx.rowsAffected`: Number of affected rows ([MySQL mutations](#mysql-specific-behavior) only)
428+
- `request` (free variable): HTTP [request object](#request-object) (read-only)
429+
- `response` (free variable): HTTP [response object](#response-object) (writable)
423430

424431
**For type: one:**
425432

@@ -590,3 +597,101 @@ accepts: [json, form] # Both (default)
590597

591598
> [!WARNING]
592599
> Returns 415 Unsupported Media Type if the request Content-Type doesn't match.
600+
601+
## Request Object
602+
603+
The `request` free variable provides read-only access to HTTP request information. Available in [pre](#pre-transform), [post](#post-transform), and [mock JS](#mock-js-variables).
604+
605+
| Property | Type | Description |
606+
|----------|------|-------------|
607+
| `method` | string | HTTP method (e.g., `"GET"`, `"POST"`) |
608+
| `url` | string | Request URL |
609+
| `headers` | [Headers](#headers-api) | Request headers (read-only) |
610+
611+
```yaml
612+
transform:
613+
pre: |
614+
// Check request method
615+
if (request.method !== 'POST') {
616+
throw { status: 405, body: { error: 'POST required' } };
617+
}
618+
// Read custom header
619+
const apiKey = request.headers.get('X-API-Key');
620+
if (!apiKey) {
621+
throw { status: 401, body: { error: 'API key required' } };
622+
}
623+
return input;
624+
```
625+
626+
## Response Object
627+
628+
The `response` free variable allows modifying HTTP response properties. **Only available in [post-transform](#post-transform).**
629+
630+
| Property | Type | Access | Description |
631+
|----------|------|--------|-------------|
632+
| `status` | number | get/set | HTTP status code (100-599) |
633+
| `statusText` | string | get/set | HTTP status text |
634+
| `headers` | [Headers](#headers-api) | get (object is writable) | Response headers |
635+
| `ok` | boolean | get | `true` if status is 200-299 |
636+
637+
```yaml
638+
transform:
639+
post: |
640+
// Set custom response headers
641+
response.headers.set('X-Custom-Header', 'value');
642+
response.headers.set('Cache-Control', 'max-age=3600');
643+
644+
// Change status code (affects HTTP response)
645+
response.status = 201;
646+
647+
return output;
648+
```
649+
650+
> [!NOTE]
651+
> Setting `response.status` changes the HTTP status code of the response. This is different from throwing an error with `status` — the response body is still the return value of post-transform.
652+
653+
> [!WARNING]
654+
> `response` is only available in post-transform. Accessing it in pre-transform or mock JS will result in `undefined`.
655+
656+
## Headers API
657+
658+
The Headers API follows the [Web API Headers](https://developer.mozilla.org/en-US/docs/Web/API/Headers) interface.
659+
660+
| Method | Description |
661+
|--------|-------------|
662+
| `get(name)` | Get header value (`null` if not found) |
663+
| `has(name)` | Check if header exists |
664+
| `set(name, value)` | Set header value (replaces existing) |
665+
| `append(name, value)` | Add header value (allows multiple) |
666+
| `delete(name)` | Remove header |
667+
| `keys()` | Get all header names |
668+
| `values()` | Get all header values |
669+
| `entries()` | Get `[name, value]` pairs |
670+
| `forEach(callback)` | Iterate with `callback(value, name)` |
671+
672+
> [!NOTE]
673+
> Header names are case-insensitive. `headers.get('Content-Type')` and `headers.get('content-type')` return the same value.
674+
675+
> [!IMPORTANT]
676+
> `request.headers` is read-only — `set()`, `append()`, and `delete()` are silently ignored.
677+
> `response.headers` is writable — all methods work as expected.
678+
679+
```yaml
680+
transform:
681+
post: |
682+
// Read request headers
683+
const userAgent = request.headers.get('User-Agent');
684+
const acceptLanguage = request.headers.get('Accept-Language');
685+
686+
// Write response headers
687+
response.headers.set('Content-Language', 'en');
688+
response.headers.append('Set-Cookie', 'session=abc; HttpOnly');
689+
response.headers.append('Set-Cookie', 'user=123');
690+
691+
// Iterate headers
692+
request.headers.forEach((value, name) => {
693+
console.log(name + ': ' + value);
694+
});
695+
696+
return { ...output, userAgent };
697+
```

0 commit comments

Comments
 (0)