Registry / http-networking / mg-api-js

mg-api-js

JSON →
library3.0.2jsnpmunverified

The `mg-api-js` library provides a robust JavaScript wrapper for interacting with the MyGeotab API, designed for both client-side browser environments and Node.js applications. Currently stable at version `3.0.2`, the library offers a convenient abstraction over direct API calls, managing authentication via username/password or session IDs. It includes built-in credential storage (localStorage in browsers, a mock in Node.js) with options for custom datastore integration. Key differentiators include its dual-environment support and direct integration with MyGeotab's authentication mechanisms, simplifying common API interaction patterns. Releases occur on an as-needed basis, addressing bugs, security patches, and Node.js compatibility updates, such as recent fixes for Node.js 22+.

npm install mg-api-js
INSTALL
IMPORT
SIG · MG-API-JS
M
mg-api-js
http-networkingjavascriptv3.0.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

GeotabApi
const GeotabApi = require('mg-api-js');
import { GeotabApi } from 'mg-api-js';
This package's documentation primarily shows CommonJS `require()` for Node.js environments. Attempting to use named ESM imports for `GeotabApi` will fail as it exports as a CommonJS default.
GeotabApi
const api = new window.GeotabApi(authentication, options);
import GeotabApi from 'https://cdn.jsdelivr.net/npm/mg-api-js';
For browser environments, the library is loaded via a script tag (e.g., CDN) and exposes `GeotabApi` as a global variable on the `window` object.
GeotabApi (ESM Interop)
import GeotabApi from 'mg-api-js';
const GeotabApi = require('mg-api-js'); // In a native ESM module
While CommonJS `require()` is shown in the documentation, modern Node.js environments (v18+) support `import GeotabApi from 'mg-api-js'` in ESM modules due to CommonJS interop for default exports. Ensure your project is configured for ESM if using this syntax.

Demonstrates how to initialize the GeotabApi client with credentials and options, then make a basic API call to retrieve device information. Uses environment variables for sensitive data.

const GeotabApi = require('mg-api-js'); // Replace with your actual Geotab credentials and server path const authentication = { credentials: { database: process.env.GEOTAB_DATABASE ?? 'your_database_name', userName: process.env.GEOTAB_USERNAME ?? 'your_username', password: process.env.GEOTAB_PASSWORD ?? 'your_password' }, // If not using a sessionId, 'path' can be omitted to default to 'my.geotab.com' // If using sessionId, 'path' is required. path: process.env.GEOTAB_SERVER_PATH ?? 'your.server.address' }; const options = { rememberMe: true, // Store credentials/session in datastore (localStorage or mock) timeout: 30, // API call timeout in seconds // newCredentialStore: new YourCustomDataStore() // Uncomment to provide your own datastore }; // Create an instance of the GeotabApi wrapper const api = new GeotabApi(authentication, options); // Example: Authenticate and then make a simple API call async function runApiCall() { try { console.log('Attempting to authenticate with MyGeotab API...'); // This is a placeholder for a common Geotab API call. // Replace 'Get' and the parameters with an actual method and parameters you need. const result = await api.call('Get', { typeName: 'Device', search: { name: 'YourTestDevice' }, resultsLimit: 1 }); console.log('API Call successful. Devices found:', result); if (result && result.length > 0) { console.log('First device name:', result[0].name); } } catch (error) { console.error('An error occurred during API interaction:', error); if (error.response) { console.error('HTTP Status:', error.response.status); console.error('Response Data:', error.response.data); } } finally { console.log('API interaction attempt finished.'); } } runApiCall();
Debug
Known issues
breakingThe `fullResponse` option no longer provides the full Axios response object directly since v3.0.0. The structure of the response when `fullResponse` is enabled has changed.
fix
Review the updated documentation on 'Full Responses' in the README to adapt your code to the new response structure when `fullResponse` is set to true.
affects: >=3.0.0
breakingThe minimum required Node.js version has been increased to 18.0.0.
fix
Ensure your Node.js environment is updated to version 18.0.0 or higher to use `mg-api-js` v3.0.2 and above.
affects: >=3.0.2
breakingThe `GeotabApi` object no longer accepts an authentication callback function as a parameter during instantiation.
fix
Remove the authentication callback from the `GeotabApi` constructor. Authentication is now handled implicitly or through direct credential/session ID objects.
affects: >=2.0.0
gotchaWhen authenticating with a `sessionId`, the `path` property in the authentication object is a required parameter.
fix
Always provide the full server address in the `path` field of the authentication object if you are using a `sessionId`.
affects: >=2.0.0
gotchaIf providing a username/password for initial authentication and omitting the `path` property, the library will default the server address to `my.geotab.com`.
fix
Explicitly set the `path` property in the authentication object if your Geotab server is not `my.geotab.com` to avoid incorrect routing.
affects: >=1.0.0
gotchaBy default, credentials/sessions are stored in `localStorage` in browsers and a `LocalStorageMock` in Node.js. If you use a custom datastore, it must implement `get()`, `set()`, and `clear()` methods.
fix
To override the default storage, provide an object implementing `get()`, `set()`, and `clear()` methods to the `newCredentialStore` option during API initialization.
affects: >=1.0.0
gotchaA security vulnerability related to the `follow-redirects` dependency was patched in `v2.1.3`.
fix
Upgrade to `mg-api-js` version `2.1.3` or higher to ensure you have the security patches for `follow-redirects`.
affects: <2.1.3
Errors
Common errors & fixes
ReferenceError: localStorage is not defined
Running `mg-api-js` in Node.js 22+ environments before v3.0.2, where a fix for localStorage detection was implemented.
fix
Upgrade to `mg-api-js` version `3.0.2` or higher to fix compatibility with Node.js 22+.
TypeError: GeotabApi is not a constructor
Attempting to use `import { GeotabApi } from 'mg-api-js'` in a Node.js ESM module when `mg-api-js` primarily exposes its API as a CommonJS default export.
fix
Use `import GeotabApi from 'mg-api-js';` for ESM environments, or `const GeotabApi = require('mg-api-js');` for CommonJS environments.
Error: Response object structure changed for fullResponse option
Code written for `mg-api-js` versions prior to `v3.0.0` expecting the `fullResponse` option to return the complete Axios response object, which was altered in `v3.0.0`.
fix
Review the 'Full Responses' section in the library's README for `v3.0.0` onwards and adjust your code to handle the new response object structure.
TypeError: api.min.js missing for nodejs
A packaging error in earlier versions (`v2.0.0`) that prevented `api.min.js` from being correctly bundled for Node.js usage.
fix
Upgrade to `mg-api-js` version `2.0.1` or higher to resolve the missing `api.min.js` file for Node.js.
Error: Empty error when making multi-call
A bug in earlier versions (`v2.1.0`) causing multi-calls to return an empty error object.
fix
Upgrade to `mg-api-js` version `2.1.1` or higher to fix the bug with empty errors on multi-calls.
Upgrade
Version history
3.0.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
16 hits · last 30 days
node
10
Amazon
1
OpenAI (training)
1
Resources
mg-api-js — npm install mg-api-js · libregistry