# strictcat

> Support type-safe RestFulAPI life

Latest version **0.0.1-alpha.20** (published 2023-01-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install strictcat
pnpm add strictcat
yarn add strictcat
bun add strictcat
```

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.1-alpha.20 |
| Published | 2023-01-10 |
| First published | 2022-08-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 12.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | yupix <yupi0982@outlook.jp> |
| Maintainers | yupix |

## Links

- npm: https://www.npmjs.com/package/strictcat
- Repository: git@github.com:yupix/strictcat.git
- npm.io page: https://npm.io/package/strictcat

## Recent versions

- 0.0.1-alpha.20 (latest) — 2023-01-10
- 0.0.1-alpha.19 — 2023-01-10
- 0.0.1-alpha.18 — 2023-01-08
- 0.0.1-alpha.17 — 2023-01-08
- 0.0.1-alpha.16 — 2022-12-20
- 0.0.1-alpha.15 — 2022-12-17
- 0.0.1-alpha.14 — 2022-12-17
- 0.0.1-alpha.13 — 2022-12-17
- 0.0.1-alpha.12 — 2022-12-17
- 0.0.1-alpha.11 — 2022-12-16
- 0.0.1-alpha.10 — 2022-12-16
- 0.0.1-alpha.9 — 2022-11-14
- 0.0.1-alpha.8 — 2022-11-14
- 0.0.1-alpha.7 — 2022-09-25
- 0.0.1-alpha.6 — 2022-09-19
- … 5 more at https://npm.io/package/strictcat/versions

## README

# Meowo

このライブラリはTypeScriptでRestAPIを型安全に使う為のAPI Clientを提供します。

## 使い方

```ts
import { apiClient } from "strictcat"

type Schema = {
    resource: {
        "/hello": {
            GET: {
                response: {"message": "hello"};
            };
        };
        "/echo": {
            POST: {
                body: {
                    "message": string
                }
                response: {"message": string}
            }
        }
    };
  };
  
const api = apiClient<Schema>("https://develop.sankosc.co.jp/apitest/api")

api.call('GET', "/hello").then((res) => {
    if (res.type === 'succeeded'){
        console.log(res.data.message)
    }
})

api.call("POST", "/echo", { body: { message: "hello world" }}).then((res) => {
    if (res.type === 'succeeded'){
        console.log(res.data.message)
    }
})
  

```

## 便利機能

### SharedBody

最初にapiClientを初期化する際に渡すことで、全てのリクエストに対して同様のbodyを付与することが出来ます

```ts
apiClient<Schema>("", "POST", {
  sharedBody: { token: "xxxxx" },
});
```

### assertIsSuccess

指定したAPIのレスポンスが成功していると保証します。

```ts
import { apiClient, assertIsSuccess } from "strictcat"

type Schema = {
    resource: {
        "/hello": {
            GET: {
                response: {"message": "hello"};
            };
        };
        "/echo": {
            POST: {
                body: {
                    "message": string
                }
                response: {"message": string}
            }
        }
    };
  };
  
const api = apiClient<Schema>("https://develop.sankosc.co.jp/apitest/api")

api.call('GET', "/hello").then((res) => {
    assertIsSuccess(res)
    console.log(res.data.message)
})
```

## 注意事項

### `content-type`はデフォルトで `text/plain` です

都度変更する場合は以下のようにheaderを指定してください

```ts
await api.call('POST', '/api/re', {headers: {'Content-type': 'application/json'}, body: {text: 'hello world'}})
```

全体で変更する場合は以下のようにApiClientの呼び出し方を変更してください

```ts
const api = apiClient<Schema>("https://develop.sankosc.co.jp/apitest/api", "application/json")
```

### set-cookieが設定されない

[MDN](https://developer.mozilla.org/ja/docs/Web/API/Fetch_API/Using_Fetch) からの引用になりますが、credentialsが`include`になっていないと設定されません。

> credentials オプションを include に設定しない限り、fetch() は次のように動作します。
>
>-    オリジン間リクエストではクッキーを送信しません。
>-    オリジン間のレスポンスでは、送り返されたクッキーを設定しません。
>-    2018 年 8 月現在、既定の資格情報ポリシーは same-origin に変更されています。 Firefox もバージョン 61.0b13 で変更されました）。

以下のように変更してください

```ts
await api.call('POST', '/api/auth/signin', {credentials: 'include', headers: {'content-type': 'application/json'}}, {username, password})
```

## 謝辞

このlibraryは [強力な型補完を行うRestAPI ClientをTypeScriptで実装した](https://blog.wh-plus.co.jp/entry/2020/12/21/104033) というWHITE PLUS TechBlog様の記事を参考に作成したライブラリです。
問題があった際はissue等にご連絡ください。

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