/
app-definition.ts
96 lines (85 loc) · 2.47 KB
/
app-definition.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import copy from 'fast-copy'
import { freezeSys, toPlainObject } from 'contentful-sdk-core'
import { DefaultElements, BasicMetaSysProps, SysLink, MakeRequest, Link } from '../common-types'
import enhanceWithMethods from '../enhance-with-methods'
import { wrapCollection } from '../common-utils'
import createAppDefinitionApi, { ContentfulAppDefinitionAPI } from '../create-app-definition-api'
import { SetOptional, Except } from 'type-fest'
import { FieldType } from './field-type'
import { ParameterDefinition } from './widget-parameters'
export interface NavigationItem {
name: string
path: string
}
type LocationType = 'app-config' | 'entry-sidebar' | 'entry-editor' | 'dialog' | 'page'
export interface SimpleLocation {
location: LocationType
}
export interface EntryFieldLocation {
location: 'entry-field'
fieldTypes: FieldType[]
}
export interface PageLocation {
location: 'page'
navigationItem?: NavigationItem
}
export type AppLocation = SimpleLocation | EntryFieldLocation | PageLocation
export type AppDefinitionProps = {
/**
* System metadata
*/
sys: BasicMetaSysProps & { organization: SysLink }
/**
* App name
*/
name: string
/**
* URL where the root HTML document of the app can be found
*/
src?: string
/**
* Link to an AppBundle
*/
bundle?: Link<'AppBundle'>
/**
* Locations where the app can be installed
*/
locations?: AppLocation[]
/**
* Instance parameter definitions
*/
parameters?: {
instance?: ParameterDefinition[]
}
}
export type CreateAppDefinitionProps = SetOptional<
Except<AppDefinitionProps, 'sys' | 'bundle'>,
'src' | 'locations'
>
export type AppDefinition = ContentfulAppDefinitionAPI &
AppDefinitionProps &
DefaultElements<AppDefinitionProps>
/**
* @private
* @param makeRequest - function to make requests via an adapter
* @param data - Raw App Definition data
* @return Wrapped App Definition data
*/
export function wrapAppDefinition(
makeRequest: MakeRequest,
data: AppDefinitionProps
): AppDefinition {
const appDefinition = toPlainObject(copy(data))
const appDefinitionWithMethods = enhanceWithMethods(
appDefinition,
createAppDefinitionApi(makeRequest)
)
return freezeSys(appDefinitionWithMethods)
}
/**
* @private
* @param makeRequest - function to make requests via an adapter
* @param data - Raw App Definition collection data
* @return Wrapped App Definition collection data
*/
export const wrapAppDefinitionCollection = wrapCollection(wrapAppDefinition)