@torpor/build 0.4.14 → 1.0.1

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.
Files changed (193) hide show
  1. package/README.md +20 -2
  2. package/dist/Jsonify-Dg6gChd3.d.mts +10 -0
  3. package/dist/Jsonify-Dg6gChd3.d.mts.map +1 -0
  4. package/dist/ParseRouteParams-DBmqlmLh.d.mts +42 -0
  5. package/dist/ParseRouteParams-DBmqlmLh.d.mts.map +1 -0
  6. package/dist/{RouteType-B6BMOyYw.mjs → Router-C4ADUfhv.mjs} +30 -42
  7. package/dist/Router-C4ADUfhv.mjs.map +1 -0
  8. package/dist/Server-BnjLJleZ.mjs +104 -0
  9. package/dist/Server-BnjLJleZ.mjs.map +1 -0
  10. package/dist/Server-Cler2w5O.d.mts +61 -0
  11. package/dist/Server-Cler2w5O.d.mts.map +1 -0
  12. package/dist/ServerEvent-CTx3Q-hk.d.mts +48 -0
  13. package/dist/ServerEvent-CTx3Q-hk.d.mts.map +1 -0
  14. package/dist/{ServerEvent-CxPwA4lH.mjs → ServerEvent-Dljn5i7A.mjs} +37 -11
  15. package/dist/ServerEvent-Dljn5i7A.mjs.map +1 -0
  16. package/dist/Site-CE0_o5Ci.d.mts +566 -0
  17. package/dist/Site-CE0_o5Ci.d.mts.map +1 -0
  18. package/dist/StandardSchema-D28L8_zZ.d.mts +100 -0
  19. package/dist/StandardSchema-D28L8_zZ.d.mts.map +1 -0
  20. package/dist/TypedResponse-6lznqN4U.d.mts +22 -0
  21. package/dist/TypedResponse-6lznqN4U.d.mts.map +1 -0
  22. package/dist/{_page-BQs4WvCO.mjs → _page-4cQKs_Dn.mjs} +3 -5
  23. package/dist/_page-4cQKs_Dn.mjs.map +1 -0
  24. package/dist/bin/index.d.mts +1 -1
  25. package/dist/bin/index.js +15 -11
  26. package/dist/bin/index.js.map +1 -1
  27. package/dist/clientEntry.d.mts +1 -0
  28. package/dist/clientEntry.mjs +59 -0
  29. package/dist/clientEntry.mjs.map +1 -0
  30. package/dist/clientEntryDev.d.mts +1 -0
  31. package/dist/clientEntryDev.mjs +7 -0
  32. package/dist/clientEntryDev.mjs.map +1 -0
  33. package/dist/dev.d.mts +3 -0
  34. package/dist/dev.mjs +3 -0
  35. package/dist/endpoint-EULRGCKA.mjs +108 -0
  36. package/dist/endpoint-EULRGCKA.mjs.map +1 -0
  37. package/dist/flattenHeaders-DM5qsSqO.mjs +13 -0
  38. package/dist/flattenHeaders-DM5qsSqO.mjs.map +1 -0
  39. package/dist/form.d.mts +35 -0
  40. package/dist/form.d.mts.map +1 -0
  41. package/dist/form.mjs +48 -0
  42. package/dist/form.mjs.map +1 -0
  43. package/dist/index.d.mts +72 -156
  44. package/dist/index.d.mts.map +1 -1
  45. package/dist/index.mjs +158 -26
  46. package/dist/index.mjs.map +1 -1
  47. package/dist/load-BntzvmwL.mjs +228 -0
  48. package/dist/load-BntzvmwL.mjs.map +1 -0
  49. package/dist/nav.d.mts +58 -5
  50. package/dist/nav.d.mts.map +1 -1
  51. package/dist/nav.mjs +63 -209
  52. package/dist/nav.mjs.map +1 -1
  53. package/dist/openapi.d.mts +144 -0
  54. package/dist/openapi.d.mts.map +1 -0
  55. package/dist/openapi.mjs +2 -0
  56. package/dist/pathTrie-D4Ax2Mu9.mjs +160 -0
  57. package/dist/pathTrie-D4Ax2Mu9.mjs.map +1 -0
  58. package/dist/plugin-D8Www11B.mjs +202 -0
  59. package/dist/plugin-D8Www11B.mjs.map +1 -0
  60. package/dist/{response-C5TtAsh1.mjs → response-Bg4w2S1q.mjs} +16 -39
  61. package/dist/response-Bg4w2S1q.mjs.map +1 -0
  62. package/dist/response.d.mts +309 -289
  63. package/dist/response.d.mts.map +1 -1
  64. package/dist/response.mjs +3 -4
  65. package/dist/run.d.mts +11 -4
  66. package/dist/run.d.mts.map +1 -1
  67. package/dist/run.mjs +2 -8
  68. package/dist/runOpenApi-DlOgF2Il.mjs +1419 -0
  69. package/dist/runOpenApi-DlOgF2Il.mjs.map +1 -0
  70. package/dist/schema.d.mts +32 -0
  71. package/dist/schema.d.mts.map +1 -0
  72. package/dist/schema.mjs +2 -0
  73. package/dist/server/Server.d.mts +2 -0
  74. package/dist/server/Server.mjs +2 -0
  75. package/dist/server.d.mts +11 -8
  76. package/dist/server.d.mts.map +1 -1
  77. package/dist/server.mjs +133 -4
  78. package/dist/server.mjs.map +1 -0
  79. package/dist/serverEntry-1qY3wZig.mjs +351 -0
  80. package/dist/serverEntry-1qY3wZig.mjs.map +1 -0
  81. package/dist/serverEntry-BAO8m03V.d.mts +6 -0
  82. package/dist/serverEntry-BAO8m03V.d.mts.map +1 -0
  83. package/dist/serverEntry.d.mts +2 -0
  84. package/dist/serverEntry.mjs +2 -0
  85. package/dist/state.d.mts.map +1 -1
  86. package/dist/state.mjs +2 -3
  87. package/dist/test.d.mts +8 -10
  88. package/dist/test.d.mts.map +1 -1
  89. package/dist/test.mjs +181 -79
  90. package/dist/test.mjs.map +1 -1
  91. package/dist/{seeOther-B4Yhu9iq.mjs → unprocessable-DAZbrDeR.mjs} +34 -13
  92. package/dist/unprocessable-DAZbrDeR.mjs.map +1 -0
  93. package/dist/validate-Ok4krlkX.mjs +40 -0
  94. package/dist/validate-Ok4krlkX.mjs.map +1 -0
  95. package/package.json +30 -18
  96. package/dist/RouteType-B6BMOyYw.mjs.map +0 -1
  97. package/dist/Server-CF5wDJp6.d.mts +0 -78
  98. package/dist/Server-CF5wDJp6.d.mts.map +0 -1
  99. package/dist/ServerEvent-CxPwA4lH.mjs.map +0 -1
  100. package/dist/Site-DgC6WWn1.d.mts +0 -78
  101. package/dist/Site-DgC6WWn1.d.mts.map +0 -1
  102. package/dist/_page-BQs4WvCO.mjs.map +0 -1
  103. package/dist/connectMiddleware-D7nehjj3.mjs +0 -252
  104. package/dist/connectMiddleware-D7nehjj3.mjs.map +0 -1
  105. package/dist/pathToRegex-TUAMc3zS.mjs +0 -11
  106. package/dist/pathToRegex-TUAMc3zS.mjs.map +0 -1
  107. package/dist/response-C5TtAsh1.mjs.map +0 -1
  108. package/dist/run-BPFtSYA3.mjs +0 -332
  109. package/dist/run-BPFtSYA3.mjs.map +0 -1
  110. package/dist/seeOther-B4Yhu9iq.mjs.map +0 -1
  111. package/src/bin/index.ts +0 -11
  112. package/src/index.ts +0 -30
  113. package/src/nav/formSubmit.ts +0 -56
  114. package/src/nav/load.ts +0 -9
  115. package/src/nav/loadData.ts +0 -134
  116. package/src/nav/navigate.ts +0 -164
  117. package/src/nav/reload.ts +0 -9
  118. package/src/nav.ts +0 -4
  119. package/src/response/badRequest.ts +0 -19
  120. package/src/response/created.ts +0 -20
  121. package/src/response/forbidden.ts +0 -21
  122. package/src/response/found.ts +0 -30
  123. package/src/response/methodNotAllowed.ts +0 -18
  124. package/src/response/movedPermanently.ts +0 -25
  125. package/src/response/notFound.ts +0 -24
  126. package/src/response/notModified.ts +0 -34
  127. package/src/response/ok.ts +0 -29
  128. package/src/response/permanentRedirect.ts +0 -30
  129. package/src/response/response.ts +0 -22
  130. package/src/response/seeOther.ts +0 -19
  131. package/src/response/serverError.ts +0 -24
  132. package/src/response/temporaryRedirect.ts +0 -31
  133. package/src/response/transfer.ts +0 -14
  134. package/src/response/unauthorized.ts +0 -21
  135. package/src/response/unprocessable.ts +0 -18
  136. package/src/response.ts +0 -37
  137. package/src/run/prepareTemplate.ts +0 -39
  138. package/src/run/run.ts +0 -62
  139. package/src/run/runBuild.ts +0 -77
  140. package/src/run/runDev.ts +0 -89
  141. package/src/run/runPreview.ts +0 -93
  142. package/src/run.ts +0 -6
  143. package/src/server/CookieHelper.ts +0 -36
  144. package/src/server/HeaderHelper.ts +0 -25
  145. package/src/server/Routerx.ts +0 -72
  146. package/src/server/Server.ts +0 -144
  147. package/src/server/ServerEvent.ts +0 -39
  148. package/src/server/connect/bufferToArrayBuffer.ts +0 -8
  149. package/src/server/connect/connectMiddleware.ts +0 -81
  150. package/src/server/connect/flattenHeaders.ts +0 -44
  151. package/src/server/connect/nodeMessageToNodeResponse.ts +0 -89
  152. package/src/server/connect/readableToBuffer.ts +0 -16
  153. package/src/server/connect/requestToNodeMessage.ts +0 -26
  154. package/src/server/contentType.ts +0 -85
  155. package/src/server/types/HttpMethod.ts +0 -12
  156. package/src/server/types/MiddlewareFunction.ts +0 -8
  157. package/src/server/types/ServerFunction.ts +0 -7
  158. package/src/server.ts +0 -7
  159. package/src/site/Router.ts +0 -164
  160. package/src/site/Site.ts +0 -214
  161. package/src/site/clientEntry.ts +0 -108
  162. package/src/site/clientEntryDev.ts +0 -3
  163. package/src/site/defaultAdapter.ts +0 -11
  164. package/src/site/manifest.ts +0 -54
  165. package/src/site/serverEntry.ts +0 -439
  166. package/src/state/$page.ts +0 -24
  167. package/src/state/client.ts +0 -15
  168. package/src/state.ts +0 -3
  169. package/src/test/runTest.ts +0 -350
  170. package/src/test.ts +0 -3
  171. package/src/types/Adapter.ts +0 -8
  172. package/src/types/ClientState.ts +0 -8
  173. package/src/types/InternalState.ts +0 -7
  174. package/src/types/LayoutHandler.ts +0 -5
  175. package/src/types/LayoutPath.ts +0 -10
  176. package/src/types/ManifestRoute.ts +0 -6
  177. package/src/types/PageEndPoint.ts +0 -39
  178. package/src/types/PageLoadEvent.ts +0 -14
  179. package/src/types/PageServerAction.ts +0 -7
  180. package/src/types/PageServerEndPoint.ts +0 -16
  181. package/src/types/PageServerLoad.ts +0 -7
  182. package/src/types/PageState.ts +0 -8
  183. package/src/types/Route.ts +0 -11
  184. package/src/types/RouteHandler.ts +0 -16
  185. package/src/types/RouteLayoutHandler.ts +0 -5
  186. package/src/types/RouteMatchResult.ts +0 -7
  187. package/src/types/RouteType.ts +0 -19
  188. package/src/types/ServerEndPoint.ts +0 -37
  189. package/src/types/ServerHook.ts +0 -11
  190. package/src/types/ServerLoadEvent.ts +0 -37
  191. package/src/types/ServerRequest.ts +0 -7
  192. package/src/utils/pathToRegex.ts +0 -15
  193. package/src/vite-env.d.ts +0 -2
