|
1 |
| -import PubNub from '../lib/types'; |
| 1 | +import PubNub, { PubNubError } from '../lib/types'; |
2 | 2 |
|
3 | 3 | const pubnub = new PubNub({
|
4 |
| - publishKey: 'demo', |
5 |
| - subscribeKey: 'demo', |
6 |
| - userId: 'myUniqueUserId' |
7 |
| - }); |
| 4 | + publishKey: 'demo', |
| 5 | + subscribeKey: 'demo', |
| 6 | + userId: 'myUniqueUserId', |
| 7 | +}); |
8 | 8 |
|
9 |
| -// snippet.grantTokenVariousResources |
| 9 | +// snippet.grantTokenVariousResources |
10 | 10 | try {
|
11 |
| - const token = await pubnub.grantToken({ |
12 |
| - ttl: 15, |
13 |
| - authorized_uuid: "my-authorized-uuid", |
14 |
| - resources: { |
15 |
| - channels: { |
16 |
| - "channel-a": { |
17 |
| - read: true, |
18 |
| - }, |
19 |
| - "channel-b": { |
20 |
| - read: true, |
21 |
| - write: true, |
22 |
| - }, |
23 |
| - "channel-c": { |
24 |
| - read: true, |
25 |
| - write: true, |
26 |
| - }, |
27 |
| - "channel-d": { |
28 |
| - read: true, |
29 |
| - write: true, |
30 |
| - }, |
31 |
| - }, |
32 |
| - groups: { |
33 |
| - "channel-group-b": { |
34 |
| - read: true, |
35 |
| - }, |
36 |
| - }, |
37 |
| - uuids: { |
38 |
| - "uuid-c": { |
39 |
| - get: true, |
40 |
| - }, |
41 |
| - "uuid-d": { |
42 |
| - get: true, |
43 |
| - update: true, |
44 |
| - }, |
45 |
| - }, |
46 |
| - }, |
47 |
| - }); |
48 |
| -} catch (status) { |
49 |
| - console.log(status); |
| 11 | + const token = await pubnub.grantToken({ |
| 12 | + ttl: 15, |
| 13 | + authorized_uuid: 'my-authorized-uuid', |
| 14 | + resources: { |
| 15 | + channels: { |
| 16 | + 'channel-a': { |
| 17 | + read: true, |
| 18 | + }, |
| 19 | + 'channel-b': { |
| 20 | + read: true, |
| 21 | + write: true, |
| 22 | + }, |
| 23 | + 'channel-c': { |
| 24 | + read: true, |
| 25 | + write: true, |
| 26 | + }, |
| 27 | + 'channel-d': { |
| 28 | + read: true, |
| 29 | + write: true, |
| 30 | + }, |
| 31 | + }, |
| 32 | + groups: { |
| 33 | + 'channel-group-b': { |
| 34 | + read: true, |
| 35 | + }, |
| 36 | + }, |
| 37 | + uuids: { |
| 38 | + 'uuid-c': { |
| 39 | + get: true, |
| 40 | + }, |
| 41 | + 'uuid-d': { |
| 42 | + get: true, |
| 43 | + update: true, |
| 44 | + }, |
| 45 | + }, |
| 46 | + }, |
| 47 | + }); |
| 48 | +} catch (error) { |
| 49 | + console.error( |
| 50 | + `Grant token error: ${error}.${(error as PubNubError).status ? ` Additional information: ${(error as PubNubError).status}` : ''}`, |
| 51 | + ); |
50 | 52 | }
|
51 | 53 | // snippet.end
|
52 | 54 |
|
53 | 55 | // snippet.grantTokenUsingRegEx
|
54 | 56 | try {
|
55 |
| - const token = await pubnub.grantToken({ |
56 |
| - ttl: 15, |
57 |
| - authorized_uuid: "my-authorized-uuid", |
58 |
| - patterns: { |
59 |
| - channels: { |
60 |
| - "^channel-[A-Za-z0-9]$": { |
61 |
| - read: true, |
62 |
| - }, |
63 |
| - }, |
64 |
| - }, |
65 |
| - }); |
66 |
| -} catch (status) { |
67 |
| - console.log(status); |
| 57 | + const token = await pubnub.grantToken({ |
| 58 | + ttl: 15, |
| 59 | + authorized_uuid: 'my-authorized-uuid', |
| 60 | + patterns: { |
| 61 | + channels: { |
| 62 | + '^channel-[A-Za-z0-9]$': { |
| 63 | + read: true, |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + }); |
| 68 | +} catch (error) { |
| 69 | + console.error( |
| 70 | + `Grant token error: ${error}.${ |
| 71 | + (error as PubNubError).status ? ` Additional information: ${(error as PubNubError).status}` : '' |
| 72 | + }`, |
| 73 | + ); |
68 | 74 | }
|
69 | 75 | // snippet.end
|
70 | 76 |
|
71 | 77 | // snippet.grantTokenRegExAndResources
|
72 | 78 | try {
|
73 |
| - const token = await pubnub.grantToken({ |
74 |
| - ttl: 15, |
75 |
| - authorized_uuid: "my-authorized-uuid", |
76 |
| - resources: { |
77 |
| - channels: { |
78 |
| - "channel-a": { |
79 |
| - read: true, |
80 |
| - }, |
81 |
| - "channel-b": { |
82 |
| - read: true, |
83 |
| - write: true, |
84 |
| - }, |
85 |
| - "channel-c": { |
86 |
| - read: true, |
87 |
| - write: true, |
88 |
| - }, |
89 |
| - "channel-d": { |
90 |
| - read: true, |
91 |
| - write: true, |
92 |
| - }, |
93 |
| - }, |
94 |
| - groups: { |
95 |
| - "channel-group-b": { |
96 |
| - read: true, |
97 |
| - }, |
98 |
| - }, |
99 |
| - uuids: { |
100 |
| - "uuid-c": { |
101 |
| - get: true, |
102 |
| - }, |
103 |
| - "uuid-d": { |
104 |
| - get: true, |
105 |
| - update: true, |
106 |
| - }, |
107 |
| - }, |
108 |
| - }, |
109 |
| - patterns: { |
110 |
| - channels: { |
111 |
| - "^channel-[A-Za-z0-9]$": { |
112 |
| - read: true |
113 |
| - }, |
114 |
| - }, |
115 |
| - }, |
116 |
| - }); |
117 |
| -} catch (status) { |
118 |
| - console.log(status); |
119 |
| -} |
120 |
| -// snippet.end |
121 |
| - |
122 |
| -// snippet.grantTokenSpaceUserResources |
123 |
| -try { |
124 |
| - const token = await pubnub.grantToken({ |
125 |
| - ttl: 15, |
126 |
| - authorizedUserId: "my-authorized-userId", |
127 |
| - resources: { |
128 |
| - spaces: { |
129 |
| - "space-a": { |
130 |
| - read: true, |
131 |
| - }, |
132 |
| - "space-b": { |
133 |
| - read: true, |
134 |
| - write: true, |
135 |
| - }, |
136 |
| - "space-c": { |
137 |
| - read: true, |
138 |
| - write: true, |
139 |
| - }, |
140 |
| - "space-d": { |
141 |
| - read: true, |
142 |
| - write: true, |
143 |
| - }, |
144 |
| - }, |
145 |
| - users: { |
146 |
| - "userId-c": { |
147 |
| - get: true, |
148 |
| - }, |
149 |
| - "userId-d": { |
150 |
| - get: true, |
151 |
| - update: true, |
152 |
| - }, |
153 |
| - }, |
154 |
| - }, |
155 |
| - }); |
156 |
| -} catch (status) { |
157 |
| - console.log(status); |
158 |
| -} |
159 |
| -// snippet.end |
160 |
| - |
161 |
| -// snippet.grantTokenSpaceUserRegEx |
162 |
| -try { |
163 |
| - const token = await pubnub.grantToken({ |
164 |
| - ttl: 15, |
165 |
| - authorizedUserId: "my-authorized-userId", |
166 |
| - patterns: { |
167 |
| - spaces: { |
168 |
| - "^space-[A-Za-z0-9]$": { |
169 |
| - read: true, |
170 |
| - }, |
171 |
| - }, |
172 |
| - }, |
173 |
| - }); |
174 |
| -} catch (status) { |
175 |
| - console.log(status); |
176 |
| -} |
177 |
| -// snippet.end |
178 |
| - |
179 |
| - |
180 |
| -// snippet.grantTokenSpacesUserRegExResources |
181 |
| -try { |
182 |
| - const token = await pubnub.grantToken({ |
183 |
| - ttl: 15, |
184 |
| - authorizedUserId: "my-authorized-userId", |
185 |
| - resources: { |
186 |
| - spaces: { |
187 |
| - "space-a": { |
188 |
| - read: true, |
189 |
| - }, |
190 |
| - "space-b": { |
191 |
| - read: true, |
192 |
| - write: true, |
193 |
| - }, |
194 |
| - "space-c": { |
195 |
| - read: true, |
196 |
| - write: true, |
197 |
| - }, |
198 |
| - "space-d": { |
199 |
| - read: true, |
200 |
| - write: true, |
201 |
| - }, |
202 |
| - }, |
203 |
| - users: { |
204 |
| - "userId-c": { |
205 |
| - get: true, |
206 |
| - }, |
207 |
| - "userId-d": { |
208 |
| - get: true, |
209 |
| - update: true, |
210 |
| - }, |
211 |
| - }, |
212 |
| - }, |
213 |
| - patterns: { |
214 |
| - spaces: { |
215 |
| - "^space-[A-Za-z0-9]$": { |
216 |
| - read: true |
217 |
| - }, |
218 |
| - }, |
219 |
| - }, |
220 |
| - }); |
221 |
| -} catch (status) { |
222 |
| - console.log(status); |
| 79 | + const token = await pubnub.grantToken({ |
| 80 | + ttl: 15, |
| 81 | + authorized_uuid: 'my-authorized-uuid', |
| 82 | + resources: { |
| 83 | + channels: { |
| 84 | + 'channel-a': { |
| 85 | + read: true, |
| 86 | + }, |
| 87 | + 'channel-b': { |
| 88 | + read: true, |
| 89 | + write: true, |
| 90 | + }, |
| 91 | + 'channel-c': { |
| 92 | + read: true, |
| 93 | + write: true, |
| 94 | + }, |
| 95 | + 'channel-d': { |
| 96 | + read: true, |
| 97 | + write: true, |
| 98 | + }, |
| 99 | + }, |
| 100 | + groups: { |
| 101 | + 'channel-group-b': { |
| 102 | + read: true, |
| 103 | + }, |
| 104 | + }, |
| 105 | + uuids: { |
| 106 | + 'uuid-c': { |
| 107 | + get: true, |
| 108 | + }, |
| 109 | + 'uuid-d': { |
| 110 | + get: true, |
| 111 | + update: true, |
| 112 | + }, |
| 113 | + }, |
| 114 | + }, |
| 115 | + patterns: { |
| 116 | + channels: { |
| 117 | + '^channel-[A-Za-z0-9]$': { |
| 118 | + read: true, |
| 119 | + }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + }); |
| 123 | +} catch (error) { |
| 124 | + console.error( |
| 125 | + `Grant token error: ${error}.${(error as PubNubError).status ? ` Additional information: ${(error as PubNubError).status}` : ''}`, |
| 126 | + ); |
223 | 127 | }
|
224 | 128 | // snippet.end
|
225 |
| - |
0 commit comments