/
locale.ts
145 lines (137 loc) · 4.11 KB
/
locale.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import copy from 'fast-copy'
import { freezeSys, toPlainObject } from 'contentful-sdk-core'
import { Except, SetOptional } from 'type-fest'
import enhanceWithMethods from '../enhance-with-methods'
import { wrapCollection } from '../common-utils'
import { BasicMetaSysProps, SysLink, DefaultElements, MakeRequest } from '../common-types'
export type LocaleProps = {
sys: BasicMetaSysProps & { space: SysLink; environment: SysLink }
/**
* Locale name
*/
name: string
/**
* Locale code (example: en-us)
*/
code: string
/**
* Internal locale code
*/
internal_code: string
/**
* Locale code to fallback to when there is not content for the current locale
*/
fallbackCode: string | null
/**
* If the content under this locale should be available on the CDA (for public reading)
*/
contentDeliveryApi: boolean
/**
* If the content under this locale should be available on the CMA (for editing)
*/
contentManagementApi: boolean
/**
* If this is the default locale
*/
default: boolean
/**
* If the locale needs to be filled in on entries or not
*/
optional: boolean
}
export type CreateLocaleProps = Omit<
SetOptional<
Except<LocaleProps, 'sys'>,
'optional' | 'contentManagementApi' | 'default' | 'contentDeliveryApi'
>,
'internal_code'
>
export interface Locale extends LocaleProps, DefaultElements<LocaleProps> {
/**
* 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.getSpace('<space_id>')
* .then((space) => space.getEnvironment('<environment_id>'))
* .then((environment) => environment.getLocale('<locale_id>'))
* .then((locale) => locale.delete())
* .then(() => console.log(`locale deleted.`))
* .catch(console.error)
* ```
*/
delete(): Promise<void>
/**
* Sends an update to the server with any changes made to the object's properties
* @return Object returned from the server with updated changes.
* @example ```javascript
* const contentful = require('contentful-management')
*
* const client = contentful.createClient({
* accessToken: '<content_management_api_key>'
* })
*
* client.getSpace('<space_id>')
* .then((space) => space.getEnvironment('<environment_id>'))
* .then((environment) => environment.getLocale('<locale_id>'))
* .then((locale) => {
* locale.name = 'New locale name'
* return locale.update()
* })
* .then((locale) => console.log(`locale ${locale.sys.id} updated.`))
* .catch(console.error)
* ```
*/
update(): Promise<Locale>
}
function createLocaleApi(makeRequest: MakeRequest) {
const getParams = (locale: LocaleProps) => ({
spaceId: locale.sys.space.sys.id,
environmentId: locale.sys.environment.sys.id,
localeId: locale.sys.id,
})
return {
update: function () {
const raw = this.toPlainObject() as LocaleProps
return makeRequest({
entityType: 'Locale',
action: 'update',
params: getParams(raw),
payload: raw,
}).then((data) => wrapLocale(makeRequest, data))
},
delete: function () {
const raw = this.toPlainObject() as LocaleProps
return makeRequest({
entityType: 'Locale',
action: 'delete',
params: getParams(raw),
}).then(() => {
// noop
})
},
}
}
/**
* @private
* @param makeRequest - function to make requests via an adapter
* @param data - Raw locale data
* @return Wrapped locale data
*/
export function wrapLocale(makeRequest: MakeRequest, data: LocaleProps): Locale {
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
delete data.internal_code
const locale = toPlainObject(copy(data))
const localeWithMethods = enhanceWithMethods(locale, createLocaleApi(makeRequest))
return freezeSys(localeWithMethods)
}
/**
* @private
*/
export const wrapLocaleCollection = wrapCollection(wrapLocale)