From 2a3b28642d789a06636c96934654fb529479d157 Mon Sep 17 00:00:00 2001 From: ivanauth Date: Wed, 8 Jul 2026 20:34:09 -0400 Subject: [PATCH] docs: add example using the promisified client Signed-off-by: ivanauth --- examples/v1/example-promises.js | 103 ++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 examples/v1/example-promises.js diff --git a/examples/v1/example-promises.js b/examples/v1/example-promises.js new file mode 100644 index 0000000..acb1d7d --- /dev/null +++ b/examples/v1/example-promises.js @@ -0,0 +1,103 @@ +import { v1 } from "@authzed/authzed-node"; +// set up it on localhost like this: +// const client = v1.NewClient('mytokenhere', 'localhost:50051', v1.ClientSecurity.INSECURE_LOCALHOST_ALLOWED); +const client = v1.NewClient("mytokenhere"); + +// Each callback-style method on the client has a promise-style equivalent +// available at the `.promises` property. +const { promises: promiseClient } = client; + +try { + const writeRequest = v1.WriteSchemaRequest.create({ + schema: `definition test/user {} + + definition test/document { + relation viewer: test/user + permission view = viewer + } + `, + }); + + // Write a schema. + await promiseClient.writeSchema(writeRequest); + + // Write a relationship. + const writeRelationshipRequest = v1.WriteRelationshipsRequest.create({ + updates: [ + v1.RelationshipUpdate.create({ + relationship: v1.Relationship.create({ + resource: v1.ObjectReference.create({ + objectType: "test/document", + objectId: "somedocument", + }), + relation: "viewer", + subject: v1.SubjectReference.create({ + object: v1.ObjectReference.create({ + objectType: "test/user", + objectId: "fred", + }), + }), + }), + operation: v1.RelationshipUpdate_Operation.CREATE, + }), + ], + }); + + await promiseClient.writeRelationships(writeRelationshipRequest); + + // Check a permission. + const checkPermissionRequest = v1.CheckPermissionRequest.create({ + resource: v1.ObjectReference.create({ + objectType: "test/document", + objectId: "somedocument", + }), + permission: "view", + subject: v1.SubjectReference.create({ + object: v1.ObjectReference.create({ + objectType: "test/user", + objectId: "fred", + }), + }), + consistency: v1.Consistency.create({ + requirement: { + oneofKind: "fullyConsistent", + fullyConsistent: true, + }, + }), + }); + + const checkResult = await promiseClient.checkPermission(checkPermissionRequest); + + console.log( + checkResult.permissionship === v1.CheckPermissionResponse_Permissionship.HAS_PERMISSION, + ); + + // Lookup Resources + // For stream-returning methods, the promise-style method resolves to an array + // of response objects once the stream has been closed. + + const lookupResourcesRequest = v1.LookupResourcesRequest.create({ + consistency: v1.Consistency.create({ + requirement: { + oneofKind: "fullyConsistent", + fullyConsistent: true, + }, + }), + resourceObjectType: "test/document", + permission: "view", + subject: v1.SubjectReference.create({ + object: v1.ObjectReference.create({ + objectType: "test/user", + objectId: "fred", + }), + }), + }); + + const results = await promiseClient.lookupResources(lookupResourcesRequest); + + console.log(results); +} finally { + // Close the client so the underlying gRPC channel is released and the process + // can exit. + client.close(); +}