@@ -1,164 +0,0 @@
1
- import { clearLayoutSlot, fillLayoutSlot, hydrate } from "@torpor/view";
2
- import { type Component, type SlotRender } from "@torpor/view";
3
- import { mount } from "@torpor/view";
4
- import $page from "../state/$page";
5
- import client from "../state/client";
6
- import type LayoutPath from "../types/LayoutPath";
7
- import type PageEndPoint from "../types/PageEndPoint";
8
- import type PageServerEndPoint from "../types/PageServerEndPoint";
9
- import formSubmit from "./formSubmit";
10
- import loadData from "./loadData";
11
-
12
- // @ts-ignore
13
- export default async function navigate(url: URL, withHydration = false): Promise<boolean> {
14
- let parent = document.getElementById("app");
15
- if (!parent) {
16
- // TODO: 500
17
- console.log("500");
18
- return false;
19
- }
20
-
21
- const path = url.pathname;
22
- const query = url.searchParams;
23
-
24
- //console.log(`navigating to '${path}'${query.size ? ` with ${query}` : ""}`);
25
-
26
- const route = client.router.match(path, query);
27
- if (!route) {
28
- // TODO: 404
29
- console.log("404");
30
- return false;
31
- }
32
-
33
- // Update $page before building the components
34
- $page.url = url;
35
- if (path.endsWith("/_error")) {
36
- $page.status = parseInt(query.get("status") ?? "404");
37
- $page.error = { message: query.get("message") ?? "" };
38
- // Make it look a bit classier by removing the query
39
- window.history.replaceState({}, "", url.toString().split("?")[0]);
40
- } else {
41
- $page.status = 200;
42
- }
43
- const handler = route.handler;
44
- const params = route.params || {};
45
-
46
- // There must be a client endpoint with a component
47
- const clientEndPoint: PageEndPoint | undefined = (await handler.endPoint()).default;
48
- if (!clientEndPoint?.component) {
49
- // TODO: 404
50
- console.log("404");
51
- return false;
52
- }
53
-
54
- // There may be a server endpoint
55
- const serverEndPoint: PageServerEndPoint | undefined =
56
- handler.serverEndPoint && (await handler.serverEndPoint())?.default;
57
-
58
- let newLayoutStack: LayoutPath[] = [];
59
-
60
- // Pass the data into $props
61
- // TODO: Don't load if this is the first time -- it should have been passed
62
- // to us, somehow...
63
- const data = await loadData(
64
- handler,
65
- params,
66
- path,
67
- query,
68
- newLayoutStack,
69
- clientEndPoint,
70
- serverEndPoint,
71
- );
72
- // We may have form data in a hidden input -- not sure if this is the best
73
- // way to do it
74
- let formInput = document.getElementById("t-form-data") as HTMLInputElement;
75
- let form: Record<string, string> | undefined;
76
- if (formInput) {
77
- form = JSON.parse(formInput.value);
78
- $page.form = form;
79
- formInput.remove();
80
- }
81
- let $props: Record<string, any> = { data };
82
-
83
- // We may have form data in $page.form, either from the hidden input
84
- // (above), or added via onformsubmit (below)
85
- $props.form = $page.form;
86
-
87
- // Add some special context for submitting Forms client-side
88
- const $context = {
89
- TorporBuildContext: { onformsubmit: formSubmit },
90
- };
91
-
92
- client.layoutStack.push({ path: route.handler.path, data: {}, reuse: false, slotRegion: null });
93
- client.layoutStack = newLayoutStack;
94
- let layoutStack = client.layoutStack;
95
-
96
- // If there are layouts, work our way upwards, pushing each component into
97
- // the default slot of its parent
98
- // TODO: There's probably a nicer way to do this with reducers or something
99
- let component = clientEndPoint.component as Component;
100
- let slots: Record<string, SlotRender> | undefined = undefined;
101
- if (handler.layouts) {
102
- let slotFunctions: SlotRender[] = [];
103
- // The last slot function will render the client component
104
- slotFunctions[handler.layouts.length] = function clientComponent(parent, anchor) {
105
- let i = layoutStack.length - 1;
106
- layoutStack[i].slotRegion = fillLayoutSlot(
107
- clientEndPoint.component!,
108
- slotFunctions[i + 1],
109
- parent,
110
- anchor,
111
- $props,
112
- $context,
113
- );
114
- };
115
-
116
- // Each earlier slot function will render a layout
117
- for (let i = handler.layouts.length - 1; i >= 0; i--) {
118
- const layoutEndPoint: PageEndPoint | undefined = (await handler.layouts[i].endPoint())
119
- ?.default;
120
- if (layoutEndPoint?.component) {
121
- if (layoutStack[i].reuse) {
122
- // Set the parent to add the new content to (from the old
123
- // content), clear the range under this point, and set the
124
- // component to the slot function within this layout
125
- parent = layoutStack[i].slotRegion.startNode.parentNode as HTMLElement;
126
- clearLayoutSlot(layoutStack[i].slotRegion);
127
- component = slotFunctions[i + 1];
128
- break;
129
- } else if (i === 0) {
130
- component = layoutEndPoint.component as Component;
131
- slots = { _: slotFunctions[i + 1] };
132
- } else {
133
- slotFunctions[i] = function layoutComponent(parent, anchor, _, $context) {
134
- layoutStack[i - 1].slotRegion = fillLayoutSlot(
135
- layoutEndPoint.component!,
136
- slotFunctions[i + 1],
137
- parent,
138
- anchor,
139
- $props,
140
- $context,
141
- );
142
- };
143
- }
144
- }
145
- }
146
- }
147
-
148
- if (withHydration) {
149
- hydrate(parent, component, $props, slots);
150
- } else {
151
- try {
152
- mount(parent, component, $props, slots);
153
- } catch (error) {
154
- // TODO: Show a proper Error component
155
- parent.innerHTML = '<span style="color: red">Script syntax error</span><p>' + error + "</p>";
156
- console.log(error);
157
- }
158
- }
159
-
160
- // Reset prefetched data on each navigation
161
- client.prefetchedData = {};
162
-
163
- return true;
164
- }
package/src/nav/reload.ts DELETED
@@ -1,9 +0,0 @@
1
- import navigate from "./navigate";
2
-
3
- /**
4
- * Reloads data for the currently loaded page and rerenders it.
5
- */
6
- export default async function reload(): Promise<boolean> {
7
- const url = new URL(document.location.href);
8
- return await navigate(url);
9
- }
package/src/nav.ts DELETED
@@ -1,4 +0,0 @@
1
- import load from "./nav/load";
2
- import reload from "./nav/reload";
3
-
4
- export { load, reload };
@@ -1,19 +0,0 @@
1
- import response from "./response";
2
-
3
- /**
4
- * 400 Bad Request
5
- *
6
- * The HTTP 400 Bad Request client error response status code indicates that the
7
- * server would not process the request due to something the server considered
8
- * to be a client error. The reason for a 400 response is typically due to
9
- * malformed request syntax, invalid request message framing, or deceptive
10
- * request routing.
11
- *
12
- * Clients that receive a 400 response should expect that repeating the request
13
- * without modification will fail with the same error.
14
- *
15
- * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/400
16
- */
17
- export default function badRequest(body?: object | string): Response {
18
- return response(400, body ?? "Bad request");
19
- }
@@ -1,20 +0,0 @@
1
- import response from "./response";
2
-
3
- /**
4
- * 201 Created
5
- *
6
- * The HTTP 201 Created successful response status code indicates that the HTTP
7
- * request has led to the creation of a resource. This status code is commonly
8
- * sent as the result of a POST request.
9
- *
10
- * The new resource, or a description and link to the new resource, is created
11
- * before the response is returned. The newly-created items can be returned in
12
- * the body of the response message, but must be locatable by the URL of the
13
- * initiating request or by the URL in the value of the Location header provided
14
- * with the response.
15
- *
16
- * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/201
17
- */
18
- export default function created(body?: object | string): Response {
19
- return response(201, body);
20
- }
@@ -1,21 +0,0 @@
1
- import response from "./response";
2
-
3
- /**
4
- * 403 Forbidden
5
- *
6
- * The HTTP 403 Forbidden client error response status code indicates that the
7
- * server understood the request but refused to process it. This status is
8
- * similar to 401, except that for 403 Forbidden responses, authenticating or
9
- * re-authenticating makes no difference. The request failure is tied to
10
- * application logic, such as insufficient permissions to a resource or action.
11
- *
12
- * Clients that receive a 403 response should expect that repeating the request
13
- * without modification will fail with the same error. Server owners may decide
14
- * to send a 404 response instead of a 403 if acknowledging the existence of a
15
- * resource to clients with insufficient privileges is not desired.
16
- *
17
- * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/403
18
- */
19
- export default function forbidden(body?: object | string): Response {
20
- return response(403, body ?? "Forbidden");
21
- }
@@ -1,30 +0,0 @@
1
- import transfer from "./transfer";
2
-
3
- /**
4
- * 302 Found
5
- *
6
- * The HTTP 302 Found redirection response status code indicates that the
7
- * requested resource has been temporarily moved to the URL in the Location
8
- * header.
9
- *
10
- * A browser receiving this status will automatically request the resource at
11
- * the URL in the Location header, redirecting the user to the new page. Search
12
- * engines receiving this response will not attribute links to the original URL
13
- * to the new resource, meaning no SEO value is transferred to the new URL.
14
- *
15
- * > Note: In the Fetch Standard, when a user agent receives a 302 in response
16
- * > to a POST request, it uses the GET method in the subsequent redirection
17
- * > request, as permitted by the HTTP specification. To avoid user agents
18
- * > modifying the request, use 307 Temporary Redirect instead, as altering the
19
- * > method after a 307 response is prohibited.
20
- *
21
- * > In cases where you want any request method to be changed to GET, use 303
22
- * > See Other. This is useful when you want to give a response to a PUT method
23
- * > that is not the uploaded resource but a confirmation message such as: "you
24
- * > successfully uploaded XYZ".
25
- *
26
- * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/302
27
- */
28
- export default function permRedirect(location: string): Response {
29
- return transfer(308, location);
30
- }
@@ -1,18 +0,0 @@
1
- import response from "./response";
2
-
3
- /**
4
- * 405 Method Not Allowed
5
- *
6
- * The HTTP 405 Method Not Allowed client error response status code indicates
7
- * that the server knows the request method, but the target resource doesn't
8
- * support this method. The server must generate an Allow header in a 405
9
- * response with a list of methods that the target resource currently supports.
10
- *
11
- * Improper server-side permissions set on files or directories may cause a 405
12
- * response when the request would otherwise be expected to succeed.
13
- *
14
- * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/405
15
- */
16
- export default function methodNotAllowed(body?: object | string): Response {
17
- return response(405, body ?? "Method not allowed");
18
- }
@@ -1,25 +0,0 @@
1
- import transfer from "./transfer";
2
-
3
- /**
4
- * 301 Moved Permanently
5
- *
6
- * The HTTP 301 Moved Permanently redirection response status code indicates
7
- * that the requested resource has been permanently moved to the URL in the
8
- * Location header.
9
- *
10
- * A browser receiving this status will automatically request the resource at
11
- * the URL in the Location header, redirecting the user to the new page. Search
12
- * engines receiving this response will attribute links to the original URL to
13
- * the redirected resource, passing the SEO ranking to the new URL.
14
- *
15
- * > Note: In the Fetch Standard, when a user agent receives a 301 in response
16
- * > to a POST request, it uses the GET method in the subsequent redirection
17
- * > request, as permitted by the HTTP specification. To avoid user agents
18
- * > modifying the request, use 308 Permanent Redirect instead, as altering the
19
- * > method after a 308 response is prohibited.
20
- *
21
- * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/301
22
- */
23
- export default function permRedirect(location: string): Response {
24
- return transfer(308, location);
25
- }
@@ -1,24 +0,0 @@
1
- import response from "./response";
2
-
3
- /**
4
- * 404 Not Found
5
- *
6
- * The HTTP 404 Not Found client error response status code indicates that the
7
- * server cannot find the requested resource. Links that lead to a 404 page are
8
- * often called broken or dead links and can be subject to link rot.
9
- *
10
- * A 404 status code only indicates that the resource is missing without
11
- * indicating if this is temporary or permanent. If a resource is permanently
12
- * removed, servers should send the 410 Gone status instead.
13
- *
14
- * 404 errors on a website can lead to a poor user experience for your visitors,
15
- * so the number of broken links (internal and external) should be minimized to
16
- * prevent frustration for readers. Common causes of 404 responses are mistyped
17
- * URLs or pages that are moved or deleted without redirection. For more
18
- * information, see the Redirections in HTTP guide.
19
- *
20
- * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/404
21
- */
22
- export default function notFound(body?: object | string): Response {
23
- return response(404, body ?? "Not found");
24
- }
@@ -1,34 +0,0 @@
1
- import transfer from "./transfer";
2
-
3
- /**
4
- * 304 Not Modified
5
- *
6
- * The HTTP 304 Not Modified redirection response status code indicates that
7
- * there is no need to retransmit the requested resources.
8
- *
9
- * This response code is sent when the request is a conditional GET or HEAD
10
- * request with an If-None-Match or an If-Modified-Since header and the
11
- * condition evaluates to 'false'. It confirms that the resource cached by the
12
- * client is still valid and that the server would have sent a 200 OK response
13
- * with the resource if the condition evaluated to 'true'. See HTTP caching for
14
- * more information.
15
- *
16
- * The response must not contain a body and must include the headers that would
17
- * have been sent in an equivalent 200 response, such as:
18
- *
19
- * - Cache-Control
20
- * - Content-Location
21
- * - Date
22
- * - ETag
23
- * - Expires
24
- * - Vary
25
- *
26
- * > Note: Many developer tools' network panels of browsers create extraneous
27
- * > requests leading to 304 responses, so that access to the local cache is
28
- * > visible to developers.
29
- *
30
- * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/304
31
- */
32
- export default function notModified(location: string): Response {
33
- return transfer(304, location);
34
- }
@@ -1,29 +0,0 @@
1
- import response from "./response";
2
-
3
- /**
4
- * 200 OK
5
- *
6
- * The HTTP 200 OK successful response status code indicates that a request has
7
- * succeeded. A 200 OK response is cacheable by default.
8
- *
9
- * A 200 OK response has a different meaning and format depending on the HTTP
10
- * request method. Here's how they vary for different methods:
11
- *
12
- * - GET: A resource was retrieved by the server and included in the response
13
- * body.
14
- * - POST: An action succeeded; the response has a message body describing the
15
- * result.
16
- * - HEAD: Identical to GET, except there is no message body.
17
- * - TRACE: The response has a message body containing the request as received
18
- * by the server.
19
- *
20
- * Although possible, successful PUT or DELETE requests often do not result in a
21
- * 200 OK response. It is more common to see 201 Created if the resource is
22
- * uploaded or created for the first time, or 204 No Content upon successful
23
- * deletion of a resource.
24
- *
25
- * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/200
26
- */
27
- export default function ok(body?: object | string): Response {
28
- return response(200, body);
29
- }
@@ -1,30 +0,0 @@
1
- import transfer from "./transfer";
2
-
3
- /**
4
- * 308 Permanent Redirect
5
- *
6
- * The HTTP 308 Permanent Redirect redirection response status code indicates
7
- * that the requested resource has been permanently moved to the URL given by
8
- * the Location header.
9
- *
10
- * A browser receiving this status will automatically request the resource at
11
- * the URL in the Location header, redirecting the user to the new page. Search
12
- * engines receiving this response will attribute links to the original URL to
13
- * the redirected resource, passing the SEO ranking to the new URL.
14
- *
15
- * The request method and the body will not be modified by the client in the
16
- * redirected request. A 301 Moved Permanently requires the request method and
17
- * the body to remain unchanged when redirection is performed, but this is
18
- * incorrectly handled by older clients to use the GET method instead.
19
- *
20
- * > Note: Some Web applications may use the 308 Permanent Redirect in a
21
- * > non-standard way and for different purposes. For example, Google Drive uses
22
- * > a 308 Resume Incomplete response to indicate to the client when an
23
- * > unfinished upload has stalled. See Perform a resumable download on the
24
- * > Google Drive documentation for more information.
25
- *
26
- * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/308
27
- */
28
- export default function permanentRedirect(location: string): Response {
29
- return transfer(308, location);
30
- }
@@ -1,22 +0,0 @@
1
- /**
2
- * Creates a response with the supplied status code and optional body.
3
- *
4
- * If the body is an object, it will be converted to JSON and the content-type
5
- * header set to "application/json". Otherwise, if the body is a string, the
6
- * content-type header will be set to "text/plain".
7
- */
8
- export default function response(status: number, body?: object | string): Response {
9
- let headers: Record<string, string> | undefined = undefined;
10
- if (body) {
11
- if (typeof body === "object") {
12
- headers = { "content-type": "application/json" };
13
- body = JSON.stringify(body);
14
- } else {
15
- headers = { "content-type": "text/plain" };
16
- }
17
- }
18
- return new Response(body, {
19
- status,
20
- headers,
21
- });
22
- }
@@ -1,19 +0,0 @@
1
- import transfer from "./transfer";
2
-
3
- /**
4
- * 303 See Other
5
- *
6
- * The HTTP 303 See Other redirection response status code indicates that the
7
- * browser should redirect to the URL in the Location header instead of
8
- * rendering the requested resource.
9
- *
10
- * This response code is often sent back as a result of PUT or POST methods so
11
- * the client may retrieve a confirmation, or view a representation of a
12
- * real-world object (see HTTP range-14). The method to retrieve the redirected
13
- * resource is always GET.
14
- *
15
- * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/303
16
- */
17
- export default function seeOther(location: string): Response {
18
- return transfer(303, location);
19
- }
@@ -1,24 +0,0 @@
1
- import response from "./response";
2
-
3
- /**
4
- * 500 Server Error
5
- *
6
- * The HTTP 500 Internal Server Error server error response status code
7
- * indicates that the server encountered an unexpected condition that prevented
8
- * it from fulfilling the request. This error is a generic "catch-all" response
9
- * to server issues, indicating that the server cannot find a more appropriate
10
- * 5XX error to respond with.
11
- *
12
- * If you're a visitor seeing 500 errors on a web page, these issues require
13
- * investigation by server owners or administrators. There are many possible
14
- * causes of 500 errors, including: improper server configuration, out-of-memory
15
- * (OOM) issues, unhandled exceptions, improper file permissions, or other
16
- * complex factors. Server administrators may proactively log occurrences of
17
- * server error responses, like the 500 status code, with details about the
18
- * initiating requests to improve the stability of a service in the future.
19
- *
20
- * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/500
21
- */
22
- export default function serverError(body?: object | string): Response {
23
- return response(500, body ?? "Server error");
24
- }
@@ -1,31 +0,0 @@
1
- import transfer from "./transfer";
2
-
3
- /**
4
- * 307 Temporary Redirect
5
- *
6
- * The HTTP 307 Temporary Redirect redirection response status code indicates
7
- * that the resource requested has been temporarily moved to the URL in the
8
- * Location header.
9
- *
10
- * A browser receiving this status will automatically request the resource at
11
- * the URL in the Location header, redirecting the user to the new page. Search
12
- * engines receiving this response will not attribute links to the original URL
13
- * to the new resource, meaning no SEO value is transferred to the new URL.
14
- *
15
- * The method and the body of the original request are reused to perform the
16
- * redirected request. In the cases where you want the request method to be
17
- * changed to GET, use 303 See Other instead. This is useful when you want to
18
- * give an answer to a successful PUT request that is not the uploaded resource,
19
- * but a status monitor or confirmation message like "You have successfully
20
- * uploaded XYZ".
21
- *
22
- * The difference between 307 and 302 is that 307 guarantees that the client
23
- * will not change the request method and body when the redirected request is
24
- * made. With 302, older clients incorrectly changed the method to GET. 307 and
25
- * 302 responses are identical when the request method is GET.
26
- *
27
- * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/307
28
- */
29
- export default function temporaryRedirect(location: string): Response {
30
- return transfer(307, location);
31
- }
@@ -1,14 +0,0 @@
1
- /**
2
- * Creates a redirect response with the supplied status code.
3
- */
4
- export default function transfer(
5
- status: 300 | 301 | 302 | 303 | 304 | 307 | 308,
6
- location: string,
7
- ): Response {
8
- return new Response(null, {
9
- status,
10
- headers: {
11
- location,
12
- },
13
- });
14
- }
@@ -1,21 +0,0 @@
1
- import response from "./response";
2
-
3
- /**
4
- * 401 Unauthorized
5
- *
6
- * The HTTP 401 Unauthorized client error response status code indicates that a
7
- * request was not successful because it lacks valid authentication credentials
8
- * for the requested resource. This status code is sent with an HTTP
9
- * WWW-Authenticate response header that contains information on the
10
- * authentication scheme the server expects the client to include to make the
11
- * request successfully.
12
- *
13
- * A 401 Unauthorized is similar to the 403 Forbidden response, except that a
14
- * 403 is returned when a request contains valid credentials, but the client
15
- * does not have permissions to perform a certain action.
16
- *
17
- * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/401
18
- */
19
- export default function unauthorized(body?: object | string): Response {
20
- return response(401, body ?? "Unauthorized");
21
- }
@@ -1,18 +0,0 @@
1
- import response from "./response";
2
-
3
- /**
4
- * 422 Unprocessable Content
5
- *
6
- * The HTTP 422 Unprocessable Content client error response status code
7
- * indicates that the server understood the content type of the request content,
8
- * and the syntax of the request content was correct, but it was unable to
9
- * process the contained instructions.
10
- *
11
- * Clients that receive a 422 response should expect that repeating the request
12
- * without modification will fail with the same error.
13
- *
14
- * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/422
15
- */
16
- export default function unprocessable(body?: object | string): Response {
17
- return response(422, body ?? "Unprocessable");
18
- }
package/src/response.ts DELETED
@@ -1,37 +0,0 @@
1
- import badRequest from "./response/badRequest";
2
- import created from "./response/created";
3
- import forbidden from "./response/forbidden";
4
- import found from "./response/found";
5
- import methodNotAllowed from "./response/methodNotAllowed";
6
- import movedPermanently from "./response/movedPermanently";
7
- import notFound from "./response/notFound";
8
- import notModified from "./response/notModified";
9
- import ok from "./response/ok";
10
- import permanentRedirect from "./response/permanentRedirect";
11
- import response from "./response/response";
12
- import seeOther from "./response/seeOther";
13
- import serverError from "./response/serverError";
14
- import temporaryRedirect from "./response/temporaryRedirect";
15
- import transfer from "./response/transfer";
16
- import unauthorized from "./response/unauthorized";
17
- import unprocessable from "./response/unprocessable";
18
-
19
- export {
20
- response,
21
- ok,
22
- created,
23
- transfer,
24
- movedPermanently,
25
- found,
26
- seeOther,
27
- notModified,
28
- temporaryRedirect,
29
- permanentRedirect,
30
- badRequest,
31
- unauthorized,
32
- forbidden,
33
- notFound,
34
- methodNotAllowed,
35
- unprocessable,
36
- serverError,
37
- };