Skip to content

Commit ec6d789

Browse files
authored
Merge pull request #2 from imagekit-developer/test-pr
Test pr
2 parents c647818 + 5d37c9d commit ec6d789

17 files changed

+317
-4046
lines changed

.github/workflows/test.yml

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,35 @@ jobs:
1010
test:
1111
runs-on: ubuntu-latest
1212

13-
strategy:
14-
matrix:
15-
node-version: [18.x, 20.x]
13+
permissions:
14+
contents: read
15+
pull-requests: write
1616

1717
steps:
1818
- name: Checkout code
1919
uses: actions/checkout@v4
2020

21-
- name: Setup Node.js ${{ matrix.node-version }}
21+
- name: Setup Node.js 14.x
2222
uses: actions/setup-node@v4
2323
with:
24-
node-version: ${{ matrix.node-version }}
25-
cache: 'npm'
24+
node-version: 14.x
2625

2726
- name: Install dependencies
28-
run: npm ci
27+
run: npm install
2928

3029
- name: Run tests
3130
run: npm test
3231

3332
- name: Run tests with coverage
3433
run: npm run test:coverage
3534

36-
- name: Coverage Report
37-
uses: ArtiomTr/jest-coverage-report-action@v2
38-
if: matrix.node-version == '20.x' && github.event_name == 'pull_request'
35+
- name: Jest Coverage Comment
36+
if: github.event_name == 'pull_request'
37+
uses: MishaKav/jest-coverage-comment@main
3938
with:
40-
coverage-file: ./coverage/coverage-summary.json
41-
base-coverage-file: ./coverage/coverage-summary.json
42-
annotations: all
43-
test-script: npm run test:coverage
39+
coverage-summary-path: ./coverage/coverage-summary.json
40+
title: Test Coverage
41+
badge-title: Coverage
42+
hide-comment: false
43+
create-new-comment: false
44+
hide-summary: false

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Dependencies
22
node_modules/
3+
package-lock.json
34

45
# Test coverage
56
coverage/

README.md

Lines changed: 24 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
Template 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
6577
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-
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.

examples.js

Lines changed: 0 additions & 210 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;

0 commit comments

Comments
 (0)