@hyperspan/framework 1.0.22 → 1.0.23
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/package.json +1 -1
- package/src/actions.test.ts +28 -0
- package/src/types.ts +2 -2
package/package.json
CHANGED
package/src/actions.test.ts
CHANGED
|
@@ -71,6 +71,34 @@ describe('createAction', () => {
|
|
|
71
71
|
expect(responseText).toContain('<p>Hello, John Doe!</p>');
|
|
72
72
|
});
|
|
73
73
|
|
|
74
|
+
test('returns HSHtml directly from POST handler', async () => {
|
|
75
|
+
const action = createAction({
|
|
76
|
+
name: 'test',
|
|
77
|
+
}).form((c) => {
|
|
78
|
+
return html`
|
|
79
|
+
<form>
|
|
80
|
+
<input type="text" name="name" />
|
|
81
|
+
<button type="submit">Submit</button>
|
|
82
|
+
</form>
|
|
83
|
+
`;
|
|
84
|
+
}).post(async (c, { data }) => {
|
|
85
|
+
return html`<p>Hello, ${data?.name}!</p>`;
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
const formData = new FormData();
|
|
89
|
+
formData.append('name', 'Jane Doe');
|
|
90
|
+
|
|
91
|
+
const request = new Request(`http://localhost:3000${action._path()}`, {
|
|
92
|
+
method: 'POST',
|
|
93
|
+
body: formData,
|
|
94
|
+
});
|
|
95
|
+
const response = await action.fetch(request);
|
|
96
|
+
expect(response).toBeInstanceOf(Response);
|
|
97
|
+
expect(response.status).toBe(200);
|
|
98
|
+
const responseText = await response.text();
|
|
99
|
+
expect(responseText).toContain('<p>Hello, Jane Doe!</p>');
|
|
100
|
+
});
|
|
101
|
+
|
|
74
102
|
test('errors thrown on POST handler provided by user are caught and rendered', async () => {
|
|
75
103
|
const action = createAction({
|
|
76
104
|
name: 'test',
|
package/src/types.ts
CHANGED
|
@@ -190,14 +190,14 @@ export namespace Hyperspan {
|
|
|
190
190
|
c: Context, props: ActionFormProps<S>
|
|
191
191
|
) => ActionFormResponse;
|
|
192
192
|
// Form handler
|
|
193
|
-
export type
|
|
193
|
+
export type ActionFormHandlerReturn = RouteHandlerReturn;
|
|
194
194
|
export type ActionFormHandlerProps<S extends z.ZodType | undefined = undefined> = {
|
|
195
195
|
data: InferActionData<S>;
|
|
196
196
|
error?: ZodValidationError | Error;
|
|
197
197
|
};
|
|
198
198
|
export type ActionFormHandler<S extends z.ZodType | undefined = undefined> = (
|
|
199
199
|
c: Context, props: ActionFormHandlerProps<S>
|
|
200
|
-
) =>
|
|
200
|
+
) => ActionFormHandlerReturn | Promise<ActionFormHandlerReturn>;
|
|
201
201
|
// Action API
|
|
202
202
|
export interface Action<S extends z.ZodType | undefined = undefined> {
|
|
203
203
|
_kind: 'hsAction';
|