Skip to content

Commit

Permalink
feat: add follow option for server-side redirect handling (#9)
Browse files Browse the repository at this point in the history
- added a new options token `follow` 
- `follow` instructs the server to follow redirects (for whitelisted
domains) when `url` points to an external URL
  • Loading branch information
tex-murphy authored Aug 14, 2024
1 parent c8c65b0 commit df3dce7
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
19 changes: 18 additions & 1 deletion src/__test__/image-options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@ import { generateOptionsString, parseOptionsString } from "../image-options";
import { VisionaryImageOptions } from "../types/visionary.types";

describe(parseOptionsString.name, () => {
test("options defaults", () => {
const parsedOptions = parseOptionsString("");

expect(parsedOptions.debug).toBeFalsy();
expect(parsedOptions.download).toBeFalsy();
expect(parsedOptions.follow).toBeFalsy();
});

test("parses an options string with size and format specified", () => {
const optionsString = "xs,f_auto";
const optionsString = "auto,xs";

const parsedOptions = parseOptionsString(optionsString);

expect(parsedOptions.size).toBe(ImageSizeToken.xs);
expect(parsedOptions.format).toBe(ImageFormatToken.AUTO);
});

test("tests a size token (lg)", () => {
Expand Down Expand Up @@ -39,6 +48,14 @@ describe(parseOptionsString.name, () => {
expect(parsedOptions.size).toBe(ImageSizeToken["4k"]);
});

test("parses an options string with follow set", () => {
const optionsString = "4k,follow";

const parsedOptions = parseOptionsString(optionsString);

expect(parsedOptions.follow).toBe(true);
});

test("parses an options string with format set", () => {
const optionsString = "avif,lg";

Expand Down
1 change: 1 addition & 0 deletions src/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ export enum ImageSizeToken {
export enum UrlOptionToken {
DEBUG = "debug",
DOWNLOAD = "download",
FOLLOW = "follow",
}
13 changes: 12 additions & 1 deletion src/image-options.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { ImageFormatToken, ImageSizeToken, UrlOptionToken } from "./enum";
import { isDebugToken, isDownloadToken, isImageFormatToken, isImageSizeToken } from "./token";
import {
isDebugToken,
isDownloadToken,
isFollowToken,
isImageFormatToken,
isImageSizeToken,
} from "./token";
import { VisionaryImageOptions } from "./types/visionary.types";

export const parseOptionTokens = (optionTokens: string[] = []): VisionaryImageOptions => {
Expand All @@ -11,6 +17,8 @@ export const parseOptionTokens = (optionTokens: string[] = []): VisionaryImageOp
returnOptions.debug = true;
} else if (isDownloadToken(token)) {
returnOptions.download = true;
} else if (isFollowToken(token)) {
returnOptions.follow = true;
} else if (isImageFormatToken(token)) {
returnOptions.format = token;
}
Expand All @@ -29,6 +37,9 @@ export const generateOptionsString = (options: VisionaryImageOptions): string |
if (options.download) {
tokenArr.push(UrlOptionToken.DOWNLOAD);
}
if (options.follow) {
tokenArr.push(UrlOptionToken.FOLLOW);
}
if (options.format) {
if (options.format !== ImageFormatToken.AUTO) {
tokenArr.push(options.format);
Expand Down
2 changes: 2 additions & 0 deletions src/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export const isDebugToken = (token: string) => token === UrlOptionToken.DEBUG;

export const isDownloadToken = (token: string) => token === UrlOptionToken.DOWNLOAD;

export const isFollowToken = (token: string) => token === UrlOptionToken.FOLLOW;

export const isImageFormatToken = (format: string): format is ImageFormatToken =>
Object.values(ImageFormatToken).includes(format as ImageFormatToken);

Expand Down
1 change: 1 addition & 0 deletions src/types/visionary.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export interface VisionaryImageOptions {
download?: boolean;
/** Specifies a custom endpoint when `generateVisionaryUrl()` is used */
endpoint?: string;
follow?: boolean;
format?: ImageFormatToken;
size?: ImageSizeToken;
}
Expand Down

0 comments on commit df3dce7

Please sign in to comment.