@sales-planner/http-client 0.4.2 → 0.5.1
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 +25 -0
- package/dist/client.d.ts +1 -79
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +1 -330
- package/dist/clients/api-keys-client.d.ts +8 -0
- package/dist/clients/api-keys-client.d.ts.map +1 -0
- package/dist/clients/api-keys-client.js +12 -0
- package/dist/clients/base-client.d.ts +22 -0
- package/dist/clients/base-client.d.ts.map +1 -0
- package/dist/clients/base-client.js +80 -0
- package/dist/clients/import-export-base-client.d.ts +7 -0
- package/dist/clients/import-export-base-client.d.ts.map +1 -0
- package/dist/clients/import-export-base-client.js +46 -0
- package/dist/clients/index.d.ts +3 -0
- package/dist/clients/index.d.ts.map +1 -0
- package/dist/clients/index.js +2 -0
- package/dist/clients/marketplaces-client.d.ts +16 -0
- package/dist/clients/marketplaces-client.d.ts.map +1 -0
- package/dist/clients/marketplaces-client.js +36 -0
- package/dist/clients/me-client.d.ts +6 -0
- package/dist/clients/me-client.d.ts.map +1 -0
- package/dist/clients/me-client.js +6 -0
- package/dist/clients/roles-client.d.ts +10 -0
- package/dist/clients/roles-client.d.ts.map +1 -0
- package/dist/clients/roles-client.js +18 -0
- package/dist/clients/sales-history-client.d.ts +16 -0
- package/dist/clients/sales-history-client.d.ts.map +1 -0
- package/dist/clients/sales-history-client.js +36 -0
- package/dist/clients/sales-planner-client.d.ts +91 -0
- package/dist/clients/sales-planner-client.d.ts.map +1 -0
- package/dist/clients/sales-planner-client.js +174 -0
- package/dist/clients/shops-client.d.ts +11 -0
- package/dist/clients/shops-client.d.ts.map +1 -0
- package/dist/clients/shops-client.js +21 -0
- package/dist/clients/skus-client.d.ts +16 -0
- package/dist/clients/skus-client.d.ts.map +1 -0
- package/dist/clients/skus-client.js +36 -0
- package/dist/clients/tenants-client.d.ts +11 -0
- package/dist/clients/tenants-client.d.ts.map +1 -0
- package/dist/clients/tenants-client.js +21 -0
- package/dist/clients/user-roles-client.d.ts +7 -0
- package/dist/clients/user-roles-client.d.ts.map +1 -0
- package/dist/clients/user-roles-client.js +9 -0
- package/dist/clients/users-client.d.ts +9 -0
- package/dist/clients/users-client.d.ts.map +1 -0
- package/dist/clients/users-client.js +15 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -29,12 +29,37 @@ const shops = await client.getShops();
|
|
|
29
29
|
// Get SKUs for a shop
|
|
30
30
|
const skus = await client.getSkus({ shop_id: 1, tenant_id: 1 });
|
|
31
31
|
|
|
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
|
+
});
|
|
38
|
+
|
|
32
39
|
// Import sales history (marketplace field required)
|
|
33
40
|
const result = await client.importSalesHistoryJson([
|
|
34
41
|
{ sku_code: 'SKU-001', period: '2026-01', quantity: 100, marketplace: 'WB' }
|
|
35
42
|
], { shop_id: 1, tenant_id: 1 });
|
|
36
43
|
```
|
|
37
44
|
|
|
45
|
+
## Error Handling
|
|
46
|
+
|
|
47
|
+
The API returns standard HTTP status codes:
|
|
48
|
+
- `409 Conflict` - Duplicate resource (e.g., duplicate email, SKU code, sales period)
|
|
49
|
+
- `404 Not Found` - Resource not found
|
|
50
|
+
- `403 Forbidden` - Insufficient permissions
|
|
51
|
+
- `400 Bad Request` - Validation error
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
try {
|
|
55
|
+
await client.createUser({ email: 'user@example.com', name: 'John' });
|
|
56
|
+
} catch (error) {
|
|
57
|
+
if (error.response?.status === 409) {
|
|
58
|
+
console.error('User with this email already exists');
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
38
63
|
## Available Methods
|
|
39
64
|
|
|
40
65
|
### Authentication
|
package/dist/client.d.ts
CHANGED
|
@@ -1,80 +1,2 @@
|
|
|
1
|
-
|
|
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: Omit<CreateSkuDto, 'shop_id' | 'tenant_id'>, 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: Omit<CreateSalesHistoryDto, 'shop_id' | 'tenant_id'>, 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(): Promise<Marketplace[]>;
|
|
66
|
-
getMarketplace(id: string): Promise<Marketplace>;
|
|
67
|
-
createMarketplace(dto: CreateMarketplaceDto): Promise<Marketplace>;
|
|
68
|
-
updateMarketplace(id: string, dto: Partial<CreateMarketplaceDto>): Promise<Marketplace>;
|
|
69
|
-
deleteMarketplace(id: string): 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
|
package/dist/client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
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
|
|
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() {
|
|
293
|
-
return this.request('GET', '/marketplaces');
|
|
294
|
-
}
|
|
295
|
-
async getMarketplace(id) {
|
|
296
|
-
return this.request('GET', `/marketplaces/${id}`);
|
|
297
|
-
}
|
|
298
|
-
async createMarketplace(dto) {
|
|
299
|
-
return this.request('POST', '/marketplaces', { body: dto });
|
|
300
|
-
}
|
|
301
|
-
async updateMarketplace(id, dto) {
|
|
302
|
-
return this.request('PUT', `/marketplaces/${id}`, { body: dto });
|
|
303
|
-
}
|
|
304
|
-
async deleteMarketplace(id) {
|
|
305
|
-
return this.request('DELETE', `/marketplaces/${id}`);
|
|
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"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
export class ApiError extends Error {
|
|
2
|
+
status;
|
|
3
|
+
constructor(status, message) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.status = status;
|
|
6
|
+
this.name = 'ApiError';
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export class BaseClient {
|
|
10
|
+
baseUrl;
|
|
11
|
+
apiKey;
|
|
12
|
+
constructor(config) {
|
|
13
|
+
this.baseUrl = config.baseUrl.replace(/\/$/, '');
|
|
14
|
+
this.apiKey = config.apiKey;
|
|
15
|
+
}
|
|
16
|
+
async handleErrorResponse(response) {
|
|
17
|
+
const text = await response.text();
|
|
18
|
+
let message = response.statusText;
|
|
19
|
+
if (text) {
|
|
20
|
+
try {
|
|
21
|
+
const error = JSON.parse(text);
|
|
22
|
+
message = error.message || message;
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
message = text;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
throw new ApiError(response.status, message || 'Request failed');
|
|
29
|
+
}
|
|
30
|
+
async request(method, path, options) {
|
|
31
|
+
const url = new URL(this.baseUrl + path);
|
|
32
|
+
if (options?.params) {
|
|
33
|
+
for (const [key, value] of Object.entries(options.params)) {
|
|
34
|
+
if (value !== undefined) {
|
|
35
|
+
url.searchParams.set(key, String(value));
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const response = await fetch(url.toString(), {
|
|
40
|
+
method,
|
|
41
|
+
headers: {
|
|
42
|
+
'Content-Type': 'application/json',
|
|
43
|
+
'X-API-Key': this.apiKey,
|
|
44
|
+
},
|
|
45
|
+
body: options?.body ? JSON.stringify(options.body) : undefined,
|
|
46
|
+
});
|
|
47
|
+
if (!response.ok) {
|
|
48
|
+
await this.handleErrorResponse(response);
|
|
49
|
+
}
|
|
50
|
+
if (response.status === 204) {
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
// Handle empty response body (e.g., void return type from NestJS)
|
|
54
|
+
const text = await response.text();
|
|
55
|
+
if (!text) {
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
return JSON.parse(text);
|
|
59
|
+
}
|
|
60
|
+
async requestText(method, path, options) {
|
|
61
|
+
const url = new URL(this.baseUrl + path);
|
|
62
|
+
if (options?.params) {
|
|
63
|
+
for (const [key, value] of Object.entries(options.params)) {
|
|
64
|
+
if (value !== undefined) {
|
|
65
|
+
url.searchParams.set(key, String(value));
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
const response = await fetch(url.toString(), {
|
|
70
|
+
method,
|
|
71
|
+
headers: {
|
|
72
|
+
'X-API-Key': this.apiKey,
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
if (!response.ok) {
|
|
76
|
+
await this.handleErrorResponse(response);
|
|
77
|
+
}
|
|
78
|
+
return response.text();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { BaseClient } from './base-client.js';
|
|
2
|
+
export declare class ImportExportBaseClient extends BaseClient {
|
|
3
|
+
protected uploadCsv<T>(path: string, csvContent: string, params: Record<string, string | number | undefined>): Promise<T>;
|
|
4
|
+
protected requestPublic<T>(method: string, path: string): Promise<T>;
|
|
5
|
+
protected requestTextPublic(method: string, path: string): Promise<string>;
|
|
6
|
+
}
|
|
7
|
+
//# sourceMappingURL=import-export-base-client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"import-export-base-client.d.ts","sourceRoot":"","sources":["../../src/clients/import-export-base-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE9C,qBAAa,sBAAuB,SAAQ,UAAU;cACpC,SAAS,CAAC,CAAC,EACzB,IAAI,EAAE,MAAM,EACZ,UAAU,EAAE,MAAM,EAClB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC,GAClD,OAAO,CAAC,CAAC,CAAC;cA4BG,aAAa,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC;cAiB1D,iBAAiB,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;CAWjF"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { BaseClient } from './base-client.js';
|
|
2
|
+
export class ImportExportBaseClient extends BaseClient {
|
|
3
|
+
async uploadCsv(path, csvContent, params) {
|
|
4
|
+
const url = new URL(this.baseUrl + path);
|
|
5
|
+
for (const [key, value] of Object.entries(params)) {
|
|
6
|
+
if (value !== undefined) {
|
|
7
|
+
url.searchParams.set(key, String(value));
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
const formData = new FormData();
|
|
11
|
+
const blob = new Blob([csvContent], { type: 'text/csv' });
|
|
12
|
+
formData.append('file', blob, 'upload.csv');
|
|
13
|
+
const response = await fetch(url.toString(), {
|
|
14
|
+
method: 'POST',
|
|
15
|
+
headers: {
|
|
16
|
+
'X-API-Key': this.apiKey,
|
|
17
|
+
},
|
|
18
|
+
body: formData,
|
|
19
|
+
});
|
|
20
|
+
if (!response.ok) {
|
|
21
|
+
await this.handleErrorResponse(response);
|
|
22
|
+
}
|
|
23
|
+
return response.json();
|
|
24
|
+
}
|
|
25
|
+
async requestPublic(method, path) {
|
|
26
|
+
const url = new URL(this.baseUrl + path);
|
|
27
|
+
const response = await fetch(url.toString(), {
|
|
28
|
+
method,
|
|
29
|
+
headers: {
|
|
30
|
+
'Content-Type': 'application/json',
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
if (!response.ok) {
|
|
34
|
+
await this.handleErrorResponse(response);
|
|
35
|
+
}
|
|
36
|
+
return response.json();
|
|
37
|
+
}
|
|
38
|
+
async requestTextPublic(method, path) {
|
|
39
|
+
const url = new URL(this.baseUrl + path);
|
|
40
|
+
const response = await fetch(url.toString(), { method });
|
|
41
|
+
if (!response.ok) {
|
|
42
|
+
await this.handleErrorResponse(response);
|
|
43
|
+
}
|
|
44
|
+
return response.text();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/clients/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAC/D,OAAO,EAAE,QAAQ,EAAE,KAAK,YAAY,EAAE,MAAM,kBAAkB,CAAC"}
|