@hyperspan/framework 0.3.5 → 0.4.0

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/server.js CHANGED
@@ -1858,8 +1858,25 @@ function createRoute(handler) {
1858
1858
  ..._middleware,
1859
1859
  async (context) => {
1860
1860
  const method = context.req.method.toUpperCase();
1861
+ if (method === "OPTIONS") {
1862
+ return context.html(render(html`
1863
+ <!DOCTYPE html>
1864
+ <html lang="en"></html>
1865
+ `), {
1866
+ status: 200,
1867
+ headers: {
1868
+ "Access-Control-Allow-Origin": "*",
1869
+ "Access-Control-Allow-Methods": [
1870
+ "HEAD",
1871
+ "OPTIONS",
1872
+ ...Object.keys(_handlers)
1873
+ ].join(", "),
1874
+ "Access-Control-Allow-Headers": "Content-Type, Authorization"
1875
+ }
1876
+ });
1877
+ }
1861
1878
  return returnHTMLResponse(context, () => {
1862
- const handler2 = _handlers[method];
1879
+ const handler2 = method === "HEAD" ? _handlers["GET"] : _handlers[method];
1863
1880
  if (!handler2) {
1864
1881
  throw new HTTPException(405, { message: "Method not allowed" });
1865
1882
  }
@@ -1908,7 +1925,24 @@ function createAPIRoute(handler) {
1908
1925
  ..._middleware,
1909
1926
  async (context) => {
1910
1927
  const method = context.req.method.toUpperCase();
1911
- const handler2 = _handlers[method];
1928
+ if (method === "OPTIONS") {
1929
+ return context.json({
1930
+ meta: { success: true, dtResponse: new Date },
1931
+ data: {}
1932
+ }, {
1933
+ status: 200,
1934
+ headers: {
1935
+ "Access-Control-Allow-Origin": "*",
1936
+ "Access-Control-Allow-Methods": [
1937
+ "HEAD",
1938
+ "OPTIONS",
1939
+ ...Object.keys(_handlers)
1940
+ ].join(", "),
1941
+ "Access-Control-Allow-Headers": "Content-Type, Authorization"
1942
+ }
1943
+ });
1944
+ }
1945
+ const handler2 = method === "HEAD" ? _handlers["GET"] : _handlers[method];
1912
1946
  if (!handler2) {
1913
1947
  return context.json({
1914
1948
  meta: { success: false, dtResponse: new Date },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyperspan/framework",
3
- "version": "0.3.5",
3
+ "version": "0.4.0",
4
4
  "description": "Hyperspan Web Framework",
5
5
  "main": "dist/server.ts",
6
6
  "types": "src/server.ts",
@@ -73,7 +73,10 @@ class HSAction extends HTMLElement {
73
73
  }
74
74
 
75
75
  connectedCallback() {
76
- actionFormObserver.observe(this, { childList: true, subtree: true });
76
+ setTimeout(() => {
77
+ bindHSActionForm(this, this.querySelector('form') as HTMLFormElement);
78
+ actionFormObserver.observe(this, { childList: true, subtree: true });
79
+ }, 10);
77
80
  }
78
81
  }
79
82
  window.customElements.define('hs-action', HSAction);
@@ -91,6 +94,10 @@ const actionFormObserver = new MutationObserver((list) => {
91
94
  * Bind the form inside an hs-action element to the action URL and submit handler
92
95
  */
93
96
  function bindHSActionForm(hsActionElement: HSAction, form: HTMLFormElement) {
97
+ if (!form) {
98
+ return;
99
+ }
100
+
94
101
  form.setAttribute('action', hsActionElement.getAttribute('url') || '');
95
102
  const submitHandler = (e: Event) => {
96
103
  formSubmitToRoute(e, form as HTMLFormElement, {
@@ -116,8 +123,6 @@ function formSubmitToRoute(e: Event, form: HTMLFormElement, opts: TFormSubmitOpt
116
123
  'X-Request-Type': 'partial',
117
124
  };
118
125
 
119
- let response: Response;
120
-
121
126
  const hsActionTag = form.closest('hs-action');
122
127
  const submitBtn = form.querySelector('button[type=submit],input[type=submit]');
123
128
  if (submitBtn) {
@@ -136,7 +141,6 @@ function formSubmitToRoute(e: Event, form: HTMLFormElement, opts: TFormSubmitOpt
136
141
  return '';
137
142
  }
138
143
 
139
- response = res;
140
144
  return res.text();
141
145
  })
142
146
  .then((content: string) => {
package/src/server.ts CHANGED
@@ -83,8 +83,32 @@ export function createRoute(handler?: THSRouteHandler): THSRoute {
83
83
  async (context: Context) => {
84
84
  const method = context.req.method.toUpperCase();
85
85
 
86
+ // Handle CORS preflight requests
87
+ if (method === 'OPTIONS') {
88
+ return context.html(
89
+ render(html`
90
+ <!DOCTYPE html>
91
+ <html lang="en"></html>
92
+ `),
93
+ {
94
+ status: 200,
95
+ headers: {
96
+ 'Access-Control-Allow-Origin': '*',
97
+ 'Access-Control-Allow-Methods': [
98
+ 'HEAD',
99
+ 'OPTIONS',
100
+ ...Object.keys(_handlers),
101
+ ].join(', '),
102
+ 'Access-Control-Allow-Headers': 'Content-Type, Authorization',
103
+ },
104
+ }
105
+ );
106
+ }
107
+
108
+ // Handle other requests, HEAD is GET with no body
86
109
  return returnHTMLResponse(context, () => {
87
- const handler = _handlers[method];
110
+ const handler = method === 'HEAD' ? _handlers['GET'] : _handlers[method];
111
+
88
112
  if (!handler) {
89
113
  throw new HTTPException(405, { message: 'Method not allowed' });
90
114
  }
@@ -142,7 +166,31 @@ export function createAPIRoute(handler?: THSAPIRouteHandler): THSAPIRoute {
142
166
  ..._middleware,
143
167
  async (context: Context) => {
144
168
  const method = context.req.method.toUpperCase();
145
- const handler = _handlers[method];
169
+
170
+ // Handle CORS preflight requests
171
+ if (method === 'OPTIONS') {
172
+ return context.json(
173
+ {
174
+ meta: { success: true, dtResponse: new Date() },
175
+ data: {},
176
+ },
177
+ {
178
+ status: 200,
179
+ headers: {
180
+ 'Access-Control-Allow-Origin': '*',
181
+ 'Access-Control-Allow-Methods': [
182
+ 'HEAD',
183
+ 'OPTIONS',
184
+ ...Object.keys(_handlers),
185
+ ].join(', '),
186
+ 'Access-Control-Allow-Headers': 'Content-Type, Authorization',
187
+ },
188
+ }
189
+ );
190
+ }
191
+
192
+ // Handle other requests, HEAD is GET with no body
193
+ const handler = method === 'HEAD' ? _handlers['GET'] : _handlers[method];
146
194
 
147
195
  if (!handler) {
148
196
  return context.json(