Skip to content

Commit 7331c56

Browse files
authored
docs: parse raw stream (#389)
* docs: parse raw stream * fix: comment
1 parent c657d2c commit 7331c56

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,40 @@ const response = await fgaClient.executeApiRequest({
960960
});
961961
```
962962

963+
#### Example: Calling a Streaming Endpoint
964+
965+
For streaming endpoints (e.g. `streamed-list-objects`), use `executeStreamedApiRequest` instead of `executeApiRequest`. This returns a raw Node.js readable stream. To parse the newline-delimited JSON (NDJSON) response into individual objects, use the `parseNDJSONStream` helper exported by the SDK:
966+
967+
```javascript
968+
const { OpenFgaClient, parseNDJSONStream } = require('@openfga/sdk');
969+
970+
const fgaClient = new OpenFgaClient({
971+
apiUrl: process.env.FGA_API_URL,
972+
storeId: process.env.FGA_STORE_ID,
973+
});
974+
975+
const streamResponse = await fgaClient.executeStreamedApiRequest({
976+
operationName: 'StreamedListObjects',
977+
method: 'POST',
978+
path: '/stores/{store_id}/streamed-list-objects',
979+
pathParams: { store_id: process.env.FGA_STORE_ID },
980+
body: {
981+
authorization_model_id: process.env.FGA_MODEL_ID,
982+
user: 'user:alice',
983+
relation: 'reader',
984+
type: 'document',
985+
},
986+
});
987+
988+
// executeStreamedApiRequest returns the raw Node.js Readable stream directly.
989+
// Use parseNDJSONStream to iterate over the parsed JSON objects:
990+
for await (const item of parseNDJSONStream(streamResponse)) {
991+
console.log('Object:', item.object);
992+
}
993+
```
994+
995+
> **Note:** Unlike `executeApiRequest` (which wraps the response in a `CallResult` with a `$response` property), `executeStreamedApiRequest` returns the raw stream directly. `parseNDJSONStream` handles chunked data, partial lines, and buffer flushing automatically. It accepts Node.js `Readable` streams, `AsyncIterable`s, or even plain strings and `Buffer`s.
996+
963997
### Retries
964998

965999
If a network request fails with a 429 or 5xx error from the server, the SDK will automatically retry the request up to 3 times with a minimum wait time of 100 milliseconds between each attempt.

0 commit comments

Comments
 (0)