Skip to content

Commit

Permalink
chore: uptake fresh 1.6.0 and std 0.208.0
Browse files Browse the repository at this point in the history
  • Loading branch information
deer committed Dec 6, 2023
1 parent 311e103 commit a5d1863
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 61 deletions.
4 changes: 2 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"compilerOptions": { "jsx": "react-jsx", "jsxImportSource": "preact" },
"imports": {
"@/": "./",
"$fresh/": "https://deno.land/x/fresh@1.5.4/",
"$fresh/": "https://deno.land/x/fresh@1.6.0/",
"$gfm": "https://deno.land/x/[email protected]/mod.ts",
"preact": "https://esm.sh/[email protected]",
"preact/": "https://esm.sh/[email protected]/",
Expand All @@ -31,7 +31,7 @@
"@preact/signals-core": "https://esm.sh/*@preact/[email protected]",
"twind-preset-tailwind/": "https://esm.sh/@twind/[email protected]/",
"twind-preset-ext": "https://esm.sh/@twind/[email protected]/",
"std/": "https://deno.land/std@0.205.0/",
"std/": "https://deno.land/std@0.208.0/",
"stripe": "npm:/[email protected]",
"feed": "npm:/[email protected]",
"kv_oauth/": "https://deno.land/x/[email protected]/",
Expand Down
74 changes: 37 additions & 37 deletions e2e_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2023 the Deno authors. All rights reserved. MIT license.

import { createHandler, Status } from "$fresh/server.ts";
import { createHandler } from "$fresh/server.ts";
import manifest from "@/fresh.gen.ts";
import {
collectValues,
Expand All @@ -24,7 +24,7 @@ import {
assertObjectMatch,
assertStringIncludes,
} from "std/assert/mod.ts";
import { isRedirectStatus } from "std/http/status.ts";
import { isRedirectStatus, STATUS_CODE } from "std/http/status.ts";
import { resolvesNext, returnsNext, stub } from "std/testing/mock.ts";
import Stripe from "stripe";
import options from "./fresh.config.ts";
Expand Down Expand Up @@ -94,7 +94,7 @@ Deno.test("[e2e] security headers", async () => {
Deno.test("[e2e] GET /", async () => {
const resp = await handler(new Request("http://localhost"));

assertEquals(resp.status, Status.OK);
assertEquals(resp.status, STATUS_CODE.OK);
assertHtml(resp);
});

Expand Down Expand Up @@ -184,15 +184,15 @@ Deno.test("[e2e] GET /blog", async () => {
new Request("http://localhost/blog"),
);

assertEquals(resp.status, Status.OK);
assertEquals(resp.status, STATUS_CODE.OK);
assertHtml(resp);
});

Deno.test("[e2e] GET /pricing", async () => {
const req = new Request("http://localhost/pricing");
const resp = await handler(req);

assertEquals(resp.status, Status.NotFound);
assertEquals(resp.status, STATUS_CODE.NotFound);
assertHtml(resp);
});

Expand Down Expand Up @@ -255,7 +255,7 @@ Deno.test("[e2e] GET /dashboard/stats", async (test) => {
}),
);

assertEquals(resp.status, Status.OK);
assertEquals(resp.status, STATUS_CODE.OK);
assertHtml(resp);
assertStringIncludes(await resp.text(), "<!--frsh-chart_default");
});
Expand All @@ -279,7 +279,7 @@ Deno.test("[e2e] GET /dashboard/users", async (test) => {
}),
);

assertEquals(resp.status, Status.OK);
assertEquals(resp.status, STATUS_CODE.OK);
assertHtml(resp);
assertStringIncludes(await resp.text(), "<!--frsh-userstable_default");
});
Expand All @@ -290,7 +290,7 @@ Deno.test("[e2e] GET /submit", async () => {
new Request("http://localhost/submit"),
);

assertEquals(resp.status, Status.OK);
assertEquals(resp.status, STATUS_CODE.OK);
assertHtml(resp);
});

