|
| 1 | +import { DefaultApi } from '../index.js'; |
| 2 | +import ApiClient from '../ApiClient.js'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Base class representing a connection to the Rel server |
| 6 | + */ |
| 7 | +class ConnectionBase { |
| 8 | + /** |
| 9 | + * Create a connection |
| 10 | + * @param {String} [params.basePath] - The base URL against which to resolve every API call's (relative) path. |
| 11 | + * The default is http://127.0.0.1:8010. |
| 12 | + * @param {Array.<String>} [params.authentications] - The authentication methods to be included for all API calls. |
| 13 | + * This is a placeholder for eventual use with the cloud connection. |
| 14 | + * @param {Array.<String>} [params.defaultHeaders] - The default HTTP headers to be included for all API calls. |
| 15 | + * The default is {} |
| 16 | + * @param {Number} [params.timeout] - The default HTTP timeout for all API calls. The default is 60000 |
| 17 | + * @param {Boolean} [params.cache] - If set to false an additional timestamp parameter is added to all API GET |
| 18 | + * calls to prevent browser caching. The default is true |
| 19 | + * @param {Boolean} [params.enableCookies] - If set to true, the client will save the cookies from each server |
| 20 | + * response, and return them in the next request. |
| 21 | + */ |
| 22 | + constructor(params = {}) { |
| 23 | + const apiClient = new ApiClient(); |
| 24 | + this._defaultApi = new DefaultApi(apiClient); |
| 25 | + this._api = this._defaultApi.apiClient; |
| 26 | + this._versionMap = new Map() |
| 27 | + |
| 28 | + if (params.basePath) { |
| 29 | + this._api.basePath = params.basePath; |
| 30 | + } |
| 31 | + if (params.authentications) { |
| 32 | + this._api.authentications = params.authentications; |
| 33 | + } |
| 34 | + if (params.defaultHeaders) { |
| 35 | + this._api.defaultHeaders = params.defaultHeaders; |
| 36 | + } |
| 37 | + if (params.hasOwnProperty('timeout')) { |
| 38 | + this._api.timeout = params.timeout; |
| 39 | + } |
| 40 | + if (params.hasOwnProperty('cache')) { |
| 41 | + this._api.cache = params.cache; |
| 42 | + } |
| 43 | + if (params.hasOwnProperty('enableCookies')) { |
| 44 | + this._api.enableCookies = params.enableCookies; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + get api() { |
| 49 | + return this._api; |
| 50 | + } |
| 51 | + |
| 52 | + get defaultApi() { |
| 53 | + return this._defaultApi; |
| 54 | + } |
| 55 | + |
| 56 | + get transactionPost() { |
| 57 | + return this._transactionPost; |
| 58 | + } |
| 59 | + |
| 60 | + get basePath() { |
| 61 | + return this._api.basePath; |
| 62 | + } |
| 63 | + set basePath(basePath) { |
| 64 | + this._api.basePath = basePath; |
| 65 | + } |
| 66 | + |
| 67 | + get authentications() { |
| 68 | + return this._api.authentications; |
| 69 | + } |
| 70 | + set authentications(authentications) { |
| 71 | + this._api.authentications = authentications; |
| 72 | + } |
| 73 | + |
| 74 | + get defaultHeaders() { |
| 75 | + return this._api.defaultHeaders; |
| 76 | + } |
| 77 | + set defaultHeaders(defaultHeaders) { |
| 78 | + this._api.defaultHeaders = defaultHeaders; |
| 79 | + } |
| 80 | + |
| 81 | + get cache() { |
| 82 | + return this._api.cache; |
| 83 | + } |
| 84 | + set cache(cache) { |
| 85 | + this._api.cache = cache; |
| 86 | + } |
| 87 | + |
| 88 | + get timeout() { |
| 89 | + return this._api.timeout; |
| 90 | + } |
| 91 | + set timeout(timeout) { |
| 92 | + this._api.timeout = timeout; |
| 93 | + } |
| 94 | + |
| 95 | + get enableCookies() { |
| 96 | + return this._api.enableCookies; |
| 97 | + } |
| 98 | + set enableCookies(enableCookies) { |
| 99 | + this._api.enableCookies = enableCookies; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Returns the database's current transaction version value. |
| 104 | + * |
| 105 | + * @param {String} dbname - The name of the database |
| 106 | + */ |
| 107 | + getTransactionVersion(dbname) { |
| 108 | + const version = this._versionMap.get(dbname); |
| 109 | + return version == null ? 0 : version; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Sets the database's current transaction version value. |
| 114 | + * |
| 115 | + * @param {String} dbname - The name of the database |
| 116 | + * @param {Int} transactionVersion - The database's transaction version value |
| 117 | + */ |
| 118 | + setTransactionVersion(dbname, transactionVersion) { |
| 119 | + this._versionMap.set(dbname, transactionVersion); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +export default ConnectionBase; |
0 commit comments