Skip to content

Commit 3518723

Browse files
committed
feat: add multiple handler support with example handlers and tests, update README
1 parent 5d37c9d commit 3518723

File tree

6 files changed

+76
-4
lines changed

6 files changed

+76
-4
lines changed

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,27 @@ npm run test:watch # Watch mode
9191
npm run test:coverage # With coverage
9292
```
9393

94+
### Multiple Handlers
95+
96+
This template supports multiple handlers in the same repository. Each handler should follow the naming convention `handler*.js` with a corresponding test file `handler*.test.js`.
97+
98+
**Current structure:**
99+
```
100+
handler1.js # Handler implementation 1
101+
handler1.test.js # Tests for handler 1
102+
handler2.js # Handler implementation 2
103+
handler2.test.js # Tests for handler 2
104+
```
105+
106+
All test files matching `**/*.test.js` will be automatically discovered and executed by Jest. The CI pipeline will run all tests for all handlers.
107+
108+
**To add a new handler:**
109+
1. Create `handlerN.js` with your handler implementation
110+
2. Create `handlerN.test.js` with corresponding tests
111+
3. Run `npm test` to verify all handlers pass their tests
112+
113+
Coverage is collected for all `handler*.js` files automatically.
114+
94115
## CI/CD
95116

96-
GitHub Actions automatically runs tests on push/PR to `main` or `develop` branches.
117+
GitHub Actions automatically runs tests on push/PR to `main` or `develop` branches. All test files will be executed and coverage will be reported for all handlers.
File renamed without changes.

handler.test.js renamed to handler1.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { handler } = require('./handler');
1+
const { handler } = require('./handler1');
22

33
describe('URL Endpoint Function Handler', () => {
44
// Mock context object for testing
@@ -16,7 +16,7 @@ describe('URL Endpoint Function Handler', () => {
1616

1717
it('should return the original URL unchanged', () => {
1818
const url = 'https://ik.imagekit.io/demo/image.jpg';
19-
const urlPrefix = 'demo';
19+
const urlPrefix = '/';
2020

2121
const result = handler(url, urlPrefix, mockContext);
2222

handler2.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* URL Endpoint Function Handler 2
3+
*
4+
* This is a default no-op handler that returns the URL unchanged.
5+
* Modify this function to implement your custom URL transformation logic.
6+
*
7+
* @param {string} url - Full request URL including protocol, hostname, path, and query string
8+
* @param {string} urlPrefix - Pattern identifier from client configuration
9+
* @param {object} context - Read-only request context
10+
* @param {string} context.host - Request hostname
11+
* @param {string} context.clientNumber - Client identifier
12+
* @param {boolean} context.isDebug - Debug mode flag
13+
* @param {object} context.logger - Request logger
14+
*
15+
* @returns {object} Result object with url and optional signURL flag
16+
*/
17+
function handler(url, urlPrefix, context) {
18+
// Default behavior: return URL unchanged without signing
19+
return {
20+
url: url,
21+
signURL: false
22+
};
23+
}
24+
25+
module.exports.handler = handler;

handler2.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const { handler } = require('./handler2');
2+
3+
describe('URL Endpoint Function Handler 2', () => {
4+
// Mock context object for testing
5+
const mockContext = {
6+
host: 'ik.imagekit.io',
7+
clientNumber: 'test-client-123',
8+
isDebug: false,
9+
logger: {
10+
info: jest.fn(),
11+
error: jest.fn(),
12+
warn: jest.fn(),
13+
debug: jest.fn()
14+
}
15+
};
16+
17+
it('should return the original URL unchanged', () => {
18+
const url = 'https://ik.imagekit.io/demo/image.jpg';
19+
const urlPrefix = '/';
20+
21+
const result = handler(url, urlPrefix, mockContext);
22+
23+
expect(result.url).toBe(url);
24+
expect(result.signURL).toBe(false);
25+
});
26+
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"testEnvironment": "node",
2424
"coverageDirectory": "coverage",
2525
"collectCoverageFrom": [
26-
"handler.js"
26+
"handler*.js"
2727
],
2828
"testMatch": [
2929
"**/*.test.js"

0 commit comments

Comments
 (0)