@enadhq/enad-ts-sdk
v0.16.9
Published
A TypeScript SDK for interacting with the **Enad API**. Provides a clean, type-safe interface with all responses standardized to `[data, error]`.
Downloads
1,160
Readme
Enad SDK
A TypeScript SDK for interacting with the Enad API.
Provides a clean, type-safe interface with all responses standardized to [data, error].
Installation
pnpm add @enadhq/enad-ts-sdk
# or
npm install @enadhq/enad-ts-sdk
# or
yarn add @enadhq/enad-ts-sdkimport { ShopperClient } from "@enadhq/enad-ts-sdk";
// Minimum configuration
export const client = new ShopperClient({
apiKey: "api-key-123",
appId: "app-id-UUID",
groupId: "store-group-slug",
});Usage
- Fetch a single product by SKU
const [product, error] = await client.products.getBySku("SKU123");
if (error) {
console.error("Failed to fetch product:", error.message);
} else {
console.log("Product id:", product.id);
}- Fetch multiple products by SKUs
const [skus, error] = await client.products.getBySkus(["SKU123", "SKU456"]);
if (error) {
console.error("Failed to fetch products:", error.message);
} else {
console.log("skus:", skus);
}- Search products
const [result, error] = await client.products.search("laptop", { page: 1, per_page: 5 });
if (error) {
console.error("Search failed:", error.message);
} else {
console.log("Search results:", result);
}Response Format
All SDK methods return a standardized tuple-style response, making it simple and predictable to handle results:
type ApiResponse<T> = [data: T, error: null] | [data: null, error: ApiError];This design allows for easy destructuring and explicit naming, for example:
const [tags, error] = await client.tags.getAll();
if (error) {
console.error("Failed to fetch tags:", error.message);
return;
}
console.log("Tags:", tags);Because of the response contract:
- data is guaranteed to be defined if error is null.
- error is guaranteed to be defined if data is null.
This means you can safely use one or the other without additional null checks, and you don’t need try/catch blocks for simple error handling.
