Skip to content

Commit 8320cf1

Browse files
committed
docs: api
Signed-off-by: Lexus Drumgold <[email protected]>
1 parent e80e1c3 commit 8320cf1

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

README.md

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,21 @@ Create [Node.js errors][1]
1616
- [Install](#install)
1717
- [Use](#use)
1818
- [API](#api)
19+
- [`createNodeError(code, Base, message)`](#createnodeerrorcode-base-message)
20+
- [`determineSpecificType(value)`](#determinespecifictypevalue)
1921
- [Types](#types)
2022
- [Enums](#enums)
2123
- [Type Defintions](#type-definitions)
2224
- [Contribute](#contribute)
2325

2426
## What is this?
2527

26-
This package provides an API for creating [Node.js errors][1].
28+
This package provides utilities for creating [Node.js errors][1].
2729

2830
## When should I use this?
2931

30-
This package is designed to help developers build [ponyfills][2] and utilities like [`mlly`][3] by providing a universal
31-
API for creating [Node.js errors][1].
32+
This package is designed to help developers build Node.js tools like [ponyfills][2], as well as more verbose utilities
33+
like [`mlly`][3], by providing a universal API for creating [Node.js errors][1].
3234

3335
### Differences between Node.js
3436

@@ -280,11 +282,39 @@ err instanceof TypeError: true
280282

281283
## API
282284

283-
**TODO**: Update documentation.
285+
This package exports the following identifiers:
286+
287+
- [`createNodeError`](#createnodeerrorcode-base-message)
288+
- [`determineSpecificType`](#determinespecifictypevalue)
289+
290+
There is no default export.
291+
292+
### `createNodeError(code, Base, message)`
293+
294+
Creates a Node.js error constructor.
295+
296+
If the given error `message` is a function, constructor arguments are passed to `message`. If the `message` is a string,
297+
constructor arguments are passed to [`util.format`][7] instead.
298+
299+
- `{ErrorCode}` **`code`** &mdash; Node.js error code
300+
- `{B extends ErrorConstructor}` **`Base`** &mdash; Error base class
301+
- `{M extends MessageFn | string}` **`message`** &mdash; Error message or message function
302+
- **Returns**: `{NodeErrorConstructor<B, M>}` `NodeError` constructor
303+
304+
> **Source**: [`src/create-node-error.ts`](src/create-node-error.ts)
305+
306+
### `determineSpecificType(value)`
307+
308+
Determines the specific type of a value for type-mismatch errors.
309+
310+
- `{unknown}` **`value`** &mdash; Value to evaluate
311+
- **Returns**: `{string}` Specific type of `value`
312+
313+
> **Source**: [`src/determine-specific-type.ts`](src/determine-specific-type.ts)
284314
285315
## Types
286316

287-
This package is fully typed with [TypeScript][7]. It exports the following definitions:
317+
This package is fully typed with [TypeScript][8]. It exports the following definitions:
288318

289319
### Enums
290320

@@ -306,4 +336,5 @@ See [`CONTRIBUTING.md`](CONTRIBUTING.md).
306336
[4]: https://nodejs.org/api/errors.html#errorstacktracelimit
307337
[5]: https://github.com/nodejs/node/blob/v19.3.0/lib/internal/errors.js#L484-L496
308338
[6]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
309-
[7]: https://www.typescriptlang.org
339+
[7]: https://nodejs.org/api/util.html#utilformatformat-args
340+
[8]: https://www.typescriptlang.org

src/create-node-error.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ import type { MessageFn, NodeError, NodeErrorConstructor } from '#src/types'
1313
/**
1414
* Creates a Node.js error constructor.
1515
*
16+
* If the given error `message` is a function, constructor arguments are passed
17+
* to `message`. If the `message` is a string, constructor arguments are passed
18+
* to [`util.format`][2] instead.
19+
*
20+
* [1]: {@link ./types/fn-message.ts}
21+
* [2]: https://nodejs.org/api/util.html#utilformatformat-args
22+
*
1623
* @template B - Error base class type
1724
* @template M - Error message type
1825
* @template T - Error base type

src/determine-specific-type.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { inspect } from 'node-inspect-extracted'
99
/**
1010
* Determines the specific type of a value for type-mismatch errors.
1111
*
12-
* @param {unknown} value - Value to detect type of
12+
* @param {unknown} value - Value to evaluate
1313
* @return {string} Specific type of `value`
1414
*/
1515
function determineSpecificType(value: unknown): string {

0 commit comments

Comments
 (0)