Skip to content

Commit 71b46e6

Browse files
committed
refactor(manager): use constants for API URL fetches
1 parent 6036e20 commit 71b46e6

File tree

6 files changed

+27
-10
lines changed

6 files changed

+27
-10
lines changed

lib/alerts/actions/alerts.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import fetch from 'isomorphic-fetch'
33
import { fetchStopsAndRoutes } from '../../gtfs/actions/general'
44

55
import { secureFetch } from '../../common/actions'
6+
import {GTFS_API_PREFIX} from '../../common/constants'
67
import { getAlertsUrl, getFeedId } from '../../common/util/modules'
78
import { setErrorMessage } from '../../manager/actions/status'
89
import {getActiveProject} from '../../manager/selectors'
@@ -129,7 +130,9 @@ export function editAlert (alert) {
129130
export function fetchEntity (entity, activeProject) {
130131
const feed = activeProject.feedSources.find(f => getFeedId(f) === entity.entity.AgencyId)
131132
const feedId = getFeedId(feed)
132-
const url = entity.type === 'stop' ? `/api/manager/stops/${entity.entity.StopId}?feed=${feedId}` : `/api/manager/routes/${entity.entity.RouteId}?feed=${feedId}`
133+
const url = entity.type === 'stop'
134+
? `${GTFS_API_PREFIX}stops/${entity.entity.StopId}?feed=${feedId}`
135+
: `${GTFS_API_PREFIX}routes/${entity.entity.RouteId}?feed=${feedId}`
133136
return fetch(url)
134137
.then((response) => {
135138
return response.json()

lib/common/constants/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const SECURE: string = 'secure/'
2+
export const API_PREFIX: string = `/api/manager/`
3+
export const SECURE_API_PREFIX: string = `${API_PREFIX}${SECURE}`
4+
// TODO: move GTFS_API_PREFIX to gtfs sub path
5+
// export const GTFS_API_PREFIX: string = `${API_PREFIX}gtfs/`
6+
export const GTFS_API_PREFIX: string = `${API_PREFIX}`
7+
export const GTFS_GRAPHQL_PREFIX: string = `${GTFS_API_PREFIX}graphql`
8+
export const EDITOR_PREFIX: string = `/api/editor/`
9+
export const SECURE_EDITOR_PREFIX: string = `${EDITOR_PREFIX}${SECURE}`

lib/gtfs/actions/general.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { clearStops } from './stops'
44
import { clearPatterns } from './patterns'
55
import { clearRoutes } from './routes'
66
import { stopsAndRoutes, compose, patternsAndStopsForBoundingBox } from '../util/graphql'
7+
import {GTFS_GRAPHQL_PREFIX} from '../../common/constants'
78
import { getFeedId } from '../../common/util/modules'
89
import {getActiveProject} from '../../manager/selectors'
910

@@ -78,7 +79,7 @@ export function fetchStopsAndRoutes (entities, module) {
7879
variables: JSON.stringify({feedId, routeId, stopId})
7980
})
8081
dispatch(requestStopsAndRoutes(feedId, routeId, stopId, module))
81-
return fetch(`/api/manager/graphql`, {method, body})
82+
return fetch(GTFS_GRAPHQL_PREFIX, {method, body})
8283
.then(response => response.json())
8384
.then(results => dispatch(receivedStopsAndRoutes(results, module)))
8485
}

lib/gtfs/components/gtfsmapsearch.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {connect} from 'react-redux'
33
import fetch from 'isomorphic-fetch'
44
import { Button } from 'react-bootstrap'
55

6+
import {GTFS_API_PREFIX} from '../../common/constants'
67
import ActiveGtfsMap from '../containers/ActiveGtfsMap'
78
import GtfsSearch from './gtfssearch'
89

@@ -24,7 +25,7 @@ class GtfsMapSearch extends Component {
2425
}
2526

2627
getPatterns (input) {
27-
return fetch(`/api/manager/patterns?route=${input.route.route_id}&feed=${input.route.feed_id}`)
28+
return fetch(`${GTFS_API_PREFIX}patterns?route=${input.route.route_id}&feed=${input.route.feed_id}`)
2829
.then((response) => {
2930
return response.json()
3031
})

lib/gtfs/util/graphql.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// @flow
22

3+
import {GTFS_GRAPHQL_PREFIX} from '../../common/constants'
4+
35
// variable names/keys must match those specified in GraphQL schema
46
export function compose (query: string, variables: Object) {
5-
return `/api/manager/graphql?query=${encodeURIComponent(query)}&variables=${encodeURIComponent(JSON.stringify(variables))}`
7+
return `${GTFS_GRAPHQL_PREFIX}?query=${encodeURIComponent(query)}&variables=${encodeURIComponent(JSON.stringify(variables))}`
68
}
79

810
export const feed = `

lib/manager/actions/versions.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import S3 from 'aws-sdk/clients/s3'
44

55
import { secureFetch } from '../../common/actions'
66
import { getConfigProperty } from '../../common/util/config'
7+
import {GTFS_GRAPHQL_PREFIX, SECURE_API_PREFIX} from '../../common/constants'
78
import fileDownload from '../../common/util/file-download'
89
import { setErrorMessage, startJobMonitor } from './status'
910
import { fetchFeedSource } from './feeds'
@@ -59,7 +60,7 @@ export function receiveFeedVersion (feedVersion) {
5960
export function fetchFeedVersion (feedVersionId) {
6061
return function (dispatch, getState) {
6162
dispatch(requestingFeedVersion())
62-
const url = `/api/manager/secure/feedversion/${feedVersionId}`
63+
const url = `${SECURE_API_PREFIX}feedversion/${feedVersionId}`
6364
return dispatch(secureFetch(url))
6465
.then(response => response.json())
6566
.then(version => {
@@ -85,7 +86,7 @@ export function publishedFeedVersion (feedVersion) {
8586
export function publishFeedVersion (feedVersion) {
8687
return function (dispatch, getState) {
8788
dispatch(publishingFeedVersion(feedVersion))
88-
const url = `/api/manager/secure/feedversion/${feedVersion.id}/publish`
89+
const url = `${SECURE_API_PREFIX}feedversion/${feedVersion.id}/publish`
8990
return dispatch(secureFetch(url, 'post'))
9091
.then(response => response.json())
9192
.then(version => {
@@ -170,7 +171,7 @@ export function deletingFeedVersion (feedVersion) {
170171
export function deleteFeedVersion (feedVersion, changes) {
171172
return function (dispatch, getState) {
172173
dispatch(deletingFeedVersion(feedVersion))
173-
const url = '/api/manager/secure/feedversion/' + feedVersion.id
174+
const url = `${SECURE_API_PREFIX}feedversion/${feedVersion.id}`
174175
return dispatch(secureFetch(url, 'delete'))
175176
.then((res) => {
176177
// fetch feed source with versions + snapshots
@@ -243,7 +244,7 @@ export function fetchFeedVersionIsochrones (feedVersion, fromLat, fromLon, toLat
243244
}
244245
dispatch(requestingFeedVersionIsochrones(feedVersion, fromLat, fromLon, toLat, toLon, date, fromTime, toTime))
245246
const params = {fromLat, fromLon, toLat, toLon, date, fromTime, toTime}
246-
const url = `/api/manager/secure/feedversion/${feedVersion.id}/isochrones?${qs.stringify(params)}`
247+
const url = `${SECURE_API_PREFIX}feedversion/${feedVersion.id}/isochrones?${qs.stringify(params)}`
247248
return dispatch(secureFetch(url))
248249
.then(res => {
249250
console.log(res.status)
@@ -310,7 +311,7 @@ export function creatingFeedVersionFromSnapshot () {
310311
export function createFeedVersionFromSnapshot (feedSource, snapshotId) {
311312
return function (dispatch, getState) {
312313
dispatch(creatingFeedVersionFromSnapshot())
313-
const url = `/api/manager/secure/feedversion/fromsnapshot?feedSourceId=${feedSource.id}&snapshotId=${snapshotId}`
314+
const url = `${SECURE_API_PREFIX}feedversion/fromsnapshot?feedSourceId=${feedSource.id}&snapshotId=${snapshotId}`
314315
return dispatch(secureFetch(url, 'post'))
315316
.then((res) => {
316317
if (res) dispatch(startJobMonitor())
@@ -327,7 +328,7 @@ export function renamingFeedVersion () {
327328
export function renameFeedVersion (feedVersion, name) {
328329
return function (dispatch, getState) {
329330
dispatch(renamingFeedVersion())
330-
const url = `/api/manager/secure/feedversion/${feedVersion.id}/rename?name=${name}`
331+
const url = `${SECURE_API_PREFIX}feedversion/${feedVersion.id}/rename?name=${name}`
331332
return dispatch(secureFetch(url, 'put'))
332333
.then((res) => {
333334
dispatch(fetchFeedVersion(feedVersion.id))

0 commit comments

Comments
 (0)