Skip to content

Commit

Permalink
Merge pull request #34542 from software-mansion-labs/ts/centralized-a…
Browse files Browse the repository at this point in the history
…pi-types

[TS migration] Create centralized types for API params
  • Loading branch information
roryabraham authored Jan 26, 2024
2 parents 4dbd99b + c8f663a commit d4db5fc
Show file tree
Hide file tree
Showing 152 changed files with 1,720 additions and 948 deletions.
28 changes: 13 additions & 15 deletions src/libs/API.ts → src/libs/API/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type {OnyxUpdate} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import Log from '@libs/Log';
import * as Middleware from '@libs/Middleware';
import * as SequentialQueue from '@libs/Network/SequentialQueue';
import * as Pusher from '@libs/Pusher/pusher';
import * as Request from '@libs/Request';
import CONST from '@src/CONST';
import type OnyxRequest from '@src/types/onyx/Request';
import type Response from '@src/types/onyx/Response';
import pkg from '../../package.json';
import Log from './Log';
import * as Middleware from './Middleware';
import * as SequentialQueue from './Network/SequentialQueue';
import * as Pusher from './Pusher/pusher';
import * as Request from './Request';
import pkg from '../../../package.json';
import type {ApiRequest, ApiRequestCommandParameters, ReadCommand, SideEffectRequestCommand, WriteCommand} from './types';

// Setup API middlewares. Each request made will pass through a series of middleware functions that will get called in sequence (each one passing the result of the previous to the next).
// Note: The ordering here is intentional as we want to Log, Recheck Connection, Reauthenticate, and Save the Response in Onyx. Errors thrown in one middleware will bubble to the next.
Expand Down Expand Up @@ -38,8 +38,6 @@ type OnyxData = {
finallyData?: OnyxUpdate[];
};

type ApiRequestType = ValueOf<typeof CONST.API_REQUEST_TYPE>;

