@nitrotool/errors 0.0.5 → 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.
- package/README.md +72 -0
- package/dist/index.d.mts +12 -0
- package/dist/index.d.ts +12 -0
- package/package.json +2 -1
- 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/dist/index.d.mts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import * as h3 from 'h3';
|
|
2
|
+
|
|
3
|
+
declare const UnauthenticatedError: (message?: string) => h3.H3Error<unknown>;
|
|
4
|
+
declare const UnauthorizedError: (message?: string) => h3.H3Error<unknown>;
|
|
5
|
+
declare const NotFoundError: (message?: string) => h3.H3Error<unknown>;
|
|
6
|
+
declare const EntityAlreadyExistsError: (message?: string) => h3.H3Error<unknown>;
|
|
7
|
+
declare const ServerError: (message?: string) => h3.H3Error<unknown>;
|
|
8
|
+
declare const ClientError: (message?: string) => h3.H3Error<unknown>;
|
|
9
|
+
declare const UnprocessableEntityError: (message?: string) => h3.H3Error<unknown>;
|
|
10
|
+
declare const BadRequestError: (message?: string) => h3.H3Error<unknown>;
|
|
11
|
+
|
|
12
|
+
export { BadRequestError, ClientError, EntityAlreadyExistsError, NotFoundError, ServerError, UnauthenticatedError, UnauthorizedError, UnprocessableEntityError };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import * as h3 from 'h3';
|
|
2
|
+
|
|
3
|
+
declare const UnauthenticatedError: (message?: string) => h3.H3Error<unknown>;
|
|
4
|
+
declare const UnauthorizedError: (message?: string) => h3.H3Error<unknown>;
|
|
5
|
+
declare const NotFoundError: (message?: string) => h3.H3Error<unknown>;
|
|
6
|
+
declare const EntityAlreadyExistsError: (message?: string) => h3.H3Error<unknown>;
|
|
7
|
+
declare const ServerError: (message?: string) => h3.H3Error<unknown>;
|
|
8
|
+
declare const ClientError: (message?: string) => h3.H3Error<unknown>;
|
|
9
|
+
declare const UnprocessableEntityError: (message?: string) => h3.H3Error<unknown>;
|
|
10
|
+
declare const BadRequestError: (message?: string) => h3.H3Error<unknown>;
|
|
11
|
+
|
|
12
|
+
export { BadRequestError, ClientError, EntityAlreadyExistsError, NotFoundError, ServerError, UnauthenticatedError, UnauthorizedError, UnprocessableEntityError };
|
package/package.json
CHANGED
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 });
|