aotrautils-srv 0.0.925 → 0.0.927

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
 
2
2
 
3
- /*utils COMMONS library associated with aotra version : «1_29072022-2359 (07/04/2024-18:24:28)»*/
3
+ /*utils COMMONS library associated with aotra version : «1_29072022-2359 (07/04/2024-19:08:28)»*/
4
4
  /*-----------------------------------------------------------------------------*/
5
5
 
6
6
 
@@ -5221,7 +5221,7 @@ class OpenAIAPIClient{
5221
5221
  };
5222
5222
 
5223
5223
  return new Promise((resolve,reject)=>{
5224
- performHTTPRequestNode(this.apiURL,"POST",headers,parameters).then((responseObj)=>{
5224
+ performHTTPRequest(this.apiURL,"POST",headers,parameters,true).then((responseObj)=>{
5225
5225
  const responseData=responseObj.responseData;
5226
5226
  const data=responseData;
5227
5227
 
@@ -5248,48 +5248,6 @@ class OpenAIAPIClient{
5248
5248
  });
5249
5249
 
5250
5250
 
5251
- // DOES NOT WORK :
5252
- // return new Promise((resolve,reject)=>{
5253
- //
5254
- // fetch(self.apiURL, {
5255
- // method: "POST",
5256
- // headers: headers,
5257
- // body: JSON.stringify(parameters),
5258
- // })
5259
- // // STRANGE : DOES NOT WORK !!:
5260
- // //.then(response => response.json())
5261
- // // STRANGE : DOES WORK :
5262
- // .then(response => {
5263
- //
5264
- // // DBG
5265
- // console.log("~~~~~~~~~~~response :",response);
5266
- //
5267
- // return response.json();
5268
- // })
5269
- // .then(data => {
5270
- //
5271
- // if(data.error){
5272
- // const error=data.error;
5273
- // // TRACE
5274
- // console.error("Error:", error);
5275
- // reject(""+error);
5276
- // return;
5277
- // }
5278
- //
5279
- // // DBG
5280
- // console.log("~~~~~~~~~~~data :",data);
5281
- //
5282
- // const assistantReply = data.choices[0].message.content;
5283
- // resolve(assistantReply);
5284
- // })
5285
- // .catch(error => {
5286
- // // TRACE
5287
- // console.error("Error:", error);
5288
- // reject(""+error);
5289
- // });
5290
- // });
5291
-
5292
-
5293
5251
 
5294
5252
 
5295
5253
  }
@@ -5317,7 +5275,7 @@ AOTRAUTILS_LIB_IS_LOADED=true;
5317
5275
 
5318
5276
 
5319
5277
 
5320
- /*utils SERVER library associated with aotra version : «1_29072022-2359 (07/04/2024-18:24:28)»*/
5278
+ /*utils SERVER library associated with aotra version : «1_29072022-2359 (07/04/2024-19:08:28)»*/
5321
5279
  /*-----------------------------------------------------------------------------*/
5322
5280
 
5323
5281
 
@@ -7000,27 +6958,11 @@ getListManager=function(config){
7000
6958
 
7001
6959
 
7002
6960
  // NO : IN A NODE CONTEXT WITH requre("") WILL RESULT IN AN UNDEFINED FUNCTION ERROR !!!
7003
- // function performHTTPRequestNode(...){...
6961
+ // function performHTTPRequest(...){...
7004
6962
  // USE THIS INSTEAD :
7005
- performHTTPRequestNode=function(completeURL,httpMethod="GET",headers={},requestBodyOrNamedArgs=null,addCORSHeader=ADD_CORS_HEADER){
7006
-
7007
- if(!require){
7008
- // TODO : FIXME : Support also a browser context!!
7009
- lognow("ERROR : require() not found, this may need we are not running in a nodejs context. This is currently not supported. Aborting");
7010
- throw new Error("Unsupported context non-nodejs for performHTTPRequestNode().");
7011
- }
7012
-
7013
- const isSecure=(!empty(completeURL) && contains(completeURL.toLowerCase(),"https://"));
7014
- const httpHandler=isSecure?require("https"):require("http");
7015
-
7016
- // Options for the HTTP request
7017
- const options = {
7018
- url: completeURL,
7019
- method: httpMethod,
7020
- };
6963
+ performHTTPRequest=function(completeURL,httpMethod="GET",headers={},requestBodyOrNamedArgs=null,isNodeContext=false,addCORSHeader=ADD_CORS_HEADER){
7021
6964
 
7022
6965
  if(contains(["POST","PUT"],httpMethod)){
7023
- options.json=true;
7024
6966
  headers["Content-Type"]="application/json";
7025
6967
  // To remove the CORS error message (cf. https://medium.com/@dtkatz/3-ways-to-fix-the-cors-error-and-how-access-control-allow-origin-works-d97d55946d9)
7026
6968
  if(addCORSHeader) headers["Access-Control-Allow-Origin"]="*";
@@ -7035,6 +6977,65 @@ performHTTPRequestNode=function(completeURL,httpMethod="GET",headers={},requestB
7035
6977
  lognow("formatted API URL : "+completeURL);
7036
6978
  }
7037
6979
 
6980
+ // CASE BROWSER CONTEXT :
6981
+ if(!isNodeContext || typeof(require)=="undefined"){
6982
+ // TRACE
6983
+ lognow("INFO : require() not found, this may need we are not running in a nodejs context. This is currently not supported. Aborting");
6984
+
6985
+ const bodyL=((contains(["POST","PUT"],httpMethod) && requestBodyOrNamedArgs)?JSON.stringify(requestBodyOrNamedArgs):null);
6986
+
6987
+ return new Promise((resolve,reject)=>{
6988
+ fetch(completeURL, {
6989
+ method: httpMethod,
6990
+ headers: headers,
6991
+ body: body,
6992
+ })
6993
+ // STRANGE : DOES NOT WORK !!:
6994
+ //.then(response => response.json())
6995
+ // STRANGE : DOES WORK :
6996
+ .then(response => {
6997
+
6998
+ // DBG
6999
+ console.log("~~~~~~~~~~~response :",response);
7000
+
7001
+ return response.json();
7002
+ }).then(data => {
7003
+
7004
+ if(data.error){
7005
+ const error=data.error;
7006
+ // TRACE
7007
+ console.error("Error:", error);
7008
+ reject(""+error);
7009
+ return;
7010
+ }
7011
+
7012
+ // DBG
7013
+ console.log("~~~~~~~~~~~data :",data);
7014
+
7015
+ resolve(data);
7016
+ }).catch(error => {
7017
+ // TRACE
7018
+ console.error("Error:", error);
7019
+ reject(`${error}`);
7020
+ });
7021
+ });
7022
+ }// else :
7023
+
7024
+ // CASE NODEJS CONTEXT :
7025
+
7026
+ const isSecure=(!empty(completeURL) && contains(completeURL.toLowerCase(),"https://"));
7027
+ const httpHandler=isSecure?require("https"):require("http");
7028
+
7029
+ // Options for the HTTP request
7030
+ const options = {
7031
+ url: completeURL,
7032
+ method: httpMethod,
7033
+ };
7034
+
7035
+
7036
+ if(contains(["POST","PUT"],httpMethod)){
7037
+ options.json=true;
7038
+ }
7038
7039
 
7039
7040
  options.headers=headers;
7040
7041
 
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aotrautils-srv",
3
- "version": "0.0.925",
3
+ "version": "0.0.927",
4
4
  "main": "aotrautils-srv.build.js",
5
5
  "description": "A library for vanilla javascript utils (server-side) used in aotra javascript CMS",
6
6
  "author": "Jeremie Ratomposon <info@alqemia.com> (https://alqemia.com)",