Contributing to Unpic
ascorbic/unpicThis guide will help you add new image CDN providers to Unpic. It covers implementation details, utility functions, and best practices.
Overview
Each provider consists of:
- A TypeScript file containing the implementation
(
src/providers/[provider].ts
) and types for provider-specific operations and options. - A test file (
src/providers/[provider].test.ts
) - Example URLs in
src/demo/examples.json
- Detection domains or paths in
data
if appropriate - Adding to the types and exports in:
src/providers/types.ts
src/types.ts
src/extract.ts
src/transform.ts
Core Concepts and Utilities
URL Manipulation
The library provides several utilities for URL handling:
Operations Handlers
The most important utility is createOperationsHandlers
, which creates
standardized parser and generator functions:
Step-by-Step Implementation
Let’s create a complete example provider “example-cdn”:
1. Define Operations Interface
2. Configure Operations Handlers
3. Implement Core Functions
4. Add Comprehensive Tests
5. Update Types and Add Examples
Parameter Handling Patterns
Query Parameters vs Path Segments
Providers use different URL patterns for operations:
Best Practices
When to Use Utilities
Use the provided utilities when:
- You need standard parameter mapping
- Your provider follows common URL patterns
- You want automatic parameter normalization
When to Create Custom Solutions
Create custom handlers when:
- Your provider has unique URL structures
- Parameters have complex interdependencies
- You need special encoding or encryption
- The provider requires custom protocols
Error Handling
Always handle:
- Invalid URLs
- Missing parameters
- Malformed parameters
- Unsupported formats
- Edge cases
Type Safety
- Define clear interfaces extending
Operations
- Only add provider-specific operations
- Use proper TypeScript generics
- Document supported operations
Testing Requirements
-
Basic Operations
- Width/height resizing
- Format conversion
- Quality settings
- Provider-specific features
-
URL Handling
- Absolute URLs
- Relative URLs
- URL with existing parameters
- Invalid URLs
-
Parameter Edge Cases
- Missing parameters
- Invalid values
- Parameter combinations
- Default values
-
Common Scenarios
- Standard transformations
- Format conversion
- Quality adjustment
- Size constraints
Final Checklist
Before submitting:
- Implementation complete with proper types
- Comprehensive tests covering all features
- Types updated in all files listed above
- Example added to examples.json
- Detection domains or paths added if needed
- All tests passing, including unit tests and E2E tests
Development Environment
Deno Setup
This project uses Deno for development. If you haven’t already, install Deno from https://deno.com.
Basic commands:
Running Tests
Tests are written using Deno’s built-in test framework. Run them from the project root:
Image Defaults
When implementing a provider, follow these default behaviors for consistency across CDNs. If the provider does not support a feature then it can be omitted, but these are the defaults to aim for so that users have a consistent experience.
Format Handling
- Enable auto format detection/content negotiation when supported
- When supported, priority order for formats should be. For services that
generate images locally, it is ok to prefer WebP over AVIF for performance
reasons.
- AVIF
- WebP
- Original format
Image Fitting
Default to fit=cover
behavior (equivalent to CSS object-fit: cover
). This
means:
- Image should fill requested dimensions
- Maintain aspect ratio
- Crop if necessary
- Avoid distortion
Size Handling
- Never upscale images beyond their original dimensions
- Return largest available size when requested size is too large
- Maintain requested aspect ratio even when size is constrained
Local Development Server
The project includes a playground application in the demo
directory for
testing providers visually:
- Start the development server:
The playground is crucial for testing as it:
- Provides real-world testing with actual CDN endpoints
- Allows visual verification of image operations
- Tests responsive image behavior
- Verifies URL generation patterns
When adding a new provider:
- Add an example URL to
demo/src/examples.json
- Ideally use a public sample image from the CDN’s documentation
- If unavailable, use any publicly-accessible image on that CDN
- Do not skip this - no provider can be added without an example URL, because otherwise it cannot be tested
- Test comprehensively:
- Verify resizing behavior
- Check that defaults are properly applied
- Test format conversion
- Verify responsive behavior
- Ensure upscaling limits work
- Check aspect ratio handling
End-to-End Testing
The E2E tests in e2e.test.ts
verify that providers work with real CDN
endpoints. They use the images from examples.json
to test real operations:
Getting Help
If you need help:
- Review existing provider implementations
- Check test files for patterns
- Open an issue for discussion
- Ask questions in pull requests
Remember that clear, well-tested code is more important than clever solutions. Take time to write comprehensive tests and documentation.