22
33Template for creating and testing ImageKit URL Endpoint Functions with unit tests and GitHub Actions CI.
44
5+ ## Requirements
6+
7+ - ** Node.js 14.x** - Required for compatibility
8+
59## Quick Start
610
711``` bash
@@ -16,8 +20,13 @@ function handler(url, urlPrefix, context) {
1620 // Your logic here
1721 return { url: modifiedUrl }
1822}
23+
24+ // Important: Always export using this format
25+ module .exports .handler = handler;
1926```
2027
28+ ** Note:** Always include ` module.exports.handler = handler; ` at the end of your handler file.
29+
2130### Parameters
2231
2332- ** ` url ` ** - Full request URL including protocol, hostname, path, and query string
@@ -49,62 +58,31 @@ function handler(url, urlPrefix, context) {
4958
5059## Examples
5160
52- ### 1. Simple URL Rewrite
61+ See the [ ` examples/ ` ] ( ./examples ) folder for ready-to-use handler implementations:
5362
54- ``` javascript
55- function handler (url , urlPrefix , context ) {
56- return {
57- url: url .replace (' /v1/' , ' /v2/' )
58- };
59- }
60- ```
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
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.
6173
62- ### 2. Extract Path Parameters
74+ ### Quick Example
6375
6476``` javascript
6577function handler (url , urlPrefix , context ) {
66- // Convert: /w_100/h_200/image.jpg → /image.jpg?tr=w-100,h-200
67- const parsedUrl = new URL (url);
68- const params = [];
69-
70- parsedUrl .pathname = parsedUrl .pathname .replace (
71- / \/ (w| h)_(\d + )/ g ,
72- (match , key , value ) => {
73- params .push (` ${ key} -${ value} ` );
74- return ' ' ;
75- }
76- );
77-
78- if (params .length > 0 ) {
79- parsedUrl .search = ` ?tr=${ params .join (' ,' )} ` ;
80- }
81-
8278 return {
83- url: parsedUrl .toString (),
84- signURL: true
79+ url: url .replace (' /v1/' , ' /v2/' )
8580 };
8681}
87- ```
88-
89- ### 3. Block Private Paths
9082
91- ``` javascript
92- function handler (url , urlPrefix , context ) {
93- const parsedUrl = new URL (url);
94-
95- if (parsedUrl .pathname .includes (' /private/' )) {
96- return {
97- status: 403 ,
98- body: { error: ' Access denied' }
99- };
100- }
101-
102- return { url };
103- }
83+ module .exports .handler = handler;
10484```
10585
106- ** See [ examples.js] ( ./examples.js ) for 12+ more examples** including hostname changes, routing, query transformations, and more.
107-
10886## Testing
10987
11088``` bash
@@ -115,4 +93,4 @@ npm run test:coverage # With coverage
11593
11694## CI/CD
11795
118- GitHub Actions automatically runs tests on push/PR to ` main ` or ` develop ` branches. Tests run on Node.js 18.x and 20.x.
96+ GitHub Actions automatically runs tests on push/PR to ` main ` or ` develop ` branches.
0 commit comments