UNPKG

3.19 kBTypeScriptView Raw
1/// <reference types="node" />
2import * as http2 from 'http2';
3export declare type MetadataValue = string | Buffer;
4export declare type MetadataObject = Map<string, MetadataValue[]>;
5export interface MetadataOptions {
6 idempotentRequest?: boolean;
7 waitForReady?: boolean;
8 cacheableRequest?: boolean;
9 corked?: boolean;
10}
11/**
12 * A class for storing metadata. Keys are normalized to lowercase ASCII.
13 */
14export declare class Metadata {
15 protected internalRepr: MetadataObject;
16 private options;
17 constructor(options?: MetadataOptions);
18 /**
19 * Sets the given value for the given key by replacing any other values
20 * associated with that key. Normalizes the key.
21 * @param key The key to whose value should be set.
22 * @param value The value to set. Must be a buffer if and only
23 * if the normalized key ends with '-bin'.
24 */
25 set(key: string, value: MetadataValue): void;
26 /**
27 * Adds the given value for the given key by appending to a list of previous
28 * values associated with that key. Normalizes the key.
29 * @param key The key for which a new value should be appended.
30 * @param value The value to add. Must be a buffer if and only
31 * if the normalized key ends with '-bin'.
32 */
33 add(key: string, value: MetadataValue): void;
34 /**
35 * Removes the given key and any associated values. Normalizes the key.
36 * @param key The key whose values should be removed.
37 */
38 remove(key: string): void;
39 /**
40 * Gets a list of all values associated with the key. Normalizes the key.
41 * @param key The key whose value should be retrieved.
42 * @return A list of values associated with the given key.
43 */
44 get(key: string): MetadataValue[];
45 /**
46 * Gets a plain object mapping each key to the first value associated with it.
47 * This reflects the most common way that people will want to see metadata.
48 * @return A key/value mapping of the metadata.
49 */
50 getMap(): {
51 [key: string]: MetadataValue;
52 };
53 /**
54 * Clones the metadata object.
55 * @return The newly cloned object.
56 */
57 clone(): Metadata;
58 /**
59 * Merges all key-value pairs from a given Metadata object into this one.
60 * If both this object and the given object have values in the same key,
61 * values from the other Metadata object will be appended to this object's
62 * values.
63 * @param other A Metadata object.
64 */
65 merge(other: Metadata): void;
66 setOptions(options: MetadataOptions): void;
67 getOptions(): MetadataOptions;
68 /**
69 * Creates an OutgoingHttpHeaders object that can be used with the http2 API.
70 */
71 toHttp2Headers(): http2.OutgoingHttpHeaders;
72 private _getCoreRepresentation;
73 /**
74 * This modifies the behavior of JSON.stringify to show an object
75 * representation of the metadata map.
76 */
77 toJSON(): {
78 [key: string]: MetadataValue[];
79 };
80 /**
81 * Returns a new Metadata object based fields in a given IncomingHttpHeaders
82 * object.
83 * @param headers An IncomingHttpHeaders object.
84 */
85 static fromHttp2Headers(headers: http2.IncomingHttpHeaders): Metadata;
86}