Skip to content

Commit

Permalink
[v.1.3.0] rename fileId prop to url; clean up unit tests (#7)
Browse files Browse the repository at this point in the history
- rename `isBase64UrlFormatted()` to `isBase64UrlEncoded()` for clarity
- rename `fileId` prop to `url` for clarity
- tidy up tests and comments
- minor version bump of this package -> v.1.30
- minor version bumps to dev dependencies (`typescript`, `vite`,
`vite-plugin-dts`, `vitest`)
  • Loading branch information
tex-murphy authored Jun 25, 2024
1 parent f830910 commit c356671
Show file tree
Hide file tree
Showing 13 changed files with 452 additions and 403 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ npm-debug.log*

node_modules
dist
bin
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
!.vscode/settings.json
.idea
.DS_Store
*.suo
Expand Down
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
46 changes: 18 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
**Visionary URL** is a lightweight Typescript library for generating image URLs with built-in Blurhash placeholders.

![NPM version](https://img.shields.io/npm/v/visionary-url?style=flat-square&color=528987) ![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/visionary-cloud/visionary-url/.github%2Fworkflows%2Fci-cd-workflow.yml?branch=master&style=flat-square)
![NPM bundle size](https://img.shields.io/bundlephobia/min/visionary-url?style=flat-square&color=blue)


![NPM bundle size](https://img.shields.io/bundlephobia/min/visionary-url?style=flat-square&color=blue)

## Getting started

Expand All @@ -16,65 +14,57 @@ npm install --save visionary-url
### Generating a Visionary URL

```javascript
import { generateVisionaryUrl } from 'visionary-url';
import { generateVisionaryUrl } from "visionary-url";

const url = generateVisionaryUrl({
blurhash: "LCDJYN9FxG_M_N%L%M%M4o~ptRIA", // blurhash string
blurhashX: 4, // blurhash x components
blurhashY: 4, // blurhash y components
bcc: "#baccae", // background color code
fileId: "image:101", // image ID or URL
sourceHeight: 1200, // image height
sourceWidth: 1600, // image width
url: "https://example.cdn/media/gato.jpg", // image URL or file ID
});
```



## How it works

A Visionary URL is formatted using 3 or 4 URL segments:
| | base path | | visionary code | | options (optional) | | filename |
| | base path | | visionary code | | options (optional) | | filename |
| -- | -- | -- | -- | -- | -- | -- | -- |
| `/` | `image` | `/` | `<visionary code, base64url>` | `/` |`<option tokens>` | `/` | `image.jpg` |

### Example URLs

```
https://examplecdn.cloud/image/aW1hZ2U6MTAwMDEhODAwITYwMA/image.jpg
https://examplecdn.cloud/image/aW1hZ2U6MTAwMDEhODAwITYwMA/4k/image.jpg
https://example.cdn/image/aW1hZ2U6MTAwMDEhODAwITYwMA/image.jpg
https://example.cdn/image/aW1hZ2U6MTAwMDEhODAwITYwMA/4k/image.jpg
```

### Visionary code

The **Visionary code** is a base64url'd, exclamation-point separated list of image placeholder data, detailed as the following:


| Attribute | Description |
| -- | -- |
| Image ID | Image URL or internal image ID (required) |
| Image width | Used to compute aspect ratio of image placeholder (required) |
| Image height | Used to compute aspect ratio of image placeholder (required) |
| Background color code | Base layer background color code (e.g. `#BACCAE`) |
| Blurhash code | Blurhash string of the image |
| Blurhash x components | Number of x components the blurhash code represents |
| Blurhash y components | Number of y components the blurhash code represents |

| Attribute | Description |
| --------------------- | ------------------------------------------------------------ |
| Image URL / ID | Image URL or Visionary file ID (required) |
| Image width | Used to compute aspect ratio of image placeholder (required) |
| Image height | Used to compute aspect ratio of image placeholder (required) |
| Background color code | Base layer background color code (e.g. `#BACCAE`) |
| Blurhash code | Blurhash string of the image |
| Blurhash x components | Number of x components the blurhash code represents |
| Blurhash y components | Number of y components the blurhash code represents |

### Image options

Image options are optional and are provided as comma separated tokens.


#### Size token

One of the following tokens may be included in the options string to specify an image size: `xs`, `sm`, `md`, `lg`, `xl`, `xxl`, `4k`, `5k`

#### Download token

| token | description |
| -- | --- |
| `download` | Indication for server to return `content-disposition: attachment;` in response headers |



| token | description |
| ---------- | -------------------------------------------------------------------------------------- |
| `download` | Indication for server to return `content-disposition: attachment;` in response headers |
2 changes: 1 addition & 1 deletion lib/visionary-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export { parseOptionsString } from "../src/image-options";
export * from "../src/token";
export {
formatToContentType,
isBase64UrlFormatted,
isBase64UrlEncoded,
suggestedBlurhashComponentDimensions,
} from "../src/util";
export type {
Expand Down
Loading

0 comments on commit c356671

Please sign in to comment.