Skip to content

Commit bafc102

Browse files
committed
document rpcTransport
resolves #186
1 parent 6ebe962 commit bafc102

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

README.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,12 @@ Clients can be configured globally, at the RPC callsite, or with [middleware](ht
206206

207207
##### Configuration Options
208208

209-
| Name | Description | Type | Example |
210-
| ------- | -------------------------------------------------------------------------------------- | ---------------------- | ---------------------------- |
211-
| baseURL | The base URL for the RPC. The service path will be appended to this string. | string | "https://my.server.com/" |
212-
| headers | HTTP headers to include in the RPC. | Record<string, string> | { "idempotency-key": "foo" } |
213-
| prefix | A path prefix such as "/my/custom/prefix". Defaults to "/twirp", but can be set to "". | string | "/my/custom/prefix" |
209+
| Name | Description | Type | Example |
210+
| ------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------- | ---------------------------- |
211+
| baseURL | The base URL for the RPC. The service path will be appended to this string. | string | "https://my.server.com/" |
212+
| headers | HTTP headers to include in the RPC. | Record<string, string> | { "idempotency-key": "foo" } |
213+
| prefix | A path prefix such as "/my/custom/prefix". Defaults to "/twirp", but can be set to "". | string | "/my/custom/prefix" |
214+
| rpcTransport | The transport to use for the RPC. Defaults to `fetch`. Overrides must conform to a subset of the fetch interface defined by the `RpcTransport` type. | `RpcTransport` | `fetch` |
214215

215216
##### Example
216217

@@ -271,6 +272,21 @@ const hat = await MakeHat(
271272
console.log(hat);
272273
```
273274

275+
##### rpcTransport
276+
277+
Twirp abstracts many network details from clients. Sometimes you will want more control over the underlying network request. For these cases, TwirpScript exposes `rpcTransport`.
278+
279+
`rpcTransport` can be used to customize the `fetch` request made. For example, setting `fetch`'s [credentials](https://developer.mozilla.org/en-US/docs/Web/API/fetch#credentials) option to include (which will include cookies in cross origin requests):
280+
281+
```ts
282+
import { client } from "twirpscript";
283+
284+
client.rpcTransport = (url, opts) =>
285+
fetch(url, { ...opts, credentials: "include" });
286+
```
287+
288+
`rpcTransport` could also be used to replace `fetch` use entirely with something like `axios` or an [https agent](https://nodejs.org/api/https.html#https_class_https_agent).
289+
274290
#### Server
275291

276292
Servers can be configured by passing a configuration object to `createTwirpServer`.

0 commit comments

Comments
 (0)