Skip to content

Commit 7111e2e

Browse files
committed
feat: enhance fetch commands to support custom headers for GET, POST, and PATCH requests
1 parent f9efd6e commit 7111e2e

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

docs/api/fetch.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,33 @@ Initiates a GET request to the specified URL. Returns a handle that identifies t
1616

1717
```cpp
1818
webcc::FetchRequest get(webcc::string_view url);
19+
webcc::FetchRequest get(webcc::string_view url, webcc::string_view headers_json);
1920
```
2021
22+
The `headers_json` argument is a JSON string of HTTP headers, e.g. `{"apikey":"...","Authorization":"Bearer ..."}`. If omitted, no custom headers are sent.
23+
2124
### `post`
2225
2326
Initiates a POST request to the specified URL with the given body. Returns a handle that identifies the request.
2427
2528
```cpp
2629
webcc::FetchRequest post(webcc::string_view url, webcc::string_view body);
30+
webcc::FetchRequest post(webcc::string_view url, webcc::string_view body, webcc::string_view headers_json);
2731
```
2832

33+
The `headers_json` argument is a JSON string of HTTP headers, e.g. `{"apikey":"...","Authorization":"Bearer ..."}`. If omitted, no custom headers are sent.
34+
2935
### `patch`
3036

3137
Initiates a PATCH request to the specified URL with the given body. Returns a handle that identifies the request.
3238

3339
```cpp
3440
webcc::FetchRequest patch(webcc::string_view url, webcc::string_view body);
41+
webcc::FetchRequest patch(webcc::string_view url, webcc::string_view body, webcc::string_view headers_json);
3542
```
3643
44+
The `headers_json` argument is a JSON string of HTTP headers, e.g. `{"apikey":"...","Authorization":"Bearer ..."}`. If omitted, no custom headers are sent.
45+
3746
## Events
3847
3948
Fetch operations are asynchronous. You must poll for events to receive the results.

schema.def

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ websocket|command|CLOSE|close|handle(WebSocket):handle|{ const ws = websockets[h
173173
# ------------------------------------------------------------------------------
174174
fetch|event|SUCCESS|handle(FetchRequest):id string:data
175175
fetch|event|ERROR|handle(FetchRequest):id string:error
176-
fetch|command|GET|get|string:url RET:handle(FetchRequest)|{ const id = (window.webcc_next_id = (window.webcc_next_id || 0) + 1); fetch(url).then(r => r.text().then(d => ({ ok: r.ok, status: r.status, statusText: r.statusText, data: d }))).then(res => { if(res.ok) push_event_fetch_SUCCESS(id, res.data); else push_event_fetch_ERROR(id, res.data && res.data.length ? res.data : (res.status + ' ' + res.statusText)); }).catch(e => push_event_fetch_ERROR(id, e.toString())); return id; }
177-
fetch|command|POST|post|string:url string:body RET:handle(FetchRequest)|{ const id = (window.webcc_next_id = (window.webcc_next_id || 0) + 1); fetch(url, { method: 'POST', body: body }).then(r => r.text().then(d => ({ ok: r.ok, status: r.status, statusText: r.statusText, data: d }))).then(res => { if(res.ok) push_event_fetch_SUCCESS(id, res.data); else push_event_fetch_ERROR(id, res.data && res.data.length ? res.data : (res.status + ' ' + res.statusText)); }).catch(e => push_event_fetch_ERROR(id, e.toString())); return id; }
178-
fetch|command|PATCH|patch|string:url string:body RET:handle(FetchRequest)|{ const id = (window.webcc_next_id = (window.webcc_next_id || 0) + 1); fetch(url, { method: 'PATCH', body: body }).then(r => r.text().then(d => ({ ok: r.ok, status: r.status, statusText: r.statusText, data: d }))).then(res => { if(res.ok) push_event_fetch_SUCCESS(id, res.data); else push_event_fetch_ERROR(id, res.data && res.data.length ? res.data : (res.status + ' ' + res.statusText)); }).catch(e => push_event_fetch_ERROR(id, e.toString())); return id; }
176+
fetch|command|GET|get|string:url string:headers RET:handle(FetchRequest)|{ const id = (window.webcc_next_id = (window.webcc_next_id || 0) + 1); let h = {}; try { h = JSON.parse(headers); } catch(e) {} fetch(url, { headers: h }).then(r => r.text().then(d => ({ ok: r.ok, status: r.status, statusText: r.statusText, data: d }))).then(res => { if(res.ok) push_event_fetch_SUCCESS(id, res.data); else push_event_fetch_ERROR(id, res.data && res.data.length ? res.data : (res.status + ' ' + res.statusText)); }).catch(e => push_event_fetch_ERROR(id, e.toString())); return id; }
177+
fetch|command|POST|post|string:url string:body string:headers RET:handle(FetchRequest)|{ const id = (window.webcc_next_id = (window.webcc_next_id || 0) + 1); let h = {}; try { h = JSON.parse(headers); } catch(e) {} fetch(url, { method: 'POST', body: body, headers: h }).then(r => r.text().then(d => ({ ok: r.ok, status: r.status, statusText: r.statusText, data: d }))).then(res => { if(res.ok) push_event_fetch_SUCCESS(id, res.data); else push_event_fetch_ERROR(id, res.data && res.data.length ? res.data : (res.status + ' ' + res.statusText)); }).catch(e => push_event_fetch_ERROR(id, e.toString())); return id; }
178+
fetch|command|PATCH|patch|string:url string:body string:headers RET:handle(FetchRequest)|{ const id = (window.webcc_next_id = (window.webcc_next_id || 0) + 1); let h = {}; try { h = JSON.parse(headers); } catch(e) {} fetch(url, { method: 'PATCH', body: body, headers: h }).then(r => r.text().then(d => ({ ok: r.ok, status: r.status, statusText: r.statusText, data: d }))).then(res => { if(res.ok) push_event_fetch_SUCCESS(id, res.data); else push_event_fetch_ERROR(id, res.data && res.data.length ? res.data : (res.status + ' ' + res.statusText)); }).catch(e => push_event_fetch_ERROR(id, e.toString())); return id; }
179179

180180
# ------------------------------------------------------------------------------
181181
# IMAGES

0 commit comments

Comments
 (0)