UNPKG

mdl-identifi-ts

Version:

TypeScript client SDK for the headless micro CRM, Minddale.

91 lines (60 loc) 3.36 kB
![Minddale](https://storage.googleapis.com/fcx-logos/minddale/Mindale-dark-tagline.png "Building digitally immortal brands!") # mdl-identifi-ts [mdl-identifi-ts](https://minddale.com) is a TypeScript utility library provided by [Firecodex](https://firecodex.com) to simplify local storage operations for your JavaScript/TypeScript projects, helping you manage data persistence for digitally immortal brands! ## Library This `mdl-identifi-ts` (v1) is a TypeScript/JavaScript client library designed to handle local storage operations, such as saving and retrieving JSON objects or string values, with type safety and error handling. ## Setup This library is a lightweight module that can be easily integrated into your JavaScript or TypeScript project. ### Install Run the following commands to install the library: ```bash cd your-app npm i mdl-identifi-ts --save ``` ### Use Once registered with Minddale, get your API Key generated to connect with the platform. > **Note**: Ensure you store the API Key securely in an environment file or another secure method. If you are not making requests from your registered domain (website), ensure you set the registered client domain in the request header `x-mdl-domain`. The `mdl-identifi-ts` library provides methods to save and retrieve data from the browser's local storage. Below are examples of how to use the core functionalities. ```typescript // Import the IdentifiClient and dotenv import { IdentifiClient } from 'mdl-identifi-ts'; import dotenv from 'dotenv'; // Load environment variables dotenv.config(); // Retrieve API Key from environment const apiKey = process.env.MDL_API_KEY; // Initialize the client with API Key const identifiClient = new IdentifiClient(apiKey); // Save a JSON object to local storage const jsonData = { userId: 123, username: "john_doe" }; identifiClient.saveJsonToLocalStorage("userData", jsonData); // Save a string value to local storage identifiClient.saveValueToLocalStorage("theme", "dark"); // Retrieve an item from local storage const userData = identifiClient.getItemFromLocalStorage<{ userId: number, username: string }>("userData"); if (userData) { console.log(userData.userId); // Output: 123 console.log(userData.username); // Output: john_doe } else { console.log("No data found for key: userData"); } const theme = identifiClient.getItemFromLocalStorage<string>("theme"); if (theme) { console.log(theme); // Output: dark } else { console.log("No data found for key: theme"); } ``` ### Core Functionalities The `IdentifiClient` class provides the following methods: - **`saveJsonToLocalStorage(key: string, jsonObject: object): void`** Saves a JSON object to local storage under the specified key. The object is stringified before being stored. - **`saveValueToLocalStorage(key: string, value: string): void`** Saves a string value to local storage under the specified key. - **`getItemFromLocalStorage<T = any>(key: string): T | null`** Retrieves an item from local storage by key. Attempts to parse the value as JSON; if parsing fails, returns the raw value. Returns `null` if the key does not exist. Supports TypeScript generics for type-safe retrieval. ## Support We at Firecodex are happy to assist with any queries or suggestions. Feel free to reach out to us: > support@minddale.com Happy Coding!