Skip to content

feat(sdk): connect rpc client export #545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 8, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -45,3 +45,6 @@ format license-check lint test: ci

doc:
cd lib && npm ci && npm run doc

generate-platform:
./scripts/platform.sh
114 changes: 57 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ const clientId = "applicationNameFromIdP";
const refreshToken = "refreshTokenValueFromIdP";

// AuthProviders are middlewares that add `Authorization` or other bearer tokens to requests.
// These include The `refresh` provider can be handed a refresh and optional access token.
// These include The `refresh` provider can be handed a refresh and optional access token.
const authProvider = await AuthProviders.refreshAuthProvider({
clientId,
exchange: 'refresh',
@@ -79,6 +79,61 @@ A more complete example of using an OIDC compatible provider
with support for authorization code flow with PKCE and DPoP
is available in the [sample `web-app` folder](./web-app/src/session.ts)

## Platform Client

The Platform Client provides an interface to interact with the OpenTDF platform's RPC services.

### Usage Example

Below is an example of how to use the `PlatformClient` to interact with the platform's RPC services.

```typescript
import { AuthProvider, OpenTDF } from '@opentdf/sdk';
import { PlatformClient } from '@opentdf/sdk/platform';

const authProvider: AuthProvider = {/* configure your auth provider */};
const platform = new PlatformClient({
authProvider,
platformUrl: '/api',
});

async function exampleUsage() {
// Fetch well-known configuration
const wellKnownResponse = await platform.v1.wellknown.getWellKnownConfiguration({});
console.log('Well-known configuration:', wellKnownResponse.configuration);

// List policy attributes
const attributesResponse = await platform.v1.attributes.listAttributes({});
console.log('Policy Attributes:', attributesResponse.attributes);
}

exampleUsage();
```

### Using Interceptor

The `PlatformClient` client supports the use of interceptors for customizing RPC calls. Interceptors allow you to modify requests or responses, such as adding custom headers or handling authentication, before or after the RPC call is executed.

Below is an example of using an interceptor to add an `Authorization` header to all outgoing requests:

```typescript
import { platformConnect, PlatformClient } from '@opentdf/sdk/platform';

const authInterceptor: platformConnect.Interceptor = (next) => async (req) => {
req.header.set('Authorization', `Bearer ${accessToken}`);
return await next(req); // Pass the modified request to the next handler in the chain
};

const platform = new PlatformClient({
interceptors: [authInterceptor], // Attach the interceptor
platformUrl: '/api',
});
```

### Key Notes
Interceptors are particularly useful for scenarios where you need to dynamically modify requests, such as adding authentication tokens or logging request/response data.


## Build and Test

```shell
@@ -105,59 +160,4 @@ To check out, build, and validate your installation, and test the sample web app
nvm use
make test
make start
```

## Use the platform

Version 2 of this library adds support for ABAC management tasks.
This is provided with the [opentdf Platform](https://github.com/opentdf/platform).

### Generate Typescript code from platform protobufs

```sh
scripts/platform.sh
```

This will clone the platform repo and generate Typescript code in `lib/src/platform`.

### Import Typescript code

```ts
import { GetAttributeRequest } from './lib/src/platform/policy/attributes/attributes_pb';
import { Attribute, AttributeRuleTypeEnum } from './lib/src/platform/policy/objects_pb';
import {
createConnectTransport,
} from '@connectrpc/connect-web'
import {
createPromiseClient,
} from '@connectrpc/connect'

const attrData = {
name: "my-attr",
rule: AttributeRuleTypeEnum.ALL_OF,
namespace: {name: 'my-namespace'},
values: [{value: 'my-value'}],
active: true,
extraField: 'this will be ignored' // only proto defined fields and value types are respected
}
const attr = new Attribute(attrData);
console.log(attr.toJson());

// {
// namespace: { name: 'my-namespace' },
// name: 'my-attr',
// rule: 'ATTRIBUTE_RULE_TYPE_ENUM_ALL_OF',
// values: [ { value: 'my-value' } ],
// active: true
// }

const req = new GetAttributeRequest({id: 'uuid-here'});
const client = createPromiseClient(
AttributesService,
createConnectTransport({
baseUrl: 'localhost:8080',
})
)
```

This is an example to instantiate an `Attribute` and create a `GetAttributeRequest`.
```
13 changes: 0 additions & 13 deletions buf.gen.yaml

This file was deleted.

41 changes: 30 additions & 11 deletions cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions lib/buf.gen.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: v2

clean: true
plugins:
- local: protoc-gen-es
out: src/platform
include_imports: true
opt: target=ts,import_extension=.js
Loading
Loading