Skip to content

Commit

Permalink
remove superagent
Browse files Browse the repository at this point in the history
  • Loading branch information
arshaw committed Jan 30, 2019
1 parent 5c44373 commit a4490bf
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 75 deletions.
1 change: 0 additions & 1 deletion demos/gcal.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<link href='../dist/core/main.css' rel='stylesheet' />
<link href='../dist/daygrid/main.css' rel='stylesheet' />
<link href='../dist/list/main.css' rel='stylesheet' />
<script src='../node_modules/superagent/superagent.js'></script>
<script src='../dist/core/main.js'></script>
<script src='../dist/interaction/main.js'></script>
<script src='../dist/daygrid/main.js'></script>
Expand Down
1 change: 0 additions & 1 deletion demos/json.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<link href='../dist/daygrid/main.css' rel='stylesheet' />
<link href='../dist/timegrid/main.css' rel='stylesheet' />
<link href='../dist/list/main.css' rel='stylesheet' />
<script src='../node_modules/superagent/superagent.js'></script>
<script src='../dist/core/main.js'></script>
<script src='../dist/interaction/main.js'></script>
<script src='../dist/daygrid/main.js'></script>
Expand Down
7 changes: 4 additions & 3 deletions demos/time-zones.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<link href='../dist/daygrid/main.css' rel='stylesheet' />
<link href='../dist/timegrid/main.css' rel='stylesheet' />
<link href='../dist/list/main.css' rel='stylesheet' />
<script src='../node_modules/superagent/superagent.js'></script>
<script src='../dist/core/main.js'></script>
<script src='../dist/interaction/main.js'></script>
<script src='../dist/daygrid/main.js'></script>
Expand Down Expand Up @@ -59,8 +58,8 @@
calendar.render();

