-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(fetch-state-machine): update readme and descriptions
- Loading branch information
Showing
2 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <[email protected]> (https://ali.mihandoost.com)", | ||
"keywords": [ | ||
"context", | ||
|