Skip to content

Commit

Permalink
feat: Implement FlagshipLinkRequest interface through localMethods
Browse files Browse the repository at this point in the history
In previous commits we configured the application's cozy-client to use
CozyPouchLink

This link is configured for all react-native side queries, but not for
cozy-app queries as they are served inside of WebViews and use a
different cozy-client instance

We want cozy-apps to benefits from the CozyPouchLink by redirecting
their queries to the react-native side using the new FlagshipLink
interface

With this interface, all cozy-apps queries can be redirected to the
react-native side using cozy-intent/post-me

In order to intercept them, then we should declare
`FlagshipLinkRequest()` method `localMethods`

Related PR: cozy/cozy-client#1505
  • Loading branch information
Ldoppea committed Sep 24, 2024
1 parent fb98bc3 commit 9918f36
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/@types/cozy-client.d.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -157,6 +163,10 @@ declare module 'cozy-client' {
on: (event: string, callback: () => void) => void
removeListener: (event: string, callback: () => void) => void
logout: () => Promise<void>
query: (
queryDefinition: QueryDefinition,
options?: QueryOptions
) => Promise<QueryResult>
}

export const createMockClient = (options?: ClientOptions): CozyClient =>
Expand Down
33 changes: 33 additions & 0 deletions src/libs/intents/flagshipLink.ts
Original file line number Diff line number Diff line change
@@ -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<QueryResult> => {
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
}
}
5 changes: 4 additions & 1 deletion src/libs/intents/localMethods.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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
}
}

0 comments on commit 9918f36

Please sign in to comment.