imgix-url-builder
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -1,173 +0,11 @@ | ||
const camelCaseToParamCase = (input) => { | ||
return input.replace(/[A-Z]/g, (match) => { | ||
return `-${match.toLowerCase()}`; | ||
}); | ||
import { Client } from "./Client.js"; | ||
import { buildURL } from "./buildURL.js"; | ||
import { buildWidthSrcSet } from "./buildWidthSrcSet.js"; | ||
import { buildPixelDensitySrcSet } from "./buildPixelDensitySrcSet.js"; | ||
export { | ||
Client, | ||
buildPixelDensitySrcSet, | ||
buildURL, | ||
buildWidthSrcSet | ||
}; | ||
const buildURL = (url, params) => { | ||
const instance = new URL(url); | ||
for (const camelCasedParamKey in params) { | ||
const paramKey = camelCaseToParamCase(camelCasedParamKey); | ||
const paramValue = params[camelCasedParamKey]; | ||
if (paramValue === void 0) { | ||
instance.searchParams.delete(paramKey); | ||
} else if (Array.isArray(paramValue)) { | ||
instance.searchParams.set(paramKey, paramValue.join(",")); | ||
} else { | ||
instance.searchParams.set(paramKey, `${paramValue}`); | ||
} | ||
} | ||
const s = instance.searchParams.get("s"); | ||
if (s) { | ||
instance.searchParams.delete("s"); | ||
instance.searchParams.append("s", s); | ||
} | ||
return instance.toString(); | ||
}; | ||
const buildPixelDensitySrcSet = (url, { pixelDensities, ...params }) => { | ||
return pixelDensities.map((dpr) => { | ||
return `${buildURL(url, { ...params, dpr })} ${dpr}x`; | ||
}).join(", "); | ||
}; | ||
const buildWidthSrcSet = (url, { widths, ...params }) => { | ||
return widths.map((width) => { | ||
return `${buildURL(url, { ...params, w: void 0, width })} ${width}w`; | ||
}).join(", "); | ||
}; | ||
class Client { | ||
/** | ||
* Creates a new `Client` instance for an Imgix domain. | ||
* | ||
* @param options - Options to instantiate a new client. | ||
* | ||
* @returns A `Client` instance for the given Imgix domain. | ||
*/ | ||
constructor(options) { | ||
this.baseURL = options.baseURL; | ||
} | ||
/** | ||
* Builds a URL to an Imgix image with Imgix URL API parameters for the | ||
* client's base URL. | ||
* | ||
* @example | ||
* | ||
* ```ts | ||
* const client = new Client({ baseURL: "https://example.imgix.net" }); | ||
* const url = client.buildURLForPath("/image.png", { width: 400 }); | ||
* // => https://example.imgix.net/image.png?width=400 | ||
* ``` | ||
* | ||
* @example | ||
* | ||
* ```ts | ||
* const client = new Client({ | ||
* baseURL: "https://example.imgix.net/folder", | ||
* }); | ||
* const url = client.buildURLForPath("./image.png", { width: 400 }); | ||
* // => https://example.imgix.net/folder/image.png?width=400 | ||
* ``` | ||
* | ||
* @param path - Path to the image relative to the client's base URL. | ||
* @param params - An object of Imgix URL API parameters. | ||
* | ||
* @returns The full absolute URL to the image with the given Imgix URL API | ||
* parameters applied. | ||
*/ | ||
buildURLForPath(path, params = {}) { | ||
return buildURL(`${new URL(path, this.baseURL)}`, params); | ||
} | ||
/** | ||
* Builds an `<img>` `srcset` attribute value for a given set of widths for | ||
* the client's base URL. It can also optinally apply Imgix URL API parameters | ||
* to the URLs. | ||
* | ||
* The `width` URL parameter will be applied for each `srcset` entry. If a | ||
* `width` or `w` parameter is provided to the `params` parameter, it will be | ||
* ignored. | ||
* | ||
* @example | ||
* | ||
* ```ts | ||
* const client = new Client({ baseURL: "https://example.imgix.net" }); | ||
* const srcset = client.buildWidthSrcSetForPath("/image.png", { | ||
* widths: [400, 800, 1600], | ||
* }); | ||
* // => https://example.imgix.net/image.png?width=400 400w, | ||
* // https://example.imgix.net/image.png?width=800 800w, | ||
* // https://example.imgix.net/image.png?width=1600 1600w | ||
* ``` | ||
* | ||
* @example | ||
* | ||
* ```ts | ||
* const client = new Client({ | ||
* baseURL: "https://example.imgix.net", | ||
* }); | ||
* const srcset = client.buildWidthSrcSetForPath("/image.png", { | ||
* widths: [400, 800, 1600], | ||
* sat: -100, | ||
* }); | ||
* // => https://example.imgix.net/image.png?width=400&sat=-100 400w, | ||
* // https://example.imgix.net/image.png?width=800&sat=-100 800w, | ||
* // https://example.imgix.net/image.png?width=1600&sat=-100 1600w | ||
* ``` | ||
* | ||
* @param path - Path to the image relative to the client's base URL. | ||
* @param params - An object of Imgix URL API parameters. The `widths` | ||
* parameter defines the resulting `srcset` widths. | ||
* | ||
* @returns A `srcset` attribute value for `url` with the given Imgix URL API | ||
* parameters applied. | ||
*/ | ||
buildWidthSrcSetForPath(path, params) { | ||
return buildWidthSrcSet(`${new URL(path, this.baseURL)}`, params); | ||
} | ||
/** | ||
* Builds an `<img>` `srcset` attribute value for a given set of pixel | ||
* densities for the client's base URL. It can also optinally apply Imgix URL | ||
* API parameters to the URLs. | ||
* | ||
* The `dpr` URL parameter will be applied for each `srcset` entry. If a `dpr` | ||
* parameter is provided to the `params` parameter, it will be ignored. | ||
* | ||
* @example | ||
* | ||
* ```ts | ||
* const client = new Client({ baseURL: "https://example.imgix.net" }); | ||
* const srcset = client.buildPixelDensitySrcSetForPath("/image.png", { | ||
* pixelDensities: [1, 2, 3], | ||
* }); | ||
* // => https://example.imgix.net/image.png?dpr=1 1x, | ||
* // https://example.imgix.net/image.png?dpr=2 2x, | ||
* // https://example.imgix.net/image.png?dpr=3 3x | ||
* ``` | ||
* | ||
* @example | ||
* | ||
* ```ts | ||
* const client = new Client({ baseURL: "https://example.imgix.net" }); | ||
* const srcset = client.buildPixelDensitySrcSetForPath("/image.png", { | ||
* pixelDensities: [1, 2, 3], | ||
* sat: -100, | ||
* }); | ||
* // => https://example.imgix.net/image.png?dpr=1&sat=-100 1x, | ||
* // https://example.imgix.net/image.png?dpr=2&sat=-100 2x, | ||
* // https://example.imgix.net/image.png?dpr=3&sat=-100 3x | ||
* ``` | ||
* | ||
* @param path - Path to the image relative to the client's base URL. | ||
* @param params - An object of Imgix URL API parameters. The `pixelDensities` | ||
* parameter defines the resulting `srcset` widths. | ||
* | ||
* @returns A `srcset` attribute value for `url` with the given Imgix URL API | ||
* parameters applied. | ||
*/ | ||
buildPixelDensitySrcSetForPath(path, params) { | ||
return buildPixelDensitySrcSet(`${new URL(path, this.baseURL)}`, params); | ||
} | ||
} | ||
export { Client, buildPixelDensitySrcSet, buildURL, buildWidthSrcSet }; | ||
//# sourceMappingURL=index.mjs.map | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "imgix-url-builder", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "JavaScript/TypeScript Imgix URL builders for browsers and Node.js", | ||
@@ -19,15 +19,37 @@ "keywords": [ | ||
".": { | ||
"require": "./dist/index.cjs", | ||
"import": "./dist/index.mjs" | ||
"require": { | ||
"types": "./dist/index.d.ts", | ||
"default": "./dist/index.cjs" | ||
}, | ||
"import": { | ||
"types": "./dist/index.d.ts", | ||
"default": "./dist/index.js" | ||
} | ||
}, | ||
"./node": { | ||
"require": "./dist/node/index.cjs", | ||
"import": "./dist/node/index.mjs" | ||
"require": { | ||
"types": "./dist/node/index.d.ts", | ||
"default": "./dist/node.cjs" | ||
}, | ||
"import": { | ||
"types": "./dist/node/index.d.ts", | ||
"default": "./dist/node.js" | ||
} | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
"main": "dist/index.cjs", | ||
"module": "dist/index.mjs", | ||
"react-native": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"main": "./dist/index.cjs", | ||
"module": "./dist/index.js", | ||
"react-native": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"typesVersions": { | ||
"*": { | ||
"*": [ | ||
"dist/index.d.ts" | ||
], | ||
"node": [ | ||
"dist/node/index.d.ts" | ||
] | ||
} | ||
}, | ||
"files": [ | ||
@@ -38,7 +60,6 @@ "dist", | ||
"scripts": { | ||
"build": "siroc build", | ||
"dev": "siroc build --watch", | ||
"build": "vite build", | ||
"dev": "vite build --watch", | ||
"format": "prettier --write .", | ||
"gen": "node --loader ts-node/esm --experimental-json-modules ./scripts/generateImgixURLParamsTypeFile.ts", | ||
"lint": "eslint --ext .js,.ts .", | ||
"gen": "npx tsx ./scripts/generateImgixURLParamsTypeFile.ts", | ||
"prepare": "npm run build", | ||
@@ -49,27 +70,30 @@ "release": "npm run test && standard-version && git push --follow-tags && npm run build && npm publish", | ||
"release:dry": "standard-version --dry-run", | ||
"lint": "eslint --ext .js,.ts .", | ||
"types": "tsc --noEmit", | ||
"unit": "nyc --reporter=lcovonly --reporter=text --exclude-after-remap=false ava", | ||
"size": "size-limit", | ||
"test": "npm run lint && npm run unit && npm run build && npm run size", | ||
"unit": "nyc --reporter=lcovonly --reporter=text --exclude-after-remap=false ava" | ||
"test": "npm run lint && npm run types && npm run unit && npm run build && npm run size" | ||
}, | ||
"devDependencies": { | ||
"@size-limit/preset-small-lib": "^8.2.4", | ||
"@types/prettier": "^2.7.2", | ||
"@typescript-eslint/eslint-plugin": "^5.59.7", | ||
"@typescript-eslint/parser": "^5.59.7", | ||
"ava": "^5.3.0", | ||
"eslint": "^8.41.0", | ||
"eslint-config-prettier": "^8.8.0", | ||
"eslint-plugin-prettier": "^4.2.1", | ||
"eslint-plugin-tsdoc": "^0.2.17", | ||
"imgix-url-params": "^11.15.0", | ||
"nyc": "^15.1.0", | ||
"prettier": "^2.8.8", | ||
"prettier-plugin-jsdoc": "^0.4.2", | ||
"siroc": "^0.16.0", | ||
"size-limit": "^8.2.4", | ||
"@size-limit/preset-small-lib": "^11.1.4", | ||
"@types/prettier": "^3.0.0", | ||
"@typescript-eslint/eslint-plugin": "^6.21.0", | ||
"@typescript-eslint/parser": "^6.21.0", | ||
"ava": "^6.1.3", | ||
"eslint": "^8.57.0", | ||
"eslint-config-prettier": "^9.1.0", | ||
"eslint-plugin-prettier": "^5.2.1", | ||
"eslint-plugin-tsdoc": "^0.3.0", | ||
"imgix-url-params": "^11.23.0", | ||
"nyc": "^17.0.0", | ||
"prettier": "^3.3.3", | ||
"prettier-plugin-jsdoc": "^1.3.0", | ||
"size-limit": "^11.1.4", | ||
"standard-version": "^9.5.0", | ||
"ts-eager": "^2.0.2", | ||
"ts-morph": "^18.0.0", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^5.0.4" | ||
"ts-morph": "^23.0.0", | ||
"tsx": "^4.16.2", | ||
"typescript": "^5.5.3", | ||
"vite": "^5.3.4", | ||
"vite-plugin-sdk": "^0.1.2" | ||
}, | ||
@@ -76,0 +100,0 @@ "engines": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
71
429532
21
10550
1