@inploi/sdk
v2.1.3
Published
This package contains the core functionality for the inploi SDK.
Readme
@inploi/sdk
This package contains the core functionality for the inploi SDK.
Installation
npm install @inploi/sdkUsage
Initialise the SDK with initialiseSdk:
import { initialiseSdk } from '@inploi/sdk';
const sdk = initialiseSdk({
publishableKey: 'your-publishable-key',
env: 'sandbox',
});Plugins
You can write custom plugins via the createPlugin function. The plugin will have two dependencies injected: a logger service and a an apiClient service, which can be used to make requests to the inploi API.
Example:
export const myCounterPlugin = createPlugin(({ logger, apiClient }) => {
let count = 0;
logger.info('myCounterPlugin initialised');
return {
add: () => {
count++;
logger.info(`count is now ${count}`);
},
getCount: () => count,
};
});External dependencies
If your plugin requires external dependencies, you may want to wrap the createPlugin function with whatever it takes, to then generate a function that returns a plugin:
import { createPlugin } from '@inploi/sdk';
type CounterExternalDeps = {onUpdate: (newCounter: number) => void};
export const myCounterPlugin = ({onUpdate}: CounterExternalDeps) =>
createPlugin(({ logger, apiClient }) => {
let count = 0;
logger.info('myCounterPlugin initialised');
return {
add: () => {
count++;
logger.info(`count is now ${count}`);
onUpdate(count);
},
getCount: () => count,
};
});Using plugins
Register a plugin with sdk.register. The plugin receives its declared services
and the returned object is exposed to the caller.
const sdk = initialiseSdk({
publishableKey: 'your-publishable-key',
env: 'sandbox',
});
const simple = sdk.register(mySimplePlugin);
const myCounterPlugin = sdk.register(myPluginWithDeps({ onUpdate: newCounter => console.log(newCounter) }));
myCounterPlugin.add();