| id | aliases | tags |
|---|---|---|
AGENTS |
This document provides guidelines for AI coding agents working on this repository.
service-injector is a lightweight JavaScript/TypeScript library for SaaS providers to embed their services on client websites as a floating tab/window with iframe integration. The library is designed to work without external dependencies.
Tech Stack:
- Primary Language: TypeScript
- Build Tool: tsup (based on esbuild)
- Output Formats: ESM, CommonJS, IIFE (browser)
- No runtime dependencies
service-injector/
├── src/ # TypeScript source files
│ ├── index.ts # Entry point, exports
│ ├── types.ts # TypeScript interfaces
│ ├── defaults.ts # Default config, templates, styles
│ ├── utils.ts # Helper functions
│ ├── animation.ts # Animation system
│ ├── ServiceInjector.ts # Main class
│ └── auto-install.ts # Script-tag auto-installation
├── dist/ # Build output (generated, gitignored)
│ ├── index.js # ESM bundle
│ ├── index.cjs # CommonJS bundle
│ ├── index.iife.js # Browser bundle (auto-installs)
│ ├── index.d.ts # TypeScript declarations
│ └── *.map # Source maps
├── demo/
│ └── index.html # Demo page
├── docs/ # Documentation
│ ├── README.md # Overview and navigation
│ ├── installation.md # Installation methods
│ ├── configuration.md # All configuration options
│ ├── customization.md # Templates, styles, theming
│ ├── docking.md # Dockable feature
│ ├── api.md # Programmatic API reference
│ └── mobile.md # Mobile behavior
├── package.json
├── tsconfig.json
├── tsup.config.ts
├── .gitignore
├── .npmignore
├── README.md
├── LICENSE
└── AGENTS.md
# Install dependencies
npm install
# Build all formats (ESM, CJS, IIFE)
npm run build
# Build in watch mode for development
npm run dev- Use modern TypeScript features (ES2015+ target)
- Prefer
constandletovervar - Use explicit type annotations for function parameters and return types
- Use interfaces for object shapes, types for unions/primitives
- Document public APIs with JSDoc comments
// Good
export function parseValue(val: string): string | number | boolean {
// ...
}
// Good - interface for object shape
export interface ServiceInjectorConfig {
url?: string | null;
position?: 'left' | 'right' | 'top' | 'bottom';
}- Variables/functions:
camelCase - Classes:
PascalCase - Constants:
UPPER_SNAKE_CASE - Interfaces/Types:
PascalCase - Private class members:
camelCase(no underscore prefix)
- One class per file for main classes
- Group related utilities in single files
- Keep files focused and reasonably sized
- Export types from
types.ts - Export defaults from
defaults.ts
The main class that handles all functionality:
const injector = new ServiceInjector({
saasUrl: 'https://example.com',
position: 'right',
draggable: true
});
injector.install(); // Mount to DOM
injector.toggle(); // Toggle window
injector.destroy(); // CleanupThe IIFE bundle auto-installs when loaded via script tag:
<script id="service-injector" src="dist/index.iife.js?p=right&o=100px"></script>This is handled by auto-install.ts which:
- Detects script tag with
id="service-injector" - Creates ServiceInjector instance
- Calls
install()automatically
Two configuration methods:
- Programmatic: Pass options to
new ServiceInjector(options) - Script tag: Query params (
?p=right) or data attributes (data-position="right")
Short keys (for query params) are mapped to long names in defaults.ts:
const CONFIG_MAPPING = {
'position': 'p',
'offset': 'o',
// ...
};The injector exposes global functions based on the prefix:
// With default prefix "si":
window.siToggleWindow = () => injector.toggle();
window.siDestroy = () => injector.destroy();
// With custom prefix "my":
window.myToggleWindow = () => injector.toggle();
window.myDestroy = () => injector.destroy();Templates support placeholders:
%prefix%- Element ID prefix%url%- The configured URL
const tabTemplate = "<a onclick='return %prefix%ToggleWindow();'>Open</a>";- Add to
InternalConfiginterface intypes.ts - Add to
ServiceInjectorConfiginterface intypes.ts(readable name) - Add default value in
DEFAULT_CONFIGindefaults.ts - Add mapping in
CONFIG_MAPPING(for script tag support) - Add mapping in
OPTIONS_TO_CONFIG(for programmatic API) - Handle the option in
ServiceInjector.install() - Update documentation in
docs/configuration.md - Update
docs/api.mdif adding new methods
- Window state is in
this.state.winandthis.state.winPosition - Drag/resize logic is in
initDragAndResize() - Toggle animation is in
expand()andcollapse()
- Default styles are in
DEFAULT_STYLESindefaults.ts - Use
%prefix%placeholder for element IDs
Currently no automated test framework. Test manually by:
- Run
npm run build - Open
demo/index.htmlin a browser - Test floating tab/window functionality
- Test mobile behavior (opens in new window)
- Test drag and resize functionality
- Test different configurations
# Build first
npm run build
# Publish to npm
npm publishThe prepublishOnly script automatically runs the build.
Documentation lives in the docs/ folder:
| File | Content |
|---|---|
docs/README.md |
Overview and navigation hub |
docs/installation.md |
npm, CDN, script tag, self-hosting |
docs/configuration.md |
All configuration options |
docs/customization.md |
Templates, styles, theming |
docs/docking.md |
Dockable feature deep-dive |
docs/api.md |
Programmatic API reference |
docs/mobile.md |
Mobile device behavior |
Important: When adding or modifying features, always update the relevant documentation files. Keep documentation in sync with code changes. The docs are the primary reference for users.
- Always update documentation if it's needed according to performed changes
- Always commit changes after completing a task
- Do not push to remote unless explicitly requested by the user
- Run
npm run buildafter making source changes to verify compilation - Keep backwards compatibility with script tag usage