Aller au contenu principal

Headers

HTTP header record constructor

Callable

const headersFromIterable = Headers([
['key1', 'value1'],
['key2', 'value2']
]);// { key1: 'value1, key2: 'value2' }
const headersFromObject = Headers({
key1: 'value1',
key2: 'value2'
});// { key1: 'value1, key2: 'value2' }

Index

Codec

__decode__

  • __decode__(this, input, context): Result<Readonly<Record<string, string>>, CodecError>
  • Returns the decoded input, Result.Ok or Result.Error()

    interface SomeObject {
    foo: string
    }
    const someCodec: Codec<SomeObject> = ...;
    const input: unknown = ...;
    const decoded = Codec.decode(someCodec, input);

__encode__

  • __encode__(this, input): unknown
  • Returns the encoded input

    interface SomeObject {
    foo: string
    }
    const someCodec: Codec<SomeObject> = ...;
    const someObject: SomeObject = { foo: "bar" }
    const encoded = Codec.decode(someCodec, someObject);

__schema__

  • __schema__(this): JSONValue
  • Returns the JSONSchema corresponding to the decoded type

    const someCodec: Codec<unknown> = ...;
    const jsonSchema = Codec.schema(someCodec);

Formatting

asString

  • asString(this, self): string
  • Converts the given value to a String.

    Number.asString(123); // '123'

Type

__inspect__

__inspect__: Option<(anyValue, depth, options, inspect) => string>

When defined, returns a custom string representation. To be useful, it should be bound to a prototype (ex: Struct)

import { inspect } from 'node:util';

interface Foo {
foo: boolean;
}
const Foo = Struct.define<Foo>({
typeName: 'Foo',
__inspect__: (self) => `Foo { ${String(self.foo)} }`,
});
const myStruct = Struct.create(Foo, { foo: true });// 'Foo { true }'
inspect(myStruct);// 'Foo { true }'

asInstance

  • asInstance(this, anyValue): Option<Readonly<Record<string, string>>>
  • Try to convert anyValue to enum value or else returns Option.None

    const StringType: Type<string>;
    StringType.asInstance('foo'); // Option.Some('foo')
    StringType.asInstance(12); // Option.None

hasInstance

  • hasInstance(this, anyValue): anyValue is Readonly<Record<string, string>>
  • Return true if the given value is an instance of the class.

    const StringType: Type<string>;
    StringType.hasInstance('foo'); // true
    StringType.hasInstance(42); // false

Other

__call__

__call__: (values) => Headers = ...

empty

empty: () => Readonly<{}>

typeName

typeName: string = 'Headers'