Skip to content

Commit 616ea68

Browse files
Add contentTypes global reference and tooltip to content types docs (#279)
1 parent 76aa346 commit 616ea68

File tree

8 files changed

+250
-0
lines changed

8 files changed

+250
-0
lines changed

docs/technical-details/reference/content-types.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ title: Content Types
66

77
Harper supports several different content types (or MIME types) for both HTTP request bodies (describing operations) as well as for serializing content into HTTP response bodies. Harper follows HTTP standards for specifying both request body content types and acceptable response body content types. Any of these content types can be used with any of the standard Harper operations.
88

9+
:::tip Need a custom content type?
10+
11+
Harper's extensible content type system lets you add support for any serialization format (XML, YAML, proprietary formats, etc.) by registering custom handlers in the [`contentTypes`](./globals.md#contenttypes) global Map. See the linked API reference for detailed implementation types, handler properties, and examples.
12+
13+
:::
14+
915
For request body content, the content type should be specified with the `Content-Type` header. For example with JSON, use `Content-Type: application/json` and for CBOR, include `Content-Type: application/cbor`. To request that the response body be encoded with a specific content type, use the `Accept` header. If you want the response to be in JSON, use `Accept: application/json`. If you want the response to be in CBOR, use `Accept: application/cbor`.
1016

1117
The following content types are supported:

docs/technical-details/reference/globals.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,3 +325,59 @@ Returns map of shard number to an array of its associated nodes
325325
### `server.hostname`
326326

327327
Returns the hostname of the current node
328+
329+
### `server.contentTypes`
330+
331+
Returns the `Map` of registered content type handlers. Same as the [`contentTypes`](./globals#contenttypes) global.
332+
333+
## `contentTypes`
334+
335+
Returns a [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) of content type handlers for request/response serialization.
336+
337+
HarperDB uses content negotiation to automatically handle data serialization and deserialization for HTTP requests and other protocols. This process works by:
338+
339+
1. **Request Processing**: Comparing the `Content-Type` header with registered handlers to deserialize incoming data into structured formats for processing and storage
340+
2. **Response Generation**: Comparing the `Accept` header with registered handlers to serialize structured data into the appropriate response format
341+
342+
### Built-in Content Types
343+
344+
HarperDB includes handlers for common formats:
345+
346+
- **JSON** (`application/json`)
347+
- **CBOR** (`application/cbor`)
348+
- **MessagePack** (`application/msgpack`)
349+
- **CSV** (`text/csv`)
350+
- **Event-Stream** (`text/event-stream`)
351+
- And more...
352+
353+
### Custom Content Type Handlers
354+
355+
You can extend or replace content type handlers by modifying the `contentTypes` map from the `server` global (or `harperdb` export). The map is keyed by MIME type, with values being handler objects containing these optional properties:
356+
357+
#### Handler Properties
358+
359+
- **`serialize(data: any): Buffer | Uint8Array | string`**
360+
Called to convert data structures into the target format for responses. Should return binary data (Buffer/Uint8Array) or a string.
361+
362+
- **`serializeStream(data: any): ReadableStream`**
363+
Called to convert data structures into streaming format. Useful for handling asynchronous iterables or large datasets.
364+
365+
- **`deserialize(buffer: Buffer | string): any`**
366+
Called to convert incoming request data into structured format. Receives a string for text MIME types (`text/*`) and a Buffer for binary types. Only used if `deserializeStream` is not defined.
367+
368+
- **`deserializeStream(stream: ReadableStream): any`**
369+
Called to convert incoming request streams into structured format. Returns deserialized data (potentially as an asynchronous iterable).
370+
371+
- **`q: number`** _(default: 1)_
372+
Quality indicator between 0 and 1 representing serialization fidelity. Used in content negotiation to select the best format when multiple options are available. The server chooses the content type with the highest product of client quality × server quality values.
373+
374+
For example, if you wanted to define an XML serializer (that can respond with XML to requests with `Accept: text/xml`) you could write:
375+
376+
```javascript
377+
contentTypes.set('text/xml', {
378+
serialize(data) {
379+
return '<root>' ... some serialization '</root>';
380+
},
381+
q: 0.8,
382+
});
383+
```

versioned_docs/version-4.4/technical-details/reference/content-types.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ title: Content Types
66

77
Harper supports several different content types (or MIME types) for both HTTP request bodies (describing operations) as well as for serializing content into HTTP response bodies. Harper follows HTTP standards for specifying both request body content types and acceptable response body content types. Any of these content types can be used with any of the standard Harper operations.
88

9+
:::tip Need a custom content type?
10+
11+
Harper's extensible content type system lets you add support for any serialization format (XML, YAML, proprietary formats, etc.) by registering custom handlers in the [`contentTypes`](./globals.md#contenttypes) global Map. See the linked API reference for detailed implementation types, handler properties, and examples.
12+
13+
:::
14+
915
For request body content, the content type should be specified with the `Content-Type` header. For example with JSON, use `Content-Type: application/json` and for CBOR, include `Content-Type: application/cbor`. To request that the response body be encoded with a specific content type, use the `Accept` header. If you want the response to be in JSON, use `Accept: application/json`. If you want the response to be in CBOR, use `Accept: application/cbor`.
1016

1117
The following content types are supported:

versioned_docs/version-4.4/technical-details/reference/globals.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,59 @@ This records the provided value as a metric into Harper's analytics. Harper effi
255255
- `path` - This is an optional path (like a URL path). For a URL like /my-resource/, you would typically include a path of "my-resource", not including the id so you can group by all the requests to "my-resource" instead of individually aggregating by each individual id.
256256
- `method` - Optional method to group by.
257257
- `type` - Optional type to group by.
258+
259+
### `server.contentTypes`
260+
261+
Returns the `Map` of registered content type handlers. Same as the [`contentTypes`](./globals#contenttypes) global.
262+
263+
## `contentTypes`
264+
265+
Returns a [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) of content type handlers for request/response serialization.
266+
267+
HarperDB uses content negotiation to automatically handle data serialization and deserialization for HTTP requests and other protocols. This process works by:
268+
269+
1. **Request Processing**: Comparing the `Content-Type` header with registered handlers to deserialize incoming data into structured formats for processing and storage
270+
2. **Response Generation**: Comparing the `Accept` header with registered handlers to serialize structured data into the appropriate response format
271+
272+
### Built-in Content Types
273+
274+
HarperDB includes handlers for common formats:
275+
276+
- **JSON** (`application/json`)
277+
- **CBOR** (`application/cbor`)
278+
- **MessagePack** (`application/msgpack`)
279+
- **CSV** (`text/csv`)
280+
- **Event-Stream** (`text/event-stream`)
281+
- And more...
282+
283+
### Custom Content Type Handlers
284+
285+
You can extend or replace content type handlers by modifying the `contentTypes` map from the `server` global (or `harperdb` export). The map is keyed by MIME type, with values being handler objects containing these optional properties:
286+
287+
#### Handler Properties
288+
289+
- **`serialize(data: any): Buffer | Uint8Array | string`**
290+
Called to convert data structures into the target format for responses. Should return binary data (Buffer/Uint8Array) or a string.
291+
292+
- **`serializeStream(data: any): ReadableStream`**
293+
Called to convert data structures into streaming format. Useful for handling asynchronous iterables or large datasets.
294+
295+
- **`deserialize(buffer: Buffer | string): any`**
296+
Called to convert incoming request data into structured format. Receives a string for text MIME types (`text/*`) and a Buffer for binary types. Only used if `deserializeStream` is not defined.
297+
298+
- **`deserializeStream(stream: ReadableStream): any`**
299+
Called to convert incoming request streams into structured format. Returns deserialized data (potentially as an asynchronous iterable).
300+
301+
- **`q: number`** _(default: 1)_
302+
Quality indicator between 0 and 1 representing serialization fidelity. Used in content negotiation to select the best format when multiple options are available. The server chooses the content type with the highest product of client quality × server quality values.
303+
304+
For example, if you wanted to define an XML serializer (that can respond with XML to requests with `Accept: text/xml`) you could write:
305+
306+
```javascript
307+
contentTypes.set('text/xml', {
308+
serialize(data) {
309+
return '<root>' ... some serialization '</root>';
310+
},
311+
q: 0.8,
312+
});
313+
```

versioned_docs/version-4.5/technical-details/reference/content-types.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ title: Content Types
66

77
Harper supports several different content types (or MIME types) for both HTTP request bodies (describing operations) as well as for serializing content into HTTP response bodies. Harper follows HTTP standards for specifying both request body content types and acceptable response body content types. Any of these content types can be used with any of the standard Harper operations.
88

9+
:::tip Need a custom content type?
10+
11+
Harper's extensible content type system lets you add support for any serialization format (XML, YAML, proprietary formats, etc.) by registering custom handlers in the [`contentTypes`](./globals.md#contenttypes) global Map. See the linked API reference for detailed implementation types, handler properties, and examples.
12+
13+
:::
14+
915
For request body content, the content type should be specified with the `Content-Type` header. For example with JSON, use `Content-Type: application/json` and for CBOR, include `Content-Type: application/cbor`. To request that the response body be encoded with a specific content type, use the `Accept` header. If you want the response to be in JSON, use `Accept: application/json`. If you want the response to be in CBOR, use `Accept: application/cbor`.
1016

1117
The following content types are supported:

versioned_docs/version-4.5/technical-details/reference/globals.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,59 @@ server.resources.getMatch('/NewResource/some-id');
302302
/ or specify the export/protocol type, to allow it to be limited:
303303
server.resources.getMatch('/NewResource/some-id', 'my-protocol');
304304
```
305+
306+
### `server.contentTypes`
307+
308+
Returns the `Map` of registered content type handlers. Same as the [`contentTypes`](./globals#contenttypes) global.
309+
310+
## `contentTypes`
311+
312+
Returns a [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) of content type handlers for request/response serialization.
313+
314+
HarperDB uses content negotiation to automatically handle data serialization and deserialization for HTTP requests and other protocols. This process works by:
315+
316+
1. **Request Processing**: Comparing the `Content-Type` header with registered handlers to deserialize incoming data into structured formats for processing and storage
317+
2. **Response Generation**: Comparing the `Accept` header with registered handlers to serialize structured data into the appropriate response format
318+
319+
### Built-in Content Types
320+
321+
HarperDB includes handlers for common formats:
322+
323+
- **JSON** (`application/json`)
324+
- **CBOR** (`application/cbor`)
325+
- **MessagePack** (`application/msgpack`)
326+
- **CSV** (`text/csv`)
327+
- **Event-Stream** (`text/event-stream`)
328+
- And more...
329+
330+
### Custom Content Type Handlers
331+
332+
You can extend or replace content type handlers by modifying the `contentTypes` map from the `server` global (or `harperdb` export). The map is keyed by MIME type, with values being handler objects containing these optional properties:
333+
334+
#### Handler Properties
335+
336+
- **`serialize(data: any): Buffer | Uint8Array | string`**
337+
Called to convert data structures into the target format for responses. Should return binary data (Buffer/Uint8Array) or a string.
338+
339+
- **`serializeStream(data: any): ReadableStream`**
340+
Called to convert data structures into streaming format. Useful for handling asynchronous iterables or large datasets.
341+
342+
- **`deserialize(buffer: Buffer | string): any`**
343+
Called to convert incoming request data into structured format. Receives a string for text MIME types (`text/*`) and a Buffer for binary types. Only used if `deserializeStream` is not defined.
344+
345+
- **`deserializeStream(stream: ReadableStream): any`**
346+
Called to convert incoming request streams into structured format. Returns deserialized data (potentially as an asynchronous iterable).
347+
348+
- **`q: number`** _(default: 1)_
349+
Quality indicator between 0 and 1 representing serialization fidelity. Used in content negotiation to select the best format when multiple options are available. The server chooses the content type with the highest product of client quality × server quality values.
350+
351+
For example, if you wanted to define an XML serializer (that can respond with XML to requests with `Accept: text/xml`) you could write:
352+
353+
```javascript
354+
contentTypes.set('text/xml', {
355+
serialize(data) {
356+
return '<root>' ... some serialization '</root>';
357+
},
358+
q: 0.8,
359+
});
360+
```

versioned_docs/version-4.6/technical-details/reference/content-types.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ title: Content Types
66

77
Harper supports several different content types (or MIME types) for both HTTP request bodies (describing operations) as well as for serializing content into HTTP response bodies. Harper follows HTTP standards for specifying both request body content types and acceptable response body content types. Any of these content types can be used with any of the standard Harper operations.
88

9+
:::tip Need a custom content type?
10+
11+
Harper's extensible content type system lets you add support for any serialization format (XML, YAML, proprietary formats, etc.) by registering custom handlers in the [`contentTypes`](./globals.md#contenttypes) global Map. See the linked API reference for detailed implementation types, handler properties, and examples.
12+
13+
:::
14+
915
For request body content, the content type should be specified with the `Content-Type` header. For example with JSON, use `Content-Type: application/json` and for CBOR, include `Content-Type: application/cbor`. To request that the response body be encoded with a specific content type, use the `Accept` header. If you want the response to be in JSON, use `Accept: application/json`. If you want the response to be in CBOR, use `Accept: application/cbor`.
1016

1117
The following content types are supported:

versioned_docs/version-4.6/technical-details/reference/globals.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,3 +325,61 @@ Returns map of shard number to an array of its associated nodes
325325
### `server.hostname`
326326

327327
Returns the hostname of the current node
328+
329+
### `server.contentTypes`
330+
331+
Returns the `Map` of registered content type handlers. Same as the [`contentTypes`](./globals#contenttypes) global.
332+
333+
## `contentTypes`
334+
335+
Returns a [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) of content type handlers for request/response serialization.
336+
337+
HarperDB uses content negotiation to automatically handle data serialization and deserialization for HTTP requests and other protocols. This process works by:
338+
339+
1. **Request Processing**: Comparing the `Content-Type` header with registered handlers to deserialize incoming data into structured formats for processing and storage
340+
2. **Response Generation**: Comparing the `Accept` header with registered handlers to serialize structured data into the appropriate response format
341+
342+
### Built-in Content Types
343+
344+
HarperDB includes handlers for common formats:
345+
346+
- **JSON** (`application/json`)
347+
- **CBOR** (`application/cbor`)
348+
- **MessagePack** (`application/msgpack`)
349+
- **CSV** (`text/csv`)
350+
- **Event-Stream** (`text/event-stream`)
351+
- And more...
352+
353+
For detailed information about each built-in content type, including usage recommendations and performance characteristics, see the [Content Types reference](./content-types.md).
354+
355+
### Custom Content Type Handlers
356+
357+
You can extend or replace content type handlers by modifying the `contentTypes` map from the `server` global (or `harperdb` export). The map is keyed by MIME type, with values being handler objects containing these optional properties:
358+
359+
#### Handler Properties
360+
361+
- **`serialize(data: any): Buffer | Uint8Array | string`**
362+
Called to convert data structures into the target format for responses. Should return binary data (Buffer/Uint8Array) or a string.
363+
364+
- **`serializeStream(data: any): ReadableStream`**
365+
Called to convert data structures into streaming format. Useful for handling asynchronous iterables or large datasets.
366+
367+
- **`deserialize(buffer: Buffer | string): any`**
368+
Called to convert incoming request data into structured format. Receives a string for text MIME types (`text/*`) and a Buffer for binary types. Only used if `deserializeStream` is not defined.
369+
370+
- **`deserializeStream(stream: ReadableStream): any`**
371+
Called to convert incoming request streams into structured format. Returns deserialized data (potentially as an asynchronous iterable).
372+
373+
- **`q: number`** _(default: 1)_
374+
Quality indicator between 0 and 1 representing serialization fidelity. Used in content negotiation to select the best format when multiple options are available. The server chooses the content type with the highest product of client quality × server quality values.
375+
376+
For example, if you wanted to define an XML serializer (that can respond with XML to requests with `Accept: text/xml`) you could write:
377+
378+
```javascript
379+
contentTypes.set('text/xml', {
380+
serialize(data) {
381+
return '<root>' ... some serialization '</root>';
382+
},
383+
q: 0.8,
384+
});
385+
```

0 commit comments

Comments
 (0)