Expand All @@ -299,7 +299,7 @@ Deno.test("[e2e] GET /feed", async () => {
new Request("http://localhost/feed"),
);

assertEquals(resp.status, Status.OK);
assertEquals(resp.status, STATUS_CODE.OK);
assertXml(resp);
});

Expand All @@ -312,7 +312,7 @@ Deno.test("[e2e] GET /api/items", async () => {
const resp = await handler(req);
const { values } = await resp.json();

assertEquals(resp.status, Status.OK);
assertEquals(resp.status, STATUS_CODE.OK);
assertJson(resp);
assertArrayIncludes(values, [item1, item2]);
});
Expand Down Expand Up @@ -391,15 +391,15 @@ Deno.test("[e2e] GET /api/items/[id]", async (test) => {
await test.step("serves not found response if item not found", async () => {
const resp = await handler(req);

assertEquals(resp.status, Status.NotFound);
assertEquals(resp.status, STATUS_CODE.NotFound);
assertEquals(await resp.text(), "Item not found");
});

await test.step("serves item as JSON", async () => {
await createItem(item);
const resp = await handler(req);

assertEquals(resp.status, Status.OK);
assertEquals(resp.status, STATUS_CODE.OK);
assertJson(resp);
assertEquals(await resp.json(), item);
});
Expand All @@ -416,7 +416,7 @@ Deno.test("[e2e] GET /api/users", async () => {

const { values } = await resp.json();

assertEquals(resp.status, Status.OK);
assertEquals(resp.status, STATUS_CODE.OK);
assertJson(resp);
assertArrayIncludes(values, [user1, user2]);
});
Expand All @@ -428,7 +428,7 @@ Deno.test("[e2e] GET /api/users/[login]", async (test) => {
await test.step("serves not found response if user not found", async () => {
const resp = await handler(req);

assertEquals(resp.status, Status.NotFound);
assertEquals(resp.status, STATUS_CODE.NotFound);
assertText(resp);
assertEquals(await resp.text(), "User not found");
});
Expand All @@ -437,7 +437,7 @@ Deno.test("[e2e] GET /api/users/[login]", async (test) => {
await createUser(user);
const resp = await handler(req);

assertEquals(resp.status, Status.OK);
assertEquals(resp.status, STATUS_CODE.OK);
assertJson(resp);
assertEquals(await resp.json(), user);
});
Expand All @@ -454,7 +454,7 @@ Deno.test("[e2e] GET /api/users/[login]/items", async (test) => {
await test.step("serves not found response if user not found", async () => {
const resp = await handler(req);

assertEquals(resp.status, Status.NotFound);
assertEquals(resp.status, STATUS_CODE.NotFound);
assertText(resp);
assertEquals(await resp.text(), "User not found");
});
Expand All @@ -465,7 +465,7 @@ Deno.test("[e2e] GET /api/users/[login]/items", async (test) => {
const resp = await handler(req);
const { values } = await resp.json();

assertEquals(resp.status, Status.OK);
assertEquals(resp.status, STATUS_CODE.OK);
assertJson(resp);
assertArrayIncludes(values, [item]);
});
Expand All @@ -481,7 +481,7 @@ Deno.test("[e2e] POST /api/vote", async (test) => {
await test.step("serves unauthorized response if the session user is not signed in", async () => {
const resp = await handler(new Request(url, { method: "POST" }));

assertEquals(resp.status, Status.Unauthorized);
assertEquals(resp.status, STATUS_CODE.Unauthorized);
assertText(resp);
assertEquals(await resp.text(), "User must be signed in");
});
Expand All @@ -494,7 +494,7 @@ Deno.test("[e2e] POST /api/vote", async (test) => {
}),
);

assertEquals(resp.status, Status.NotFound);
assertEquals(resp.status, STATUS_CODE.NotFound);
assertText(resp);
assertEquals(await resp.text(), "Item not found");
});
Expand All @@ -509,7 +509,7 @@ Deno.test("[e2e] POST /api/vote", async (test) => {
}),
);

assertEquals(resp.status, Status.Created);
assertEquals(resp.status, STATUS_CODE.Created);
});

await test.step("serves an error response if the `item_id` URL parameter is missing", async () => {
Expand All @@ -520,7 +520,7 @@ Deno.test("[e2e] POST /api/vote", async (test) => {
}),
);

assertEquals(resp.status, Status.BadRequest);
assertEquals(resp.status, STATUS_CODE.BadRequest);
assertEquals(await resp.text(), "`item_id` URL parameter missing");
});
});
Expand Down Expand Up @@ -553,7 +553,7 @@ Deno.test("[e2e] POST /api/stripe-webhooks", async (test) => {
Deno.env.delete("STRIPE_SECRET_KEY");
const resp = await handler(new Request(url, { method: "POST" }));

assertEquals(resp.status, Status.NotFound);
assertEquals(resp.status, STATUS_CODE.NotFound);
assertText(resp);
assertEquals(await resp.text(), "Not Found");
});
Expand All @@ -562,7 +562,7 @@ Deno.test("[e2e] POST /api/stripe-webhooks", async (test) => {
Deno.env.set("STRIPE_SECRET_KEY", crypto.randomUUID());
const resp = await handler(new Request(url, { method: "POST" }));

assertEquals(resp.status, Status.BadRequest);
assertEquals(resp.status, STATUS_CODE.BadRequest);
assertText(resp);
assertEquals(await resp.text(), "`Stripe-Signature` header is missing");
});
Expand All @@ -576,7 +576,7 @@ Deno.test("[e2e] POST /api/stripe-webhooks", async (test) => {
}),
);

