/
app-bundle.ts
99 lines (88 loc) · 2.67 KB
/
app-bundle.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
97
98
99
import { freezeSys, toPlainObject } from 'contentful-sdk-core'
import copy from 'fast-copy'
import { Except } from 'type-fest'
import { wrapCollection } from '../common-utils'
import { BasicMetaSysProps, DefaultElements, MakeRequest, SysLink } from '../common-types'
import enhanceWithMethods from '../enhance-with-methods'
type AppBundleSys = Except<BasicMetaSysProps, 'version'> & {
appDefinition: SysLink
organization: SysLink
}
export type AppBundleFile = {
name: string
size: number
md5: string
}
export type CreateAppBundleProps = {
appUploadId: string
comment?: string
}
export type AppBundleProps = {
/**
* System metadata
*/
sys: AppBundleSys
/**
* List of all the files that are in this bundle
*/
files: AppBundleFile[]
/**
* A comment that describes this bundle
*/
comment?: string
}
export interface AppBundle extends AppBundleProps, DefaultElements<AppBundleProps> {
/**
* Deletes this object on the server.
* @return Promise for the deletion. It contains no data, but the Promise error case should be handled.
* @example ```javascript
* const contentful = require('contentful-management')
*
* const client = contentful.createClient({
* accessToken: '<content_management_api_key>'
* })
*
* client.getOrganization('<org_id>')
* .then((org) => org.getAppDefinition('<app_def_id>'))
* .then((appDefinition) => appDefinition.getAppBundle('<app-bundle-id>'))
* .then((appBundle) => appBundle.delete())
* .catch(console.error)
* ```
*/
delete(): Promise<void>
}
function createAppBundleApi(makeRequest: MakeRequest) {
const getParams = (data: AppBundleProps) => ({
organizationId: data.sys.organization.sys.id,
appDefinitionId: data.sys.appDefinition.sys.id,
appBundleId: data.sys.id,
})
return {
delete: function del() {
const data = this.toPlainObject() as AppBundleProps
return makeRequest({
entityType: 'AppBundle',
action: 'delete',
params: getParams(data),
})
},
}
}
/**
* @private
* @param makeRequest - function to make requests via an adapter
* @param data - Raw App Bundle data
* @return Wrapped App Bundle data
*/
export function wrapAppBundle(makeRequest: MakeRequest, data: AppBundleProps): AppBundle {
const appBundle = toPlainObject(copy(data))
const appBundleWithMethods = enhanceWithMethods(appBundle, createAppBundleApi(makeRequest))
return freezeSys(appBundleWithMethods)
}
/**
* @private
* @param makeRequest - function to make requests via an adapter
* @param data - Raw App Bundle collection data
* @return Wrapped App Bundle collection data
*/
export const wrapAppBundleCollection = wrapCollection(wrapAppBundle)