@nitrotool/errors 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +72 -0
  2. package/package.json +1 -1
  3. package/src/index.ts +0 -31
package/README.md ADDED
@@ -0,0 +1,72 @@
1
+ # @nitrotool/errors
2
+
3
+ Reusable error utilities for H3-based applications.
4
+ This package provides a collection of typed error helpers that create `H3Error` instances with standardized HTTP status
5
+ codes and messages.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ pnpm install @nitrotool/errors
11
+ ```
12
+
13
+ ```bash
14
+ npm install @nitrotool/errors
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ```ts
20
+ import {
21
+ UnauthenticatedError,
22
+ UnauthorizedError,
23
+ NotFoundError,
24
+ EntityAlreadyExistsError,
25
+ ServerError,
26
+ ClientError,
27
+ UnprocessableEntityError,
28
+ BadRequestError,
29
+ } from '@nitrotool/errors';
30
+
31
+ // Example in an API handler
32
+ export default defineEventHandler((event) => {
33
+ const authHeader = getHeader(event, 'authorization');
34
+ if (!authHeader) {
35
+ throw UnauthenticatedError('Missing auth token');
36
+ }
37
+
38
+ if (!isUserAllowed(authHeader)) {
39
+ throw UnauthorizedError('You do not have access to this resource');
40
+ }
41
+
42
+ // ...
43
+ });
44
+ ```
45
+
46
+ ## API
47
+
48
+ | Function | Status Code | Status Message |
49
+ |------------------------------|-------------|---------------------------|
50
+ | `UnauthenticatedError` | `401` | `Unauthenticated` |
51
+ | `UnauthorizedError` | `403` | `Unauthorized` |
52
+ | `NotFoundError` | `404` | `Not found` |
53
+ | `EntityAlreadyExistsError` | `409` | `Entity Already Exists` |
54
+ | `ClientError` | `400` | `Client Error` |
55
+ | `BadRequestError` | `400` | `Bad Request` |
56
+ | `UnprocessableEntityError` | `422` | `Unprocessable Entity` |
57
+ | `ServerError` | `500` | `Internal Server Error` |
58
+
59
+ ## Development
60
+
61
+ Build the package:
62
+
63
+ ```bash
64
+ pnpm build
65
+ ```
66
+
67
+ The package uses [unbuild](https://github.com/unjs/unbuild) for generating `dist/` with both `index.mjs` and type
68
+ definitions.
69
+
70
+ ## License
71
+
72
+ MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nitrotool/errors",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "type": "module",
5
5
  "main": "./dist/index.mjs",
6
6
  "exports": {
package/src/index.ts DELETED
@@ -1,31 +0,0 @@
1
- import { createError } from 'h3';
2
-
3
- export const UnauthenticatedError = (message?: string) =>
4
- createError({ statusCode: 401, statusMessage: 'Unauthenticated', message });
5
- export const UnauthorizedError = (message?: string) =>
6
- createError({ statusCode: 403, statusMessage: 'Unauthorized', message });
7
- export const NotFoundError = (message?: string) =>
8
- createError({ statusCode: 404, statusMessage: 'Not found', message });
9
- export const EntityAlreadyExistsError = (message?: string) =>
10
- createError({
11
- statusCode: 409,
12
- statusMessage: 'Entity Already Exists',
13
- message,
14
- });
15
- export const ServerError = (message?: string) =>
16
- createError({
17
- statusCode: 500,
18
- statusMessage: 'Internal Server Error',
19
- message,
20
- });
21
- export const ClientError = (message?: string) =>
22
- createError({ statusCode: 400, statusMessage: 'Client Error', message });
23
- export const UnprocessableEntityError = (message?: string) =>
24
- createError({
25
- statusCode: 422,
26
- statusMessage: 'Unprocessable Entity',
27
- message,
28
- });
29
-
30
- export const BadRequestError = (message?: string) =>
31
- createError({ statusCode: 400, statusMessage: 'Bad Request', message });