forked from sanity-io/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.ts
97 lines (78 loc) · 3.43 KB
/
config.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
import {generateHelpUrl} from './generateHelpUrl'
import type {ClientConfig, InitializedClientConfig} from './types'
import * as validate from './validators'
import * as warnings from './warnings'
const defaultCdnHost = 'apicdn.sanity.io'
export const defaultConfig = {
apiHost: 'https://api.sanity.io',
apiVersion: '1',
useProjectHostname: true,
} satisfies ClientConfig
const LOCALHOSTS = ['localhost', '127.0.0.1', '0.0.0.0']
const isLocal = (host: string) => LOCALHOSTS.indexOf(host) !== -1
export const validateApiVersion = function validateApiVersion(apiVersion: string) {
if (apiVersion === '1' || apiVersion === 'X') {
return
}
const apiDate = new Date(apiVersion)
const apiVersionValid =
/^\d{4}-\d{2}-\d{2}$/.test(apiVersion) && apiDate instanceof Date && apiDate.getTime() > 0
if (!apiVersionValid) {
throw new Error('Invalid API version string, expected `1` or date in format `YYYY-MM-DD`')
}
}
export const initConfig = (
config: Partial<ClientConfig>,
prevConfig: Partial<ClientConfig>
): InitializedClientConfig => {
const specifiedConfig = Object.assign({}, prevConfig, config)
if (!specifiedConfig.apiVersion) {
warnings.printNoApiVersionSpecifiedWarning()
}
const newConfig = Object.assign({} as InitializedClientConfig, defaultConfig, specifiedConfig)
const projectBased = newConfig.useProjectHostname
if (typeof Promise === 'undefined') {
const helpUrl = generateHelpUrl('js-client-promise-polyfill')
throw new Error(`No native Promise-implementation found, polyfill needed - see ${helpUrl}`)
}
if (projectBased && !newConfig.projectId) {
throw new Error('Configuration must contain `projectId`')
}
const isBrowser = typeof window !== 'undefined' && window.location && window.location.hostname
const isLocalhost = isBrowser && isLocal(window.location.hostname)
if (isBrowser && isLocalhost && newConfig.token && newConfig.ignoreBrowserTokenWarning !== true) {
warnings.printBrowserTokenWarning()
} else if (typeof newConfig.useCdn === 'undefined') {
warnings.printCdnWarning()
}
if (projectBased) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- the nullability here is wrong, as line 48 throws an error if it's undefined
validate.projectId(newConfig.projectId!)
}
if (newConfig.dataset) {
validate.dataset(newConfig.dataset)
}
if ('requestTagPrefix' in newConfig) {
// Allow setting and unsetting request tag prefix
newConfig.requestTagPrefix = newConfig.requestTagPrefix
? validate.requestTag(newConfig.requestTagPrefix).replace(/\.+$/, '')
: undefined
}
newConfig.apiVersion = `${newConfig.apiVersion}`.replace(/^v/, '')
newConfig.isDefaultApi = newConfig.apiHost === defaultConfig.apiHost
// If `useCdn` is undefined, we treat it as `true`
newConfig.useCdn = newConfig.useCdn !== false && !newConfig.withCredentials
validateApiVersion(newConfig.apiVersion)
const hostParts = newConfig.apiHost.split('://', 2)
const protocol = hostParts[0]
const host = hostParts[1]
const cdnHost = newConfig.isDefaultApi ? defaultCdnHost : host
if (newConfig.useProjectHostname) {
newConfig.url = `${protocol}://${newConfig.projectId}.${host}/v${newConfig.apiVersion}`
newConfig.cdnUrl = `${protocol}://${newConfig.projectId}.${cdnHost}/v${newConfig.apiVersion}`
} else {
newConfig.url = `${newConfig.apiHost}/v${newConfig.apiVersion}`
newConfig.cdnUrl = newConfig.url
}
return newConfig
}