diff --git a/jsconfig.json b/jsconfig.json new file mode 100644 index 0000000..84f31a1 --- /dev/null +++ b/jsconfig.json @@ -0,0 +1,5 @@ +{ + "extends": "./.svelte-kit/tsconfig.json", + "compilerOptions": {}, + "include": ["src"] +} diff --git a/package-lock.json b/package-lock.json index 5a23000..235a2cb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "0.0.1", "dependencies": { "@fortawesome/free-regular-svg-icons": "^6.6.0", - "onebusaway-sdk": "^0.1.0-alpha.31" + "onebusaway-sdk": "^1.0.1" }, "devDependencies": { "@fortawesome/fontawesome-svg-core": "^6.5.2", @@ -3281,10 +3281,9 @@ } }, "node_modules/onebusaway-sdk": { - "version": "0.1.0-alpha.31", - "resolved": "https://registry.npmjs.org/onebusaway-sdk/-/onebusaway-sdk-0.1.0-alpha.31.tgz", - "integrity": "sha512-gL+qAwo9f/J6/bDgoWhj6xwPSeGPj2p5EopKxyKse82LVTb79jUYUWru42V57JGkHvKFF3lA9GYLXr2deFYjkQ==", - "license": "Apache-2.0", + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/onebusaway-sdk/-/onebusaway-sdk-1.0.1.tgz", + "integrity": "sha512-QBM7xB9KT1txhVkejyaUeZol36bgNaKEMs0xCvMC9IBvmyCZwIGXomUO6MilWnzlVUFF62Z2A4Dcfeu4PxbSZA==", "dependencies": { "@types/node": "^18.11.18", "@types/node-fetch": "^2.6.4", diff --git a/package.json b/package.json index 48ec675..d80b5b9 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,6 @@ "type": "module", "dependencies": { "@fortawesome/free-regular-svg-icons": "^6.6.0", - "onebusaway-sdk": "^0.1.0-alpha.31" + "onebusaway-sdk": "^1.0.1" } } diff --git a/src/lib/RestAPI/arrivalsAndDeparturesForStop.js b/src/lib/RestAPI/arrivalsAndDeparturesForStop.js deleted file mode 100644 index 47237e4..0000000 --- a/src/lib/RestAPI/arrivalsAndDeparturesForStop.js +++ /dev/null @@ -1,20 +0,0 @@ -import { error, json } from '@sveltejs/kit'; -import { PUBLIC_OBA_SERVER_URL as baseURL } from '$env/static/public'; -import { PRIVATE_OBA_API_KEY as apiKey } from '$env/static/private'; - -import onebusaway from 'onebusaway-sdk'; - -const oba = new onebusaway({ - baseURL, - apiKey -}); - -export default async function (stopID) { - const response = await oba.arrivalAndDeparture.list(stopID); - - if (response.code !== 200) { - error(500, 'Unable to fetch arrivals-and-departures-for-stop.'); - } - - return json(response); -} diff --git a/src/lib/RestAPI/tripDetails.js b/src/lib/RestAPI/tripDetails.js deleted file mode 100644 index 722c93a..0000000 --- a/src/lib/RestAPI/tripDetails.js +++ /dev/null @@ -1,24 +0,0 @@ -import { error, json } from '@sveltejs/kit'; -import { PUBLIC_OBA_SERVER_URL as baseURL } from '$env/static/public'; -import { PRIVATE_OBA_API_KEY as apiKey } from '$env/static/private'; - -export async function GET({ params, url }) { - const { tripId } = params; - const vehicleId = url.searchParams.get('vehicleId'); - const serviceDate = url.searchParams.get('serviceDate'); - - let apiURL = `${baseURL}/api/where/trip-details/${tripId}.json?key=${apiKey}`; - - if (vehicleId) apiURL += `&vehicleId=${vehicleId}`; - if (serviceDate) apiURL += `&serviceDate=${serviceDate}`; - - const response = await fetch(apiURL); - - if (!response.ok) { - error(500, 'Unable to fetch trip-details.'); - return; - } - - const data = await response.json(); - return json(data); -} diff --git a/src/lib/RestAPI/stop.js b/src/lib/obaSdk.js similarity index 68% rename from src/lib/RestAPI/stop.js rename to src/lib/obaSdk.js index a5a7519..ab13781 100644 --- a/src/lib/RestAPI/stop.js +++ b/src/lib/obaSdk.js @@ -1,20 +1,19 @@ -import { error, json } from '@sveltejs/kit'; - +import onebusaway from 'onebusaway-sdk'; import { PUBLIC_OBA_SERVER_URL as baseURL } from '$env/static/public'; import { PRIVATE_OBA_API_KEY as apiKey } from '$env/static/private'; -import onebusaway from 'onebusaway-sdk'; +import { error, json } from '@sveltejs/kit'; const oba = new onebusaway({ baseURL, apiKey }); -export default async function (stopID) { - const response = await oba.stop.retrieve(stopID); - +export function handleOBAResponse(response, entityName) { if (response.code !== 200) { - error(500, 'Unable to fetch arrivals-and-departures-for-stop.'); + return error(500, `Unable to fetch ${entityName}.`); } return json(response); } + +export default oba; diff --git a/src/routes/api/oba/arrivals-and-departures-for-stop/[id]/+server.js b/src/routes/api/oba/arrivals-and-departures-for-stop/[id]/+server.js index 9ebfd51..0e034f5 100644 --- a/src/routes/api/oba/arrivals-and-departures-for-stop/[id]/+server.js +++ b/src/routes/api/oba/arrivals-and-departures-for-stop/[id]/+server.js @@ -1,6 +1,8 @@ -import arrivalDepartureAPI from '$lib/RestAPI/arrivalsAndDeparturesForStop'; +import oba, { handleOBAResponse } from '$lib/obaSdk.js'; /** @type {import('./$types').RequestHandler} */ export async function GET({ params }) { - return arrivalDepartureAPI(params.id); + const stopID = params.id; + const response = await oba.arrivalAndDeparture.list(stopID); + return handleOBAResponse(response, 'arrivals-and-departures-for-stop'); } diff --git a/src/routes/api/oba/shape/[shapeId]/+server.js b/src/routes/api/oba/shape/[shapeId]/+server.js index 8eb0596..f61bdb5 100644 --- a/src/routes/api/oba/shape/[shapeId]/+server.js +++ b/src/routes/api/oba/shape/[shapeId]/+server.js @@ -1,23 +1,9 @@ -import { error, json } from '@sveltejs/kit'; -import { PUBLIC_OBA_SERVER_URL as baseURL } from '$env/static/public'; -import { PRIVATE_OBA_API_KEY as apiKey } from '$env/static/private'; +import oba, { handleOBAResponse } from '$lib/obaSdk.js'; +/** @type {import('./$types').RequestHandler} */ export async function GET({ params }) { - const { shapeId } = params; + const shapeId = params.shapeId; + const response = await oba.shape.retrieve(shapeId); - let apiURL = `${baseURL}/api/where/shape/${shapeId}.json?key=${apiKey}`; - - try { - const response = await fetch(apiURL); - - if (!response.ok) { - throw new Error(`HTTP error! status: ${response.status}`); - } - - const data = await response.json(); - return json(data); - } catch (err) { - console.error('Error fetching shape data:', err); - throw error(500, 'Unable to fetch shape data.'); - } + return handleOBAResponse(response, 'shape'); } diff --git a/src/routes/api/oba/stop/[stopID]/+server.js b/src/routes/api/oba/stop/[stopID]/+server.js index aed93b1..6c3add6 100644 --- a/src/routes/api/oba/stop/[stopID]/+server.js +++ b/src/routes/api/oba/stop/[stopID]/+server.js @@ -1,6 +1,10 @@ -import stopAPI from '$lib/RestAPI/stop'; +import oba, { handleOBAResponse } from '$lib/obaSdk'; /** @type {import('./$types').RequestHandler} */ export async function GET({ params }) { - return stopAPI(params.stopID); + const stopID = params.stopID; + + const response = await oba.stop.retrieve(stopID); + + return handleOBAResponse(response, 'stop'); } diff --git a/src/routes/api/oba/stops-for-location/+server.js b/src/routes/api/oba/stops-for-location/+server.js index e20af1b..63d8f76 100644 --- a/src/routes/api/oba/stops-for-location/+server.js +++ b/src/routes/api/oba/stops-for-location/+server.js @@ -1,21 +1,16 @@ -import { error, json } from '@sveltejs/kit'; - -import { PUBLIC_OBA_SERVER_URL as baseURL } from '$env/static/public'; - -import { PRIVATE_OBA_API_KEY as apiKey } from '$env/static/private'; +import oba, { handleOBAResponse } from '$lib/obaSdk'; /** @type {import('./$types').RequestHandler} */ export async function GET({ url }) { - const lat = url.searchParams.get('lat'); - const lng = url.searchParams.get('lng'); - const apiURL = `${baseURL}/api/where/stops-for-location.json?key=${apiKey}&lat=${lat}&lon=${lng}`; - const response = await fetch(apiURL); + const lat = +url.searchParams.get('lat'); + const lng = +url.searchParams.get('lng'); + + const queryParams = { + lat: lat, + lon: lng + }; - if (!response.ok) { - error(500, 'Unable to fetch stops-for-location.'); - return; - } + const response = await oba.stopsForLocation.list(queryParams); - const data = await response.json(); - return json(data); + return handleOBAResponse(response, 'stops-for-location'); } diff --git a/src/routes/api/oba/trip-details/[tripId]/+server.js b/src/routes/api/oba/trip-details/[tripId]/+server.js index d499846..9d72265 100644 --- a/src/routes/api/oba/trip-details/[tripId]/+server.js +++ b/src/routes/api/oba/trip-details/[tripId]/+server.js @@ -1,6 +1,4 @@ -import { error, json } from '@sveltejs/kit'; -import { PUBLIC_OBA_SERVER_URL as baseURL } from '$env/static/public'; -import { PRIVATE_OBA_API_KEY as apiKey } from '$env/static/private'; +import oba, { handleOBAResponse } from '$lib/obaSdk.js'; export async function GET({ params, url }) { const { tripId } = params; @@ -11,34 +9,20 @@ export async function GET({ params, url }) { const includeStatus = url.searchParams.get('includeStatus') || 'true'; const time = url.searchParams.get('time'); - let apiURL = `${baseURL}/api/where/trip-details/${tripId}.json?key=${apiKey}`; + const queryParams = { + includeTrip, + includeSchedule, + includeStatus + }; - if (serviceDate) apiURL += `&serviceDate=${serviceDate}`; - apiURL += `&includeTrip=${includeTrip}`; - apiURL += `&includeSchedule=${includeSchedule}`; - apiURL += `&includeStatus=${includeStatus}`; - if (time) apiURL += `&time=${time}`; - - try { - const response = await fetch(apiURL); - - if (!response.ok) { - throw new Error(`HTTP error! status: ${response.status}`); - } - - const data = await response.json(); - - // Ensure stops are included in the references - if (!data.data.references || !data.data.references.stops) { - // If stops are not included, we might need to fetch them separately - // This is a placeholder for that logic - data.data.references = data.data.references || {}; - data.data.references.stops = []; // Fetch stops here if needed - } + if (time) { + queryParams.time = time; + } - return json(data); - } catch (err) { - console.error('Error fetching trip details:', err); - throw error(500, 'Unable to fetch trip details.'); + if (serviceDate) { + queryParams.serviceDate = serviceDate; } + const response = await oba.tripDetails.retrieve(tripId, queryParams); + + return handleOBAResponse(response, 'trip-details'); } diff --git a/src/routes/stops/[stopID]/+page.server.js b/src/routes/stops/[stopID]/+page.server.js index 462ff55..53bfa0a 100644 --- a/src/routes/stops/[stopID]/+page.server.js +++ b/src/routes/stops/[stopID]/+page.server.js @@ -1,12 +1,17 @@ -import stopAPI from '$lib/RestAPI/stop.js'; -import arrivalDepartureAPI from '$lib/RestAPI/arrivalsAndDeparturesForStop.js'; +import oba, { handleOBAResponse } from '$lib/obaSdk.js'; export async function load({ params }) { const stopID = params.stopID; - const stopResponse = await stopAPI(stopID); - const stopBody = await stopResponse.json(); - const arrivalsAndDeparturesResponse = await arrivalDepartureAPI(stopID); - const arrivalsAndDeparturesResponseJSON = await arrivalsAndDeparturesResponse.json(); + + const stopResponse = await oba.stop.retrieve(stopID); + const stopBody = await handleOBAResponse(stopResponse, 'stop').json(); + + const arrivalsAndDeparturesResponse = await oba.arrivalAndDeparture.list(stopID); + + const arrivalsAndDeparturesResponseJSON = await handleOBAResponse( + arrivalsAndDeparturesResponse, + 'arrivals-and-departures-for-stop' + ).json(); return { stopID: params.stopID, diff --git a/svelte.config.js b/svelte.config.js index 8d3bc0e..e1de387 100644 --- a/svelte.config.js +++ b/svelte.config.js @@ -10,7 +10,8 @@ const config = { adapter: adapter(), alias: { $components: './src/components', - $images: './src/assets/images' + $images: './src/assets/images', + $lib: './src/lib' } }, preprocess: vitePreprocess()