@288-toolkit/http

4.1.1 • Public • Published

Http

A collection of functions to work with http.

cacheHeaders

Generates a cache-control header object from the provided options.

If enabled: false, the cache-control header will be 'private, no-cache, noindex, max-age=0, must-revalidate'.

type CacheHeadersOptions = {
	// Disables the cache entirely
	enabled?: boolean;
	// All cache-control directives
	maxAge?: number;
	sMaxAge?: number;
	staleWhileRevalidate?: number;
	staleIfError?: number;
	private?: boolean;
	public?: boolean;
	noCache?: boolean;
	noStore?: boolean;
	noTransform?: boolean;
	immutable?: boolean;
	mustRevalidate?: boolean;
	proxyRevalidate?: boolean;
	mustUnderstand?: boolean;
};

const cacheControl = cacheHeaders({
	// ...
});

return new Response('', {
	headers: cacheControl
});

isRedirectResponse

Returns true if the response is a redirect response.

const isRedirectResponse: (response: Response) => boolean;

getLangFromRequest

Get the language from the request.

const getLangFromRequest = <Language extends string>(
	request: Request,
	{ supportedLanguages: Language[]; defaultLanguage: Language }
) => Language;

parseAcceptLanguage

Given a string of the form "en,fr;q=0.9,de;q=0.8", returns an array of AcceptLanguageEntry ordered by priority. This is mostly used to parse the 'Accept-Language' header.

type AcceptLanguageEntry = {
	lang: string;
	priority: number;
};

const parseAcceptLanguage = (acceptLanguage: string): Maybe<AcceptLanguageEntry[]>

getVercelIpHeaders

Get Vercel headers from a request.

const getVercelIpHeaders: (request: Request) => {
	country: Maybe<string>;
	region: Maybe<string>;
	city: Maybe<string>;
	latitude: Maybe<string>;
	longitude: Maybe<string>;
	timezone: Maybe<string>;
	ip: Maybe<string>;
};

Readme

Keywords

none

Package Sidebar

Install

npm i @288-toolkit/http

Weekly Downloads

313

Version

4.1.1

License

MIT

Unpacked Size

10.4 kB

Total Files

14

Last publish

Collaborators

  • nitriques
  • f-elix