Skip to content

Commit 9b4fe29

Browse files
authored
feat: support list users (#97)
2 parents c5b786f + 7d42e75 commit 9b4fe29

17 files changed

+972
-342
lines changed

.github/dependabot.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ updates:
88
dependencies:
99
patterns:
1010
- "*"
11-
exclude-patterns:
12-
- "eslint"
1311
- package-ecosystem: "github-actions"
1412
directory: "/"
1513
schedule:
@@ -18,3 +16,5 @@ updates:
1816
dependencies:
1917
patterns:
2018
- "*"
19+
exclude-patterns:
20+
- "eslint"

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# Changelog
22

3+
## v0.4.0
4+
5+
### [0.4.0](https://github.com/openfga/js-sdk/compare/v0.3.5...v0.4.0) (2024-04-30)
6+
7+
- feat: support the [ListUsers](https://github.com/openfga/rfcs/blob/main/20231214-listUsers-api.md) endpoint (#97)
8+
- feat!: support overriding storeId per request (#97)
9+
`OpenFgaClient` now supports specifying the storeId in the options to override it per request
10+
11+
[BREAKING CHANGE] the underlying `OpenFgaApi` now expects `storeId` as the first param on relevant methods,
12+
if you are still using this class, make sure you update your references when needed.
13+
314
## v0.3.5
415

516
### [0.3.5](https://github.com/openfga/js-sdk/compare/v0.3.4...v0.3.5) (2024-03-19)

README.md

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ This is an autogenerated JavaScript SDK for OpenFGA. It provides a wrapper aroun
3838
- [Expand](#expand)
3939
- [List Objects](#list-objects)
4040
- [List Relations](#list-relations)
41+
- [List Users](#list-users)
4142
- [Assertions](#assertions)
4243
- [Read Assertions](#read-assertions)
4344
- [Write Assertions](#write-assertions)
@@ -97,7 +98,7 @@ const { OpenFgaClient } = require('@openfga/sdk'); // OR import { OpenFgaClient
9798
const fgaClient = new OpenFgaClient({
9899
apiUrl: process.env.FGA_API_URL, // required
99100
storeId: process.env.FGA_STORE_ID, // not needed when calling `CreateStore` or `ListStores`
100-
authorizationModelId: process.env.FGA_AUTHORIZATION_MODEL_ID, // Optional, can be overridden per request
101+
authorizationModelId: process.env.FGA_MODEL_ID, // Optional, can be overridden per request
101102
});
102103
```
103104

@@ -109,7 +110,7 @@ const { OpenFgaClient } = require('@openfga/sdk'); // OR import { OpenFgaClient
109110
const fgaClient = new OpenFgaClient({
110111
apiUrl: process.env.FGA_API_URL, // required
111112
storeId: process.env.FGA_STORE_ID, // not needed when calling `CreateStore` or `ListStores`
112-
authorizationModelId: process.env.FGA_AUTHORIZATION_MODEL_ID, // Optional, can be overridden per request
113+
authorizationModelId: process.env.FGA_MODEL_ID, // Optional, can be overridden per request
113114
credentials: {
114115
method: CredentialsMethod.ApiToken,
115116
config: {
@@ -127,7 +128,7 @@ const { OpenFgaClient } = require('@openfga/sdk'); // OR import { OpenFgaClient
127128
const fgaClient = new OpenFgaClient({
128129
apiUrl: process.env.FGA_API_URL, // required
129130
storeId: process.env.FGA_STORE_ID, // not needed when calling `CreateStore` or `ListStores`
130-
authorizationModelId: process.env.FGA_AUTHORIZATION_MODEL_ID, // Optional, can be overridden per request
131+
authorizationModelId: process.env.FGA_MODEL_ID, // Optional, can be overridden per request
131132
credentials: {
132133
method: CredentialsMethod.ClientCredentials,
133134
config: {
@@ -462,7 +463,7 @@ const { responses } = await fgaClient.batchCheck([{
462463
user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
463464
relation: "viewer",
464465
object: "document:roadmap",
465-
contextualTuples: [{
466+
contextual_tuples: [{
466467
user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
467468
relation: "writer",
468469
object: "document:roadmap"
@@ -491,7 +492,7 @@ responses = [{
491492
user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
492493
relation: "viewer",
493494
object: "document:roadmap",
494-
contextualTuples: [{
495+
contextual_tuples: [{
495496
user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
496497
relation: "writer",
497498
object: "document:roadmap"
@@ -537,7 +538,7 @@ const response = await fgaClient.listObjects({
537538
user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
538539
relation: "viewer",
539540
type: "document",
540-
contextualTuples: [{
541+
contextual_tuples: [{
541542
user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
542543
relation: "writer",
543544
object: "document:budget"
@@ -563,7 +564,7 @@ const response = await fgaClient.listRelations({
563564
user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
564565
object: "document:roadmap",
565566
relations: ["can_view", "can_edit", "can_delete"],
566-
contextualTuples: [{
567+
contextual_tuples: [{
567568
user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
568569
relation: "writer",
569570
object: "document:roadmap"
@@ -573,6 +574,49 @@ const response = await fgaClient.listRelations({
573574
// response.relations = ["can_view", "can_edit"]
574575
```
575576

577+
##### List Users
578+
579+
List the users who have a certain relation to a particular type.
580+
581+
[API Documentation](https://openfga.dev/api/service#/Relationship%20Queries/ListUsers)
582+
583+
```js
584+
const options = {};
585+
586+
// To override the authorization model id for this request
587+
options.authorization_model_id = "1uHxCSuTP0VKPYSnkq1pbb1jeZw";
588+
589+
// Only a single filter is allowed for the time being
590+
const userFilters = [{type: "user"}];
591+
// user filters can also be of the form
592+
// const userFilters = [{type: "team", relation: "member"}];
593+
594+
const response = await fgaClient.listUsers({
595+
object: {
596+
type: "document",
597+
id: "roadmap"
598+
},
599+
relation: "can_read",
600+
user_filters: userFilters,
601+
context: {
602+
"view_count": 100
603+
},
604+
contextualTuples:
605+
[{
606+
user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
607+
relation: "editor",
608+
object: "folder:product"
609+
}, {
610+
user: "folder:product",
611+
relation: "parent",
612+
object: "document:roadmap"
613+
}]
614+
}, options);
615+
616+
// response.users = [{object: {type: "user", id: "81684243-9356-4421-8fbf-a4f8d36aa31b"}}, {userset: { type: "user" }}, ...]
617+
// response.excluded_users = [ {object: {type: "user", id: "4a455e27-d15a-4434-82e0-136f9c2aa4cf"}}, ... ]
618+
```
619+
576620
#### Assertions
577621

578622
##### Read Assertions
@@ -622,17 +666,19 @@ const response = await fgaClient.writeAssertions([{
622666

623667
### Retries
624668

625-
By default API requests are retried up to 15 times on 429 and 5xx errors and credential requests are retried to 3 times on 429 and 5xx errors. In both instances they will wait a minimum of 100 milliseconds between requests and up to a maximum of
669+
If a network request fails with a 429 or 5xx error from the server, the SDK will automatically retry the request up to 15 times with a minimum wait time of 100 milliseconds between each attempt.
670+
671+
To customize this behavior, create an object with `maxRetry` and `minWaitInMs` properties. `maxRetry` determines the maximum number of retries (up to 15), while `minWaitInMs` sets the minimum wait time between retries in milliseconds.
626672

627-
In order to change the behavior for API requests, pass a `retryParams` object in the `OpenFgaClient` constructor with a `maxRetry` property to control the amount of retries and a `minWaitInMs` to control the minimum wait time between retried requests.
673+
Apply your custom retry values by setting to `retryParams` on the to the configuration object passed to the `OpenFgaClient` call.
628674

629675
```javascript
630676
const { OpenFgaClient } = require('@openfga/sdk'); // OR import { OpenFgaClient } from '@openfga/sdk';
631677

632678
const fgaClient = new OpenFgaClient({
633679
apiUrl: process.env.FGA_API_URL, // required
634680
storeId: process.env.FGA_STORE_ID, // not needed when calling `CreateStore` or `ListStores`
635-
authorizationModelId: process.env.FGA_AUTHORIZATION_MODEL_ID, // Optional, can be overridden per request
681+
authorizationModelId: process.env.FGA_MODEL_ID, // Optional, can be overridden per request
636682
retryParams: {
637683
maxRetry: 3, // retry up to 3 times on API requests
638684
minWaitInMs: 250 // wait a minimum of 250 milliseconds between requests

0 commit comments

Comments
 (0)