Skip to content
This repository has been archived by the owner on Sep 7, 2024. It is now read-only.

Commit

Permalink
refactor: fixed route
Browse files Browse the repository at this point in the history
  • Loading branch information
kamiya10 committed May 1, 2024
1 parent d65861c commit 679244a
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 23 deletions.
31 changes: 29 additions & 2 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1"
tauri-plugin-fs = "2.0.0-beta.6"
tauri-plugin-os = "2.0.0-beta.4"
whoami = "1.5.1"
13 changes: 8 additions & 5 deletions src/class/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { Route } from "@/class/route";

export class ExpTechApi {
token: string;
route: Route;

constructor(token?: string) {
this.token = token || "";
this.route = new Route();
}

setToken(token: string) {
Expand All @@ -15,6 +17,7 @@ export class ExpTechApi {

async #get(url: string, options?: RequestInit) {
const res = await fetch(url, {
keepalive: true,
connectTimeout: 2000,
...options
});
Expand All @@ -27,11 +30,11 @@ export class ExpTechApi {
}

async getStations(requestOptions?: RequestInit): Promise<Record<string, Station>> {
return await this.#get(Route.station, requestOptions);
return await this.#get(this.route.station, requestOptions);
}

async getReportList(limit?: number, requestOptions?: RequestInit): Promise<PartialReport[]> {
const data = await this.#get(Route.reportList(limit), requestOptions) as PartialReport[];
const data = await this.#get(this.route.reportList(limit), requestOptions) as PartialReport[];

for (const report of data) {
report.no = +report.id.split("-")[0];
Expand All @@ -41,7 +44,7 @@ export class ExpTechApi {
}

async getReport(id: string, requestOptions?: RequestInit): Promise<Report> {
const data = await this.#get(Route.report(id), requestOptions);
const data = await this.#get(this.route.report(id), requestOptions);

data.no = +data.id.split("-")[0];
data.int = Object.keys(data.list).reduce(
Expand All @@ -66,10 +69,10 @@ export class ExpTechApi {
}

async getRts(time?: number, requestOptions?: RequestInit): Promise<Rts> {
return await this.#get(Route.rts(time), requestOptions);
return await this.#get(this.route.rts(time), requestOptions);
}

async getEew(time?: number, requestOptions?: RequestInit): Promise<Eew> {
return await this.#get(Route.eew(time), requestOptions);
return await this.#get(this.route.eew(time), requestOptions);
}
}
48 changes: 33 additions & 15 deletions src/class/route.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,55 @@
interface RouteConfig {
api: number;
lb: number;
}

export class Route {
static get lbUrl() {
return `https://lb-${Math.ceil(Math.random() * 4)}.exptech.com.tw` as const;
api: number;
lb: number;

constructor(route: Partial<RouteConfig> = {}) {
this.api = route.api ?? Math.ceil(Math.random() * 2);
this.lb = route.lb ?? Math.ceil(Math.random() * 4);
};

random() {
this.api = Math.ceil(Math.random() * 2);
this.lb = Math.ceil(Math.random() * 4);
}

get lbUrl() {
return `https://lb-${this.lb}.exptech.com.tw` as const;
}

static get apiUrl() {
return `https://api-${Math.ceil(Math.random() * 2)}.exptech.com.tw` as const;
get apiUrl() {
return `https://api-${this.api}.exptech.com.tw` as const;
}

static get station() {
get station() {
return "https://raw.githubusercontent.com/ExpTechTW/API/master/resource/station.json" as const;
}

static reportList(limit = 50) {
return `${Route.apiUrl}/api/v2/eq/report?limit=${limit}` as const;
reportList(limit = 50) {
return `${this.apiUrl}/api/v2/eq/report?limit=${limit}` as const;
}

static report(id: string) {
return `${Route.apiUrl}/api/v2/eq/report/${id}` as const;
report(id: string) {
return `${this.apiUrl}/api/v2/eq/report/${id}` as const;
}

static rts(time?: number) {
rts(time?: number) {
if (time) {
return `${Route.lbUrl}/api/v1/trem/rts/${time}` as const;
return `${this.lbUrl}/api/v1/trem/rts/${time}` as const;
}

return `${Route.lbUrl}/api/v1/trem/rts` as const;
return `${this.lbUrl}/api/v1/trem/rts` as const;
}

static eew(time?: number) {
eew(time?: number) {
if (time) {
return `${Route.lbUrl}/api/v1/eq/eew/${time}` as const;
return `${this.lbUrl}/api/v1/eq/eew/${time}` as const;
}

return `${Route.lbUrl}/api/v1/eq/eew` as const;
return `${this.lbUrl}/api/v1/eq/eew` as const;
}
}
9 changes: 8 additions & 1 deletion src/view/EarthquakeView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@
import RtsMarker from "@/components/map/RtsMarker.vue";
import RtsColorLegend from "@/components/map/RtsColorLegend.vue";
import { onMounted } from "vue";
import { computed, onMounted } from "vue";
import { useRtsStore } from "@/stores/rts_store";
import { useStationStore } from "@/stores/station_store";
import Global from "@/global";
import TimeDisplay from "@/components/misc/TimeDisplay.vue";
const rtsStore = useRtsStore();
const stationStore = useStationStore();
const currentTime = computed(() => {
if (rtsStore.time) return rtsStore.time;
return Date.now();
});
onMounted(() => {
Global.api.getStations().then((v) => {
stationStore.$patch({ value: v });
Expand All @@ -29,6 +35,7 @@ onMounted(() => {
/>
</template>
<RtsColorLegend id="rts-color-legend" />
<TimeDisplay :time="currentTime" />
</div>
</template>

Expand Down

0 comments on commit 679244a

Please sign in to comment.