From 2e1a34634aaa188175a86bef9e28dc51953ec9ed Mon Sep 17 00:00:00 2001 From: Blazej Kustra Date: Thu, 14 Sep 2023 15:10:27 +0200 Subject: [PATCH 1/3] [TS migration] Migrate 'Request.js' lib to TypeScript --- src/libs/{Request.js => Request.ts} | 32 ++++++++--------------------- src/types/onyx/Request.ts | 4 ++-- 2 files changed, 11 insertions(+), 25 deletions(-) rename src/libs/{Request.js => Request.ts} (54%) diff --git a/src/libs/Request.js b/src/libs/Request.ts similarity index 54% rename from src/libs/Request.js rename to src/libs/Request.ts index 577dcf3cb85d..57aedee91b8e 100644 --- a/src/libs/Request.js +++ b/src/libs/Request.ts @@ -1,43 +1,29 @@ -import _ from 'underscore'; import HttpUtils from './HttpUtils'; import enhanceParameters from './Network/enhanceParameters'; import * as NetworkStore from './Network/NetworkStore'; +import Request from '../types/onyx/Request'; -let middlewares = []; +type Middleware = (response: unknown, request: Request, isFromSequentialQueue: boolean) => Promise; -/** - * @param {Object} request - * @param {String} request.command - * @param {Object} request.data - * @param {String} request.type - * @param {Boolean} request.shouldUseSecure - * @returns {Promise} - */ -function makeXHR(request) { +let middlewares: Middleware[] = []; + +function makeXHR(request: Request): Promise { const finalParameters = enhanceParameters(request.command, request.data); return NetworkStore.hasReadRequiredDataFromStorage().then(() => { // If we're using the Supportal token and this is not a Supportal request // let's just return a promise that will resolve itself. if (NetworkStore.getSupportAuthToken() && !NetworkStore.isSupportRequest(request.command)) { - return new Promise((resolve) => resolve()); + return new Promise((resolve) => resolve(undefined)); } return HttpUtils.xhr(request.command, finalParameters, request.type, request.shouldUseSecure); }); } -/** - * @param {Object} request - * @param {Boolean} [isFromSequentialQueue] - * @returns {Promise} - */ -function processWithMiddleware(request, isFromSequentialQueue = false) { - return _.reduce(middlewares, (last, middleware) => middleware(last, request, isFromSequentialQueue), makeXHR(request)); +function processWithMiddleware(request: Request, isFromSequentialQueue = false): Promise { + return middlewares.reduce((last, middleware) => middleware(last, request, isFromSequentialQueue), makeXHR(request)); } -/** - * @param {Function} middleware - */ -function use(middleware) { +function use(middleware: Middleware) { middlewares.push(middleware); } diff --git a/src/types/onyx/Request.ts b/src/types/onyx/Request.ts index 1df20cfb28fe..0ed6c8ebcef2 100644 --- a/src/types/onyx/Request.ts +++ b/src/types/onyx/Request.ts @@ -1,8 +1,8 @@ import {OnyxUpdate} from 'react-native-onyx'; type Request = { - command?: string; - data?: Record; + command: string; + data: Record; type?: string; shouldUseSecure?: boolean; successData?: OnyxUpdate[]; From 5bb29efa15f0c1c690e7ed04bcc5739eb4da8e69 Mon Sep 17 00:00:00 2001 From: Blazej Kustra Date: Thu, 14 Sep 2023 15:13:26 +0200 Subject: [PATCH 2/3] Make data optional --- src/libs/Request.ts | 2 +- src/types/onyx/Request.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/Request.ts b/src/libs/Request.ts index 57aedee91b8e..ac4913e9833a 100644 --- a/src/libs/Request.ts +++ b/src/libs/Request.ts @@ -8,7 +8,7 @@ type Middleware = (response: unknown, request: Request, isFromSequentialQueue: b let middlewares: Middleware[] = []; function makeXHR(request: Request): Promise { - const finalParameters = enhanceParameters(request.command, request.data); + const finalParameters = enhanceParameters(request.command, request?.data ?? {}); return NetworkStore.hasReadRequiredDataFromStorage().then(() => { // If we're using the Supportal token and this is not a Supportal request // let's just return a promise that will resolve itself. diff --git a/src/types/onyx/Request.ts b/src/types/onyx/Request.ts index 0ed6c8ebcef2..94f14af0ddb3 100644 --- a/src/types/onyx/Request.ts +++ b/src/types/onyx/Request.ts @@ -2,7 +2,7 @@ import {OnyxUpdate} from 'react-native-onyx'; type Request = { command: string; - data: Record; + data?: Record; type?: string; shouldUseSecure?: boolean; successData?: OnyxUpdate[]; From 7c9bf0484c2d56b90a419d4df02cedd5b312b81a Mon Sep 17 00:00:00 2001 From: Blazej Kustra Date: Fri, 15 Sep 2023 09:51:25 +0200 Subject: [PATCH 3/3] Add generic param to promise --- src/libs/Request.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/Request.ts b/src/libs/Request.ts index ac4913e9833a..459deaf89e1e 100644 --- a/src/libs/Request.ts +++ b/src/libs/Request.ts @@ -13,7 +13,7 @@ function makeXHR(request: Request): Promise { // If we're using the Supportal token and this is not a Supportal request // let's just return a promise that will resolve itself. if (NetworkStore.getSupportAuthToken() && !NetworkStore.isSupportRequest(request.command)) { - return new Promise((resolve) => resolve(undefined)); + return new Promise((resolve) => resolve()); } return HttpUtils.xhr(request.command, finalParameters, request.type, request.shouldUseSecure); });