|
| 1 | +# URL Endpoint Function Template |
| 2 | + |
| 3 | +Template for creating and testing ImageKit URL Endpoint Functions with unit tests and GitHub Actions CI. |
| 4 | + |
| 5 | +## Quick Start |
| 6 | + |
| 7 | +```bash |
| 8 | +npm install |
| 9 | +npm test |
| 10 | +``` |
| 11 | + |
| 12 | +## Handler Signature |
| 13 | + |
| 14 | +```javascript |
| 15 | +function handler(url, urlPrefix, context) { |
| 16 | + // Your logic here |
| 17 | + return { url: modifiedUrl } |
| 18 | +} |
| 19 | +``` |
| 20 | + |
| 21 | +### Parameters |
| 22 | + |
| 23 | +- **`url`** - Full request URL including protocol, hostname, path, and query string |
| 24 | +- **`urlPrefix`** - Pattern identifier from client configuration |
| 25 | +- **`context`** - Request context object: |
| 26 | + - `host` - Request hostname |
| 27 | + - `clientNumber` - Client identifier |
| 28 | + - `isDebug` - Debug mode flag |
| 29 | + - `logger` - Request logger (`.info()`, `.warn()`, `.error()`, `.debug()`) |
| 30 | + |
| 31 | +### Return Types |
| 32 | + |
| 33 | +**Normal Result** (continue processing): |
| 34 | +```javascript |
| 35 | +{ |
| 36 | + url: string, // Required: Full URL with protocol + hostname |
| 37 | + signURL: boolean // Optional: Should server sign this URL? (default: false) |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +**Early Response** (stop processing): |
| 42 | +```javascript |
| 43 | +{ |
| 44 | + status: number, // Required: HTTP status code |
| 45 | + body: string | object, // Required: Response body |
| 46 | + headers: Record<string, string> // Optional: Response headers |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +## Examples |
| 51 | + |
| 52 | +### 1. Simple URL Rewrite |
| 53 | + |
| 54 | +```javascript |
| 55 | +function handler(url, urlPrefix, context) { |
| 56 | + return { |
| 57 | + url: url.replace('/v1/', '/v2/') |
| 58 | + }; |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +### 2. Extract Path Parameters |
| 63 | + |
| 64 | +```javascript |
| 65 | +function 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 | + |
| 82 | + return { |
| 83 | + url: parsedUrl.toString(), |
| 84 | + signURL: true |
| 85 | + }; |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +### 3. Block Private Paths |
| 90 | + |
| 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 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +**See [examples.js](./examples.js) for 12+ more examples** including hostname changes, routing, query transformations, and more. |
| 107 | + |
| 108 | +## Testing |
| 109 | + |
| 110 | +```bash |
| 111 | +npm test # Run tests |
| 112 | +npm run test:watch # Watch mode |
| 113 | +npm run test:coverage # With coverage |
| 114 | +``` |
| 115 | + |
| 116 | +## CI/CD |
| 117 | + |
| 118 | +GitHub Actions automatically runs tests on push/PR to `main` or `develop` branches. Tests run on Node.js 18.x and 20.x. |
0 commit comments