Skip to content

Commit

Permalink
feat: allow first request without specified stop IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
krystxf committed Apr 14, 2024
1 parent bfa7854 commit 8994ac6
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { stopIDs } from "./data/stop-ids";

const stopIDSchema = z.enum(stopIDs);

export const StopIDsSchema = z.array(stopIDSchema).nonempty().max(10);
export const StopIDsSchema = z.array(stopIDSchema).max(10);

export const SubscribeSchema = z.object({
subscribe: StopIDsSchema,
Expand Down
9 changes: 3 additions & 6 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ const fetchData = async (clientID?: string) => {
* If there are no departures and clientID is not
* provided, there is no need to update the state.
*/
if (!res.departures.length && !clientID) return;

if (res.departures.length === 0) return;

/**
* update the state with the fetched departures
Expand Down Expand Up @@ -94,11 +95,7 @@ const fetchData = async (clientID?: string) => {
const server = Bun.serve<ClientData>({
fetch(req, server) {
try {
const stopIDsHeaderRaw = req.headers.get(STOP_IDS_HEADER);
if (!stopIDsHeaderRaw) {
throw `"${STOP_IDS_HEADER}" header is missing`;
}

const stopIDsHeaderRaw = req.headers.get(STOP_IDS_HEADER) ?? "[]";
const StopIDsHeaderParsed: unknown = JSON.parse(stopIDsHeaderRaw);
const res = StopIDsSchema.safeParse(StopIDsHeaderParsed);
if (!res.success) {
Expand Down

0 comments on commit 8994ac6

Please sign in to comment.