From d3bceacefbacf38ab9d3ff3505eaf191519bfbec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Gu=CC=88ell=20Segarra?= Date: Thu, 20 Jul 2023 18:53:40 +0200 Subject: [PATCH] Allow axios options to be passed --- lib/client.ts | 59 +++++++++++++++------ lib/model.ts | 125 +++++++++++++++++++++++++++++++------------- lib/report.ts | 18 +++++-- lib/types.ts | 3 ++ npm-shrinkwrap.json | 4 +- package.json | 2 +- 6 files changed, 150 insertions(+), 61 deletions(-) diff --git a/lib/client.ts b/lib/client.ts index 5fa5bf2..18022e2 100644 --- a/lib/client.ts +++ b/lib/client.ts @@ -7,6 +7,7 @@ import { IsShortcutFavoriteOpts, AttributeConditionParserOpts, ButTreeOpenOpts, + RequestOptions, } from "./types"; import { createEvalDomainPayload, @@ -46,8 +47,8 @@ export class Client { return this.axiosInstance; } - public async _fetch(options: FetchOpts): Promise { - const { service = "object" } = options; + public async _fetch(data: FetchOpts): Promise { + const { service = "object", options = {} } = data; const { host, token } = this; if (service != "common" && service != "db" && !token) { @@ -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)}`); @@ -104,29 +106,35 @@ export class Client { return token; } - public async getDatabases(): Promise { + public async getDatabases(options?: RequestOptions): Promise { return await this._fetch({ service: "db", payload: ["list"], + options, }); } - public async getServerVersion(): Promise { + public async getServerVersion(options?: RequestOptions): Promise { return await this._fetch({ service: "db", payload: ["server_version"], + options, }); } - public async getLoginMessage(): Promise { + public async getLoginMessage(options?: RequestOptions): Promise { const loginMessage = await this._fetch({ service: "common", payload: ["login_message"], + options, }); return loginMessage || ""; } - public async refreshToken(token: string): Promise { + public async refreshToken( + token: string, + options?: RequestOptions + ): Promise { const { database } = this; if (!database) { @@ -136,6 +144,7 @@ export class Client { const refreshedToken = await this._fetch({ service: "common", payload: ["refresh_token", token], + options, }); this.token = refreshedToken; return refreshedToken; @@ -145,8 +154,11 @@ export class Client { this.clientHeader = clientHeader; } - public async evalDomain(options: EvalDomainOpts): Promise { - const { values, domain, context } = options; + public async evalDomain( + data: EvalDomainOpts, + options?: RequestOptions + ): Promise { + const { values, domain, context } = data; const { database, token } = this; const executePayload = createEvalDomainPayload({ @@ -159,13 +171,15 @@ export class Client { return await this._fetch({ payload: executePayload, + options, }); } public async parseCondition( - options: AttributeConditionParserOpts + data: AttributeConditionParserOpts, + options?: RequestOptions ): Promise { - const { values, condition, context } = options; + const { values, condition, context } = data; const { database, token } = this; const executePayload = createAttributeConditionPayload({ @@ -178,11 +192,15 @@ export class Client { return await this._fetch({ payload: executePayload, + options, }); } - public async getShortcuts(options: GetShortcutsOpts): Promise { - const { context } = options; + public async getShortcuts( + data: GetShortcutsOpts, + options?: RequestOptions + ): Promise { + const { context } = data; const { database, token } = this; const executePayload = createGetShortcutsPayload({ @@ -193,13 +211,15 @@ export class Client { return await this._fetch({ payload: executePayload, + options, }); } public async isShortcutFavorite( - options: IsShortcutFavoriteOpts + data: IsShortcutFavoriteOpts, + options?: RequestOptions ): Promise { - const { context, payload } = options; + const { context, payload } = data; const { database, token } = this; const executePayload = createIsShortcutFavoritePayload({ @@ -211,11 +231,15 @@ export class Client { return await this._fetch({ payload: executePayload, + options, }); } - public async treeButOpen(options: ButTreeOpenOpts): Promise { - const { context, model, id } = options; + public async treeButOpen( + data: ButTreeOpenOpts, + options?: RequestOptions + ): Promise { + const { context, model, id } = data; const { database, token } = this; const executePayload = createButTreeOpenPayload({ @@ -228,6 +252,7 @@ export class Client { return await this._fetch({ payload: executePayload, + options, }); } } diff --git a/lib/model.ts b/lib/model.ts index 390a93a..2f6369f 100644 --- a/lib/model.ts +++ b/lib/model.ts @@ -14,6 +14,7 @@ import { ModelPermReadOpts, ModelFieldsGetOpts, ModelExportDataOpts, + RequestOptions, } from "./types"; import { createSearchPayload, @@ -42,7 +43,10 @@ export class Model { this.client = client; } - public async search(options: ModelSearchOpts): Promise { + public async search( + data: ModelSearchOpts, + options?: RequestOptions + ): Promise { const { params = [], offset = 0, @@ -50,7 +54,7 @@ export class Model { order = 0, context = null, count = false, - } = options; + } = data; const { model } = this; const { database, token } = this.client; @@ -68,11 +72,15 @@ export class Model { return await this.client._fetch({ payload, + options, }); } - public async read(options: ModelReadOpts): Promise { - const { ids, fields, context } = options; + public async read( + data: ModelReadOpts, + options?: RequestOptions + ): Promise { + const { ids, fields, context } = data; const { model } = this; const { database, token } = this.client; @@ -87,11 +95,15 @@ export class Model { return await this.client._fetch({ payload, + options, }); } - public async read_and_eval_ui(options: ModelReadEvalUiOpts): Promise { - const { ids, fields, context, attrs } = options; + public async read_and_eval_ui( + data: ModelReadEvalUiOpts, + options?: RequestOptions + ): Promise { + const { ids, fields, context, attrs } = data; const { model } = this; const { database, token } = this.client; @@ -107,11 +119,15 @@ export class Model { return await this.client._fetch({ payload, + options, }); } - public async fields_view_get(options: ModelFieldsViewGetOpts): Promise { - const { id, context, type, toolbar, version } = options; + public async fields_view_get( + data: ModelFieldsViewGetOpts, + options?: RequestOptions + ): Promise { + const { id, context, type, toolbar, version } = data; const { model } = this; const { database, token } = this.client; @@ -128,11 +144,15 @@ export class Model { return await this.client._fetch({ payload, + options, }); } - public async fields_get(options: ModelFieldsGetOpts): Promise { - const { fields, context } = options; + public async fields_get( + data: ModelFieldsGetOpts, + options?: RequestOptions + ): Promise { + const { fields, context } = data; const { model } = this; const { database, token } = this.client; @@ -146,11 +166,15 @@ export class Model { return await this.client._fetch({ payload, + options, }); } - public async execute(options: ModelExecuteOpts): Promise { - const { payload, action, context } = options; + public async execute( + data: ModelExecuteOpts, + options?: RequestOptions + ): Promise { + const { payload, action, context } = data; const { model } = this; const { database, token } = this.client; @@ -165,11 +189,15 @@ export class Model { return await this.client._fetch({ payload: executePayload, + options, }); } - public async write(options: ModelWriteOpts): Promise { - const { ids, values, context } = options; + public async write( + data: ModelWriteOpts, + options?: RequestOptions + ): Promise { + const { ids, values, context } = data; const { model } = this; const { database, token } = this.client; @@ -184,11 +212,15 @@ export class Model { return await this.client._fetch({ payload, + options, }); } - public async create(options: ModelCreateOpts): Promise { - const { values, context } = options; + public async create( + data: ModelCreateOpts, + options?: RequestOptions + ): Promise { + const { values, context } = data; const { model } = this; const { database, token } = this.client; @@ -202,11 +234,15 @@ export class Model { return await this.client._fetch({ payload, + options, }); } - public async delete(options: ModelDeleteOpts): Promise { - const { ids } = options; + public async delete( + data: ModelDeleteOpts, + options?: RequestOptions + ): Promise { + const { ids } = data; const { model } = this; const { database, token } = this.client; @@ -219,11 +255,15 @@ export class Model { return await this.client._fetch({ payload, + options, }); } - public async executeWorkflow(options: ModelExecuteOpts): Promise { - const { payload, action, context } = options; + public async executeWorkflow( + data: ModelExecuteOpts, + options?: RequestOptions + ): Promise { + const { payload, action, context } = data; const { model } = this; const { database, token } = this.client; @@ -238,13 +278,15 @@ export class Model { return await this.client._fetch({ payload: executePayload, + options, }); } public async executeOnChange( - options: ModelExecuteOnChangeOpts + data: ModelExecuteOnChangeOpts, + options?: RequestOptions ): Promise { - const { payload, action, ids } = options; + const { payload, action, ids } = data; const { model } = this; const { database, token } = this.client; @@ -259,11 +301,15 @@ export class Model { return await this.client._fetch({ payload: executePayload, + options, }); } - public async name_search(options: ModelNameSearchOpts): Promise { - const { payload, context, attrs, operator, limit } = options; + public async name_search( + data: ModelNameSearchOpts, + options?: RequestOptions + ): Promise { + const { payload, context, attrs, operator, limit } = data; const { model } = this; const { database, token } = this.client; @@ -280,11 +326,15 @@ export class Model { return await this.client._fetch({ payload: executePayload, + options, }); } - public async copy(options: ModelCopyOpts): Promise { - const { id, context } = options; + public async copy( + data: ModelCopyOpts, + options?: RequestOptions + ): Promise { + const { id, context } = data; const { model } = this; const { database, token } = this.client; @@ -298,11 +348,15 @@ export class Model { return await this.client._fetch({ payload: executePayload, + options, }); } - public async perm_read(options: ModelPermReadOpts): Promise { - const { ids, context } = options; + public async perm_read( + data: ModelPermReadOpts, + options?: RequestOptions + ): Promise { + const { ids, context } = data; const { model } = this; const { database, token } = this.client; @@ -316,17 +370,15 @@ export class Model { return await this.client._fetch({ payload, + options, }); } - public async export_data(options: ModelExportDataOpts): Promise { - const { - fields = [], - context, - domain = [], - format, - limit = null, - } = options; + public async export_data( + data: ModelExportDataOpts, + options?: RequestOptions + ): Promise { + const { fields = [], context, domain = [], format, limit = null } = data; const { model } = this; const { database, token } = this.client; @@ -343,6 +395,7 @@ export class Model { return await this.client._fetch({ payload, + options, }); } } diff --git a/lib/report.ts b/lib/report.ts index 3255257..db65715 100644 --- a/lib/report.ts +++ b/lib/report.ts @@ -1,5 +1,5 @@ import { Client } from "./client"; -import { CreateReportOpts, GetReportOpts } from "./types"; +import { CreateReportOpts, GetReportOpts, RequestOptions } from "./types"; import { createCreateReportPayload, createGetReportPayload } from "./payloads"; export class Report { @@ -9,8 +9,11 @@ export class Report { this.client = client; } - public async create(options: CreateReportOpts): Promise { - const { ids, name, datas, context = null } = options; + public async create( + data: CreateReportOpts, + options?: RequestOptions + ): Promise { + const { ids, name, datas, context = null } = data; const { database, token } = this.client; @@ -26,11 +29,15 @@ export class Report { return await this.client._fetch({ service: "report", payload, + options, }); } - public async get(options: GetReportOpts): Promise { - const { id } = options; + public async get( + data: GetReportOpts, + options?: RequestOptions + ): Promise { + const { id } = data; const { database, token } = this.client; const payload = createGetReportPayload({ @@ -42,6 +49,7 @@ export class Report { return await this.client._fetch({ service: "report", payload, + options, }); } } diff --git a/lib/types.ts b/lib/types.ts index 35322d2..b8d2d3c 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -38,6 +38,7 @@ export type Payload = Array< export type FetchOpts = { payload: Payload; service?: string; + options?: RequestOptions; }; export type ModelSearchOpts = { @@ -226,3 +227,5 @@ export type ModelExportDataPayload = Database & Token & Model & ModelExportDataOpts; + +export type RequestOptions = any; diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 5a44b93..42054ce 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -1,12 +1,12 @@ { "name": "@gisce/powerp.js", - "version": "1.2.1-rc.1", + "version": "1.3.0-rc.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@gisce/powerp.js", - "version": "1.2.1-rc.1", + "version": "1.3.0-rc.0", "license": "MIT", "devDependencies": { "@rollup/plugin-commonjs": "^21.0.1", diff --git a/package.json b/package.json index 5a5dd12..6b6a407 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@gisce/powerp.js", - "version": "1.2.1-rc.1", + "version": "1.3.0-rc.0", "description": "PowERP Javascript Client", "main": "dist/index.js", "module": "dist/index.esm.js",