# @tdc-cl/async-stuff

> Declarative handling of asynchronous stuff related to REST APIs

Latest version **2.8.1** (published 2022-02-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install @tdc-cl/async-stuff
pnpm add @tdc-cl/async-stuff
yarn add @tdc-cl/async-stuff
bun add @tdc-cl/async-stuff
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.8.1 |
| Published | 2022-02-07 |
| First published | 2021-01-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=10 |
| Dependencies | 5 |
| Unpacked size | 412.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Robin40 |
| Maintainers | tdc-cl |

## Links

- npm: https://www.npmjs.com/package/@tdc-cl/async-stuff
- Repository: https://github.com/Robin40/async-stuff
- Homepage: https://github.com/Robin40/async-stuff#readme
- Issues: https://github.com/Robin40/async-stuff/issues
- npm.io page: https://npm.io/package/@tdc-cl/async-stuff

## Dependencies (5)

- [qs](https://npm.io/package/qs.md) ^6.10.3
- [lodash](https://npm.io/package/lodash.md) ^4.17.20
- [auto-bind](https://npm.io/package/auto-bind.md) ^4.0.0
- [lodash-es](https://npm.io/package/lodash-es.md) ^4.17.20
- [@types/lodash](https://npm.io/package/@types/lodash.md) ^4.14.167

## Recent versions

- 2.8.1 (latest) — 2022-02-07
- 2.8.0 — 2022-02-07
- 2.7.0 — 2021-11-25
- 2.6.0 — 2021-11-16
- 2.5.0 — 2021-10-03
- 2.4.0 — 2021-08-19
- 2.3.6 — 2021-08-18
- 2.3.5 — 2021-08-18
- 2.3.4 — 2021-08-16
- 2.3.3 — 2021-08-11
- 2.3.2 — 2021-08-09
- 2.3.1 — 2021-07-20
- 2.3.0 — 2021-07-20
- 2.2.0 — 2021-07-20
- 2.1.4 — 2021-07-13
- … 15 more at https://npm.io/package/@tdc-cl/async-stuff/versions

## README

# @tdc-cl/async-stuff

## Installation

Install async-stuff and its peer dependencies

> TypeScript type definitions included

### npm

```shell
npm install @tdc-cl/async-stuff decimal.js js-joda superstruct
```

### yarn

```shell
yarn add @tdc-cl/async-stuff decimal.js js-joda superstruct
```

## Basic example

```typescript
// types.ts

export interface EmailPass {
    email: string;
    pass: string;
}

export interface Tokens {
    access: string;
    refresh: string;
}

export interface Todo {
    id: number;
    title: string;
    done: boolean;
}
```

```typescript
// endpoints.ts

import { Server } from '@tdc-cl/async-stuff';
import { EmailPass, Tokens, Todo } from './types';

const server = new Server('http.../api');

export const login = server.endpoint.post<EmailPass, Tokens>('login/');

export const getAllTodos = server.endpoint.getAll<Todo[]>('/user/todos');
export const todoById = server.endpoint.get<number, Todo>('user/todos/');
export const createTodo = server.endpoint.post<Omit<Todo, 'id'>, void>('/user/todos/');
export const replaceTodo = server.endpoint.put<number, Omit<Todo, 'id'>, Todo>('user/todos');
export const patchTodo = server.endpoint.patch<number, Partial<Omit<Todo, 'id'>>, Todo>('/user/todos');
export const deleteTodo = server.endpoint.delete<number, void>('user/todos');
```

```typescript
// LoginForm.tsx

import { login } from './endpoints';

export default function LoginForm() {
    async function onSubmit() {
        const tokens = await login.fetch({ user: ..., pass: ... });
        // `tokens` is of type `Tokens` as defined in the endpoint
        ...
    }
    ...
}
```


```typescript
// Todos.tsx

import { getAllTodos, todoById, createTodo, replaceTodo, patchTodo, deleteTodo } from './endpoints';
import { Todo } from './types';

...

const todos: Todo[] = await getAllTodos.fetch();
const newTodo: Todo = await createTodo.fetch({ title: 'foo', done: false });
const thatTodo: Todo = await todoById.fetch(newTodo.id);
const anotherTodo: Todo = await replaceTodo.fetch(thatTodo.id, { title: 'bar', done: false });
const doneTodo: Todo = await patchTodo.fetch(anotherTodo.id, { done: true });
await deleteTodo.fetch(doneTodo.id);
```

> Note: authentication tokens will be handled in a future version

---
_Source: https://npm.io/package/@tdc-cl/async-stuff · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
