Unofficial Javascript / Typescript client for Ory Keto using Axios.
This package is currently compatible with Keto v0.8.0-alpha.2
import { Keto } from 'keto-client'
const api = new Keto.Client({
readUrl: "keto.url:4466",
writeUrl: "keto.url:4467",
axios: {}, // Optional Axios Config
headers: {} // Optional Request Headers
})
const Keto = require("keto-client").Keto
const api = new Keto.Client({
readUrl: "keto.url:4466",
writeUrl: "keto.url:4467",
axios: {}, // Optional Axios Config
headers: {} // Optional Request Headers
})
const { status } = await api.MetadataApi.GetServerStatus()
const { status } = await api.MetadataApi.GetServerAndDatabaseStatus()
const { version } = await api.MetadataApi.GetVersion()
NOTE: According to the Official Ory Keto documentation there are two endpoints to check a tuple, one using a GET request, one using a POST request, for completeness both are implemented, both take the same input and return the same output, the two method are Keto.ReadApi.CheckGet
and Keto.ReadApi.CheckPost
.
const { allowed } = await api.ReadApi.CheckGet({
namespace: "books",
object: "book1",
relation: "read",
subject_id: "user1234",
max_depth: 5,
})
Using a Subject Set:
const { allowed } = await api.ReadApi.CheckGet({
namespace: "books",
object: "book1",
relation: "read",
subject_set: {
namespace: "groups",
object: "readers",
relation: "member"
},
max_depth: 5,
})
const subjects: ExpandResponse = await api.ReadApi.Expand({
namespace: "books",
object: "book1",
relation: "read",
max_depth: 5
})
interface ExpandResponse {
children: ExpandResponse[]
subject_set?: SubjectSet
subject_id?: string
type: string
}
NOTE: Only the namespace field is required
// Get all tuples in namespace books, 100 at a time, starting from token
const tuples = await api.ReadApi.Query({
namespace: "books"
page_size: 100,
page_token: "..."
})
// Get all tuples in namespace books related to object book1
const tuples = await api.ReadApi.Query({
namespace: "books",
object: "book1"
})
// Get all tuples in namespace books related to subject user1234
const tuples = await api.ReadApi.Query({
namespace: "books",
subject_id: "user1234"
})
// Get all tuples in namespace books related to subject set readers
const tuples = await api.ReadApi.Query({
namespace: "books",
"subject_set.namespace": "groups",
"subject_set.object": "readers"
})
type QueryResponse = {
next_page_token: string
relation_tuples: RelationTuple[]
}
type RelationTuple = {
namespace: string
object: string
relation: string
subject_id?: string
subject_set?: SubjectSet
}
const tuple = await api.WriteApi.Create({
namespace: "books",
object: "book1",
relation: "read",
subject_id: "user1234"
})
await api.WriteApi.Delete({
namespace: "books",
object: "book1",
relation: "read",
subject_id: "user1234"
})
await api.WriteApi.Delete({
namespace: "books",
object: "book1",
relation: "read",
subject_set: {
namespace: "groups",
object: "readers",
relation: "member"
}
})
const data: PatchMultipleRequest = [
{
action: "insert",
relation_tuple: {
namespace: "books",
object: "book1",
relation: "read",
subject_id: "user1234"
}
},
{
action: "insert",
relation_tuple: {
namespace: "books",
object: "book1",
relation: "read",
subject_set: {
namespace: "groups",
object: "readers",
relation: "member"
}
}
},
{
action: "delete",
relation_tuple: {
namespace: "books",
object: "book2",
relation: "read",
subject_id: "user1234"
}
},
...
]
await api.WriteApi.PatchMultiple(data)
I created this package since the official JS/TS version is auto-generated from the API config using OpenAPI Generator and I found it extremely impractical to use.