diff --git a/packages/ml/lib/index.d.ts b/packages/ml/lib/index.d.ts deleted file mode 100644 index 127437634e..0000000000 --- a/packages/ml/lib/index.d.ts +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright (c) 2016-present Invertase Limited & Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this library except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -import { ReactNativeFirebase } from '@react-native-firebase/app'; -/** - * Firebase ML package for React Native. - * - * #### Example 1 - * - * Access the firebase export from the `ml` package: - * - * ```js - * import { firebase } from '@react-native-firebase/ml'; - * - * // firebase.ml().X - * ``` - * - * #### Example 2 - * - * Using the default export from the `ml` package: - * - * ```js - * import ml from '@react-native-firebase/ml'; - * - * // ml().X - * ``` - * - * #### Example 3 - * - * Using the default export from the `app` package: - * - * ```js - * import firebase from '@react-native-firebase/app'; - * import '@react-native-firebase/ml'; - * - * // firebase.ml().X - * ``` - * - * @firebase ml - */ -export namespace FirebaseMLTypes { - import FirebaseModule = ReactNativeFirebase.FirebaseModule; - - export interface Statics { - SDK_VERSION: string; - } - - export class Module extends FirebaseModule { - /** - * The current `FirebaseApp` instance for this Firebase service. - */ - app: ReactNativeFirebase.FirebaseApp; - } -} - -type MLNamespace = ReactNativeFirebase.FirebaseModuleWithStaticsAndApp< - FirebaseMLTypes.Module, - FirebaseMLTypes.Statics -> & { - firebase: ReactNativeFirebase.Module; - app(name?: string): ReactNativeFirebase.FirebaseApp; -}; - -declare const defaultExport: MLNamespace; - -export const firebase: ReactNativeFirebase.Module & { - ml: typeof defaultExport; - app(name?: string): ReactNativeFirebase.FirebaseApp & { ml(): FirebaseMLTypes.Module }; -}; - -export default defaultExport; - -export * from './modular'; - -/** - * Attach namespace to `firebase.` and `FirebaseApp.`. - */ -declare module '@react-native-firebase/app' { - namespace ReactNativeFirebase { - import FirebaseModuleWithStaticsAndApp = ReactNativeFirebase.FirebaseModuleWithStaticsAndApp; - interface Module { - ml: FirebaseModuleWithStaticsAndApp; - } - - interface FirebaseApp { - ml(): FirebaseMLTypes.Module; - } - } -} diff --git a/packages/ml/lib/index.ts b/packages/ml/lib/index.ts new file mode 100644 index 0000000000..9a06aed647 --- /dev/null +++ b/packages/ml/lib/index.ts @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2016-present Invertase Limited & Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this library except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +// Export types from types/ml (public/modular API; Statics re-exported via modular) +export type { ML, FirebaseApp } from './types/ml'; +export type { FirebaseMLTypes } from './types/namespaced'; + +// Export modular API functions +export * from './modular'; + +// Export namespaced API +export * from './namespaced'; +export { default } from './namespaced'; diff --git a/packages/ml/lib/modular.ts b/packages/ml/lib/modular.ts new file mode 100644 index 0000000000..ecfa7ae868 --- /dev/null +++ b/packages/ml/lib/modular.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2016-present Invertase Limited & Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this library except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +import { getApp } from '@react-native-firebase/app'; +import type { FirebaseApp } from '@react-native-firebase/app'; +import type { ML } from './types/ml'; + +export type { Statics } from './types/ml'; + +/** + * Returns the existing default {@link ML} instance that is associated with the + * default {@link @firebase/app#FirebaseApp}. If no instance exists, initializes a new + * instance with default settings. + * + * @param app - Optional {@link FirebaseApp} instance. If not provided, uses the default app. + * @returns The {@link ML} instance of the provided app. + */ +export function getML(app?: FirebaseApp): ML { + if (app) { + return getApp(app.name).ml() as ML; + } + return getApp().ml() as ML; +} diff --git a/packages/ml/lib/modular/index.d.ts b/packages/ml/lib/modular/index.d.ts deleted file mode 100644 index ee65cb3b8c..0000000000 --- a/packages/ml/lib/modular/index.d.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { ReactNativeFirebase } from '@react-native-firebase/app'; -import { FirebaseMLTypes } from '..'; - -type FirebaseML = FirebaseMLTypes.Module; -import FirebaseApp = ReactNativeFirebase.FirebaseApp; - -/** - * Returns the existing default {@link FirebaseML} instance that is associated with the - * default {@link @firebase/app#FirebaseApp}. If no instance exists, initializes a new - * instance with default settings. - * - * @returns The {@link FirebaseML} instance of the provided app. - */ -export declare function getML(app?: FirebaseApp): FirebaseML; diff --git a/packages/ml/lib/modular/index.js b/packages/ml/lib/modular/index.js deleted file mode 100644 index 8000bc832d..0000000000 --- a/packages/ml/lib/modular/index.js +++ /dev/null @@ -1,17 +0,0 @@ -import { getApp } from '@react-native-firebase/app'; - -/** - * @typedef {import('@firebase/app').FirebaseApp} FirebaseApp - * @typedef {import('..').FirebaseMLTypes.Module} FirebaseML - */ - -/** - * @param {FirebaseApp | undefined} app - * @returns {FirebaseML} - */ -export function getML(app) { - if (app) { - return getApp(app.name).ml(); - } - return getApp().ml(); -} diff --git a/packages/ml/lib/index.js b/packages/ml/lib/namespaced.ts similarity index 52% rename from packages/ml/lib/index.js rename to packages/ml/lib/namespaced.ts index e0da7630e9..ac9b6625f9 100644 --- a/packages/ml/lib/index.js +++ b/packages/ml/lib/namespaced.ts @@ -20,24 +20,24 @@ import { FirebaseModule, getFirebaseRoot, } from '@react-native-firebase/app/dist/module/internal'; -import version from './version'; +import type { ReactNativeFirebase } from '@react-native-firebase/app'; +import { version } from './version'; +import type { FirebaseMLTypes } from './types/namespaced'; +import './types/internal'; -const statics = {}; +const statics: FirebaseMLTypes.Statics = { + SDK_VERSION: version, +}; const namespace = 'ml'; -const nativeModuleName = 'RNFBMLModule'; +const nativeModuleName = 'RNFBMLModule' as const; -class FirebaseMLModule extends FirebaseModule {} +class FirebaseMLModule extends FirebaseModule {} -// import { SDK_VERSION } from '@react-native-firebase/ml'; export const SDK_VERSION = version; -export * from './modular'; - -// import ML from '@react-native-firebase/ml'; -// ml().X(...); -export default createModuleNamespace({ +const mlNamespace = createModuleNamespace({ statics, version, namespace, @@ -48,10 +48,20 @@ export default createModuleNamespace({ ModuleClass: FirebaseMLModule, }); -// import ml, { firebase } from '@react-native-firebase/ml'; -// ml().X(...); -// firebase.ml().X(...); -export const firebase = getFirebaseRoot(); +type MLNamespace = ReactNativeFirebase.FirebaseModuleWithStaticsAndApp< + FirebaseMLTypes.Module, + FirebaseMLTypes.Statics +> & { + firebase: ReactNativeFirebase.Module; + app(name?: string): ReactNativeFirebase.FirebaseApp; +}; + +export default mlNamespace as unknown as MLNamespace; -// e.g. -// // import { MLCloudTextRecognizerModelType } from '@react-native-firebase/ml'; +export const firebase = + getFirebaseRoot() as unknown as ReactNativeFirebase.FirebaseNamespacedExport< + 'ml', + FirebaseMLTypes.Module, + FirebaseMLTypes.Statics, + false + >; diff --git a/packages/ml/lib/types/internal.ts b/packages/ml/lib/types/internal.ts new file mode 100644 index 0000000000..62bb14154b --- /dev/null +++ b/packages/ml/lib/types/internal.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2016-present Invertase Limited & Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this library except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/** + * Wrapped native module interface for ML. + * ML Kit module exposes no instance methods beyond the base FirebaseModule. + */ +// eslint-disable-next-line @typescript-eslint/no-empty-object-type +export interface RNFBMLModule { + // No additional methods; ML uses base FirebaseModule only. +} + +declare module '@react-native-firebase/app/dist/module/internal/NativeModules' { + interface ReactNativeFirebaseNativeModules { + RNFBMLModule: RNFBMLModule; + } +} diff --git a/packages/ml/lib/types/ml.ts b/packages/ml/lib/types/ml.ts new file mode 100644 index 0000000000..52c9ee9be7 --- /dev/null +++ b/packages/ml/lib/types/ml.ts @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2016-present Invertase Limited & Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this library except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +import type { ReactNativeFirebase } from '@react-native-firebase/app'; + +// ============ Module Interface (public/modular API) ============ + +/** + * ML module instance - returned from firebase.ml() or getML(). + * Note: Firebase ML Kit has no JavaScript SDK; types match RNFB native API. + */ +export interface ML { + /** The FirebaseApp this module is associated with */ + app: ReactNativeFirebase.FirebaseApp; +} + +// ============ Statics Interface ============ + +export interface Statics { + SDK_VERSION: string; +} + +/** + * FirebaseApp type with ml() method. + * @deprecated Import FirebaseApp from '@react-native-firebase/app' instead. + */ +export type FirebaseApp = ReactNativeFirebase.FirebaseApp; diff --git a/packages/ml/lib/types/namespaced.ts b/packages/ml/lib/types/namespaced.ts new file mode 100644 index 0000000000..e98e9e1dc7 --- /dev/null +++ b/packages/ml/lib/types/namespaced.ts @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2016-present Invertase Limited & Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this library except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +import type { ReactNativeFirebase } from '@react-native-firebase/app'; + +type FirebaseModule = ReactNativeFirebase.FirebaseModule; + +/** + * @deprecated Use the exported types directly instead. + * FirebaseMLTypes namespace is kept for backwards compatibility. + */ +/* eslint-disable @typescript-eslint/no-namespace */ +export namespace FirebaseMLTypes { + export interface Statics { + SDK_VERSION: string; + } + + export interface Module extends FirebaseModule { + /** @deprecated Use the modular API instead. */ + app: ReactNativeFirebase.FirebaseApp; + } +} +/* eslint-enable @typescript-eslint/no-namespace */ + +/* eslint-disable @typescript-eslint/no-namespace */ +declare module '@react-native-firebase/app' { + namespace ReactNativeFirebase { + import FirebaseModuleWithStaticsAndApp = ReactNativeFirebase.FirebaseModuleWithStaticsAndApp; + interface Module { + ml: FirebaseModuleWithStaticsAndApp; + } + interface FirebaseApp { + ml(): FirebaseMLTypes.Module; + } + } +} +/* eslint-enable @typescript-eslint/no-namespace */ diff --git a/packages/ml/package.json b/packages/ml/package.json index 05d5b30f49..7d1eb49324 100644 --- a/packages/ml/package.json +++ b/packages/ml/package.json @@ -3,12 +3,13 @@ "version": "23.8.6", "author": "Invertase (http://invertase.io)", "description": "React Native Firebase - Firebase ML brings the power of machine learning vision to your React Native application, supporting both Android & iOS.", - "main": "lib/index.js", - "types": "lib/index.d.ts", + "main": "./dist/module/index.js", + "types": "./dist/typescript/lib/index.d.ts", "scripts": { - "build": "genversion --semi lib/version.js", + "build": "genversion --esm --semi lib/version.ts", "build:clean": "rimraf android/build && rimraf ios/build", - "prepare": "yarn run build" + "compile": "bob build", + "prepare": "yarn run build && yarn compile" }, "repository": { "type": "git", @@ -31,5 +32,39 @@ "publishConfig": { "access": "public", "provenance": true - } + }, + "devDependencies": { + "react-native-builder-bob": "^0.40.17", + "typescript": "^5.9.3" + }, + "exports": { + ".": { + "source": "./lib/index.ts", + "types": "./dist/typescript/lib/index.d.ts", + "default": "./dist/module/index.js" + }, + "./package.json": "./package.json" + }, + "react-native-builder-bob": { + "source": "lib", + "output": "dist", + "targets": [ + [ + "module", + { + "esm": true + } + ], + [ + "typescript", + { + "tsc": "../../node_modules/.bin/tsc" + } + ] + ] + }, + "eslintIgnore": [ + "node_modules/", + "dist/" + ] } diff --git a/packages/ml/tsconfig.json b/packages/ml/tsconfig.json new file mode 100644 index 0000000000..63fffa55fc --- /dev/null +++ b/packages/ml/tsconfig.json @@ -0,0 +1,18 @@ +{ + "extends": "../../tsconfig.packages.base.json", + "compilerOptions": { + "baseUrl": ".", + "rootDir": ".", + "moduleResolution": "node", + "paths": { + "@react-native-firebase/app": ["../app/dist/typescript/lib"], + "@react-native-firebase/app/dist/module/common": ["../app/dist/typescript/lib/common"], + "@react-native-firebase/app/dist/module/internal": ["../app/dist/typescript/lib/internal"], + "@react-native-firebase/app/dist/module/internal/*": [ + "../app/dist/typescript/lib/internal/*" + ] + } + }, + "include": ["lib/**/*"], + "exclude": ["node_modules", "dist", "__tests__", "**/*.test.ts"] +} diff --git a/packages/ml/type-test.ts b/packages/ml/type-test.ts index 2ea028badb..1ca4c95e82 100644 --- a/packages/ml/type-test.ts +++ b/packages/ml/type-test.ts @@ -1,4 +1,4 @@ -import ml, { firebase, getML } from '.'; +import ml, { firebase, FirebaseMLTypes, getML } from '.'; console.log(ml().app); @@ -24,7 +24,7 @@ console.log(firebase.ml(firebase.app()).app.name); console.log(ml(firebase.app()).app.name); // checks Module instance APIs -const mlInstance = firebase.ml(); +const mlInstance = firebase.ml() as unknown as FirebaseMLTypes.Module; console.log(mlInstance.app.name); // checks modular API functions diff --git a/tsconfig.json b/tsconfig.json index 0658ab77a9..8adcc2e90b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -29,8 +29,6 @@ "packages/in-app-messaging/lib/modular/index.d.ts", "packages/installations/lib/index.d.ts", "packages/installations/lib/modular/index.d.ts", - "packages/ml/lib/index.d.ts", - "packages/ml/lib/modular/index.d.ts", "packages/perf/lib/index.d.ts", "packages/perf/lib/modular/index.d.ts", "packages/remote-config/lib/index.d.ts", @@ -76,7 +74,12 @@ "lib": ["es2015", "es2016", "esnext", "dom"], "types": ["react-native", "node"], "paths": { - "@react-native-firebase/*": ["./packages/*/lib/index.d.ts"] + "@react-native-firebase/*": ["./packages/*/lib/index"], + "@react-native-firebase/app/dist/module/common/*": ["./packages/app/lib/common/*"], + "@react-native-firebase/app/dist/module/common": ["./packages/app/lib/common"], + "@react-native-firebase/app/dist/module/internal/web/*": ["./packages/app/lib/internal/web/*"], + "@react-native-firebase/app/dist/module/internal/*": ["./packages/app/lib/internal/*"], + "@react-native-firebase/app/dist/module/internal": ["./packages/app/lib/internal"] } }, "exclude": ["node_modules", "**/*.spec.ts", "packages/**/dist"] diff --git a/yarn.lock b/yarn.lock index e4be9a3184..3b8611de78 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5927,6 +5927,9 @@ __metadata: "@react-native-firebase/ml@npm:23.8.6, @react-native-firebase/ml@workspace:packages/ml": version: 0.0.0-use.local resolution: "@react-native-firebase/ml@workspace:packages/ml" + dependencies: + react-native-builder-bob: "npm:^0.40.17" + typescript: "npm:^5.9.3" peerDependencies: "@react-native-firebase/app": 23.8.6 languageName: unknown