@forgecart/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/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@forgecart/sdk",
3
+ "version": "0.1.0",
4
+ "description": "TypeScript SDK for ForgeCart GraphQL API",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./dist/index.mjs",
11
+ "require": "./dist/index.js",
12
+ "types": "./dist/index.d.ts"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "src"
18
+ ],
19
+ "dependencies": {
20
+ "graphql": "^16.8.1",
21
+ "graphql-request": "^6.1.0",
22
+ "graphql-tag": "^2.12.6",
23
+ "graphql-ws": "^6.0.6"
24
+ },
25
+ "optionalDependencies": {
26
+ "ws": "^8.18.0"
27
+ },
28
+ "devDependencies": {
29
+ "@types/node": "^20.11.5",
30
+ "@types/jest": "^29.5.11",
31
+ "jest": "^29.7.0",
32
+ "ts-jest": "^29.1.2",
33
+ "tsup": "^8.0.1",
34
+ "typescript": "^5.3.3"
35
+ },
36
+ "publishConfig": {
37
+ "access": "public",
38
+ "registry": "https://registry.npmjs.org/"
39
+ },
40
+ "repository": {
41
+ "type": "git",
42
+ "url": "https://github.com/forgecart/sdk"
43
+ },
44
+ "keywords": [
45
+ "forgecart",
46
+ "graphql",
47
+ "sdk",
48
+ "typescript",
49
+ "ecommerce",
50
+ "api"
51
+ ],
52
+ "author": "ForgeCart",
53
+ "license": "MIT",
54
+ "scripts": {
55
+ "build": "tsup",
56
+ "dev": "tsup --watch",
57
+ "test": "jest",
58
+ "typecheck": "tsc --noEmit",
59
+ "lint": "eslint src --ext .ts,.tsx"
60
+ }
61
+ }
@@ -0,0 +1,433 @@
1
+ /**
2
+ * @forgecart/admin-sdk - Auto-generated TypeScript SDK
3
+ *
4
+ * This file was automatically generated and should not be manually edited.
5
+ * To regenerate, run: npm run codegen:ts
6
+ *
7
+ * Generated at: 2025-10-31T11:44:37.912Z
8
+ * Generator version: 1.0.0
9
+ *
10
+ * 🤖 Generated with ForgeCart SDK Generator
11
+ */
12
+
13
+
14
+ import * as Types from './types.js';
15
+
16
+ import { GraphQLClient, RequestDocument, Variables } from 'graphql-request';
17
+ import { createClient, Client as WSClient, ClientOptions as WSClientOptions } from 'graphql-ws';
18
+
19
+ /**
20
+ * SDK Configuration
21
+ */
22
+ export interface SDKConfig {
23
+ /** GraphQL HTTP endpoint */
24
+ endpoint?: string;
25
+ /** WebSocket endpoint for subscriptions */
26
+ wsEndpoint?: string;
27
+ /** Custom HTTP headers */
28
+ headers?: Record<string, string>;
29
+ /** Custom WebSocket implementation (optional, auto-detected if not provided) */
30
+ webSocketImpl?: any;
31
+ }
32
+
33
+ export type AdminLoginMutationVariables = Types.AdminLoginMutationVariables;
34
+ export type AdminLoginMutation = Types.AdminLoginMutation;
35
+ export type AdminLoginMutationResult = Types.AdminLoginMutation;
36
+ export type ProductsQueryVariables = Types.ProductsQueryVariables;
37
+ export type ProductsQuery = Types.ProductsQuery;
38
+ export type ProductsQueryResult = Types.ProductsQuery;
39
+ export type ProductQueryVariables = Types.ProductQueryVariables;
40
+ export type ProductQuery = Types.ProductQuery;
41
+ export type ProductQueryResult = Types.ProductQuery;
42
+ export type CreateProductMutationVariables = Types.CreateProductMutationVariables;
43
+ export type CreateProductMutation = Types.CreateProductMutation;
44
+ export type CreateProductMutationResult = Types.CreateProductMutation;
45
+ export type UpdateProductMutationVariables = Types.UpdateProductMutationVariables;
46
+ export type UpdateProductMutation = Types.UpdateProductMutation;
47
+ export type UpdateProductMutationResult = Types.UpdateProductMutation;
48
+ export type DeleteProductMutationVariables = Types.DeleteProductMutationVariables;
49
+ export type DeleteProductMutation = Types.DeleteProductMutation;
50
+ export type DeleteProductMutationResult = Types.DeleteProductMutation;
51
+
52
+ const adminLoginDocument = `mutation AdminLogin($username: String!, $password: String!, $rememberMe: Boolean) {
53
+ login(username: $username, password: $password, rememberMe: $rememberMe) {
54
+ __typename
55
+ ... on CurrentUser {
56
+ id
57
+ identifier
58
+ channels {
59
+ id
60
+ code
61
+ token
62
+ permissions
63
+ }
64
+ }
65
+ ... on InvalidCredentialsError {
66
+ errorCode
67
+ message
68
+ }
69
+ ... on NativeAuthStrategyError {
70
+ errorCode
71
+ message
72
+ }
73
+ }
74
+ }`;
75
+
76
+ const productsDocument = `query products($options: ProductListOptions) {
77
+ products(options: $options) {
78
+ items {
79
+ id
80
+ name
81
+ slug
82
+ description
83
+ enabled
84
+ createdAt
85
+ updatedAt
86
+ featuredAsset {
87
+ id
88
+ preview
89
+ }
90
+ variants {
91
+ id
92
+ name
93
+ sku
94
+ price
95
+ stockLevel
96
+ enabled
97
+ }
98
+ }
99
+ totalItems
100
+ }
101
+ }`;
102
+
103
+ const productDocument = `query product($id: ID!) {
104
+ product(id: $id) {
105
+ id
106
+ name
107
+ slug
108
+ description
109
+ enabled
110
+ featuredAsset {
111
+ id
112
+ preview
113
+ source
114
+ }
115
+ assets {
116
+ id
117
+ preview
118
+ source
119
+ }
120
+ variants {
121
+ id
122
+ name
123
+ sku
124
+ price
125
+ stockLevel
126
+ trackInventory
127
+ enabled
128
+ }
129
+ optionGroups {
130
+ id
131
+ name
132
+ code
133
+ }
134
+ facetValues {
135
+ id
136
+ name
137
+ code
138
+ }
139
+ }
140
+ }`;
141
+
142
+ const createProductDocument = `mutation createProduct($input: CreateProductInput!) {
143
+ createProduct(input: $input) {
144
+ id
145
+ name
146
+ slug
147
+ enabled
148
+ }
149
+ }`;
150
+
151
+ const updateProductDocument = `mutation updateProduct($input: UpdateProductInput!) {
152
+ updateProduct(input: $input) {
153
+ id
154
+ name
155
+ slug
156
+ enabled
157
+ }
158
+ }`;
159
+
160
+ const deleteProductDocument = `mutation deleteProduct($id: ID!) {
161
+ deleteProduct(id: $id) {
162
+ result
163
+ message
164
+ }
165
+ }`;
166
+
167
+ /**
168
+ * Base GraphQL Client
169
+ * Handles HTTP and WebSocket connections for queries, mutations, and subscriptions
170
+ */
171
+ class BaseGraphQLClient {
172
+ private httpClient: GraphQLClient;
173
+ private wsClient: WSClient | null = null;
174
+ private authToken: string | null = null;
175
+ private endpoint: string;
176
+ private wsEndpoint: string;
177
+ private config: SDKConfig;
178
+
179
+ constructor(config: SDKConfig) {
180
+ this.endpoint = config.endpoint || 'http://localhost:3000/admin-api';
181
+ this.wsEndpoint = config.wsEndpoint || 'ws://localhost:3000/admin-api';
182
+ this.config = config;
183
+
184
+ // Initialize HTTP client
185
+ this.httpClient = new GraphQLClient(this.endpoint, {
186
+ headers: config.headers || {},
187
+ });
188
+
189
+ // WebSocket client will be initialized on first subscription
190
+ }
191
+
192
+ /**
193
+ * Execute a GraphQL query or mutation
194
+ */
195
+ async request<T = any, V extends Variables = Variables>(
196
+ document: RequestDocument,
197
+ variables?: V
198
+ ): Promise<T> {
199
+ try {
200
+ return await this.httpClient.request<T>(document, variables as any);
201
+ } catch (error) {
202
+ this.handleError(error);
203
+ throw error;
204
+ }
205
+ }
206
+
207
+
208
+ /**
209
+ * Handle GraphQL errors
210
+ */
211
+ private handleError(error: any): void {
212
+ console.error('GraphQL Error:', error);
213
+ }
214
+
215
+
216
+ /**
217
+ * Get WebSocket implementation for current environment
218
+ */
219
+ private getWebSocketImpl(): any {
220
+ // Use custom implementation if provided
221
+ if (this.config.webSocketImpl) {
222
+ return this.config.webSocketImpl;
223
+ }
224
+
225
+ // Browser environment (native WebSocket)
226
+ if (typeof WebSocket !== 'undefined') {
227
+ return WebSocket;
228
+ }
229
+
230
+ // Node.js environment (try to import 'ws' package)
231
+ try {
232
+ // Dynamic import for Node.js
233
+ return require('ws');
234
+ } catch (e) {
235
+ console.warn('WebSocket not available. Install "ws" package for Node.js support or provide webSocketImpl in config.');
236
+ return undefined;
237
+ }
238
+ }
239
+
240
+ /**
241
+ * Initialize WebSocket client for subscriptions
242
+ */
243
+ private initializeWebSocket(): WSClient {
244
+ if (this.wsClient) {
245
+ return this.wsClient;
246
+ }
247
+
248
+ const WebSocketImpl = this.getWebSocketImpl();
249
+ if (!WebSocketImpl) {
250
+ throw new Error('WebSocket implementation not available. Please install "ws" package or provide webSocketImpl in config.');
251
+ }
252
+
253
+ const wsOptions: WSClientOptions = {
254
+ url: this.wsEndpoint,
255
+ webSocketImpl: WebSocketImpl,
256
+ connectionParams: async () => {
257
+ return this.authToken ? { authorization: this.authToken } : {};
258
+ },
259
+ retryAttempts: 5,
260
+ shouldRetry: () => true,
261
+ };
262
+
263
+ this.wsClient = createClient(wsOptions);
264
+ return this.wsClient;
265
+ }
266
+
267
+ /**
268
+ * Subscribe to a GraphQL subscription
269
+ */
270
+ subscribe<T = any>(
271
+ document: RequestDocument,
272
+ variables?: Variables
273
+ ): AsyncIterableIterator<T> {
274
+ const client = this.initializeWebSocket();
275
+ const subscription = client.iterate({
276
+ query: document as string,
277
+ variables,
278
+ });
279
+
280
+ const iterator: AsyncIterableIterator<T> = {
281
+ async next() {
282
+ const result = await subscription.next();
283
+ if (result.done) {
284
+ return { done: true, value: undefined as any };
285
+ }
286
+ if (result.value.errors) {
287
+ throw new Error(result.value.errors.map(e => e.message).join(', '));
288
+ }
289
+ return { done: false, value: result.value.data as T };
290
+ },
291
+ async return() {
292
+ subscription.return?.();
293
+ return { done: true, value: undefined as any };
294
+ },
295
+ async throw(error: any) {
296
+ subscription.throw?.(error);
297
+ return { done: true, value: undefined as any };
298
+ },
299
+ [Symbol.asyncIterator]() {
300
+ return this;
301
+ },
302
+ };
303
+
304
+ return iterator;
305
+ }
306
+
307
+
308
+ /**
309
+ * Set authentication token
310
+ */
311
+ setAuthToken(token: string): void {
312
+ this.authToken = token;
313
+ this.httpClient.setHeader('authorization', `Bearer ${token}`);
314
+
315
+ // Reconnect WebSocket with new token
316
+ if (this.wsClient) {
317
+ this.wsClient.dispose();
318
+ this.wsClient = null;
319
+ // Will be reinitialized with new token on next subscription
320
+ }
321
+ }
322
+
323
+ /**
324
+ * Clear authentication token
325
+ */
326
+ clearAuthToken(): void {
327
+ this.authToken = null;
328
+ this.httpClient.setHeader('authorization', '');
329
+
330
+ // Reconnect WebSocket without token
331
+ if (this.wsClient) {
332
+ this.wsClient.dispose();
333
+ this.wsClient = null;
334
+ }
335
+ }
336
+
337
+ /**
338
+ * Dispose of the client and close connections
339
+ */
340
+ dispose(): void {
341
+ if (this.wsClient) {
342
+ this.wsClient.dispose();
343
+ this.wsClient = null;
344
+ }
345
+ }
346
+ }
347
+
348
+ /**
349
+ * Admin namespace operations
350
+ */
351
+ class AdminOperations {
352
+ constructor(private client: BaseGraphQLClient) {}
353
+
354
+ /**
355
+ * AdminLogin mutation
356
+ */
357
+ async adminLogin(variables: Types.AdminLoginMutationVariables): Promise<Types.AdminLoginMutation> {
358
+ return await this.client.request<Types.AdminLoginMutation>(adminLoginDocument, variables);
359
+ }
360
+
361
+ /**
362
+ * products query
363
+ */
364
+ async products(variables: Types.ProductsQueryVariables): Promise<Types.ProductsQuery> {
365
+ return await this.client.request<Types.ProductsQuery>(productsDocument, variables);
366
+ }
367
+
368
+ /**
369
+ * product query
370
+ */
371
+ async product(variables: Types.ProductQueryVariables): Promise<Types.ProductQuery> {
372
+ return await this.client.request<Types.ProductQuery>(productDocument, variables);
373
+ }
374
+
375
+ /**
376
+ * createProduct mutation
377
+ */
378
+ async createProduct(variables: Types.CreateProductMutationVariables): Promise<Types.CreateProductMutation> {
379
+ return await this.client.request<Types.CreateProductMutation>(createProductDocument, variables);
380
+ }
381
+
382
+ /**
383
+ * updateProduct mutation
384
+ */
385
+ async updateProduct(variables: Types.UpdateProductMutationVariables): Promise<Types.UpdateProductMutation> {
386
+ return await this.client.request<Types.UpdateProductMutation>(updateProductDocument, variables);
387
+ }
388
+
389
+ /**
390
+ * deleteProduct mutation
391
+ */
392
+ async deleteProduct(variables: Types.DeleteProductMutationVariables): Promise<Types.DeleteProductMutation> {
393
+ return await this.client.request<Types.DeleteProductMutation>(deleteProductDocument, variables);
394
+ }
395
+ }
396
+
397
+ /**
398
+ * ForgeCartSDK
399
+ * Main SDK class with namespaced operations
400
+ */
401
+ export class ForgeCartSDK {
402
+ private client: BaseGraphQLClient;
403
+
404
+ readonly admin: AdminOperations;
405
+
406
+ constructor(config: SDKConfig = {}) {
407
+ this.client = new BaseGraphQLClient(config);
408
+
409
+ this.admin = new AdminOperations(this.client);
410
+ }
411
+
412
+ /**
413
+ * Set authentication token
414
+ * Updates both HTTP and WebSocket clients with the new token
415
+ */
416
+ setAuthToken(token: string): void {
417
+ this.client.setAuthToken(token);
418
+ }
419
+
420
+ /**
421
+ * Clear authentication token
422
+ */
423
+ clearAuthToken(): void {
424
+ this.client.clearAuthToken();
425
+ }
426
+
427
+ /**
428
+ * Dispose of the SDK and close all connections
429
+ */
430
+ dispose(): void {
431
+ this.client.dispose();
432
+ }
433
+ }