Skip to content

Commit

Permalink
Merge pull request #4 from dancastillo/v0.0.3-docs-and-types
Browse files Browse the repository at this point in the history
fix: documentation and types exporting
  • Loading branch information
dancastillo authored Nov 21, 2024
2 parents 54b9b09 + 380cd4b commit 9dc43ce
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @dancastillo/error

## 0.0.3

### Patch Changes

- Update documentation and exporting types

## 0.0.2

### Patch Changes
Expand Down
30 changes: 23 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,46 @@ A small utility for generating consistent errors

### Install

npm

```bash
npm install @dancastillo/error
```

yarn

```bash
npm add @dancastillo/error
```

pnpm

```bash
pnpm install @dancastillo/error
```

### Usage

```
createError(code, message [, details [, statusCode [, error]]])
createError(code, message [, details [, meta]])
```

- `code` (`string`, required) - The error code, you can access it via`error.code`. For consistency, it is recommended using [ErrorCode](./src/types/index.ts)
- `message` (`string`, required) - The error message. This can be customized to use interpolated strings for formatting the message.
- `code` (`number | string`, required) - The error code, you can access it via`error.code`. For http error, it is recommended using [ErrorCode](./src/types/http.types.ts)
- `message` (`string`, required) - The error message.
- `details` (`string[]`, optional) - The error details. You can include additional information about the error that has occurred. Default is an empty array.
- `statusCode` (`number`, optional) - The status code that will be used if you want to sent via HTTP. Note this will be automatically populated based on the `code` you pass in
- `meta` (`T`, optional) - Additional metadata that you can pass in with generic.

```js
import createError from '@dancastillo/error
import { createError, type DCError} from '@dancastillo/error
const CreatedError = createError('INTERNAL_ERROR', 'Error happened')
type Meta = { userId: string }
const err = CreatedError()
const err = createError<Meta>('INTERNAL_ERROR', 'Error happened', ['Details'], { userId: 'some-user-id' })
console.log(err.code) // 'INTERNAL_ERROR'
console.log(err.message) // 'Error happened'
console.log(err.details) // ['Details']
console.log(err.meta) // { userId: 'some-user-id' }
```
## License
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"name": "@dancastillo/error",
"version": "0.0.2",
"version": "0.0.3",
"description": "A small utility for generating consistent errors",
"main": "dist/index.js",
"type": "module",
"types": "dist/index.d.ts",
"exports": {
"./package.json": "./package.json",
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.js"
"import": "./dist/index.js",
"require": "./dist/index.cjs"
}
},
"homepage": "https://github.com/dancastillo/error#readme",
Expand All @@ -35,8 +35,8 @@
"format:check": "prettier --write .",
"lint": "eslint ./src",
"prepare": "husky",
"build": "tsup",
"prepublish": "npm run build && npm run lint && npm run format:check && npm run test"
"build": "rm -rf dist && tsup",
"prepublish": "npm run lint && npm run format:check && npm run test && npm run build"
},
"devDependencies": {
"@changesets/cli": "^2.27.7",
Expand Down
2 changes: 1 addition & 1 deletion src/types/dc-error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type DCError<T> = {
export type DCError<T = unknown> = {
/**
* The error message.
*/
Expand Down

0 comments on commit 9dc43ce

Please sign in to comment.