Skip to content

Commit ca63bd1

Browse files
chore: migrate from minimal-cors-server to simple-proxy-server to simplify request making
1 parent 1a0922d commit ca63bd1

File tree

8 files changed

+41
-38
lines changed

8 files changed

+41
-38
lines changed

.github/workflows/firebase-hosting-merge.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
runs-on: ubuntu-latest
1313

1414
env:
15-
VITE_PUBLIC_KIT_API_KEY: '${{ secrets.VITE_PUBLIC_KIT_API_KEY }}'
15+
VITE_CORS_PROXY_SERVER_URL: '${{ vars.VITE_CORS_PROXY_SERVER_URL }}'
1616

1717
steps:
1818
- uses: actions/checkout@v2

.github/workflows/firebase-hosting-pull-request.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ jobs:
77
build_and_preview:
88
if: '${{ github.event.pull_request.head.repo.full_name == github.repository }}'
99
runs-on: ubuntu-latest
10+
11+
env:
12+
VITE_CORS_PROXY_SERVER_URL: '${{ vars.VITE_CORS_PROXY_SERVER_URL }}'
13+
1014
steps:
1115
- uses: actions/checkout@v2
1216
- run: npm ci && npm run build

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66

77
A web app to display lectures of the [KIT](https://www.kit.edu/) for a given time and day.
88

9-
Retrieves events from the [KIT extended search](https://campus.kit.edu/sp/campus/all/extendedSearch.asp) endpoint. Uses my [`minimal-cors-server`](https://github.com/MatthiasHarzer/minimal-cors-server) to avoid CORS problems with the KIT server.
9+
Retrieves events from the [KIT extended search](https://campus.kit.edu/sp/campus/all/extendedSearch.asp) endpoint. Uses my [`simple-proxy-server`](https://github.com/MatthiasHarzer/simple-proxy-server) to avoid CORS problems with the KIT server.
1010

1111
Built using [Svelte](https://svelte.dev/).

src/lib/consts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const KIT_BASE_URL = "https://campus.kit.edu";
22

3-
export const CORS_PROXY_SERVER = "https://cors.taptwice.dev/";
3+
export const CORS_PROXY_SERVER = import.meta.env.VITE_CORS_PROXY_SERVER_URL;
44
export const EXTENDED_SEARCH_BASE_URL = `${KIT_BASE_URL}/sp/campus/all/extendedSearch.asp`;
55
export const TERMS_URL = `${KIT_BASE_URL}/sp/server/services/kit/terms.asp`;
66
export const ROOMS_QUICK_SEARCH_URL = `${KIT_BASE_URL}/sp/server/services/kit/quicksearch.asp?type=room&find=`;

src/lib/event_handler.ts

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { format_date } from "./util/util";
1+
import { format_date, json_to_encoded_form_data } from "./util/util";
22
import type {
3-
CorsProxyBody,
43
KITEvent,
54
KITExtendedSearchFormData,
65
KITRoomEventsConfig,
@@ -33,7 +32,6 @@ let current_room_fetch_id = 0;
3332

3433
const i_promise_terms = new Promise<Term[]>((resolve, reject) => {
3534
make_request(TERMS_URL, {
36-
cache: true,
3735
max_age: 60 * 60 * 24, // 1 day
3836
})
3937
.then((response) => response.json())
@@ -81,28 +79,25 @@ const get_form_data_template = async (
8179
* @param params The parameters for the request.
8280
* @returns The response.
8381
*
84-
* @see https://github.com/MatthiasHarzer/minimal-cors-server
82+
* @see https://github.com/MatthiasHarzer/simple-proxy-server
8583
*/
8684
function make_request(
8785
url: string,
8886
params: RequestParams = {},
8987
): Promise<Response> {
90-
const { form_data, headers, cache, max_age } = params;
91-
const body: CorsProxyBody = {
92-
method: "POST",
93-
url: url,
94-
data: form_data,
95-
headers: headers,
96-
cache: cache ?? true,
97-
max_age: max_age ?? 0,
98-
};
99-
100-
return fetch(CORS_PROXY_SERVER, {
88+
const { form_data, headers, max_age } = params;
89+
90+
const proxy_url = `${CORS_PROXY_SERVER}/cache/max-age:${
91+
max_age ?? 0
92+
}/${url}`;
93+
94+
return fetch(proxy_url, {
10195
method: "POST",
10296
headers: {
103-
"Content-Type": "application/json",
97+
...headers,
98+
"Content-Type": "application/x-www-form-urlencoded",
10499
},
105-
body: JSON.stringify(body),
100+
body: json_to_encoded_form_data(form_data),
106101
});
107102
}
108103

@@ -120,7 +115,6 @@ export const get_events = async (
120115

121116
const response = await make_request(EXTENDED_SEARCH_BASE_URL, {
122117
form_data: json_form_data,
123-
cache: true,
124118
max_age: 60 * 60 * 24 * 3, // 3 days
125119
});
126120
const text = await response.text();
@@ -151,7 +145,6 @@ export const get_room_events = async (
151145

152146
const response = await make_request(EXTENDED_SEARCH_BASE_URL, {
153147
form_data: json_form_data,
154-
cache: true,
155148
max_age: 60 * 60 * 24 * 3, // 3 days
156149
});
157150
const text = await response.text();
@@ -174,7 +167,6 @@ export const find_rooms = async (
174167
let fetch_id = ++current_room_fetch_id;
175168
const query_url = `${ROOMS_QUICK_SEARCH_URL}${search_term}`;
176169
const response = await make_request(query_url, {
177-
cache: true,
178170
max_age: 60 * 60 * 24 * 7 * 4, // 4 weeks
179171
});
180172

src/lib/types.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,6 @@ export interface KITExtendedSearchFormData {
1818
room: string;
1919
}
2020

21-
/**
22-
* The body for the cors proxy server request.
23-
* @see https://github.com/MatthiasHarzer/minimal-cors-server
24-
*/
25-
export interface CorsProxyBody {
26-
method: "POST" | "GET" | "PUT" | "DELETE";
27-
url: string;
28-
headers?: object;
29-
body?: string;
30-
data?: object;
31-
cache?: boolean;
32-
max_age?: number;
33-
}
34-
3521
// noinspection SpellCheckingInspection
3622
/**
3723
* The KIT event type.
@@ -266,6 +252,5 @@ export interface RawRoom {
266252
export interface RequestParams {
267253
form_data?: KITExtendedSearchFormData;
268254
headers?: object;
269-
cache?: boolean;
270255
max_age?: number;
271256
}

src/lib/util/util.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,17 @@ export const time_to_total_seconds = (time: string): number => {
160160
const [hours, minutes] = time.split(":");
161161
return parseInt(hours) * 60 * 60 + parseInt(minutes) * 60;
162162
};
163+
164+
export const json_to_encoded_form_data = (
165+
json: Record<string, any>,
166+
): string => {
167+
const encodedPairs: string[] = [];
168+
for (const key in json) {
169+
if (json.hasOwnProperty(key)) {
170+
const encodedKey = encodeURIComponent(key);
171+
const encodedValue = encodeURIComponent(json[key]);
172+
encodedPairs.push(`${encodedKey}=${encodedValue}`);
173+
}
174+
}
175+
return encodedPairs.join("&");
176+
};

src/vite-env.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
/// <reference types="svelte" />
22
/// <reference types="vite/client" />
33
declare const APP_VERSION: string;
4+
5+
interface ImportMetaEnv {
6+
VITE_CORS_PROXY_SERVER_URL: string;
7+
}
8+
9+
interface ImportMeta {
10+
readonly env: ImportMetaEnv;
11+
}

0 commit comments

Comments
 (0)