Skip to content

Commit

Permalink
fix(all): fetch calls replaces request packages.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigorodriguez committed Nov 30, 2022
1 parent e4fc246 commit ea6c721
Show file tree
Hide file tree
Showing 11 changed files with 1,668 additions and 162 deletions.
1,547 changes: 1,533 additions & 14 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"Dário Vieira <dario.junior3@gmail.com>"
],
"engines": {
"node": "=19.1.0"
"node": "=19.2.0"
},
"license": "AGPL-3.0",
"preferGlobal": true,
Expand Down Expand Up @@ -143,6 +143,7 @@
"swagger-client": "^3.18.5",
"tabulator-tables": "5.4.2",
"tedious": "15.1.2",
"textract": "^2.5.0",
"twitter-api-v2": "1.12.9",
"typescript": "4.9.3",
"typescript-rest-rpc": "^1.0.7",
Expand Down
6 changes: 3 additions & 3 deletions packages/analytics.gblib/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class GuaribasConversation extends Model<GuaribasConversation> {

@CreatedAt
@Column(DataType.DATE)
createdAt: Date;
declare createdAt: Date;

@Column(DataType.STRING(255))
text: string;
Expand Down Expand Up @@ -126,11 +126,11 @@ export class GuaribasConversationMessage extends Model<GuaribasConversationMessa

@Column(DataType.DATE)
@CreatedAt
createdAt: Date;
declare createdAt: Date;

@Column(DataType.DATE)
@UpdatedAt
updatedAt: Date;
declare updatedAt: Date;

//tslint:disable-next-line:no-use-before-declare
@ForeignKey(() => GuaribasConversation)
Expand Down
4 changes: 2 additions & 2 deletions packages/basic.gblib/models/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ export class GuaribasSchedule extends Model<GuaribasSchedule> {

@Column(DataType.DATE)
@CreatedAt
createdAt: Date;
declare createdAt: Date;

@Column(DataType.DATE)
@UpdatedAt
updatedAt: Date;
declare updatedAt: Date;
}
29 changes: 12 additions & 17 deletions packages/basic.gblib/services/SystemKeywords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,11 +383,8 @@ export class SystemKeywords {
* Retrives stock inforation for a given symbol.
*/
public async getStock ({ symbol }) {
var options = {
uri: `http://live-nse.herokuapp.com/?symbol=${symbol}`
};

let data = await request.get(options);
const url = `http://live-nse.herokuapp.com/?symbol=${symbol}`;
let data = await fetch(url);
return data;
}

Expand Down Expand Up @@ -716,8 +713,8 @@ export class SystemKeywords {
const gbaiName = `${this.min.botId}.gbai`;
const localName = Path.join('work', gbaiName, 'cache', `csv${GBAdminService.getRndReadableIdentifier()}.csv`);
const url = file['@microsoft.graph.downloadUrl'];
const response = await request({ uri: url, encoding: null });
Fs.writeFileSync(localName, response, { encoding: null });
const response = await fetch(url);
Fs.writeFileSync(localName, Buffer.from(await response.arrayBuffer()), { encoding: null });

var workbook = new Excel.Workbook();
const worksheet = await workbook.csv.readFile(localName);
Expand Down Expand Up @@ -1282,7 +1279,7 @@ export class SystemKeywords {
*
*/
public async getByHttp ({ url, headers, username, ps, qs, streaming }) {
let options = { url: url };
let options = { };
if (headers) {
options['headers'] = headers;
}
Expand All @@ -1295,14 +1292,14 @@ export class SystemKeywords {
if (qs) {
options['qs'] = qs;
}
if (streaming) {
if (streaming) { // TODO: Do it with fetch.
options['responseType'] = 'stream';
options['encoding'] = null;
}
let result = await request.get(options);
let result = await fetch(url, options);

try {
return JSON.parse(result);
return JSON.parse(await result.text());
} catch (error) {
GBLog.info(`[GET]: OK.`);

Expand All @@ -1321,12 +1318,11 @@ export class SystemKeywords {
*/
public async putByHttp ({ url, data, headers }) {
const options = {
uri: url,
json: data,
headers: headers
};

let result = await request.put(options);
let result = await fetch(url, options);
GBLog.info(`[PUT]: ${url} (${data}): ${result}`);
return typeof result === 'object' ? result : JSON.parse(result);
}
Expand All @@ -1342,12 +1338,11 @@ export class SystemKeywords {
*/
public async postByHttp ({ url, data, headers }) {
const options = {
uri: url,
json: data,
headers: headers
};

let result = await request.post(options);
let result = await fetch(url, options);
GBLog.info(`[POST]: ${url} (${data}): ${result}`);

return result ? (typeof result === 'object' ? result : JSON.parse(result)) : true;
Expand Down Expand Up @@ -1375,8 +1370,8 @@ export class SystemKeywords {
let template = await this.internalGetDocument(client, baseUrl, path, templateName);
const url = template['@microsoft.graph.downloadUrl'];
const localName = Path.join('work', gbaiName, 'cache', ``);
const response = await request({ uri: url, encoding: null });
Fs.writeFileSync(localName, response, { encoding: null });
const response = await fetch( url);
Fs.writeFileSync(localName, Buffer.from(await response.arrayBuffer()), { encoding: null });

// Loads the file as binary content.

Expand Down
33 changes: 13 additions & 20 deletions packages/core.gbapp/services/GBConversationalService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import { join } from 'path';
import shell from 'any-shell-escape';
import { exec } from 'child_process';
import prism from 'prism-media';
import request from 'request-promise-native';

import SpeechToTextV1 from 'ibm-watson/speech-to-text/v1.js';
import TextToSpeechV1 from 'ibm-watson/text-to-speech/v1.js';
import { IamAuthenticator } from 'ibm-watson/auth/index.js';
Expand Down Expand Up @@ -355,27 +355,26 @@ export class GBConversationalService {
GBLog.info(`Sending SMS to ${mobile} with text: '${text}'.`);

if (!min.instance.smsKey && min.instance.smsSecret) {
const url = 'http://sms-api.megaconecta.com.br/mt';
let options = {
method: 'POST',
url: 'http://sms-api.megaconecta.com.br/mt',
headers: {
'content-type': 'application/json',
authorization: `Bearer ${min.instance.smsSecret}`
},
body: [
{
body:
JSON.stringify({
numero: `${mobile}`,
servico: 'short',
mensagem: text,
parceiro_id: '',
codificacao: '0'
}
],
json: true
})

};

try {
const results = await request(options);
const results = await fetch(url, options);

return results;
} catch (error) {
Expand Down Expand Up @@ -961,30 +960,24 @@ export class GBConversationalService {
return Promise.reject(new Error(msg));
}
} else {
const url = urlJoin(endPoint, 'translate', new URLSearchParams({
'api-version': '3.0',
to: language
}).toString());
let options = {
method: 'POST',
baseUrl: endPoint,
url: 'translate',
qs: {
'api-version': '3.0',
to: [language]
},
headers: {
'Ocp-Apim-Subscription-Key': key,
'Ocp-Apim-Subscription-Region': 'westeurope',
'Content-type': 'application/json',
'X-ClientTraceId': GBAdminService.generateUuid()
},
body: [
{
text: text
}
],
body:text,
json: true
};

try {
const results = await request(options);
const results = await fetch(url, options);

return results[0].translations[0].text;
} catch (error) {
Expand Down
6 changes: 3 additions & 3 deletions packages/core.gbapp/services/GBDeployer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import Path from 'path';
import express from 'express';
import child_process from 'child_process';
import rimraf from 'rimraf';
import request from 'request-promise-native';

import vhost from 'vhost';
import urlJoin from 'url-join';
import Fs from 'fs';
Expand Down Expand Up @@ -522,8 +522,8 @@ export class GBDeployer implements IGBDeployer {
GBLog.info(`Downloading ${itemPath}...`);
const url = item['@microsoft.graph.downloadUrl'];

const response = await request({ uri: url, encoding: null });
Fs.writeFileSync(itemPath, response, { encoding: null });
const response = await fetch(url);
Fs.writeFileSync(itemPath, Buffer.from(await response.arrayBuffer()), { encoding: null });
Fs.utimesSync(itemPath, new Date(), new Date(item.lastModifiedDateTime));
} else {
GBLog.info(`Local is up to date: ${itemPath}...`);
Expand Down
13 changes: 6 additions & 7 deletions packages/core.gbapp/services/GBMinService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ import cliProgress from 'cli-progress';
import { DialogSet, TextPrompt } from 'botbuilder-dialogs';
import express from 'express';
import Swagger from 'swagger-client';
import request from 'request-promise-native';

import removeRoute from 'express-remove-route';
import AuthenticationContext from '@azure/msal-node';
import AuthenticationContext from 'adal-node';
import wash from 'washyourmouthoutwithsoap';
import { FacebookAdapter } from 'botbuilder-adapter-facebook';
import path from 'path';
Expand Down Expand Up @@ -574,18 +574,18 @@ export class GBMinService {
* Gets Webchat token from Bot Service.
*/
private async getWebchatToken (instance: any) {
const url = 'https://directline.botframework.com/v3/directline/tokens/generate';
const options = {
url: 'https://directline.botframework.com/v3/directline/tokens/generate',
method: 'POST',
headers: {
Authorization: `Bearer ${instance.webchatKey}`
}
};

try {
const json = await request(options);
const res = await fetch(url, options);

return JSON.parse(json);
return await res.json();
} catch (error) {
const msg = `[botId:${instance.botId}] Error calling Direct Line to generate a token for Web control: ${error}.`;

Expand All @@ -598,15 +598,14 @@ export class GBMinService {
*/
private async getSTSToken (instance: any) {
const options = {
url: instance.speechEndpoint,
method: 'POST',
headers: {
'Ocp-Apim-Subscription-Key': instance.speechKey
}
};

try {
return await request(options);
return await fetch(instance.speechEndpoint, options);
} catch (error) {
const msg = `Error calling Speech to Text client. Error is: ${error}.`;

Expand Down
4 changes: 2 additions & 2 deletions packages/kb.gbapp/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,11 @@ export class GuaribasQuestion extends Model<GuaribasQuestion> {

@Column(DataType.DATE)
@CreatedAt
createdAt: Date;
declare createdAt: Date;

@Column(DataType.DATE)
@UpdatedAt
updatedAt: Date;
declare updatedAt: Date;

//tslint:disable-next-line:no-use-before-declare
@ForeignKey(() => GuaribasAnswer)
Expand Down

0 comments on commit ea6c721

Please sign in to comment.