@sales-planner/http-client 0.5.0 → 0.6.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.
Files changed (47) hide show
  1. package/README.md +91 -22
  2. package/dist/client.d.ts +1 -79
  3. package/dist/client.d.ts.map +1 -1
  4. package/dist/client.js +1 -330
  5. package/dist/clients/api-keys-client.d.ts +8 -0
  6. package/dist/clients/api-keys-client.d.ts.map +1 -0
  7. package/dist/clients/api-keys-client.js +12 -0
  8. package/dist/clients/base-client.d.ts +22 -0
  9. package/dist/clients/base-client.d.ts.map +1 -0
  10. package/dist/clients/base-client.js +80 -0
  11. package/dist/clients/import-export-base-client.d.ts +7 -0
  12. package/dist/clients/import-export-base-client.d.ts.map +1 -0
  13. package/dist/clients/import-export-base-client.js +46 -0
  14. package/dist/clients/index.d.ts +3 -0
  15. package/dist/clients/index.d.ts.map +1 -0
  16. package/dist/clients/index.js +2 -0
  17. package/dist/clients/marketplaces-client.d.ts +16 -0
  18. package/dist/clients/marketplaces-client.d.ts.map +1 -0
  19. package/dist/clients/marketplaces-client.js +36 -0
  20. package/dist/clients/me-client.d.ts +6 -0
  21. package/dist/clients/me-client.d.ts.map +1 -0
  22. package/dist/clients/me-client.js +6 -0
  23. package/dist/clients/roles-client.d.ts +10 -0
  24. package/dist/clients/roles-client.d.ts.map +1 -0
  25. package/dist/clients/roles-client.js +18 -0
  26. package/dist/clients/sales-history-client.d.ts +16 -0
  27. package/dist/clients/sales-history-client.d.ts.map +1 -0
  28. package/dist/clients/sales-history-client.js +36 -0
  29. package/dist/clients/sales-planner-client.d.ts +91 -0
  30. package/dist/clients/sales-planner-client.d.ts.map +1 -0
  31. package/dist/clients/sales-planner-client.js +114 -0
  32. package/dist/clients/shops-client.d.ts +11 -0
  33. package/dist/clients/shops-client.d.ts.map +1 -0
  34. package/dist/clients/shops-client.js +21 -0
  35. package/dist/clients/skus-client.d.ts +16 -0
  36. package/dist/clients/skus-client.d.ts.map +1 -0
  37. package/dist/clients/skus-client.js +36 -0
  38. package/dist/clients/tenants-client.d.ts +11 -0
  39. package/dist/clients/tenants-client.d.ts.map +1 -0
  40. package/dist/clients/tenants-client.js +21 -0
  41. package/dist/clients/user-roles-client.d.ts +7 -0
  42. package/dist/clients/user-roles-client.d.ts.map +1 -0
  43. package/dist/clients/user-roles-client.js +9 -0
  44. package/dist/clients/users-client.d.ts +9 -0
  45. package/dist/clients/users-client.d.ts.map +1 -0
  46. package/dist/clients/users-client.js +15 -0
  47. package/package.json +2 -2
package/README.md CHANGED
@@ -1,51 +1,120 @@
1
1
  # @sales-planner/http-client
2
2
 
3
- Type-safe HTTP client for the Sales Planner API.
3
+ TypeScript HTTP client for the Sales Planner API with full type safety.
4
4
 
5
5
  ## Installation
6
6
 
7
7
  ```bash
8
- npm install @sales-planner/http-client
9
- # or
10
8
  pnpm add @sales-planner/http-client
11
9
  ```
12
10
 
13
11
  ## Usage
14
12
 
