Skip to content

Commit

Permalink
feat: telefuncConfig.httpHeaders
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Feb 28, 2022
1 parent 6035d97 commit 28993e1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions telefunc/client/callTelefunc/makeHttpRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ async function makeHttpRequest(callContext: {
telefuncUrl: string
httpRequestBody: string
telefunctionName: string
httpHeaders: Record<string, string>
}): Promise<{ telefunctionReturn: unknown } | { telefunctionCallError: TelefunctionError }> {
const method = 'POST'

Expand All @@ -19,6 +20,7 @@ async function makeHttpRequest(callContext: {
body: callContext.httpRequestBody,
credentials: 'same-origin',
headers: {
...callContext.httpHeaders,
'Content-Type': 'text/plain',
},
})
Expand Down
14 changes: 14 additions & 0 deletions telefunc/client/telefuncConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ assertProxySupport()
type TelefuncClientConfig = {
/** The Telefunc HTTP endpoint URL, e.g. `https://example.org/_telefunc`. Default: `/_telefunc`. */
telefuncUrl: string
httpHeaders: Record<string, string>
}

const configSpec = {
Expand All @@ -24,6 +25,17 @@ const configSpec = {
return '/_telefunc'
},
},
httpHeaders: {
validate(val: unknown) {
assertUsage(
typeof val === 'object' && val !== null && Object.values(val).every((v) => typeof v === 'string'),
'`telefuncConfig.httpHeaders` should be an object of strings',
)
},
getDefault() {
return {}
},
},
}

function getTelefuncConfigObject(): TelefuncClientConfig {
Expand All @@ -32,6 +44,8 @@ function getTelefuncConfigObject(): TelefuncClientConfig {
{
// prettier-ignore
get telefuncUrl() { return configProvidedByUser['telefuncUrl'] ?? configSpec['telefuncUrl'].getDefault() },
// prettier-ignore
get httpHeaders() { return configProvidedByUser['httpHeaders'] ?? configSpec['httpHeaders'].getDefault() },
},
{ set },
)
Expand Down

0 comments on commit 28993e1

Please sign in to comment.