From 44ad6be72bae6f572349b21d1536a0f21aaaef70 Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Sun, 15 Sep 2024 16:42:56 +0330 Subject: [PATCH] docs(fetch-state-machine): update readme and descriptions --- packages/fetch-state-machine/README.md | 93 +++++++++++++++++++++++ packages/fetch-state-machine/package.json | 2 +- 2 files changed, 94 insertions(+), 1 deletion(-) diff --git a/packages/fetch-state-machine/README.md b/packages/fetch-state-machine/README.md index 63704b8e..94b26ac9 100644 --- a/packages/fetch-state-machine/README.md +++ b/packages/fetch-state-machine/README.md @@ -1 +1,94 @@ # @alwatr/fetch-state-machine + +A powerful TypeScript library for managing asynchronous fetch requests with a built-in state machine. + +## Features + +* **Streamlined Fetch Handling:** Simplify the complexities of fetch requests with clear state management. +* **State Machine Integration:** Built-in state machine tracks request progress (initial, loading, failed, complete). +* **Flexible Customization:** Extend base classes to tailor behavior to your specific needs. +* **JSON Support:** Specialized class for handling JSON responses (`AlwatrJsonFetchStateMachine`). +* **TypeScript Support:** Written in TypeScript with full type definitions for improved code quality and developer experience. +* **Built-in Logging:** Integrated logging for debugging and monitoring fetch operations. + +## Installation + +```bash +npm install @alwatr/fetch-state-machine +``` + +## Usage + +### Basic Fetch + +```typescript +import {AlwatrFetchStateMachine} from '@alwatr/fetch-state-machine'; + +const fetcher = new AlwatrFetchStateMachine({ + url: '/api/data', + method: 'GET', +}); + +fetcher.request(); + +// Access state and response +console.log('Current state:', fetcher.state); // 'loading' + +// Wait for completion +fetcher.subscribe(({state}) => { + if (state === 'complete') { + console.log('Response:', fetcher.rawResponse); + } else if (state === 'failed') { + console.error('Fetch failed!'); + } +}); +``` + +### JSON Fetch + +```typescript +import {AlwatrJsonFetchStateMachine} from '@alwatr/fetch-state-machine'; + +const jsonFetcher = new AlwatrJsonFetchStateMachine<{data: string[]}>({ + url: '/api/users', + method: 'GET', +}); + +jsonFetcher.request(); + +// Wait for JSON response +jsonFetcher.subscribe(({state}) => { + if (state === 'complete') { + console.log('JSON Data:', jsonFetcher.jsonResponse); + } +}); +``` + +## API + +### Core Classes + +* **`AlwatrFetchStateMachineBase`** + * Base class providing core fetch functionality and state management. + * Extend this class to create custom fetch state machines with additional states or events. + +* **`AlwatrFetchStateMachine`** + * Concrete implementation for handling general fetch requests. + * Provides access to `state` and `rawResponse`. + +* **`AlwatrJsonFetchStateMachineBase`** + * Base class for handling fetch requests that return JSON data. + * Automatically parses JSON responses. + * Extend this class for custom JSON fetch state machines. + +* **`AlwatrJsonFetchStateMachine`** + * Concrete implementation for fetching and parsing JSON data. + * Provides access to `state`, `jsonResponse`, and `rawResponse`. + +## Contributing + +Contributions are welcome! Please read the contributing guidelines before submitting a pull request. + +## License + +This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. diff --git a/packages/fetch-state-machine/package.json b/packages/fetch-state-machine/package.json index 0f3d5786..40d7392f 100644 --- a/packages/fetch-state-machine/package.json +++ b/packages/fetch-state-machine/package.json @@ -1,7 +1,7 @@ { "name": "@alwatr/fetch-state-machine", "version": "1.2.6", - "description": "Elegant powerful context manager base on alwatr signal, written in tiny TypeScript, ES module.", + "description": "A powerful TypeScript library for managing asynchronous fetch requests with a built-in state machine.", "author": "S. Ali Mihandoost (https://ali.mihandoost.com)", "keywords": [ "context",