@liveblocks/react
Advanced tools
Comparing version 0.2.2 to 0.3.0
@@ -15,2 +15,3 @@ import { Client, RecordData, Others, Presence, Record, InitialStorageFactory, Room } from "@liveblocks/client"; | ||
export declare function useMyPresence<T extends Presence>(room: string, initPresence: () => T): [T, (overrides: Partial<T>) => void]; | ||
export declare function useUpdateMyPresence<T extends Presence>(room: string): (overrides: Partial<T>) => void; | ||
export declare function useOthersPresence<T extends Presence>(room: string): Others<T>; | ||
@@ -17,0 +18,0 @@ export declare function useStorage<TRoot extends RecordData>(room: string, initialStorage: InitialStorageFactory<TRoot>): [ |
116
lib/index.js
@@ -564,2 +564,66 @@ Object.defineProperty(exports, '__esModule', { value: true }); | ||
}; | ||
function fetchAuthorize(endpoint, room) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const res = yield fetch(endpoint, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
room, | ||
}), | ||
}); | ||
if (!res.ok) { | ||
throw new AuthenticationError(`Authentication error. Liveblocks could not parse the response of your authentication "${endpoint}"`); | ||
} | ||
let authResponse = null; | ||
try { | ||
authResponse = yield res.json(); | ||
} | ||
catch (er) { | ||
throw new AuthenticationError(`Authentication error. Liveblocks could not parse the response of your authentication "${endpoint}"`); | ||
} | ||
if (typeof authResponse.token !== "string") { | ||
throw new AuthenticationError(`Authentication error. Liveblocks could not parse the response of your authentication "${endpoint}"`); | ||
} | ||
return authResponse.token; | ||
}); | ||
} | ||
function auth(endpoint, room) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (typeof endpoint === "string") { | ||
return fetchAuthorize(endpoint, room); | ||
} | ||
if (typeof endpoint === "function") { | ||
return endpoint(room); | ||
} | ||
throw new Error("Authentication error. Liveblocks could not parse the response of your authentication endpoint"); | ||
}); | ||
} | ||
class AuthenticationError extends Error { | ||
constructor(message) { | ||
super(message); | ||
} | ||
} | ||
function parseToken(token) { | ||
const tokenParts = token.split("."); | ||
if (tokenParts.length !== 3) { | ||
throw new AuthenticationError(`Authentication error. Liveblocks could not parse the response of your authentication endpoint`); | ||
} | ||
const data = JSON.parse(atob(tokenParts[1])); | ||
if (typeof data.actor !== "number") { | ||
throw new AuthenticationError(`Authentication error. Liveblocks could not parse the response of your authentication endpoint`); | ||
} | ||
return data; | ||
} | ||
var __awaiter$1 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __rest = (undefined && undefined.__rest) || function (s, e) { | ||
@@ -767,3 +831,3 @@ var t = {}; | ||
function connect() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return __awaiter$1(this, void 0, void 0, function* () { | ||
if (state === RoomState.Connected || state === RoomState.Connecting) { | ||
@@ -776,5 +840,4 @@ return; | ||
try { | ||
const response = yield authorize(authEndpoint, name); | ||
token = response.token; | ||
actor = response.actor; | ||
token = yield auth(authEndpoint, name); | ||
actor = parseToken(token).actor; | ||
} | ||
@@ -804,2 +867,3 @@ catch (er) { | ||
state = RoomState.Default; | ||
_users = {}; | ||
clearTimeout(retryTimeoutId); | ||
@@ -917,38 +981,2 @@ _listeners.open = []; | ||
} | ||
function authorize(endpoint, room) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const res = yield fetch(endpoint, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
room, | ||
}), | ||
}); | ||
if (!res.ok) { | ||
throw new AuthenticationError(`Authentication error. Liveblocks could not parse the response of your authentication "${endpoint}"`); | ||
} | ||
let authResponse = null; | ||
try { | ||
authResponse = yield res.json(); | ||
} | ||
catch (er) { | ||
throw new AuthenticationError(`Authentication error. Liveblocks could not parse the response of your authentication "${endpoint}"`); | ||
} | ||
if (typeof authResponse.token !== "string" || | ||
typeof authResponse.actor !== "number") { | ||
throw new AuthenticationError(`Authentication error. Liveblocks could not parse the response of your authentication "${endpoint}"`); | ||
} | ||
return { | ||
token: authResponse.token, | ||
actor: authResponse.actor, | ||
}; | ||
}); | ||
} | ||
class AuthenticationError extends Error { | ||
constructor(message) { | ||
super(message); | ||
} | ||
} | ||
@@ -1092,2 +1120,9 @@ function createClient(options) { | ||
} | ||
function useUpdateMyPresence(room) { | ||
var client = useClient(); | ||
return React.useCallback(function (overrides) { | ||
var _room = client.getRoom(room); | ||
_room.updatePresence(overrides); | ||
}, [room, client]); | ||
} | ||
function useOthersPresence(room) { | ||
@@ -1169,2 +1204,3 @@ var client = useClient(); | ||
exports.useStorage = useStorage; | ||
exports.useUpdateMyPresence = useUpdateMyPresence; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@liveblocks/react", | ||
"version": "0.2.2", | ||
"version": "0.3.0", | ||
"description": "", | ||
@@ -16,3 +16,3 @@ "main": "./lib/index.js", | ||
"dependencies": { | ||
"@liveblocks/client": "0.2.1" | ||
"@liveblocks/client": "0.3.0" | ||
}, | ||
@@ -29,3 +29,2 @@ "peerDependencies": { | ||
"@testing-library/jest-dom": "^5.11.9", | ||
"@testing-library/react": "^11.2.5", | ||
"@types/jest": "^26.0.20", | ||
@@ -32,0 +31,0 @@ "@types/react": "^16.14.0", |
Sorry, the diff of this file is not supported yet
129706
18
1219
+ Added@liveblocks/client@0.3.0(transitive)
- Removed@liveblocks/client@0.2.1(transitive)
Updated@liveblocks/client@0.3.0