13
+ ### Quick Start
14
+
15
15
  ```typescript
16
16
  import { SalesPlannerClient } from '@sales-planner/http-client';
17
17
 
18
18
  const client = new SalesPlannerClient({
19
19
  baseUrl: 'https://sales-planner-back.vercel.app',
20
- apiKey: 'your-api-key'
20
+ getAuthToken: () => 'your-api-key',
21
21
  });
22
22
 
23
- // Get current user
24
- const me = await client.getMe();
23
+ // Check if API is healthy
24
+ const health = await client.getHealth();
25
+ console.log(health); // { status: 'ok', version: '1.0.0' }
26
+ ```
27
+
28
+ ### API Styles
25
29
 
26
- // Get shops
27
- const shops = await client.getShops();
30
+ The client supports two complementary API styles:
28
31
 
29
- // Get SKUs for a shop
30
- const skus = await client.getSkus({ shop_id: 1, tenant_id: 1 });
32
+ #### 1. **Namespaced API** (Recommended)
31
33
 
32
- // Create API key (key is auto-generated)
33
- const apiKey = await client.createApiKey({
34
- user_id: 1,
35
- name: 'Production Key',
36
- expires_at: '2026-12-31T23:59:59Z'
37
- });
34
+ Access resources through domain-specific sub-clients:
38
35
 
39
- // Import sales history (marketplace field required)
40
- const result = await client.importSalesHistoryJson([
41
- { sku_code: 'SKU-001', period: '2026-01', quantity: 100, marketplace: 'WB' }
42
- ], { shop_id: 1, tenant_id: 1 });
36
+ ```typescript
37
+ // Users
38
+ const users = await client.users.getUsers();
39
+ const user = await client.users.getUser(1);
40
+
41
+ // Tenants & Shops
42
+ const tenants = await client.tenants.getTenants();
43
+ const shops = await client.shops.getShops(tenantId);
44
+
45
+ // SKUs with import/export
46
+ const skus = await client.skus.getSkus({ tenantId, shopId });
47
+ await client.skus.importSkusJson(items, { tenantId, shopId });
48
+ const csv = await client.skus.exportSkusCsv({ tenantId, shopId });
49
+
50
+ // Sales History
51
+ const history = await client.salesHistory.getSalesHistory(
52
+ { tenantId, shopId },
53
+ { start: '2024-01', end: '2024-12' }
54
+ );
55
+
56
+ // Marketplaces
57
+ const marketplaces = await client.marketplaces.getMarketplaces({ tenantId });
58
+ ```
59
+
60
+ **Benefits:**
61
+ - Clear domain separation
62
+ - IDE autocomplete by domain
63
+ - Easier to discover related methods
64
+
65
+ #### 2. **Flat API** (Backward Compatible)
66
+
67
+ Access all methods directly on the client:
68
+
69
+ ```typescript
70
+ // Backward compatible with existing code
71
+ const users = await client.getUsers();
72
+ const user = await client.getUser(1);
73
+ const skus = await client.getSkus({ tenantId, shopId });
74
+ ```
75
+
76
+ ## Import/Export Pattern
77
+
78
+ Resources that support bulk operations (SKUs, Sales History, Marketplaces) follow a consistent pattern:
79
+
80
+ ```typescript
81
+ // Import from JSON
82
+ const result = await client.skus.importSkusJson(
83
+ [
84
+ { code: 'SKU-001', title: 'Product 1' },
85
+ { code: 'SKU-002', title: 'Product 2' },
86
+ ],
87
+ { tenantId, shopId }
88
+ );
89
+ // Returns: { inserted: 2, updated: 0, errors: [] }
90
+
91
+ // Import from CSV file
92
+ const csvContent = await fs.readFile('skus.csv', 'utf-8');
93
+ const result = await client.skus.importSkusCsv(csvContent, { tenantId, shopId });
94
+
95
+ // Export to CSV
96
+ const csv = await client.skus.exportSkusCsv({ tenantId, shopId });
97
+
98
+ // Get example templates
99
+ const exampleCsv = await client.skus.getSkusExampleCsv();
43
100
  ```
44
101
 
45
102
  ## Error Handling
46
103
 
