| 
 | 1 | +import { util } from "./util";  | 
 | 2 | +import logger from "./logger";  | 
 | 3 | + | 
 | 4 | +export class API {  | 
 | 5 | + constructor(private readonly _options: any) { }  | 
 | 6 | + | 
 | 7 | + private _buildUrl(method: string): string {  | 
 | 8 | + const protocol = this._options.secure ? "https://" : "http://";  | 
 | 9 | + let url =  | 
 | 10 | + protocol +  | 
 | 11 | + this._options.host +  | 
 | 12 | + ":" +  | 
 | 13 | + this._options.port +  | 
 | 14 | + this._options.path +  | 
 | 15 | + this._options.key +  | 
 | 16 | + "/" +  | 
 | 17 | + method;  | 
 | 18 | + const queryString = "?ts=" + new Date().getTime() + "" + Math.random();  | 
 | 19 | + url += queryString;  | 
 | 20 | + | 
 | 21 | + return url;  | 
 | 22 | + }  | 
 | 23 | + | 
 | 24 | + /** Get a unique ID from the server via XHR and initialize with it. */  | 
 | 25 | + async retrieveId(): Promise<string> {  | 
 | 26 | + const url = this._buildUrl("id");  | 
 | 27 | + | 
 | 28 | + try {  | 
 | 29 | + const response = await fetch(url);  | 
 | 30 | + | 
 | 31 | + if (response.status !== 200) {  | 
 | 32 | + throw new Error(`Error. Status:${response.status}`);  | 
 | 33 | + }  | 
 | 34 | + | 
 | 35 | + return response.text();  | 
 | 36 | + } catch (error) {  | 
 | 37 | + logger.error("Error retrieving ID", error);  | 
 | 38 | + | 
 | 39 | + let pathError = "";  | 
 | 40 | + | 
 | 41 | + if (  | 
 | 42 | + this._options.path === "/" &&  | 
 | 43 | + this._options.host !== util.CLOUD_HOST  | 
 | 44 | + ) {  | 
 | 45 | + pathError =  | 
 | 46 | + " If you passed in a `path` to your self-hosted PeerServer, " +  | 
 | 47 | + "you'll also need to pass in that same path when creating a new " +  | 
 | 48 | + "Peer.";  | 
 | 49 | + }  | 
 | 50 | + | 
 | 51 | + throw new Error("Could not get an ID from the server." + pathError);  | 
 | 52 | + }  | 
 | 53 | + }  | 
 | 54 | + | 
 | 55 | + /** @deprecated */  | 
 | 56 | + async listAllPeers(): Promise<any[]> {  | 
 | 57 | + const url = this._buildUrl("peers");  | 
 | 58 | + | 
 | 59 | + try {  | 
 | 60 | + const response = await fetch(url);  | 
 | 61 | + | 
 | 62 | + if (response.status !== 200) {  | 
 | 63 | + if (response.status === 401) {  | 
 | 64 | + let helpfulError = "";  | 
 | 65 | + | 
 | 66 | + if (this._options.host === util.CLOUD_HOST) {  | 
 | 67 | + helpfulError =  | 
 | 68 | + "It looks like you're using the cloud server. You can email " +  | 
 | 69 | + "team@peerjs.com to enable peer listing for your API key.";  | 
 | 70 | + } else {  | 
 | 71 | + helpfulError =  | 
 | 72 | + "You need to enable `allow_discovery` on your self-hosted " +  | 
 | 73 | + "PeerServer to use this feature.";  | 
 | 74 | + }  | 
 | 75 | + | 
 | 76 | + throw new Error("It doesn't look like you have permission to list peers IDs. " +  | 
 | 77 | + helpfulError);  | 
 | 78 | + }  | 
 | 79 | + | 
 | 80 | + throw new Error(`Error. Status:${response.status}`);  | 
 | 81 | + }  | 
 | 82 | + | 
 | 83 | + return response.json();  | 
 | 84 | + } catch (error) {  | 
 | 85 | + logger.error("Error retrieving list peers", error);  | 
 | 86 | + | 
 | 87 | + throw new Error("Could not get list peers from the server." + error);  | 
 | 88 | + }  | 
 | 89 | + }  | 
 | 90 | +}  | 
0 commit comments