Skip to content
This repository was archived by the owner on Jul 1, 2024. It is now read-only.

Commit e479961

Browse files
chendrixsrenatus
andauthored
Update readme (#55)
* Update readme * Update README for npm.js search bars * Remove rewrite of SDK example usage as it's now explicit in the README * Remove broken Speakeasy generated example usages --------- Co-authored-by: Stephan Renatus <stephan@styra.com>
1 parent 76df9e6 commit e479961

File tree

2 files changed

+14
-195
lines changed

2 files changed

+14
-195
lines changed

README.md

Lines changed: 14 additions & 191 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
# OPA Typescript SDK
22

3-
Styra's OPA SDK
3+
The Styra-supported driver to connect to Open Policy Agent (OPA) and Enterprise OPA deployments.
44

55
[![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
66
[![NPM Version](https://img.shields.io/npm/v/%40styra%2Fopa?style=flat&color=%2324b6e0)](https://www.npmjs.com/package/@styra/opa)
77

8+
> [!IMPORTANT]
9+
> The documentation for this SDK lives at https://docs.styra.com/sdk, with reference documentation available at https://styrainc.github.io/opa-typescript
10+
11+
You can use the Styra OPA SDK to connect to [Open Policy Agent](https://www.openpolicyagent.org/) and [Enterprise OPA](https://www.styra.com/enterprise-opa/) deployments.
12+
813
<!-- Start SDK Installation [installation] -->
914
## SDK Installation
1015

@@ -251,37 +256,13 @@ Please refer to [the repository's README.md](https://github.com/StyraInc/opa-typ
251256
252257
---
253258

254-
<!-- Start SDK Example Usage [usage] -->
255-
## SDK Example Usage
256-
257-
### Example
258-
259-
```typescript
260-
import { OpaApiClient } from "@styra/opa";
261-
262-
const opaApiClient = new OpaApiClient();
263-
264-
async function run() {
265-
const result = await opaApiClient.executePolicyWithInput({
266-
path: "app/rbac",
267-
requestBody: {
268-
input: {
269-
user: "alice",
270-
action: "read",
271-
object: "id123",
272-
type: "dog",
273-
},
274-
},
275-
});
276-
277-
// Handle the result
278-
console.log(result);
279-
}
259+
# OPA OpenAPI SDK (low-level)
280260

281-
run();
261+
<!--
262+
We've removed most of the auto-generated Speakeasy examples because they generate the wrong import path.
263+
-->
282264

283-
```
284-
<!-- End SDK Example Usage [usage] -->
265+
<!-- No SDK Example Usage [usage] -->
285266

286267
<!-- Start Available Resources and Operations [operations] -->
287268
## Available Resources and Operations
@@ -293,169 +274,11 @@ run();
293274
* [health](docs/sdks/opaapiclient/README.md#health) - Verify the server is operational
294275
<!-- End Available Resources and Operations [operations] -->
295276

296-
<!-- Start Error Handling [errors] -->
297-
## Error Handling
298-
299-
All SDK methods return a response object or throw an error. If Error objects are specified in your OpenAPI Spec, the SDK will throw the appropriate Error type.
300-
301-
| Error Object | Status Code | Content Type |
302-
| ------------------ | ------------------ | ------------------ |
303-
| errors.ClientError | 400 | application/json |
304-
| errors.ServerError | 500 | application/json |
305-
| errors.SDKError | 4xx-5xx | */* |
306-
307-
Validation errors can also occur when either method arguments or data returned from the server do not match the expected format. The `SDKValidationError` that is thrown as a result will capture the raw value that failed validation in an attribute called `rawValue`. Additionally, a `pretty()` method is available on this error that can be used to log a nicely formatted string since validation errors can list many issues and the plain error string may be difficult read when debugging.
308-
309-
310-
```typescript
311-
import { OpaApiClient } from "@styra/opa";
312-
import * as errors from "@styra/opa/sdk/models/errors";
313-
314-
const opaApiClient = new OpaApiClient();
315-
316-
async function run() {
317-
let result;
318-
try {
319-
result = await opaApiClient.executePolicy({
320-
path: "app/rbac",
321-
});
322-
} catch (err) {
323-
switch (true) {
324-
case err instanceof errors.SDKValidationError: {
325-
// Validation errors can be pretty-printed
326-
console.error(err.pretty());
327-
// Raw value may also be inspected
328-
console.error(err.rawValue);
329-
return;
330-
}
331-
case err instanceof errors.ClientError: {
332-
console.error(err); // handle exception
333-
return;
334-
}
335-
case err instanceof errors.ServerError: {
336-
console.error(err); // handle exception
337-
return;
338-
}
339-
default: {
340-
throw err;
341-
}
342-
}
343-
}
344-
345-
// Handle the result
346-
console.log(result);
347-
}
348-
349-
run();
350-
351-
```
352-
<!-- End Error Handling [errors] -->
353-
354-
<!-- Start Server Selection [server] -->
355-
## Server Selection
356-
357-
### Select Server by Index
358-
359-
You can override the default server globally by passing a server index to the `serverIdx` optional parameter when initializing the SDK client instance. The selected server will then be used as the default on the operations that use it. This table lists the indexes associated with the available servers:
360-
361-
| # | Server | Variables |
362-
| - | ------ | --------- |
363-
| 0 | `http://localhost:8181` | None |
364-
365-
```typescript
366-
import { OpaApiClient } from "@styra/opa";
367-
368-
const opaApiClient = new OpaApiClient({
369-
serverIdx: 0,
370-
});
371-
372-
async function run() {
373-
const result = await opaApiClient.executePolicy({
374-
path: "app/rbac",
375-
});
376-
377-
// Handle the result
378-
console.log(result);
379-
}
380-
381-
run();
382-
383-
```
384-
277+
<!-- No Error Handling [errors] -->
385278

386-
### Override Server URL Per-Client
279+
<!-- No Server Selection [server] -->
387280

388-
The default server can also be overridden globally by passing a URL to the `serverURL` optional parameter when initializing the SDK client instance. For example:
389-
390-
```typescript
391-
import { OpaApiClient } from "@styra/opa";
392-
393-
const opaApiClient = new OpaApiClient({
394-
serverURL: "http://localhost:8181",
395-
});
396-
397-
async function run() {
398-
const result = await opaApiClient.executePolicy({
399-
path: "app/rbac",
400-
});
401-
402-
// Handle the result
403-
console.log(result);
404-
}
405-
406-
run();
407-
408-
```
409-
<!-- End Server Selection [server] -->
410-
411-
<!-- Start Custom HTTP Client [http-client] -->
412-
## Custom HTTP Client
413-
414-
The TypeScript SDK makes API calls using an `HTTPClient` that wraps the native
415-
[Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API). This
416-
client is a thin wrapper around `fetch` and provides the ability to attach hooks
417-
around the request lifecycle that can be used to modify the request or handle
418-
errors and response.
419-
420-
The `HTTPClient` constructor takes an optional `fetcher` argument that can be
421-
used to integrate a third-party HTTP client or when writing tests to mock out
422-
the HTTP client and feed in fixtures.
423-
424-
The following example shows how to use the `"beforeRequest"` hook to to add a
425-
custom header and a timeout to requests and how to use the `"requestError"` hook
426-
to log errors:
427-
428-
```typescript
429-
import { OpaApiClient } from "@styra/opa";
430-
import { HTTPClient } from "@styra/opa/lib/http";
431-
432-
const httpClient = new HTTPClient({
433-
// fetcher takes a function that has the same signature as native `fetch`.
434-
fetcher: (request) => {
435-
return fetch(request);
436-
}
437-
});
438-
439-
httpClient.addHook("beforeRequest", (request) => {
440-
const nextRequest = new Request(request, {
441-
signal: request.signal || AbortSignal.timeout(5000);
442-
});
443-
444-
nextRequest.headers.set("x-custom-header", "custom value");
445-
446-
return nextRequest;
447-
});
448-
449-
httpClient.addHook("requestError", (error, request) => {
450-
console.group("Request Error");
451-
console.log("Reason:", `${error}`);
452-
console.log("Endpoint:", `${request.method} ${request.url}`);
453-
console.groupEnd();
454-
});
455-
456-
const sdk = new OpaApiClient({ httpClient });
457-
```
458-
<!-- End Custom HTTP Client [http-client] -->
281+
<!-- No Custom HTTP Client [http-client] -->
459282

460283
<!-- Placeholder for Future Speakeasy SDK Sections -->
461284

typedoc.config.cjs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ module.exports = {
3535
pattern: `> For low-level SDK usage, see the sections below.\n\n---`,
3636
replace: "",
3737
},
38-
{
39-
pattern: "## SDK Example Usage\n",
40-
replace: "## Low-level SDK Examples", // TODO(sr): insert more caveats?
41-
},
4238
{
4339
// this captures all links to speakeasy's generated docs
4440
pattern: "docs/sdks/opaapiclient/README\\.md",

0 commit comments

Comments
 (0)