@besar1/simple-sdk-loader
v1.0.0
Published
Dynamic loader for simple-sdk with version management
Maintainers
Readme
Simple SDK Loader
A dynamic loader for @besar1/simple-sdk that can load different versions from NPM or CDN based on configuration.
Installation
npm install @besar1/simple-sdk-loader
# or
pnpm add @besar1/simple-sdk-loader
# or
yarn add @besar1/simple-sdk-loaderUsage
Loading from NPM
import { SimpleSDKLoader } from '@besar1/simple-sdk-loader';
const loader = new SimpleSDKLoader({
package: 'npm',
version: '1.0.0' // optional, defaults to 'latest'
});
const sdk = await loader.load();
const result = await sdk.doSomething();
console.log(result);Note: For NPM loading with specific versions, the loader uses ESM imports from CDN since dynamic imports can't specify package versions directly. For 'latest' version, it first tries to import locally installed packages, then falls back to CDN.
Loading from CDN
import { SimpleSDKLoader } from '@besar1/simple-sdk-loader';
const loader = new SimpleSDKLoader({
package: 'umd',
version: '1.0.0',
cdnUrl: 'https://unpkg.com' // optional, defaults to unpkg
});
const sdk = await loader.load();
const result = await sdk.doSomething();
console.log(result);Browser Usage
<script src="https://unpkg.com/@besar1/simple-sdk-loader@latest/dist/index.umd.js"></script>
<script>
const loader = new SimpleSDKLoader.SimpleSDKLoader({
package: 'umd',
version: 'latest'
});
loader.load().then(sdk => {
return sdk.doSomething();
}).then(result => {
console.log(result);
});
</script>API
SimpleSDKLoader
Constructor
new SimpleSDKLoader(config: LoaderConfig, options?: LoaderOptions)Configuration
interface LoaderConfig {
package: 'npm' | 'umd';
version?: string | 'latest';
cdnUrl?: string; // Custom CDN URL for UMD loading
}
interface LoaderOptions {
timeout?: number; // Timeout for loading SDK (default: 10000ms)
retries?: number; // Number of retries for failed loads (default: 3)
}Methods
load(): Promise<SDKClient>- Load the SDK and return the client instancegetSDK(): SDKClient | null- Get the currently loaded SDK instanceisLoaded(): boolean- Check if SDK is loadedgetConfig(): LoaderConfig- Get current configuration
Version Loading Strategy
NPM Package Loading (package: 'npm')
- Latest version: Tries local import first, falls back to CDN ESM
- Specific version: Always loads from CDN using ESM format
- URL format:
https://unpkg.com/@besar1/simple-sdk@{version}/dist/index.esm.js
CDN Loading (package: 'umd')
- Any version: Loads UMD bundle from CDN
- URL format:
https://unpkg.com/@besar1/simple-sdk@{version}/dist/index.umd.js
Features
- ✅ Dynamic loading from NPM or CDN
- ✅ Version-specific loading: Load exact versions specified in configuration
- ✅ TypeScript support with proper type re-exports
- ✅ Configurable timeouts and retries
- ✅ Browser and Node.js support
- ✅ Custom CDN URL support
- ✅ Fallback strategies for different environments
How it works
- NPM Loading:
- For 'latest': Tries dynamic import of locally installed package, falls back to CDN ESM
- For specific versions: Uses CDN ESM imports with version-specific URLs
- CDN Loading: Dynamically injects script tags to load UMD bundles from CDN
- Type Safety: Re-exports types from the core SDK to ensure interface compatibility
- Version Management: Constructs version-specific URLs for precise version loading
License
MIT