assertEquals(resp.status, Status.InternalServerError);
assertEquals(resp.status, STATUS_CODE.InternalServerError);
assertText(resp);
assertEquals(
await resp.text(),
Expand All @@ -593,7 +593,7 @@ Deno.test("[e2e] POST /api/stripe-webhooks", async (test) => {
}),
);

assertEquals(resp.status, Status.BadRequest);
assertEquals(resp.status, STATUS_CODE.BadRequest);
assertText(resp);
assertEquals(
await resp.text(),
Expand All @@ -619,7 +619,7 @@ Deno.test("[e2e] POST /api/stripe-webhooks", async (test) => {

constructEventAsyncStub.restore();

assertEquals(resp.status, Status.NotFound);
assertEquals(resp.status, STATUS_CODE.NotFound);
assertText(resp);
assertEquals(await resp.text(), "User not found");
});
Expand Down Expand Up @@ -648,7 +648,7 @@ Deno.test("[e2e] POST /api/stripe-webhooks", async (test) => {

constructEventAsyncStub.restore();

assertEquals(resp.status, Status.Created);
assertEquals(resp.status, STATUS_CODE.Created);
assertEquals(await getUser(user.login), { ...user, isSubscribed: true });
});

Expand All @@ -670,7 +670,7 @@ Deno.test("[e2e] POST /api/stripe-webhooks", async (test) => {

constructEventAsyncStub.restore();

assertEquals(resp.status, Status.NotFound);
assertEquals(resp.status, STATUS_CODE.NotFound);
assertText(resp);
assertEquals(await resp.text(), "User not found");
});
Expand Down Expand Up @@ -700,7 +700,7 @@ Deno.test("[e2e] POST /api/stripe-webhooks", async (test) => {
constructEventAsyncStub.restore();

assertEquals(await getUser(user.login), { ...user, isSubscribed: false });
assertEquals(resp.status, Status.Accepted);
assertEquals(resp.status, STATUS_CODE.Accepted);
});

await test.step("serves bad request response if the event type is not supported", async () => {
Expand All @@ -721,7 +721,7 @@ Deno.test("[e2e] POST /api/stripe-webhooks", async (test) => {

constructEventAsyncStub.restore();

assertEquals(resp.status, Status.BadRequest);
assertEquals(resp.status, STATUS_CODE.BadRequest);
assertText(resp);
assertEquals(await resp.text(), "Event type not supported");
});
Expand All @@ -746,7 +746,7 @@ Deno.test("[e2e] GET /account", async (test) => {
}),
);

assertEquals(resp.status, Status.OK);
assertEquals(resp.status, STATUS_CODE.OK);
assertHtml(resp);
assertStringIncludes(await resp.text(), 'href="/account/upgrade"');
});
Expand All @@ -761,7 +761,7 @@ Deno.test("[e2e] GET /account", async (test) => {
}),
);

