@forgecart/sdk 1.2.5 → 1.2.6

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 (58) hide show
  1. package/README.md +2 -87
  2. package/dist/admin-types.generated.d.ts +9884 -0
  3. package/dist/admin.d.ts +55 -8
  4. package/dist/admin.generated.d.ts +790 -0
  5. package/dist/admin.generated.js +1801 -0
  6. package/dist/admin.js +112 -23
  7. package/dist/client.generated.d.ts +251 -0
  8. package/dist/client.generated.js +757 -0
  9. package/dist/documents.generated.d.ts +465 -0
  10. package/dist/documents.generated.js +24283 -0
  11. package/dist/error-utils.d.ts +25 -0
  12. package/dist/error-utils.js +59 -0
  13. package/dist/hook-event-map.generated.d.ts +243 -0
  14. package/dist/hook-event-map.generated.js +9 -0
  15. package/dist/index.d.ts +23 -59
  16. package/dist/index.js +35 -83
  17. package/dist/sdk-hook-subscription.generated.d.ts +29 -0
  18. package/dist/sdk-hook-subscription.generated.js +73 -0
  19. package/dist/sdk-plugin.generated.d.ts +38 -0
  20. package/dist/sdk-plugin.generated.js +31 -0
  21. package/dist/sdk-types.generated.d.ts +56 -0
  22. package/dist/sdk-types.generated.js +28 -0
  23. package/dist/shop-types.generated.d.ts +4776 -0
  24. package/dist/shop.d.ts +18 -8
  25. package/dist/shop.generated.d.ts +213 -0
  26. package/dist/shop.generated.js +465 -0
  27. package/dist/shop.js +37 -23
  28. package/dist/upload.d.ts +14 -0
  29. package/dist/upload.js +163 -0
  30. package/package.json +10 -25
  31. package/src/admin-types.generated.ts +28377 -0
  32. package/src/admin.generated.ts +1771 -0
  33. package/src/admin.ts +55 -9
  34. package/src/client.generated.ts +845 -0
  35. package/src/documents.generated.ts +24730 -0
  36. package/src/error-utils.ts +74 -0
  37. package/src/hook-event-map.generated.ts +252 -0
  38. package/src/index.ts +23 -115
  39. package/src/sdk-hook-subscription.generated.ts +93 -0
  40. package/src/sdk-plugin.generated.ts +59 -0
  41. package/src/sdk-types.generated.ts +79 -0
  42. package/src/shop-types.generated.ts +10400 -0
  43. package/src/shop.generated.ts +452 -0
  44. package/src/shop.ts +18 -9
  45. package/src/upload.ts +211 -0
  46. package/LICENSE +0 -21
  47. package/dist/admin-namespace.d.ts +0 -2688
  48. package/dist/admin-namespace.js +0 -9691
  49. package/dist/admin-types.d.ts +0 -16195
  50. package/dist/shop-namespace.d.ts +0 -718
  51. package/dist/shop-namespace.js +0 -3124
  52. package/dist/shop-types.d.ts +0 -6310
  53. package/src/admin-namespace.ts +0 -11428
  54. package/src/admin-types.ts +0 -10809
  55. package/src/shop-namespace.ts +0 -3547
  56. package/src/shop-types.ts +0 -4684
  57. /package/dist/{admin-types.js → admin-types.generated.js} +0 -0
  58. /package/dist/{shop-types.js → shop-types.generated.js} +0 -0
