# @dry-express-responses/types

> ExpressJS middleware to reduce boilerplate in response creation

Latest version **0.0.37** (published 2022-09-08) · ISC license · 0 weekly downloads

## Install

```sh
npm install @dry-express-responses/types
pnpm add @dry-express-responses/types
yarn add @dry-express-responses/types
bun add @dry-express-responses/types
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.37 |
| Published | 2022-09-08 |
| First published | 2022-08-26 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 23 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Omri Levy |
| Maintainers | omrilevy0197 |
| Keywords | express, response, middleware, http-status-codes, typescript, http, status |

## Links

- npm: https://www.npmjs.com/package/@dry-express-responses/types
- Repository: https://github.com/Omri-Levy/dry-express-responses
- Homepage: https://github.com/Omri-Levy/dry-express-responses#readme
- Issues: https://github.com/Omri-Levy/dry-express-responses/issues
- npm.io page: https://npm.io/package/@dry-express-responses/types

## Dependencies (2)

- [zod](https://npm.io/package/zod.md) ^3.18.0
- [http-status-codes](https://npm.io/package/http-status-codes.md) ^2.2.0

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.0.37 (latest) — 2022-09-08
- 0.0.36 — 2022-09-05
- 0.0.35 — 2022-09-05
- 0.0.34 — 2022-08-29
- 0.0.33 — 2022-08-27
- 0.0.32 — 2022-08-26
- 0.0.31 — 2022-08-26
- 0.0.30 — 2022-08-26

## README

# Credit to packages used in this project

- zod - https://github.com/colinhacks/zod
- yup - https://github.com/jquense/yup
- http-status-codes
	- https://github.com/prettymuchbryce/http-status-codes
- express - https://github.com/expressjs/express
- express-async-errors
	- https://github.com/davidbanham/express-async-errors

### Description

A small ExpressJS middleware written in TypeScript that wraps around
http-status-codes to send consistent responses, reducing the instances
of
res.status, res.send and having to pass a status code and
its associated message.

### How to use

#### Step 1.1

Install dry-express-responses

```bash
npm install @dry-express-responses/core @dry-express-responses/types
yarn add @dry-express-responses/core @dry-express-responses/types
pnpm add @dry-express-responses/core @dry-express-responses/types
```

#### Step 1.2

In addition to responses, an optional global error handler middleware
with errors, and validation errors using zod or yup is also available.
The zod/yup packages are only required for validation errors,
@dry-express-responses/errors can work on its own, for validation
errors both @dry-express-responses/errors and
@dry-express-responses/zod (or yup) are required.

```bash
npm install @dry-express-responses/errors @dry-express-responses/zod @dry-express-responses/types
yarn add @dry-express-responses/errors @dry-express-responses/zod @dry-express-responses/types
pnpm add @dry-express-responses/errors @dry-express-responses/zod @dry-express-responses/types
```

#### Step 2

Create a file in src called dry-express-responses.d.ts and copy the
following content, this is required for the types to work.

```typescript
import {
	BadRequest,
	Created,
	Forbidden,
	InternalServerError,
	NotFound,
	Ok,
	Unauthorized,
} from '@dry-express-responses/types';

declare global {
	declare namespace Express {
		export interface Response {
			ok: Ok;

			created: Created;

			badRequest: BadRequest;

			unauthorized: Unauthorized;

			forbidden: Forbidden;

			notFound: NotFound;

			internalServerError: InternalServerError;
		}
	}
}
```

#### Example usage

```typescript
import {dryExpressResponses} from '@dry-express-responses/core';
import {
	dryExpressErrors,
	BadRequestError
} from '@dry-express-responses/errors';
import {ZodValidationError} from '@dry-express-responses/zod';
import {
	zParse,
	zSafeParse
} from '@dry-express-responses/zod-request-validation';
import express from 'express';
// Can be used with yup instead
import z from 'zod';

const app = express();

// Can use either or both of the middleware
app.use(dryExpressResponses);

app.get('/', (req, res) => {
	res.badRequest({
		data: {
			email: 'foo@bar.com',
		},
		message: 'This is a bad request',
		errors: [
			{
				field: 'email',
				message: 'invalid email',
			},
		],
	});
});

app.post('/oops', (req, res) => {
	throw new BadRequestError();
});

app.put('/validate', (req, res) => {
	// Validate body/query/params directly and return a typesafe version
	const result = zSafeParse({
		body: z.object({
			email: z.string(),
		}),
		query: z.object({
			id: z.string(),
		}),
		params: z.object({
			color: z.string(),
		}),
	}, req);
	// Or just throw ZodValidationError directly on validation error using zParse
	const {body} = zParse({
		body: z.object({
			email: z.string(),
		}),
		query: z.object({
			id: z.string(),
		}),
		params: z.object({
			color: z.string(),
		}),
	}, req);

	// Lets zod infer that data exists if success = true
	if (!result.success) {
		// Uses express-async-errors under the hood
		throw new ZodValidationError(result.error);
	}

	return res.ok({
		// Both are typesafe!
		data: body.email ?? result.body.email,
	});
});

// Has to be placed after all the routes and middleware
// Has to be placed after all the routes and middleware
app.use(dryExpressErrors({
	// Optional, defaults to console.error
logger: console.log,
	// Optional data to pass to the response (can be any key/value)
version: 5
}));

app.listen(3000, () => console.log('listening on port 3000'));
```

### Don't want to use a middleware or overload response?

```typescript
import {dried} from '@dry-express-responses/core';
import express from 'express';

const app = express();

app.get('/', (req, res) => {
	dried(res).ok();
});

// Pass response once!
app.get('/reuse', (req, res) => {
	const dry = dried(res);

	if (req.query.foo === 'bar') {
		return dry.badRequest({
			message: 'Wrong foo',
			errors: [
				{
					field: 'foo',
					message: 'Received bar instead of foo!',
				},
			],
		});
	}

	if (!req.query.foo) {
		return dry.notFound({
			message: 'Foo was not found :(',
		});
	}

	dry.ok();
});

app.listen(3000, () => console.log('listening on port 3000'));
```

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