This guide covers how to contribute to the next-api-bridge package.
- Node.js 18+
- npm or yarn
- Git
- Clone the repository:
git clone <repository-url>
cd next-api-bridge- Install dependencies:
npm install# Build the package
npm run build
# Watch mode for development
npm run dev
# Type checking
npm run typecheck
# Prepare for publishing
npm run prepublishOnlysrc/
index.ts # Main entry point (server-only)
create-client.ts # Factory function
client.ts # Main client class
config/
constants.ts # Default constants (no env vars)
cookies/
build-cookie-header.ts # Cookie header builder
parse-set-cookie.ts # Pure cookie parsing utilities
sync-response-cookies.ts # Cookie sync from response
logger/
colors.ts # ANSI color utilities
logger.ts # Logging functions
types/
auth.ts # Authentication types
client.ts # Client and request types
response.ts # Response types
cookies.ts # Cookie types
index.ts # Type re-exports
form/
cache.ts # Page revalidation helper
feedback.ts # Toast helpers (client-only)
form-data.ts # FormData parser
utils.ts # Redirect helpers
index.ts # Form helper exports
examples/
bridge.ts # Example API bridge instances
products.actions.ts # Product CRUD examples
weather.actions.ts # Weather API example
index.ts # Example re-exports
The package uses server-only to prevent client-side usage. All core code must run on the server.
The package does not read environment variables directly. All configuration is passed through the createNextApiBridge() factory function.
Code is organized by responsibility:
config/- Constants and configurationcookies/- Cookie handling logiclogger/- Logging utilitiestypes/- TypeScript type definitionsform/- Form helper utilities
Cookie parsing functions are pure (no Next.js dependencies) for easier testing.
Cookies are passed through method calls, not stored as shared state. This ensures concurrency safety.
- Use strict TypeScript mode
- Prefer explicit types over
any - Use interfaces for public APIs
- Use type aliases for utility types
- Constants: UPPER_SNAKE_CASE
- Types: PascalCase with
Iprefix for interfaces (e.g.,IUser) - Functions: camelCase
- Files: kebab-case
- Group imports: standard, external, internal
- Use absolute imports for internal modules
- Order:
import 'server-only'first, then other imports
- Use JSDoc for public APIs
- Keep comments concise and relevant
- Document non-obvious logic
Define types in the appropriate file in src/types/:
// src/types/feature.ts
export interface FeatureOptions {
// options
}Export from src/types/index.ts.
Add implementation in the appropriate module (e.g., src/cookies/, src/logger/).
Add methods to NextApiBridgeClient in src/client.ts if needed.
If the feature requires configuration, update ApiBridgeOptions and the factory function.
Add example usage in examples/ directory.
Update README.md with the new feature.
Use the example Server Actions in the examples/ directory:
import { getProductsExampleAction } from 'next-api-bridge/examples';
const response = await getProductsExampleAction();
console.log(response);Run type checking before committing:
npm run typecheckBuild the package to ensure it compiles correctly:
npm run buildThe package uses tsup for bundling:
npm run buildThis outputs to the dist/ directory with:
- ESM format (
dist/index.js) - CommonJS format (
dist/index.cjs) - TypeScript declarations (
dist/index.d.ts)
Publishing is automated via GitHub Actions. To publish a new version:
- Update version in
package.json(follow semantic versioning) - Commit and push your changes
- Create and push a version tag:
git tag v0.1.5
git push origin v0.1.5The GitHub Actions workflow will automatically:
- Run type checking
- Build the package
- Publish to npm using Trusted Publishing (OIDC)
You can also trigger the workflow manually from the GitHub Actions tab.
If you need to publish manually:
npm run prepublishOnly
npm publishWhen reporting issues, include:
- Next.js version
- Node.js version
- Package version
- Minimal reproduction code
- Expected vs actual behavior
- Error messages and stack traces
- Fork the repository
- Branch from
main(e.g.,feature/your-feature) - Commit with clear messages
- Push to your fork
- Create a pull request
- Code follows project structure
- Types are properly defined
- No environment variables are read directly
- Server-only enforcement is maintained
- TypeScript compiles without errors
- Documentation is updated
- Examples are added if applicable
By contributing, you agree that your contributions will be licensed under the MIT License.