@liveblocks/node 0.17.0 → 0.17.1-beta1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts ADDED
@@ -0,0 +1,46 @@
1
+ declare type AuthorizeOptions = {
2
+ /**
3
+ * The secret api provided at https://liveblocks.io/dashboard/apikeys
4
+ */
5
+ secret: string;
6
+ /**
7
+ * The room provided in the authorization request body
8
+ */
9
+ room: string;
10
+ /**
11
+ * The id of the user that try to connect. It should be used to get information about the connected users in the room (name, avatar, etc).
12
+ */
13
+ userId?: string;
14
+ /**
15
+ * The info associated to the user. Can be used to store the name or the profile picture to implement avatar for example. Can't exceed 1KB when serialized as JSON
16
+ */
17
+ userInfo?: unknown;
18
+ };
19
+ declare type AuthorizeResponse = {
20
+ status: number;
21
+ body: string;
22
+ error?: Error;
23
+ };
24
+ /**
25
+ * @example
26
+ * export default async function auth(req, res) {
27
+ *
28
+ * // Implement your own security here.
29
+ *
30
+ * const room = req.body.room;
31
+ * const response = await authorize({
32
+ * room,
33
+ * secret,
34
+ * userId: "123", // Optional
35
+ * userInfo: { // Optional
36
+ * name: "Ada Lovelace"
37
+ * }
38
+ * });
39
+ * return res.status(response.status).end(response.body);
40
+ * }
41
+ */
42
+ declare function authorize(
43
+ options: AuthorizeOptions
44
+ ): Promise<AuthorizeResponse>;
45
+
46
+ export { authorize };
package/index.js ADDED
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ function _interopDefaultLegacy(e) {
3
+ return e && "object" == typeof e && "default" in e ? e : { default: e };
4
+ }
5
+ Object.defineProperty(exports, "__esModule", { value: !0 });
6
+ var fetch__default = _interopDefaultLegacy(require("node-fetch"));
7
+ exports.authorize = async function (options) {
8
+ try {
9
+ const {
10
+ room: room,
11
+ secret: secret,
12
+ userId: userId,
13
+ userInfo: userInfo,
14
+ } = options;
15
+ if (!("string" == typeof room && room.length > 0))
16
+ throw new Error(
17
+ "Invalid room. Please provide a non-empty string as the room. For more information: https://liveblocks.io/docs/api-reference/liveblocks-node#authorize"
18
+ );
19
+ const result = await fetch__default.default(
20
+ options.liveblocksAuthorizeEndpoint ||
21
+ "https://liveblocks.io/api/authorize",
22
+ {
23
+ method: "POST",
24
+ headers: {
25
+ Authorization: `Bearer: ${secret}`,
26
+ "Content-Type": "application/json",
27
+ },
28
+ body: JSON.stringify({
29
+ room: room,
30
+ userId: userId,
31
+ userInfo: userInfo,
32
+ }),
33
+ }
34
+ );
35
+ return result.ok
36
+ ? { status: 200, body: await result.text() }
37
+ : { status: 403, body: await result.text() };
38
+ } catch (er) {
39
+ return {
40
+ status: 403,
41
+ body: 'Call to "https://liveblocks.io/api/authorize" failed. See "error" for more information.',
42
+ error: er,
43
+ };
44
+ }
45
+ };
package/index.mjs ADDED
@@ -0,0 +1,73 @@
1
+ import fetch from "node-fetch";
2
+ function __awaiter(thisArg, _arguments, P, generator) {
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) {
5
+ try {
6
+ step(generator.next(value));
7
+ } catch (e) {
8
+ reject(e);
9
+ }
10
+ }
11
+ function rejected(value) {
12
+ try {
13
+ step(generator.throw(value));
14
+ } catch (e) {
15
+ reject(e);
16
+ }
17
+ }
18
+ function step(result) {
19
+ var value;
20
+ result.done
21
+ ? resolve(result.value)
22
+ : ((value = result.value),
23
+ value instanceof P
24
+ ? value
25
+ : new P(function (resolve) {
26
+ resolve(value);
27
+ })).then(fulfilled, rejected);
28
+ }
29
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
30
+ });
31
+ }
32
+ function authorize(options) {
33
+ return __awaiter(this, void 0, void 0, function* () {
34
+ try {
35
+ const {
36
+ room: room,
37
+ secret: secret,
38
+ userId: userId,
39
+ userInfo: userInfo,
40
+ } = options;
41
+ if (!("string" == typeof room && room.length > 0))
42
+ throw new Error(
43
+ "Invalid room. Please provide a non-empty string as the room. For more information: https://liveblocks.io/docs/api-reference/liveblocks-node#authorize"
44
+ );
45
+ const result = yield fetch(
46
+ options.liveblocksAuthorizeEndpoint ||
47
+ "https://liveblocks.io/api/authorize",
48
+ {
49
+ method: "POST",
50
+ headers: {
51
+ Authorization: `Bearer: ${secret}`,
52
+ "Content-Type": "application/json",
53
+ },
54
+ body: JSON.stringify({
55
+ room: room,
56
+ userId: userId,
57
+ userInfo: userInfo,
58
+ }),
59
+ }
60
+ );
61
+ return result.ok
62
+ ? { status: 200, body: yield result.text() }
63
+ : { status: 403, body: yield result.text() };
64
+ } catch (er) {
65
+ return {
66
+ status: 403,
67
+ body: 'Call to "https://liveblocks.io/api/authorize" failed. See "error" for more information.',
68
+ error: er,
69
+ };
70
+ }
71
+ });
72
+ }
73
+ export { authorize };
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "@liveblocks/node",
3
- "version": "0.17.0",
3
+ "version": "0.17.1-beta1",
4
4
  "description": "A server-side utility that lets you set up a Liveblocks authentication endpoint.",
5
- "main": "./lib/cjs/index.js",
6
- "module": "./lib/esm/index.js",
5
+ "main": "./index.js",
6
+ "module": "./index.mjs",
7
+ "types": "./index.d.ts",
7
8
  "files": [
8
- "lib/"
9
+ "**"
9
10
  ],
10
11
  "keywords": [
11
12
  "node",