-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.test-d.ts
148 lines (130 loc) · 2.99 KB
/
index.test-d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { expectError } from "tsd";
import type {
EnhanceApiFn,
EnhanceApiFnChain,
EnhanceApiRes,
EnhanceElemFn,
EnhanceHeadFn,
EnhanceHtmlFn,
EnhancePageFn,
EnhancePreflightFn,
} from ".";
// Preflight
export const preflight: EnhancePreflightFn = function (req) {
const { method, path } = req;
const title = `Todos — ${path} `;
console.log(`Handling ${method} ${path}...`);
return { title };
};
type Todo = {
title: string;
completed?: boolean;
};
// API Handler
// sync
export const get: EnhanceApiFn = function (request) {
console.log(`Handling ${request.path}...`);
const filter: string = request.body.filter as string;
const todos: Todo[] = [
{ title: "todo 1", completed: false },
{ title: "todo 2", completed: true },
{ title: "todo 3" },
];
let invalidResponse: EnhanceApiRes = { json: { todos } };
expectError(
(invalidResponse = {
json: { foo: "bar" },
body: "",
}),
);
return {
session: {},
status: 200,
cacheControl: "public, max-age=14400",
json: { filter, todos },
};
};
// async
export const post: EnhanceApiFn = async function (req) {
return {
json: { req },
};
};
// chain
export const patch: EnhanceApiFnChain = [
(req) => {
return { session: { foo: "bar", ...req.session } };
},
async (req) => {
return { json: { path: req.path } };
},
async () => {
return { status: 302, location: "/" };
},
];
// Custom Element
export const TodoItem: EnhanceElemFn = function ({
html,
state: { attrs, store, instanceID, context },
}) {
const todoId = attrs["todo-id"];
const completed = typeof attrs.completed === "string";
const myHtml: EnhanceHtmlFn = html;
typeof myHtml === "function";
context.todoId = todoId;
store.myCustomData;
return html`
<div class="flex gap-2 mb-1">
<input
id="todo-${instanceID}"
todo-id="${todoId}"
type="checkbox"
name="completed"
${completed ? "checked" : ""}
/>
<slot></slot>
</div>
`;
};
// Page Element
export const page: EnhancePageFn = function ({
html,
state: { attrs, store, instanceID, context },
}) {
const todoId = attrs["todo-id"];
const completed = typeof attrs.completed === "string";
const myHtml: EnhanceHtmlFn = html;
typeof myHtml === "function";
context.todoId = todoId;
store.myCustomData;
return html`
<div class="flex gap-2 mb-1">
<input
id="todo-${instanceID}"
todo-id="${todoId}"
type="checkbox"
name="completed"
${completed ? "checked" : ""}
/>
<slot></slot>
</div>
`;
};
// Head Function
export const Head: EnhanceHeadFn = function (state) {
const { req, status, error, store } = state;
if (status > 399 && error) {
return error;
}
const { path } = req;
const title = `Todos — ${path} - ${store.docTitle}`;
return `
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>${title}</title>
<link rel="stylesheet" href="/_static/styles.css">
</head>
`;
};