From 32aaa2fbd969fb49d770e5abedf16198ea4ee0e4 Mon Sep 17 00:00:00 2001 From: Alicja Miloszewska Date: Wed, 2 Oct 2024 11:27:03 +0200 Subject: [PATCH] [OV JS] Add TS definition for AnyMap (#26711) ### Details: - Add definition of `OVAny` to `addon.ts` to encapsulate the concept of [ov::Any](https://docs.openvino.ai/2024/api/c_cpp_api/classov_1_1_any.html) from C++ API in a way that's clear and can be easily updated if the underlying C++ type changes. Improved readability and maintainability. -- 'OVAny' corresponds to Python API [OVAny](https://docs.openvino.ai/2024/api/ie_python_api/_autosummary/openvino.OVAny.html) - In future updates to Node.js API, the OVAny type is expected to be enhanced with additional functionality. - Missed linter changes to `core.test.js` due to upstream merges ### Tickets: - [CVS-149319](https://jira.devtools.intel.com/browse/CVS-149319) --- src/bindings/js/node/lib/addon.ts | 35 +++++++++----------- src/bindings/js/node/tests/unit/core.test.js | 24 +++++++------- 2 files changed, 28 insertions(+), 31 deletions(-) diff --git a/src/bindings/js/node/lib/addon.ts b/src/bindings/js/node/lib/addon.ts index 060af2cfec92e8..24c9d780aa9f7e 100644 --- a/src/bindings/js/node/lib/addon.ts +++ b/src/bindings/js/node/lib/addon.ts @@ -21,6 +21,8 @@ type elementTypeString = | 'f32' | 'string'; +type OVAny = string | number | boolean; + /** * Core represents an OpenVINO runtime Core entity. * @@ -48,7 +50,7 @@ interface Core { compileModel( model: Model, deviceName: string, - config?: { [propertyName: string]: string }, + config?: Record, ): Promise; /** * Asynchronously reads a model and creates a compiled model @@ -67,7 +69,7 @@ interface Core { compileModel( modelPath: string, deviceName: string, - config?: { [propertyName: string]: string }, + config?: Record, ): Promise; /** * A synchronous version of {@link Core.compileModel}. @@ -76,7 +78,7 @@ interface Core { compileModelSync( model: Model, deviceName: string, - config?: { [propertyName: string]: string }, + config?: Record, ): CompiledModel; /** * A synchronous version of {@link Core.compileModel}. @@ -85,7 +87,7 @@ interface Core { compileModelSync( modelPath: string, deviceName: string, - config?: { [propertyName: string]: string }, + config?: Record, ): CompiledModel; /** * It returns a list of available inference devices. @@ -101,7 +103,7 @@ interface Core { * It gets the properties dedicated to device behaviour. * @param propertyName A property name. */ - getProperty(propertyName: string): string | number | boolean; + getProperty(propertyName: string): OVAny; /** * It gets the properties dedicated to device behaviour. @@ -111,7 +113,7 @@ interface Core { getProperty( deviceName: string, propertyName: string, - ): string | number | boolean; + ): OVAny; /** * It returns information on the version of device plugins. * @param deviceName A device name to identify a plugin. @@ -135,7 +137,7 @@ interface Core { importModel( modelStream: Buffer, device: string, - config?: { [key: string]: string | number | boolean }, + config?: Record, ): Promise; /** * A synchronous version of {@link Core.importModel}. @@ -144,7 +146,7 @@ interface Core { importModelSync( modelStream: Buffer, device: string, - config?: { [key: string]: string | number | boolean }, + config?: Record, ): CompiledModel; /** * It reads models from the IR / ONNX / PDPD / TF and TFLite formats. @@ -197,16 +199,13 @@ interface Core { * It sets the properties. * @param properties An object with the property name - property value pairs. */ - setProperty(properties: { [key: string]: string | number | boolean }): void; + setProperty(properties: Record): void; /** * It sets the properties for a device. * @param deviceName The name of a device. * @param properties An object with the property name - property value pairs. */ - setProperty( - deviceName: string, - properties: { [key: string]: string | number | boolean }, - ): void; + setProperty(deviceName: string, properties: Record): void; /** * It queries the device if it supports specified model with the specified * properties. @@ -218,8 +217,8 @@ interface Core { queryModel( model: Model, deviceName: string, - properties?: {[key: string]: string | number | boolean}, - ): {[key: string]: string | number | boolean}; + properties?: Record, + ): { [key: string]: string }; } interface CoreConstructor { new (): Core; @@ -325,7 +324,7 @@ interface CompiledModel { * @param propertyName A string to get the property value. * @returns The property value. */ - getProperty(propertyName: string): string | number | boolean; + getProperty(propertyName: string): OVAny; /** * It creates an inference request object used to infer the compiled model. * @return {InferRequest} @@ -380,9 +379,7 @@ interface CompiledModel { * @param property An object with the key-value pairs. * (property name, property value) */ - setProperty(properties: { - [propertyName: string]: string | number | boolean; - }): void; + setProperty(properties: Record): void; } /** diff --git a/src/bindings/js/node/tests/unit/core.test.js b/src/bindings/js/node/tests/unit/core.test.js index 6cf431a38b5030..f62adda9f90f9c 100644 --- a/src/bindings/js/node/tests/unit/core.test.js +++ b/src/bindings/js/node/tests/unit/core.test.js @@ -12,11 +12,11 @@ describe('ov.Core tests', () => { before(async () => { await isModelAvailable(testModels.testModelFP32); }); - + beforeEach(() => { core = new ov.Core(); }); - + it('Core.setProperty()', () => { const tmpDir = '/tmp'; @@ -83,29 +83,29 @@ describe('ov.Core tests', () => { it('Core.queryModel() with empty parameters should throw an error', () => { assert.throws( () => core.queryModel().then(), - /'queryModel' method called with incorrect parameters./ - ) + /'queryModel' method called with incorrect parameters./, + ); }); it('Core.queryModel() with less arguments should throw an error', () => { assert.throws( - () => core.queryModel("Unexpected Argument").then(), - /'queryModel' method called with incorrect parameters./ - ) + () => core.queryModel('Unexpected Argument').then(), + /'queryModel' method called with incorrect parameters./, + ); }); it('Core.queryModel() with incorrect arguments should throw an error', () => { const model = core.readModelSync(getModelPath().xml); assert.throws( - () => core.queryModel(model, "arg1", "arg2").then(), - /'queryModel' method called with incorrect parameters./ - ) + () => core.queryModel(model, 'arg1', 'arg2').then(), + /'queryModel' method called with incorrect parameters./, + ); }); it('Core.queryModel() should have device in the result values', () => { const model = core.readModelSync(getModelPath().xml); const device = 'CPU'; - const query_model = core.queryModel(model, device); - assert(Object.values(query_model).includes(device)); + const queryModel = core.queryModel(model, device); + assert(Object.values(queryModel).includes(device)); }); });