-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathcontentful.ts
200 lines (186 loc) · 5.44 KB
/
contentful.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
* Contentful Delivery API SDK. Allows you to create instances of a client
* with access to the Contentful Content Delivery API.
*/
import axios from 'axios'
import type { AxiosAdapter, AxiosRequestConfig, AxiosResponse } from 'axios'
import { createHttpClient, getUserAgentHeader } from 'contentful-sdk-core'
import { createGlobalOptions } from './create-global-options.js'
import { makeClient } from './make-client.js'
import {
validateRemoveUnresolvedParam,
validateResolveLinksParam,
} from './utils/validate-params.js'
import type { ContentfulClientApi } from './types/client.js'
/**
* @category Client
*/
export type ClientLogLevel = 'error' | 'warning' | 'info' | string
/**
* Client initialization parameters
* @category Client
*/
export interface CreateClientParams {
/**
* Space ID
*/
space: string
/**
* Contentful CDA Access Token
*/
accessToken: string
/**
* Contentful Environment ID
* @defaultValue "master"
*/
environment?: string
/**
* Requests will be made over http instead of the default https
* @defaultValue true
*/
insecure?: boolean
/**
* API host. Also usable with preview.contentful.com.
* @defaultValue "cdn.contentful.com"
*/
host?: string
/**
* Path appended to the host to support gateways/proxies with custom urls
*/
basePath?: string
/**
* Optional Node.js HTTP agent for proxying
* (see <a href="https://nodejs.org/api/http.html#http_class_http_agent">Node.js docs</a>
* and <a href="https://www.npmjs.com/package/https-proxy-agent">https-proxy-agent</a>)
*/
httpAgent?: AxiosRequestConfig['httpAgent']
/**
* Optional Node.js HTTP agent for proxying
* (see <a href="https://nodejs.org/api/http.html#http_class_http_agent">Node.js docs</a>
* and <a href="https://www.npmjs.com/package/https-proxy-agent">https-proxy-agent</a>)
*/
httpsAgent?: AxiosRequestConfig['httpsAgent']
/**
* Optional Axios proxy (see <a href="https://github.com/mzabriskie/axios#request-config"> axios docs </a>)
*/
proxy?: AxiosRequestConfig['proxy']
/**
* Optional additional headers
*/
headers?: Record<string, string>
/**
* Optional axios request adapter (see <a href="https://github.com/mzabriskie/axios#request-config"> axios docs </a>)
*/
adapter?: AxiosAdapter
/**
* Application name and version e.g myApp/version
*/
application?: string
/**
* Integration name and version e.g react/version
*/
integration?: string
/**
* If we should retry on errors and 429 rate limit exceptions
* @defaultValue true
*/
retryOnError?: boolean
/**
* A log handler function to process given log messages and errors.
* (The default can be found at: https://github.com/contentful/contentful-sdk-core/blob/master/src/create-http-client.ts)
* @param level - Log level, e.g. error, warning, or info
* @param data - Log data
*/
logHandler?: (level: ClientLogLevel, data?: Record<string, any> | string) => void
/**
* connection timeout in milliseconds (default:30000)
*/
timeout?: number
/**
* Optional number of retries before failure.
* @defaultValue 5
*/
retryLimit?: number
/**
* Interceptor called on every request. Takes Axios request config as an arg.
*/
requestLogger?: (request: AxiosRequestConfig | Error) => unknown
/**
* Interceptor called on every response. Takes Axios response object as an arg.
*/
responseLogger?: (response: AxiosResponse<any> | Error) => unknown
/**
* Enable Content Source Maps.
* @remarks
* This feature is only available when using the Content Preview API.
*/
includeContentSourceMaps?: boolean
/**
* Enable alpha features.
*/
alphaFeatures?: {
/**
* @deprecated Use the `includeContentSourceMaps` option directly instead.
*/
includeContentSourceMaps?: boolean
}
}
/**
* Create a client instance
* @param params - Client initialization parameters
* @category Client
* @example
* ```typescript
* const contentful = require('contentful')
* const client = contentful.createClient({
* accessToken: 'myAccessToken',
* space: 'mySpaceId'
* })
* ```
*/
export function createClient(params: CreateClientParams): ContentfulClientApi<undefined> {
if (!params.accessToken) {
throw new TypeError('Expected parameter accessToken')
}
if (!params.space) {
throw new TypeError('Expected parameter space')
}
validateResolveLinksParam(params)
validateRemoveUnresolvedParam(params)
const defaultConfig = {
resolveLinks: true,
removeUnresolved: false,
defaultHostname: 'cdn.contentful.com',
environment: 'master',
}
const config = {
...defaultConfig,
...params,
}
const userAgentHeader = getUserAgentHeader(
`contentful.js/${__VERSION__}`,
config.application,
config.integration,
)
config.headers = {
...config.headers,
'Content-Type': 'application/vnd.contentful.delivery.v1+json',
'X-Contentful-User-Agent': userAgentHeader,
}
const http = createHttpClient(axios, config)
if (!http.defaults.baseURL) {
throw new Error('Please define a baseURL')
}
const getGlobalOptions = createGlobalOptions({
space: config.space,
environment: config.environment,
spaceBaseUrl: http.defaults.baseURL,
environmentBaseUrl: `${http.defaults.baseURL}environments/${config.environment}`,
})
// Append environment to baseURL
http.defaults.baseURL = getGlobalOptions({}).environmentBaseUrl
return makeClient({
http,
getGlobalOptions,
})
}