@helebba/sdk 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,21 @@
1
+ # Helebba SDK
2
+
3
+ Cliente minimo para leer inventario desde aplicaciones externas.
4
+
5
+ ```ts
6
+ import { createHelebbaClient } from '@hlb/sdk';
7
+
8
+ const helebba = createHelebbaClient({
9
+ apiKey: process.env.HELEBBA_API_KEY!,
10
+ });
11
+
12
+ const products = await helebba.products.list({ search: 'camisa' });
13
+ const brands = await helebba.brands.list();
14
+ const categories = await helebba.categories.list();
15
+ ```
16
+
17
+ Para desarrollo, la API key debe incluir organizacion y scope:
18
+
19
+ ```txt
20
+ DEV_API_KEYS="hlb_dev_xxxxxxxxxxxxxxxxxxxx:id=sdk-dev,organizationId=<org-id>,scopes=inventory:read,products=GSDK"
21
+ ```
@@ -0,0 +1,64 @@
1
+ type OffsetPageInfo = {
2
+ page: number;
3
+ pages: number;
4
+ pageSize: number;
5
+ totalItems: number;
6
+ hasPreviousPage: boolean;
7
+ hasNextPage: boolean;
8
+ previousPage: number | null;
9
+ nextPage: number | null;
10
+ };
11
+ type OffsetPaginatedResult<T> = {
12
+ kind: 'offset';
13
+ count: number;
14
+ items: T[];
15
+ pageInfo: OffsetPageInfo;
16
+ };
17
+ type ListParams = {
18
+ page?: number;
19
+ limit?: number;
20
+ search?: string;
21
+ };
22
+ type HelebbaClientOptions = {
23
+ apiKey: string;
24
+ baseUrl?: string;
25
+ fetcher?: typeof fetch;
26
+ };
27
+ type HelebbaClient = {
28
+ products: {
29
+ list: (params?: ListParams) => Promise<OffsetPaginatedResult<Product>>;
30
+ get: (productId: string) => Promise<Product>;
31
+ };
32
+ brands: {
33
+ list: (params?: ListParams) => Promise<OffsetPaginatedResult<InventoryBrand>>;
34
+ };
35
+ categories: {
36
+ list: (params?: ListParams) => Promise<OffsetPaginatedResult<Category>>;
37
+ };
38
+ };
39
+ type Product = Record<string, unknown> & {
40
+ id: string;
41
+ name: string;
42
+ sku?: string;
43
+ description?: string;
44
+ };
45
+ type InventoryBrand = Record<string, unknown> & {
46
+ id: string;
47
+ name: string;
48
+ description?: string;
49
+ };
50
+ type Category = Record<string, unknown> & {
51
+ id: string;
52
+ name: string;
53
+ description?: string;
54
+ };
55
+
56
+ declare const createHelebbaClient: (options: HelebbaClientOptions) => HelebbaClient;
57
+
58
+ type HelebbaApiError = Error & {
59
+ status: number;
60
+ body: unknown;
61
+ };
62
+ declare const createHelebbaApiError: (message: string, status: number, body: unknown) => HelebbaApiError;
63
+
64
+ export { type Category, type HelebbaApiError, type HelebbaClient, type HelebbaClientOptions, type InventoryBrand, type ListParams, type OffsetPaginatedResult, type Product, createHelebbaApiError, createHelebbaClient };
package/dist/index.mjs ADDED
@@ -0,0 +1,68 @@
1
+ // src/http.ts
2
+ var createHelebbaApiError = (message, status, body) => {
3
+ const error = new Error(message);
4
+ error.name = "HelebbaApiError";
5
+ error.status = status;
6
+ error.body = body;
7
+ return error;
8
+ };
9
+ var trimTrailingSlash = (value) => value.replace(/\/+$/, "");
10
+ var DEFAULT_BASE_URL = "https://api.helebba.com/api/v1";
11
+ var appendQuery = (url, query) => {
12
+ if (!query) return;
13
+ if (query.page !== void 0) url.searchParams.set("page", String(query.page));
14
+ if (query.limit !== void 0) url.searchParams.set("limit", String(query.limit));
15
+ if (query.search) url.searchParams.set("search", query.search);
16
+ };
17
+ var createHttpClient = (options) => {
18
+ if (!options.apiKey) throw new Error("Helebba SDK requires apiKey");
19
+ const apiKey = options.apiKey;
20
+ const baseUrl = trimTrailingSlash(options.baseUrl ?? DEFAULT_BASE_URL);
21
+ const fetcher = options.fetcher ?? fetch;
22
+ return {
23
+ get: async (path, options2 = {}) => {
24
+ const url = new URL(`${baseUrl}${path}`);
25
+ appendQuery(url, options2.query);
26
+ const timestamp = Date.now().toString();
27
+ const response = await fetcher(url, {
28
+ method: "GET",
29
+ headers: {
30
+ "api-key": apiKey,
31
+ "x-timestamp": timestamp,
32
+ "x-path": url.pathname,
33
+ "x-client-user-agent": "GSDK/0.1.0 (node)",
34
+ Accept: "application/json"
35
+ }
36
+ });
37
+ const contentType = response.headers.get("content-type") ?? "";
38
+ const body = contentType.includes("application/json") ? await response.json() : await response.text();
39
+ if (!response.ok) {
40
+ const message = typeof body === "object" && body && "message" in body ? String(body.message) : `Helebba API request failed with status ${response.status}`;
41
+ throw createHelebbaApiError(message, response.status, body);
42
+ }
43
+ return body;
44
+ }
45
+ };
46
+ };
47
+
48
+ // src/client.ts
49
+ var createHelebbaClient = (options) => {
50
+ const http = createHttpClient(options);
51
+ return {
52
+ products: {
53
+ list: (params = {}) => http.get("/sdk/products", { query: params }),
54
+ get: (productId) => http.get(`/sdk/products/${encodeURIComponent(productId)}`)
55
+ },
56
+ brands: {
57
+ list: (params = {}) => http.get("/sdk/brands", { query: params })
58
+ },
59
+ categories: {
60
+ list: (params = {}) => http.get("/sdk/categories", { query: params })
61
+ }
62
+ };
63
+ };
64
+ export {
65
+ createHelebbaApiError,
66
+ createHelebbaClient
67
+ };
68
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/http.ts","../src/client.ts"],"sourcesContent":["import type { HelebbaClientOptions, ListParams } from './types';\n\ntype RequestOptions = {\n query?: ListParams;\n};\n\nexport type HelebbaApiError = Error & {\n status: number;\n body: unknown;\n};\n\nexport const createHelebbaApiError = (\n message: string,\n status: number,\n body: unknown,\n): HelebbaApiError => {\n const error = new Error(message) as HelebbaApiError;\n error.name = 'HelebbaApiError';\n error.status = status;\n error.body = body;\n return error;\n};\n\nconst trimTrailingSlash = (value: string) => value.replace(/\\/+$/, '');\nconst DEFAULT_BASE_URL = 'https://api.helebba.com/api/v1';\n\nconst appendQuery = (url: URL, query: ListParams | undefined) => {\n if (!query) return;\n\n if (query.page !== undefined) url.searchParams.set('page', String(query.page));\n if (query.limit !== undefined) url.searchParams.set('limit', String(query.limit));\n if (query.search) url.searchParams.set('search', query.search);\n};\n\nexport type HttpClient = {\n get: <T>(path: string, options?: RequestOptions) => Promise<T>;\n};\n\nexport const createHttpClient = (options: HelebbaClientOptions): HttpClient => {\n if (!options.apiKey) throw new Error('Helebba SDK requires apiKey');\n\n const apiKey = options.apiKey;\n const baseUrl = trimTrailingSlash(options.baseUrl ?? DEFAULT_BASE_URL);\n const fetcher = options.fetcher ?? fetch;\n\n return {\n get: async <T>(path: string, options: RequestOptions = {}): Promise<T> => {\n const url = new URL(`${baseUrl}${path}`);\n appendQuery(url, options.query);\n\n const timestamp = Date.now().toString();\n\n const response = await fetcher(url, {\n method: 'GET',\n headers: {\n 'api-key': apiKey,\n 'x-timestamp': timestamp,\n 'x-path': url.pathname,\n 'x-client-user-agent': 'GSDK/0.1.0 (node)',\n Accept: 'application/json',\n },\n });\n\n const contentType = response.headers.get('content-type') ?? '';\n const body = contentType.includes('application/json')\n ? await response.json()\n : await response.text();\n\n if (!response.ok) {\n const message =\n typeof body === 'object' && body && 'message' in body\n ? String((body as { message: unknown }).message)\n : `Helebba API request failed with status ${response.status}`;\n\n throw createHelebbaApiError(message, response.status, body);\n }\n\n return body as T;\n },\n };\n};\n","import { createHttpClient } from './http';\nimport type {\n Category,\n HelebbaClient,\n HelebbaClientOptions,\n InventoryBrand,\n OffsetPaginatedResult,\n Product,\n} from './types';\n\nexport const createHelebbaClient = (options: HelebbaClientOptions): HelebbaClient => {\n const http = createHttpClient(options);\n\n return {\n products: {\n list: (params = {}) =>\n http.get<OffsetPaginatedResult<Product>>('/sdk/products', { query: params }),\n get: (productId) => http.get<Product>(`/sdk/products/${encodeURIComponent(productId)}`),\n },\n brands: {\n list: (params = {}) =>\n http.get<OffsetPaginatedResult<InventoryBrand>>('/sdk/brands', { query: params }),\n },\n categories: {\n list: (params = {}) =>\n http.get<OffsetPaginatedResult<Category>>('/sdk/categories', { query: params }),\n },\n };\n};\n"],"mappings":";AAWO,IAAM,wBAAwB,CACnC,SACA,QACA,SACoB;AACpB,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AI;AAChB,cAAM,UACJ,OAAO,SAAS,YAAY,QAAQ,aAAa,OAC7C,OAAQ,KAA8B,OAAO,IAC7C,0CAA0C,SAAS,MAAM;AAE/D,cAAM,sBAAsB,SAAS,SAAS,QAAQ,IAAI;AAAA,MAC5D;AAEA,aAAO;AAAA,IACT;AAAA,EACF;AACF;;;ACtEO,IAAM,sBAAsB,CAAC,YAAiD;AACnF,QAAM,OAAO,iBAAiB,OAAO;AAErC,SAAO;AAAA,IACL,UAAU;AAAA,MACR,MAAM,CAAC,SAAS,CAAC,MACf,KAAK,IAAoC,iBAAiB,EAAE,OAAO,OAAO,CAAC;AAAA,MAC7E,KAAK,CAAC,cAAc,KAAK,IAAa,iBAAiB,mBAAmB,SAAS,CAAC,EAAE;AAAA,IACxF;AAAA,IACA,QAAQ;AAAA,MACN,MAAM,CAAC,SAAS,CAAC,MACf,KAAK,IAA2C,eAAe,EAAE,OAAO,OAAO,CAAC;AAAA,IACpF;AAAA,IACA,YAAY;AAAA,MACV,MAAM,CAAC,SAAS,CAAC,MACf,KAAK,IAAqC,mBAAmB,EAAE,OAAO,OAAO,CAAC;AAAA,IAClF;AAAA,EACF;AACF;","names":["options"]}
Binary file
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@helebba/sdk",
3
+ "version": "0.1.0",
4
+ "description": "Helebba SDK for external applications.",
5
+ "main": "./dist/index.mjs",
6
+ "types": "./dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "development": "./src/index.ts",
10
+ "import": "./dist/index.mjs",
11
+ "default": "./dist/index.mjs"
12
+ }
13
+ },
14
+ "scripts": {
15
+ "dev": "NODE_OPTIONS='--conditions=development' tsup --config tsup.dev.config.ts --watch",
16
+ "build": "tsup",
17
+ "check-types": "tsc -p tsconfig.json --noEmit",
18
+ "clean": "rm -rf dist"
19
+ },
20
+ "keywords": [],
21
+ "author": "",
22
+ "license": "ISC",
23
+ "type": "module",
24
+ "devDependencies": {
25
+ "@hlb/tooling": "workspace:*",
26
+ "@types/node": "catalog:",
27
+ "tsup": "catalog:",
28
+ "typescript": "catalog:"
29
+ }
30
+ }
package/src/client.ts ADDED
@@ -0,0 +1,29 @@
1
+ import { createHttpClient } from './http';
2
+ import type {
3
+ Category,
4
+ HelebbaClient,
5
+ HelebbaClientOptions,
6
+ InventoryBrand,
7
+ OffsetPaginatedResult,
8
+ Product,
9
+ } from './types';
10
+
11
+ export const createHelebbaClient = (options: HelebbaClientOptions): HelebbaClient => {
12
+ const http = createHttpClient(options);
13
+
14
+ return {
15
+ products: {
16
+ list: (params = {}) =>
17
+ http.get<OffsetPaginatedResult<Product>>('/sdk/products', { query: params }),
18
+ get: (productId) => http.get<Product>(`/sdk/products/${encodeURIComponent(productId)}`),
19
+ },
20
+ brands: {
21
+ list: (params = {}) =>
22
+ http.get<OffsetPaginatedResult<InventoryBrand>>('/sdk/brands', { query: params }),
23
+ },
24
+ categories: {
25
+ list: (params = {}) =>
26
+ http.get<OffsetPaginatedResult<Category>>('/sdk/categories', { query: params }),
27
+ },
28
+ };
29
+ };
package/src/http.ts ADDED
@@ -0,0 +1,81 @@
1
+ import type { HelebbaClientOptions, ListParams } from './types';
2
+
3
+ type RequestOptions = {
4
+ query?: ListParams;
5
+ };
6
+
7
+ export type HelebbaApiError = Error & {
8
+ status: number;
9
+ body: unknown;
10
+ };
11
+
12
+ export const createHelebbaApiError = (
13
+ message: string,
14
+ status: number,
15
+ body: unknown,
16
+ ): HelebbaApiError => {
17
+ const error = new Error(message) as HelebbaApiError;
18
+ error.name = 'HelebbaApiError';
19
+ error.status = status;
20
+ error.body = body;
21
+ return error;
22
+ };
23
+
24
+ const trimTrailingSlash = (value: string) => value.replace(/\/+$/, '');
25
+ const DEFAULT_BASE_URL = 'https://api.helebba.com/api/v1';
26
+
27
+ const appendQuery = (url: URL, query: ListParams | undefined) => {
28
+ if (!query) return;
29
+
30
+ if (query.page !== undefined) url.searchParams.set('page', String(query.page));
31
+ if (query.limit !== undefined) url.searchParams.set('limit', String(query.limit));
32
+ if (query.search) url.searchParams.set('search', query.search);
33
+ };
34
+
35
+ export type HttpClient = {
36
+ get: <T>(path: string, options?: RequestOptions) => Promise<T>;
37
+ };
38
+
39
+ export const createHttpClient = (options: HelebbaClientOptions): HttpClient => {
40
+ if (!options.apiKey) throw new Error('Helebba SDK requires apiKey');
41
+
42
+ const apiKey = options.apiKey;
43
+ const baseUrl = trimTrailingSlash(options.baseUrl ?? DEFAULT_BASE_URL);
44
+ const fetcher = options.fetcher ?? fetch;
45
+
46
+ return {
47
+ get: async <T>(path: string, options: RequestOptions = {}): Promise<T> => {
48
+ const url = new URL(`${baseUrl}${path}`);
49
+ appendQuery(url, options.query);
50
+
51
+ const timestamp = Date.now().toString();
52
+
53
+ const response = await fetcher(url, {
54
+ method: 'GET',
55
+ headers: {
56
+ 'api-key': apiKey,
57
+ 'x-timestamp': timestamp,
58
+ 'x-path': url.pathname,
59
+ 'x-client-user-agent': 'GSDK/0.1.0 (node)',
60
+ Accept: 'application/json',
61
+ },
62
+ });
63
+
64
+ const contentType = response.headers.get('content-type') ?? '';
65
+ const body = contentType.includes('application/json')
66
+ ? await response.json()
67
+ : await response.text();
68
+
69
+ if (!response.ok) {
70
+ const message =
71
+ typeof body === 'object' && body && 'message' in body
72
+ ? String((body as { message: unknown }).message)
73
+ : `Helebba API request failed with status ${response.status}`;
74
+
75
+ throw createHelebbaApiError(message, response.status, body);
76
+ }
77
+
78
+ return body as T;
79
+ },
80
+ };
81
+ };
package/src/index.ts ADDED
@@ -0,0 +1,12 @@
1
+ export { createHelebbaClient } from './client';
2
+ export { createHelebbaApiError } from './http';
3
+ export type { HelebbaApiError } from './http';
4
+ export type {
5
+ Category,
6
+ HelebbaClient,
7
+ HelebbaClientOptions,
8
+ InventoryBrand,
9
+ ListParams,
10
+ OffsetPaginatedResult,
11
+ Product,
12
+ } from './types';
package/src/types.ts ADDED
@@ -0,0 +1,61 @@
1
+ export type OffsetPageInfo = {
2
+ page: number;
3
+ pages: number;
4
+ pageSize: number;
5
+ totalItems: number;
6
+ hasPreviousPage: boolean;
7
+ hasNextPage: boolean;
8
+ previousPage: number | null;
9
+ nextPage: number | null;
10
+ };
11
+
12
+ export type OffsetPaginatedResult<T> = {
13
+ kind: 'offset';
14
+ count: number;
15
+ items: T[];
16
+ pageInfo: OffsetPageInfo;
17
+ };
18
+
19
+ export type ListParams = {
20
+ page?: number;
21
+ limit?: number;
22
+ search?: string;
23
+ };
24
+
25
+ export type HelebbaClientOptions = {
26
+ apiKey: string;
27
+ baseUrl?: string;
28
+ fetcher?: typeof fetch;
29
+ };
30
+
31
+ export type HelebbaClient = {
32
+ products: {
33
+ list: (params?: ListParams) => Promise<OffsetPaginatedResult<Product>>;
34
+ get: (productId: string) => Promise<Product>;
35
+ };
36
+ brands: {
37
+ list: (params?: ListParams) => Promise<OffsetPaginatedResult<InventoryBrand>>;
38
+ };
39
+ categories: {
40
+ list: (params?: ListParams) => Promise<OffsetPaginatedResult<Category>>;
41
+ };
42
+ };
43
+
44
+ export type Product = Record<string, unknown> & {
45
+ id: string;
46
+ name: string;
47
+ sku?: string;
48
+ description?: string;
49
+ };
50
+
51
+ export type InventoryBrand = Record<string, unknown> & {
52
+ id: string;
53
+ name: string;
54
+ description?: string;
55
+ };
56
+
57
+ export type Category = Record<string, unknown> & {
58
+ id: string;
59
+ name: string;
60
+ description?: string;
61
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "@hlb/tooling/tsconfig/base.json",
3
+ "compilerOptions": {
4
+ "rootDir": "./src",
5
+ "outDir": "./dist",
6
+ "lib": ["ES2022", "DOM"]
7
+ },
8
+ "include": ["src/**/*.ts"]
9
+ }
package/tsup.config.ts ADDED
@@ -0,0 +1,11 @@
1
+ import { defineConfig } from 'tsup';
2
+
3
+ export default defineConfig({
4
+ entry: ['src/index.ts'],
5
+ format: ['esm'],
6
+ outExtension: () => ({ js: '.mjs' }),
7
+ dts: true,
8
+ sourcemap: true,
9
+ clean: true,
10
+ target: 'es2022',
11
+ });
@@ -0,0 +1,11 @@
1
+ import { defineConfig } from 'tsup';
2
+
3
+ export default defineConfig({
4
+ entry: ['src/index.ts'],
5
+ format: ['esm'],
6
+ outExtension: () => ({ js: '.mjs' }),
7
+ dts: false,
8
+ sourcemap: true,
9
+ clean: true,
10
+ target: 'es2022',
11
+ });