@torpor/build 0.4.9 → 0.4.10
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 +7 -3
- package/CHANGELOG.md +0 -473
- package/scripts/postbuild.ts +0 -12
- package/src/bin/index.ts +0 -11
- package/src/index.ts +0 -30
- package/src/nav/formSubmit.ts +0 -56
- package/src/nav/load.ts +0 -9
- package/src/nav/loadData.ts +0 -134
- package/src/nav/navigate.ts +0 -164
- package/src/nav/reload.ts +0 -9
- package/src/nav.ts +0 -4
- package/src/response/badRequest.ts +0 -19
- package/src/response/created.ts +0 -20
- package/src/response/forbidden.ts +0 -21
- package/src/response/found.ts +0 -30
- package/src/response/methodNotAllowed.ts +0 -18
- package/src/response/movedPermanently.ts +0 -25
- package/src/response/notFound.ts +0 -24
- package/src/response/notModified.ts +0 -34
- package/src/response/ok.ts +0 -29
- package/src/response/permanentRedirect.ts +0 -30
- package/src/response/response.ts +0 -22
- package/src/response/seeOther.ts +0 -19
- package/src/response/serverError.ts +0 -24
- package/src/response/temporaryRedirect.ts +0 -31
- package/src/response/transfer.ts +0 -14
- package/src/response/unauthorized.ts +0 -21
- package/src/response/unprocessable.ts +0 -18
- package/src/response.ts +0 -37
- package/src/run/prepareTemplate.ts +0 -39
- package/src/run/run.ts +0 -62
- package/src/run/runBuild.ts +0 -77
- package/src/run/runDev.ts +0 -89
- package/src/run/runPreview.ts +0 -93
- package/src/run.ts +0 -6
- package/src/server/CookieHelper.ts +0 -36
- package/src/server/HeaderHelper.ts +0 -25
- package/src/server/Routerx.ts +0 -72
- package/src/server/Server.ts +0 -144
- package/src/server/ServerEvent.ts +0 -39
- package/src/server/connect/bufferToArrayBuffer.ts +0 -8
- package/src/server/connect/connectMiddleware.ts +0 -81
- package/src/server/connect/flattenHeaders.ts +0 -44
- package/src/server/connect/nodeMessageToNodeResponse.ts +0 -89
- package/src/server/connect/readableToBuffer.ts +0 -16
- package/src/server/connect/requestToNodeMessage.ts +0 -26
- package/src/server/contentType.ts +0 -85
- package/src/server/types/HttpMethod.ts +0 -12
- package/src/server/types/MiddlewareFunction.ts +0 -8
- package/src/server/types/ServerFunction.ts +0 -7
- package/src/server.ts +0 -7
- package/src/site/Router.ts +0 -164
- package/src/site/Site.ts +0 -214
- package/src/site/clientEntry.ts +0 -108
- package/src/site/clientEntryDev.ts +0 -3
- package/src/site/defaultAdapter.ts +0 -11
- package/src/site/manifest.ts +0 -54
- package/src/site/serverEntry.ts +0 -417
- package/src/state/$page.ts +0 -24
- package/src/state/client.ts +0 -15
- package/src/state.ts +0 -3
- package/src/test/runTest.ts +0 -350
- package/src/test.ts +0 -3
- package/src/types/Adapter.ts +0 -8
- package/src/types/ClientState.ts +0 -8
- package/src/types/InternalState.ts +0 -7
- package/src/types/LayoutHandler.ts +0 -5
- package/src/types/LayoutPath.ts +0 -10
- package/src/types/ManifestRoute.ts +0 -6
- package/src/types/PageEndPoint.ts +0 -39
- package/src/types/PageLoadEvent.ts +0 -14
- package/src/types/PageServerAction.ts +0 -7
- package/src/types/PageServerEndPoint.ts +0 -16
- package/src/types/PageServerLoad.ts +0 -7
- package/src/types/PageState.ts +0 -8
- package/src/types/Route.ts +0 -11
- package/src/types/RouteHandler.ts +0 -16
- package/src/types/RouteLayoutHandler.ts +0 -5
- package/src/types/RouteMatchResult.ts +0 -7
- package/src/types/RouteType.ts +0 -19
- package/src/types/ServerEndPoint.ts +0 -37
- package/src/types/ServerHook.ts +0 -11
- package/src/types/ServerLoadEvent.ts +0 -37
- package/src/types/ServerRequest.ts +0 -7
- package/src/utils/pathToRegex.ts +0 -15
- package/src/vite-env.d.ts +0 -2
- package/test/api/_hook.server.ts +0 -3
- package/test/api/posts/+page.server.ts +0 -3
- package/test/api/posts/[id]/+page.server.ts +0 -3
- package/test/app.test.ts +0 -47
- package/test/router.test.ts +0 -78
- package/test/routes/+page.server.ts +0 -3
- package/test/routes/+page.ts +0 -3
- package/test/routes/_error.ts +0 -3
- package/test/routes/_hook.server.ts +0 -3
- package/test/routes/_layout.server.ts +0 -3
- package/test/routes/_layout.ts +0 -3
- package/test/routes/posts/+page.ts +0 -3
- package/test/routes/posts/[id]/+page.ts +0 -3
- package/test/site.test.ts +0 -40
- package/tsconfig.json +0 -9
- package/tsdown.config.ts +0 -18
- package/vitest.config.ts +0 -3
package/src/nav/loadData.ts
DELETED
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
import client from "../state/client";
|
|
2
|
-
import type LayoutPath from "../types/LayoutPath";
|
|
3
|
-
import type PageEndPoint from "../types/PageEndPoint";
|
|
4
|
-
import type PageServerEndPoint from "../types/PageServerEndPoint";
|
|
5
|
-
import type RouteHandler from "../types/RouteHandler";
|
|
6
|
-
|
|
7
|
-
export default async function loadData(
|
|
8
|
-
handler: RouteHandler,
|
|
9
|
-
params: Record<string, string>,
|
|
10
|
-
path: string,
|
|
11
|
-
query: URLSearchParams,
|
|
12
|
-
newLayoutStack: LayoutPath[],
|
|
13
|
-
clientEndPoint: PageEndPoint | undefined,
|
|
14
|
-
serverEndPoint: PageServerEndPoint | undefined,
|
|
15
|
-
prefetch = false,
|
|
16
|
-
): Promise<Record<string, string> | void> {
|
|
17
|
-
let data = {};
|
|
18
|
-
if (handler.layouts) {
|
|
19
|
-
let layoutStack = client.layoutStack;
|
|
20
|
-
for (let [i, layout] of handler.layouts.entries()) {
|
|
21
|
-
let layoutPath = layout.path;
|
|
22
|
-
for (let key in params) {
|
|
23
|
-
layoutPath = layoutPath.replace(`[${key}]`, params[key]);
|
|
24
|
-
}
|
|
25
|
-
if (layoutStack.at(i)?.path === layoutPath) {
|
|
26
|
-
// We've already loaded this layout, we can just re-use its data and UI
|
|
27
|
-
if (!prefetch) {
|
|
28
|
-
layoutStack[i].reuse = true;
|
|
29
|
-
}
|
|
30
|
-
newLayoutStack[i] = layoutStack[i];
|
|
31
|
-
Object.assign(data, layoutStack[i].data);
|
|
32
|
-
} else {
|
|
33
|
-
const stackLayout = { path: layoutPath, data: {}, reuse: false, slotRegion: null };
|
|
34
|
-
const layoutEndPoint: PageEndPoint | undefined = (await layout.endPoint())?.default;
|
|
35
|
-
const layoutServerEndPoint: PageServerEndPoint | undefined =
|
|
36
|
-
layout.serverEndPoint && (await layout.serverEndPoint())?.default;
|
|
37
|
-
const layoutResponse = await loadClientAndServerData(
|
|
38
|
-
stackLayout.data,
|
|
39
|
-
document.location.origin + layoutPath,
|
|
40
|
-
query,
|
|
41
|
-
params,
|
|
42
|
-
layoutEndPoint,
|
|
43
|
-
layoutServerEndPoint,
|
|
44
|
-
);
|
|
45
|
-
if (layoutResponse?.ok === false) {
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
Object.assign(data, stackLayout.data);
|
|
49
|
-
newLayoutStack.push(stackLayout);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
let endPointResponse = await loadClientAndServerData(
|
|
54
|
-
data,
|
|
55
|
-
document.location.origin + path,
|
|
56
|
-
query,
|
|
57
|
-
params,
|
|
58
|
-
clientEndPoint,
|
|
59
|
-
serverEndPoint,
|
|
60
|
-
);
|
|
61
|
-
if (endPointResponse?.ok === false) {
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
return data;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
async function loadClientAndServerData(
|
|
68
|
-
data: Record<string, any>,
|
|
69
|
-
location: string,
|
|
70
|
-
query: URLSearchParams,
|
|
71
|
-
params: Record<string, string>,
|
|
72
|
-
clientEndPoint?: PageEndPoint,
|
|
73
|
-
serverEndPoint?: PageServerEndPoint,
|
|
74
|
-
): Promise<Response | undefined | void> {
|
|
75
|
-
let prefetchedData = client.prefetchedData;
|
|
76
|
-
|
|
77
|
-
if (clientEndPoint?.load) {
|
|
78
|
-
const clientKey = location + (query.size > 0 ? `?${query}` : "");
|
|
79
|
-
if (prefetchedData[clientKey]) {
|
|
80
|
-
Object.assign(data, prefetchedData[clientKey]);
|
|
81
|
-
} else {
|
|
82
|
-
const clientUrl = new URL(document.location.href);
|
|
83
|
-
for (let [name, value] of query) {
|
|
84
|
-
clientUrl.searchParams.append(name, value);
|
|
85
|
-
}
|
|
86
|
-
const clientParams = buildClientParams(clientUrl, params, data);
|
|
87
|
-
const clientResponse = await clientEndPoint.load(clientParams);
|
|
88
|
-
if (clientResponse) {
|
|
89
|
-
if (clientResponse.ok) {
|
|
90
|
-
if (clientResponse.headers.get("Content-Type")?.includes("application/json")) {
|
|
91
|
-
const clientData = await clientResponse.json();
|
|
92
|
-
Object.assign(data, clientData);
|
|
93
|
-
prefetchedData[clientKey] = clientData;
|
|
94
|
-
}
|
|
95
|
-
} else {
|
|
96
|
-
return clientResponse;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
if (serverEndPoint?.load) {
|
|
103
|
-
const serverLocation = location.replace(/\/$/, "") + "/~server";
|
|
104
|
-
const serverKey = serverLocation + (query.size > 0 ? `?${query}` : "");
|
|
105
|
-
if (prefetchedData[serverKey]) {
|
|
106
|
-
Object.assign(data, prefetchedData[serverKey]);
|
|
107
|
-
} else {
|
|
108
|
-
const serverUrl = new URL(serverLocation);
|
|
109
|
-
for (let [name, value] of query) {
|
|
110
|
-
serverUrl.searchParams.append(name, value);
|
|
111
|
-
}
|
|
112
|
-
const serverResponse = await fetch(serverUrl);
|
|
113
|
-
if (serverResponse) {
|
|
114
|
-
if (serverResponse.ok) {
|
|
115
|
-
if (serverResponse.headers.get("Content-Type")?.includes("application/json")) {
|
|
116
|
-
const serverData = await serverResponse.json();
|
|
117
|
-
Object.assign(data, serverData);
|
|
118
|
-
prefetchedData[serverKey] = serverData;
|
|
119
|
-
}
|
|
120
|
-
} else {
|
|
121
|
-
return serverResponse;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
function buildClientParams(url: URL, params: Record<string, string>, data: Record<string, any>) {
|
|
129
|
-
return {
|
|
130
|
-
url,
|
|
131
|
-
params,
|
|
132
|
-
data,
|
|
133
|
-
};
|
|
134
|
-
}
|
package/src/nav/navigate.ts
DELETED
|
@@ -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
package/src/nav.ts
DELETED
|
@@ -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
|
-
}
|
package/src/response/created.ts
DELETED
|
@@ -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
|
-
}
|
package/src/response/found.ts
DELETED
|
@@ -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
|
-
}
|
package/src/response/notFound.ts
DELETED
|
@@ -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
|
-
}
|
package/src/response/ok.ts
DELETED
|
@@ -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
|
-
}
|
package/src/response/response.ts
DELETED
|
@@ -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
|
-
}
|
package/src/response/seeOther.ts
DELETED
|
@@ -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
|
-
}
|