Skip to content

Commit a7dc2b6

Browse files
committed
chore: add package.json for URL Endpoint Function template with Jest configuration
1 parent 6510f87 commit a7dc2b6

File tree

8 files changed

+4251
-0
lines changed

8 files changed

+4251
-0
lines changed

.github/workflows/test.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Test URL Endpoint Function
2+
3+
on:
4+
push:
5+
branches: [ main, develop ]
6+
pull_request:
7+
branches: [ main, develop ]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
13+
strategy:
14+
matrix:
15+
node-version: [18.x, 20.x]
16+
17+
steps:
18+
- name: Checkout code
19+
uses: actions/checkout@v4
20+
21+
- name: Setup Node.js ${{ matrix.node-version }}
22+
uses: actions/setup-node@v4
23+
with:
24+
node-version: ${{ matrix.node-version }}
25+
cache: 'npm'
26+
27+
- name: Install dependencies
28+
run: npm ci
29+
30+
- name: Run tests
31+
run: npm test
32+
33+
- name: Run tests with coverage
34+
run: npm run test:coverage
35+
36+
- name: Upload coverage reports
37+
uses: codecov/codecov-action@v3
38+
if: matrix.node-version == '20.x'
39+
with:
40+
file: ./coverage/coverage-final.json
41+
flags: unittests
42+
name: codecov-umbrella
43+
continue-on-error: true

.gitignore

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Dependencies
2+
node_modules/
3+
4+
# Test coverage
5+
coverage/
6+
*.lcov
7+
.nyc_output/
8+
9+
# IDE
10+
.vscode/
11+
.idea/
12+
*.swp
13+
*.swo
14+
*~
15+
.DS_Store
16+
17+
# Logs
18+
logs/
19+
*.log
20+
npm-debug.log*
21+
yarn-debug.log*
22+
yarn-error.log*
23+
24+
# Environment variables
25+
.env
26+
.env.local
27+
.env.*.local
28+
29+
# Build output
30+
dist/
31+
build/

README.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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

Comments
 (0)