Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom headers - Possible Implementation #6690

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/common/src/event-sources/json-feed-event-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ let eventSourceDef: EventSourceDef<JsonFeedMeta> = {
},

fetch(arg, success, failure) {
let { meta } = arg.eventSource
let { meta, additionalHeaders } = arg.eventSource
let requestParams = buildRequestParams(meta, arg.range, arg.context)

requestJson(
Expand All @@ -45,6 +45,7 @@ let eventSourceDef: EventSourceDef<JsonFeedMeta> = {
(errorMessage, xhr) => {
failure({ message: errorMessage, xhr })
},
additionalHeaders,
)
},

Expand Down
2 changes: 2 additions & 0 deletions packages/common/src/structs/event-source-parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const EVENT_SOURCE_REFINERS = { // does NOT include EVENT_UI_REFINERS
// for any network-related sources
success: identity as Identity<EventSourceSuccessResponseHandler>,
failure: identity as Identity<EventSourceErrorResponseHandler>,
additionalHeaders: identity as Identity<Headers>,
}

type BuiltInEventSourceRefiners = typeof EVENT_SOURCE_REFINERS &
Expand Down Expand Up @@ -76,6 +77,7 @@ export function parseEventSource(
meta: metaRes.meta,
ui: createEventUi(refined, context),
extendedProps: extra,
additionalHeaders: refined.additionalHeaders,
}
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/common/src/structs/event-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface EventSource<Meta> {
ui: EventUi
success: EventSourceSuccessResponseHandler | null
failure: EventSourceErrorResponseHandler | null
additionalHeaders: Headers | null
extendedProps: Dictionary // undocumented
}

Expand Down
8 changes: 7 additions & 1 deletion packages/common/src/util/requestJson.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Dictionary } from '../options'

export function requestJson(method: string, url: string, params: Dictionary, successCallback, failureCallback) {
export function requestJson(method: string, url: string, params: Dictionary, successCallback, failureCallback, additionalHeaders: Headers | null) {
method = method.toUpperCase()

let body = null
Expand All @@ -18,6 +18,12 @@ export function requestJson(method: string, url: string, params: Dictionary, suc
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
}

if (!!additionalHeaders) {
additionalHeaders.forEach((value: string, key: string) => {
xhr.setRequestHeader(key, value);
});
}

xhr.onload = () => {
if (xhr.status >= 200 && xhr.status < 400) {
let parsed = false
Expand Down
6 changes: 4 additions & 2 deletions packages/google-calendar/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ let eventSourceDef: EventSourceDef<GCalMeta> = {
fetch(arg, onSuccess, onFailure) {
let { dateEnv, options } = arg.context
let meta: GCalMeta = arg.eventSource.meta
let { additionalHeaders } = arg.eventSource;
let apiKey = meta.googleCalendarApiKey || options.googleCalendarApiKey

if (!apiKey) {
Expand Down Expand Up @@ -76,8 +77,9 @@ let eventSourceDef: EventSourceDef<GCalMeta> = {
}
}, (message, xhr) => {
onFailure({ message, xhr })
})
}
},
additionalHeaders,
)}
},
}

Expand Down
11 changes: 9 additions & 2 deletions packages/icalendar/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ let eventSourceDef: EventSourceDef<ICalFeedMeta> = {
},

fetch(arg, onSuccess, onFailure) {
let { meta } = arg.eventSource
let { meta, additionalHeaders } = arg.eventSource
let { internalState } = meta

function handleICalEvents(errorMessage, iCalExpander: IcalExpander, xhr) {
Expand Down Expand Up @@ -83,6 +83,7 @@ let eventSourceDef: EventSourceDef<ICalFeedMeta> = {
internalState.errorMessage = errorMessage
internalState.xhr = xhr
},
additionalHeaders,
)
} else if (!internalState.completed) {
internalState.callbacks.push(handleICalEvents)
Expand All @@ -92,9 +93,15 @@ let eventSourceDef: EventSourceDef<ICalFeedMeta> = {
},
}

function requestICal(url: string, successCallback: Success, failureCallback: Failure) {
function requestICal(url: string, successCallback: Success, failureCallback: Failure, additionalHeaders: Headers | null) {
const xhr = new XMLHttpRequest()
xhr.open('GET', url, true)
if (!!additionalHeaders) {
additionalHeaders.forEach((value: string, key: string) => {
xhr.setRequestHeader(key, value);
});
}

xhr.onload = () => {
if (xhr.status >= 200 && xhr.status < 400) {
successCallback(xhr.responseText, xhr)
Expand Down