/
index.js
74 lines (62 loc) · 2.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { HttpError, Redirect, ActionFailure } from '../runtime/control.js';
import { BROWSER, DEV } from 'esm-env';
// For some reason we need to type the params as well here,
// JSdoc doesn't seem to like @type with function overloads
/**
* @type {import('@sveltejs/kit').error}
* @param {number} status
* @param {any} message
*/
export function error(status, message) {
if ((!BROWSER || DEV) && (isNaN(status) || status < 400 || status > 599)) {
throw new Error(`HTTP error status codes must be between 400 and 599 — ${status} is invalid`);
}
return new HttpError(status, message);
}
/** @type {import('@sveltejs/kit').redirect} */
export function redirect(status, location) {
if ((!BROWSER || DEV) && (isNaN(status) || status < 300 || status > 308)) {
throw new Error('Invalid status code');
}
return new Redirect(status, location);
}
/** @type {import('@sveltejs/kit').json} */
export function json(data, init) {
// TODO deprecate this in favour of `Response.json` when it's
// more widely supported
const body = JSON.stringify(data);
// we can't just do `text(JSON.stringify(data), init)` because
// it will set a default `content-type` header. duplicated code
// means less duplicated work
const headers = new Headers(init?.headers);
if (!headers.has('content-length')) {
headers.set('content-length', encoder.encode(body).byteLength.toString());
}
if (!headers.has('content-type')) {
headers.set('content-type', 'application/json');
}
return new Response(body, {
...init,
headers
});
}
const encoder = new TextEncoder();
/** @type {import('@sveltejs/kit').text} */
export function text(body, init) {
const headers = new Headers(init?.headers);
if (!headers.has('content-length')) {
headers.set('content-length', encoder.encode(body).byteLength.toString());
}
return new Response(body, {
...init,
headers
});
}
/**
* Generates an `ActionFailure` object.
* @param {number} status
* @param {Record<string, any> | undefined} [data]
*/
export function fail(status, data) {
return new ActionFailure(status, data);
}