# yupify

> be fast, be type-safe

Latest version **0.0.5** (published 2023-05-29) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.5 |
| Published | 2023-05-29 |
| First published | 2022-10-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 26.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Artemiy Davydov |
| Maintainers | angonoka |
| Keywords | fastify, yup, validation, backend |

## Links

- npm: https://www.npmjs.com/package/yupify
- Repository: https://github.com/fax1ty/yupify
- Homepage: https://github.com/fax1ty/yupify#readme
- Issues: https://github.com/fax1ty/yupify/issues
- npm.io page: https://npm.io/package/yupify

## Alternatives

- [@regle/core](https://npm.io/package/@regle/core.md) — 47.0K weekly downloads
- [typeof-arguments](https://npm.io/package/typeof-arguments.md) — 12.5K weekly downloads
- [@lokalise/projects-engine-contracts](https://npm.io/package/@lokalise/projects-engine-contracts.md) — 978 weekly downloads
- [@osjwnpm/nam-laboriosam-quibusdam](https://npm.io/package/@osjwnpm/nam-laboriosam-quibusdam.md) — 70 weekly downloads
- [@oridune/validator](https://npm.io/package/@oridune/validator.md) — 16 weekly downloads

## Recent versions

- 0.0.5 (latest) — 2023-05-29
- 0.0.4 — 2023-01-25
- 0.0.3 — 2022-10-18
- 0.0.2 — 2022-10-10
- 0.0.1 — 2022-10-10

## README

![cover](cover.png)

# yupify - be fast, be type-safe

In fact, it's just a plugin for [`fastify`](https://github.com/fastify/fastify) that combinig validation schema declaration and typing for any request. No more, no less

#### Before

```ts
fastify.post<{ Querystring: { hello: string } }>(
  "/",
  { schema: { querystring: { hello: string } } },
  ({ query: { hello } }) => {
    return hello;
  }
);
```

#### After

```ts
yupify.post(
  "/",
  { [Yupify.Query]: yup.object({ hello: yup.string() }) },
  ({ [Yupify.Query]: { hello } }) => {
    return hello;
  }
);
```

## When to use it

- You want pretty-damn easy solution to add schema validation to your fastify app
- You don't want to add messy generics to your codebase

## When NOT to use it

- You need to have maximum control over the server (#todo)

## Small docs

### How to use it

1. Register the plugin

```ts
const server = fastify();
server.register(yupifyPlugin);
```

2. Create Yupify instance

```ts
const yupify = createYupify(server);
```

3. Use it at your own

```ts
yupify.get(
  "/hello",
  {
    [Yupify.Query]: yup.object({
      echo: yup.string(),
    }),
  },
  ({ [Yupify.Query]: { echo } }) => echo
);
```

> You will need to install yup. Make sure it is version 1.0.0-beta.7 and above

### Available "chunks"

`Yupify.Body`, `Yupify.Query`, `Yupify.Params`, `Yupify.Headers`, `Yupify.Response`

### Available methods

`yupify.get`, `yupify.head`, `yupify.post`, `yupify.put`, `yupify.delete`, `yupify.options`, `yupify.patch`

### How to create a hook

#### Simple example

```ts
import { HookConstructor } from "yupify";

// Everything will be type-safe, relax
export const withSecret: HookConstructor = (cb) => {
  return (req, res) => {
    const token = req.headers.authorization;
    if (!token) return res.code(401).send();
    if (token !== process.env.SECRET) return res.code(401).send();
    return await cb(req, res);
  };
};
```

#### Advanced example

```ts
import { HookConstructor } from "yupify";

export const withAuth: HookConstructor<
  // Args tuple
  [JWTVerifyGetKey],
  // Object that you can attach to .req
  { user: UserData }
> = (cb, [jwks]) => {
  return async (req, res) => {
    const token = req.headers.authorization;
    if (!token) return res.code(401).send();
    const user = await getUserDataFromToken(token, jwks);
    if (!user) return res.code(401).send();
    return await cb({ ...req, user }, res);
  };
};
```

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