47
- The API returns standard HTTP status codes:
48
- - `409 Conflict` - Duplicate resource (e.g., duplicate email, SKU code, sales period)
104
+ ```typescript
105
+ import { ApiError } from '@sales-planner/http-client';
106
+
107
+ try {
108
+ await client.users.getUser(999);
109
+ } catch (error) {
110
+ if (error instanceof ApiError) {
111
+ console.error(`API Error ${error.status}: ${error.message}`);
112
+ }
113
+ }
114
+ ```
115
+
116
+ Common HTTP status codes:
117
+ - `409 Conflict` - Duplicate resource (email, SKU code, sales period)
49
118
  - `404 Not Found` - Resource not found
50
119
  - `403 Forbidden` - Insufficient permissions
51
120
  - `400 Bad Request` - Validation error
package/dist/client.d.ts CHANGED
@@ -1,80 +1,2 @@
1
- import type { ApiKey, Marketplace, Role, SalesHistory, Shop, Sku, Tenant, User, CreateApiKeyDto, CreateMarketplaceRequest, CreateRoleDto, CreateSalesHistoryRequest, CreateShopDto, CreateSkuRequest, CreateTenantDto, CreateTenantWithShopDto, CreateUserDto, CreateUserRoleDto, ImportSalesHistoryItem, ImportSkuItem, UpdateSalesHistoryDto, UpdateSkuDto, DeleteDataResult, ImportResult, SalesHistoryExportItem, SkuExportItem, TenantWithShopAndApiKey, UserWithRolesAndTenants, PeriodQuery, ShopContextParams } from '@sales-planner/shared';
2
- export interface ClientConfig {
3
- baseUrl: string;
4
- apiKey: string;
5
- }
6
- export declare class SalesPlannerClient {
7
- private baseUrl;
8
- private apiKey;
9
- constructor(config: ClientConfig);
10
- private handleErrorResponse;
11
- private request;
12
- private requestText;
13
- private uploadCsv;
14
- private requestPublic;
15
- private requestTextPublic;
16
- getMe(): Promise<UserWithRolesAndTenants>;
17
- getUsers(): Promise<User[]>;
18
- getUser(id: number): Promise<User>;
19
- createUser(dto: CreateUserDto): Promise<User>;
20
- deleteUser(id: number): Promise<void>;
21
- getTenants(): Promise<Tenant[]>;
22
- getTenant(id: number): Promise<Tenant>;
23
- createTenant(dto: CreateTenantDto): Promise<Tenant>;
24
- createTenantWithShopAndUser(dto: CreateTenantWithShopDto): Promise<TenantWithShopAndApiKey>;
25
- updateTenant(id: number, dto: Partial<CreateTenantDto>): Promise<Tenant>;
26
- deleteTenant(id: number): Promise<void>;
27
- getShops(tenantId?: number): Promise<Shop[]>;
28
- getShop(id: number): Promise<Shop>;
29
- createShop(dto: CreateShopDto): Promise<Shop>;
30
- updateShop(id: number, dto: Partial<CreateShopDto>): Promise<Shop>;
31
- deleteShop(id: number): Promise<void>;
32
- deleteShopData(id: number): Promise<DeleteDataResult>;
33
- getSkus(ctx: ShopContextParams): Promise<Sku[]>;
34
- getSku(id: number, ctx: ShopContextParams): Promise<Sku>;
35
- createSku(dto: CreateSkuRequest, ctx: ShopContextParams): Promise<Sku>;
36
- updateSku(id: number, dto: UpdateSkuDto, ctx: ShopContextParams): Promise<Sku>;
37
- deleteSku(id: number, ctx: ShopContextParams): Promise<void>;
38
- importSkusJson(items: ImportSkuItem[], ctx: ShopContextParams): Promise<ImportResult>;
39
- importSkusCsv(csvContent: string, ctx: ShopContextParams): Promise<ImportResult>;
40
- exportSkusJson(ctx: ShopContextParams): Promise<SkuExportItem[]>;
41
- exportSkusCsv(ctx: ShopContextParams): Promise<string>;
42
- getSkusExampleJson(): Promise<ImportSkuItem[]>;
43
- getSkusExampleCsv(): Promise<string>;
44
- getSalesHistory(ctx: ShopContextParams, query?: PeriodQuery): Promise<SalesHistory[]>;
45
- getSalesHistoryItem(id: number, ctx: ShopContextParams): Promise<SalesHistory>;
46
- createSalesHistory(dto: CreateSalesHistoryRequest, ctx: ShopContextParams): Promise<SalesHistory>;
47
- updateSalesHistory(id: number, dto: UpdateSalesHistoryDto, ctx: ShopContextParams): Promise<SalesHistory>;
48
- deleteSalesHistory(id: number, ctx: ShopContextParams): Promise<void>;
49
- importSalesHistoryJson(items: ImportSalesHistoryItem[], ctx: ShopContextParams): Promise<ImportResult>;
50
- importSalesHistoryCsv(csvContent: string, ctx: ShopContextParams): Promise<ImportResult>;
51
- exportSalesHistoryJson(ctx: ShopContextParams, query?: PeriodQuery): Promise<SalesHistoryExportItem[]>;
52
- exportSalesHistoryCsv(ctx: ShopContextParams, query?: PeriodQuery): Promise<string>;
53
- getSalesHistoryExampleJson(): Promise<ImportSalesHistoryItem[]>;
54
- getSalesHistoryExampleCsv(): Promise<string>;
55
- getRoles(): Promise<Role[]>;
56
- getRole(id: number): Promise<Role>;
57
- createRole(dto: CreateRoleDto): Promise<Role>;
58
- updateRole(id: number, dto: Partial<CreateRoleDto>): Promise<Role>;
59
- deleteRole(id: number): Promise<void>;
60
- createUserRole(dto: CreateUserRoleDto): Promise<void>;
61
- deleteUserRole(id: number): Promise<void>;
62
- getApiKeys(userId?: number): Promise<ApiKey[]>;
63
- createApiKey(dto: CreateApiKeyDto): Promise<ApiKey>;
64
- deleteApiKey(id: number): Promise<void>;
65
- getMarketplaces(ctx: ShopContextParams): Promise<Marketplace[]>;
66
- getMarketplace(id: string, ctx: ShopContextParams): Promise<Marketplace>;
67
- createMarketplace(dto: CreateMarketplaceRequest, ctx: ShopContextParams): Promise<Marketplace>;
68
- updateMarketplace(id: string, dto: Partial<CreateMarketplaceRequest>, ctx: ShopContextParams): Promise<Marketplace>;
69
- deleteMarketplace(id: string, ctx: ShopContextParams): Promise<void>;
70
- getRoot(): Promise<string>;
71
- getHealth(): Promise<{
72
- status: string;
73
- version: string;
74
- }>;
75
- }
76
- export declare class ApiError extends Error {
77
- status: number;
78
- constructor(status: number, message: string);
79
- }
1
+ export { SalesPlannerClient, ApiError, type ClientConfig } from './clients/index.js';
80
2
  //# sourceMappingURL=client.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,MAAM,EACN,WAAW,EACX,IAAI,EACJ,YAAY,EACZ,IAAI,EACJ,GAAG,EACH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GAAG,EAAE,iBAAiB,GAAG,OAAO,CAAC,IAAI,CAAC;IAMpE,OAAO,IAAI,OAAO,CAAC,MAAM,CAAC;IAQ1B,SAAS,IAAI,OAAO,CAAC;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC;CAOhE;AAED,qBAAa,QAAS,SAAQ,KAAK;IAExB,MAAM,EAAE,MAAM;gBAAd,MAAM,EAAE,MAAM,EACrB,OAAO,EAAE,MAAM;CAKlB"}
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,QAAQ,EAAE,KAAK,YAAY,EAAE,MAAM,oBAAoB,CAAC"}
package/dist/client.js CHANGED
@@ -1,330 +1 @@
1
- export class SalesPlannerClient {
2
- baseUrl;
3
- apiKey;
4
- constructor(config) {
5
- this.baseUrl = config.baseUrl.replace(/\/$/, '');
6
- this.apiKey = config.apiKey;
7
- }
8
- async handleErrorResponse(response) {
9
- const text = await response.text();
10
- let message = response.statusText;
11
- if (text) {
12
- try {
13
- const error = JSON.parse(text);
14
- message = error.message || message;
15
- }
16
- catch {
17
- message = text;
18
- }
19
- }
20
- throw new ApiError(response.status, message || 'Request failed');
21
- }
22
- async request(method, path, options) {
23
- const url = new URL(this.baseUrl + path);
24
- if (options?.params) {
25
- for (const [key, value] of Object.entries(options.params)) {
26
- if (value !== undefined) {
27
- url.searchParams.set(key, String(value));
28
- }
29
- }
30
- }
31
- const response = await fetch(url.toString(), {
32
- method,
33
- headers: {
34
- 'Content-Type': 'application/json',
35
- 'X-API-Key': this.apiKey,
36
- },
37
- body: options?.body ? JSON.stringify(options.body) : undefined,
38
- });
39
- if (!response.ok) {
40
- await this.handleErrorResponse(response);
41
- }
42
- if (response.status === 204) {
43
- return undefined;
44
- }
45
- // Handle empty response body (e.g., void return type from NestJS)
46
- const text = await response.text();
47
- if (!text) {
48
- return undefined;
49
- }
50
- return JSON.parse(text);
51
- }
52
- async requestText(method, path, options) {
53
- const url = new URL(this.baseUrl + path);
54
- if (options?.params) {
55
- for (const [key, value] of Object.entries(options.params)) {
56
- if (value !== undefined) {
57
- url.searchParams.set(key, String(value));
58
- }
59
- }
60
- }
61
- const response = await fetch(url.toString(), {
62
- method,
63
- headers: {
64
- 'X-API-Key': this.apiKey,
65
- },
66
- });
67
- if (!response.ok) {
68
- await this.handleErrorResponse(response);
69
- }
70
- return response.text();
71
- }
72
- async uploadCsv(path, csvContent, params) {
73
- const url = new URL(this.baseUrl + path);
74
- for (const [key, value] of Object.entries(params)) {
75
- if (value !== undefined) {
76
- url.searchParams.set(key, String(value));
77
- }
78
- }
79
- const formData = new FormData();
80
- const blob = new Blob([csvContent], { type: 'text/csv' });
81
- formData.append('file', blob, 'upload.csv');
82
- const response = await fetch(url.toString(), {
83
- method: 'POST',
84
- headers: {
85
- 'X-API-Key': this.apiKey,
86
- },
87
- body: formData,
88
- });
89
- if (!response.ok) {
90
- await this.handleErrorResponse(response);
91
- }
92
- return response.json();
93
- }
94
- async requestPublic(method, path) {
95
- const url = new URL(this.baseUrl + path);
96
- const response = await fetch(url.toString(), {
97
- method,
98
- headers: {
99
- 'Content-Type': 'application/json',
100
- },
101
- });
102
- if (!response.ok) {
103
- await this.handleErrorResponse(response);
104
- }
105
- return response.json();
106
- }
107
- async requestTextPublic(method, path) {
108
- const url = new URL(this.baseUrl + path);
109
- const response = await fetch(url.toString(), { method });
110
- if (!response.ok) {
111
- await this.handleErrorResponse(response);
112
- }
113
- return response.text();
114
- }
115
- // ============================================================
116
- // Me
117
- // ============================================================
118
- async getMe() {
119
- return this.request('GET', '/me');
120
- }
121
- // ============================================================
122
- // Users
123
- // ============================================================
124
- async getUsers() {
125
- return this.request('GET', '/users');
126
- }
127
- async getUser(id) {
128
- return this.request('GET', `/users/${id}`);
129
- }
130
- async createUser(dto) {
131
- return this.request('POST', '/users', { body: dto });
132
- }
133
- async deleteUser(id) {
134
- return this.request('DELETE', `/users/${id}`);
135
- }
136
- // ============================================================
137
- // Tenants
138
- // ============================================================
139
- async getTenants() {
140
- return this.request('GET', '/tenants');
141
- }
142
- async getTenant(id) {
143
- return this.request('GET', `/tenants/${id}`);
144
- }
145
- async createTenant(dto) {
146
- return this.request('POST', '/tenants', { body: dto });
147
- }
148
- async createTenantWithShopAndUser(dto) {
149
- return this.request('POST', '/tenants/with-shop-and-user', { body: dto });
150
- }
151
- async updateTenant(id, dto) {
152
- return this.request('PUT', `/tenants/${id}`, { body: dto });
153
- }
154
- async deleteTenant(id) {
155
- return this.request('DELETE', `/tenants/${id}`);
156
- }
157
- // ============================================================
158
- // Shops
159
- // ============================================================
160
- async getShops(tenantId) {
161
- return this.request('GET', '/shops', { params: { tenantId } });
162
- }
163
- async getShop(id) {
164
- return this.request('GET', `/shops/${id}`);
165
- }
166
- async createShop(dto) {
167
- return this.request('POST', '/shops', { body: dto });
168
- }
169
- async updateShop(id, dto) {
170
- return this.request('PUT', `/shops/${id}`, { body: dto });
171
- }
172
- async deleteShop(id) {
173
- return this.request('DELETE', `/shops/${id}`);
174
- }
175
- async deleteShopData(id) {
176
- return this.request('DELETE', `/shops/${id}/data`);
177
- }
178
- // ============================================================
179
- // SKUs
180
- // ============================================================
181
- async getSkus(ctx) {
182
- return this.request('GET', '/skus', { params: ctx });
183
- }
184
- async getSku(id, ctx) {
185
- return this.request('GET', `/skus/${id}`, { params: ctx });
186
- }
187
- async createSku(dto, ctx) {
188
- return this.request('POST', '/skus', { body: dto, params: ctx });
189
- }
190
- async updateSku(id, dto, ctx) {
191
- return this.request('PUT', `/skus/${id}`, { body: dto, params: ctx });
192
- }
193
- async deleteSku(id, ctx) {
194
- return this.request('DELETE', `/skus/${id}`, { params: ctx });
195
- }
196
- async importSkusJson(items, ctx) {
197
- return this.request('POST', '/skus/import/json', { body: items, params: ctx });
198
- }
199
- async importSkusCsv(csvContent, ctx) {
200
- return this.uploadCsv('/skus/import/csv', csvContent, ctx);
201
- }
202
- async exportSkusJson(ctx) {
203
- return this.request('GET', '/skus/export/json', { params: ctx });
204
- }
205
- async exportSkusCsv(ctx) {
206
- return this.requestText('GET', '/skus/export/csv', { params: ctx });
207
- }
208
- async getSkusExampleJson() {
209
- return this.requestPublic('GET', '/skus/examples/json');
210
- }
211
- async getSkusExampleCsv() {
212
- return this.requestTextPublic('GET', '/skus/examples/csv');
213
- }
214
- // ============================================================
215
- // Sales History
216
- // ============================================================
217
- async getSalesHistory(ctx, query) {
218
- return this.request('GET', '/sales-history', { params: { ...ctx, ...query } });
219
- }
220
- async getSalesHistoryItem(id, ctx) {
221
- return this.request('GET', `/sales-history/${id}`, { params: ctx });
222
- }
223
- async createSalesHistory(dto, ctx) {
224
- return this.request('POST', '/sales-history', { body: dto, params: ctx });
225
- }
226
- async updateSalesHistory(id, dto, ctx) {
227
- return this.request('PUT', `/sales-history/${id}`, { body: dto, params: ctx });
228
- }
229
- async deleteSalesHistory(id, ctx) {
230
- return this.request('DELETE', `/sales-history/${id}`, { params: ctx });
231
- }
232
- async importSalesHistoryJson(items, ctx) {
233
- return this.request('POST', '/sales-history/import/json', { body: items, params: ctx });
234
- }
235
- async importSalesHistoryCsv(csvContent, ctx) {
236
- return this.uploadCsv('/sales-history/import/csv', csvContent, ctx);
237
- }
238
- async exportSalesHistoryJson(ctx, query) {
239
- return this.request('GET', '/sales-history/export/json', { params: { ...ctx, ...query } });
240
- }
241
- async exportSalesHistoryCsv(ctx, query) {
242
- return this.requestText('GET', '/sales-history/export/csv', { params: { ...ctx, ...query } });
243
- }
244
- async getSalesHistoryExampleJson() {
245
- return this.requestPublic('GET', '/sales-history/examples/json');
246
- }
247
- async getSalesHistoryExampleCsv() {
248
- return this.requestTextPublic('GET', '/sales-history/examples/csv');
249
- }
250
- // ============================================================
251
- // Roles
252
- // ============================================================
253
- async getRoles() {
254
- return this.request('GET', '/roles');
255
- }
256
- async getRole(id) {
257
- return this.request('GET', `/roles/${id}`);
258
- }
259
- async createRole(dto) {
260
- return this.request('POST', '/roles', { body: dto });
261
- }
262
- async updateRole(id, dto) {
263
- return this.request('PUT', `/roles/${id}`, { body: dto });
264
- }
265
- async deleteRole(id) {
266
- return this.request('DELETE', `/roles/${id}`);
267
- }
268
- // ============================================================
269
- // User Roles
270
- // ============================================================
271
- async createUserRole(dto) {
272
- return this.request('POST', '/user-roles', { body: dto });
273
- }
274
- async deleteUserRole(id) {
275
- return this.request('DELETE', `/user-roles/${id}`);
276
- }
277
- // ============================================================
278
- // API Keys
279
- // ============================================================
280
- async getApiKeys(userId) {
281
- return this.request('GET', '/api-keys', { params: { user_id: userId } });
282
- }
283
- async createApiKey(dto) {
284
- return this.request('POST', '/api-keys', { body: dto });
285
- }
286
- async deleteApiKey(id) {
287
- return this.request('DELETE', `/api-keys/${id}`);
288
- }
289
- // ============================================================
290
- // Marketplaces
291
- // ============================================================
292
- async getMarketplaces(ctx) {
293
- return this.request('GET', '/marketplaces', { params: ctx });
294
- }
295
- async getMarketplace(id, ctx) {
296
- return this.request('GET', `/marketplaces/${id}`, { params: ctx });
297
- }
298
- async createMarketplace(dto, ctx) {
299
- return this.request('POST', '/marketplaces', { body: dto, params: ctx });
300
- }
301
- async updateMarketplace(id, dto, ctx) {
302
- return this.request('PUT', `/marketplaces/${id}`, { body: dto, params: ctx });
303
- }
304
- async deleteMarketplace(id, ctx) {
305
- return this.request('DELETE', `/marketplaces/${id}`, { params: ctx });
306
- }
307
- // Health & Info (unauthenticated)
308
- async getRoot() {
309
- const response = await fetch(`${this.baseUrl}/`);
310
- if (!response.ok) {
311
- throw new ApiError(response.status, response.statusText);
312
- }
313
- return response.text();
314
- }
315
- async getHealth() {
316
- const response = await fetch(`${this.baseUrl}/health`);
317
- if (!response.ok) {
318
- throw new ApiError(response.status, response.statusText);
319
- }
320
- return response.json();
321
- }
322
- }
323
- export class ApiError extends Error {
324
- status;
325
- constructor(status, message) {
326
- super(message);
327
- this.status = status;
328
- this.name = 'ApiError';
329
- }
330
- }
1
+ export { SalesPlannerClient, ApiError } from './clients/index.js';
@@ -0,0 +1,8 @@
1
+ import type { ApiKey, CreateApiKeyDto } from '@sales-planner/shared';
2
+ import { BaseClient } from './base-client.js';
3
+ export declare class ApiKeysClient extends BaseClient {
4
+ getApiKeys(userId?: number): Promise<ApiKey[]>;
5
+ createApiKey(dto: CreateApiKeyDto): Promise<ApiKey>;
6
+ deleteApiKey(id: number): Promise<void>;
7
+ }
8
+ //# sourceMappingURL=api-keys-client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api-keys-client.d.ts","sourceRoot":"","sources":["../../src/clients/api-keys-client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACrE,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE9C,qBAAa,aAAc,SAAQ,UAAU;IACrC,UAAU,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAI9C,YAAY,CAAC,GAAG,EAAE,eAAe,GAAG,OAAO,CAAC,MAAM,CAAC;IAInD,YAAY,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAG9C"}
@@ -0,0 +1,12 @@
1
+ import { BaseClient } from './base-client.js';
2
+ export class ApiKeysClient extends BaseClient {
3
+ async getApiKeys(userId) {
4
+ return this.request('GET', '/api-keys', { params: { user_id: userId } });
5
+ }
6
+ async createApiKey(dto) {
7
+ return this.request('POST', '/api-keys', { body: dto });
8
+ }
9
+ async deleteApiKey(id) {
10
+ return this.request('DELETE', `/api-keys/${id}`);
11
+ }
12
+ }
@@ -0,0 +1,22 @@
1
+ export declare class ApiError extends Error {
2
+ status: number;
3
+ constructor(status: number, message: string);
4
+ }
5
+ export interface ClientConfig {
6
+ baseUrl: string;
7
+ apiKey: string;
8
+ }
9
+ export declare class BaseClient {
10
+ protected baseUrl: string;
11
+ protected apiKey: string;
12
+ constructor(config: ClientConfig);
13
+ protected handleErrorResponse(response: Response): Promise<never>;
14
+ protected request<T>(method: string, path: string, options?: {
15
+ body?: unknown;
16
+ params?: Record<string, string | number | undefined>;
17
+ }): Promise<T>;
18
+ protected requestText(method: string, path: string, options?: {
19
+ params?: Record<string, string | number | undefined>;
20
+ }): Promise<string>;
21
+ }
22
+ //# sourceMappingURL=base-client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base-client.d.ts","sourceRoot":"","sources":["../../src/clients/base-client.ts"],"names":[],"mappings":"AAAA,qBAAa,QAAS,SAAQ,KAAK;IAExB,MAAM,EAAE,MAAM;gBAAd,MAAM,EAAE,MAAM,EACrB,OAAO,EAAE,MAAM;CAKlB;AAED,MAAM,WAAW,YAAY;IAC3B,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,qBAAa,UAAU;IACrB,SAAS,CAAC,OAAO,EAAE,MAAM,CAAC;IAC1B,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC;gBAEb,MAAM,EAAE,YAAY;cAKhB,mBAAmB,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO,CAAC,KAAK,CAAC;cAcvD,OAAO,CAAC,CAAC,EACvB,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE;QACR,IAAI,CAAC,EAAE,OAAO,CAAC;QACf,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC,CAAC;KACtD,GACA,OAAO,CAAC,CAAC,CAAC;cAqCG,WAAW,CACzB,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE;QACR,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC,CAAC;KACtD,GACA,OAAO,CAAC,MAAM,CAAC;CAwBnB"}