Skip to content

Commit

Permalink
Add option to WriteTokenNodeUrl to get url from /nodes api
Browse files Browse the repository at this point in the history
  • Loading branch information
elv-zenia committed Jun 12, 2024
1 parent 080abdc commit 4cd9657
Showing 1 changed file with 54 additions and 3 deletions.
57 changes: 54 additions & 3 deletions src/ElvClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -585,10 +585,11 @@ class ElvClient {
* @namedParams
* @param {string=} matchEndpoint - Return node(s) matching the specified endpoint
* @param {string=} matchNodeId - Return node(s) matching the specified node ID
* * @param {string=} matchWriteToken - Return node(s) matching the specified write token
*
* @return {Promise<Array<Object>>} - A list of nodes in the space matching the parameters
*/
async SpaceNodes({matchEndpoint, matchNodeId}={}) {
async SpaceNodes({matchEndpoint, matchNodeId, matchWriteToken}={}) {
let nodes;
this.SetStaticToken();

Expand Down Expand Up @@ -632,6 +633,7 @@ class ElvClient {
});
} else if(matchNodeId) {
this.SetStaticToken();

let node = await this.utils.ResponseToJson(
this.HttpClient.Request({
path: UrlJoin("nodes", matchNodeId),
Expand All @@ -644,6 +646,24 @@ class ElvClient {

this.ClearStaticToken();
return [node];
} else if(matchWriteToken) {
this.SetStaticToken();

const {nodes} = await this.utils.ResponseToJson(
this.HttpClient.Request({
path: UrlJoin("nodes"),
method: "GET",
headers: {
Authorization: `Bearer ${this.staticToken}`
},
queryParams: {
token: matchWriteToken
}
})
);

this.ClearStaticToken();
return nodes;
}
}

Expand All @@ -661,8 +681,39 @@ class ElvClient {
};
}

WriteTokenNodeUrl({writeToken}) {
const nodeUrl = this.HttpClient.draftURIs[writeToken];
/**
* Return node url for a given write token
*
* @methodGroup Nodes
* @namedParams
* @param {string} writeToken - The write token to match to a node
* @param {boolean=} networkCall - If specified, will make a network call to /nodes?token=<writeToken>
*
* @returns {Promise<string>} - The node url for a write token
*/
async WriteTokenNodeUrl({writeToken, networkCall=false}) {
let nodeUrl;

if(networkCall) {
const nodes = await this.SpaceNodes({matchWriteToken: writeToken});

nodeUrl = (
nodes &&
nodes[0] &&
nodes[0].services &&
nodes[0].services.fabric_api &&
nodes[0].services.fabric_api.urls &&
nodes[0].services.fabric_api.urls[0]
);

if(!nodeUrl) {
console.error(`No node url found for write token: ${writeToken}`);

return "";
}
} else {
nodeUrl = this.HttpClient.draftURIs[writeToken];
}

return nodeUrl ? nodeUrl.toString() : undefined;
}
Expand Down

0 comments on commit 4cd9657

Please sign in to comment.