@neo-edi/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/dist/index.js ADDED
@@ -0,0 +1,512 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ NeoEdiApiError: () => NeoEdiApiError,
24
+ NeoEdiClient: () => NeoEdiClient,
25
+ NeoEdiError: () => NeoEdiError,
26
+ NeoEdiNetworkError: () => NeoEdiNetworkError,
27
+ NeoEdiValidationError: () => NeoEdiValidationError,
28
+ Queries: () => Queries
29
+ });
30
+ module.exports = __toCommonJS(index_exports);
31
+
32
+ // src/errors.ts
33
+ var NeoEdiError = class extends Error {
34
+ constructor(message, code, statusCode, details) {
35
+ super(message);
36
+ this.code = code;
37
+ this.statusCode = statusCode;
38
+ this.details = details;
39
+ this.name = "NeoEdiError";
40
+ }
41
+ };
42
+ var NeoEdiApiError = class extends NeoEdiError {
43
+ constructor(message, statusCode, code, details) {
44
+ super(message, code, statusCode, details);
45
+ this.statusCode = statusCode;
46
+ this.name = "NeoEdiApiError";
47
+ }
48
+ };
49
+ var NeoEdiNetworkError = class extends NeoEdiError {
50
+ constructor(message, cause) {
51
+ super(message, "NETWORK_ERROR");
52
+ this.cause = cause;
53
+ this.name = "NeoEdiNetworkError";
54
+ }
55
+ };
56
+ var NeoEdiValidationError = class extends NeoEdiError {
57
+ constructor(message, details) {
58
+ super(message, "VALIDATION_ERROR", 400, details);
59
+ this.name = "NeoEdiValidationError";
60
+ }
61
+ };
62
+
63
+ // src/http.ts
64
+ var HttpClient = class {
65
+ constructor(config) {
66
+ this.baseUrl = config.baseUrl.replace(/\/$/, "");
67
+ this.apiKey = config.apiKey;
68
+ this.timeout = config.timeout ?? 3e4;
69
+ this.defaultHeaders = {
70
+ "Content-Type": "application/json",
71
+ "X-SDK-Version": "0.1.0",
72
+ ...config.headers
73
+ };
74
+ }
75
+ async request(options) {
76
+ const url = this.buildUrl(options.path, options.query);
77
+ const headers = {
78
+ ...this.defaultHeaders,
79
+ "X-API-Key": this.apiKey,
80
+ ...options.headers
81
+ };
82
+ const controller = new AbortController();
83
+ const timeoutId = setTimeout(() => controller.abort(), this.timeout);
84
+ try {
85
+ let body;
86
+ let finalHeaders = headers;
87
+ if (options.body instanceof FormData) {
88
+ body = options.body;
89
+ const { "Content-Type": _, ...restHeaders } = finalHeaders;
90
+ finalHeaders = restHeaders;
91
+ } else if (options.body) {
92
+ body = JSON.stringify(options.body);
93
+ }
94
+ const response = await fetch(url, {
95
+ method: options.method,
96
+ headers: finalHeaders,
97
+ body,
98
+ signal: controller.signal
99
+ });
100
+ clearTimeout(timeoutId);
101
+ const data = await this.parseResponse(response);
102
+ if (!response.ok) {
103
+ throw new NeoEdiApiError(
104
+ data.error || `Request failed with status ${response.status}`,
105
+ response.status,
106
+ data.code,
107
+ data.details
108
+ );
109
+ }
110
+ return data;
111
+ } catch (error) {
112
+ clearTimeout(timeoutId);
113
+ if (error instanceof NeoEdiApiError) {
114
+ throw error;
115
+ }
116
+ if (error instanceof Error) {
117
+ if (error.name === "AbortError") {
118
+ throw new NeoEdiNetworkError("Request timeout", error);
119
+ }
120
+ throw new NeoEdiNetworkError(error.message, error);
121
+ }
122
+ throw new NeoEdiNetworkError("Unknown error occurred");
123
+ }
124
+ }
125
+ buildUrl(path, query) {
126
+ const url = new URL(path, this.baseUrl);
127
+ if (query) {
128
+ Object.entries(query).forEach(([key, value]) => {
129
+ if (value !== void 0) {
130
+ url.searchParams.append(key, String(value));
131
+ }
132
+ });
133
+ }
134
+ return url.toString();
135
+ }
136
+ async parseResponse(response) {
137
+ const contentType = response.headers.get("content-type");
138
+ if (contentType?.includes("application/json")) {
139
+ try {
140
+ return await response.json();
141
+ } catch {
142
+ return { error: "Invalid JSON response" };
143
+ }
144
+ }
145
+ const text = await response.text();
146
+ return { data: text };
147
+ }
148
+ // Convenience methods
149
+ get(path, query) {
150
+ return this.request({ method: "GET", path, query });
151
+ }
152
+ post(path, body) {
153
+ return this.request({ method: "POST", path, body });
154
+ }
155
+ patch(path, body) {
156
+ return this.request({ method: "PATCH", path, body });
157
+ }
158
+ put(path, body) {
159
+ return this.request({ method: "PUT", path, body });
160
+ }
161
+ delete(path) {
162
+ return this.request({ method: "DELETE", path });
163
+ }
164
+ };
165
+
166
+ // src/resources/customers.ts
167
+ var CustomersResource = class {
168
+ constructor(http) {
169
+ this.http = http;
170
+ }
171
+ /**
172
+ * List all customers
173
+ */
174
+ async list(params) {
175
+ const response = await this.http.get(
176
+ "/api/v1/customers",
177
+ params
178
+ );
179
+ if (!response.success) {
180
+ throw new Error(response.error);
181
+ }
182
+ return response.data;
183
+ }
184
+ /**
185
+ * Get a customer by ID
186
+ */
187
+ async get(customerId) {
188
+ const response = await this.http.get(
189
+ `/api/v1/customers/${encodeURIComponent(customerId)}`
190
+ );
191
+ if (!response.success) {
192
+ throw new Error(response.error);
193
+ }
194
+ return response.data;
195
+ }
196
+ /**
197
+ * Create a new customer
198
+ */
199
+ async create(data) {
200
+ const response = await this.http.post(
201
+ "/api/v1/customers",
202
+ data
203
+ );
204
+ if (!response.success) {
205
+ throw new Error(response.error);
206
+ }
207
+ return response.data;
208
+ }
209
+ /**
210
+ * Update a customer
211
+ */
212
+ async update(customerId, data) {
213
+ const response = await this.http.patch(
214
+ `/api/v1/customers/${encodeURIComponent(customerId)}`,
215
+ data
216
+ );
217
+ if (!response.success) {
218
+ throw new Error(response.error);
219
+ }
220
+ return response.data;
221
+ }
222
+ /**
223
+ * Delete a customer
224
+ */
225
+ async delete(customerId) {
226
+ await this.http.delete(
227
+ `/api/v1/customers/${encodeURIComponent(customerId)}`
228
+ );
229
+ }
230
+ };
231
+
232
+ // src/resources/partner-customers.ts
233
+ var PartnerCustomersResource = class {
234
+ constructor(http) {
235
+ this.http = http;
236
+ }
237
+ /**
238
+ * Register customer-partner relationships
239
+ * Creates or activates relationships between a customer and multiple trading partners
240
+ */
241
+ async register(data) {
242
+ const response = await this.http.post(
243
+ "/api/v1/partner-customers",
244
+ data
245
+ );
246
+ if (!response.success) {
247
+ throw new Error(response.error);
248
+ }
249
+ return response.data;
250
+ }
251
+ };
252
+
253
+ // src/resources/mapping-templates.ts
254
+ var MappingTemplatesResource = class {
255
+ constructor(http) {
256
+ this.http = http;
257
+ }
258
+ /**
259
+ * List all mapping templates
260
+ */
261
+ async list(params) {
262
+ const response = await this.http.get(
263
+ "/api/v1/mapping-templates",
264
+ params
265
+ );
266
+ if (!response.success) {
267
+ throw new Error(response.error);
268
+ }
269
+ return response.data;
270
+ }
271
+ /**
272
+ * Get a mapping template by ID
273
+ */
274
+ async get(templateId) {
275
+ const response = await this.http.get(
276
+ `/api/v1/mapping-templates/${encodeURIComponent(templateId)}`
277
+ );
278
+ if (!response.success) {
279
+ throw new Error(response.error);
280
+ }
281
+ return response.data;
282
+ }
283
+ /**
284
+ * Create a new mapping template
285
+ */
286
+ async create(data) {
287
+ const response = await this.http.post(
288
+ "/api/v1/mapping-templates",
289
+ data
290
+ );
291
+ if (!response.success) {
292
+ throw new Error(response.error);
293
+ }
294
+ return response.data;
295
+ }
296
+ /**
297
+ * Update a mapping template
298
+ */
299
+ async update(templateId, data) {
300
+ const response = await this.http.put(
301
+ `/api/v1/mapping-templates/${encodeURIComponent(templateId)}`,
302
+ data
303
+ );
304
+ if (!response.success) {
305
+ throw new Error(response.error);
306
+ }
307
+ return response.data;
308
+ }
309
+ /**
310
+ * Delete a mapping template
311
+ */
312
+ async delete(templateId) {
313
+ await this.http.delete(
314
+ `/api/v1/mapping-templates/${encodeURIComponent(templateId)}`
315
+ );
316
+ }
317
+ };
318
+
319
+ // src/resources/ingest.ts
320
+ var IngestResource = class {
321
+ constructor(http) {
322
+ this.http = http;
323
+ }
324
+ /**
325
+ * Upload a CSV file for ingestion
326
+ *
327
+ * @example Browser
328
+ * ```typescript
329
+ * const input = document.querySelector('input[type="file"]');
330
+ * const file = input.files[0];
331
+ * await client.ingest.uploadCsv(file, options);
332
+ * ```
333
+ *
334
+ * @example Node.js
335
+ * ```typescript
336
+ * import { readFileSync } from 'fs';
337
+ * const buffer = readFileSync('data.csv');
338
+ * const blob = new Blob([buffer], { type: 'text/csv' });
339
+ * await client.ingest.uploadCsv(blob, options);
340
+ * ```
341
+ */
342
+ async uploadCsv(file, options) {
343
+ const formData = new FormData();
344
+ formData.append("file", file, "data.csv");
345
+ formData.append("partnerId", options.partnerId);
346
+ formData.append("mappingConfig", JSON.stringify(options.mappingConfig));
347
+ if (options.transformationConfig) {
348
+ formData.append(
349
+ "transformationConfig",
350
+ JSON.stringify(options.transformationConfig)
351
+ );
352
+ }
353
+ const response = await this.http.request({
354
+ method: "POST",
355
+ path: "/api/v1/ingest-csv",
356
+ body: formData
357
+ });
358
+ if (!response.success) {
359
+ throw new Error(response.error);
360
+ }
361
+ return response.data;
362
+ }
363
+ /**
364
+ * Perform a dry run of CSV ingestion (validation only)
365
+ */
366
+ async dryRun(file, options) {
367
+ const formData = new FormData();
368
+ formData.append("file", file, "data.csv");
369
+ formData.append("partnerId", options.partnerId);
370
+ formData.append("mappingConfig", JSON.stringify(options.mappingConfig));
371
+ if (options.transformationConfig) {
372
+ formData.append(
373
+ "transformationConfig",
374
+ JSON.stringify(options.transformationConfig)
375
+ );
376
+ }
377
+ const response = await this.http.request({
378
+ method: "POST",
379
+ path: "/api/v1/ingest-csv/dry-run",
380
+ body: formData
381
+ });
382
+ if (!response.success) {
383
+ throw new Error(response.error);
384
+ }
385
+ return response.data;
386
+ }
387
+ };
388
+
389
+ // src/resources/graphql.ts
390
+ var GraphQLResource = class {
391
+ constructor(http) {
392
+ this.http = http;
393
+ }
394
+ /**
395
+ * Execute a GraphQL query
396
+ */
397
+ async query(query, variables, options) {
398
+ const response = await this.http.post("/api/v1/graphql", {
399
+ query,
400
+ variables,
401
+ operationName: options?.operationName
402
+ });
403
+ if (response.errors && response.errors.length > 0) {
404
+ const errorMessage = response.errors.map((e) => e.message).join("; ");
405
+ throw new Error(`GraphQL Error: ${errorMessage}`);
406
+ }
407
+ if (!response.data) {
408
+ throw new Error("No data returned from GraphQL query");
409
+ }
410
+ return response.data;
411
+ }
412
+ /**
413
+ * Execute a GraphQL mutation
414
+ */
415
+ async mutate(mutation, variables, options) {
416
+ return this.query(mutation, variables, options);
417
+ }
418
+ };
419
+ var Queries = {
420
+ /**
421
+ * Get EDI 852 headers with filtering
422
+ */
423
+ GET_EDI_852_HEADERS: `
424
+ query GetEdi852Headers($filter: Edi852HeaderFilterInput) {
425
+ edi852Headers(filter: $filter) {
426
+ id
427
+ documentId
428
+ reportTypeCode
429
+ reportDate
430
+ vendorPartnerId
431
+ retailerPartnerId
432
+ status
433
+ totalLocations
434
+ totalItems
435
+ createdAt
436
+ }
437
+ }
438
+ `,
439
+ /**
440
+ * Get a single EDI 852 header by ID with locations
441
+ */
442
+ GET_EDI_852_HEADER_BY_ID: `
443
+ query GetEdi852HeaderById($id: ID!) {
444
+ edi852Header(id: $id) {
445
+ id
446
+ documentId
447
+ reportTypeCode
448
+ reportDate
449
+ vendorPartnerId
450
+ retailerPartnerId
451
+ status
452
+ totalLocations
453
+ totalItems
454
+ metadata
455
+ createdAt
456
+ locations {
457
+ id
458
+ locationNumber
459
+ locationName
460
+ activityDate
461
+ items {
462
+ id
463
+ upc
464
+ productDescription
465
+ quantityOnHand
466
+ quantitySold
467
+ unitCost
468
+ }
469
+ }
470
+ }
471
+ }
472
+ `,
473
+ /**
474
+ * Get trading partner execution summaries
475
+ */
476
+ GET_EXECUTION_SUMMARIES: `
477
+ query GetExecutionSummaries($partnerId: ID!) {
478
+ tradingPartnerExecutionSummaries(partnerId: $partnerId) {
479
+ tradingPartnerId
480
+ customerId
481
+ lastExecutedAt
482
+ documentCount
483
+ }
484
+ }
485
+ `
486
+ };
487
+
488
+ // src/client.ts
489
+ var NeoEdiClient = class {
490
+ constructor(config) {
491
+ this.http = new HttpClient({
492
+ baseUrl: config.baseUrl,
493
+ apiKey: config.apiKey,
494
+ timeout: config.timeout,
495
+ headers: config.headers
496
+ });
497
+ this.customers = new CustomersResource(this.http);
498
+ this.partnerCustomers = new PartnerCustomersResource(this.http);
499
+ this.mappingTemplates = new MappingTemplatesResource(this.http);
500
+ this.ingest = new IngestResource(this.http);
501
+ this.graphql = new GraphQLResource(this.http);
502
+ }
503
+ };
504
+ // Annotate the CommonJS export names for ESM import in node:
505
+ 0 && (module.exports = {
506
+ NeoEdiApiError,
507
+ NeoEdiClient,
508
+ NeoEdiError,
509
+ NeoEdiNetworkError,
510
+ NeoEdiValidationError,
511
+ Queries
512
+ });