diff --git a/packages/ml/lib/index.ts b/packages/ml/lib/index.ts new file mode 100644 index 0000000000..ed29b146ff --- /dev/null +++ b/packages/ml/lib/index.ts @@ -0,0 +1,25 @@ +/* + * 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 * from './modular'; + +export type { FirebaseApp, FirebaseML } from './types/ml'; + +export type { FirebaseMLTypes } from './types/namespaced'; + +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..c43abef6a1 --- /dev/null +++ b/packages/ml/lib/modular.ts @@ -0,0 +1,32 @@ +/* + * 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, FirebaseML } from './types/ml'; + +/** + * Returns the {@link FirebaseML} instance for the default or given {@link FirebaseApp}. + * + * @param app - The Firebase `FirebaseApp` to use. When omitted, the default app is used. + * @returns The ML service instance for that app. + */ +export function getML(app?: FirebaseApp): FirebaseML { + if (app) { + return getApp(app.name).ml(); + } + return getApp().ml(); +} diff --git a/packages/ml/lib/modular/index.d.ts b/packages/ml/lib/modular/index.d.ts deleted file mode 100644 index d5569800c8..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 58% rename from packages/ml/lib/index.js rename to packages/ml/lib/namespaced.ts index e0da7630e9..dc838e65c9 100644 --- a/packages/ml/lib/index.js +++ b/packages/ml/lib/namespaced.ts @@ -15,29 +15,38 @@ * */ +import type { ReactNativeFirebase } from '@react-native-firebase/app'; import { createModuleNamespace, FirebaseModule, getFirebaseRoot, } from '@react-native-firebase/app/dist/module/internal'; -import version from './version'; +import './types/internal'; +import type { FirebaseMLTypes } from './types/namespaced'; +import { version } from './version'; -const statics = {}; +const statics = { + SDK_VERSION: version, +}; const namespace = 'ml'; const nativeModuleName = 'RNFBMLModule'; -class FirebaseMLModule extends FirebaseModule {} +class FirebaseMLModule extends FirebaseModule {} + +type MLNamespace = ReactNativeFirebase.FirebaseModuleWithStaticsAndApp< + FirebaseMLTypes.Module, + FirebaseMLTypes.Statics +> & { + firebase: ReactNativeFirebase.Module; + app(name?: string): ReactNativeFirebase.FirebaseApp; +}; // 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 defaultExport = createModuleNamespace({ statics, version, namespace, @@ -46,12 +55,14 @@ export default createModuleNamespace({ hasMultiAppSupport: true, hasCustomUrlOrRegionSupport: false, ModuleClass: FirebaseMLModule, -}); +}) as unknown as MLNamespace; + +export default defaultExport; // import ml, { firebase } from '@react-native-firebase/ml'; // ml().X(...); // firebase.ml().X(...); -export const firebase = getFirebaseRoot(); - -// e.g. -// // import { MLCloudTextRecognizerModelType } from '@react-native-firebase/ml'; +export const firebase = getFirebaseRoot() as unknown as ReactNativeFirebase.Module & { + ml: typeof defaultExport; + app(name?: string): ReactNativeFirebase.FirebaseApp & { ml(): FirebaseMLTypes.Module }; +}; diff --git a/packages/ml/lib/types/internal.ts b/packages/ml/lib/types/internal.ts new file mode 100644 index 0000000000..a8e2529eed --- /dev/null +++ b/packages/ml/lib/types/internal.ts @@ -0,0 +1,29 @@ +/* + * 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 contract for `RNFBMLModule`. + * The namespaced `FirebaseMLModule` implementation does not call into native from JS; + * this type anchors `FirebaseModule` and documents the bridge key. + */ +export interface RNFBMLModule {} + +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..1963b13d37 --- /dev/null +++ b/packages/ml/lib/types/ml.ts @@ -0,0 +1,32 @@ +/* + * 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'; + +export type FirebaseApp = ReactNativeFirebase.FirebaseApp; + +/** + * Firebase ML service instance for the modular API. + * + * Current Firebase JS SDK releases do not ship a `firebase/ml` modular entry point; this interface + * follows the same modular service-instance pattern used elsewhere in React Native Firebase (see e.g. + * the Firestore package's `Firestore` type). + */ +export interface FirebaseML extends ReactNativeFirebase.FirebaseModule { + /** The FirebaseApp this module is associated with */ + app: FirebaseApp; +} diff --git a/packages/ml/lib/index.d.ts b/packages/ml/lib/types/namespaced.ts similarity index 67% rename from packages/ml/lib/index.d.ts rename to packages/ml/lib/types/namespaced.ts index 127437634e..4a59240e74 100644 --- a/packages/ml/lib/index.d.ts +++ b/packages/ml/lib/types/namespaced.ts @@ -15,7 +15,8 @@ * */ -import { ReactNativeFirebase } from '@react-native-firebase/app'; +import type { ReactNativeFirebase } from '@react-native-firebase/app'; + /** * Firebase ML package for React Native. * @@ -52,46 +53,40 @@ import { ReactNativeFirebase } from '@react-native-firebase/app'; * * @firebase ml */ +/** + * @deprecated Use the exported types directly instead. + * FirebaseMLTypes namespace is kept for backwards compatibility. + */ +/* eslint-disable @typescript-eslint/no-namespace */ export namespace FirebaseMLTypes { - import FirebaseModule = ReactNativeFirebase.FirebaseModule; + /** + * @deprecated Use the exported types directly instead. FirebaseMLTypes namespace is kept for backwards compatibility. + */ + type FirebaseModule = ReactNativeFirebase.FirebaseModule; + /** + * @deprecated Use the default export statics instead. + */ export interface Statics { + /** @deprecated Use the default export statics instead. */ SDK_VERSION: string; } - export class Module extends FirebaseModule { + /** + * @deprecated Use the exported `FirebaseML` type instead. + */ + export interface Module extends FirebaseModule { /** + * @deprecated Use the exported `FirebaseML` type instead. + * * 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; } @@ -101,3 +96,4 @@ declare module '@react-native-firebase/app' { } } } +/* eslint-enable @typescript-eslint/no-namespace */ diff --git a/packages/ml/package.json b/packages/ml/package.json index 314b951bc5..ced6eb24eb 100644 --- a/packages/ml/package.json +++ b/packages/ml/package.json @@ -3,12 +3,13 @@ "version": "24.0.0", "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,38 @@ "publishConfig": { "access": "public", "provenance": true - } + }, + "devDependencies": { + "react-native-builder-bob": "^0.40.13" + }, + "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..995ff9f3f0 --- /dev/null +++ b/packages/ml/tsconfig.json @@ -0,0 +1,16 @@ +{ + "extends": "../../tsconfig.packages.base.json", + "compilerOptions": { + "baseUrl": ".", + "rootDir": ".", + "paths": { + "@react-native-firebase/app/dist/module/internal": ["../app/dist/typescript/lib/internal"], + "@react-native-firebase/app/dist/module/internal/NativeModules": [ + "../app/dist/typescript/lib/internal/NativeModules" + ], + "@react-native-firebase/app": ["../app/dist/typescript/lib"] + } + }, + "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..28678b75ff 100644 --- a/packages/ml/type-test.ts +++ b/packages/ml/type-test.ts @@ -1,4 +1,12 @@ -import ml, { firebase, getML } from '.'; +// Import from package sources so root `yarn tsc:compile` does not require `packages/ml/dist`. +import ml, { + firebase, + getML, + SDK_VERSION, + type FirebaseApp, + type FirebaseML, + type FirebaseMLTypes, +} from './lib/index'; console.log(ml().app); @@ -33,3 +41,22 @@ console.log(modularML1.app.name); const modularML2 = getML(firebase.app()); console.log(modularML2.app.name); + +// modular public types +const modularInstance: FirebaseML = getML(); +const modularWithNamedApp: FirebaseML = getML(firebase.app()); +console.log(modularInstance.app.name); +console.log(modularWithNamedApp.app.name); + +const namedApp: FirebaseApp = firebase.app(); +console.log(getML(namedApp).app.name); + +// named SDK_VERSION export +const sdkVersion: string = SDK_VERSION; +console.log(sdkVersion); + +// deprecated namespace types remain assignable from runtime values +const namespaceModule: FirebaseMLTypes.Module = firebase.ml(); +const namespaceStatics: FirebaseMLTypes.Statics = firebase.ml; +console.log(namespaceModule.app.name); +console.log(namespaceStatics.SDK_VERSION); diff --git a/tsconfig.json b/tsconfig.json index 2a4298e5e0..1353010bdb 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -19,8 +19,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/functions/type-test.ts", @@ -70,7 +68,7 @@ ], "@react-native-firebase/firestore/pipelines": [ "./packages/firestore/dist/typescript/lib/pipelines/index.d.ts" - ], + ] } }, "exclude": ["node_modules", "**/*.spec.ts", "packages/**/dist"]