@shgysk8zer0/http
A JavaScript library that provides various utilities for working with HTTP
ok
for 200.HTTPError
class that inherits from Error.File
object (derived from Blob
) and URL.canParse()
for URL validation.@shgysk8zer0/consts
).openLink()
function compatible with various JavaScript environments.Cookie
class for working with HTTP cookies, enabling easy cookie creation and management.[!WARNING] Parsing of form data uses regex which has been reported as vulnerable to ReDoS attacks.
parseMultipartFormData()
is deprecated and will be removed in an upcoming release. Instead, in node > 18 you can usenew Request(body, { headers }).formData()
.
npm i @shgysk8zer0/http
import { HTTPError } from 'shgysk8zer0/http@shgysk8zer0/http/error.js';
import { NOT_IMPLEMENTED, INTERNAL_SERVER_ERROR } from 'shgysk8zer0/http@shgysk8zer0/http/status.js';
import { JSON } from 'shgysk8zer0/http@shgysk8zer0/http/types.js';
import { Cookie } from 'shgysk8zer0/http@shgysk8zer0/http/cookie.js';
import
sThis package is available on unpkg.com as a collection of modules, making it easily accessible for browser-based projects. It is designed to be versatile and is not limited to a specific Node.js environment, ensuring compatibility across various platforms.
import { HTTPError } from 'https://unpkg.com/@shgysk8zer0/http/error.js';
import { NOT_IMPLEMENTED, INTERNAL_SERVER_ERROR } from 'https://unpkg.com/@shgysk8zer0/http/status.js';
import { JSON } from 'https://unpkg.com/@shgysk8zer0/http/types.js';
import { Cookie } from 'https://unpkg.com/@shgysk8zer0/http/cookie.js';
export async function handler() {
try {
const error = new HTTPError('Not implemented.', {
status: NOT_IMPLEMENTED,
cause: new Error('I have not done this yet...'),
});
throw err;
} catch (err) {
if (err instanceof HTTPError) { // Error has an HTTP status & message for use by client
return Response.json(error, {
status: error.status,
headers: new Headers({
'Content-Type': JSON,
'Set-Cookie': new Cookie('uid', crypto.randomUUID(), {
domain: 'example.com',
path: '/foo',
maxAge: 86_400_000,
sameSite: 'Strict',
httpOnly: true,
partitioned: true,
})
}),
});
} else { // It is not an HTTPError and may contain sensitive into
return Response.json({
error: {
messsage: 'Something broke :(',
status: INTERNAL_SERVER_ERROR,
}
}, { status: INTERNAL_SERVER_ERROR });
}
}
}