htmv 0.0.46 → 0.0.48
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli/commands/new.js +1 -0
- package/dist/http/helpers.d.ts +6 -0
- package/dist/http/helpers.js +16 -0
- package/dist/http/index.d.ts +1 -0
- package/dist/http/index.js +1 -0
- package/dist/http/response.d.ts +10 -0
- package/dist/http/response.js +43 -0
- package/dist/routing.js +3 -2
- package/dist/views.js +2 -3
- package/package.json +1 -1
package/dist/cli/commands/new.js
CHANGED
|
@@ -71,6 +71,7 @@ export default async (_params: RouteParams) => {
|
|
|
71
71
|
tsconfigWithoutStartingBracket = `{
|
|
72
72
|
"exclude": ["public"],
|
|
73
73
|
${tsconfigWithoutStartingBracket}`;
|
|
74
|
+
await fs.writeFile(path.join(fullPath, "tsconfig.json"), tsconfigWithoutStartingBracket);
|
|
74
75
|
console.log(`All done! Project ${name} created.`);
|
|
75
76
|
console.log(`Now run cd ${name} and start building your next big project!`);
|
|
76
77
|
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type Headers, type HttpResponse } from "./response";
|
|
2
|
+
export declare function httpResponse(response: HttpResponse): HttpResponse;
|
|
3
|
+
export declare function badRequest(body?: string | object, headers?: Headers): HttpResponse;
|
|
4
|
+
export declare function created(body?: string | object, headers?: Headers): HttpResponse;
|
|
5
|
+
export declare function ok(body?: string | object, headers?: Headers): HttpResponse;
|
|
6
|
+
export declare function noContent(headers?: Headers): HttpResponse;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { requestHelper } from "./response";
|
|
2
|
+
export function httpResponse(response) {
|
|
3
|
+
return response;
|
|
4
|
+
}
|
|
5
|
+
export function badRequest(body, headers) {
|
|
6
|
+
return requestHelper(400, body, headers);
|
|
7
|
+
}
|
|
8
|
+
export function created(body, headers) {
|
|
9
|
+
return requestHelper(201, body, headers);
|
|
10
|
+
}
|
|
11
|
+
export function ok(body, headers) {
|
|
12
|
+
return requestHelper(200, body, headers);
|
|
13
|
+
}
|
|
14
|
+
export function noContent(headers) {
|
|
15
|
+
return requestHelper(204, undefined, headers);
|
|
16
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./helpers";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./helpers";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type Headers = Record<string, string>;
|
|
2
|
+
export type HttpResponse = {
|
|
3
|
+
status: number;
|
|
4
|
+
headers?: Headers;
|
|
5
|
+
body?: string;
|
|
6
|
+
};
|
|
7
|
+
type ResponseLike = string | object | HttpResponse;
|
|
8
|
+
export declare function resolveResponse(result: ResponseLike): Response;
|
|
9
|
+
export declare function requestHelper(status: number, body?: string | object, headers?: Headers): HttpResponse;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export function resolveResponse(result) {
|
|
2
|
+
if (typeof result === "string") {
|
|
3
|
+
return new Response(result, {
|
|
4
|
+
headers: { "Content-Type": "text/html; charset=utf-8" },
|
|
5
|
+
});
|
|
6
|
+
}
|
|
7
|
+
if (isHttpResponse(result)) {
|
|
8
|
+
return new Response(result.body, {
|
|
9
|
+
status: result.status,
|
|
10
|
+
headers: result.headers,
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
return new Response(JSON.stringify(result), {
|
|
14
|
+
headers: { "Content-Type": "application/json; charset=utf-8" },
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
function isHttpResponse(value) {
|
|
18
|
+
if (value === null)
|
|
19
|
+
return false;
|
|
20
|
+
if (!("status" in value) || typeof value.status !== "number")
|
|
21
|
+
return false;
|
|
22
|
+
if (!("headers" in value) || typeof value.headers !== "object")
|
|
23
|
+
return false;
|
|
24
|
+
if (!("body" in value) || typeof value.body !== "string")
|
|
25
|
+
return false;
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
export function requestHelper(status, body, headers) {
|
|
29
|
+
if (body === undefined)
|
|
30
|
+
return {
|
|
31
|
+
status,
|
|
32
|
+
headers,
|
|
33
|
+
};
|
|
34
|
+
return {
|
|
35
|
+
status,
|
|
36
|
+
body: typeof body === "string" ? body : JSON.stringify(body),
|
|
37
|
+
headers: headers !== undefined
|
|
38
|
+
? headers
|
|
39
|
+
: typeof body === "string"
|
|
40
|
+
? { "Content-Type": "text/plain; charset=utf-8" }
|
|
41
|
+
: { "Content-Type": "application/json; charset=utf-8" },
|
|
42
|
+
};
|
|
43
|
+
}
|
package/dist/routing.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import fs from "node:fs/promises";
|
|
2
2
|
import path from "node:path";
|
|
3
|
+
import { resolveResponse } from "./http/response";
|
|
3
4
|
export async function registerRoutes(app, baseDir, prefix = "/") {
|
|
4
5
|
const entries = await fs.readdir(baseDir, { withFileTypes: true });
|
|
5
6
|
for (const entry of entries) {
|
|
@@ -15,7 +16,7 @@ export async function registerRoutes(app, baseDir, prefix = "/") {
|
|
|
15
16
|
if (defaultFn && typeof defaultFn === "function") {
|
|
16
17
|
app.all(prefix, async ({ request, query, params }) => {
|
|
17
18
|
const result = await defaultFn({ request, query, params });
|
|
18
|
-
return result;
|
|
19
|
+
return resolveResponse(result);
|
|
19
20
|
});
|
|
20
21
|
console.log(`Registered ${fullPath} on ${prefix} route with method all`);
|
|
21
22
|
}
|
|
@@ -29,7 +30,7 @@ export async function registerRoutes(app, baseDir, prefix = "/") {
|
|
|
29
30
|
continue;
|
|
30
31
|
app[name](prefix, async ({ request, query, params }) => {
|
|
31
32
|
const result = await fn({ request, query, params });
|
|
32
|
-
return result;
|
|
33
|
+
return resolveResponse(result);
|
|
33
34
|
});
|
|
34
35
|
console.log(`Registered ${fullPath} on ${prefix} route with method ${name}`);
|
|
35
36
|
}
|
package/dist/views.js
CHANGED
|
@@ -3,6 +3,7 @@ import path from "node:path";
|
|
|
3
3
|
import { parse } from "./compiler/parser";
|
|
4
4
|
import { render } from "./compiler/renderer";
|
|
5
5
|
import { tokenize } from "./compiler/tokenizer";
|
|
6
|
+
import { resolveResponse } from "./http/response";
|
|
6
7
|
let viewsPath = "";
|
|
7
8
|
export function setViewsPath(path) {
|
|
8
9
|
viewsPath = path;
|
|
@@ -15,7 +16,5 @@ export async function view(view, props) {
|
|
|
15
16
|
const tokens = tokenize(code);
|
|
16
17
|
const root = parse(tokens);
|
|
17
18
|
const rendered = render(root, props);
|
|
18
|
-
return
|
|
19
|
-
headers: { "Content-Type": "text/html; charset=utf-8" },
|
|
20
|
-
});
|
|
19
|
+
return resolveResponse(rendered);
|
|
21
20
|
}
|