Skip to content

Commit

Permalink
Merge pull request #27438 from software-mansion-labs/ts-migration/Req…
Browse files Browse the repository at this point in the history
…uest

[No QA][TS migration] Migrate 'Request.js' lib to TypeScript
  • Loading branch information
arosiclair authored Sep 16, 2023
2 parents 76c1559 + 7c9bf04 commit c77814f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 25 deletions.
34 changes: 10 additions & 24 deletions src/libs/Request.js → src/libs/Request.ts
Original file line number Diff line number Diff line change
@@ -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<unknown>;

/**
* @param {Object} request
* @param {String} request.command
* @param {Object} request.data
* @param {String} request.type
* @param {Boolean} request.shouldUseSecure
* @returns {Promise}
*/
function makeXHR(request) {
const finalParameters = enhanceParameters(request.command, request.data);
let middlewares: Middleware[] = [];

function makeXHR(request: Request): Promise<unknown> {
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<void>((resolve) => resolve());
}
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<unknown> {
return middlewares.reduce((last, middleware) => middleware(last, request, isFromSequentialQueue), makeXHR(request));
}

/**
* @param {Function} middleware
*/
function use(middleware) {
function use(middleware: Middleware) {
middlewares.push(middleware);
}

Expand Down
2 changes: 1 addition & 1 deletion src/types/onyx/Request.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {OnyxUpdate} from 'react-native-onyx';

type Request = {
command?: string;
command: string;
data?: Record<string, unknown>;
type?: string;
shouldUseSecure?: boolean;
Expand Down

0 comments on commit c77814f

Please sign in to comment.