@juriskop/swish-client
Version:
A JavaScript/TypeScript client for interfacing with SWISH supporting HTTP digest auth.
48 lines • 2.03 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SWISHClient = void 0;
const axios_1 = require("axios");
const axios_http_digest_auth_1 = require("@juriskop/axios-http-digest-auth");
const URLProcessor_1 = require("./URLProcessor");
class SWISHClient {
constructor(username, password, baseUrl) {
this.baseUrl = baseUrl;
this.axiosInstance = axios_1.default.create({});
(0, axios_http_digest_auth_1.httpDigestAuth)(this.axiosInstance, { username, password });
}
async getProgramCodeAsJson(programName) {
let response = await this.axiosInstance.get(`${(0, URLProcessor_1.joinToUrl)([this.baseUrl, "/p/", programName])}?format=json`);
return response.data;
}
async getProgramCodeAsRawText(programName) {
let response = await this.axiosInstance.get(`${(0, URLProcessor_1.joinToUrl)([this.baseUrl, "/p/", programName])}?format=raw`, { responseType: "text" });
return response.data;
}
async queryExistingProgram(programName, query) {
let programCode = `:- include('${programName}').`;
let response = await this.axiosInstance.post(`${(0, URLProcessor_1.joinToUrl)([this.baseUrl, "/pengine/create"])}`, {
"src_text": programCode,
"format": "json",
"application": "swish",
"destroy": true,
"ask": query,
"solutions": "all",
"chunk": 10000
});
return response.data.answer;
}
async queryCustomProgram(programCode, query) {
let response = await this.axiosInstance.post(`${(0, URLProcessor_1.joinToUrl)([this.baseUrl, "/pengine/create"])}`, {
"src_text": programCode,
"format": "json",
"application": "swish",
"destroy": true,
"ask": query,
"solutions": "all",
"chunk": 10000
});
return response.data.answer;
}
}
exports.SWISHClient = SWISHClient;
//# sourceMappingURL=SWISHClient.js.map