Allow sending headers in extension widget

This commit is contained in:
Svilen Markov 2025-03-26 17:59:41 +00:00
parent ca2732668e
commit c6e0230e5d
2 changed files with 16 additions and 0 deletions

View File

@ -1413,6 +1413,7 @@ Display a widget provided by an external source (3rd party). If you want to lear
| url | string | yes | | | url | string | yes | |
| fallback-content-type | string | no | | | fallback-content-type | string | no | |
| allow-potentially-dangerous-html | boolean | no | false | | allow-potentially-dangerous-html | boolean | no | false |
| headers | key & value | no | |
| parameters | key & value | no | | | parameters | key & value | no | |
##### `url` ##### `url`
@ -1421,6 +1422,14 @@ The URL of the extension. **Note that the query gets stripped from this URL and
##### `fallback-content-type` ##### `fallback-content-type`
Optionally specify the fallback content type of the extension if the URL does not return a valid `Widget-Content-Type` header. Currently the only supported value for this property is `html`. Optionally specify the fallback content type of the extension if the URL does not return a valid `Widget-Content-Type` header. Currently the only supported value for this property is `html`.
##### `headers`
Optionally specify the headers that will be sent with the request. Example:
```yaml
headers:
x-api-key: ${SECRET_KEY}
```
##### `allow-potentially-dangerous-html` ##### `allow-potentially-dangerous-html`
Whether to allow the extension to display HTML. Whether to allow the extension to display HTML.

View File

@ -22,6 +22,7 @@ type extensionWidget struct {
URL string `yaml:"url"` URL string `yaml:"url"`
FallbackContentType string `yaml:"fallback-content-type"` FallbackContentType string `yaml:"fallback-content-type"`
Parameters queryParametersField `yaml:"parameters"` Parameters queryParametersField `yaml:"parameters"`
Headers map[string]string `yaml:"headers"`
AllowHtml bool `yaml:"allow-potentially-dangerous-html"` AllowHtml bool `yaml:"allow-potentially-dangerous-html"`
Extension extension `yaml:"-"` Extension extension `yaml:"-"`
cachedHTML template.HTML `yaml:"-"` cachedHTML template.HTML `yaml:"-"`
@ -46,6 +47,7 @@ func (widget *extensionWidget) update(ctx context.Context) {
URL: widget.URL, URL: widget.URL,
FallbackContentType: widget.FallbackContentType, FallbackContentType: widget.FallbackContentType,
Parameters: widget.Parameters, Parameters: widget.Parameters,
Headers: widget.Headers,
AllowHtml: widget.AllowHtml, AllowHtml: widget.AllowHtml,
}) })
@ -85,6 +87,7 @@ type extensionRequestOptions struct {
URL string `yaml:"url"` URL string `yaml:"url"`
FallbackContentType string `yaml:"fallback-content-type"` FallbackContentType string `yaml:"fallback-content-type"`
Parameters queryParametersField `yaml:"parameters"` Parameters queryParametersField `yaml:"parameters"`
Headers map[string]string `yaml:"headers"`
AllowHtml bool `yaml:"allow-potentially-dangerous-html"` AllowHtml bool `yaml:"allow-potentially-dangerous-html"`
} }
@ -113,6 +116,10 @@ func fetchExtension(options extensionRequestOptions) (extension, error) {
request.URL.RawQuery = options.Parameters.toQueryString() request.URL.RawQuery = options.Parameters.toQueryString()
} }
for key, value := range options.Headers {
request.Header.Add(key, value)
}
response, err := http.DefaultClient.Do(request) response, err := http.DefaultClient.Do(request)
if err != nil { if err != nil {
slog.Error("Failed fetching extension", "url", options.URL, "error", err) slog.Error("Failed fetching extension", "url", options.URL, "error", err)