@planet-a/affinity-node
Version:
API wrapper for the affinity.co API
57 lines (56 loc) • 1.93 kB
JavaScript
import { RequestContext } from "./http/http.js";
/**
*
* Represents the configuration of a server including its
* url template and variable configuration based on the url.
*
*/
export class ServerConfiguration {
constructor(url, variableConfiguration) {
Object.defineProperty(this, "url", {
enumerable: true,
configurable: true,
writable: true,
value: url
});
Object.defineProperty(this, "variableConfiguration", {
enumerable: true,
configurable: true,
writable: true,
value: variableConfiguration
});
}
/**
* Sets the value of the variables of this server. Variables are included in
* the `url` of this ServerConfiguration in the form `{variableName}`
*
* @param variableConfiguration a partial variable configuration for the
* variables contained in the url
*/
setVariables(variableConfiguration) {
Object.assign(this.variableConfiguration, variableConfiguration);
}
getConfiguration() {
return this.variableConfiguration;
}
getUrl() {
let replacedUrl = this.url;
for (const [key, value] of Object.entries(this.variableConfiguration)) {
replacedUrl = replacedUrl.replaceAll(`{${key}}`, value);
}
return replacedUrl;
}
/**
* Creates a new request context for this server using the url with variables
* replaced with their respective values and the endpoint of the request appended.
*
* @param endpoint the endpoint to be queried on the server
* @param httpMethod httpMethod to be used
*
*/
makeRequestContext(endpoint, httpMethod) {
return new RequestContext(this.getUrl() + endpoint, httpMethod);
}
}
export const server1 = new ServerConfiguration("https://api.affinity.co", {});
export const servers = [server1];