Files
upstream-ory-keto/contrib/docs-code-samples/expand-api-display-access/01-expand-beach/index.js
upstream-mirror 9456a4e922
Some checks failed
CI / Run lints and checks (push) Failing after 47s
CI / Run tests (push) Failing after 1s
CI / Run race tests (push) Failing after 5s
CI / Build CLI docs (push) Has been skipped
CI / Run buf actions (push) Failing after 5s
CodeQL / Analyze (go) (push) Failing after 3s
CodeQL / Analyze (javascript) (push) Failing after 2s
Docker Image Scanners / scanners (push) Failing after 3s
Format / format (push) Failing after 3s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Draft newsletter (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
snapshot of https://github.com/ory/keto.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:08:34 +05:00

72 lines
1.8 KiB
JavaScript

// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
import grpc from "@ory/keto-grpc-client/node_modules/@grpc/grpc-js/build/src/index.js"
import { relationTuples, expand, expandService } from "@ory/keto-grpc-client"
const expandClient = new expandService.ExpandServiceClient(
"127.0.0.1:4466",
grpc.credentials.createInsecure(),
)
const subjectSet = new relationTuples.SubjectSet()
subjectSet.setNamespace("files")
subjectSet.setRelation("access")
subjectSet.setObject("/photos/beach.jpg")
const sub = new relationTuples.Subject()
sub.setSet(subjectSet)
const expandRequest = new expand.ExpandRequest()
expandRequest.setSubject(sub)
expandRequest.setMaxDepth(3)
// helper to get a nice result
const subjectJSON = (subject) => {
if (subject.hasId()) {
return { subject_id: subject.getId() }
}
const set = subject.getSet()
return {
subject_set: {
namespace: set.getNamespace(),
object: set.getObject(),
relation: set.getRelation(),
},
}
}
// helper to get a nice result
const prettyTree = (tree) => {
const [nodeType, tuple, children] = [
tree.getNodeType(),
{
tuple: {
namespace: "",
object: "",
relation: "",
...subjectJSON(tree.getSubject()),
},
},
tree.getChildrenList(),
]
switch (nodeType) {
case expand.NodeType.NODE_TYPE_LEAF:
return { type: "leaf", ...tuple }
case expand.NodeType.NODE_TYPE_UNION: {
const mapped = children.map(prettyTree)
return mapped.length > 0
? { type: "union", children: mapped, ...tuple }
: { type: "union", ...tuple }
}
}
}
expandClient.expand(expandRequest, (error, resp) => {
if (error) {
console.log("Encountered error:", error)
} else {
console.log(JSON.stringify(prettyTree(resp.getTree()), null, 2))
}
})