// load the list of available timezones, build the <select> options
superagent.get('php/get-time-zones.php').end(function(err, res) {
var timeZones = res ? (res.body || JSON.parse(res.text)) : [];
// it's HIGHLY recommended to use a different library for network requests, not this internal util func
FullCalendar.requestJson('GET', 'php/get-time-zones.php', {}, function(timeZones) {

timeZones.forEach(function(timeZone) {
var optionEl;
Expand All @@ -72,6 +71,8 @@
timeZoneSelectorEl.appendChild(optionEl);
}
});
}, function() {
// TODO: handle error
});

// when the timezone selector changes, dynamically change the calendar option
Expand Down
1 change: 0 additions & 1 deletion karma.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ module.exports = function(config) {
// list of files / patterns to load in the browser
files: [

'node_modules/superagent/superagent.js',
'node_modules/moment/moment.js',
'node_modules/moment/locale/es.js', // only spanish for testing
'node_modules/moment-timezone/builds/moment-timezone-with-data.js',
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
"rollup-plugin-node-resolve": "^4.0.0",
"rollup-plugin-sourcemaps": "^0.4.2",
"rrule": "^2.6.0",
"superagent": "^3.8.3",
"tslib": "^1.9.3",
"tslint": "^5.12.1",
"tslint-config-standard": "^7.1.0",
Expand Down
1 change: 0 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ if (!/^(development|production)$/.test(process.env.BUILD)) {
}

let packageGlobals = {
superagent: 'superagent',
luxon: 'luxon',
moment: 'moment',
rrule: 'rrule',
Expand Down
37 changes: 9 additions & 28 deletions src/core/event-sources/json-feed-event-source.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import request from 'superagent'
import requestJson from '../util/requestJson'
import Calendar from '../Calendar'
import { EventSourceDef } from '../structs/event-source'
import { DateRange } from '../datelib/date-range'
Expand Down Expand Up @@ -35,36 +35,17 @@ let eventSourceDef: EventSourceDef = {

fetch(arg, success, failure) {
let meta: JsonFeedMeta = arg.eventSource.meta
let theRequest
let requestParams = buildRequestParams(meta, arg.range, arg.calendar)

if (meta.method === 'GET') {
theRequest = request.get(meta.url).query(requestParams) // querystring params
} else {
theRequest = request(meta.method, meta.url).send(requestParams) // body data
}

theRequest.end((error, res) => {
let rawEvents

if (!error) {
if (res.body) { // parsed JSON
rawEvents = res.body
} else if (res.text) {
// if the server doesn't set Content-Type, won't be parsed as JSON. parse anyway.
rawEvents = JSON.parse(res.text)
}

if (rawEvents) {
success({ rawEvents, response: res })
} else {
failure({ message: 'Invalid JSON response', response: res })
}

} else {
failure(error) // error has { error, response }
requestJson(
meta.method, meta.url, requestParams,
function(rawEvents, xhr) {
success({ rawEvents, xhr })
},
function(errorMessage, xhr) {
failure({ message: errorMessage, xhr })
}
})
)
}

}
Expand Down
2 changes: 2 additions & 0 deletions src/core/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,5 @@ export { default as Slicer, SlicedProps } from './common/slicing-utils'
export { EventMutation, applyMutationToEventStore } from './structs/event-mutation'
export { Constraint, ConstraintInput, AllowFunc, isPropsValid, isInteractionValid } from './validation'
export { default as EventApi } from './api/EventApi'

export { default as requestJson } from './util/requestJson'
5 changes: 0 additions & 5 deletions src/core/package.json

This file was deleted.

4 changes: 2 additions & 2 deletions src/core/reducers/eventSources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@ function fetchSource(eventSource: EventSource, fetchRange: DateRange, calendar:
let sourceSuccessRes

if (eventSource.success) {
sourceSuccessRes = eventSource.success(rawEvents, res.response)
sourceSuccessRes = eventSource.success(rawEvents, res.xhr)
}
if (calSuccess) {
calSuccessRes = calSuccess(rawEvents, res.response)
calSuccessRes = calSuccess(rawEvents, res.xhr)
}

rawEvents = sourceSuccessRes || calSuccessRes || rawEvents
Expand Down
2 changes: 1 addition & 1 deletion src/core/structs/event-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export type EventSourceFetcher = (
calendar: Calendar
range: DateRange
},
success: (res: { rawEvents: EventInput[], response?: any }) => void,
success: (res: { rawEvents: EventInput[], xhr?: XMLHttpRequest }) => void,
failure: (error: EventSourceError) => void
) => (void | PromiseLike<EventInput[]>)

Expand Down
49 changes: 49 additions & 0 deletions src/core/util/requestJson.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

export default function requestJson(method: string, url: string, params: object, successCallback, failureCallback) {
method = method.toUpperCase()

let body = null
if (method === 'GET') {
url = injectQueryStringParams(url, params)
} else {
body = encodeParams(params)
}

let xhr = new XMLHttpRequest()
xhr.open(method, url, true)

xhr.onload = function() {
if (xhr.status >= 200 && xhr.status < 400) {
try {
let res = JSON.parse(xhr.responseText)
successCallback(res, xhr)
} catch (err) {
failureCallback('Failure parsing JSON', xhr)
}
} else {
failureCallback('Request failed', xhr)
}
}

xhr.onerror = function() {
failureCallback('Request failed', xhr)
}

xhr.send(body)
}

function injectQueryStringParams(url: string, params) {
return url +
(url.indexOf('?') === -1 ? '?' : '&') +
encodeParams(params)
}

function encodeParams(params) {
let parts = []

for (let key in params) {
parts.push(encodeURIComponent(key) + '=' + encodeURIComponent(params[key]))
}

return parts.join('&')
}
49 changes: 22 additions & 27 deletions src/gcal/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as request from 'superagent'
import { createPlugin, EventSourceDef, refineProps, addDays, DateEnv } from '@fullcalendar/core'
import { createPlugin, EventSourceDef, refineProps, addDays, DateEnv, requestJson } from '@fullcalendar/core'

// TODO: expose somehow
const API_BASE = 'https://www.googleapis.com/calendar/v3/calendars'
Expand Down Expand Up @@ -53,31 +52,27 @@ let eventSourceDef: EventSourceDef = {
calendar.dateEnv
)

request.get(url)
.query(requestParams)
.end((error, res) => {

// make an error object with more info if we can
if (res && res.body && res.body.error) {
error = {
message: 'Google Calendar API: ' + res.body.error.message,
response: res,
errors: res.body.error.errors
}
}

if (error) {
onFailure(error)
} else {
onSuccess({
rawEvents: gcalItemsToRawEventDefs(
res.body.items,
requestParams.timeZone
),
response: res
})
}
})
requestJson('GET', url, requestParams, function(body, xhr) {

if (body.error) {
onFailure({
message: 'Google Calendar API: ' + body.error.message,
errors: body.error.errors,
xhr
})
} else {
onSuccess({
rawEvents: gcalItemsToRawEventDefs(
body.items,
requestParams.timeZone
),
xhr
})
}

}, function(message, xhr) {
onFailure({ message, xhr })
})
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/gcal/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
{
"title": "FullCalendar Google Calendar Plugin",
"dependencies": {
"superagent": ""
}
"title": "FullCalendar Google Calendar Plugin"
}

0 comments on commit a4490bf

Please sign in to comment.