# @tectonique/api-standards

> TypeScript utilities for creating end to end type safe APIs with RFC 7807 (Problem Details) based error handling.

Latest version **1.0.0** (published 2023-02-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install @tectonique/api-standards
pnpm add @tectonique/api-standards
yarn add @tectonique/api-standards
bun add @tectonique/api-standards
```

## 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 | 1.0.0 |
| Published | 2023-02-18 |
| First published | 2023-02-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 31.7 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Peter Kuhmann |
| Maintainers | hedgehogs-mind |
| Keywords | typesafety, envelope, api, problemdetails, rfc7807, 7807 |

## Links

- npm: https://www.npmjs.com/package/@tectonique/api-standards
- Repository: https://github.com/tectonique/api-standards
- Homepage: https://github.com/tectonique/api-standards#readme
- Issues: https://github.com/tectonique/api-standards/issues
- npm.io page: https://npm.io/package/@tectonique/api-standards

## Dependencies (1)

- [uuid](https://npm.io/package/uuid.md) ^9.0.0

## Alternatives

- [replicas-cli](https://npm.io/package/replicas-cli.md) — 3.0K weekly downloads
- [env-contract](https://npm.io/package/env-contract.md) — 133 weekly downloads
- [@openveo/api](https://npm.io/package/@openveo/api.md) — 61 weekly downloads
- [@ryniaubenpm2/cumque-error-reiciendis](https://npm.io/package/@ryniaubenpm2/cumque-error-reiciendis.md) — 54 weekly downloads
- [ts-global-type-extra](https://npm.io/package/ts-global-type-extra.md) — 11 weekly downloads

## Recent versions

- 1.0.0 (latest) — 2023-02-18
- 0.0.13 — 2023-02-05
- 0.0.12 — 2023-02-05
- 0.0.11 — 2023-02-05
- 0.0.10 — 2023-02-05
- 0.0.9 — 2023-02-05
- 0.0.8 — 2023-02-05
- 0.0.7 — 2023-02-05
- 0.0.6 — 2023-02-05
- 0.0.5 — 2023-02-05
- 0.0.4 — 2023-02-04
- 0.0.3 — 2023-02-04
- 0.0.2 — 2023-02-04
- 0.0.1 — 2023-02-04

## README

<p align="center">
  <br>
  <br>
  <img src=".assets/logo.png" alt="Logo of library api-standards" width="400">
  <br>
  <br>
</p>
<hr>

<p align="center">
  <a href="https://www.npmjs.com/package/@tectonique/api-standards">
    <img alt="npm" src="https://img.shields.io/npm/v/@tectonique/api-standards?color=%23e62770&label=NPM">
  </a>
  <a href="https://codecov.io/gh/tectonique/api-standards" > 
   <img src="https://codecov.io/gh/tectonique/api-standards/branch/main/graph/badge.svg?token=L5O8JPNGQZ"/> 
  </a>
</p>

# 🌩 API Standards

This library provides utilities for creating end to end type safe APIs.

The library advocates:

- API response envelopes
- Error handling with [RFC 7807 Problem Details 🔗](https://www.rfc-editor.org/rfc/rfc7807)

# 📖 Table of contents

<!-- TOC -->
* [🌩 API Standards](#-api-standards)
* [📖 Table of contents](#-table-of-contents)
* [📦 NPM Package](#-npm-package)
* [💾 Installation](#-installation)
* [🏁 Goal](#-goal)
* [📑 Documentation](#-documentation)
  * [⚠️ Problem Details](#-problem-details)
  * [📨 Response Envelopes](#-response-envelopes)
* [📜 Changelog](#-changelog)
* [🦔 Author](#-author)
<!-- TOC -->

# [📦 NPM Package](https://www.npmjs.com/package/@tectonique/api-standards)

# 💾 Installation

Using npm:
```bash
npm i @tectonique/api-standards
```

Using yarn:
```
yarn add @tectonique/api-standards
```

# 🏁 Goal
The goal of the library is to help you create type safe code like this:
```typescript
import { ResponseEnvelopes } from "@tectonique/api-standards"

// Import response and error (problem detail) types
import { ProblemDetailSuperType } from "@backend/ProblemDetailSuperType"
import { API_GetUsers_Response } from "@backend/ApiResponses"

// Make the API call
const data = await axios.get("/api/users")
  .then((response) => response.data)
  .catch((error) => error.response.data)

// Check and inspect envelope
if ( ResponseEnvelopes.isEnvelope(data) ) {
  const envelope = data as ResponseEnvelopes.Envelope<
    ProblemDetailSuperType,
    API_GetUsers_Response
  >
  
  // Success envelope ... obviously ^^
  if ( envelope.success ) {
    console.log(
      "User email adresses:",
      envelope.payload.map(user => user.email).join(', ')
    )
    
  // Problem detail
  } else if ( envelope.type === "unauthorized" ) {
    throw new Error("Session expired")
    
  } else {
    throw new Error("Unhandled problem detail: " + envelope.type)
  }
  
} else {
  throw new Error("Didn't receive an envelope")
}
```

# 📑 Documentation

## [⚠️ Problem Details](./ProblemDetails/README.md)

## [📨 Response Envelopes](./ResponseEnvelopes/README.md)

# [📜 Changelog](CHANGELOG.md)

# 🦔 Author
<p align="center">
  <b>Peter Kuhmann</b>
  <br>
  <a href="https://github.com/hedgehogs-mind">GitHub: hedgehogs-mind</a>
  <br>
  <br>
  <br>
  <b>Tectonique</b>
  <br>
  <br>
  <img src=".assets/tectonique-small.png" alt="Tectonique logo" width="300">
</p>

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