-
Notifications
You must be signed in to change notification settings - Fork 97
/
space.ts
37 lines (33 loc) · 1.37 KB
/
space.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
import { freezeSys, toPlainObject } from 'contentful-sdk-core'
import copy from 'fast-copy'
import { BasicMetaSysProps, DefaultElements, MakeRequest } from '../common-types'
import { wrapCollection } from '../common-utils'
import createSpaceApi, { ContentfulSpaceAPI } from '../create-space-api'
import enhanceWithMethods from '../enhance-with-methods'
export type SpaceProps = {
sys: BasicMetaSysProps & { organization: { sys: { id: string } } }
name: string
}
export type Space = SpaceProps & DefaultElements<SpaceProps> & ContentfulSpaceAPI
/**
* This method creates the API for the given space with all the methods for
* reading and creating other entities. It also passes down a clone of the
* http client with a space id, so the base path for requests now has the
* space id already set.
* @private
* @param makeRequest - function to make requests via an adapter
* @param data - API response for a Space
* @return {Space}
*/
export function wrapSpace(makeRequest: MakeRequest, data: SpaceProps): Space {
const space = toPlainObject(copy(data))
const spaceApi = createSpaceApi(makeRequest)
const enhancedSpace = enhanceWithMethods(space, spaceApi)
return freezeSys(enhancedSpace)
}
/**
* This method wraps each space in a collection with the space API. See wrapSpace
* above for more details.
* @private
*/
export const wrapSpaceCollection = wrapCollection(wrapSpace)