# @studiokeywi/papi

> PAPI - The Proxy API tool to make API calls friendlier

Latest version **0.1.7** (published 2024-05-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install @studiokeywi/papi
pnpm add @studiokeywi/papi
yarn add @studiokeywi/papi
bun add @studiokeywi/papi
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.7 |
| Published | 2024-05-28 |
| First published | 2023-12-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 86.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | studioKeywi |
| Maintainers | tgdkeywi |

## Links

- npm: https://www.npmjs.com/package/@studiokeywi/papi
- Repository: https://github.com/studiokeywi/papi
- Homepage: https://studiokeywi.github.io/papi
- Issues: https://github.com/studiokeywi/papi/issues
- npm.io page: https://npm.io/package/@studiokeywi/papi

## Recent versions

- 0.1.7 (latest) — 2024-05-28
- 0.1.6 — 2024-05-19
- 0.1.5 — 2024-05-19
- 0.1.4 — 2024-05-19
- 0.1.3 — 2024-05-17
- 0.1.2 — 2024-05-17
- 0.1.1 — 2024-05-03
- 0.1.0 — 2024-05-03
- 0.0.5 — 2023-12-02
- 0.0.4 — 2023-12-02
- 0.0.3 — 2023-12-02
- 0.0.2 — 2023-12-02
- 0.0.1 — 2023-12-01

## README

# PAPI

> The Proxy API tool from studioKeywi for better DX. Zero dependencies. Built with ❤️ and [Bun](https://bun.sh/), [Material for MkDocs](https://squidfunk.github.io/mkdocs-material/), [TypeDoc](https://typedoc.org/), and [Vitest](https://vitest.dev/)

![NPM Version](https://img.shields.io/npm/v/@studiokeywi/papi)
![NPM Type Definitions](https://img.shields.io/npm/types/@studiokeywi/papi)
![npm bundle size (scoped)](https://img.shields.io/bundlephobia/minzip/@studiokeywi/papi)
![NPM Unpacked Size](https://img.shields.io/npm/unpacked-size/@studiokeywi/papi)
![NPM License](https://img.shields.io/npm/l/@studiokeywi/papi)

## Installation

PAPI is available to install through the NPM registry and can be installed by any runtime that is compatible with NPM installs:

### Node

Node projects can install PAPI as a dependency:

```sh
npm install @studiokeywi/papi
```

### Bun

Bun projects can install PAPI as a dependency:

```sh
bun add @studiokeywi/papi
```

### Deno

Deno projects can import PAPI using the `npm:` specifier:

```typescript
import { papi } from 'npm:@studiokeywi/papi';
```

Or by adding to the import map of `deno.json`:

```json
{
  "imports": {
    "papi": "npm:@studiokeywi/papi"
  }
}
```

```typescript
import { papi } from 'papi';
```

> PAPI is a TypeScript first implementation. Along with providing and exporting its own types, exports are provided for the Bun and Deno runtimes as well as an explicit `@studiokeywi/papi/ts` export entry
>
> Under the hood, PAPI is a simple builder syntax combined with a custom [JS Proxy object][mdn proxy] used to wrap around the [Web API `fetch` method][mdn fetch]

> PAPI performs no validation on API responses, nor does PAPI provide any integrated security features. When building an API caller, PAPI allows an API key/token value to be provided automatically in the Authorization header in `Bearer: [token]` format in plaintext. This could be detected by any software that can read or intercept network requests
>
> Beyond this, PAPI allows any standard option that can be provided to the second argument of your runtime's implementation of the Web API `fetch` method. If you are concerned about the security/validity of your network calls, we strongly suggest utilizing other libraries in addition to/instead of PAPI to perform your security/validation

## Usage

PAPI is designed with a chainable builder approach to defining the shape of any API, combined with a chained property approach to calling the various API endpoints. The combination allows for a natural and expressive way to declare and request expected API responses

### PAPI in 4 lines

<!-- prettier-ignore -->
> ```typescript
> import { papi } from '@studiokeywi/papi';
>
> const url = 'https://jsonplaceholder.typicode.com';
> const albumShape = { userId: 0, id: 0, title: '' };
> const allAlbums = await papi(url)                     /* 1. Define a base URL */
>   .path('albums', albums => albums.get([albumShape])) /* 2. Define API endpoints */
>   .build()                                            /* 3. Create API caller */
>   .albums.get();                                      /* 4. Make an API request */
> // typeof allAlbums = { userId: number; id: number; title: string }[]
> ```

### API Builder

PAPI allows API endpoints to be defined as simply or robustly as needed. An expanded example of the JSONPlaceholder API's `/albums` endpoint is demonstrated below:

#### Example Endpoint

> ```typescript
> import { papi } from '@studiokeywi/papi';
>
> const albumShape = { userId: 0, id: 0, title: '' };
> const errShape = {};
> const photoShape = { albumId: 0, id: 0, title: '', url: '', thumbnailUrl: '' };
>
> const apiCaller = papi('https://jsonplaceholder.typicode.com')
>   .path('albums', $albums =>
>     $albums
>       .get([albumShape], $get => $get.error(errShape).queryOpt(albumShape))
>       .post(albumShape, $post => $post.bodyOpt(albumShape).error(errShape))
>       .slug($albumId =>
>         $albumId
>           .get(albumShape, $get => $get.error(errShape))
>           .delete({}, $del => $del.error(errShape))
>           .patch(albumShape, $patch => $patch.bodyOpt(albumShape).error(errShape))
>           .put(albumShape, $put => $put.bodyOpt(albumShape).error(errShape))
>           .path('photos', $photos => $photos.get([photoShape], $get => $get.error(errShape)))
>       )
>   )
>   .build();
> ```

See the [builder docs] for more

### API Caller

The API Caller tool allows arbitrary API endpoints to be called. Following from the example above:

#### Example Caller

> ```typescript
> const data = { title: "studioKeywi's Loudest Hits!" };
> const newAlbum = await apiCaller.albums.post({ data });
> if (!('id' in newAlbum)) throw new Error(`API Post failed and returned ${newAlbum}`);
>
> const photos = await apiCaller.albums[newAlbum.id].photos.get();
> if (!('length' in photos)) throw new Error(`API Get failed and returned ${photos}`);
>
> photos.forEach(photo => {
>   // ...
> });
> ```

See the [caller docs] for more

## API Documentation

Generated by TypeDoc: [API Documentation][api docs]

## Future

Think PAPI is missing a feature? [Open an issue!][git issue]

## Links

[PAPI Docs]  
[PAPI NPMJS]  
[PAPI Repo]

<!-- External Links -->

[api docs]: https://studiokeywi.github.io/papi/api-docs/
[builder docs]: https://studiokeywi.github.io/papi/builder/
[caller docs]: https://studiokeywi.github.io/papi/caller/
[git issue]: https://github.studiokeywi.dev/papi/issues/new
[mdn fetch]: https://developer.mozilla.org/en-US/docs/Web/API/fetch
[mdn proxy]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
[PAPI Docs]: https://studiokeywi.github.io/papi
[PAPI NPMJS]: https://npmjs.com/package/@studiokeywi/papi
[PAPI Repo]: https://github.studiokeywi.dev/papi

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