Skip to content

Commit

Permalink
docs(fetch-state-machine): update readme and descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
alimd committed Sep 15, 2024
1 parent 7477341 commit 44ad6be
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
93 changes: 93 additions & 0 deletions packages/fetch-state-machine/README.md
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.
2 changes: 1 addition & 1 deletion packages/fetch-state-machine/package.json
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",
Expand Down

0 comments on commit 44ad6be

Please sign in to comment.