You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This client makes it easy to interact with [Permify](https://github.com/Permify/permify) from your Node.js application.
17
+
This client makes it easy to interact with [Permify](https://github.com/Permify/permify) from your Node.js application, providing type-safe access to Permify's authorization capabilities.
18
+
19
+
## Features
20
+
21
+
- Full TypeScript support
22
+
- Promise-based API
23
+
- Support for all Permify gRPC endpoints
24
+
- Built-in error handling
25
+
- Interceptor support for authentication and logging
26
+
- Streaming support for real-time updates
18
27
19
28
# Installation
20
29
@@ -30,37 +39,64 @@ Use yarn to install (Please be aware that Yarn versions greater than v1.10.0 and
30
39
yarn add @permify/permify-node
31
40
```
32
41
33
-
# How to use
42
+
# Getting Started
43
+
44
+
## Prerequisites
45
+
46
+
- Node.js 14.x or later
47
+
- A running instance of Permify server (local or cloud)
48
+
- Basic understanding of Permify's authorization model
34
49
35
-
### Create a new tenant
50
+
## Basic Usage
51
+
52
+
### 1. Initialize the Client
53
+
54
+
First, create a new client instance to connect to your Permify server:
36
55
37
56
```typescript
38
57
import*aspermifyfrom"@permify/permify-node";
39
58
40
59
const client =permify.grpc.newClient({
41
-
endpoint: "localhost:3478",
42
-
cert: undefined,
43
-
insecure: true
60
+
endpoint: "localhost:3478", // Replace with your Permify server URL
61
+
cert: undefined, // Optional: SSL certificate
62
+
insecure: true, // Set to false in production
63
+
timeout: 5000, // Request timeout in milliseconds
44
64
});
65
+
```
45
66
46
-
client.tenancy.create({
67
+
### 2. Tenant Management
68
+
69
+
```typescript
70
+
import*aspermifyfrom"@permify/permify-node";
71
+
72
+
const client =permify.grpc.newClient({
73
+
endpoint: "localhost:3478",
74
+
cert: undefined,
75
+
insecure: true,
76
+
});
77
+
78
+
client.tenancy
79
+
.create({
47
80
id: "t1",
48
-
name: "Tenant 1"
49
-
}).then((response) => {
81
+
name: "Tenant 1",
82
+
})
83
+
.then((response) => {
50
84
console.log(response);
51
85
// handle response
52
-
})
86
+
});
53
87
```
54
88
55
-
### Write Schema
89
+
### 3. Schema Management
90
+
91
+
Define your authorization model using Permify's schema language. Here's a more comprehensive example:
56
92
57
93
```typescript
58
94
import*aspermifyfrom"@permify/permify-node";
59
95
60
96
const client =permify.grpc.newClient({
61
-
endpoint: "localhost:3478",
62
-
cert: undefined,
63
-
insecure: true
97
+
endpoint: "localhost:3478",
98
+
cert: undefined,
99
+
insecure: true,
64
100
});
65
101
66
102
let schema =`
@@ -74,155 +110,209 @@ let schema = `
74
110
`;
75
111
76
112
// Write the schema
77
-
client.tenancy.create({
113
+
client.tenancy
114
+
.create({
78
115
tenantId: "t1",
79
-
schema: schema
80
-
}).then((response) => {
116
+
schema: schema,
117
+
})
118
+
.then((response) => {
81
119
// handle response
82
-
})
120
+
});
83
121
```
84
122
85
-
### Write Relationships
123
+
### 4. Relationship Management
124
+
125
+
Create relationships between entities to define access rules. Here are some common patterns:
86
126
87
127
```typescript
88
128
import*aspermifyfrom"@permify/permify-node";
89
129
90
130
const client =permify.grpc.newClient({
91
-
endpoint: "localhost:3478",
92
-
cert: undefined,
93
-
insecure: true
131
+
endpoint: "localhost:3478",
132
+
cert: undefined,
133
+
insecure: true,
94
134
});
95
135
96
-
client.relationship.write({
136
+
client.relationship
137
+
.write({
97
138
tenantId: "t1",
98
139
metadata: {
99
-
schemaVersion: ""
140
+
schemaVersion: "",
100
141
},
101
-
tuples: [{
142
+
tuples: [
143
+
{
102
144
entity: {
103
-
type: "document",
104
-
id: "1"
145
+
type: "document",
146
+
id: "1",
105
147
},
106
148
relation: "viewer",
107
149
subject: {
108
-
type: "user",
109
-
id: "1"
110
-
}
111
-
}]
112
-
}).then((response) => {
150
+
type: "user",
151
+
id: "1",
152
+
},
153
+
},
154
+
],
155
+
})
156
+
.then((response) => {
113
157
// handle response
114
-
})
158
+
});
115
159
```
116
160
117
-
### Check
161
+
### 5. Permission Checks
162
+
163
+
Verify if a user has a specific permission on a resource. Here are different ways to perform checks:
118
164
119
165
```typescript
120
166
import*aspermifyfrom"@permify/permify-node";
121
167
122
168
const client =permify.grpc.newClient({
123
-
endpoint: "localhost:3478",
124
-
cert: undefined,
125
-
insecure: true
169
+
endpoint: "localhost:3478",
170
+
cert: undefined,
171
+
insecure: true,
126
172
});
127
173
128
-
client.permission.check({
174
+
client.permission
175
+
.check({
129
176
tenantId: "t1",
130
177
metadata: {
131
-
snapToken: "",
132
-
schemaVersion: "",
133
-
depth: 20
178
+
snapToken: "",
179
+
schemaVersion: "",
180
+
depth: 20,
134
181
},
135
182
entity: {
136
-
type: "document",
137
-
id: "1"
183
+
type: "document",
184
+
id: "1",
138
185
},
139
186
permission: "view",
140
187
subject: {
141
-
type: "user",
142
-
id: "3"
143
-
}
144
-
}).then((response) => {
188
+
type: "user",
189
+
id: "3",
190
+
},
191
+
})
192
+
.then((response) => {
145
193
if (response.can===permify.grpc.base.CheckResult.CHECK_RESULT_ALLOWED) {
0 commit comments