Skip to content

Commit 2ed934c

Browse files
refactor(ml): migrate to TypeScript (#9005)
--------- Co-authored-by: Mike Hardy <github@mikehardy.net>
1 parent 054c458 commit 2ed934c

14 files changed

Lines changed: 248 additions & 81 deletions

File tree

packages/ml/lib/index.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (c) 2016-present Invertase Limited & Contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this library except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
export * from './modular';
19+
20+
export type { FirebaseApp, FirebaseML } from './types/ml';
21+
22+
export type { FirebaseMLTypes } from './types/namespaced';
23+
24+
export * from './namespaced';
25+
export { default } from './namespaced';

packages/ml/lib/modular.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2016-present Invertase Limited & Contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this library except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
import { getApp } from '@react-native-firebase/app';
19+
import type { FirebaseApp, FirebaseML } from './types/ml';
20+
21+
/**
22+
* Returns the {@link FirebaseML} instance for the default or given {@link FirebaseApp}.
23+
*
24+
* @param app - The Firebase `FirebaseApp` to use. When omitted, the default app is used.
25+
* @returns The ML service instance for that app.
26+
*/
27+
export function getML(app?: FirebaseApp): FirebaseML {
28+
if (app) {
29+
return getApp(app.name).ml();
30+
}
31+
return getApp().ml();
32+
}

packages/ml/lib/modular/index.d.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

packages/ml/lib/modular/index.js

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,38 @@
1515
*
1616
*/
1717

18+
import type { ReactNativeFirebase } from '@react-native-firebase/app';
1819
import {
1920
createModuleNamespace,
2021
FirebaseModule,
2122
getFirebaseRoot,
2223
} from '@react-native-firebase/app/dist/module/internal';
23-
import version from './version';
24+
import './types/internal';
25+
import type { FirebaseMLTypes } from './types/namespaced';
26+
import { version } from './version';
2427

25-
const statics = {};
28+
const statics = {
29+
SDK_VERSION: version,
30+
};
2631

2732
const namespace = 'ml';
2833

2934
const nativeModuleName = 'RNFBMLModule';
3035

31-
class FirebaseMLModule extends FirebaseModule {}
36+
class FirebaseMLModule extends FirebaseModule<typeof nativeModuleName> {}
37+
38+
type MLNamespace = ReactNativeFirebase.FirebaseModuleWithStaticsAndApp<
39+
FirebaseMLTypes.Module,
40+
FirebaseMLTypes.Statics
41+
> & {
42+
firebase: ReactNativeFirebase.Module;
43+
app(name?: string): ReactNativeFirebase.FirebaseApp;
44+
};
3245

3346
// import { SDK_VERSION } from '@react-native-firebase/ml';
3447
export const SDK_VERSION = version;
3548

36-
export * from './modular';
37-
38-
// import ML from '@react-native-firebase/ml';
39-
// ml().X(...);
40-
export default createModuleNamespace({
49+
const defaultExport = createModuleNamespace({
4150
statics,
4251
version,
4352
namespace,
@@ -46,12 +55,14 @@ export default createModuleNamespace({
4655
hasMultiAppSupport: true,
4756
hasCustomUrlOrRegionSupport: false,
4857
ModuleClass: FirebaseMLModule,
49-
});
58+
}) as unknown as MLNamespace;
59+
60+
export default defaultExport;
5061

5162
// import ml, { firebase } from '@react-native-firebase/ml';
5263
// ml().X(...);
5364
// firebase.ml().X(...);
54-
export const firebase = getFirebaseRoot();
55-
56-
// e.g.
57-
// // import { MLCloudTextRecognizerModelType } from '@react-native-firebase/ml';
65+
export const firebase = getFirebaseRoot() as unknown as ReactNativeFirebase.Module & {
66+
ml: typeof defaultExport;
67+
app(name?: string): ReactNativeFirebase.FirebaseApp & { ml(): FirebaseMLTypes.Module };
68+
};

packages/ml/lib/types/internal.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) 2016-present Invertase Limited & Contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this library except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
/**
19+
* Wrapped native module contract for `RNFBMLModule`.
20+
* The namespaced `FirebaseMLModule` implementation does not call into native from JS;
21+
* this type anchors `FirebaseModule<typeof nativeModuleName>` and documents the bridge key.
22+
*/
23+
export interface RNFBMLModule {}
24+
25+
declare module '@react-native-firebase/app/dist/module/internal/NativeModules' {
26+
interface ReactNativeFirebaseNativeModules {
27+
RNFBMLModule: RNFBMLModule;
28+
}
29+
}

packages/ml/lib/types/ml.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2016-present Invertase Limited & Contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this library except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
import type { ReactNativeFirebase } from '@react-native-firebase/app';
19+
20+
export type FirebaseApp = ReactNativeFirebase.FirebaseApp;
21+
22+
/**
23+
* Firebase ML service instance for the modular API.
24+
*
25+
* Current Firebase JS SDK releases do not ship a `firebase/ml` modular entry point; this interface
26+
* follows the same modular service-instance pattern used elsewhere in React Native Firebase (see e.g.
27+
* the Firestore package's `Firestore` type).
28+
*/
29+
export interface FirebaseML extends ReactNativeFirebase.FirebaseModule {
30+
/** The FirebaseApp this module is associated with */
31+
app: FirebaseApp;
32+
}
Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
*
1616
*/
1717

18-
import { ReactNativeFirebase } from '@react-native-firebase/app';
18+
import type { ReactNativeFirebase } from '@react-native-firebase/app';
19+
1920
/**
2021
* Firebase ML package for React Native.
2122
*
@@ -52,46 +53,40 @@ import { ReactNativeFirebase } from '@react-native-firebase/app';
5253
*
5354
* @firebase ml
5455
*/
56+
/**
57+
* @deprecated Use the exported types directly instead.
58+
* FirebaseMLTypes namespace is kept for backwards compatibility.
59+
*/
60+
/* eslint-disable @typescript-eslint/no-namespace */
5561
export namespace FirebaseMLTypes {
56-
import FirebaseModule = ReactNativeFirebase.FirebaseModule;
62+
/**
63+
* @deprecated Use the exported types directly instead. FirebaseMLTypes namespace is kept for backwards compatibility.
64+
*/
65+
type FirebaseModule = ReactNativeFirebase.FirebaseModule;
5766

67+
/**
68+
* @deprecated Use the default export statics instead.
69+
*/
5870
export interface Statics {
71+
/** @deprecated Use the default export statics instead. */
5972
SDK_VERSION: string;
6073
}
6174

62-
export class Module extends FirebaseModule {
75+
/**
76+
* @deprecated Use the exported `FirebaseML` type instead.
77+
*/
78+
export interface Module extends FirebaseModule {
6379
/**
80+
* @deprecated Use the exported `FirebaseML` type instead.
81+
*
6482
* The current `FirebaseApp` instance for this Firebase service.
6583
*/
6684
app: ReactNativeFirebase.FirebaseApp;
6785
}
6886
}
6987

70-
type MLNamespace = ReactNativeFirebase.FirebaseModuleWithStaticsAndApp<
71-
FirebaseMLTypes.Module,
72-
FirebaseMLTypes.Statics
73-
> & {
74-
firebase: ReactNativeFirebase.Module;
75-
app(name?: string): ReactNativeFirebase.FirebaseApp;
76-
};
77-
78-
declare const defaultExport: MLNamespace;
79-
80-
export const firebase: ReactNativeFirebase.Module & {
81-
ml: typeof defaultExport;
82-
app(name?: string): ReactNativeFirebase.FirebaseApp & { ml(): FirebaseMLTypes.Module };
83-
};
84-
85-
export default defaultExport;
86-
87-
export * from './modular';
88-
89-
/**
90-
* Attach namespace to `firebase.` and `FirebaseApp.`.
91-
*/
9288
declare module '@react-native-firebase/app' {
9389
namespace ReactNativeFirebase {
94-
import FirebaseModuleWithStaticsAndApp = ReactNativeFirebase.FirebaseModuleWithStaticsAndApp;
9590
interface Module {
9691
ml: FirebaseModuleWithStaticsAndApp<FirebaseMLTypes.Module, FirebaseMLTypes.Statics>;
9792
}
@@ -101,3 +96,4 @@ declare module '@react-native-firebase/app' {
10196
}
10297
}
10398
}
99+
/* eslint-enable @typescript-eslint/no-namespace */

packages/ml/package.json

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
"version": "24.1.0",
44
"author": "Invertase <oss@invertase.io> (http://invertase.io)",
55
"description": "React Native Firebase - Firebase ML brings the power of machine learning vision to your React Native application, supporting both Android & iOS.",
6-
"main": "lib/index.js",
7-
"types": "lib/index.d.ts",
6+
"main": "./dist/module/index.js",
7+
"types": "./dist/typescript/lib/index.d.ts",
88
"scripts": {
9-
"build": "genversion --semi lib/version.js",
9+
"build": "genversion --esm --semi lib/version.ts",
1010
"build:clean": "rimraf android/build && rimraf ios/build",
11-
"prepare": "yarn run build"
11+
"compile": "bob build",
12+
"prepare": "yarn run build && yarn compile"
1213
},
1314
"repository": {
1415
"type": "git",
@@ -31,5 +32,38 @@
3132
"publishConfig": {
3233
"access": "public",
3334
"provenance": true
34-
}
35+
},
36+
"devDependencies": {
37+
"react-native-builder-bob": "^0.41.0"
38+
},
39+
"exports": {
40+
".": {
41+
"source": "./lib/index.ts",
42+
"types": "./dist/typescript/lib/index.d.ts",
43+
"default": "./dist/module/index.js"
44+
},
45+
"./package.json": "./package.json"
46+
},
47+
"react-native-builder-bob": {
48+
"source": "lib",
49+
"output": "dist",
50+
"targets": [
51+
[
52+
"module",
53+
{
54+
"esm": true
55+
}
56+
],
57+
[
58+
"typescript",
59+
{
60+
"tsc": "../../node_modules/.bin/tsc"
61+
}
62+
]
63+
]
64+
},
65+
"eslintIgnore": [
66+
"node_modules/",
67+
"dist/"
68+
]
3569
}

packages/ml/tsconfig.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"extends": "../../tsconfig.packages.base.json",
3+
"compilerOptions": {
4+
"rootDir": ".",
5+
"paths": {
6+
"@react-native-firebase/app/dist/module/internal": ["../app/dist/typescript/lib/internal"],
7+
"@react-native-firebase/app/dist/module/internal/NativeModules": [
8+
"../app/dist/typescript/lib/internal/NativeModules"
9+
],
10+
"@react-native-firebase/app": ["../app/dist/typescript/lib"]
11+
}
12+
},
13+
"include": ["lib/**/*"],
14+
"exclude": ["node_modules", "dist", "__tests__", "**/*.test.ts"]
15+
}

0 commit comments

Comments
 (0)