@htlkg/data 0.0.1 → 0.0.3
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 +53 -0
- package/dist/client/index.d.ts +117 -31
- package/dist/client/index.js +74 -22
- package/dist/client/index.js.map +1 -1
- package/dist/content-collections/index.js +20 -24
- package/dist/content-collections/index.js.map +1 -1
- package/dist/hooks/index.d.ts +113 -5
- package/dist/hooks/index.js +165 -182
- package/dist/hooks/index.js.map +1 -1
- package/dist/index.d.ts +8 -4
- package/dist/index.js +305 -182
- package/dist/index.js.map +1 -1
- package/dist/queries/index.d.ts +78 -1
- package/dist/queries/index.js +47 -0
- package/dist/queries/index.js.map +1 -1
- package/dist/stores/index.d.ts +106 -0
- package/dist/stores/index.js +108 -0
- package/dist/stores/index.js.map +1 -0
- package/package.json +60 -37
- package/src/client/__tests__/server.test.ts +100 -0
- package/src/client/client.md +91 -0
- package/src/client/index.test.ts +232 -0
- package/src/client/index.ts +145 -0
- package/src/client/server.ts +118 -0
- package/src/content-collections/content-collections.md +87 -0
- package/src/content-collections/generator.ts +314 -0
- package/src/content-collections/index.ts +32 -0
- package/src/content-collections/schemas.ts +75 -0
- package/src/content-collections/sync.ts +139 -0
- package/src/hooks/README.md +293 -0
- package/src/hooks/createDataHook.ts +208 -0
- package/src/hooks/data-hook-errors.property.test.ts +270 -0
- package/src/hooks/data-hook-filters.property.test.ts +263 -0
- package/src/hooks/data-hooks.property.test.ts +190 -0
- package/src/hooks/hooks.test.ts +76 -0
- package/src/hooks/index.ts +21 -0
- package/src/hooks/useAccounts.ts +66 -0
- package/src/hooks/useBrands.ts +95 -0
- package/src/hooks/useProducts.ts +88 -0
- package/src/hooks/useUsers.ts +89 -0
- package/src/index.ts +32 -0
- package/src/mutations/accounts.ts +127 -0
- package/src/mutations/brands.ts +133 -0
- package/src/mutations/index.ts +32 -0
- package/src/mutations/mutations.md +96 -0
- package/src/mutations/users.ts +136 -0
- package/src/queries/accounts.ts +121 -0
- package/src/queries/brands.ts +176 -0
- package/src/queries/index.ts +45 -0
- package/src/queries/products.ts +282 -0
- package/src/queries/queries.md +88 -0
- package/src/queries/server-helpers.ts +114 -0
- package/src/queries/users.ts +199 -0
- package/src/stores/createStores.ts +148 -0
- package/src/stores/index.ts +15 -0
- package/src/stores/stores.md +104 -0
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Brand Query Functions
|
|
3
|
+
*
|
|
4
|
+
* Provides query functions for fetching brand data from the GraphQL API.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { Brand } from "@htlkg/core/types";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Get a single brand by ID
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import { getBrand } from '@htlkg/data/queries';
|
|
15
|
+
* import { generateClient } from '@htlkg/data/client';
|
|
16
|
+
*
|
|
17
|
+
* const client = generateClient<Schema>();
|
|
18
|
+
* const brand = await getBrand(client, 'brand-123');
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export async function getBrand<TClient = any>(
|
|
22
|
+
client: TClient,
|
|
23
|
+
id: string,
|
|
24
|
+
): Promise<Brand | null> {
|
|
25
|
+
try {
|
|
26
|
+
const { data, errors } = await (client as any).models.Brand.get({ id });
|
|
27
|
+
|
|
28
|
+
if (errors) {
|
|
29
|
+
console.error("[getBrand] GraphQL errors:", errors);
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return data as Brand;
|
|
34
|
+
} catch (error) {
|
|
35
|
+
console.error("[getBrand] Error fetching brand:", error);
|
|
36
|
+
throw error;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* List all brands with optional filtering
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* import { listBrands } from '@htlkg/data/queries';
|
|
46
|
+
* import { generateClient } from '@htlkg/data/client';
|
|
47
|
+
*
|
|
48
|
+
* const client = generateClient<Schema>();
|
|
49
|
+
* const brands = await listBrands(client, {
|
|
50
|
+
* filter: { status: { eq: 'active' } }
|
|
51
|
+
* });
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export async function listBrands<TClient = any>(
|
|
55
|
+
client: TClient,
|
|
56
|
+
options?: {
|
|
57
|
+
filter?: any;
|
|
58
|
+
limit?: number;
|
|
59
|
+
nextToken?: string;
|
|
60
|
+
},
|
|
61
|
+
): Promise<{ items: Brand[]; nextToken?: string }> {
|
|
62
|
+
try {
|
|
63
|
+
const { data, errors, nextToken } = await (client as any).models.Brand.list(
|
|
64
|
+
options,
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
if (errors) {
|
|
68
|
+
console.error("[listBrands] GraphQL errors:", errors);
|
|
69
|
+
return { items: [], nextToken: undefined };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
items: (data || []) as Brand[],
|
|
74
|
+
nextToken,
|
|
75
|
+
};
|
|
76
|
+
} catch (error) {
|
|
77
|
+
console.error("[listBrands] Error fetching brands:", error);
|
|
78
|
+
throw error;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Get a brand with its product instances
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* import { getBrandWithProducts } from '@htlkg/data/queries';
|
|
88
|
+
* import { generateClient } from '@htlkg/data/client';
|
|
89
|
+
*
|
|
90
|
+
* const client = generateClient<Schema>();
|
|
91
|
+
* const brand = await getBrandWithProducts(client, 'brand-123');
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
export async function getBrandWithProducts<TClient = any>(
|
|
95
|
+
client: TClient,
|
|
96
|
+
id: string,
|
|
97
|
+
): Promise<Brand | null> {
|
|
98
|
+
try {
|
|
99
|
+
const { data, errors } = await (client as any).models.Brand.get(
|
|
100
|
+
{ id },
|
|
101
|
+
{
|
|
102
|
+
selectionSet: [
|
|
103
|
+
"id",
|
|
104
|
+
"name",
|
|
105
|
+
"accountId",
|
|
106
|
+
"logo",
|
|
107
|
+
"timezone",
|
|
108
|
+
"status",
|
|
109
|
+
"settings",
|
|
110
|
+
"productInstances.*",
|
|
111
|
+
],
|
|
112
|
+
},
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
if (errors) {
|
|
116
|
+
console.error("[getBrandWithProducts] GraphQL errors:", errors);
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return data as Brand;
|
|
121
|
+
} catch (error) {
|
|
122
|
+
console.error("[getBrandWithProducts] Error fetching brand:", error);
|
|
123
|
+
throw error;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* List brands by account ID
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* ```typescript
|
|
132
|
+
* import { listBrandsByAccount } from '@htlkg/data/queries';
|
|
133
|
+
* import { generateClient } from '@htlkg/data/client';
|
|
134
|
+
*
|
|
135
|
+
* const client = generateClient<Schema>();
|
|
136
|
+
* const brands = await listBrandsByAccount(client, 'account-123');
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
export async function listBrandsByAccount<TClient = any>(
|
|
140
|
+
client: TClient,
|
|
141
|
+
accountId: string,
|
|
142
|
+
options?: {
|
|
143
|
+
limit?: number;
|
|
144
|
+
nextToken?: string;
|
|
145
|
+
},
|
|
146
|
+
): Promise<{ items: Brand[]; nextToken?: string }> {
|
|
147
|
+
return listBrands(client, {
|
|
148
|
+
filter: { accountId: { eq: accountId } },
|
|
149
|
+
...options,
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* List active brands
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* ```typescript
|
|
158
|
+
* import { listActiveBrands } from '@htlkg/data/queries';
|
|
159
|
+
* import { generateClient } from '@htlkg/data/client';
|
|
160
|
+
*
|
|
161
|
+
* const client = generateClient<Schema>();
|
|
162
|
+
* const brands = await listActiveBrands(client);
|
|
163
|
+
* ```
|
|
164
|
+
*/
|
|
165
|
+
export async function listActiveBrands<TClient = any>(
|
|
166
|
+
client: TClient,
|
|
167
|
+
options?: {
|
|
168
|
+
limit?: number;
|
|
169
|
+
nextToken?: string;
|
|
170
|
+
},
|
|
171
|
+
): Promise<{ items: Brand[]; nextToken?: string }> {
|
|
172
|
+
return listBrands(client, {
|
|
173
|
+
filter: { status: { eq: "active" } },
|
|
174
|
+
...options,
|
|
175
|
+
});
|
|
176
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Query Functions for @htlkg/data
|
|
3
|
+
*
|
|
4
|
+
* Provides query functions for fetching data from the GraphQL API.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// Brand queries
|
|
8
|
+
export {
|
|
9
|
+
getBrand,
|
|
10
|
+
listBrands,
|
|
11
|
+
getBrandWithProducts,
|
|
12
|
+
listBrandsByAccount,
|
|
13
|
+
listActiveBrands,
|
|
14
|
+
} from "./brands";
|
|
15
|
+
|
|
16
|
+
// Account queries
|
|
17
|
+
export {
|
|
18
|
+
getAccount,
|
|
19
|
+
listAccounts,
|
|
20
|
+
getAccountWithBrands,
|
|
21
|
+
} from "./accounts";
|
|
22
|
+
|
|
23
|
+
// User queries
|
|
24
|
+
export {
|
|
25
|
+
getUser,
|
|
26
|
+
getUserByCognitoId,
|
|
27
|
+
getUserByEmail,
|
|
28
|
+
listUsers,
|
|
29
|
+
listUsersByAccount,
|
|
30
|
+
listActiveUsers,
|
|
31
|
+
} from "./users";
|
|
32
|
+
|
|
33
|
+
// Product queries
|
|
34
|
+
export {
|
|
35
|
+
getProduct,
|
|
36
|
+
listProducts,
|
|
37
|
+
listActiveProducts,
|
|
38
|
+
getProductInstance,
|
|
39
|
+
listProductInstancesByBrand,
|
|
40
|
+
listProductInstancesByAccount,
|
|
41
|
+
listEnabledProductInstancesByBrand,
|
|
42
|
+
} from "./products";
|
|
43
|
+
|
|
44
|
+
// Server-side query helpers
|
|
45
|
+
export { executeServerQuery, executePublicQuery } from "./server-helpers";
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Product Query Functions
|
|
3
|
+
*
|
|
4
|
+
* Provides query functions for fetching product and product instance data from the GraphQL API.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { Product, ProductInstance } from "@htlkg/core/types";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Get a single product by ID
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import { getProduct } from '@htlkg/data/queries';
|
|
15
|
+
* import { generateClient } from '@htlkg/data/client';
|
|
16
|
+
*
|
|
17
|
+
* const client = generateClient<Schema>();
|
|
18
|
+
* const product = await getProduct(client, 'product-123');
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export async function getProduct<TClient = any>(
|
|
22
|
+
client: TClient,
|
|
23
|
+
id: string,
|
|
24
|
+
): Promise<Product | null> {
|
|
25
|
+
try {
|
|
26
|
+
const { data, errors } = await (client as any).models.Product.get({ id });
|
|
27
|
+
|
|
28
|
+
if (errors) {
|
|
29
|
+
console.error("[getProduct] GraphQL errors:", errors);
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return data as Product;
|
|
34
|
+
} catch (error) {
|
|
35
|
+
console.error("[getProduct] Error fetching product:", error);
|
|
36
|
+
throw error;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* List all products with optional filtering
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* import { listProducts } from '@htlkg/data/queries';
|
|
46
|
+
* import { generateClient } from '@htlkg/data/client';
|
|
47
|
+
*
|
|
48
|
+
* const client = generateClient<Schema>();
|
|
49
|
+
* const products = await listProducts(client, {
|
|
50
|
+
* filter: { isActive: { eq: true } }
|
|
51
|
+
* });
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export async function listProducts<TClient = any>(
|
|
55
|
+
client: TClient,
|
|
56
|
+
options?: {
|
|
57
|
+
filter?: any;
|
|
58
|
+
limit?: number;
|
|
59
|
+
nextToken?: string;
|
|
60
|
+
},
|
|
61
|
+
): Promise<{ items: Product[]; nextToken?: string }> {
|
|
62
|
+
try {
|
|
63
|
+
const { data, errors, nextToken } = await (
|
|
64
|
+
client as any
|
|
65
|
+
).models.Product.list(options);
|
|
66
|
+
|
|
67
|
+
if (errors) {
|
|
68
|
+
console.error("[listProducts] GraphQL errors:", errors);
|
|
69
|
+
return { items: [], nextToken: undefined };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
items: (data || []) as Product[],
|
|
74
|
+
nextToken,
|
|
75
|
+
};
|
|
76
|
+
} catch (error) {
|
|
77
|
+
console.error("[listProducts] Error fetching products:", error);
|
|
78
|
+
throw error;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* List active products
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* import { listActiveProducts } from '@htlkg/data/queries';
|
|
88
|
+
* import { generateClient } from '@htlkg/data/client';
|
|
89
|
+
*
|
|
90
|
+
* const client = generateClient<Schema>();
|
|
91
|
+
* const products = await listActiveProducts(client);
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
export async function listActiveProducts<TClient = any>(
|
|
95
|
+
client: TClient,
|
|
96
|
+
options?: {
|
|
97
|
+
limit?: number;
|
|
98
|
+
nextToken?: string;
|
|
99
|
+
},
|
|
100
|
+
): Promise<{ items: Product[]; nextToken?: string }> {
|
|
101
|
+
return listProducts(client, {
|
|
102
|
+
filter: { isActive: { eq: true } },
|
|
103
|
+
...options,
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Get a single product instance by ID
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* ```typescript
|
|
112
|
+
* import { getProductInstance } from '@htlkg/data/queries';
|
|
113
|
+
* import { generateClient } from '@htlkg/data/client';
|
|
114
|
+
*
|
|
115
|
+
* const client = generateClient<Schema>();
|
|
116
|
+
* const instance = await getProductInstance(client, 'instance-123');
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
export async function getProductInstance<TClient = any>(
|
|
120
|
+
client: TClient,
|
|
121
|
+
id: string,
|
|
122
|
+
): Promise<ProductInstance | null> {
|
|
123
|
+
try {
|
|
124
|
+
const { data, errors } = await (client as any).models.ProductInstance.get({
|
|
125
|
+
id,
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
if (errors) {
|
|
129
|
+
console.error("[getProductInstance] GraphQL errors:", errors);
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return data as ProductInstance;
|
|
134
|
+
} catch (error) {
|
|
135
|
+
console.error("[getProductInstance] Error fetching product instance:", error);
|
|
136
|
+
throw error;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* List product instances by brand ID
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* ```typescript
|
|
145
|
+
* import { listProductInstancesByBrand } from '@htlkg/data/queries';
|
|
146
|
+
* import { generateClient } from '@htlkg/data/client';
|
|
147
|
+
*
|
|
148
|
+
* const client = generateClient<Schema>();
|
|
149
|
+
* const instances = await listProductInstancesByBrand(client, 'brand-123');
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
export async function listProductInstancesByBrand<TClient = any>(
|
|
153
|
+
client: TClient,
|
|
154
|
+
brandId: string,
|
|
155
|
+
options?: {
|
|
156
|
+
limit?: number;
|
|
157
|
+
nextToken?: string;
|
|
158
|
+
},
|
|
159
|
+
): Promise<{ items: ProductInstance[]; nextToken?: string }> {
|
|
160
|
+
try {
|
|
161
|
+
const { data, errors, nextToken } = await (
|
|
162
|
+
client as any
|
|
163
|
+
).models.ProductInstance.list({
|
|
164
|
+
filter: { brandId: { eq: brandId } },
|
|
165
|
+
...options,
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
if (errors) {
|
|
169
|
+
console.error("[listProductInstancesByBrand] GraphQL errors:", errors);
|
|
170
|
+
return { items: [], nextToken: undefined };
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return {
|
|
174
|
+
items: (data || []) as ProductInstance[],
|
|
175
|
+
nextToken,
|
|
176
|
+
};
|
|
177
|
+
} catch (error) {
|
|
178
|
+
console.error(
|
|
179
|
+
"[listProductInstancesByBrand] Error fetching product instances:",
|
|
180
|
+
error,
|
|
181
|
+
);
|
|
182
|
+
throw error;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* List product instances by account ID
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* ```typescript
|
|
191
|
+
* import { listProductInstancesByAccount } from '@htlkg/data/queries';
|
|
192
|
+
* import { generateClient } from '@htlkg/data/client';
|
|
193
|
+
*
|
|
194
|
+
* const client = generateClient<Schema>();
|
|
195
|
+
* const instances = await listProductInstancesByAccount(client, 'account-123');
|
|
196
|
+
* ```
|
|
197
|
+
*/
|
|
198
|
+
export async function listProductInstancesByAccount<TClient = any>(
|
|
199
|
+
client: TClient,
|
|
200
|
+
accountId: string,
|
|
201
|
+
options?: {
|
|
202
|
+
limit?: number;
|
|
203
|
+
nextToken?: string;
|
|
204
|
+
},
|
|
205
|
+
): Promise<{ items: ProductInstance[]; nextToken?: string }> {
|
|
206
|
+
try {
|
|
207
|
+
const { data, errors, nextToken } = await (
|
|
208
|
+
client as any
|
|
209
|
+
).models.ProductInstance.list({
|
|
210
|
+
filter: { accountId: { eq: accountId } },
|
|
211
|
+
...options,
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
if (errors) {
|
|
215
|
+
console.error("[listProductInstancesByAccount] GraphQL errors:", errors);
|
|
216
|
+
return { items: [], nextToken: undefined };
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return {
|
|
220
|
+
items: (data || []) as ProductInstance[],
|
|
221
|
+
nextToken,
|
|
222
|
+
};
|
|
223
|
+
} catch (error) {
|
|
224
|
+
console.error(
|
|
225
|
+
"[listProductInstancesByAccount] Error fetching product instances:",
|
|
226
|
+
error,
|
|
227
|
+
);
|
|
228
|
+
throw error;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* List enabled product instances by brand ID
|
|
234
|
+
*
|
|
235
|
+
* @example
|
|
236
|
+
* ```typescript
|
|
237
|
+
* import { listEnabledProductInstancesByBrand } from '@htlkg/data/queries';
|
|
238
|
+
* import { generateClient } from '@htlkg/data/client';
|
|
239
|
+
*
|
|
240
|
+
* const client = generateClient<Schema>();
|
|
241
|
+
* const instances = await listEnabledProductInstancesByBrand(client, 'brand-123');
|
|
242
|
+
* ```
|
|
243
|
+
*/
|
|
244
|
+
export async function listEnabledProductInstancesByBrand<TClient = any>(
|
|
245
|
+
client: TClient,
|
|
246
|
+
brandId: string,
|
|
247
|
+
options?: {
|
|
248
|
+
limit?: number;
|
|
249
|
+
nextToken?: string;
|
|
250
|
+
},
|
|
251
|
+
): Promise<{ items: ProductInstance[]; nextToken?: string }> {
|
|
252
|
+
try {
|
|
253
|
+
const { data, errors, nextToken } = await (
|
|
254
|
+
client as any
|
|
255
|
+
).models.ProductInstance.list({
|
|
256
|
+
filter: {
|
|
257
|
+
brandId: { eq: brandId },
|
|
258
|
+
enabled: { eq: true },
|
|
259
|
+
},
|
|
260
|
+
...options,
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
if (errors) {
|
|
264
|
+
console.error(
|
|
265
|
+
"[listEnabledProductInstancesByBrand] GraphQL errors:",
|
|
266
|
+
errors,
|
|
267
|
+
);
|
|
268
|
+
return { items: [], nextToken: undefined };
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
return {
|
|
272
|
+
items: (data || []) as ProductInstance[],
|
|
273
|
+
nextToken,
|
|
274
|
+
};
|
|
275
|
+
} catch (error) {
|
|
276
|
+
console.error(
|
|
277
|
+
"[listEnabledProductInstancesByBrand] Error fetching product instances:",
|
|
278
|
+
error,
|
|
279
|
+
);
|
|
280
|
+
throw error;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# Queries Module
|
|
2
|
+
|
|
3
|
+
GraphQL query functions for fetching data from Amplify models.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
import {
|
|
9
|
+
fetchAccounts,
|
|
10
|
+
fetchBrands,
|
|
11
|
+
fetchUsers,
|
|
12
|
+
fetchProducts,
|
|
13
|
+
} from '@htlkg/data/queries';
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Available Queries
|
|
17
|
+
|
|
18
|
+
### fetchAccounts
|
|
19
|
+
|
|
20
|
+
```typescript
|
|
21
|
+
const accounts = await fetchAccounts(client, {
|
|
22
|
+
filter: { status: { eq: 'active' } },
|
|
23
|
+
limit: 50,
|
|
24
|
+
});
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### fetchBrands
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
const brands = await fetchBrands(client, {
|
|
31
|
+
accountId: 'account-123',
|
|
32
|
+
activeOnly: true,
|
|
33
|
+
limit: 100,
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### fetchUsers
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
const users = await fetchUsers(client, {
|
|
41
|
+
brandId: 'brand-456',
|
|
42
|
+
accountId: 'account-123',
|
|
43
|
+
});
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### fetchProducts
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
const products = await fetchProducts(client, {
|
|
50
|
+
activeOnly: true,
|
|
51
|
+
});
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Server-Side Usage
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
import { fetchBrands } from '@htlkg/data/queries';
|
|
58
|
+
import { generateServerClient } from '@htlkg/data/client';
|
|
59
|
+
|
|
60
|
+
const result = await runWithAmplifyServerContext({
|
|
61
|
+
astroServerContext: { cookies: Astro.cookies, request: Astro.request },
|
|
62
|
+
operation: async () => {
|
|
63
|
+
const client = generateServerClient<Schema>();
|
|
64
|
+
return await fetchBrands(client, { accountId });
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Filter Options
|
|
70
|
+
|
|
71
|
+
All queries support GraphQL filter syntax:
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
// Equality
|
|
75
|
+
{ status: { eq: 'active' } }
|
|
76
|
+
|
|
77
|
+
// Contains
|
|
78
|
+
{ name: { contains: 'hotel' } }
|
|
79
|
+
|
|
80
|
+
// Begins with
|
|
81
|
+
{ email: { beginsWith: 'admin' } }
|
|
82
|
+
|
|
83
|
+
// Comparison
|
|
84
|
+
{ createdAt: { gt: '2024-01-01' } }
|
|
85
|
+
|
|
86
|
+
// Combining filters
|
|
87
|
+
{ and: [{ status: { eq: 'active' } }, { accountId: { eq: '123' } }] }
|
|
88
|
+
```
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server-Side Query Helpers
|
|
3
|
+
*
|
|
4
|
+
* Convenience functions for executing server-side queries in Astro pages.
|
|
5
|
+
* These helpers simplify the common pattern of using runWithAmplifyServerContext.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { AstroGlobal } from "astro";
|
|
9
|
+
import { generateServerClient } from "../client";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Type for the runWithAmplifyServerContext function
|
|
13
|
+
*/
|
|
14
|
+
type RunWithAmplifyServerContext = (options: {
|
|
15
|
+
astroServerContext: {
|
|
16
|
+
cookies: AstroGlobal["cookies"];
|
|
17
|
+
request: AstroGlobal["request"];
|
|
18
|
+
};
|
|
19
|
+
operation: (contextSpec: unknown) => Promise<unknown>;
|
|
20
|
+
}) => Promise<unknown>;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Helper to execute server-side queries in Astro pages
|
|
24
|
+
*
|
|
25
|
+
* This function wraps the common pattern of using runWithAmplifyServerContext
|
|
26
|
+
* with generateServerClient, making it easier to fetch data server-side.
|
|
27
|
+
*
|
|
28
|
+
* **Note**: This requires `createRunWithAmplifyServerContext` from `@htlkg/core/amplify-astro-adapter`.
|
|
29
|
+
* If you need more control, use the full pattern directly.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* import type { Schema } from '../amplify/data/resource';
|
|
34
|
+
* import { executeServerQuery } from '@htlkg/data/queries/server-helpers';
|
|
35
|
+
* import { createRunWithAmplifyServerContext } from '@htlkg/core/amplify-astro-adapter';
|
|
36
|
+
* import outputs from '../amplify_outputs.json';
|
|
37
|
+
*
|
|
38
|
+
* const runWithAmplifyServerContext = createRunWithAmplifyServerContext({ config: outputs });
|
|
39
|
+
*
|
|
40
|
+
* const users = await executeServerQuery<Schema>(
|
|
41
|
+
* Astro,
|
|
42
|
+
* runWithAmplifyServerContext,
|
|
43
|
+
* async (client) => {
|
|
44
|
+
* const result = await client.models.User.list();
|
|
45
|
+
* return result.data || [];
|
|
46
|
+
* }
|
|
47
|
+
* );
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export async function executeServerQuery<
|
|
51
|
+
TSchema extends Record<string, unknown>,
|
|
52
|
+
TResult,
|
|
53
|
+
>(
|
|
54
|
+
astro: AstroGlobal,
|
|
55
|
+
runWithAmplifyServerContext: RunWithAmplifyServerContext,
|
|
56
|
+
operation: (client: ReturnType<typeof generateServerClient<TSchema>>) => Promise<TResult>,
|
|
57
|
+
): Promise<TResult> {
|
|
58
|
+
const result = await runWithAmplifyServerContext({
|
|
59
|
+
astroServerContext: {
|
|
60
|
+
cookies: astro.cookies,
|
|
61
|
+
request: astro.request,
|
|
62
|
+
},
|
|
63
|
+
operation: async (contextSpec: unknown) => {
|
|
64
|
+
const client = generateServerClient<TSchema>();
|
|
65
|
+
return await operation(client);
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
return result as TResult;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Helper to execute server-side queries with API key authentication (public data)
|
|
73
|
+
*
|
|
74
|
+
* Use this for fetching public data that doesn't require user authentication.
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```typescript
|
|
78
|
+
* import type { Schema } from '../amplify/data/resource';
|
|
79
|
+
* import { executePublicQuery } from '@htlkg/data/queries/server-helpers';
|
|
80
|
+
* import { createRunWithAmplifyServerContext } from '@htlkg/core/amplify-astro-adapter';
|
|
81
|
+
* import outputs from '../amplify_outputs.json';
|
|
82
|
+
*
|
|
83
|
+
* const runWithAmplifyServerContext = createRunWithAmplifyServerContext({ config: outputs });
|
|
84
|
+
*
|
|
85
|
+
* const brands = await executePublicQuery<Schema>(
|
|
86
|
+
* Astro,
|
|
87
|
+
* runWithAmplifyServerContext,
|
|
88
|
+
* async (client) => {
|
|
89
|
+
* const result = await client.models.Brand.list();
|
|
90
|
+
* return result.data || [];
|
|
91
|
+
* }
|
|
92
|
+
* );
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
export async function executePublicQuery<
|
|
96
|
+
TSchema extends Record<string, unknown>,
|
|
97
|
+
TResult,
|
|
98
|
+
>(
|
|
99
|
+
astro: AstroGlobal,
|
|
100
|
+
runWithAmplifyServerContext: RunWithAmplifyServerContext,
|
|
101
|
+
operation: (client: ReturnType<typeof generateServerClient<TSchema>>) => Promise<TResult>,
|
|
102
|
+
): Promise<TResult> {
|
|
103
|
+
const result = await runWithAmplifyServerContext({
|
|
104
|
+
astroServerContext: {
|
|
105
|
+
cookies: astro.cookies,
|
|
106
|
+
request: astro.request,
|
|
107
|
+
},
|
|
108
|
+
operation: async (_contextSpec: unknown) => {
|
|
109
|
+
const client = generateServerClient<TSchema>({ authMode: "apiKey" });
|
|
110
|
+
return await operation(client);
|
|
111
|
+
},
|
|
112
|
+
});
|
|
113
|
+
return result as TResult;
|
|
114
|
+
}
|