TypeScript implementation for RFC 8941 Structured Field Values for HTTP.
import {
Decimal,
decodeDictionary,
decodeItem,
decodeList,
DisplayString,
Integer,
Item,
Token,
} from "@shogo82148/sfv";
// decoding Items
const item = decodeItem("abc");
const value = item.value;
if (value instanceof Integer) {
// Integers
}
if (value instanceof Decimal) {
// Decimals
}
if (typeof value === "string") {
// Strings
}
if (value instanceof Token) {
// Tokens
}
if (value instanceof Uint8Array) {
// Binary Sequences
}
if (typeof value === "boolean") {
// Booleans
}
if (value instanceof Date) {
// Dates
}
if (value instanceof DisplayString) {
// Display Strings
}
// decoding Lists
const list = decodeList("abc, efg");
// decoding dictionary
const dict = decodeDictionary("foo=bar, baz=qux");
import {
Decimal,
encodeItem,
encodeList,
Integer,
Item,
Token,
} from "@shogo82148/sfv";
// encoding Items
encodeItem(new Item(new Token("a"))); // a
encodeItem(new Item("a")); // "a"
encodeItem(new Item(new Integer(10))); // 10
encodeItem(new Item(new Decimal(3.14))); // 3.14
encodeItem(new Item(new Uint8Array([1, 2, 3]))); // :AQID:
encodeItem(new Item(true)); // ?1
encodeItem(new Item(new Date(0))); // @0
encodeItem(new Item(new DisplayString("a"))); // %"a"
// encoding Lists
encodeList([new Item(new Token("abc")), new Item(new Token("efg"))]); // abc, efg
// encoding Dictionary
const dict = new Dictionary();
dict.set("foo", new Item(new Token("bar")));
dict.set("baz", new Item(new Token("qux")));
encodeDictionary(dict); // foo=bar, baz=qux
SFV types are mapped to TypeScript types as described in this section. Note that
only List
, Dictionary
, and Item
can be in a top-level.
The actual type might be one of them:
Type of SFV | Example of SFV | Type in TypeScript | Example in TypeScript |
---|---|---|---|
Integer | 10 |
Integer |
new Integer(10) |
Decimal | 3.14 |
Decimal |
new Decimal(3.14) |
String | "hello" |
string |
"hello" |
Token | x |
Token |
new Token("x") |
Byte Seq | :AQID: |
Uint8Array |
new Uint8Array([1, 2, 3]) |
Boolean | ?1 |
boolean |
true |
Date | @1659578233 |
Date |
new Date(1659578233000) |
DisplayString | %"f%c3%bc%c3%bc" |
DisplayString |
new DisplayString("füü") |
Inner List | (1 2) |
InnerList |
new InnerList() |
Parameters are ordered map of key-value pairs. They are decoded to
Parameters
.
Lists are decoded to List
.
Inner Lists are decoded to InnerList
.
Dictionaries are ordered maps of key-value pairs. They are decoded to
Dictionary
.