package/src/upload.ts ADDED
@@ -0,0 +1,211 @@
1
+ /**
2
+ * Standalone fetch-based multipart upload function for ForgeCart SDK.
3
+ * Zero Apollo dependency — works in both Node.js (18+) and browser.
4
+ * Implements the GraphQL multipart request spec.
5
+ */
6
+
7
+ interface ExtractableFile {
8
+ name?: string;
9
+ type?: string;
10
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
11
+ stream?: () => any;
12
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
13
+ arrayBuffer?: () => Promise<any>;
14
+ }
15
+
16
+ /**
17
+ * Check if a value is an extractable file (File, Blob, Buffer, or similar)
18
+ */
19
+ function isExtractableFile(value: unknown): value is ExtractableFile {
20
+ if (typeof value !== 'object' || value === null) {
21
+ return false;
22
+ }
23
+
24
+ // Check for File
25
+ if (typeof File !== 'undefined' && value instanceof File) {
26
+ return true;
27
+ }
28
+
29
+ // Check for Blob
30
+ if (typeof Blob !== 'undefined' && value instanceof Blob) {
31
+ return true;
32
+ }
33
+
34
+ // Check for Buffer (Node.js — not available in browser)
35
+ if (typeof Buffer !== 'undefined' && Buffer.isBuffer(value)) {
36
+ return true;
37
+ }
38
+
39
+ return false;
40
+ }
41
+
42
+ /**
43
+ * Get file name from extractable file
44
+ */
45
+ function getFileName(file: ExtractableFile): string {
46
+ if (file.name) {
47
+ return file.name;
48
+ }
49
+ return 'file';
50
+ }
51
+
52
+ /**
53
+ * Convert file to Blob for FormData
54
+ */
55
+ async function fileToBlob(file: ExtractableFile): Promise<Blob> {
56
+ // If it's already a Blob/File, return it
57
+ if (file instanceof Blob) {
58
+ return file;
59
+ }
60
+
61
+ // If it's a Buffer (Node.js — not available in browser), convert to Blob
62
+ if (typeof Buffer !== 'undefined' && Buffer.isBuffer(file)) {
63
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
64
+ return new Blob([file as any]);
65
+ }
66
+
67
+ // For other objects, try to get arrayBuffer
68
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
69
+ const val = file as any;
70
+ if (val.arrayBuffer) {
71
+ const arrayBuffer = await val.arrayBuffer();
72
+ return new Blob([arrayBuffer]);
73
+ }
74
+
75
+ if (val.data && typeof Buffer !== 'undefined' && Buffer.isBuffer(val.data)) {
76
+ return new Blob([val.data]);
77
+ }
78
+
79
+ // Last resort - stringify
80
+ return new Blob([JSON.stringify(file)]);
81
+ }
82
+
83
+ /**
84
+ * Recursively extracts files from variables and returns paths to them
85
+ */
86
+ function extractFiles(
87
+ value: unknown,
88
+ path = '',
89
+ files: Map<ExtractableFile, string[]> = new Map(),
90
+ ): { clone: unknown; files: Map<ExtractableFile, string[]> } {
91
+ if (isExtractableFile(value)) {
92
+ const existingPaths = files.get(value) || [];
93
+ files.set(value, [...existingPaths, path]);
94
+ return { clone: null, files };
95
+ }
96
+
97
+ if (Array.isArray(value)) {
98
+ const clone: unknown[] = [];
99
+ value.forEach((item, index) => {
100
+ const result = extractFiles(item, `${path}.${index}`, files);
101
+ clone[index] = result.clone;
102
+ files = result.files;
103
+ });
104
+ return { clone, files };
105
+ }
106
+
107
+ if (typeof value === 'object' && value !== null) {
108
+ const clone: Record<string, unknown> = {};
109
+ Object.entries(value).forEach(([key, val]) => {
110
+ const result = extractFiles(val, path ? `${path}.${key}` : key, files);
111
+ clone[key] = result.clone;
112
+ files = result.files;
113
+ });
114
+ return { clone, files };
115
+ }
116
+
117
+ return { clone: value, files };
118
+ }
119
+
120
+ export interface UploadResult<T> {
121
+ data: T;
122
+ extensions?: Record<string, unknown>;
123
+ }
124
+
125
+ /**
126
+ * Performs a GraphQL mutation with file upload support via the multipart request spec.
127
+ * Falls back to standard JSON POST when no files are detected in variables.
128
+ */
129
+ export async function uploadViaFetch<T>(
130
+ endpoint: string,
131
+ document: string,
132
+ variables: Record<string, unknown>,
133
+ headers: Record<string, string>,
134
+ ): Promise<UploadResult<T>> {
135
+ const { clone, files } = extractFiles(variables);
136
+
137
+ const hasFiles = files.size > 0;
138
+ let body: FormData | string;
139
+ const fetchHeaders = { ...headers };
140
+
141
+ if (hasFiles) {
142
+ const formData = new FormData();
143
+ formData.append('operations', JSON.stringify({
144
+ query: document,
145
+ variables: clone,
146
+ }));
147
+
148
+ const map: Record<string, string[]> = {};
149
+ const fileList: { key: string; file: ExtractableFile }[] = [];
150
+ let fileIndex = 0;
151
+ files.forEach((paths, file) => {
152
+ const key = fileIndex.toString();
153
+ map[key] = paths.map((p) => `variables.${p}`);
154
+ fileList.push({ key, file });
155
+ fileIndex++;
156
+ });
157
+
158
+ formData.append('map', JSON.stringify(map));
159
+
160
+ for (const { key, file } of fileList) {
161
+ const filename = getFileName(file);
162
+ if (file instanceof File) {
163
+ formData.append(key, file, filename);
164
+ } else if (file instanceof Blob) {
165
+ formData.append(key, file, filename);
166
+ } else {
167
+ const blob = await fileToBlob(file);
168
+ formData.append(key, blob, filename);
169
+ }
170
+ }
171
+
172
+ body = formData;
173
+ // Let FormData set its own Content-Type with boundary
174
+ delete fetchHeaders['content-type'];
175
+ // Required by Apollo Server to accept multipart requests
176
+ fetchHeaders['apollo-require-preflight'] = 'true';
177
+ } else {
178
+ fetchHeaders['content-type'] = 'application/json';
179
+ body = JSON.stringify({
180
+ query: document,
181
+ variables,
182
+ });
183
+ }
184
+
185
+ const response = await fetch(endpoint, {
186
+ method: 'POST',
187
+ headers: fetchHeaders,
188
+ body: body as never,
189
+ });
190
+
191
+ const result = await response.json() as {
192
+ data?: T;
193
+ errors?: Array<{ message: string; extensions?: Record<string, unknown> }>;
194
+ extensions?: Record<string, unknown>;
195
+ };
196
+
197
+ if (result.errors && result.errors.length > 0) {
198
+ const error = new Error(result.errors[0].message) as Error & { response: { errors: typeof result.errors } };
199
+ error.response = { errors: result.errors };
200
+ throw error;
201
+ }
202
+
203
+ if (!response.ok) {
204
+ throw new Error(`HTTP error: ${response.status}`);
205
+ }
206
+
207
+ return {
208
+ data: result.data as T,
209
+ extensions: result.extensions,
210
+ };
211
+ }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2022 Vendure
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.