/
contentful-management.ts
93 lines (84 loc) · 2.79 KB
/
contentful-management.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
/**
* Contentful Management API SDK. Allows you to create instances of a client
* with access to the Contentful Content Management API.
* @packageDocumentation
*/
import { getUserAgentHeader } from 'contentful-sdk-core'
import type { RestAdapterParams } from './adapters/REST/rest-adapter'
import type { MakeRequest } from './common-types'
import { AdapterParams, createAdapter } from './create-adapter'
import createContentfulApi, { ClientAPI } from './create-contentful-api'
import type { PlainClientAPI } from './plain/common-types'
import type { DefaultParams } from './plain/plain-client'
import { createPlainClient } from './plain/plain-client'
import * as editorInterfaceDefaults from './constants/editor-interface-defaults'
export type { ClientAPI } from './create-contentful-api'
export { asIterator } from './plain/as-iterator'
export { isDraft, isPublished, isUpdated } from './plain/checks'
export type { PlainClientAPI } from './plain/common-types'
export { createClient }
export { RestAdapter } from './adapters/REST/rest-adapter'
export { editorInterfaceDefaults }
export type PlainClientDefaultParams = DefaultParams
interface UserAgentParams {
/**
* Application name and version e.g myApp/version
*/
application?: string
/**
* Integration name and version e.g react/version
*/
integration?: string
feature?: string
}
/**
* @deprecated
*/
export type ClientParams = RestAdapterParams & UserAgentParams
type ClientOptions = (RestAdapterParams | AdapterParams) & UserAgentParams
/**
* Create a client instance
* @param params - Client initialization parameters
*
* ```javascript
* const client = contentfulManagement.createClient({
* accessToken: 'myAccessToken'
* })
* ```
*/
function createClient(params: ClientOptions): ClientAPI
function createClient(
params: ClientOptions,
opts: {
type: 'plain'
defaults?: DefaultParams
}
): PlainClientAPI
function createClient(
params: ClientOptions,
opts: {
type?: 'plain'
defaults?: DefaultParams
} = {}
): ClientAPI | PlainClientAPI {
const sdkMain =
opts.type === 'plain' ? 'contentful-management-plain.js' : 'contentful-management.js'
const userAgent = getUserAgentHeader(
// @ts-expect-error
`${sdkMain}/${__VERSION__}`,
params.application,
params.integration,
params.feature
)
const adapter = createAdapter(params)
// Parameters<?> and ReturnType<?> only return the types of the last overload
// https://github.com/microsoft/TypeScript/issues/26591
// @ts-expect-error
const makeRequest: MakeRequest = (options: Parameters<MakeRequest>[0]): ReturnType<MakeRequest> =>
adapter.makeRequest({ ...options, userAgent })
if (opts.type === 'plain') {
return createPlainClient(makeRequest, opts.defaults)
} else {
return createContentfulApi(makeRequest) as ClientAPI
}
}