|
| 1 | +# API Reference |
| 2 | + |
| 3 | +This file catalogs every public export from `@git-stunts/trailer-codec` so you can confidently import, configure, and extend the codec. |
| 4 | + |
| 5 | +## Encoding & decoding helpers |
| 6 | + |
| 7 | +### `decodeMessage(message: string)` |
| 8 | +- Deprecated convenience wrapper around `new TrailerCodec().decode(message)`. Use `TrailerCodec` instances instead. (to be removed in v3.0) |
| 9 | +- Input: a raw commit payload (title, optional body, trailers) as a string. |
| 10 | +- Output: `{ title: string, body: string, trailers: Record<string, string> }` where `body` is trimmed via `formatBodySegment` (see below) and trailer keys are normalized to lowercase. |
| 11 | +- Throws `TrailerCodecError` subclasses (e.g., `TrailerNoSeparatorError`, `TrailerValueInvalidError`, or `CommitMessageInvalidError`) for invalid titles, missing blank lines, oversized messages, or malformed trailers. |
| 12 | + |
| 13 | +### `encodeMessage({ title: string, body?: string, trailers?: Record<string, string> })` |
| 14 | +-- Deprecated convenience wrapper around `new TrailerCodec().encode(payload)`. Use `TrailerCodec` instances instead. (to be removed in v3.0) |
| 15 | +- Builds a `GitCommitMessage` under the hood and returns the canonical string. Trailers are converted from plain objects to `GitTrailer` instances via the default factory (see `GitTrailer` below). |
| 16 | + |
| 17 | +### `formatBodySegment(body?: string, { keepTrailingNewline = false })` |
| 18 | +- Shared helper for `decodeMessage` to trim whitespace while optionally keeping a trailing newline when you plan to write the body back into a template. |
| 19 | + |
| 20 | +### `createMessageHelpers({ service, bodyFormatOptions } = {})` |
| 21 | +- Returns `{ decodeMessage, encodeMessage }` bound to the injected `TrailerCodecService` instance; a new service is created when none is provided. |
| 22 | +- Supports `bodyFormatOptions` (forwarded to `formatBodySegment`) and is useful for advanced/test wiring. |
| 23 | + |
| 24 | +### `createDefaultTrailerCodec({ bodyFormatOptions } = {})` |
| 25 | +- Creates a new `TrailerCodecService`, builds a `TrailerCodec`, and returns it so you can call `encodeMessage()`/`decodeMessage()` without manually wiring services. |
| 26 | + |
| 27 | +### `TrailerCodec` |
| 28 | +- Constructor opts: `{ service = new TrailerCodecService(), bodyFormatOptions }`. |
| 29 | +- Exposes `decodeMessage(input)`/`decode(input)` and `encodeMessage(payload)`/`encode(payload)` methods that delegate to `createMessageHelpers()`. |
| 30 | +- The `decode()` and `encode()` methods are convenience aliases added in v2.1.0. |
| 31 | + |
| 32 | +### `createConfiguredCodec({ keyPattern, keyMaxLength, parserOptions, formatters, bodyFormatOptions } = {})` |
| 33 | +- Creates a schema bundle via `createGitTrailerSchemaBundle`, a `TrailerParser`, and a `TrailerCodecService`, then exposes `{ service, helpers, decodeMessage, encodeMessage }`. |
| 34 | +- Use this when you need custom trailer patterns, parser tweaks, formatter hooks, or tightly controlled key lengths. |
| 35 | + |
| 36 | +## Domain model exports |
| 37 | + |
| 38 | +### `GitCommitMessage` |
| 39 | +```ts |
| 40 | +new GitCommitMessage( |
| 41 | + payload: { title: string; body?: string; trailers?: GitTrailerInput[] }, |
| 42 | + options?: { trailerSchema?: ReturnType<typeof getDefaultTrailerSchemaBundle>['schema']; formatters?: { titleFormatter?: (value: string) => string; bodyFormatter?: (value: string) => string } } |
| 43 | +); |
| 44 | +``` |
| 45 | +- Validates via `GitCommitMessageSchema`, normalizes title/body with optional formatters, and converts `trailers` to `GitTrailer` instances. |
| 46 | +- `toString()` returns a Git-style commit string (title, optional body, blank line, trailers). |
| 47 | + |
| 48 | +### `GitTrailer` |
| 49 | +- Accepts `(key: string, value: string, schema = GitTrailerSchema)`. |
| 50 | +- Validates using `GitTrailerSchema` and normalizes the key to lowercase and the value to a trimmed string. |
| 51 | +- Throws `TrailerInvalidError` or `TrailerValueInvalidError` when the provided key/value fail schema validation. |
| 52 | + |
| 53 | +## Services & parsers |
| 54 | + |
| 55 | +### `TrailerCodecService` |
| 56 | +- Core decode/encode logic; see `docs/SERVICE.md` for how the pipeline is wired. |
| 57 | +- Constructor options: |
| 58 | + - `schemaBundle`: result of `createGitTrailerSchemaBundle({ keyPattern, keyMaxLength })` (defaults: `keyPattern` = `[A-Za-z0-9_-]+`, `keyMaxLength` = `100`, see the schema section below). |
| 59 | + - `trailerFactory`: function that instantiates trailers (defaults to `GitTrailer`). |
| 60 | + - `parser`: instance of `TrailerParser`. |
| 61 | + - `messageNormalizer`, `titleExtractor`, `bodyComposer`: helper classes that normalize lines, extract the title, and compose the body. |
| 62 | + - `formatters`: optional `{ titleFormatter, bodyFormatter }` that run before serialization. |
| 63 | +- `decode(message)` enforces message size, normalizes lines, extracts the title, splits body/trailers with `TrailerParser`, composes the body, builds trailers with `trailerFactory`, and constructs a `GitCommitMessage`. |
| 64 | + - `encode(messageEntity)` accepts either a `GitCommitMessage` instance or a plain payload object, validates it against `GitCommitMessageSchema`, and returns the canonical commit string produced by the entity. |
| 65 | + |
| 66 | +### `TrailerParser` |
| 67 | +- Constructor takes `{ keyPattern = TRAILER_KEY_RAW_PATTERN_STRING }`. |
| 68 | +- `split(lines)` finds where the trailer block begins (walks backward, validates the blank-line separator) and returns `{ bodyLines, trailerLines }`. |
| 69 | +- Used internally by `TrailerCodecService` and is injectable for custom parsing strategies. |
| 70 | + |
| 71 | +## Schemas & constants |
| 72 | + |
| 73 | +### `createGitTrailerSchemaBundle({ keyPattern, keyMaxLength } = {})` |
| 74 | +- Returns `{ schema, keyPattern, keyRegex }` with the schema used by `GitTrailer` and `GitCommitMessage`. |
| 75 | +- Default `keyPattern` is `[A-Za-z0-9_-]+` and `keyMaxLength` defaults to `100`. |
| 76 | + |
| 77 | +### `TRAILER_KEY_RAW_PATTERN_STRING` / `TRAILER_KEY_REGEX` |
| 78 | +- Exported from the default schema bundle; use them to keep custom parsers aligned with validation rules. |
| 79 | + |
| 80 | +## Errors |
| 81 | + |
| 82 | +### `TrailerCodecError` |
| 83 | +- Base error type used throughout the codec (`index.js` re-exports it). |
| 84 | +- Signature: `(message: string, meta: Record<string, unknown> = {})`. |
| 85 | + |
| 86 | +### Validation error subclasses |
| 87 | + |
| 88 | +| Error | Thrown by | Meaning | |
| 89 | +| --- | --- | --- | |
| 90 | +| `TrailerTooLargeError` | `MessageNormalizer.guardMessageSize` (called by `TrailerCodecService.decode` and the exported `decodeMessage`) | Message exceeds the 5 MB guard in `MessageNormalizer`. | |
| 91 | +| `TrailerNoSeparatorError` | `TrailerParser.split` / `TrailerCodecService.decode` when the blank-line guard fails | A trailer block was found without a blank line separating it from the body (see `TrailerParser`). | |
| 92 | +| `TrailerValueInvalidError` | `GitTrailer` via `GitTrailerSchema.parse` when constructing trailers | A trailer value violated the `GitTrailerSchema` (e.g., contained `\n`). | |
| 93 | +| `TrailerInvalidError` | `GitTrailer` via `GitTrailerSchema.parse` when constructing trailers | Trailer key or value failed validation (`GitTrailerSchema`). | |
| 94 | +| `CommitMessageInvalidError` | `GitCommitMessage` via `GitCommitMessageSchema.parse` (triggered by `TrailerCodecService.decode` or `encode`) | The `GitCommitMessageSchema` rejected the title/body/trailers combination. | |
0 commit comments