Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .github/workflows/cla.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
name: "CLA"
on: # yamllint disable-line rule:truthy
on: # yamllint disable-line rule:truthy
issue_comment:
types:
- "created"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
with:
useLockFile: false
- name: "Run prettier"
run: "CI=true yarn run prettier src -c"
run: "CI=true yarn run oxfmt --check"
- name: "Run lint"
run: "CI=true yarn lint"
- name: "Run publint"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
# yamllint disable rule:line-length
name: "Publish to NPM"
on: # yamllint disable-line rule:truthy
on: # yamllint disable-line rule:truthy
release:
types:
- "published"
Expand Down
4 changes: 4 additions & 0 deletions .oxfmtrc.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "./node_modules/oxfmt/configuration_schema.json",
"ignorePatterns": ["src/authzedapi"],
}
1 change: 0 additions & 1 deletion .prettierignore

This file was deleted.

1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ You can regenerate the code by executing `buf.gen.yaml`:
Releases are published to NPM using a GitHub action.

To create a new release:

1. Use the [yarn version] command to bump the package version.
1. Create a new [release on GitHub] to initiate the release action.

Expand Down
75 changes: 38 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,21 @@ Everything required to connect and make API calls is located in a module respect
You will have to provide a your own API Token from the [Authzed dashboard] in place of `t_your_token_here_1234567deadbeef` in the following example:

```js
import { v1 } from '@authzed/authzed-node';
import { v1 } from "@authzed/authzed-node";
// if your endpoint is localhost
// const client = v1.NewClient('t_your_token_here_1234567deadbeef', 'localhost:50051', v1.ClientSecurity.INSECURE_LOCALHOST_ALLOWED);
const client = v1.NewClient('t_your_token_here_1234567deadbeef');
const client = v1.NewClient("t_your_token_here_1234567deadbeef");
```

Or to use a custom certificate authority, load the CA certificate and pass the file reference to `NewClientWithCustomCert`.

```js
import { v1 } from '@authzed/authzed-node';
import fs from 'fs';
import { v1 } from "@authzed/authzed-node";
import fs from "fs";

const endpoint = 'localhost:50051';
const cert = fs.readFileSync('path/to/cert.pem');
const client = v1.NewClientWithCustomCert('t_your_token_here_1234567deadbeef', endpoint, cert);
const endpoint = "localhost:50051";
const cert = fs.readFileSync("path/to/cert.pem");
const client = v1.NewClientWithCustomCert("t_your_token_here_1234567deadbeef", endpoint, cert);
```

### Performing an API call
Expand All @@ -80,36 +81,36 @@ Because of the verbosity of these types, we recommend writing your own functions
The `create` method on generated classes takes attributes as input and defaults unspecified attributes to their empty value. This allows you to create request messages, for example, by specifying only relevant fields and leaves optional fields empty.

```js
import { v1 } from '@authzed/authzed-node';
import { v1 } from "@authzed/authzed-node";

const client = v1.NewClient('token')
const client = v1.NewClient("token");

// Create the relationship between the resource and the user.
const firstPost = v1.ObjectReference.create({
objectType: "blog/post",
objectId: "1",
objectType: "blog/post",
objectId: "1",
});

// Create the user reference.
const emilia = v1.ObjectReference.create({
objectType: "blog/user",
objectId: "emilia",
objectType: "blog/user",
objectId: "emilia",
});

// Create the subject reference using the user reference
const subject = v1.SubjectReference.create({
object: emilia,
object: emilia,
});

const checkPermissionRequest = v1.CheckPermissionRequest.create({
resource: firstPost,
permission: "read",
subject,
resource: firstPost,
permission: "read",
subject,
});

client.checkPermission(checkPermissionRequest, (err, response) => {
console.log(response);
console.log(err);
console.log(response);
console.log(err);
});
```

Expand All @@ -131,9 +132,9 @@ const result = await promiseClient.checkPermission(checkPermissionRequest);
For stream-returning methods, including `client.readRelationships()`, `client.lookupResources()` and `client.lookupSubjects()`, the promise-style method will result in an array of response objects once the stream has been closed.

```js
import { v1 } from '@authzed/authzed-node';
import { v1 } from "@authzed/authzed-node";

const client = v1.NewClient('token');
const client = v1.NewClient("token");
const { promises: promiseClient } = client; // access client.promises

const results = await promiseClient.readRelationships(/** req **/);
Expand All @@ -150,28 +151,28 @@ writing context will fail silently and the relations won't reflect the context.
An example:

```js
import { protobuf } from '@authzed/authzed-node';
import { protobuf } from "@authzed/authzed-node";
const { Struct } = protobuf;

