Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flatpage routes now raise a 404 when they should #754

Merged
merged 1 commit into from
Oct 3, 2024
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
41 changes: 30 additions & 11 deletions src/lib/api/flatpages.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
import type { Flatpage } from "./types";
import type { APIResponse, Flatpage } from "./types";

import { BASE_API_URL } from "@/config/config";
import { getApiResponse } from "$lib/utils/api";

/**
* Return the tip of the day flatpage
* @param fetch
*/
export async function getTipOfDay(
fetch = globalThis.fetch,
): Promise<Flatpage | undefined> {
const endpoint = new URL("flatpages/tipofday/", BASE_API_URL);
try {
const resp = await fetch(endpoint, { credentials: "include" }).catch(
console.error,
);
if (!resp) return;
if (!resp.ok) return;
return resp.json();
} catch (e) {
return;
const { data, error } = await get("/tipofday/", fetch);
if (!error) {
return data;
}
}

/**
* Get a single flat page
* @param path
* @param fetch
*/
export async function get(
path: string,
fetch = globalThis.fetch,
): Promise<APIResponse<Flatpage, unknown>> {
if (!path.startsWith("/")) {
path = "/" + path;
}

const endpoint = new URL("flatpages" + path, BASE_API_URL);
console.log(endpoint.href);
const resp = await fetch(endpoint, { credentials: "include" });

return getApiResponse<Flatpage>(resp);
}
35 changes: 35 additions & 0 deletions src/lib/api/tests/flatpages.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { afterEach, describe, expect, test, vi } from "vitest";

import { BASE_API_URL } from "@/config/config";
import { flatpage } from "@/test/fixtures/flatpages";
import * as flatpages from "../flatpages";

describe("flatpages", () => {
afterEach(() => {
vi.restoreAllMocks();
});

test("flatpages.get", async () => {
const mockFetch = vi.fn().mockImplementation(async (endpoint, options) => {
return {
ok: true,
status: 200,
async json() {
return flatpage;
},
};
});

const { error, data } = await flatpages.get(flatpage.url, mockFetch);

expect(error).toBeUndefined();
expect(data).toEqual(flatpage);

expect(mockFetch).toHaveBeenCalledWith(
new URL("/api/flatpages" + flatpage.url, BASE_API_URL),
{
credentials: "include",
},
);
});
});
4 changes: 2 additions & 2 deletions src/routes/(pages)/[...path]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
export let data;
$: title = data.data.title;
$: content = data.data.content;
$: title = data.title;
$: content = data.content;
</script>

<svelte:head>
Expand Down
20 changes: 8 additions & 12 deletions src/routes/(pages)/[...path]/+page.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
// load data for flatpages
import { redirect } from "@sveltejs/kit";
import { error } from "@sveltejs/kit";
import { BASE_API_URL } from "@/config/config.js";
import { isRedirectCode } from "@/lib/utils/api";
import { getApiResponse } from "$lib/utils/api";
import type { Flatpage } from "@/lib/api/types.js";

const ROOT = new URL("flatpages/", BASE_API_URL);
import * as flatpages from "$lib/api/flatpages";

/** @type {import('./$types').PageLoad} */
export async function load({ fetch, params }) {
const endpoint = new URL(params.path, ROOT);
const resp = await fetch(endpoint, { credentials: "include" });
// we should be following redirects, so this shouldn't happen
if (isRedirectCode(resp.status)) {
redirect(resp.status, resp.headers.get("Location"));
const { data, error: err } = await flatpages.get(params.path, fetch);

if (err) {
return error(err.status, { message: err.message });
}
return getApiResponse<Flatpage>(resp);

return data;
}
19 changes: 6 additions & 13 deletions src/routes/(pages)/home/+page.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,27 @@
// load homepage data
import { error } from "@sveltejs/kit";
import DOMPurify from "isomorphic-dompurify";
import { marked } from "marked";
import { gfmHeadingId } from "marked-gfm-heading-id";
import DOMPurify from "isomorphic-dompurify";

import { BASE_API_URL } from "@/config/config.js";
import { isErrorCode } from "@/lib/utils/api";
import * as flatpages from "$lib/api/flatpages";

marked.use(gfmHeadingId());

const ROOT = new URL("flatpages/", BASE_API_URL);
const endpoint = new URL("home/", ROOT);

/** @type {import('./$types').PageLoad} */
export async function load({ fetch }) {
const resp = await fetch(endpoint, { credentials: "include" });
const { data: page, error: err } = await flatpages.get("/home/", fetch);

if (isErrorCode(resp.status)) {
error(resp.status, resp.statusText);
if (err) {
return error(err.status, { message: err.message });
}

const page = await resp.json();

return {
title: page.title,
url: page.url,
content: render(page.content),
};
}

function render(content) {
function render(content: string): string {
return DOMPurify.sanitize(marked.parse(content));
}