From 45d6c92800cb227b7b021206cb7e3f68fe91ba7a Mon Sep 17 00:00:00 2001 From: Jean-Philippe Monette Date: Wed, 17 Feb 2021 12:02:10 +0000 Subject: [PATCH 1/9] Add promise overload to mobileSync --- src/react.force.mobilesync.ts | 189 ++++++++++++++++------------------ src/typings/mobilesync.d.ts | 102 ++++++++++++++++-- 2 files changed, 181 insertions(+), 110 deletions(-) diff --git a/src/react.force.mobilesync.ts b/src/react.force.mobilesync.ts index 9dd4a8f..032b81b 100644 --- a/src/react.force.mobilesync.ts +++ b/src/react.force.mobilesync.ts @@ -26,7 +26,18 @@ import { NativeModules } from "react-native"; import { exec as forceExec, ExecErrorCallback, ExecSuccessCallback } from "./react.force.common"; import { StoreConfig } from "./react.force.smartstore"; -import { SyncDownTarget, SyncEvent, SyncMethod, SyncOptions, SyncStatus } from "./typings/mobilesync"; +import { + CleanResyncGhostsOverload, + GetSyncStatusOverload, + ReSyncOverload, + SyncDownOverload, + SyncDownTarget, + SyncEvent, + SyncMethod, + SyncOptions, + SyncStatus, + SyncUpOverload, +} from "./typings/mobilesync"; const { MobileSyncReactBridge, SFMobileSyncReactBridge } = NativeModules; @@ -48,11 +59,26 @@ const checkFirstArg = (arg: StoreConfig) => { }; const exec = ( - successCB: ExecSuccessCallback, - errorCB: ExecErrorCallback, + successCB: ExecSuccessCallback | undefined, + errorCB: ExecErrorCallback | undefined, methodName: SyncMethod, args: Record, -) => { +): Promise | void => { + if (!successCB || !errorCB) { + return new Promise((resolve, reject) => { + forceExec( + "SFMobileSyncReactBridge", + "MobileSyncReactBridge", + SFMobileSyncReactBridge, + MobileSyncReactBridge, + resolve, + reject, + methodName, + args, + ); + }); + } + forceExec( "SFMobileSyncReactBridge", "MobileSyncReactBridge", @@ -65,53 +91,19 @@ const exec = ( ); }; -type SyncDownOverload = { - ( - storeConfig: StoreConfig, - target: SyncDownTarget, - soupName: string, - options: SyncOptions, - syncName: string, - successCB: ExecSuccessCallback, - errorCB: ExecErrorCallback, - ): void; - ( - storeConfig: StoreConfig, - target: SyncDownTarget, - soupName: string, - options: SyncOptions, - successCB: ExecSuccessCallback, - errorCB: ExecErrorCallback, - ): void; -}; - export const syncDown: SyncDownOverload = ( storeConfig: StoreConfig, target: SyncDownTarget, soupName: string, options: SyncOptions, - x: string | ExecSuccessCallback, - y: ExecSuccessCallback | ExecErrorCallback, - z?: ExecErrorCallback, -): void => { + x?: any, + y?: any, + z?: any, +): any => { storeConfig = checkFirstArg(storeConfig); + const { syncName, successCB, errorCB } = processOpts(x, y, z); - let syncName: string | undefined; - let successCB: ExecSuccessCallback; - let errorCB: ExecErrorCallback; - - // syncName optional (new in 6.0) - if (typeof x === "function") { - syncName = undefined; - successCB = x; - errorCB = y as ExecErrorCallback; - } else { - syncName = x; - successCB = y as ExecSuccessCallback; - errorCB = z as ExecErrorCallback; - } - - exec(successCB, errorCB, "syncDown", { + return exec(successCB, errorCB, "syncDown", { target: target, soupName: soupName, options: options, @@ -121,14 +113,15 @@ export const syncDown: SyncDownOverload = ( }); }; -export const reSync = ( +export const reSync: ReSyncOverload = ( storeConfig: StoreConfig, syncIdOrName: string, - successCB: ExecSuccessCallback, - errorCB: ExecErrorCallback, -): void => { + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): any => { storeConfig = checkFirstArg(storeConfig); - exec(successCB, errorCB, "reSync", { + + return exec(successCB, errorCB, "reSync", { syncId: typeof syncIdOrName === "string" ? null : syncIdOrName, syncName: typeof syncIdOrName === "string" ? syncIdOrName : null, isGlobalStore: storeConfig.isGlobalStore, @@ -136,67 +129,34 @@ export const reSync = ( }); }; -export const cleanResyncGhosts = ( +export const cleanResyncGhosts: CleanResyncGhostsOverload = ( storeConfig: StoreConfig, syncId: string, - successCB: ExecSuccessCallback, - errorCB: ExecErrorCallback, -): void => { + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): any => { storeConfig = checkFirstArg(storeConfig); - exec(successCB, errorCB, "cleanResyncGhosts", { + + return exec(successCB, errorCB, "cleanResyncGhosts", { syncId: syncId, isGlobalStore: storeConfig.isGlobalStore, storeName: storeConfig.storeName, }); }; -type SyncUpOverload = { - ( - storeConfig: StoreConfig, - target: SyncDownTarget, - soupName: string, - options: SyncOptions, - syncName: string, - successCB: ExecSuccessCallback, - errorCB: ExecErrorCallback, - ): void; - ( - storeConfig: StoreConfig, - target: SyncDownTarget, - soupName: string, - options: SyncOptions, - successCB: ExecSuccessCallback, - errorCB: ExecErrorCallback, - ): void; -}; - export const syncUp: SyncUpOverload = ( storeConfig: StoreConfig, target: SyncDownTarget, soupName: string, options: SyncOptions, - x: string | ExecSuccessCallback, - y: ExecSuccessCallback | ExecErrorCallback, - z?: ExecErrorCallback, -): void => { + x?: any, + y?: any, + z?: any, +): any => { storeConfig = checkFirstArg(storeConfig); + const { syncName, successCB, errorCB } = processOpts(x, y, z); - let syncName: string | undefined; - let successCB: ExecSuccessCallback; - let errorCB: ExecErrorCallback; - - // syncName optional (new in 6.0) - if (typeof x === "function") { - syncName = undefined; - successCB = x; - errorCB = y as ExecErrorCallback; - } else { - syncName = x; - successCB = y as ExecSuccessCallback; - errorCB = z as ExecErrorCallback; - } - - exec(successCB, errorCB, "syncUp", { + return exec(successCB, errorCB, "syncUp", { target: target, soupName: soupName, options: options, @@ -206,14 +166,15 @@ export const syncUp: SyncUpOverload = ( }); }; -export const getSyncStatus = ( +export const getSyncStatus: GetSyncStatusOverload = ( storeConfig: StoreConfig, syncIdOrName: string, - successCB: ExecSuccessCallback, - errorCB: ExecErrorCallback, -): void => { + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): any => { storeConfig = checkFirstArg(storeConfig); - exec(successCB, errorCB, "getSyncStatus", { + + return exec(successCB, errorCB, "getSyncStatus", { syncId: typeof syncIdOrName === "string" ? null : syncIdOrName, syncName: typeof syncIdOrName === "string" ? syncIdOrName : null, isGlobalStore: storeConfig.isGlobalStore, @@ -224,11 +185,12 @@ export const getSyncStatus = ( export const deleteSync = ( storeConfig: StoreConfig, syncIdOrName: string, - successCB: ExecSuccessCallback, - errorCB: ExecErrorCallback, -): void => { + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): Promise | void => { storeConfig = checkFirstArg(storeConfig); - exec(successCB, errorCB, "deleteSync", { + + return exec(successCB, errorCB, "deleteSync", { syncId: typeof syncIdOrName === "string" ? null : syncIdOrName, syncName: typeof syncIdOrName === "string" ? syncIdOrName : null, isGlobalStore: storeConfig.isGlobalStore, @@ -240,3 +202,24 @@ export const MERGE_MODE = { OVERWRITE: "OVERWRITE", LEAVE_IF_CHANGED: "LEAVE_IF_CHANGED", }; + +const processOpts = (x: any, y: any, z: any) => { + let syncName: string | undefined; + let successCB: ExecSuccessCallback | undefined; + let errorCB: ExecErrorCallback | undefined; + + // syncName optional (new in 6.0) + if (typeof x === "string") { + syncName = x; + } + + if (typeof x === "function") { + successCB = x as ExecSuccessCallback; + errorCB = y as ExecErrorCallback; + } else if (typeof z === "function") { + successCB = y as ExecSuccessCallback; + errorCB = z as ExecErrorCallback; + } + + return { syncName, successCB, errorCB }; +}; diff --git a/src/typings/mobilesync.d.ts b/src/typings/mobilesync.d.ts index 69c13a7..960fe38 100644 --- a/src/typings/mobilesync.d.ts +++ b/src/typings/mobilesync.d.ts @@ -1,10 +1,7 @@ -export type SyncMethod = - | "cleanResyncGhosts" - | "deleteSync" - | "getSyncStatus" - | "reSync" - | "syncDown" - | "syncUp"; +import { ExecErrorCallback, ExecSuccessCallback } from "../react.force.common"; +import { StoreConfig } from "../react.force.smartstore"; + +export type SyncMethod = "cleanResyncGhosts" | "deleteSync" | "getSyncStatus" | "reSync" | "syncDown" | "syncUp"; export interface SyncEvent { soupName: string; @@ -57,3 +54,94 @@ export interface SyncStatus { totalSize: number; type: string; } + +export type SyncDownOverload = { + ( + storeConfig: StoreConfig, + target: SyncDownTarget, + soupName: string, + options: SyncOptions, + syncName: string, + successCB: ExecSuccessCallback, + errorCB: ExecErrorCallback, + ): void; + ( + storeConfig: StoreConfig, + target: SyncDownTarget, + soupName: string, + options: SyncOptions, + syncName: string, + ): Promise; + ( + storeConfig: StoreConfig, + target: SyncDownTarget, + soupName: string, + options: SyncOptions, + successCB: ExecSuccessCallback, + errorCB: ExecErrorCallback, + ): void; + (storeConfig: StoreConfig, target: SyncDownTarget, soupName: string, options: SyncOptions): Promise; +}; + +export type ReSyncOverload = { + (storeConfig: StoreConfig, syncIdOrName: string): Promise; + ( + storeConfig: StoreConfig, + syncIdOrName: string, + successCB: ExecSuccessCallback, + errorCB: ExecErrorCallback, + ): void; +}; + +export type CleanResyncGhostsOverload = { + (storeConfig: StoreConfig, syncId: string): Promise; + (storeConfig: StoreConfig, syncId: string, successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void; +}; + +export type SyncUpOverload = { + ( + storeConfig: StoreConfig, + target: SyncDownTarget, + soupName: string, + options: SyncOptions, + syncName: string, + successCB: ExecSuccessCallback, + errorCB: ExecErrorCallback, + ): void; + ( + storeConfig: StoreConfig, + target: SyncDownTarget, + soupName: string, + options: SyncOptions, + syncName: string, + ): Promise; + ( + storeConfig: StoreConfig, + target: SyncDownTarget, + soupName: string, + options: SyncOptions, + successCB: ExecSuccessCallback, + errorCB: ExecErrorCallback, + ): void; + (storeConfig: StoreConfig, target: SyncDownTarget, soupName: string, options: SyncOptions): Promise; +}; + +export type GetSyncStatusOverload = { + (storeConfig: StoreConfig, syncIdOrName: string): Promise; + ( + storeConfig: StoreConfig, + syncIdOrName: string, + successCB: ExecSuccessCallback, + errorCB: ExecErrorCallback, + ): void; +}; + +export type DeleteSyncOverload = { + (storeConfig: StoreConfig, syncIdOrName: string): Promise; + ( + storeConfig: StoreConfig, + syncIdOrName: string, + successCB: ExecSuccessCallback, + errorCB: ExecErrorCallback, + ): void; +}; From 5038ffff76462314f24768dc466c13d7132f8974 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Monette Date: Wed, 17 Feb 2021 12:16:00 +0000 Subject: [PATCH 2/9] Add promise overload to oauth --- src/react.force.oauth.ts | 84 +++++++++++++++++++++------------------- src/typings/oauth.d.ts | 22 +++++++++-- 2 files changed, 63 insertions(+), 43 deletions(-) diff --git a/src/react.force.oauth.ts b/src/react.force.oauth.ts index 6e2fb11..8377f97 100644 --- a/src/react.force.oauth.ts +++ b/src/react.force.oauth.ts @@ -26,15 +26,36 @@ import { NativeModules } from "react-native"; import { exec as forceExec, ExecSuccessCallback, ExecErrorCallback } from "./react.force.common"; -import { OAuthMethod, UserAccount } from "./typings/oauth"; +import { + AuthenticateOverload, + GetAuthCredentialsOverload, + LogoutOverload, + OAuthMethod, + UserAccount, +} from "./typings/oauth"; const { SalesforceOauthReactBridge, SFOauthReactBridge } = NativeModules; const exec = ( - successCB: ExecSuccessCallback, - errorCB: ExecErrorCallback, + successCB: ExecSuccessCallback | undefined, + errorCB: ExecErrorCallback | undefined, methodName: OAuthMethod, args: Record, -): void => { +): Promise | void => { + if (!successCB || !errorCB) { + return new Promise((resolve, reject) => { + forceExec( + "SFOauthReactBridge", + "SalesforceOauthReactBridge", + SFOauthReactBridge, + SalesforceOauthReactBridge, + resolve, + reject, + methodName, + args, + ); + }); + } + forceExec( "SFOauthReactBridge", "SalesforceOauthReactBridge", @@ -49,49 +70,34 @@ const exec = ( /** * Initiates the authentication process, with the given app configuration. - * success - The success callback function to use. - * fail - The failure/error callback function to use. - * Returns a dictionary with: - * accessToken - * refreshToken - * clientId - * userId - * orgId - * loginUrl - * instanceUrl - * userAgent - * community id - * community url + * @param successCB + * @param errorCB */ -export const authenticate = (successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void => { - exec(successCB, errorCB, "authenticate", {}); +export const authenticate: AuthenticateOverload = ( + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): any => { + return exec(successCB, errorCB, "authenticate", {}); }; /** * Obtain authentication credentials. - * success - The success callback function to use. - * fail - The failure/error callback function to use. - * Returns a dictionary with: - * accessToken - * refreshToken - * clientId - * userId - * orgId - * loginUrl - * instanceUrl - * userAgent - * community id - * community url + * @param successCB + * @param errorCB */ -export const getAuthCredentials = (successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void => { - exec(successCB, errorCB, "getAuthCredentials", {}); +export const getAuthCredentials: GetAuthCredentialsOverload = ( + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): any => { + return exec(successCB, errorCB, "getAuthCredentials", {}); }; /** - * Logout the current authenticated user. This removes any current valid session token - * as well as any OAuth refresh token. + * Logout the current authenticated user. This removes any current valid session + * token as well as any OAuth refresh token. + * @param successCB + * @param errorCB */ -export const logout = (success: ExecSuccessCallback, fail: ExecErrorCallback) => { - // @ts-ignore - exec(success, fail, "logoutCurrentUser", {}); +export const logout: LogoutOverload = (successCB?: ExecSuccessCallback, errorCB?: ExecErrorCallback): any => { + return exec(successCB, errorCB, "logoutCurrentUser", {}); }; diff --git a/src/typings/oauth.d.ts b/src/typings/oauth.d.ts index 5126822..5f0ecb4 100644 --- a/src/typings/oauth.d.ts +++ b/src/typings/oauth.d.ts @@ -1,7 +1,6 @@ -export type OAuthMethod = - | "authenticate" - | "getAuthCredentials" - | "logoutCurrentUser"; +import { ExecErrorCallback, ExecSuccessCallback } from "../react.force.common"; + +export type OAuthMethod = "authenticate" | "getAuthCredentials" | "logoutCurrentUser"; export type UserAccount = { accessToken: string; @@ -13,3 +12,18 @@ export type UserAccount = { userAgent: string; userId: string; }; + +export type AuthenticateOverload = { + (): Promise; + (successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void; +}; + +export type GetAuthCredentialsOverload = { + (): Promise; + (successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void; +}; + +export type LogoutOverload = { + (): Promise; + (successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void; +}; From 3cb680221c6e9a21ca0ac68ae1a5f9b34009ab2d Mon Sep 17 00:00:00 2001 From: Jean-Philippe Monette Date: Wed, 17 Feb 2021 14:15:24 +0000 Subject: [PATCH 3/9] Add promise overload to smartStore --- src/react.force.smartstore.ts | 376 ++++++++++++++++++++-------------- src/typings/smartstore.d.ts | 262 +++++++++++++++++++++++ 2 files changed, 489 insertions(+), 149 deletions(-) diff --git a/src/react.force.smartstore.ts b/src/react.force.smartstore.ts index e273c78..0bcade2 100644 --- a/src/react.force.smartstore.ts +++ b/src/react.force.smartstore.ts @@ -27,25 +27,70 @@ import { NativeModules } from "react-native"; import { exec as forceExec, ExecErrorCallback, ExecSuccessCallback, safeJSONparse } from "./react.force.common"; import { QuerySpecType, StoreOrder } from "./typings"; -import { SmartStoreMethod } from "./typings/smartstore"; +import { + AlterSoup, + AlterSoupWithSpec, + ClearSoup, + CloseCursor, + GetAllGlobalStores, + GetAllStores, + GetDatabaseSize, + GetSoupIndexSpecs, + GetSoupSpec, + MoveCursorToNextPage, + MoveCursorToPageIndex, + MoveCursorToPreviousPage, + OK, + QuerySoup, + RegisterSoup, + RegisterSoupWithSpec, + ReIndexSoup, + RemoveAllGlobalStores, + RemoveAllStores, + RemoveFromSoup, + RemoveSoup, + RemoveStore, + RetrieveSoupEntries, + RunSmartQuery, + SmartStoreMethod, + SObject, + SoupExists, + UpsertSoupEntries, + UpsertSoupEntriesWithExternalId, +} from "./typings/smartstore"; const { SmartStoreReactBridge, SFSmartStoreReactBridge } = NativeModules; const exec = ( - successCB: ExecSuccessCallback, - errorCB: ExecErrorCallback, + successCB: ExecSuccessCallback | undefined, + errorCB: ExecErrorCallback | undefined, methodName: SmartStoreMethod, args: any, -) => { - forceExec( - "SFSmartStoreReactBridge", - "SmartStoreReactBridge", - SFSmartStoreReactBridge, - SmartStoreReactBridge, - successCB, - errorCB, - methodName, - args, - ); +): Promise | void => { + if (successCB && errorCB) { + return forceExec( + "SFSmartStoreReactBridge", + "SmartStoreReactBridge", + SFSmartStoreReactBridge, + SmartStoreReactBridge, + successCB, + errorCB, + methodName, + args, + ); + } else { + return new Promise((resolve, reject) => { + forceExec( + "SFSmartStoreReactBridge", + "SmartStoreReactBridge", + SFSmartStoreReactBridge, + SmartStoreReactBridge, + resolve, + reject, + methodName, + args, + ); + }); + } }; /** @@ -66,9 +111,9 @@ export class StoreConfig { */ export class SoupSpec { public name: string; - public features: { [key: string]: any }; + public features: SObject; - constructor(soupName: string, features: { [key: string]: any }) { + constructor(soupName: string, features: SObject) { this.name = soupName; this.features = features; } @@ -312,28 +357,29 @@ const checkFirstArg = (arg: StoreConfig | boolean) => { }; // ====== Soup manipulation ====== -export const getDatabaseSize = ( +export const getDatabaseSize: GetDatabaseSize = ( storeConfig: StoreConfig | boolean, - successCB: ExecSuccessCallback, - errorCB: ExecErrorCallback, -): void => { + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): any => { storeConfig = checkFirstArg(storeConfig); - exec(successCB, errorCB, "getDatabaseSize", { + + return exec(successCB, errorCB, "getDatabaseSize", { isGlobalStore: storeConfig.isGlobalStore, storeName: storeConfig.storeName, }); }; -export const registerSoup = ( +export const registerSoup: RegisterSoup = ( storeConfig: StoreConfig | boolean, soupName: string, indexSpecs: SoupIndexSpec[], - successCB: ExecSuccessCallback, - errorCB: ExecErrorCallback, -): void => { + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): any => { storeConfig = checkFirstArg(storeConfig); - exec(successCB, errorCB, "registerSoup", { + return exec(successCB, errorCB, "registerSoup", { soupName, indexes: indexSpecs, isGlobalStore: storeConfig.isGlobalStore, @@ -341,16 +387,16 @@ export const registerSoup = ( }); }; -export const registerSoupWithSpec = ( +export const registerSoupWithSpec: RegisterSoupWithSpec = ( storeConfig: StoreConfig | boolean, soupSpec: QuerySpec, indexSpecs: SoupIndexSpec[], - successCB: ExecSuccessCallback, - errorCB: ExecErrorCallback, -): void => { + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): any => { storeConfig = checkFirstArg(storeConfig); - exec(successCB, errorCB, "registerSoup", { + return exec(successCB, errorCB, "registerSoup", { soupSpec, indexes: indexSpecs, isGlobalStore: storeConfig.isGlobalStore, @@ -358,62 +404,62 @@ export const registerSoupWithSpec = ( }); }; -export const removeSoup = ( +export const removeSoup: RemoveSoup = ( storeConfig: StoreConfig | boolean, soupName: string, - successCB: ExecSuccessCallback<"OK">, - errorCB: ExecErrorCallback, -): void => { + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): any => { storeConfig = checkFirstArg(storeConfig); - exec(successCB, errorCB, "removeSoup", { + return exec(successCB, errorCB, "removeSoup", { soupName, isGlobalStore: storeConfig.isGlobalStore, storeName: storeConfig.storeName, }); }; -export const getSoupIndexSpecs = ( +export const getSoupIndexSpecs: GetSoupIndexSpecs = ( storeConfig: StoreConfig | boolean, soupName: string, - successCB: ExecSuccessCallback, - errorCB: ExecErrorCallback, -): void => { + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): any => { storeConfig = checkFirstArg(storeConfig); - exec(successCB, errorCB, "getSoupIndexSpecs", { + return exec(successCB, errorCB, "getSoupIndexSpecs", { soupName, isGlobalStore: storeConfig.isGlobalStore, storeName: storeConfig.storeName, }); }; -export const getSoupSpec = ( +export const getSoupSpec: GetSoupSpec = ( storeConfig: StoreConfig | boolean, soupName: string, - successCB: ExecSuccessCallback, - errorCB: ExecErrorCallback, -): void => { + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): any => { storeConfig = checkFirstArg(storeConfig); - exec(successCB, errorCB, "getSoupSpec", { + return exec(successCB, errorCB, "getSoupSpec", { soupName, isGlobalStore: storeConfig.isGlobalStore, storeName: storeConfig.storeName, }); }; -export const alterSoup = ( +export const alterSoup: AlterSoup = ( storeConfig: StoreConfig | boolean, soupName: string, indexSpecs: SoupIndexSpec[], reIndexData: boolean, - successCB: ExecSuccessCallback, - errorCB: ExecErrorCallback, -): void => { + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): any => { storeConfig = checkFirstArg(storeConfig); - exec(successCB, errorCB, "alterSoup", { + return exec(successCB, errorCB, "alterSoup", { soupName, indexes: indexSpecs, reIndexData, @@ -422,18 +468,18 @@ export const alterSoup = ( }); }; -export const alterSoupWithSpec = ( +export const alterSoupWithSpec: AlterSoupWithSpec = ( storeConfig: StoreConfig | boolean, soupName: string, soupSpec: SoupSpec, indexSpecs: SoupIndexSpec[], reIndexData: boolean, - successCB: ExecSuccessCallback, - errorCB: ExecErrorCallback, -): void => { + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): any => { storeConfig = checkFirstArg(storeConfig); - exec(successCB, errorCB, "alterSoup", { + return exec(successCB, errorCB, "alterSoup", { soupName, soupSpec, indexes: indexSpecs, @@ -443,16 +489,16 @@ export const alterSoupWithSpec = ( }); }; -export const reIndexSoup = ( +export const reIndexSoup: ReIndexSoup = ( storeConfig: StoreConfig | boolean, soupName: string, paths: string, - successCB: ExecSuccessCallback, - errorCB: ExecErrorCallback, -): void => { + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): any => { storeConfig = checkFirstArg(storeConfig); - exec(successCB, errorCB, "reIndexSoup", { + return exec(successCB, errorCB, "reIndexSoup", { soupName, paths, isGlobalStore: storeConfig.isGlobalStore, @@ -460,56 +506,60 @@ export const reIndexSoup = ( }); }; -export const clearSoup = ( +export const clearSoup: ClearSoup = ( storeConfig: StoreConfig | boolean, soupName: string, - successCB: ExecSuccessCallback, - errorCB: ExecErrorCallback, -): void => { + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): any => { storeConfig = checkFirstArg(storeConfig); - exec(successCB, errorCB, "clearSoup", { + return exec(successCB, errorCB, "clearSoup", { soupName, isGlobalStore: storeConfig.isGlobalStore, storeName: storeConfig.storeName, }); }; -export const soupExists = ( +export const soupExists: SoupExists = ( storeConfig: StoreConfig | boolean, soupName: string, - successCB: ExecSuccessCallback, - errorCB: ExecErrorCallback, -): void => { + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): any => { storeConfig = checkFirstArg(storeConfig); - exec(successCB, errorCB, "soupExists", { + return exec(successCB, errorCB, "soupExists", { soupName, isGlobalStore: storeConfig.isGlobalStore, storeName: storeConfig.storeName, }); }; -export const querySoup = ( +export const querySoup: QuerySoup = ( storeConfig: StoreConfig | boolean, soupName: string, querySpec: QuerySpec, - successCB: ExecSuccessCallback>, - errorCB: ExecErrorCallback, -): void => { + successCB?: ExecSuccessCallback>, + errorCB?: ExecErrorCallback, +): any => { storeConfig = checkFirstArg(storeConfig); if (querySpec.queryType === "smart") { throw new Error("Smart queries can only be run using runSmartQuery"); } + if (querySpec.order != null && querySpec.orderPath == null) { querySpec.orderPath = querySpec.indexPath; - } // for backward compatibility with pre-3.3 code + } + + // for backward compatibility with pre-3.3 code // query returns serialized json on iOS starting in 7.0 const successCBdeserializing = successCB ? (result: StoreCursor) => successCB(typeof result === "string" ? safeJSONparse(result) : result) : successCB; - exec(successCBdeserializing, errorCB, "querySoup", { + + return exec(successCBdeserializing, errorCB, "querySoup", { soupName, querySpec, isGlobalStore: storeConfig.isGlobalStore, @@ -517,38 +567,40 @@ export const querySoup = ( }); }; -export const runSmartQuery = ( +export const runSmartQuery: RunSmartQuery = ( storeConfig: StoreConfig | boolean, querySpec: QuerySpec, - successCB: ExecSuccessCallback>, - errorCB: ExecErrorCallback, -): void => { + successCB?: ExecSuccessCallback>, + errorCB?: ExecErrorCallback, +): any => { storeConfig = checkFirstArg(storeConfig); if (querySpec.queryType !== "smart") { throw new Error("runSmartQuery can only run smart queries"); } + // query returns serialized json on iOS starting in 7.0 const successCBdeserializing = successCB ? (result: StoreCursor) => successCB(typeof result === "string" ? safeJSONparse(result) : result) : successCB; - exec(successCBdeserializing, errorCB, "runSmartQuery", { + + return exec(successCBdeserializing, errorCB, "runSmartQuery", { querySpec, isGlobalStore: storeConfig.isGlobalStore, storeName: storeConfig.storeName, }); }; -export const retrieveSoupEntries = ( +export const retrieveSoupEntries: RetrieveSoupEntries = ( storeConfig: StoreConfig | boolean, soupName: string, entryIds: string[], - successCB: ExecSuccessCallback>, - errorCB: ExecErrorCallback, -): void => { + successCB?: ExecSuccessCallback>, + errorCB?: ExecErrorCallback, +): any => { storeConfig = checkFirstArg(storeConfig); - exec(successCB, errorCB, "retrieveSoupEntries", { + return exec(successCB, errorCB, "retrieveSoupEntries", { soupName, entryIds, isGlobalStore: storeConfig.isGlobalStore, @@ -556,29 +608,33 @@ export const retrieveSoupEntries = ( }); }; -export const upsertSoupEntries = ( +export const upsertSoupEntries: UpsertSoupEntries = ( storeConfig: StoreConfig | boolean, soupName: string, - entries: { [key: string]: any }[], - successCB: ExecSuccessCallback, - errorCB: ExecErrorCallback, -): void => { + entries: SObject[], + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): any => { storeConfig = checkFirstArg(storeConfig); - upsertSoupEntriesWithExternalId(storeConfig, soupName, entries, "_soupEntryId", successCB, errorCB); + if (successCB && errorCB) { + return upsertSoupEntriesWithExternalId(storeConfig, soupName, entries, "_soupEntryId", successCB, errorCB); + } else { + return upsertSoupEntriesWithExternalId(storeConfig, soupName, entries, "_soupEntryId"); + } }; -export let upsertSoupEntriesWithExternalId = ( +export const upsertSoupEntriesWithExternalId: UpsertSoupEntriesWithExternalId = ( storeConfig: StoreConfig | boolean, soupName: string, - entries: { [key: string]: any }[], + entries: SObject[], externalIdPath: string, - successCB: ExecSuccessCallback, - errorCB: ExecErrorCallback, -): void => { + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): any => { storeConfig = checkFirstArg(storeConfig); - exec(successCB, errorCB, "upsertSoupEntries", { + return exec(successCB, errorCB, "upsertSoupEntries", { soupName, entries, externalIdPath, @@ -587,13 +643,13 @@ export let upsertSoupEntriesWithExternalId = ( }); }; -export const removeFromSoup = ( +export const removeFromSoup: RemoveFromSoup = ( storeConfig: StoreConfig | boolean, soupName: string, entryIdsOrQuerySpec: string[], - successCB: ExecSuccessCallback<"OK">, - errorCB: ExecErrorCallback, -): void => { + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): any => { storeConfig = checkFirstArg(storeConfig); const execArgs = { @@ -601,19 +657,21 @@ export const removeFromSoup = ( isGlobalStore: storeConfig.isGlobalStore, storeName: storeConfig.storeName, }; + execArgs[entryIdsOrQuerySpec instanceof Array ? "entryIds" : "querySpec"] = entryIdsOrQuerySpec; execArgs[entryIdsOrQuerySpec instanceof Array ? "querySpec" : "entryIds"] = null; - exec(successCB, errorCB, "removeFromSoup", execArgs); + + return exec(successCB, errorCB, "removeFromSoup", execArgs); }; // ====== Cursor manipulation ====== -export const moveCursorToPageIndex = ( +export const moveCursorToPageIndex: MoveCursorToPageIndex = ( storeConfig: StoreConfig | boolean, cursor: StoreCursor, newPageIndex: number, - successCB: ExecSuccessCallback, - errorCB: ExecErrorCallback, -): void => { + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): any => { storeConfig = checkFirstArg(storeConfig); // query returns serialized json on iOS starting in 7.0 @@ -625,7 +683,7 @@ export const moveCursorToPageIndex = ( successCBdeserializing = successCB; } - exec(successCBdeserializing, errorCB, "moveCursorToPageIndex", { + return exec(successCBdeserializing, errorCB, "moveCursorToPageIndex", { cursorId: cursor.cursorId, index: newPageIndex, isGlobalStore: storeConfig.isGlobalStore, @@ -633,53 +691,69 @@ export const moveCursorToPageIndex = ( }); }; -export const moveCursorToNextPage = ( +export const moveCursorToNextPage: MoveCursorToNextPage = ( storeConfig: StoreConfig | boolean, cursor: StoreCursor, - successCB: ExecSuccessCallback, - errorCB: ExecErrorCallback, -): void => { + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): any => { storeConfig = checkFirstArg(storeConfig); const newPageIndex = cursor.currentPageIndex + 1; + if (newPageIndex >= cursor.totalPages) { - errorCB( - // cursor, - new Error("moveCursorToNextPage called while on last page"), - ); + const err = new Error("moveCursorToNextPage called while on last page"); + + if (errorCB) { + errorCB(err); + } else { + throw err; + } } else { - moveCursorToPageIndex(storeConfig, cursor, newPageIndex, successCB, errorCB); + if (successCB && errorCB) { + moveCursorToPageIndex(storeConfig, cursor, newPageIndex, successCB, errorCB); + } else { + return moveCursorToPageIndex(storeConfig, cursor, newPageIndex); + } } }; -export const moveCursorToPreviousPage = ( +export const moveCursorToPreviousPage: MoveCursorToPreviousPage = ( storeConfig: StoreConfig | boolean, cursor: StoreCursor, - successCB: ExecSuccessCallback, - errorCB: ExecErrorCallback, -): void => { + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): any => { storeConfig = checkFirstArg(storeConfig); const newPageIndex = cursor.currentPageIndex - 1; + if (newPageIndex < 0) { - errorCB( - // cursor, - new Error("moveCursorToPreviousPage called while on first page"), - ); + const err = new Error("moveCursorToPreviousPage called while on last page"); + + if (errorCB) { + errorCB(err); + } else { + throw err; + } } else { - moveCursorToPageIndex(storeConfig, cursor, newPageIndex, successCB, errorCB); + if (successCB && errorCB) { + return moveCursorToPageIndex(storeConfig, cursor, newPageIndex, successCB, errorCB); + } else { + return moveCursorToPageIndex(storeConfig, cursor, newPageIndex); + } } }; -export const closeCursor = ( +export const closeCursor: CloseCursor = ( storeConfig: StoreConfig | boolean, cursor: StoreCursor, - successCB: ExecSuccessCallback<"OK">, - errorCB: ExecErrorCallback, -): void => { + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): any => { storeConfig = checkFirstArg(storeConfig); - exec(successCB, errorCB, "closeCursor", { + return exec(successCB, errorCB, "closeCursor", { cursorId: cursor.cursorId, isGlobalStore: storeConfig.isGlobalStore, storeName: storeConfig.storeName, @@ -687,31 +761,35 @@ export const closeCursor = ( }; // ====== Store Operations ====== -export const getAllStores = (successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void => { - exec(successCB, errorCB, "getAllStores", {}); -}; +export const getAllStores: GetAllStores = ( + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): any => exec(successCB, errorCB, "getAllStores", {}); -export const getAllGlobalStores = (successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void => { - exec(successCB, errorCB, "getAllGlobalStores", {}); -}; +export const getAllGlobalStores: GetAllGlobalStores = ( + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): any => exec(successCB, errorCB, "getAllGlobalStores", {}); -export const removeStore = ( +export const removeStore: RemoveStore = ( storeConfig: StoreConfig | boolean, - successCB: ExecSuccessCallback<"OK">, - errorCB: ExecErrorCallback, -): void => { + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): any => { storeConfig = checkFirstArg(storeConfig); - exec(successCB, errorCB, "removeStore", { + return exec(successCB, errorCB, "removeStore", { isGlobalStore: storeConfig.isGlobalStore, storeName: storeConfig.storeName, }); }; -export const removeAllGlobalStores = (successCB: ExecSuccessCallback<"OK">, errorCB: ExecErrorCallback): void => { - exec(successCB, errorCB, "removeAllGlobalStores", {}); -}; +export const removeAllGlobalStores: RemoveAllGlobalStores = ( + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): any => exec(successCB, errorCB, "removeAllGlobalStores", {}); -export const removeAllStores = (successCB: ExecSuccessCallback<"OK">, errorCB: ExecErrorCallback): void => { - exec(successCB, errorCB, "removeAllStores", {}); -}; +export const removeAllStores: RemoveAllStores = ( + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): any => exec(successCB, errorCB, "removeAllStores", {}); diff --git a/src/typings/smartstore.d.ts b/src/typings/smartstore.d.ts index cb080ba..07fcace 100644 --- a/src/typings/smartstore.d.ts +++ b/src/typings/smartstore.d.ts @@ -1,3 +1,6 @@ +import { ExecErrorCallback, ExecSuccessCallback } from "../react.force.common"; +import { QuerySpec, SoupIndexSpec, SoupSpec, StoreConfig, StoreCursor } from "../react.force.smartstore"; + export type SmartStoreMethod = | "alterSoup" | "clearSoup" @@ -20,3 +23,262 @@ export type SmartStoreMethod = | "runSmartQuery" | "soupExists" | "upsertSoupEntries"; + +export type OK = "OK"; + +export type SObject = Record; + +export type GetDatabaseSize = { + (storeConfig: StoreConfig | boolean): Promise; + (storeConfig: StoreConfig | boolean, successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void; +}; + +export type RegisterSoup = { + (storeConfig: StoreConfig | boolean, soupName: string, indexSpecs: SoupIndexSpec[]): Promise; + ( + storeConfig: StoreConfig | boolean, + soupName: string, + indexSpecs: SoupIndexSpec[], + successCB: ExecSuccessCallback, + errorCB: ExecErrorCallback, + ): void; +}; + +export type RegisterSoupWithSpec = { + (storeConfig: StoreConfig | boolean, soupSpec: QuerySpec, indexSpecs: SoupIndexSpec[]): Promise; + ( + storeConfig: StoreConfig | boolean, + soupSpec: QuerySpec, + indexSpecs: SoupIndexSpec[], + successCB: ExecSuccessCallback, + errorCB: ExecErrorCallback, + ): void; +}; + +export type RemoveSoup = { + (storeConfig: StoreConfig | boolean, soupName: string): Promise; + ( + storeConfig: StoreConfig | boolean, + soupName: string, + successCB: ExecSuccessCallback, + errorCB: ExecErrorCallback, + ): void; +}; + +export type GetSoupIndexSpecs = { + (storeConfig: StoreConfig | boolean, soupName: string): Promise; + ( + storeConfig: StoreConfig | boolean, + soupName: string, + successCB: ExecSuccessCallback, + errorCB: ExecErrorCallback, + ): void; +}; + +export type GetSoupSpec = { + (storeConfig: StoreConfig | boolean, soupName: string): Promise; + ( + storeConfig: StoreConfig | boolean, + soupName: string, + successCB: ExecSuccessCallback, + errorCB: ExecErrorCallback, + ): void; +}; + +export type AlterSoup = { + ( + storeConfig: StoreConfig | boolean, + soupName: string, + indexSpecs: SoupIndexSpec[], + reIndexData: boolean, + ): Promise; + ( + storeConfig: StoreConfig | boolean, + soupName: string, + indexSpecs: SoupIndexSpec[], + reIndexData: boolean, + successCB: ExecSuccessCallback, + errorCB: ExecErrorCallback, + ): void; +}; + +export type AlterSoupWithSpec = { + ( + storeConfig: StoreConfig | boolean, + soupName: string, + soupSpec: SoupSpec, + indexSpecs: SoupIndexSpec[], + reIndexData: boolean, + ): Promise; + ( + storeConfig: StoreConfig | boolean, + soupName: string, + soupSpec: SoupSpec, + indexSpecs: SoupIndexSpec[], + reIndexData: boolean, + successCB: ExecSuccessCallback, + errorCB: ExecErrorCallback, + ): void; +}; + +export type ReIndexSoup = { + (storeConfig: StoreConfig | boolean, soupName: string, paths: string): Promise; + ( + storeConfig: StoreConfig | boolean, + soupName: string, + paths: string, + successCB: ExecSuccessCallback, + errorCB: ExecErrorCallback, + ): void; +}; + +export type ClearSoup = { + (storeConfig: StoreConfig | boolean, soupName: string): Promise; + ( + storeConfig: StoreConfig | boolean, + soupName: string, + successCB: ExecSuccessCallback, + errorCB: ExecErrorCallback, + ): void; +}; + +export type SoupExists = { + (storeConfig: StoreConfig | boolean, soupName: string): Promise; + ( + storeConfig: StoreConfig | boolean, + soupName: string, + successCB: ExecSuccessCallback, + errorCB: ExecErrorCallback, + ): void; +}; + +export type QuerySoup = { + (storeConfig: StoreConfig | boolean, soupName: string, querySpec: QuerySpec): Promise>; + ( + storeConfig: StoreConfig | boolean, + soupName: string, + querySpec: QuerySpec, + successCB: ExecSuccessCallback>, + errorCB: ExecErrorCallback, + ): void; +}; + +export type RunSmartQuery = { + (storeConfig: StoreConfig | boolean, querySpec: QuerySpec): Promise>; + ( + storeConfig: StoreConfig | boolean, + querySpec: QuerySpec, + successCB: ExecSuccessCallback>, + errorCB: ExecErrorCallback, + ): void; +}; +export type RetrieveSoupEntries = { + (storeConfig: StoreConfig | boolean, soupName: string, entryIds: string[]): Promise>; + ( + storeConfig: StoreConfig | boolean, + soupName: string, + entryIds: string[], + successCB: ExecSuccessCallback>, + errorCB: ExecErrorCallback, + ): void; +}; + +export type UpsertSoupEntries = { + (storeConfig: StoreConfig | boolean, soupName: string, entries: SObject[]): Promise; + ( + storeConfig: StoreConfig | boolean, + soupName: string, + entries: SObject[], + successCB: ExecSuccessCallback, + errorCB: ExecErrorCallback, + ): void; +}; + +export type UpsertSoupEntriesWithExternalId = { + (storeConfig: StoreConfig | boolean, soupName: string, entries: SObject[], externalIdPath: string): Promise; + ( + storeConfig: StoreConfig | boolean, + soupName: string, + entries: SObject[], + externalIdPath: string, + successCB: ExecSuccessCallback, + errorCB: ExecErrorCallback, + ): void; +}; + +export type RemoveFromSoup = { + (storeConfig: StoreConfig | boolean, soupName: string, entryIdsOrQuerySpec: string[]): Promise; + ( + storeConfig: StoreConfig | boolean, + soupName: string, + entryIdsOrQuerySpec: string[], + successCB: ExecSuccessCallback, + errorCB: ExecErrorCallback, + ): void; +}; + +export type MoveCursorToPageIndex = { + (storeConfig: StoreConfig | boolean, cursor: StoreCursor, newPageIndex: number): Promise; + ( + storeConfig: StoreConfig | boolean, + cursor: StoreCursor, + newPageIndex: number, + successCB: ExecSuccessCallback, + errorCB: ExecErrorCallback, + ): void; +}; + +export type MoveCursorToNextPage = { + (storeConfig: StoreConfig | boolean, cursor: StoreCursor): Promise; + ( + storeConfig: StoreConfig | boolean, + cursor: StoreCursor, + successCB: ExecSuccessCallback, + errorCB: ExecErrorCallback, + ): void; +}; + +export type MoveCursorToPreviousPage = { + (storeConfig: StoreConfig | boolean, cursor: StoreCursor): Promise; + ( + storeConfig: StoreConfig | boolean, + cursor: StoreCursor, + successCB: ExecSuccessCallback, + errorCB: ExecErrorCallback, + ): void; +}; + +export type CloseCursor = { + (storeConfig: StoreConfig | boolean, cursor: StoreCursor): Promise; + ( + storeConfig: StoreConfig | boolean, + cursor: StoreCursor, + successCB: ExecSuccessCallback, + errorCB: ExecErrorCallback, + ): void; +}; + +export type GetAllStores = { + (): Promise; + (successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void; +}; + +export type GetAllGlobalStores = { + (): Promise; + (successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void; +}; + +export type RemoveStore = { + (storeConfig: StoreConfig | boolean): Promise; + (storeConfig: StoreConfig | boolean, successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void; +}; + +export type RemoveAllStores = { + (): Promise; + (successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void; +}; + +export type RemoveAllGlobalStores = { + (): Promise; + (successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void; +}; From fcc153b5964fbb1d036b616cb1ec1d3bb0d31dff Mon Sep 17 00:00:00 2001 From: Jean-Philippe Monette Date: Wed, 17 Feb 2021 14:15:41 +0000 Subject: [PATCH 4/9] Rename oauth overload types --- src/react.force.oauth.ts | 40 +++++++++++++++++----------------------- src/typings/oauth.d.ts | 6 +++--- 2 files changed, 20 insertions(+), 26 deletions(-) diff --git a/src/react.force.oauth.ts b/src/react.force.oauth.ts index 8377f97..5f2e081 100644 --- a/src/react.force.oauth.ts +++ b/src/react.force.oauth.ts @@ -25,14 +25,8 @@ */ import { NativeModules } from "react-native"; -import { exec as forceExec, ExecSuccessCallback, ExecErrorCallback } from "./react.force.common"; -import { - AuthenticateOverload, - GetAuthCredentialsOverload, - LogoutOverload, - OAuthMethod, - UserAccount, -} from "./typings/oauth"; +import { exec as forceExec, ExecErrorCallback, ExecSuccessCallback } from "./react.force.common"; +import { Authenticate, GetAuthCredentials, Logout, OAuthMethod, UserAccount } from "./typings/oauth"; const { SalesforceOauthReactBridge, SFOauthReactBridge } = NativeModules; const exec = ( @@ -41,7 +35,18 @@ const exec = ( methodName: OAuthMethod, args: Record, ): Promise | void => { - if (!successCB || !errorCB) { + if (successCB && errorCB) { + forceExec( + "SFOauthReactBridge", + "SalesforceOauthReactBridge", + SFOauthReactBridge, + SalesforceOauthReactBridge, + successCB, + errorCB, + methodName, + args, + ); + } else { return new Promise((resolve, reject) => { forceExec( "SFOauthReactBridge", @@ -55,17 +60,6 @@ const exec = ( ); }); } - - forceExec( - "SFOauthReactBridge", - "SalesforceOauthReactBridge", - SFOauthReactBridge, - SalesforceOauthReactBridge, - successCB, - errorCB, - methodName, - args, - ); }; /** @@ -73,7 +67,7 @@ const exec = ( * @param successCB * @param errorCB */ -export const authenticate: AuthenticateOverload = ( +export const authenticate: Authenticate = ( successCB?: ExecSuccessCallback, errorCB?: ExecErrorCallback, ): any => { @@ -85,7 +79,7 @@ export const authenticate: AuthenticateOverload = ( * @param successCB * @param errorCB */ -export const getAuthCredentials: GetAuthCredentialsOverload = ( +export const getAuthCredentials: GetAuthCredentials = ( successCB?: ExecSuccessCallback, errorCB?: ExecErrorCallback, ): any => { @@ -98,6 +92,6 @@ export const getAuthCredentials: GetAuthCredentialsOverload = ( * @param successCB * @param errorCB */ -export const logout: LogoutOverload = (successCB?: ExecSuccessCallback, errorCB?: ExecErrorCallback): any => { +export const logout: Logout = (successCB?: ExecSuccessCallback, errorCB?: ExecErrorCallback): any => { return exec(successCB, errorCB, "logoutCurrentUser", {}); }; diff --git a/src/typings/oauth.d.ts b/src/typings/oauth.d.ts index 5f0ecb4..ede6bc6 100644 --- a/src/typings/oauth.d.ts +++ b/src/typings/oauth.d.ts @@ -13,17 +13,17 @@ export type UserAccount = { userId: string; }; -export type AuthenticateOverload = { +export type Authenticate = { (): Promise; (successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void; }; -export type GetAuthCredentialsOverload = { +export type GetAuthCredentials = { (): Promise; (successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void; }; -export type LogoutOverload = { +export type Logout = { (): Promise; (successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void; }; From 3ee0e4661a3fecc02939b16af8fbd546a6d5952a Mon Sep 17 00:00:00 2001 From: Jean-Philippe Monette Date: Wed, 17 Feb 2021 14:16:11 +0000 Subject: [PATCH 5/9] Rename mobileSync overload types --- src/react.force.mobilesync.ts | 49 ++++++++++++++++++----------------- src/typings/mobilesync.d.ts | 12 ++++----- 2 files changed, 31 insertions(+), 30 deletions(-) diff --git a/src/react.force.mobilesync.ts b/src/react.force.mobilesync.ts index 032b81b..ff35eae 100644 --- a/src/react.force.mobilesync.ts +++ b/src/react.force.mobilesync.ts @@ -27,16 +27,17 @@ import { NativeModules } from "react-native"; import { exec as forceExec, ExecErrorCallback, ExecSuccessCallback } from "./react.force.common"; import { StoreConfig } from "./react.force.smartstore"; import { - CleanResyncGhostsOverload, - GetSyncStatusOverload, - ReSyncOverload, - SyncDownOverload, + CleanResyncGhosts, + DeleteSync, + GetSyncStatus, + ReSync, + SyncDown, SyncDownTarget, SyncEvent, SyncMethod, SyncOptions, SyncStatus, - SyncUpOverload, + SyncUp, } from "./typings/mobilesync"; const { MobileSyncReactBridge, SFMobileSyncReactBridge } = NativeModules; @@ -64,7 +65,18 @@ const exec = ( methodName: SyncMethod, args: Record, ): Promise | void => { - if (!successCB || !errorCB) { + if (successCB && errorCB) { + forceExec( + "SFMobileSyncReactBridge", + "MobileSyncReactBridge", + SFMobileSyncReactBridge, + MobileSyncReactBridge, + successCB, + errorCB, + methodName, + args, + ); + } else { return new Promise((resolve, reject) => { forceExec( "SFMobileSyncReactBridge", @@ -78,20 +90,9 @@ const exec = ( ); }); } - - forceExec( - "SFMobileSyncReactBridge", - "MobileSyncReactBridge", - SFMobileSyncReactBridge, - MobileSyncReactBridge, - successCB, - errorCB, - methodName, - args, - ); }; -export const syncDown: SyncDownOverload = ( +export const syncDown: SyncDown = ( storeConfig: StoreConfig, target: SyncDownTarget, soupName: string, @@ -113,7 +114,7 @@ export const syncDown: SyncDownOverload = ( }); }; -export const reSync: ReSyncOverload = ( +export const reSync: ReSync = ( storeConfig: StoreConfig, syncIdOrName: string, successCB?: ExecSuccessCallback, @@ -129,7 +130,7 @@ export const reSync: ReSyncOverload = ( }); }; -export const cleanResyncGhosts: CleanResyncGhostsOverload = ( +export const cleanResyncGhosts: CleanResyncGhosts = ( storeConfig: StoreConfig, syncId: string, successCB?: ExecSuccessCallback, @@ -144,7 +145,7 @@ export const cleanResyncGhosts: CleanResyncGhostsOverload = ( }); }; -export const syncUp: SyncUpOverload = ( +export const syncUp: SyncUp = ( storeConfig: StoreConfig, target: SyncDownTarget, soupName: string, @@ -166,7 +167,7 @@ export const syncUp: SyncUpOverload = ( }); }; -export const getSyncStatus: GetSyncStatusOverload = ( +export const getSyncStatus: GetSyncStatus = ( storeConfig: StoreConfig, syncIdOrName: string, successCB?: ExecSuccessCallback, @@ -182,12 +183,12 @@ export const getSyncStatus: GetSyncStatusOverload = ( }); }; -export const deleteSync = ( +export const deleteSync: DeleteSync = ( storeConfig: StoreConfig, syncIdOrName: string, successCB?: ExecSuccessCallback, errorCB?: ExecErrorCallback, -): Promise | void => { +): any => { storeConfig = checkFirstArg(storeConfig); return exec(successCB, errorCB, "deleteSync", { diff --git a/src/typings/mobilesync.d.ts b/src/typings/mobilesync.d.ts index 960fe38..a1d1002 100644 --- a/src/typings/mobilesync.d.ts +++ b/src/typings/mobilesync.d.ts @@ -55,7 +55,7 @@ export interface SyncStatus { type: string; } -export type SyncDownOverload = { +export type SyncDown = { ( storeConfig: StoreConfig, target: SyncDownTarget, @@ -83,7 +83,7 @@ export type SyncDownOverload = { (storeConfig: StoreConfig, target: SyncDownTarget, soupName: string, options: SyncOptions): Promise; }; -export type ReSyncOverload = { +export type ReSync = { (storeConfig: StoreConfig, syncIdOrName: string): Promise; ( storeConfig: StoreConfig, @@ -93,12 +93,12 @@ export type ReSyncOverload = { ): void; }; -export type CleanResyncGhostsOverload = { +export type CleanResyncGhosts = { (storeConfig: StoreConfig, syncId: string): Promise; (storeConfig: StoreConfig, syncId: string, successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void; }; -export type SyncUpOverload = { +export type SyncUp = { ( storeConfig: StoreConfig, target: SyncDownTarget, @@ -126,7 +126,7 @@ export type SyncUpOverload = { (storeConfig: StoreConfig, target: SyncDownTarget, soupName: string, options: SyncOptions): Promise; }; -export type GetSyncStatusOverload = { +export type GetSyncStatus = { (storeConfig: StoreConfig, syncIdOrName: string): Promise; ( storeConfig: StoreConfig, @@ -136,7 +136,7 @@ export type GetSyncStatusOverload = { ): void; }; -export type DeleteSyncOverload = { +export type DeleteSync = { (storeConfig: StoreConfig, syncIdOrName: string): Promise; ( storeConfig: StoreConfig, From 5e975a503d6ac6fe057de8245f46701427332b26 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Monette Date: Wed, 17 Feb 2021 14:35:43 +0000 Subject: [PATCH 6/9] Add promise overload to net --- src/react.force.net.ts | 184 +++++++++++++++++++++++------------------ src/typings/net.d.ts | 130 +++++++++++++++++++++++++++++ 2 files changed, 234 insertions(+), 80 deletions(-) create mode 100644 src/typings/net.d.ts diff --git a/src/react.force.net.ts b/src/react.force.net.ts index 24fb2f7..4e4e23a 100644 --- a/src/react.force.net.ts +++ b/src/react.force.net.ts @@ -28,9 +28,27 @@ import { NativeModules } from "react-native"; import { exec as forceExec, ExecErrorCallback, ExecSuccessCallback } from "./react.force.common"; import { sdkConsole } from "./react.force.log"; import { HttpMethod } from "./typings"; +import { + Create, + Del, + Describe, + DescribeGlobal, + DescribeLayout, + GetAttachment, + Metadata, + Query, + QueryMore, + Resources, + Retrieve, + Search, + SendRequest, + Update, + Upsert, + Versions, +} from "./typings/net"; const { SalesforceNetReactBridge, SFNetReactBridge } = NativeModules; -var apiVersion = 'v49.0'; +var apiVersion = "v49.0"; /** * Set apiVersion to be used @@ -47,44 +65,54 @@ export const getApiVersion = (): string => apiVersion; /** * Send arbitray force.com request */ -export const sendRequest = ( +export const sendRequest: SendRequest = ( endPoint: string, path: string, - successCB: ExecSuccessCallback, - errorCB: ExecErrorCallback, + successCB: any, + errorCB: any, method?: HttpMethod, payload?: Record | null, headerParams?: Record | null, fileParams?: unknown, returnBinary?: boolean, doesNotRequireAuthentication?: boolean, -): void => { - method = method || "GET"; - payload = payload || {}; - headerParams = headerParams || {}; - fileParams = fileParams || {}; // File params expected to be of the form: {: {fileMimeType:, fileUrl:, fileName:}} - returnBinary = !!returnBinary; - doesNotRequireAuthentication = !!doesNotRequireAuthentication; +): any => { const args = { endPoint, path, - method, - queryParams: payload, - headerParams, - fileParams, - returnBinary, - doesNotRequireAuthentication, + method: method || "GET", + queryParams: payload || {}, + headerParams: headerParams || {}, + fileParams: fileParams || {}, // File params expected to be of the form: {: {fileMimeType:, fileUrl:, fileName:}} + returnBinary: !!returnBinary, + doesNotRequireAuthentication: !!doesNotRequireAuthentication, }; - forceExec( - "SFNetReactBridge", - "SalesforceNetReactBridge", - SFNetReactBridge, - SalesforceNetReactBridge, - successCB, - errorCB, - "sendRequest", - args, - ); + + if (typeof successCB === "function" && typeof errorCB === "function") { + return forceExec( + "SFNetReactBridge", + "SalesforceNetReactBridge", + SFNetReactBridge, + SalesforceNetReactBridge, + successCB, + errorCB, + "sendRequest", + args, + ); + } else { + return new Promise((resolve, reject) => { + forceExec( + "SFNetReactBridge", + "SalesforceNetReactBridge", + SFNetReactBridge, + SalesforceNetReactBridge, + resolve, + reject, + "sendRequest", + args, + ); + }); + } }; /** @@ -94,7 +122,7 @@ export const sendRequest = ( * @param callback function to which response will be passed * @param [error=null] function called in case of error */ -export const versions = (successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void => +export const versions: Versions = (successCB?: ExecSuccessCallback, errorCB?: ExecErrorCallback): any => sendRequest("/services/data", "/", successCB, errorCB); /** @@ -103,7 +131,7 @@ export const versions = (successCB: ExecSuccessCallback, errorCB: ExecErro * @param callback function to which response will be passed * @param [error=null] function called in case of error */ -export const resources = (successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void => +export const resources: Resources = (successCB?: ExecSuccessCallback, errorCB?: ExecErrorCallback): any => sendRequest("/services/data", `/${apiVersion}/`, successCB, errorCB); /** @@ -112,8 +140,10 @@ export const resources = (successCB: ExecSuccessCallback, errorCB: ExecErr * @param callback function to which response will be passed * @param [error=null] function called in case of error */ -export const describeGlobal = (successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void => - sendRequest("/services/data", `/${apiVersion}/sobjects/`, successCB, errorCB); +export const describeGlobal: DescribeGlobal = ( + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): any => sendRequest("/services/data", `/${apiVersion}/sobjects/`, successCB, errorCB); /** * Describes the individual metadata for the specified object. @@ -121,8 +151,11 @@ export const describeGlobal = (successCB: ExecSuccessCallback, errorCB: Ex * @param callback function to which response will be passed * @param [error=null] function called in case of error */ -export const metadata = (objtype: string, successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void => - sendRequest("/services/data", `/${apiVersion}/sobjects/${objtype}/`, successCB, errorCB); +export const metadata: Metadata = ( + objtype: string, + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): any => sendRequest("/services/data", `/${apiVersion}/sobjects/${objtype}/`, successCB, errorCB); /** * Completely describes the individual metadata at all levels for the @@ -131,8 +164,11 @@ export const metadata = (objtype: string, successCB: ExecSuccessCallback, * @param callback function to which response will be passed * @param [error=null] function called in case of error */ -export const describe = (objtype: string, successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void => - sendRequest("/services/data", `/${apiVersion}/sobjects/${objtype}/describe/`, successCB, errorCB); +export const describe: Describe = ( + objtype: string, + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): any => sendRequest("/services/data", `/${apiVersion}/sobjects/${objtype}/describe/`, successCB, errorCB); /** * Fetches the layout configuration for a particular sobject type and record type id. @@ -141,14 +177,15 @@ export const describe = (objtype: string, successCB: ExecSuccessCallback, * @param callback function to which response will be passed * @param [error=null] function called in case of error */ -export const describeLayout = ( +export const describeLayout: DescribeLayout = ( objtype: string, recordTypeId: string, // todo: add describe typings - successCB: ExecSuccessCallback, - errorCB: ExecErrorCallback, -): void => { + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): any => { recordTypeId = recordTypeId ? recordTypeId : ""; + return sendRequest( "/services/data", `/${apiVersion}/sobjects/${objtype}/describe/layouts/${recordTypeId}`, @@ -166,23 +203,12 @@ export const describeLayout = ( * @param callback function to which response will be passed * @param [error=null] function called in case of error */ -export const create = ( +export const create: Create = ( objtype: string, fields: Record, - successCB: ExecSuccessCallback, - errorCB: ExecErrorCallback, -): void => sendRequest("/services/data", `/${apiVersion}/sobjects/${objtype}/`, successCB, errorCB, "POST", fields); - -type RetrieveOverload = { - ( - objtype: string, - id: string, - fieldlist: string[], - successCB: ExecSuccessCallback, - errorCB: ExecErrorCallback, - ): void; - (objtype: string, id: string, successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void; -}; + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): any => sendRequest("/services/data", `/${apiVersion}/sobjects/${objtype}/`, successCB, errorCB, "POST", fields); /** * Retrieves field values for a record of the given type. @@ -193,13 +219,7 @@ type RetrieveOverload = { * @param callback function to which response will be passed * @param [error=null] function called in case of error */ -export const retrieve: RetrieveOverload = ( - objtype: string, - id: string, - x: string[] | ExecSuccessCallback, - y: ExecSuccessCallback | ExecErrorCallback, - z?: ExecErrorCallback, -): void => { +export const retrieve: Retrieve = (objtype: string, id: string, x?: any, y?: any, z?: any): any => { let fieldlist; let successCB: ExecSuccessCallback; let errorCB: ExecErrorCallback; @@ -230,14 +250,14 @@ export const retrieve: RetrieveOverload = ( * @param callback function to which response will be passed * @param [error=null] function called in case of error */ -export const upsert = ( +export const upsert: Upsert = ( objtype: string, externalIdField: string, externalId: string, fields: Record, - successCB: ExecSuccessCallback, - errorCB: ExecErrorCallback, -): void => + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): any => sendRequest( "/services/data", `/${apiVersion}/sobjects/${objtype}/${externalIdField}/${externalId ? externalId : ""}`, @@ -257,13 +277,13 @@ export const upsert = ( * @param callback function to which response will be passed * @param [error=null] function called in case of error */ -export const update = ( +export const update: Update = ( objtype: string, id: string, fields: Record, - successCB: ExecSuccessCallback, - errorCB: ExecErrorCallback, -): void => + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): any => sendRequest("/services/data", `/${apiVersion}/sobjects/${objtype}/${id}`, successCB, errorCB, "PATCH", fields); /** @@ -274,12 +294,12 @@ export const update = ( * @param callback function to which response will be passed * @param [error=null] function called in case of error */ -export const del = ( +export const del: Del = ( objtype: string, id: string, - successCB: ExecSuccessCallback, - errorCB: ExecErrorCallback, -): void => sendRequest("/services/data", `/${apiVersion}/sobjects/${objtype}/${id}`, successCB, errorCB, "DELETE"); + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): any => sendRequest("/services/data", `/${apiVersion}/sobjects/${objtype}/${id}`, successCB, errorCB, "DELETE"); /** * Executes the specified SOQL query. @@ -288,7 +308,7 @@ export const del = ( * @param callback function to which response will be passed * @param [error=null] function called in case of error */ -export const query = (soql: string, successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void => +export const query: Query = (soql: string, successCB?: ExecSuccessCallback, errorCB?: ExecErrorCallback): any => sendRequest("/services/data", `/${apiVersion}/query`, successCB, errorCB, "GET", { q: soql }); /** @@ -300,7 +320,11 @@ export const query = (soql: string, successCB: ExecSuccessCallback, errorC * @param callback function to which response will be passed * @param [error=null] function called in case of error */ -export const queryMore = (url: string, successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void => { +export const queryMore: QueryMore = ( + url: string, + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): any => { const pathFromUrl = url.match(/https:\/\/[^/]*(.*)/); if (pathFromUrl && pathFromUrl.length === 2) { return sendRequest("", pathFromUrl[1], successCB, errorCB); @@ -316,7 +340,7 @@ export const queryMore = (url: string, successCB: ExecSuccessCallback, err * @param callback function to which response will be passed * @param [error=null] function called in case of error */ -export const search = (sosl: string, successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void => +export const search: Search = (sosl: string, successCB?: ExecSuccessCallback, errorCB?: ExecErrorCallback): any => sendRequest("/services/data", `/${apiVersion}/search`, successCB, errorCB, "GET", { q: sosl }); /** @@ -325,12 +349,12 @@ export const search = (sosl: string, successCB: ExecSuccessCallback, error * @param callback function to which response will be passed (attachment is returned as {encodedBody:"base64-encoded-response", contentType:"content-type"}) * @param [error=null] function called in case of error */ -export const getAttachment = ( +export const getAttachment: GetAttachment = ( id: string, // todo: add attachment typings - successCB: ExecSuccessCallback, - errorCB: ExecErrorCallback, -): void => + successCB?: ExecSuccessCallback, + errorCB?: ExecErrorCallback, +): any => sendRequest( "/services/data", `/${apiVersion}/sobjects/Attachment/${id}/Body`, diff --git a/src/typings/net.d.ts b/src/typings/net.d.ts new file mode 100644 index 0000000..0917281 --- /dev/null +++ b/src/typings/net.d.ts @@ -0,0 +1,130 @@ +import { HttpMethod } from "."; +import { ExecErrorCallback, ExecSuccessCallback } from "../react.force.common"; + +export type SendRequest = { + ( + endPoint: string, + path: string, + successCB: undefined, + errorCB: undefined, + method?: HttpMethod, + payload?: Record | null, + headerParams?: Record | null, + fileParams?: unknown, + returnBinary?: boolean, + doesNotRequireAuthentication?: boolean, + ): Promise | void; + ( + endPoint: string, + path: string, + successCB: any, + errorCB: any, + method?: HttpMethod, + payload?: Record | null, + headerParams?: Record | null, + fileParams?: unknown, + returnBinary?: boolean, + doesNotRequireAuthentication?: boolean, + ): void; +}; + +export type Versions = { + (): Promise; + (successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void; +}; + +export type Resources = { + (): Promise; + (successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void; +}; + +export type DescribeGlobal = { + (): Promise; + (successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void; +}; + +export type Metadata = { + (objtype: string): Promise; + (objtype: string, successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void; +}; + +export type Describe = { + (objtype: string): Promise; + (objtype: string, successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void; +}; + +export type DescribeLayout = { + (objtype: string, recordTypeId: string): Promise; + (objtype: string, recordTypeId: string, successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void; +}; + +export type Create = { + (objtype: string, fields: Record): Promise; + ( + objtype: string, + fields: Record, + successCB: ExecSuccessCallback, + errorCB: ExecErrorCallback, + ): void; +}; + +export type Retrieve = { + (objtype: string, id: string, fieldlist: string[]): Promise; + ( + objtype: string, + id: string, + fieldlist: string[], + successCB: ExecSuccessCallback, + errorCB: ExecErrorCallback, + ): void; + (objtype: string, id: string): Promise; + (objtype: string, id: string, successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void; +}; + +export type Upsert = { + (objtype: string, externalIdField: string, externalId: string, fields: Record): Promise; + ( + objtype: string, + externalIdField: string, + externalId: string, + fields: Record, + successCB: ExecSuccessCallback, + errorCB: ExecErrorCallback, + ): void; +}; + +export type Update = { + (objtype: string, id: string, fields: Record): Promise; + ( + objtype: string, + id: string, + fields: Record, + successCB: ExecSuccessCallback, + errorCB: ExecErrorCallback, + ): void; +}; + +export type Del = { + (objtype: string, id: string): Promise; + (objtype: string, id: string, successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void; +}; + +export type Query = { + (soql: string): Promise; + (soql: string, successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void; +}; + +export type QueryMore = { + (url: string): Promise; + (url: string, successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void; +}; + +export type Search = { + (sosl: string): Promise; + (sosl: string, successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void; +}; + +export type GetAttachment = { + (id: string): Promise; + (id: string, successCB: ExecSuccessCallback, errorCB: ExecErrorCallback): void; +}; From 6a346c4c5451180abad504683a2f58736dd05a0c Mon Sep 17 00:00:00 2001 From: Jean-Philippe Monette Date: Wed, 17 Feb 2021 14:35:54 +0000 Subject: [PATCH 7/9] Ignore .DS_Store --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 3322c58..62f1dad 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,9 @@ .circleci/fastlane/README.md .circleci/fastlane/report.* .circleci/test_output/ +.DS_Store dist node_modules/ package-lock.json -yarn.lock Podfile.lock +yarn.lock From dfd0d570d070b0888320a9c2df64360f4014c3fa Mon Sep 17 00:00:00 2001 From: Jean-Philippe Monette Date: Wed, 17 Feb 2021 15:05:20 +0000 Subject: [PATCH 8/9] Update sync up/down target types --- src/typings/mobilesync.d.ts | 94 ++++++++++++++++++++++++++++++++++--- 1 file changed, 87 insertions(+), 7 deletions(-) diff --git a/src/typings/mobilesync.d.ts b/src/typings/mobilesync.d.ts index a1d1002..057fb34 100644 --- a/src/typings/mobilesync.d.ts +++ b/src/typings/mobilesync.d.ts @@ -3,6 +3,10 @@ import { StoreConfig } from "../react.force.smartstore"; export type SyncMethod = "cleanResyncGhosts" | "deleteSync" | "getSyncStatus" | "reSync" | "syncDown" | "syncUp"; +export type SyncDownTypes = "soql" | "sosl" | "mru" | "custom"; + +export type SyncOptionsMergeModes = "OVERWRITE" | "LEAVE_IF_CHANGED"; + export interface SyncEvent { soupName: string; options: SyncOptions; @@ -21,24 +25,100 @@ export interface SyncEvent { name: string; } -export type SyncDownTarget = { - type: "soql" | "sosl" | "mru" | "custom"; +export type SyncDownTarget = + | SyncDownCustomTarget + | SyncDownLayoutTarget + | SyncDownMetadataTarget + | SyncDownMRUTarget + | SyncDownParentChildrenTarget + | SyncDownRefreshTarget + | SyncDownSOQLTarget + | SyncDownSOSLTarget; + +export type SyncDownSOQLTarget = { + type: "soql"; + query: string; +}; + +export type SyncDownSOSLTarget = { + type: "sosl"; query: string; - modificationDateFieldName?: string; +}; + +export type SyncDownMRUTarget = { + type: "mru"; + sobjectType: string; + fieldlist: string[]; +}; + +export type SyncDownRefreshTarget = { + type: "refresh"; + sobjectType: string; + fieldlist: string[]; + soupName: string; +}; + +export type SyncDownLayoutTarget = { + type: "layout"; + sobjectType: string; + formFactor: string; + layoutType: string; + mode: string; +}; + +export type SyncDownMetadataTarget = { + type: "metadata"; + sobjectType: string; +}; + +export type SyncDownParentChildrenTarget = { + type: "parent_children"; + parent: { + idFieldName: string; + sobjectType: string; + modificationDateFieldName: string; + soupName: string; + }; + parentFieldlist: string[]; + children: { + parentIdFieldName: string; + idFieldName: string; + sobjectType: string; + modificationDateFieldName: string; + soupName: string; + sobjectTypePlural: string; + }; + childrenFieldlist: string[]; + relationshipType: string; + parentSoqlFilter: string; +}; + +export type SyncDownCustomTarget = { + type: "custom"; iOSImpl?: string; - idFieldName?: string; + androidImpl?: string; + + [key: string]: any; }; export type SyncUpTarget = { createFieldlist?: string[]; updateFieldlist?: string[]; + + [key: string]: any; }; -export type SyncOptions = { - mergeMode?: "OVERWRITE" | "LEAVE_IF_CHANGED"; - fieldlist?: string[]; +export type SyncUpOptions = { + mergeMode: SyncOptionsMergeModes; + fieldlist: string[]; }; +export type SyncDownOptions = { + mergeMode: SyncOptionsMergeModes; +}; + +export type SyncOptions = SyncUpOptions | SyncDownOptions; + export interface SyncStatus { _soupEntryId: number; endTime: number; From 9e4872e1844b49bf93d19e85410eacc317c819ea Mon Sep 17 00:00:00 2001 From: Jean-Philippe Monette Date: Tue, 23 Feb 2021 12:30:33 +0000 Subject: [PATCH 9/9] Export types from .ts file --- src/typings/{index.d.ts => index.ts} | 0 src/typings/{mobilesync.d.ts => mobilesync.ts} | 0 src/typings/{net.d.ts => net.ts} | 0 src/typings/{oauth.d.ts => oauth.ts} | 0 src/typings/{smartstore.d.ts => smartstore.ts} | 0 5 files changed, 0 insertions(+), 0 deletions(-) rename src/typings/{index.d.ts => index.ts} (100%) rename src/typings/{mobilesync.d.ts => mobilesync.ts} (100%) rename src/typings/{net.d.ts => net.ts} (100%) rename src/typings/{oauth.d.ts => oauth.ts} (100%) rename src/typings/{smartstore.d.ts => smartstore.ts} (100%) diff --git a/src/typings/index.d.ts b/src/typings/index.ts similarity index 100% rename from src/typings/index.d.ts rename to src/typings/index.ts diff --git a/src/typings/mobilesync.d.ts b/src/typings/mobilesync.ts similarity index 100% rename from src/typings/mobilesync.d.ts rename to src/typings/mobilesync.ts diff --git a/src/typings/net.d.ts b/src/typings/net.ts similarity index 100% rename from src/typings/net.d.ts rename to src/typings/net.ts diff --git a/src/typings/oauth.d.ts b/src/typings/oauth.ts similarity index 100% rename from src/typings/oauth.d.ts rename to src/typings/oauth.ts diff --git a/src/typings/smartstore.d.ts b/src/typings/smartstore.ts similarity index 100% rename from src/typings/smartstore.d.ts rename to src/typings/smartstore.ts