# typebox-client

> typebox-client

Latest version **1.0.25** (published 2023-09-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install typebox-client
pnpm add typebox-client
yarn add typebox-client
bun add typebox-client
```

## 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.25 |
| Published | 2023-09-11 |
| First published | 2023-06-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 25.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 64 |
| Author | flodlc |
| Maintainers | flodlc |
| Keywords | typebox, fastify, typescript, client, api, node, js |

## Links

- npm: https://www.npmjs.com/package/typebox-client
- Repository: https://github.com/flodlc/typebox-client
- Homepage: https://github.com/flodlc/typebox-client#readme
- Issues: https://github.com/flodlc/typebox-client/issues
- npm.io page: https://npm.io/package/typebox-client

## Dependencies (1)

- [axios](https://npm.io/package/axios.md) ^1.4.0

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.0.25 (latest) — 2023-09-11
- 1.0.24 — 2023-09-02
- 1.0.23 — 2023-09-02
- 1.0.22 — 2023-09-02
- 1.0.21 — 2023-07-03
- 1.0.20 — 2023-07-03
- 1.0.19 — 2023-07-03
- 1.0.18 — 2023-07-03
- 1.0.17 — 2023-07-03
- 1.0.16 — 2023-07-03
- 1.0.15 — 2023-07-02
- 1.0.14 — 2023-06-26
- 1.0.13 — 2023-06-26
- 1.0.12 — 2023-06-26
- 1.0.11 — 2023-06-26
- … 3 more at https://npm.io/package/typebox-client/versions

## README

<h1 align="center">typebox-client</h1>
<p align="center"><a href="https://git.io/typing-svg"><img src="https://readme-typing-svg.demolab.com?font=Fira+Code&size=18&duration=2000&pause=2000&center=true&width=540&height=80&lines=First+class+api+client+for+fastify." alt="Typing SVG" /></a></p>

### typebox-client is a simple client for Typebox schema based api using:

- Fully written in TypeScript
- Typebox for easy json-schema writting
- axios (more http client supported soon) as a http client

---

Table of Contents:

- [Installation](#installation)
- [Usage](#usage)

---

## Installation

To get started, install typebox-client using npm or yarn:

```sh
npm install typebox-client
# or
yarn add typebox-client
```

## Usage

### Route definitions and api calls

```typescript
import { loadRouteDefinitions, createRouteDefinition, Type } from 'typebox-client';

const definitions = {
  getUser: createRouteDefinition({
    method: 'GET',
    // Id parameter is dynamicaly injected in the final url.
    url: `/user/:id`,
    schema: {
      params: Type.Object({
        id: Type.String(),
      }),
      response: {
        200: Type.Array(
          Type.Object({
            name: Type.String(),
            age: Type.Number(),
          })
        ),
      },
    },
  }),
};

const [createClient, schemas] = loadRouteDefinitions(definitions);

// fastify
server.get('/user', { schema: schemas.getUser }, async (request, response) => {
  response.status(200).send([{ name: 'John', age: 30 }]);
});

// client
const apiClient = createClient(axios.create({ baseURL: 'localhost' }));

async () => {
  const user = await apiClient.getUser({ params: { id: 'my-user-id' } }).call();
  // { name: 'John', age: 30 }
};
```

### Inject URL params

typebox-client use the `/my-url/:my-param` syntax to inject given parameters in the final url.

### Access the final URL

Sometime, instead of calling the client method you will want to get the computed url. It's usefull to use it as a link href for instance.

```typescript
const url = await apiClient.getUser({ params: { id: 'my-user-id' } }).url;
// https://localhost/user/my-user-id
```

---

That's it! You can now use typebox-client to create fully typed and safe api client.

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