Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TS migration] Create centralized types for API params #34542

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
dce5acc
Prepare centralized setup for API
blazejkustra Jan 15, 2024
44c85c5
Add all read commands
blazejkustra Jan 15, 2024
d6bd5f2
Fix lint
blazejkustra Jan 15, 2024
feb1d36
Add remaining write commands
blazejkustra Jan 16, 2024
c2f3bc0
Change write string commands
blazejkustra Jan 16, 2024
25e962a
Remove all Params types
blazejkustra Jan 16, 2024
240994a
Add Write params to separate files
blazejkustra Jan 16, 2024
dd6068d
Fix parameters files
blazejkustra Jan 16, 2024
fa5891d
Add remaining write commands values to the WriteCommandParameters map…
blazejkustra Jan 16, 2024
0a994a9
Merge branch 'main' into ts/centralized-api-types
blazejkustra Jan 17, 2024
b996dda
Correct some of the files manually
blazejkustra Jan 17, 2024
7dd5f0b
Add remaining write params
blazejkustra Jan 17, 2024
e98c37c
Fix typecheck
blazejkustra Jan 17, 2024
bc873ab
Merge branch 'main' into ts/centralized-api-types
blazejkustra Jan 23, 2024
8444a4d
Final API refactors, use const commands
blazejkustra Jan 23, 2024
7e2f4b6
Cast to string[]
blazejkustra Jan 23, 2024
3d1cc81
Create Policy API params
blazejkustra Jan 23, 2024
7261fc2
Add missing policy params
blazejkustra Jan 23, 2024
263b558
Update api mapping after adding Policy
blazejkustra Jan 23, 2024
d9a1330
Import Policy params in Policy.ts
blazejkustra Jan 23, 2024
ea5c641
Add Task parameters
blazejkustra Jan 23, 2024
010bfa1
Add last Task parameters, add task parameters to WRITE_COMMANDS and m…
blazejkustra Jan 23, 2024
b8fc653
Fix formatting
blazejkustra Jan 23, 2024
ff77a8e
Adjust types after review
blazejkustra Jan 24, 2024
38dea6e
Merge branch 'main' into ts/centralized-api-types
blazejkustra Jan 24, 2024
57c421f
Add new write commands
blazejkustra Jan 24, 2024
4a1e5a4
Retrigger actions
blazejkustra Jan 24, 2024
66d668b
Merge branch 'main' into ts/centralized-api-types
blazejkustra Jan 25, 2024
c5aac10
Merge branch 'main' into ts/centralized-api-types
blazejkustra Jan 26, 2024
c8f663a
Merge branch 'main' into ts/centralized-api-types
blazejkustra Jan 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
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
Loading