Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,35 @@ jobs:
test:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18.x, 20.x]
permissions:
contents: read
pull-requests: write

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup Node.js ${{ matrix.node-version }}
- name: Setup Node.js 14.x
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
node-version: 14.x

- name: Install dependencies
run: npm ci
run: npm install

- name: Run tests
run: npm test

- name: Run tests with coverage
run: npm run test:coverage

- name: Coverage Report
uses: ArtiomTr/jest-coverage-report-action@v2
if: matrix.node-version == '20.x' && github.event_name == 'pull_request'
- name: Jest Coverage Comment
if: github.event_name == 'pull_request'
uses: MishaKav/jest-coverage-comment@main
with:
coverage-file: ./coverage/coverage-summary.json
base-coverage-file: ./coverage/coverage-summary.json
annotations: all
test-script: npm run test:coverage
coverage-summary-path: ./coverage/coverage-summary.json
title: Test Coverage
badge-title: Coverage
hide-comment: false
create-new-comment: false
hide-summary: false
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Dependencies
node_modules/
package-lock.json

# Test coverage
coverage/
Expand Down
70 changes: 24 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

Template for creating and testing ImageKit URL Endpoint Functions with unit tests and GitHub Actions CI.

## Requirements

- **Node.js 14.x** - Required for compatibility

## Quick Start

```bash
Expand All @@ -16,8 +20,13 @@ function handler(url, urlPrefix, context) {
// Your logic here
return { url: modifiedUrl }
}

// Important: Always export using this format
module.exports.handler = handler;
```

**Note:** Always include `module.exports.handler = handler;` at the end of your handler file.

### Parameters

- **`url`** - Full request URL including protocol, hostname, path, and query string
Expand Down Expand Up @@ -49,62 +58,31 @@ function handler(url, urlPrefix, context) {

## Examples

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

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

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.

### 2. Extract Path Parameters
### Quick Example

```javascript
function handler(url, urlPrefix, context) {
// Convert: /w_100/h_200/image.jpg → /image.jpg?tr=w-100,h-200
const parsedUrl = new URL(url);
const params = [];

parsedUrl.pathname = parsedUrl.pathname.replace(
/\/(w|h)_(\d+)/g,
(match, key, value) => {
params.push(`${key}-${value}`);
return '';
}
);

if (params.length > 0) {
parsedUrl.search = `?tr=${params.join(',')}`;
}

return {
url: parsedUrl.toString(),
signURL: true
url: url.replace('/v1/', '/v2/')
};
}
```

### 3. Block Private Paths

```javascript
function handler(url, urlPrefix, context) {
const parsedUrl = new URL(url);

if (parsedUrl.pathname.includes('/private/')) {
return {
status: 403,
body: { error: 'Access denied' }
};
}

return { url };
}
module.exports.handler = handler;
```

**See [examples.js](./examples.js) for 12+ more examples** including hostname changes, routing, query transformations, and more.

## Testing

```bash
Expand All @@ -115,4 +93,4 @@ npm run test:coverage # With coverage

## CI/CD

GitHub Actions automatically runs tests on push/PR to `main` or `develop` branches. Tests run on Node.js 18.x and 20.x.
GitHub Actions automatically runs tests on push/PR to `main` or `develop` branches.
210 changes: 0 additions & 210 deletions examples.js

This file was deleted.

14 changes: 14 additions & 0 deletions examples/01-simple-url-rewrite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Simple URL Rewrite
*
* Change version in path (e.g., /v1/ to /v2/)
*/

function handler(url, urlPrefix, context) {
// Change /v1/ to /v2/ in path
return {
url: url.replace('/v1/', '/v2/')
};
}

module.exports.handler = handler;
Loading