assertEquals(resp.status, Status.OK);
assertEquals(resp.status, STATUS_CODE.OK);
assertHtml(resp);
assertStringIncludes(await resp.text(), 'href="/account/manage"');
});
Expand All @@ -786,7 +786,7 @@ Deno.test("[e2e] GET /account/manage", async (test) => {
}),
);

assertEquals(resp.status, Status.NotFound);
assertEquals(resp.status, STATUS_CODE.NotFound);
assertHtml(resp);
});

Expand Down Expand Up @@ -836,7 +836,7 @@ Deno.test("[e2e] GET /account/upgrade", async (test) => {
}),
);

assertEquals(resp.status, Status.InternalServerError);
assertEquals(resp.status, STATUS_CODE.InternalServerError);
assertHtml(resp);
});

Expand All @@ -849,7 +849,7 @@ Deno.test("[e2e] GET /account/upgrade", async (test) => {
}),
);

assertEquals(resp.status, Status.NotFound);
assertEquals(resp.status, STATUS_CODE.NotFound);
assertHtml(resp);
});

Expand All @@ -874,7 +874,7 @@ Deno.test("[e2e] GET /account/upgrade", async (test) => {

sessionsCreateStub.restore();

assertEquals(resp.status, Status.NotFound);
assertEquals(resp.status, STATUS_CODE.NotFound);
assertHtml(resp);
});

Expand Down Expand Up @@ -926,7 +926,7 @@ Deno.test("[e2e] GET /api/me/votes", async () => {
);
const body = await resp.json();

assertEquals(resp.status, Status.OK);
assertEquals(resp.status, STATUS_CODE.OK);
assertJson(resp);
assertArrayIncludes(body, [{ ...item1, score: 1 }, { ...item2, score: 1 }]);
});
Expand Down
11 changes: 5 additions & 6 deletions plugins/error_handling.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// Copyright 2023 the Deno authors. All rights reserved. MIT license.
import type { Plugin } from "$fresh/server.ts";
import type { State } from "@/plugins/session.ts";
import { Status } from "$fresh/server.ts";
import { BadRequestError, redirect, UnauthorizedError } from "@/utils/http.ts";
import { STATUS_TEXT } from "std/http/status.ts";
import { STATUS_CODE, STATUS_TEXT } from "std/http/status.ts";

/**
* Returns the HTTP status code corresponding to a given runtime error. By
Expand All @@ -17,10 +16,10 @@ import { STATUS_TEXT } from "std/http/status.ts";
* ```
*/
export function toErrorStatus(error: Error) {
if (error instanceof Deno.errors.NotFound) return Status.NotFound;
if (error instanceof UnauthorizedError) return Status.Unauthorized;
if (error instanceof BadRequestError) return Status.BadRequest;
return Status.InternalServerError;
if (error instanceof Deno.errors.NotFound) return STATUS_CODE.NotFound;
if (error instanceof UnauthorizedError) return STATUS_CODE.Unauthorized;
if (error instanceof BadRequestError) return STATUS_CODE.BadRequest;
return STATUS_CODE.InternalServerError;
}

export default {
Expand Down
Loading

0 comments on commit a5d1863

Please sign in to comment.