@hyperspan/framework 1.0.22 → 1.0.24

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyperspan/framework",
3
- "version": "1.0.22",
3
+ "version": "1.0.24",
4
4
  "description": "Hyperspan Web Framework",
5
5
  "main": "src/server.ts",
6
6
  "types": "src/server.ts",
@@ -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',
@@ -107,8 +107,12 @@ function formSubmitToRoute(e: Event, form: HTMLFormElement, opts: TFormSubmitOpt
107
107
  if (newUrl) {
108
108
  const resolved = new URL(newUrl, window.location.href);
109
109
 
110
- // If the new URL is the same as the current URL, we can just fetch the new HTML and apply it
111
- if (resolved.pathname === window.location.pathname) {
110
+ // Same-origin + same path: fetch updated HTML and morph in place. Cross-origin redirects
111
+ // must use full navigation (CORS and Idiomorph are not safe across domains).
112
+ if (
113
+ resolved.origin === window.location.origin &&
114
+ resolved.pathname === window.location.pathname
115
+ ) {
112
116
  const pageRes = await fetch(resolved.href, {
113
117
  headers: { Accept: 'text/html' },
114
118
  });
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 ActionFormHandlerResponse = ActionFormResponse | Response | Promise<Response>;
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
- ) => ActionFormHandlerResponse;
200
+ ) => ActionFormHandlerReturn | Promise<ActionFormHandlerReturn>;
201
201
  // Action API
202
202
  export interface Action<S extends z.ZodType | undefined = undefined> {
203
203
  _kind: 'hsAction';