-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmetadata.ts
49 lines (45 loc) · 1.25 KB
/
metadata.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright 2020 Cognite AS
import type { HttpResponse } from './httpClient/basicHttpClient';
export interface Metadata {
status: number;
headers: { [key: string]: string };
}
/**
* When making API requests, the response includes metadata such as the HTTP status code and headers.
* Often, the SDK user doesn't care about this data, and therefore the happy path
* of the SDK excludes this data by default.
*
* However, the SDK allows the user to look-up the metadata if they need it.
*
* @example
* ```ts
* const response = await client.assets.list();
* const metadata = client.getMetadata(response);
* ```
*
* @remarks
* We utilize [WeakMap](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap)
* to avoid memory leaks.
*
* @hidden
*/
export class MetadataMap {
private map: WeakMap<object, Metadata>;
constructor() {
this.map = new WeakMap();
}
public addAndReturn<T extends object, V>(
value: T,
metadata: HttpResponse<V>
): T {
this.map.set(value, {
// we extract out only what is necessary
status: metadata.status,
headers: metadata.headers,
});
return value;
}
public get(value: object): undefined | Metadata {
return this.map.get(value);
}
}