Skip to content

Commit

Permalink
Some package refactoring
Browse files Browse the repository at this point in the history
Improve docs and tests
  • Loading branch information
jollytoad committed Jun 5, 2024
1 parent ba73f99 commit ef5d7c1
Show file tree
Hide file tree
Showing 69 changed files with 619 additions and 306 deletions.
3 changes: 1 addition & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"editor.tabSize": 2,
"deno.codeLens.testArgs": [
"--allow-all",
"--no-check",
"--location=http://localhost:8000"
"--no-check"
]
}
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@ and this project adheres to

This changelog will need to be split between individual packages

## [0.13.0]

### Added

- [@http/route] `lazy()` moved into here from `@http/handler`

### Removed

- [@http/handler] package removed, `mapData()` was no longer used, and
`Awaitable` type copied into `types` module of individual packages where
required

### Fixed

- [@http/example] use a random port if 8000 is not available, and fix tests to
use this port

## [0.12.0]

### Changed
Expand Down
42 changes: 20 additions & 22 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"tasks": {
"example": "deno run -A --import-map=./import_map_local.json --watch",
"test": "deno test -A --import-map=./import_map_local.json --location=http://localhost:8000 --coverage --no-check",
"test": "deno test -A --import-map=./import_map_local.json --coverage --no-check",
"cov:gen": "deno coverage coverage --lcov --output=cov.lcov",
"cov:view": "deno coverage --html coverage",
"check": "deno check --import-map=./import_map_local.json **/*.ts",
Expand All @@ -22,26 +22,25 @@
]
},
"imports": {
"@http/assert": "jsr:@http/assert@0.12.0",
"@http/discovery": "jsr:@http/discovery@0.12.0",
"@http/examples": "jsr:@http/examples@0.12.0",
"@http/generate": "jsr:@http/generate@0.12.0",
"@http/handler": "jsr:@http/handler@0.12.0",
"@http/host-deno-deploy": "jsr:@http/host-deno-deploy@0.12.0",
"@http/host-deno-local": "jsr:@http/host-deno-local@0.12.0",
"@http/interceptor": "jsr:@http/interceptor@0.12.0",
"@http/request": "jsr:@http/request@0.12.0",
"@http/response": "jsr:@http/response@0.12.0",
"@http/route": "jsr:@http/route@0.12.0",
"@http/route-deno": "jsr:@http/route-deno@0.12.0",
"@std/assert": "jsr:@std/assert@^0.225.1",
"@std/collections": "jsr:@std/collections@^0.224.0",
"@std/fs": "jsr:@std/fs@^0.224.0",
"@std/http": "jsr:@std/http@^0.224.0",
"@std/media-types": "jsr:@std/media-types@^0.224.0",
"@std/net": "jsr:@std/net@^0.224.0",
"@std/path": "jsr:@std/path@^0.224.0",
"@std/testing": "jsr:@std/testing@^0.224.0",
"@http/assert": "jsr:@http/assert@0.13.0",
"@http/discovery": "jsr:@http/discovery@0.13.0",
"@http/examples": "jsr:@http/examples@0.13.0",
"@http/generate": "jsr:@http/generate@0.13.0",
"@http/host-deno-deploy": "jsr:@http/host-deno-deploy@0.13.0",
"@http/host-deno-local": "jsr:@http/host-deno-local@0.13.0",
"@http/interceptor": "jsr:@http/interceptor@0.13.0",
"@http/request": "jsr:@http/request@0.13.0",
"@http/response": "jsr:@http/response@0.13.0",
"@http/route": "jsr:@http/route@0.13.0",
"@http/route-deno": "jsr:@http/route-deno@0.13.0",
"@std/assert": "jsr:@std/assert@^0.226.0",
"@std/collections": "jsr:@std/collections@^1.0.0-rc.1",
"@std/fs": "jsr:@std/fs@^0.229.1",
"@std/http": "jsr:@std/http@^0.224.3",
"@std/media-types": "jsr:@std/media-types@^1.0.0-rc.1",
"@std/net": "jsr:@std/net@^0.224.2",
"@std/path": "jsr:@std/path@^0.225.2",
"@std/testing": "jsr:@std/testing@^0.225.0",
"@std/url": "jsr:@std/url@^0.224.0",
"ts-poet": "npm:ts-poet@^6.9.0"
},
Expand All @@ -50,7 +49,6 @@
"./packages/discovery",
"./packages/examples",
"./packages/generate",
"./packages/handler",
"./packages/host-deno-deploy",
"./packages/host-deno-local",
"./packages/interceptor",
Expand Down
39 changes: 20 additions & 19 deletions import_map_local.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@
"@http/examples/verify-header": "./packages/examples/verify_header.ts",
"@http/examples/when-pattern": "./packages/examples/when_pattern.ts",
"@http/generate/generate-routes-module": "./packages/generate/generate_routes_module.ts",
"@http/handler/lazy": "./packages/handler/lazy.ts",
"@http/handler/map-data": "./packages/handler/map_data.ts",
"@http/handler/types": "./packages/handler/types.ts",
"@http/host-deno-deploy/init": "./packages/host-deno-deploy/init.ts",
"@http/host-deno-deploy/types": "./packages/host-deno-deploy/types.ts",
"@http/host-deno-local/init": "./packages/host-deno-local/init.ts",
"@http/host-deno-local/load-key-and-cert": "./packages/host-deno-local/load_key_and_cert.ts",
"@http/host-deno-local/port": "./packages/host-deno-local/port.ts",
"@http/host-deno-local/server-url": "./packages/host-deno-local/server_url.ts",
"@http/host-deno-local/types": "./packages/host-deno-local/types.ts",
"@http/interceptor/by-status": "./packages/interceptor/by_status.ts",
"@http/interceptor/catch-response": "./packages/interceptor/catch_response.ts",
"@http/interceptor/cors": "./packages/interceptor/cors.ts",
Expand Down Expand Up @@ -75,31 +74,33 @@
"@http/response/temporary-redirect": "./packages/response/temporary_redirect.ts",
"@http/response/unauthorized": "./packages/response/unauthorized.ts",
"@http/route-deno/static-route": "./packages/route-deno/static_route.ts",
"@http/route-deno/types": "./packages/route-deno/types.ts",
"@http/route/as-url-pattern": "./packages/route/as_url_pattern.ts",
"@http/route/by-media-type": "./packages/route/by_media_type.ts",
"@http/route/by-method": "./packages/route/by_method.ts",
"@http/route/by-pattern": "./packages/route/by_pattern.ts",
"@http/route/by-sub-pattern": "./packages/route/by_sub_pattern.ts",
"@http/route/cascade": "./packages/route/cascade.ts",
"@http/route/handle": "./packages/route/handle.ts",
"@http/route/lazy": "./packages/route/lazy.ts",
"@http/route/types": "./packages/route/types.ts",
"@http/route/with-fallback": "./packages/route/with_fallback.ts",
"@std/assert": "jsr:@std/assert@^0.225.1",
"@std/assert/": "jsr:/@std/assert@^0.225.1/",
"@std/collections": "jsr:@std/collections@^0.224.0",
"@std/collections/": "jsr:/@std/collections@^0.224.0/",
"@std/fs": "jsr:@std/fs@^0.224.0",
"@std/fs/": "jsr:/@std/fs@^0.224.0/",
"@std/http": "jsr:@std/http@^0.224.0",
"@std/http/": "jsr:/@std/http@^0.224.0/",
"@std/media-types": "jsr:@std/media-types@^0.224.0",
"@std/media-types/": "jsr:/@std/media-types@^0.224.0/",
"@std/net": "jsr:@std/net@^0.224.0",
"@std/net/": "jsr:/@std/net@^0.224.0/",
"@std/path": "jsr:@std/path@^0.224.0",
"@std/path/": "jsr:/@std/path@^0.224.0/",
"@std/testing": "jsr:@std/testing@^0.224.0",
"@std/testing/": "jsr:/@std/testing@^0.224.0/",
"@std/assert": "jsr:@std/assert@^0.226.0",
"@std/assert/": "jsr:/@std/assert@^0.226.0/",
"@std/collections": "jsr:@std/collections@^1.0.0-rc.1",
"@std/collections/": "jsr:/@std/collections@^1.0.0-rc.1/",
"@std/fs": "jsr:@std/fs@^0.229.1",
"@std/fs/": "jsr:/@std/fs@^0.229.1/",
"@std/http": "jsr:@std/http@^0.224.3",
"@std/http/": "jsr:/@std/http@^0.224.3/",
"@std/media-types": "jsr:@std/media-types@^1.0.0-rc.1",
"@std/media-types/": "jsr:/@std/media-types@^1.0.0-rc.1/",
"@std/net": "jsr:@std/net@^0.224.2",
"@std/net/": "jsr:/@std/net@^0.224.2/",
"@std/path": "jsr:@std/path@^0.225.2",
"@std/path/": "jsr:/@std/path@^0.225.2/",
"@std/testing": "jsr:@std/testing@^0.225.0",
"@std/testing/": "jsr:/@std/testing@^0.225.0/",
"@std/url": "jsr:@std/url@^0.224.0",
"@std/url/": "jsr:/@std/url@^0.224.0/",
"ts-poet": "npm:ts-poet@^6.9.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/assert/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@http/assert",
"version": "0.12.0",
"version": "0.13.0",
"exports": {
"./assert-header": "./assert_header.ts",
"./assert-ok": "./assert_ok.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/discovery/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@http/discovery",
"version": "0.12.0",
"version": "0.13.0",
"exports": {
"./as-serializable-pattern": "./as_serializable_pattern.ts",
"./discover-routes": "./discover_routes.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/discovery/dynamic_route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { byPattern } from "@http/route/by-pattern";
import { lazy } from "@http/handler/lazy";
import { lazy } from "@http/route/lazy";
import { cascade } from "@http/route/cascade";
import {
type DiscoveredRoute,
Expand Down
8 changes: 8 additions & 0 deletions packages/examples/_base_url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { getServerUrl } from "@http/host-deno-local/server-url";

export function getBaseUrl(server: Deno.HttpServer<Deno.Addr>) {
if (server.addr.transport !== "tcp") {
throw new Error("Server is not listening on a tcp connection!");
}
return getServerUrl(server.addr.hostname, server.addr.port);
}
2 changes: 1 addition & 1 deletion packages/examples/_routes.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// IMPORTANT: This file has been automatically generated, DO NOT edit by hand.

import { lazy } from "@http/handler/lazy";
import { byMethod } from "@http/route/by-method";
import { byPattern } from "@http/route/by-pattern";
import { cascade } from "@http/route/cascade";
import { lazy } from "@http/route/lazy";

export default cascade(
byPattern("/user/:name", lazy(() => import("./_routes/user/[name].ts"))),
Expand Down
22 changes: 12 additions & 10 deletions packages/examples/by_media_type.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,53 @@ import {
assertStatus,
STATUS_CODE,
} from "@http/assert";
import { getBaseUrl } from "./_base_url.ts";

Deno.test("by_media_type", async (t) => {
await using _server = (await import("./by_media_type.ts")).default;
await using server = (await import("./by_media_type.ts")).default;
const baseUrl = getBaseUrl(server);

await t.step("/hello", async () => {
const response = await fetch("/hello");
const response = await fetch(`${baseUrl}/hello`);

assertOk(response);
assertHeader(response, "Content-Type", "text/plain;charset=UTF-8");
assertEquals(await response.text(), "Hello world");
});

await t.step("/hello.txt", async () => {
const response = await fetch("/hello.txt");
const response = await fetch(`${baseUrl}/hello.txt`);

assertOk(response);
assertHeader(response, "Content-Type", "text/plain;charset=UTF-8");
assertEquals(await response.text(), "Hello world");
});

await t.step("/hello.html", async () => {
const response = await fetch("/hello.html");
const response = await fetch(`${baseUrl}/hello.html`);

assertOk(response);
assertHeader(response, "Content-Type", "text/html");
assertStringIncludes(await response.text(), "<h1>Hello world</h1>");
});

await t.step("/hello.json", async () => {
const response = await fetch("/hello.json");
const response = await fetch(`${baseUrl}/hello.json`);

assertOk(response);
assertHeader(response, "Content-Type", "application/json");
assertStringIncludes(await response.json(), "Hello world");
});

await t.step("/hello.md (404)", async () => {
const response = await fetch("/hello.md");
const response = await fetch(`${baseUrl}/hello.md`);

assertStatus(response, STATUS_CODE.NotFound);
await response.body?.cancel();
});

await t.step("Accept: text/plain", async () => {
const response = await fetch("/hello", {
const response = await fetch(`${baseUrl}/hello`, {
headers: { Accept: "text/plain" },
});

Expand All @@ -59,7 +61,7 @@ Deno.test("by_media_type", async (t) => {
});

await t.step("Accept: text/html", async () => {
const response = await fetch("/hello", {
const response = await fetch(`${baseUrl}/hello`, {
headers: { Accept: "text/html" },
});

Expand All @@ -69,7 +71,7 @@ Deno.test("by_media_type", async (t) => {
});

await t.step("Accept: application/json", async () => {
const response = await fetch("/hello", {
const response = await fetch(`${baseUrl}/hello`, {
headers: { Accept: "application/json" },
});

Expand All @@ -79,7 +81,7 @@ Deno.test("by_media_type", async (t) => {
});

await t.step("Accept: text/markdown (406)", async () => {
const response = await fetch("/hello", {
const response = await fetch(`${baseUrl}/hello`, {
headers: { Accept: "text/markdown" },
});

Expand Down
62 changes: 33 additions & 29 deletions packages/examples/by_media_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,42 @@ import { handle } from "@http/route/handle";
import { byPattern } from "@http/route/by-pattern";
import { byMethod } from "@http/route/by-method";
import { byMediaType } from "@http/route/by-media-type";
import { port } from "@http/host-deno-local/port";

const server = Deno.serve(handle([
byPattern(
"/hello{.:ext}?",
byMethod({
GET: byMediaType({
"text/plain": () => {
return new Response("Hello world");
},
"text/html": () => {
return new Response(
"<html><body><h1>Hello world</h1></body></html>",
{
headers: {
"Content-Type": "text/html",
const server = Deno.serve(
{ port: port() },
handle([
byPattern(
"/hello{.:ext}?",
byMethod({
GET: byMediaType({
"text/plain": () => {
return new Response("Hello world");
},
"text/html": () => {
return new Response(
"<html><body><h1>Hello world</h1></body></html>",
{
headers: {
"Content-Type": "text/html",
},
},
},
);
},
"application/json": () => {
return new Response(
JSON.stringify("Hello world"),
{
headers: {
"Content-Type": "application/json",
);
},
"application/json": () => {
return new Response(
JSON.stringify("Hello world"),
{
headers: {
"Content-Type": "application/json",
},
},
},
);
},
);
},
}),
}),
}),
),
])) as Deno.HttpServer;
),
]),
) as Deno.HttpServer;

export default server;
Loading

0 comments on commit ef5d7c1

Please sign in to comment.