Skip to content

Commit 5d37c9d

Browse files
committed
feat: add example handlers for URL transformations and update README with usage instructions
1 parent 528f69b commit 5d37c9d

11 files changed

+274
-274
lines changed

README.md

Lines changed: 12 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -58,68 +58,31 @@ module.exports.handler = handler;
5858

5959
## Examples
6060

61-
### 1. Simple URL Rewrite
61+
See the [`examples/`](./examples) folder for ready-to-use handler implementations:
6262

63-
```javascript
64-
function handler(url, urlPrefix, context) {
65-
return {
66-
url: url.replace('/v1/', '/v2/')
67-
};
68-
}
63+
1. **Simple URL Rewrite** - Change version in path (e.g., /v1/ to /v2/)
64+
2. **Path Parameters** - Extract path parameters and convert to query string
65+
3. **Hostname Change** - Change domain/hostname
66+
4. **Keyword Path Rewriting** - Rewrite paths based on keyword mapping
67+
5. **Query Parameter Transformation** - Transform custom query params
68+
6. **Access Control** - Block access to private paths and sensitive files
69+
7. **Error Handling** - Handle errors gracefully with fallback
70+
8. **Video Thumbnail** - Generate video thumbnails with image extensions
6971

70-
module.exports.handler = handler;
71-
```
72+
Each example is a complete, working handler file that you can copy directly to `handler.js`. See [`examples/README.md`](./examples/README.md) for details.
7273

73-
### 2. Extract Path Parameters
74+
### Quick Example
7475

7576
```javascript
7677
function handler(url, urlPrefix, context) {
77-
// Convert: /w_100/h_200/image.jpg → /image.jpg?tr=w-100,h-200
78-
const parsedUrl = new URL(url);
79-
const params = [];
80-
81-
parsedUrl.pathname = parsedUrl.pathname.replace(
82-
/\/(w|h)_(\d+)/g,
83-
(match, key, value) => {
84-
params.push(`${key}-${value}`);
85-
return '';
86-
}
87-
);
88-
89-
if (params.length > 0) {
90-
parsedUrl.search = `?tr=${params.join(',')}`;
91-
}
92-
9378
return {
94-
url: parsedUrl.toString(),
95-
signURL: true
79+
url: url.replace('/v1/', '/v2/')
9680
};
9781
}
9882

9983
module.exports.handler = handler;
10084
```
10185

102-
### 3. Block Private Paths
103-
104-
```javascript
105-
function handler(url, urlPrefix, context) {
106-
const parsedUrl = new URL(url);
107-
108-
if (parsedUrl.pathname.includes('/private/')) {
109-
return {
110-
status: 403,
111-
body: { error: 'Access denied' }
112-
};
113-
}
114-
115-
return { url };
116-
}
117-
118-
module.exports.handler = handler;
119-
```
120-
121-
**See [examples.js](./examples.js) for 12+ more examples** including hostname changes, routing, query transformations, and more.
122-
12386
## Testing
12487

12588
```bash

examples.js

Lines changed: 0 additions & 225 deletions
This file was deleted.

examples/01-simple-url-rewrite.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Simple URL Rewrite
3+
*
4+
* Change version in path (e.g., /v1/ to /v2/)
5+
*/
6+
7+
function handler(url, urlPrefix, context) {
8+
// Change /v1/ to /v2/ in path
9+
return {
10+
url: url.replace('/v1/', '/v2/')
11+
};
12+
}
13+
14+
module.exports.handler = handler;

examples/02-path-parameters.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Parameter Extraction from Path
3+
*
4+
* Convert path-based parameters to query string transformations
5+
* Example: /w_100/h_200/image.jpg → /image.jpg?tr=w-100,h-200
6+
*/
7+
8+
function handler(url, urlPrefix, context) {
9+
const parsedUrl = new URL(url);
10+
const params = [];
11+
const regex = /\/(w|h)_(\d+)/g;
12+
13+
let cleanPath = parsedUrl.pathname.replace(regex, (match, key, value) => {
14+
params.push(`${key}-${value}`);
15+
return '';
16+
});
17+
18+
cleanPath = cleanPath.replace(/\/{2,}/g, '/');
19+
20+
if (params.length > 0) {
21+
parsedUrl.pathname = cleanPath;
22+
parsedUrl.search = `?tr=${params.join(',')}`;
23+
24+
context.logger.info({
25+
transformations: params.join(',')
26+
}, 'Converted path parameters to query string');
27+
}
28+
29+
return {
30+
url: parsedUrl.toString(),
31+
signURL: true // Server will sign the modified URL
32+
};
33+
}
34+
35+
module.exports.handler = handler;

examples/03-hostname-change.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Hostname Change
3+
*
4+
* Change domain from one hostname to another
5+
*/
6+
7+
function handler(url, urlPrefix, context) {
8+
// Change domain from old-domain.com to new-domain.com
9+
const parsedUrl = new URL(url);
10+
parsedUrl.hostname = 'new-domain.com';
11+
12+
return {
13+
url: parsedUrl.toString()
14+
};
15+
}
16+
17+
module.exports.handler = handler;

0 commit comments

Comments
 (0)