diff --git a/src/@types/cozy-client.d.ts b/src/@types/cozy-client.d.ts index 7bb4fc2af..0238cc1d1 100644 --- a/src/@types/cozy-client.d.ts +++ b/src/@types/cozy-client.d.ts @@ -1,7 +1,13 @@ /* eslint-disable @typescript-eslint/no-unused-vars */ import 'cozy-client' -import { FileDocument, CozyClientDocument } from 'cozy-client/types/types' +import { QueryDefinition } from 'cozy-client' +import { + FileDocument, + CozyClientDocument, + QueryOptions, + QueryResult +} from 'cozy-client/types/types' declare module 'cozy-client' { interface ClientOptions { @@ -157,6 +163,10 @@ declare module 'cozy-client' { on: (event: string, callback: () => void) => void removeListener: (event: string, callback: () => void) => void logout: () => Promise + query: ( + queryDefinition: QueryDefinition, + options?: QueryOptions + ) => Promise } export const createMockClient = (options?: ClientOptions): CozyClient => diff --git a/src/libs/intents/flagshipLink.ts b/src/libs/intents/flagshipLink.ts new file mode 100644 index 000000000..1248cd2a3 --- /dev/null +++ b/src/libs/intents/flagshipLink.ts @@ -0,0 +1,33 @@ +import CozyClient, { QueryDefinition } from 'cozy-client' +import type { QueryResult } from 'cozy-client/types/types' +import Minilog from 'cozy-minilog' + +import { getErrorMessage } from '/libs/functions/getErrorMessage' + +const log = Minilog('⛳🔗 flagshipLink') + +export const flagshipLinkRequest = async ( + operation: QueryDefinition, + client: CozyClient | undefined +): Promise => { + try { + if (!client) { + throw new Error( + 'FlagshipLinkRequest should not be called with undefined client' + ) + } + + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + const result = await client.query(operation) + + // eslint-disable-next-line @typescript-eslint/no-unsafe-return + return result + } catch (error) { + const errorMessage = getErrorMessage(error) + log.error( + `Something when wrong while processing FlagshipLinkRequest: ${errorMessage}`, + operation + ) + throw error + } +} diff --git a/src/libs/intents/localMethods.ts b/src/libs/intents/localMethods.ts index 362d1366b..0c68a441b 100644 --- a/src/libs/intents/localMethods.ts +++ b/src/libs/intents/localMethods.ts @@ -1,7 +1,7 @@ import { Linking } from 'react-native' import { getDeviceName } from 'react-native-device-info' -import CozyClient from 'cozy-client' +import CozyClient, { QueryDefinition } from 'cozy-client' import { FlagshipUI, NativeMethodsRegisterWithOptions, @@ -36,6 +36,7 @@ import { openApp } from '/libs/functions/openApp' import { routes } from '/constants/routes' import { sendKonnectorsLogs } from '/libs/konnectors/sendKonnectorsLogs' import { setDefaultRedirection } from '/libs/defaultRedirection/defaultRedirection' +import { flagshipLinkRequest } from '/libs/intents/flagshipLink' import { setFlagshipUI } from '/libs/intents/setFlagshipUI' import { showInAppBrowser, closeInAppBrowser } from '/libs/intents/InAppBrowser' import { isBiometryDenied } from '/app/domain/authentication/services/BiometryService' @@ -398,6 +399,8 @@ export const localMethods = ( _options: PostMeMessageOptions, colorScheme: string | undefined ) => Promise.resolve(setColorScheme(colorScheme)), + flagshipLinkRequest: (_options, operation) => + flagshipLinkRequest(operation as QueryDefinition, client), ...mergedMethods } }