/**
* All calls to API.write() will be persisted to disk as JSON with the params, successData, and failureData (or finallyData, if included in place of the former two values).
* This is so that if the network is unavailable or the app is closed, we can send the WRITE request later.
Expand All @@ -54,7 +52,7 @@ type ApiRequestType = ValueOf<typeof CONST.API_REQUEST_TYPE>;
* @param [onyxData.failureData] - Onyx instructions that will be passed to Onyx.update() when the response has jsonCode !== 200.
* @param [onyxData.finallyData] - Onyx instructions that will be passed to Onyx.update() when the response has jsonCode === 200 or jsonCode !== 200.
*/
function write(command: string, apiCommandParameters: Record<string, unknown> = {}, onyxData: OnyxData = {}) {
function write<TCommand extends WriteCommand>(command: TCommand, apiCommandParameters: ApiRequestCommandParameters[TCommand], onyxData: OnyxData = {}) {
Log.info('Called API write', false, {command, ...apiCommandParameters});
const {optimisticData, ...onyxDataWithoutOptimisticData} = onyxData;

Expand Down Expand Up @@ -112,11 +110,11 @@ function write(command: string, apiCommandParameters: Record<string, unknown> =
* response back to the caller or to trigger reconnection callbacks when re-authentication is required.
* @returns
*/
function makeRequestWithSideEffects(
command: string,
apiCommandParameters = {},
function makeRequestWithSideEffects<TCommand extends SideEffectRequestCommand | WriteCommand | ReadCommand>(
command: TCommand,
apiCommandParameters: ApiRequestCommandParameters[TCommand],
onyxData: OnyxData = {},
apiRequestType: ApiRequestType = CONST.API_REQUEST_TYPE.MAKE_REQUEST_WITH_SIDE_EFFECTS,
apiRequestType: ApiRequest = CONST.API_REQUEST_TYPE.MAKE_REQUEST_WITH_SIDE_EFFECTS,
): Promise<void | Response> {
Log.info('Called API makeRequestWithSideEffects', false, {command, ...apiCommandParameters});
const {optimisticData, ...onyxDataWithoutOptimisticData} = onyxData;
Expand Down Expand Up @@ -157,7 +155,7 @@ function makeRequestWithSideEffects(
* @param [onyxData.failureData] - Onyx instructions that will be passed to Onyx.update() when the response has jsonCode !== 200.
* @param [onyxData.finallyData] - Onyx instructions that will be passed to Onyx.update() when the response has jsonCode === 200 or jsonCode !== 200.
*/
function read(command: string, apiCommandParameters: Record<string, unknown>, onyxData: OnyxData = {}) {
function read<TCommand extends ReadCommand>(command: TCommand, apiCommandParameters: ApiRequestCommandParameters[TCommand], onyxData: OnyxData = {}) {
// Ensure all write requests on the sequential queue have finished responding before running read requests.
// Responses from read requests can overwrite the optimistic data inserted by
// write requests that use the same Onyx keys and haven't responded yet.
Expand Down
6 changes: 6 additions & 0 deletions src/libs/API/parameters/AcceptWalletTermsParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type AcceptWalletTermsParams = {
hasAcceptedTerms: boolean;
reportID: string;
};

export default AcceptWalletTermsParams;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type ActivatePhysicalExpensifyCardParams = {
cardLastFourDigits: string;
cardID: number;
};
export default ActivatePhysicalExpensifyCardParams;
13 changes: 13 additions & 0 deletions src/libs/API/parameters/AddCommentOrAttachementParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
type AddCommentOrAttachementParams = {
reportID: string;
reportActionID?: string;
commentReportActionID?: string | null;
reportComment?: string;
file?: File;
timezone?: string;
shouldAllowActionableMentionWhispers?: boolean;
clientCreatedTime?: string;
isOldDotConciergeChat?: boolean;
};

export default AddCommentOrAttachementParams;
10 changes: 10 additions & 0 deletions src/libs/API/parameters/AddEmojiReactionParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
type AddEmojiReactionParams = {
reportID: string;
skinTone: string | number;
emojiCode: string;
reportActionID: string;
createdAt: string;
useEmojiReactions: boolean;
};

export default AddEmojiReactionParams;
8 changes: 8 additions & 0 deletions src/libs/API/parameters/AddMembersToWorkspaceParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type AddMembersToWorkspaceParams = {
employees: string;
welcomeNote: string;
policyID: string;
reportCreationData?: string;
};

export default AddMembersToWorkspaceParams;
3 changes: 3 additions & 0 deletions src/libs/API/parameters/AddNewContactMethodParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type AddNewContactMethodParams = {partnerUserID: string};

export default AddNewContactMethodParams;
14 changes: 14 additions & 0 deletions src/libs/API/parameters/AddPaymentCardParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type {ValueOf} from 'type-fest';
import type CONST from '@src/CONST';

type AddPaymentCardParams = {
cardNumber: string;
cardYear: string;
cardMonth: string;
cardCVV: string;
addressName: string;
addressZip: string;
currency: ValueOf<typeof CONST.CURRENCY>;
isP2PDebitCard: boolean;
};
export default AddPaymentCardParams;
12 changes: 12 additions & 0 deletions src/libs/API/parameters/AddPersonalBankAccountParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
type AddPersonalBankAccountParams = {
addressName: string;
routingNumber: string;
accountNumber: string;
isSavings: boolean;
setupType: string;
bank?: string;
plaidAccountID: string;
plaidAccessToken: string;
};

export default AddPersonalBankAccountParams;
9 changes: 9 additions & 0 deletions src/libs/API/parameters/AddSchoolPrincipalParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
type AddSchoolPrincipalParams = {
firstName: string;
lastName: string;
partnerUserID: string;
policyID: string;
reportCreationData: string;
};

export default AddSchoolPrincipalParams;
15 changes: 15 additions & 0 deletions src/libs/API/parameters/AddWorkspaceRoomParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type {ValueOf} from 'type-fest';
import type CONST from '@src/CONST';
import type {WriteCapability} from '@src/types/onyx/Report';

type AddWorkspaceRoomParams = {
reportID: string;
createdReportActionID: string;
policyID?: string;
reportName?: string;
visibility?: ValueOf<typeof CONST.REPORT.VISIBILITY>;
writeCapability?: WriteCapability;
welcomeMessage?: string;
};

export default AddWorkspaceRoomParams;
6 changes: 6 additions & 0 deletions src/libs/API/parameters/AnswerQuestionsForWalletParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type AnswerQuestionsForWalletParams = {
idologyAnswers: string;
idNumber: string;
};

export default AnswerQuestionsForWalletParams;
10 changes: 10 additions & 0 deletions src/libs/API/parameters/AuthenticatePusherParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
type AuthenticatePusherParams = {
// eslint-disable-next-line @typescript-eslint/naming-convention
socket_id: string;
// eslint-disable-next-line @typescript-eslint/naming-convention
channel_name: string;
shouldRetry: boolean;
forceNetworkRequest: boolean;
};

export default AuthenticatePusherParams;
7 changes: 7 additions & 0 deletions src/libs/API/parameters/BankAccountHandlePlaidErrorParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type BankAccountHandlePlaidErrorParams = {
bankAccountID: number;
error: string;
errorDescription: string;
plaidRequestID: string;
};
export default BankAccountHandlePlaidErrorParams;
9 changes: 9 additions & 0 deletions src/libs/API/parameters/BeginAppleSignInParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type {ValueOf} from 'type-fest';
import type CONST from '@src/CONST';

type BeginAppleSignInParams = {
idToken: string | undefined | null;
preferredLocale: ValueOf<typeof CONST.LOCALES> | null;
};

export default BeginAppleSignInParams;
9 changes: 9 additions & 0 deletions src/libs/API/parameters/BeginGoogleSignInParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type {ValueOf} from 'type-fest';
import type CONST from '@src/CONST';

type BeginGoogleSignInParams = {
token: string | null;
preferredLocale: ValueOf<typeof CONST.LOCALES> | null;
};

export default BeginGoogleSignInParams;
5 changes: 5 additions & 0 deletions src/libs/API/parameters/BeginSignInParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type BeginSignInParams = {
email: string;
};

export default BeginSignInParams;
6 changes: 6 additions & 0 deletions src/libs/API/parameters/CancelTaskParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type CancelTaskParams = {
cancelledTaskReportActionID?: string;
taskReportID?: string;
};

export default CancelTaskParams;
6 changes: 6 additions & 0 deletions src/libs/API/parameters/ChronosRemoveOOOEventParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type ChronosRemoveOOOEventParams = {
googleEventID: string;
reportActionID: string;
};

export default ChronosRemoveOOOEventParams;
3 changes: 3 additions & 0 deletions src/libs/API/parameters/CloseAccountParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type CloseAccountParams = {message: string};

export default CloseAccountParams;
10 changes: 10 additions & 0 deletions src/libs/API/parameters/CompleteEngagementModalParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
type CompleteEngagementModalParams = {
reportID: string;
reportActionID?: string;
commentReportActionID?: string | null;
reportComment?: string;
engagementChoice: string;
timezone?: string;
};

export default CompleteEngagementModalParams;
6 changes: 6 additions & 0 deletions src/libs/API/parameters/CompleteTaskParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type CompleteTaskParams = {
taskReportID?: string;
completedTaskReportActionID?: string;
};

export default CompleteTaskParams;
7 changes: 7 additions & 0 deletions src/libs/API/parameters/ConnectBankAccountManuallyParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type ConnectBankAccountManuallyParams = {
bankAccountID: number;
accountNumber?: string;
routingNumber?: string;
plaidMask?: string;
};
export default ConnectBankAccountManuallyParams;
10 changes: 10 additions & 0 deletions src/libs/API/parameters/ConnectBankAccountWithPlaidParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
type ConnectBankAccountWithPlaidParams = {
bankAccountID: number;
routingNumber: string;
accountNumber: string;
bank?: string;
plaidAccountID: string;
plaidAccessToken: string;
};

export default ConnectBankAccountWithPlaidParams;
15 changes: 15 additions & 0 deletions src/libs/API/parameters/CreateTaskParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
type CreateTaskParams = {
parentReportActionID?: string;
parentReportID?: string;
taskReportID?: string;
createdTaskReportActionID?: string;
title?: string;
description?: string;
assignee?: string;
assigneeAccountID?: number;
assigneeChatReportID?: string;
assigneeChatReportActionID?: string;
assigneeChatCreatedReportActionID?: string;
};

export default CreateTaskParams;
20 changes: 20 additions & 0 deletions src/libs/API/parameters/CreateWorkspaceFromIOUPaymentParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
type CreateWorkspaceFromIOUPaymentParams = {
policyID: string;
announceChatReportID: string;
adminsChatReportID: string;
expenseChatReportID: string;
ownerEmail: string;
makeMeAdmin: boolean;
policyName: string;
type: string;
announceCreatedReportActionID: string;
adminsCreatedReportActionID: string;
expenseCreatedReportActionID: string;
customUnitID: string;
customUnitRateID: string;
iouReportID: string;
memberData: string;
reportActionID: string;
};

export default CreateWorkspaceFromIOUPaymentParams;
17 changes: 17 additions & 0 deletions src/libs/API/parameters/CreateWorkspaceParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
type CreateWorkspaceParams = {
policyID: string;
announceChatReportID: string;
adminsChatReportID: string;
expenseChatReportID: string;
ownerEmail: string;
makeMeAdmin: boolean;
policyName: string;
type: string;
announceCreatedReportActionID: string;
adminsCreatedReportActionID: string;
expenseCreatedReportActionID: string;
customUnitID: string;
customUnitRateID: string;
};

export default CreateWorkspaceParams;
6 changes: 6 additions & 0 deletions src/libs/API/parameters/DeleteCommentParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type DeleteCommentParams = {
reportID: string;
reportActionID: string;
};

export default DeleteCommentParams;
3 changes: 3 additions & 0 deletions src/libs/API/parameters/DeleteContactMethodParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type DeleteContactMethodParams = {partnerUserID: string};

export default DeleteContactMethodParams;
6 changes: 6 additions & 0 deletions src/libs/API/parameters/DeleteMembersFromWorkspaceParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type DeleteMembersFromWorkspaceParams = {
emailList: string;
policyID: string;
};

export default DeleteMembersFromWorkspaceParams;
3 changes: 3 additions & 0 deletions src/libs/API/parameters/DeletePaymentBankAccountParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type DeletePaymentBankAccountParams = {bankAccountID: number};

export default DeletePaymentBankAccountParams;
4 changes: 4 additions & 0 deletions src/libs/API/parameters/DeletePaymentCardParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type DeletePaymentCardParams = {
fundID: number;
};
export default DeletePaymentCardParams;
5 changes: 5 additions & 0 deletions src/libs/API/parameters/DeleteWorkspaceAvatarParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type DeleteWorkspaceAvatarParams = {
policyID: string;
};

export default DeleteWorkspaceAvatarParams;
5 changes: 5 additions & 0 deletions src/libs/API/parameters/DeleteWorkspaceParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type DeleteWorkspaceParams = {
policyID: string;
};

export default DeleteWorkspaceParams;
10 changes: 10 additions & 0 deletions src/libs/API/parameters/EditTaskAssigneeParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
type EditTaskAssigneeParams = {
taskReportID?: string;
assignee?: string;
editedTaskReportActionID?: string;
assigneeChatReportID?: string;
assigneeChatReportActionID?: string;
assigneeChatCreatedReportActionID?: string;
};

export default EditTaskAssigneeParams;
8 changes: 8 additions & 0 deletions src/libs/API/parameters/EditTaskParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type EditTaskParams = {
taskReportID?: string;
title?: string;
description?: string;
editedTaskReportActionID?: string;
};

export default EditTaskParams;
6 changes: 6 additions & 0 deletions src/libs/API/parameters/ExpandURLPreviewParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type ExpandURLPreviewParams = {
reportID: string;
reportActionID: string;
};

export default ExpandURLPreviewParams;
Loading

0 comments on commit d4db5fc

Please sign in to comment.