@arraypress/media-url 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +96 -0
- package/package.json +25 -6
package/README.md
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# @arraypress/media-url
|
|
2
|
+
|
|
3
|
+
Build media URLs with optional Cloudflare Image Resizing transforms. Three functions, zero dependencies.
|
|
4
|
+
|
|
5
|
+
Supports three modes: proxy fallback, direct URLs, and Cloudflare Image Resizing — configured with a single options object.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @arraypress/media-url
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
import { mediaUrl, buildImageUrls, buildAudioUrl } from '@arraypress/media-url';
|
|
17
|
+
|
|
18
|
+
// Proxy fallback (no base URL configured)
|
|
19
|
+
mediaUrl({ id: 42, fileKey: 'media/photo.jpg' });
|
|
20
|
+
// → '/api/media/42'
|
|
21
|
+
|
|
22
|
+
// Direct URL
|
|
23
|
+
mediaUrl(
|
|
24
|
+
{ id: 42, fileKey: 'media/photo.jpg' },
|
|
25
|
+
{ baseUrl: 'https://media.example.com' }
|
|
26
|
+
);
|
|
27
|
+
// → 'https://media.example.com/media/photo.jpg'
|
|
28
|
+
|
|
29
|
+
// With Cloudflare Image Resizing
|
|
30
|
+
mediaUrl(
|
|
31
|
+
{ id: 42, fileKey: 'media/photo.jpg', transform: { width: 400 } },
|
|
32
|
+
{ baseUrl: 'https://media.example.com', resizing: true }
|
|
33
|
+
);
|
|
34
|
+
// → '/cdn-cgi/image/w=400,q=80,f=auto,fit=cover/https://media.example.com/media/photo.jpg'
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## API
|
|
38
|
+
|
|
39
|
+
### `mediaUrl(options, config?)`
|
|
40
|
+
|
|
41
|
+
Build a public URL for a single media item.
|
|
42
|
+
|
|
43
|
+
**Options:**
|
|
44
|
+
- `id` — Media ID (used for proxy fallback).
|
|
45
|
+
- `fileKey` — Storage object key (e.g. `"media/photo.jpg"`).
|
|
46
|
+
- `transform` — Optional image transform with `width`, `height`, `quality` (default 80), `fit` (default `"cover"`), and `format` (default `"auto"`).
|
|
47
|
+
|
|
48
|
+
**Config:**
|
|
49
|
+
- `baseUrl` — Public base URL (e.g. `"https://media.example.com"`). When empty, falls back to proxy path.
|
|
50
|
+
- `resizing` — Enable Cloudflare Image Resizing (default `false`).
|
|
51
|
+
- `proxyPath` — Proxy fallback prefix (default `"/api/media"`).
|
|
52
|
+
|
|
53
|
+
### `buildImageUrls(images, config?, sizes?)`
|
|
54
|
+
|
|
55
|
+
Build URLs for a list of images with automatic thumbnail generation.
|
|
56
|
+
|
|
57
|
+
```js
|
|
58
|
+
const images = [
|
|
59
|
+
{ id: 1, file_key: 'media/photo.jpg', alt_text: 'A photo' },
|
|
60
|
+
{ id: 2, file_key: 'media/banner.jpg' },
|
|
61
|
+
];
|
|
62
|
+
|
|
63
|
+
const result = buildImageUrls(images, {
|
|
64
|
+
baseUrl: 'https://media.example.com',
|
|
65
|
+
resizing: true,
|
|
66
|
+
});
|
|
67
|
+
// [
|
|
68
|
+
// { id: 1, url: 'https://media.example.com/media/photo.jpg', thumbnail: '/cdn-cgi/image/w=400,...', alt: 'A photo' },
|
|
69
|
+
// { id: 2, url: 'https://media.example.com/media/banner.jpg', thumbnail: '/cdn-cgi/image/w=400,...', alt: '' },
|
|
70
|
+
// ]
|
|
71
|
+
|
|
72
|
+
// Custom thumbnail size
|
|
73
|
+
buildImageUrls(images, config, { thumbnail: { width: 200, quality: 60 } });
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### `buildAudioUrl(audio, config?, mediaLookup?)`
|
|
77
|
+
|
|
78
|
+
Resolve a URL for an audio file. Audio is never resized. Falls back to `external_url` when no `media_id` is present.
|
|
79
|
+
|
|
80
|
+
```js
|
|
81
|
+
// External audio
|
|
82
|
+
buildAudioUrl({ external_url: 'https://cdn.example.com/song.mp3' });
|
|
83
|
+
// → 'https://cdn.example.com/song.mp3'
|
|
84
|
+
|
|
85
|
+
// R2-hosted audio
|
|
86
|
+
buildAudioUrl(
|
|
87
|
+
{ media_id: 5 },
|
|
88
|
+
{ baseUrl: 'https://media.example.com' },
|
|
89
|
+
{ 5: 'media/song.mp3' }
|
|
90
|
+
);
|
|
91
|
+
// → 'https://media.example.com/media/song.mp3'
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## License
|
|
95
|
+
|
|
96
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,15 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arraypress/media-url",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Build media URLs with optional Cloudflare Image Resizing transforms.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.js",
|
|
7
7
|
"types": "src/index.d.ts",
|
|
8
|
-
"exports": {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./src/index.js",
|
|
11
|
+
"types": "./src/index.d.ts"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"src"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"test": "node --test tests/media-url.test.js"
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"media",
|
|
22
|
+
"url",
|
|
23
|
+
"cloudflare",
|
|
24
|
+
"image-resizing",
|
|
25
|
+
"cdn",
|
|
26
|
+
"r2"
|
|
27
|
+
],
|
|
12
28
|
"author": "David Sherlock",
|
|
13
29
|
"license": "MIT",
|
|
14
|
-
"repository": {
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "https://github.com/arraypress/media-url"
|
|
33
|
+
}
|
|
15
34
|
}
|