const writeRequest = WriteRelationshipsRequest.create({
updates: [
RelationshipUpdate.create({
relationship: Relationship.create({
resource: resource,
relation: "caveated_viewer",
subject: SubjectReference.create({
object: testUser,
}),
optionalCaveat: ContextualizedCaveat.create({
caveatName: "has_special_attribute",
context: Struct.fromJson({
special: true,
updates: [
RelationshipUpdate.create({
relationship: Relationship.create({
resource: resource,
relation: "caveated_viewer",
subject: SubjectReference.create({
object: testUser,
}),
optionalCaveat: ContextualizedCaveat.create({
caveatName: "has_special_attribute",
context: Struct.fromJson({
special: true,
}),
}),
}),
operation: RelationshipUpdate_Operation.CREATE,
}),
operation: RelationshipUpdate_Operation.CREATE,
}),
],
],
});

const response = await client.writeRelationships(writeRequest);
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
services:
spicedb-servetesting:
image: quay.io/authzed/spicedb:latest
ports: ['50051:50051']
ports: ["50051:50051"]
command: serve-testing
healthcheck:
test: ["CMD", "/usr/local/bin/grpc_health_probe", "-addr=:50051"]
Expand Down
34 changes: 19 additions & 15 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,36 @@ import { FlatCompat } from "@eslint/eslintrc";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
allConfig: js.configs.all
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
allConfig: js.configs.all,
});

export default [{
export default [
{
ignores: ["src/authzedapi/**/*", "src/*.test.js"],
}, ...compat.extends(
},
...compat.extends(
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
), {
),
{
plugins: {
"@typescript-eslint": typescriptEslint,
"@typescript-eslint": typescriptEslint,
},

languageOptions: {
globals: {
...globals.browser,
...globals.commonjs,
},
globals: {
...globals.browser,
...globals.commonjs,
},

parser: tsParser,
ecmaVersion: 12,
sourceType: "script",
parser: tsParser,
ecmaVersion: 12,
sourceType: "script",
},

rules: {},
}];
},
];
41 changes: 20 additions & 21 deletions examples/v1/example.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { v1 } from '@authzed/authzed-node';
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');
const client = v1.NewClient("mytokenhere");
const { promises: promiseClient } = client; // access client.promises after instantiating client

const writeRequest = v1.WriteSchemaRequest.create({
Expand All @@ -28,14 +28,14 @@ const writeRelationshipRequest = v1.WriteRelationshipsRequest.create({
v1.RelationshipUpdate.create({
relationship: v1.Relationship.create({
resource: v1.ObjectReference.create({
objectType: 'test/document',
objectId: 'somedocument',
objectType: "test/document",
objectId: "somedocument",
}),
relation: 'viewer',
relation: "viewer",
subject: v1.SubjectReference.create({
object: v1.ObjectReference.create({
objectType: 'test/user',
objectId: 'fred',
objectType: "test/user",
objectId: "fred",
}),
}),
}),
Expand All @@ -54,19 +54,19 @@ await new Promise((resolve, reject) => {
// Check a permission.
const checkPermissionRequest = v1.CheckPermissionRequest.create({
resource: v1.ObjectReference.create({
objectType: 'test/document',
objectId: 'somedocument',
objectType: "test/document",
objectId: "somedocument",
}),
permission: 'view',
permission: "view",
subject: v1.SubjectReference.create({
object: v1.ObjectReference.create({
objectType: 'test/user',
objectId: 'fred',
objectType: "test/user",
objectId: "fred",
}),
}),
consistency: v1.Consistency.create({
requirement: {
oneofKind: 'fullyConsistent',
oneofKind: "fullyConsistent",
fullyConsistent: true,
},
}),
Expand All @@ -80,29 +80,28 @@ const checkResult = await new Promise((resolve, reject) => {
});

console.log(
checkResult.permissionship ===
v1.CheckPermissionResponse_Permissionship.HAS_PERMISSION
checkResult.permissionship === v1.CheckPermissionResponse_Permissionship.HAS_PERMISSION,
);

// Lookup Resources

const lookupResourcesRequest = v1.LookupResourcesRequest.create({
consistency: v1.Consistency.create({
requirement: {
oneofKind: 'fullyConsistent',
oneofKind: "fullyConsistent",
fullyConsistent: true,
},
}),
resourceObjectType: 'test/document',
permission: 'view',
resourceObjectType: "test/document",
permission: "view",
subject: v1.SubjectReference.create({
object: v1.ObjectReference.create({
objectType: 'test/user',
objectId: 'fred',
objectType: "test/user",
objectId: "fred",
}),
}),
});

const results = await promiseClient.lookupResources(lookupResourcesRequest)
const results = await promiseClient.lookupResources(lookupResourcesRequest);

console.log(results);
10 changes: 5 additions & 5 deletions js-dist/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"name": "@authzed/authzed-js-node",
"author": "authzed",
"version": "0.19.0",
"type": "module",
"private": false,
"description": "authzed js client for nodejs",
"keywords": [
"authorization",
Expand All @@ -13,10 +12,11 @@
"spicedb",
"zanzibar"
],
"main": "src/index.js",
"repository": "https://github.com/authzed/authzed-node",
"license": "Apache-2.0",
"private": false,
"author": "authzed",
"repository": "https://github.com/authzed/authzed-node",
"type": "module",
"main": "src/index.js",
"scripts": {
"test": "../scripts/run-and-test.sh",
"only-run-tests": "vitest"
Expand Down
Loading