OneSDK for Node.js
OneSDK is a universal API client for Node.js. This page describes OneSDK public API and configuration options.
Important links
- OneSDK on GitHub for development and reporting issues
- OneSDK on npm
- Getting Started tutorial
Installation
OneSDK is available as Node package under the name @superfaceai/one-sdk
. For installation in a Node.js run:
npm install @superfaceai/one-sdk
Usage
Configuration
OneSDK requires a configuration file, by default located in superface/super.json
path. You can create this file using the Superface CLI by installing a profile and configuring a provider:
npx @superfaceai/cli install <profileName>
npx @superfaceai/cli configure <providerName> -p <profileName>
Initializing the OneSDK client
OneSDK package exports SuperfaceClient
constructor to initialize an SDK client:
import { SuperfaceClient } from '@superface/one-sdk';
const client = new SuperfaceClient();
Performing a use-case (getProfile
, getUsecase
, perform
)
With client initialized you can perform the use-cases in installed profiles using the following series of methods:
getProfile('<profileName>')
returns an instance of Profile from the SDK client instance,getUsecase('<usecaseName>')
returns a use-case from Profile,perform(input, [options])
executes the use-case with provided input data and optionaloptions
argument.
const profile = await client.getProfile('<profileName>');
const result = await profile.getUsecase('<usecaseName>').perform({
inputField: 1,
anotherInputField: 'hello',
});
Perform returns a result object.
Selecting a provider (getProvider
)
By default, OneSDK picks the first configured provider or the first provider according to profile's priority
array in super.json
configuration.
You can choose a specific provider for perform
. Use the getProvider
method to obtain provider's configuration and pass it into perform
:
const provider = await client.getProvider('<providerName>');
const result = await profile.getUseCase('<usecaseName>').perform(
{
// Input parameters
},
{ provider }
);
Read more in using multiple providers guide.
Handling the result (Result
type)
The perform
method always returns a Result
type that is either Ok
or Err
. This follows the NeverThrow approach. The Result
type provides multiple approaches for handling the success and error conditions:
Conditionals
Use isOk()
or isErr()
to check type of result in conditionals.
if (result.isErr()) {
// Result is error, error.toString() returns human readable description of what went wrong
console.log(result.error.toString());
} else {
// Result is ok and you can access the result data through value
console.log(result.value);
}
Matching a value or error
The Result
provides a match
method which accepts two functions. The first function handles the Ok
result and the the second one handles the Err
result. The conditionals example above can be rewritten using match
like this:
result.match(
value => console.log(value),
error => console.log(error.toString())
);
Unsafely unwrapping the result
Lastly, you can just use unwrap
method which will either return a result value, or throw an error.
try {
// Possible error is thrown here and it contains human readable description of what went wrong :)
const value = result.unwrap();
// You can access value here
console.log(value);
} catch (e) {
console.log(e);
}
Configuration
The Superface OneSDK is configurable through various environment variables:
SUPERFACE_SDK_TOKEN
- auth token to enable integrations monitoringSUPERFACE_PATH
- path to thesuper.json
configuration file; default is./superface/super.json
(relative to current working directory)SUPERFACE_API_URL
- URL of the Superface services, useful for SDK development; default ishttps://superface.ai
SUPERFACE_METRIC_DEBOUNCE_TIME_MIN
andSUPERFACE_METRIC_DEBOUNCE_TIME_MAX
- to rate limit metric reporting, OneSDK will send aggregated metrics after at leastMIN
milliseconds and at mostMAX
milliseconds; default is 1000 forMIN
and 60000 forMAX
SUPERFACE_DISABLE_METRIC_REPORTING
- set this variable to any value to disable metrics reporting; enabled by defaultDEBUG
- configures the debugging output; disabled by default, to observe all debug messages relevant to OneSDK set this variable tosuperface*