Skip to content

Commit

Permalink
Allow axios options to be passed
Browse files Browse the repository at this point in the history
  • Loading branch information
mguellsegarra committed Jul 20, 2023
1 parent 62bed97 commit d3bceac
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 61 deletions.
59 changes: 42 additions & 17 deletions lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
IsShortcutFavoriteOpts,
AttributeConditionParserOpts,
ButTreeOpenOpts,
RequestOptions,
} from "./types";
import {
createEvalDomainPayload,
Expand Down Expand Up @@ -46,8 +47,8 @@ export class Client {
return this.axiosInstance;
}

public async _fetch(options: FetchOpts): Promise<any> {
const { service = "object" } = options;
public async _fetch(data: FetchOpts): Promise<any> {
const { service = "object", options = {} } = data;
const { host, token } = this;

if (service != "common" && service != "db" && !token) {
Expand All @@ -59,12 +60,13 @@ export class Client {
try {
const response = await this.getAxiosInstance().post(
`${host}/${service}`,
options.payload,
data.payload,
{
headers: {
"Content-Type": "application/json",
"X-GISCE-Client": this.clientHeader,
},
...options,
}
);
// console.debug(`Response from API: ${JSON.stringify(response.data)}`);
Expand Down Expand Up @@ -104,29 +106,35 @@ export class Client {
return token;
}

public async getDatabases(): Promise<string[]> {
public async getDatabases(options?: RequestOptions): Promise<string[]> {
return await this._fetch({
service: "db",
payload: ["list"],
options,
});
}

public async getServerVersion(): Promise<string> {
public async getServerVersion(options?: RequestOptions): Promise<string> {
return await this._fetch({
service: "db",
payload: ["server_version"],
options,
});
}

public async getLoginMessage(): Promise<string> {
public async getLoginMessage(options?: RequestOptions): Promise<string> {
const loginMessage = await this._fetch({
service: "common",
payload: ["login_message"],
options,
});
return loginMessage || "";
}

public async refreshToken(token: string): Promise<string> {
public async refreshToken(
token: string,
options?: RequestOptions
): Promise<string> {
const { database } = this;

if (!database) {
Expand All @@ -136,6 +144,7 @@ export class Client {
const refreshedToken = await this._fetch({
service: "common",
payload: ["refresh_token", token],
options,
});
this.token = refreshedToken;
return refreshedToken;
Expand All @@ -145,8 +154,11 @@ export class Client {
this.clientHeader = clientHeader;
}

public async evalDomain(options: EvalDomainOpts): Promise<any> {
const { values, domain, context } = options;
public async evalDomain(
data: EvalDomainOpts,
options?: RequestOptions
): Promise<any> {
const { values, domain, context } = data;
const { database, token } = this;

const executePayload = createEvalDomainPayload({
Expand All @@ -159,13 +171,15 @@ export class Client {

return await this._fetch({
payload: executePayload,
options,
});
}

public async parseCondition(
options: AttributeConditionParserOpts
data: AttributeConditionParserOpts,
options?: RequestOptions
): Promise<any> {
const { values, condition, context } = options;
const { values, condition, context } = data;
const { database, token } = this;

const executePayload = createAttributeConditionPayload({
Expand All @@ -178,11 +192,15 @@ export class Client {

return await this._fetch({
payload: executePayload,
options,
});
}

public async getShortcuts(options: GetShortcutsOpts): Promise<any> {
const { context } = options;
public async getShortcuts(
data: GetShortcutsOpts,
options?: RequestOptions
): Promise<any> {
const { context } = data;
const { database, token } = this;

const executePayload = createGetShortcutsPayload({
Expand All @@ -193,13 +211,15 @@ export class Client {

return await this._fetch({
payload: executePayload,
options,
});
}

public async isShortcutFavorite(
options: IsShortcutFavoriteOpts
data: IsShortcutFavoriteOpts,
options?: RequestOptions
): Promise<any> {
const { context, payload } = options;
const { context, payload } = data;
const { database, token } = this;

const executePayload = createIsShortcutFavoritePayload({
Expand All @@ -211,11 +231,15 @@ export class Client {

return await this._fetch({
payload: executePayload,
options,
});
}

public async treeButOpen(options: ButTreeOpenOpts): Promise<any> {
const { context, model, id } = options;
public async treeButOpen(
data: ButTreeOpenOpts,
options?: RequestOptions
): Promise<any> {
const { context, model, id } = data;
const { database, token } = this;

const executePayload = createButTreeOpenPayload({
Expand All @@ -228,6 +252,7 @@ export class Client {

return await this._fetch({
payload: executePayload,
options,
});
}
}
Loading

0 comments on commit d3bceac

Please sign in to comment.