Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option to modify auto formating #141

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"license": "MIT",
"exports": {
".": {
"types": "./dist/index.d.ts",
"require": "./dist/index.cjs",
"import": "./dist/index.mjs"
}
Expand Down Expand Up @@ -58,4 +59,4 @@
"vitest": "^0.32.2"
},
"packageManager": "[email protected]"
}
}
36 changes: 24 additions & 12 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ export interface IPXHResponse {

export interface MiddlewareOptions {
fallthrough?: boolean;
autoSupportedMimes?: string[];
}

async function _handleRequest(
request: IPXHRequest,
ipx: IPX
ipx: IPX,
options: Partial<MiddlewareOptions>
): Promise<IPXHResponse> {
const res: IPXHResponse = {
statusCode: 200,
Expand Down Expand Up @@ -71,7 +73,8 @@ async function _handleRequest(
const acceptHeader = request.headers?.accept || "";
const autoFormat = autoDetectFormat(
acceptHeader,
!!(modifiers.a || modifiers.animated)
!!(modifiers.a || modifiers.animated),
options.autoSupportedMimes
);
delete modifiers.f;
delete modifiers.format;
Expand Down Expand Up @@ -130,9 +133,10 @@ async function _handleRequest(

export function handleRequest(
request: IPXHRequest,
ipx: IPX
ipx: IPX,
options: Partial<MiddlewareOptions>
): Promise<IPXHResponse> {
return _handleRequest(request, ipx).catch((error) => {
return _handleRequest(request, ipx, options).catch((error) => {
const statusCode = Number.parseInt(error.statusCode) || 500;
// eslint-disable-next-line unicorn/prefer-logical-operator-over-ternary
const statusMessage = error.statusMessage
Expand Down Expand Up @@ -162,7 +166,8 @@ export function createIPXMiddleware(
) {
return handleRequest(
{ url: request.url || "/", headers: request.headers as any },
ipx
ipx,
options
).then((_res) => {
if (options.fallthrough && next && _res.error) {
return next(_res.error);
Expand All @@ -179,20 +184,27 @@ export function createIPXMiddleware(

// --- Utils ---

function autoDetectFormat(acceptHeader: string, animated: boolean) {
if (animated) {
const acceptMime = negotiate(acceptHeader, ["image/webp", "image/gif"]);
return acceptMime?.split("/")[1] || "gif";
}
const acceptMime = negotiate(acceptHeader, [
function autoDetectFormat(
acceptHeader: string,
animated: boolean,
supportedMimes: string[] = [
"image/avif",
"image/webp",
"image/jpeg",
"image/png",
"image/tiff",
"image/heif",
"image/gif",
]);
]
) {
if (animated) {
const acceptMime = negotiate(
acceptHeader,
supportedMimes.filter((val) => ["image/webp", "image/gif"].includes(val))
);
return acceptMime?.split("/")[1] || "gif";
}
const acceptMime = negotiate(acceptHeader, supportedMimes);
return acceptMime?.split("/")[1] || "jpeg";
}

Expand Down