-
Notifications
You must be signed in to change notification settings - Fork 97
/
snapshot.ts
40 lines (36 loc) · 1.23 KB
/
snapshot.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
import copy from 'fast-copy'
import { freezeSys, toPlainObject } from 'contentful-sdk-core'
import enhanceWithMethods from '../enhance-with-methods'
import { wrapCollection } from '../common-utils'
import { MetaSysProps, DefaultElements, MakeRequest } from '../common-types'
export type SnapshotProps<T> = {
sys: MetaSysProps & {
snapshotType: string
snapshotEntityType: string
}
snapshot: T
}
export interface Snapshot<T> extends SnapshotProps<T>, DefaultElements<SnapshotProps<T>> {}
function createSnapshotApi() {
return {
/* In case the snapshot object evolve later */
}
}
/**
* @private
* @param makeRequest - function to make requests via an adapter
* @param data - Raw snapshot data
* @return Wrapped snapshot data
*/
export function wrapSnapshot<T>(_makeRequest: MakeRequest, data: SnapshotProps<T>): Snapshot<T> {
const snapshot = toPlainObject(copy(data))
const snapshotWithMethods = enhanceWithMethods(snapshot, createSnapshotApi())
return freezeSys(snapshotWithMethods)
}
/**
* @private
* @param makeRequest - function to make requests via an adapter
* @param data - Raw snapshot collection data
* @return Wrapped snapshot collection data
*/
export const wrapSnapshotCollection = wrapCollection(wrapSnapshot)