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

Fixed 3 places where API URLs were not constructed by function route #451

Merged
merged 2 commits into from
Jul 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion frontend/components/global/PageQRCode.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
</template>

<script setup lang="ts">
import { route } from "../../lib/api/base";

function getQRCodeUrl(): string {
const currentURL = window.location.href;

return `/api/v1/qrcode?data=${encodeURIComponent(currentURL)}`;
return route(`/qrcode`, { data: encodeURIComponent(currentURL) });
}
</script>

Expand Down
5 changes: 3 additions & 2 deletions frontend/lib/api/base/base-api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Requests } from "../../requests";
import { route } from ".";

const ZERO_DATE = "0001-01-01T00:00:00Z";

Expand Down Expand Up @@ -70,12 +71,12 @@ export class BaseAPI {
this.attachmentToken = attachmentToken;
}

// if a attachmentToken is present it will be added to URL as a query param
// if an attachmentToken is present, it will be added to URL as a query param
// this is done with a simple appending of the query param to the URL. If your
// URL already has a query param, this will not work.
authURL(url: string): string {
if (this.attachmentToken) {
return `/api/v1${url}?access_token=${this.attachmentToken}`;
return route(url, { access_token: this.attachmentToken });
}
return url;
}
Expand Down
6 changes: 3 additions & 3 deletions frontend/lib/api/base/urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ export function overrideParts(host: string, prefix: string) {
export type QueryValue = string | string[] | number | number[] | boolean | null | undefined;

/**
* route is a the main URL builder for the API. It will use a predefined host and prefix (global)
* in the urls.ts file and then append the passed in path parameter uring the `URL` class from the
* route is the main URL builder for the API. It will use a predefined host and prefix (global)
* in the urls.ts file and then append the passed-in path parameter using the `URL` class from the
* browser. It will also append any query parameters passed in as the second parameter.
*
* The default host `http://localhost.com` is removed from the path if it is present. This allows us
* to bootstrap the API with different hosts as needed (like for testing) but still allows us to use
* relative URLs in pruduction because the API and client bundle are served from the same server/host.
* relative URLs in production because the API and client bundle are served from the same server/host.
*/
export function route(rest: string, params: Record<string, QueryValue> = {}): string {
const url = new URL(parts.prefix + rest, parts.host);
Expand Down
4 changes: 3 additions & 1 deletion frontend/pages/reports/label-generator.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<script setup lang="ts">
import { route } from "../../lib/api/base";

definePageMeta({
middleware: ["auth"],
layout: false,
Expand Down Expand Up @@ -176,7 +178,7 @@

const data = `${origin}/a/${assetID}`;

return `/api/v1/qrcode?data=${encodeURIComponent(data)}`;
return route(`/qrcode`, { data: encodeURIComponent(data) });
}

function getItem(n: number): LabelData {
Expand Down