Skip to content

Commit 3edc3ce

Browse files
committed
feat(lib-util): add helper for constructing request headers
1 parent 8436443 commit 3edc3ce

File tree

1 file changed

+14
-0
lines changed
  • packages/netlify-cms-lib-util/src

1 file changed

+14
-0
lines changed

packages/netlify-cms-lib-util/src/API.ts

+14
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,20 @@ export async function requestWithBackoff(
116116
}
117117
}
118118

119+
type HeaderObj = Record<string, string>
120+
121+
type HeaderConfig = {
122+
headers?: HeaderObj,
123+
token?: string
124+
}
125+
async function constructRequestHeaders(headerConfig: HeaderConfig) {
126+
const { token, headers } = headerConfig
127+
const baseHeaders: HeaderObj = {'Content-Type': 'application/json; charset=utf-8', ...headers}
128+
if (token) {
129+
baseHeaders['Authorization'] = `token ${token}`
130+
}
131+
return Promise.resolve(baseHeaders)
132+
}
119133
export async function readFile(
120134
id: string | null | undefined,
121135
fetchContent: () => Promise<string | Blob>,

0 commit comments

Comments
 (0)