Skip to content

Commit

Permalink
Merge branch 'uix-new' of https://github.com/unyt-org/uix into uix-new
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasstrehle committed Oct 21, 2024
2 parents a79d62f + 0cd1542 commit 65390ad
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 23 deletions.
14 changes: 13 additions & 1 deletion docs/manual/03 JSX.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,23 @@ const list = <ul>
</ul>;

// add an item to the list
items.val.push("Item 4");
items.push("Item 4");
```

You can use other array functions such as [`filter`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter), [`reduce`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce), [`forEach`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach) or [`find`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find) in the same way.

To map over other structures like Sets, which do not natively support the `map` method like Arrays, you can use the [`map` function from DATEX](https://docs.unyt.org/manual/datex/functional-programming#map) to achieve a similar and reactive outcome. Pass the structure as the first argument, and the callback function for the transformation as the second argument.

```tsx
const items = $(new Set());

const list = <ul>
{map(items, item => <li>{item}</li>)}
</ul>;

// add an item to the list
items.add("Item 4");
```

## Input validation

Expand Down
60 changes: 52 additions & 8 deletions src/html/http-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ export class HTTPStatus<Code extends number = number, T extends Entrypoint = Ent
return new HTTPStatus(this.code, content);
}

static #map:Record<number,HTTPStatus<number, string>> = {}
static #map:Record<number,HTTPStatus<number, Entrypoint>> = {}

static #setDefault<T extends HTTPStatus<number, string>>(status: T): T {
static #setDefault<T extends HTTPStatus<number, Entrypoint>>(status: T): T {
this.#map[status.code] = status;
return status;
}
Expand All @@ -20,13 +20,57 @@ export class HTTPStatus<Code extends number = number, T extends Entrypoint = Ent
return this.#map[code];
}

// TODO add more status codes
static BAD_REQUEST = this.#setDefault(new HTTPStatus(400, "Bad Request"));
static UNAUTHORIZED = this.#setDefault(new HTTPStatus(401, "Unauthorized"));
static FORBIDDEN = this.#setDefault(new HTTPStatus(403, "Forbidden"));
static NOT_FOUND = this.#setDefault(new HTTPStatus(404, "Not Found"));
static CONTINUE = this.#setDefault(new HTTPStatus(100));
static SWITCHING_PROTOCOLS = this.#setDefault(new HTTPStatus(101));
static PROCESSING = this.#setDefault(new HTTPStatus(102));
static EARLY_HINTS = this.#setDefault(new HTTPStatus(103));

static OK = this.#setDefault(new HTTPStatus(200));
static CREATED = this.#setDefault(new HTTPStatus(201));
static ACCEPTED = this.#setDefault(new HTTPStatus(202));
static NON_AUTHORITATIVE_INFORMATION = this.#setDefault(new HTTPStatus(203));
static NO_CONTENT = this.#setDefault(new HTTPStatus(204));
static RESET_CONTENT = this.#setDefault(new HTTPStatus(205));
static PARTIAL_CONTENT = this.#setDefault(new HTTPStatus(206));
static MULTI_STATUS = this.#setDefault(new HTTPStatus(207));
static ALREADY_REPORTED = this.#setDefault(new HTTPStatus(208));
static IM_USED = this.#setDefault(new HTTPStatus(226));

static INTERNAL_SERVER_ERROR = new HTTPStatus(500, "Internal Server Error");
static MULTIPLE_CHOICES = this.#setDefault(new HTTPStatus(300));
static MOVED_PERMANENTLY = this.#setDefault(new HTTPStatus(301));
static FOUND = this.#setDefault(new HTTPStatus(302));
static SEE_OTHER = this.#setDefault(new HTTPStatus(303));
static NOT_MODIFIED = this.#setDefault(new HTTPStatus(304));
static USE_PROXY = this.#setDefault(new HTTPStatus(305));
static SWITCH_PROXY = this.#setDefault(new HTTPStatus(306));
static TEMPORARY_REDIRECT = this.#setDefault(new HTTPStatus(307));
static PERMANENT_REDIRECT = this.#setDefault(new HTTPStatus(308));

static BAD_REQUEST = this.#setDefault(new HTTPStatus(400, new Response("Bad Request")));
static UNAUTHORIZED = this.#setDefault(new HTTPStatus(401, new Response("Unauthorized")));
static FORBIDDEN = this.#setDefault(new HTTPStatus(403, new Response("Forbidden")));
static NOT_FOUND = this.#setDefault(new HTTPStatus(404, new Response("Not Found")));
static METHOD_NOT_ALLOWED = this.#setDefault(new HTTPStatus(405, new Response("Method Not Allowed")));
static CONFLICT = this.#setDefault(new HTTPStatus(409, new Response("Conflict")));
static GONE = this.#setDefault(new HTTPStatus(410, new Response("Gone")));
static LENGTH_REQUIRED = this.#setDefault(new HTTPStatus(411, new Response("Length Required")));
static PAYLOAD_TOO_LARGE = this.#setDefault(new HTTPStatus(413, new Response("Payload Too Large")));
static URI_TOO_LONG = this.#setDefault(new HTTPStatus(414, new Response("URI Too Long")));
static UNSUPPORTED_MEDIA_TYPE = this.#setDefault(new HTTPStatus(415, new Response("Unsupported Media Type")));
static RANGE_NOT_SATISFIABLE = this.#setDefault(new HTTPStatus(416, new Response("Range Not Satisfiable")));
static EXPECTATION_FAILED = this.#setDefault(new HTTPStatus(417, new Response("Expectation Failed")));
static TEAPOT = this.#setDefault(new HTTPStatus(418, new Response("I'm a teapot")));
static UPGRADE_REQUIRED = this.#setDefault(new HTTPStatus(426, new Response("Upgrade Required")));
static PRECONDITION_FAILED = this.#setDefault(new HTTPStatus(412, new Response("Precondition Failed")));
static TOO_MANY_REQUESTS = this.#setDefault(new HTTPStatus(429, new Response("Too Many Requests")));

static INTERNAL_SERVER_ERROR = this.#setDefault(new HTTPStatus(500, new Response("Internal Server Error")));
static NOT_IMPLEMENTED = this.#setDefault(new HTTPStatus(501, new Response("Not Implemented")));
static BAD_GATEWAY = this.#setDefault(new HTTPStatus(502, new Response("Bad Gateway")));
static SERVICE_UNAVAILABLE = this.#setDefault(new HTTPStatus(503, new Response("Service Unavailable")));
static GATEWAY_TIMEOUT = this.#setDefault(new HTTPStatus(504, new Response("Gateway Timeout")));
static HTTP_VERSION_NOT_SUPPORTED = this.#setDefault(new HTTPStatus(505, new Response("HTTP Version Not Supported")));
static NETWORK_AUTHENTICATION_REQUIRED = this.#setDefault(new HTTPStatus(511, new Response("Network Authentication Required")));

toString() {
return `HTTP Status ${this.code}: ${String(this.content)}`
Expand Down
6 changes: 1 addition & 5 deletions src/providers/common.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,7 @@ export class FileHandle {
*/
export function provideFile(path: string | URL) {
const resolvedPath = new Path(path, getCallerFile());
return async () => {
if (await resolvedPath.exists())
return new FileHandle(resolvedPath);
return new HTTPError(HTTPStatus.NOT_FOUND);
}
return new FileHandle(resolvedPath);
}

/**
Expand Down
23 changes: 14 additions & 9 deletions src/routing/rendering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,15 +383,20 @@ export async function resolveEntrypointRoute<T extends Entrypoint>(entrypointDat

// handle FileHandle
else if (client_type === "deno" && entrypointData.entrypoint instanceof FileHandle) {
const file = await Deno.open(entrypointData.entrypoint.path.normal_pathname);
const { mime } = await import("https://deno.land/x/mimetypes@v1.0.0/mod.ts");
const mimeType = mime.getType(entrypointData.entrypoint.path.name);

resolved.content = new Response(file.readable)
// cors headers (TODO: more general way to define cors behavior)
resolved.content.headers.set("Access-Control-Allow-Origin", "*");
resolved.content.headers.set("Access-Control-Allow-Headers", "*");
if (mimeType) resolved.content.headers.set("Content-Type", mimeType);
try {
const file = await Deno.open(entrypointData.entrypoint.path.normal_pathname);
const { mime } = await import("https://deno.land/x/mimetypes@v1.0.0/mod.ts");
const mimeType = mime.getType(entrypointData.entrypoint.path.name);

resolved.content = new Response(file.readable)
// cors headers (TODO: more general way to define cors behavior)
resolved.content.headers.set("Access-Control-Allow-Origin", "*");
resolved.content.headers.set("Access-Control-Allow-Headers", "*");
if (mimeType) resolved.content.headers.set("Content-Type", mimeType);
}
catch {
throw HTTPStatus.NOT_FOUND
}
}


Expand Down

0 comments on commit 65390ad

Please sign in to comment.