Skip to content

Commit 4af16a9

Browse files
committed
docs: add middleware docs
1 parent e9c479b commit 4af16a9

File tree

1 file changed

+56
-5
lines changed

1 file changed

+56
-5
lines changed

packages/network/README.md

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ npm install @stacks/network
1010

1111
## Usage
1212

13-
Creating a Stacks mainnet, testnet or mocknet network
13+
### Create a Stacks mainnet, testnet or mocknet network
1414

1515
```typescript
1616
import { StacksMainnet, StacksTestnet, StacksMocknet } from '@stacks/network';
@@ -22,19 +22,19 @@ const testnet = new StacksTestnet();
2222
const mocknet = new StacksMocknet();
2323
```
2424

25-
Setting a custom node URL
25+
### Set a custom node URL
2626

2727
```typescript
2828
const network = new StacksMainnet({ url: 'https://www.mystacksnode.com/' });
2929
```
3030

31-
Check if network is mainnet
31+
### Check if network is mainnet
3232

3333
```typescript
3434
const isMainnet = network.isMainnet();
3535
```
3636

37-
Example usage in transaction builder
37+
### Network usage in transaction building
3838

3939
```typescript
4040
import { makeSTXTokenTransfer } from '@stacks/transactions';
@@ -49,7 +49,58 @@ const txOptions = {
4949
const transaction = await makeSTXTokenTransfer(txOptions);
5050
```
5151

52-
Get various API URLs
52+
### Use the built-in API key middleware
53+
54+
Some Stacks APIs make use API keys to provide less rate-limited plans.
55+
56+
```typescript
57+
import { createApiKeyMiddleware, createFetchFn, StacksMainnet } from '@stacks/network';
58+
import { broadcastResponse, getNonce, makeSTXTokenTransfer } from '@stacks/transactions';
59+
60+
const myApiMiddleware = createApiKeyMiddleware('example_e8e044a3_41d8b0fe_3dd3988ef302');
61+
const myFetchFn = createFetchFn(apiMiddleware); // middlewares can be used to create a new fetch function
62+
const myMainnet = new StacksMainnet({ fetchFn: myFetchFn }); // the fetchFn options can be passed to a StacksNetwork to override the default fetch function
63+
64+
const txOptions = {
65+
recipient: 'SP3FGQ8Z7JY9BWYZ5WM53E0M9NK7WHJF0691NZ159',
66+
amount: 12345n,
67+
senderKey: 'b244296d5907de9864c0b0d51f98a13c52890be0404e83f273144cd5b9960eed01',
68+
memo: 'some memo',
69+
anchorMode: AnchorMode.Any,
70+
network: myMainnet, // make sure to pass in the custom network object
71+
};
72+
const transaction = await makeSTXTokenTransfer(txOptions); // fee-estimation will use the custom fetchFn
73+
74+
const response = await broadcastResponse(transaction, myMainnet); // make sure to broadcast via the custom network object
75+
76+
// stacks.js functions, which take a StacksNetwork object will use the custom fetchFn
77+
const nonce = await getNonce('SP3FGQ8Z7JY9BWYZ5WM53E0M9NK7WHJF0691NZ159', myMainnet);
78+
```
79+
80+
### Use custom middleware
81+
82+
Middleware can be used to hook into network calls before sending a request or after receiving a response.
83+
84+
```typescript
85+
import { createFetchFn, RequestContext, ResponseContext, StacksTestnet } from '@stacks/network';
86+
import { broadcastResponse, getNonce, makeSTXTokenTransfer } from '@stacks/transactions';
87+
88+
const preMiddleware = (ctx: RequestContext) => {
89+
ctx.init.headers = new Headers();
90+
ctx.init.headers.set('x-foo', 'bar'); // override headers and set new `x-foo` header
91+
};
92+
const postMiddleware = (ctx: ResponseContext) => {
93+
console.log(await ctx.response.json()); // log response body as json
94+
};
95+
96+
const fetchFn = createFetchFn({ pre: preMiddleware, post: preMiddleware }); // a middleware can contain `pre`, `post`, or both
97+
const network = new StacksTestnet({ fetchFn });
98+
99+
// stacks.js functions, which take a StacksNetwork object will use the custom fetchFn
100+
const nonce = await getNonce('SP3FGQ8Z7JY9BWYZ5WM53E0M9NK7WHJF0691NZ159', network);
101+
```
102+
103+
### Get various API URLs
53104

54105
```typescript
55106
const txBroadcastUrl = network.getBroadcastApiUrl();

0 commit comments

Comments
 (0)