Skip to content

Commit 0825f15

Browse files
committed
refactor: reworks and cleanups
1 parent 7946f40 commit 0825f15

File tree

3 files changed

+293
-38
lines changed

3 files changed

+293
-38
lines changed

README.md

Lines changed: 107 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,16 @@ try {
7373
url: import.meta.env.STRAPI_URL,
7474
token: import.meta.env.STRAPI_TOKEN,
7575
}, [{ // leave empty [] to fetch all the collections based on default Strapi settings
76-
name: "my-collection",
76+
name: "homepage",
7777
query: {
78-
populate: {
79-
// ...
80-
},
78+
populate: { seo: true },
8179
},
82-
}, 'yet-another-collection']);
80+
}, {
81+
name: "layout",
82+
query: {
83+
populate: { header: true, footer: true },
84+
},
85+
}, 'simple-collection-name']); // Can also pass just strings
8386
} catch (error) {
8487
console.error(error);
8588
}
@@ -89,6 +92,8 @@ export const collections = {
8992
};
9093
```
9194

95+
> **✅ Backward Compatible:** Existing code works without any changes!
96+
9297
2. Use in your Astro component:
9398

9499
```jsx
@@ -140,12 +145,87 @@ const myCollection = await fetchContent({
140145

141146
> **⚠️ Note:** The token must have **read access** to both the **Content API** and the **Content-Type Builder API** *(ONLY to the "Get Content Types" endpoint)*.
142147
148+
### Mixing 1.0.x and 1.1.0+
149+
150+
You can mix old (simple) and new (extended) format in a single `generateCollections` call:
151+
152+
```typescript
153+
strapiCollections = await generateCollections({
154+
url: import.meta.env.STRAPI_URL,
155+
token: import.meta.env.STRAPI_TOKEN,
156+
}, [
157+
// ✅ Old format - works as before
158+
{
159+
name: "homepage",
160+
query: { populate: { seo: true } }
161+
},
162+
{
163+
name: "layout",
164+
query: { populate: { header: true, footer: true } }
165+
},
166+
167+
// ✅ 1.1.0+ - with locale support
168+
{
169+
name: "pages",
170+
collectionName: "pagesEN",
171+
locale: "en",
172+
query: { sort: ['publishedAt:desc'] }
173+
},
174+
{
175+
name: "pages", // Same endpoint, different config!
176+
collectionName: "pagesDE",
177+
locale: "de",
178+
query: { sort: ['publishedAt:desc'] }
179+
},
180+
181+
// ✅ 1.1.0+ - with custom ID
182+
{
183+
name: "blog-posts",
184+
idGenerator: (data) => data.slug as string,
185+
query: { filters: { published: true } }
186+
},
187+
188+
// ✅ 1.1.0+ - combining all features
189+
{
190+
name: "articles",
191+
collectionName: "articlesMultilang",
192+
locale: ["en", "de", "fr"],
193+
idGenerator: (data) => data.slug as string
194+
}
195+
]);
196+
197+
// Result collections:
198+
// - homepage (old format)
199+
// - layout (old format)
200+
// - pagesEN (1.1.0+)
201+
// - pagesDE (1.1.0+)
202+
// - blog-posts (1.1.0+)
203+
// - articlesMultilang (1.1.0+)
204+
```
205+
143206
### Advanced Usage Examples
144207

145208
#### Custom ID Generation
146209

147210
Use slugs or custom fields as collection IDs instead of Strapi's `documentId`:
148211

212+
**Option A:** Using `generateCollections`:
213+
214+
```typescript
215+
strapiCollections = await generateCollections({
216+
url: import.meta.env.STRAPI_URL,
217+
token: import.meta.env.STRAPI_TOKEN,
218+
}, [{
219+
name: "pages",
220+
idGenerator: (data) => data.slug as string,
221+
query: { populate: { seo: true } }
222+
}]);
223+
224+
// Now you can use: getEntry('pages', 'about-us')
225+
```
226+
227+
**Option B:** Using `strapiLoader` directly:
228+
149229
```typescript
150230
import { strapiLoader } from '@sensinum/astro-strapi-loader';
151231
import { defineCollection, z } from 'astro:content';
@@ -162,13 +242,32 @@ const pages = defineCollection({
162242
content: z.string()
163243
})
164244
});
165-
166-
// Now you can use: getEntry('pages', 'about-us')
167245
```
168246

169247
#### Multiple Collections from Same Endpoint
170248

171-
Create separate collections for different languages from the same Strapi content type:
249+
**Option A:** Using `generateCollections` (recommended for multiple collections):
250+
251+
```typescript
252+
strapiCollections = await generateCollections({
253+
url: import.meta.env.STRAPI_URL,
254+
token: import.meta.env.STRAPI_TOKEN,
255+
}, [{
256+
name: "pages",
257+
collectionName: "pagesEN",
258+
locale: "en",
259+
query: { sort: ['publishedAt:desc'] }
260+
}, {
261+
name: "pages", // Same endpoint!
262+
collectionName: "pagesDE",
263+
locale: "de",
264+
query: { sort: ['publishedAt:desc'] }
265+
}]);
266+
267+
// Now you have both 'pagesEN' and 'pagesDE' collections
268+
```
269+
270+
**Option B:** Using `strapiLoader` directly:
172271

173272
```typescript
174273
const pagesEN = defineCollection({

src/utils/__tests__/loader-extended.test.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe("strapiLoader - Extended Features", () => {
3737
});
3838

3939
describe("Custom ID Generator", () => {
40-
it("powinien używać niestandardowego generatora ID", async () => {
40+
it("should use custom ID generator", async () => {
4141
const mockData = [
4242
{ documentId: "1", slug: "test-slug-1", title: "Test Title 1" },
4343
{ documentId: "2", slug: "test-slug-2", title: "Test Title 2" },
@@ -63,7 +63,7 @@ describe("strapiLoader - Extended Features", () => {
6363
});
6464
});
6565

66-
it("powinien używać złożonego generatora ID", async () => {
66+
it("should use complex ID generator", async () => {
6767
const mockData = [
6868
{ documentId: "1", category: "blog", slug: "post-1" },
6969
];
@@ -84,7 +84,7 @@ describe("strapiLoader - Extended Features", () => {
8484
});
8585
});
8686

87-
it("powinien działać dla pojedynczego elementu z niestandardowym ID", async () => {
87+
it("should work for single item with custom ID", async () => {
8888
const mockData = {
8989
documentId: "1",
9090
slug: "single-page",
@@ -108,7 +108,7 @@ describe("strapiLoader - Extended Features", () => {
108108
});
109109

110110
describe("Custom Collection Name", () => {
111-
it("powinien używać niestandardowej nazwy kolekcji", async () => {
111+
it("should use custom collection name", async () => {
112112
const mockData = [
113113
{ documentId: "1", title: "Test Title 1" },
114114
];
@@ -130,7 +130,7 @@ describe("strapiLoader - Extended Features", () => {
130130
);
131131
});
132132

133-
it("powinien używać domyślnej nazwy gdy collectionName nie jest podane", async () => {
133+
it("should use default name when collectionName is not provided", async () => {
134134
const mockData = [
135135
{ documentId: "1", title: "Test Title 1" },
136136
];
@@ -146,7 +146,7 @@ describe("strapiLoader - Extended Features", () => {
146146
});
147147

148148
describe("Locale Support", () => {
149-
it("powinien obsłużyć pojedynczą locale", async () => {
149+
it("should handle single locale", async () => {
150150
const mockData = [
151151
{ documentId: "en-1", title: "English Title", locale: "en" },
152152
{ documentId: "en-2", title: "English Title 2", locale: "en" },
@@ -184,7 +184,7 @@ describe("strapiLoader - Extended Features", () => {
184184
});
185185
});
186186

187-
it("powinien obsłużyć wiele lokalizacji", async () => {
187+
it("should handle multiple locales", async () => {
188188
const mockDataEN = [
189189
{ documentId: "doc1", title: "English Title", locale: "en" },
190190
];
@@ -230,7 +230,7 @@ describe("strapiLoader - Extended Features", () => {
230230
});
231231
});
232232

233-
it("powinien obsłużyć wiele lokalizacji z pustą odpowiedzią dla jednej", async () => {
233+
it("should handle multiple locales with empty response for one", async () => {
234234
const mockDataEN = [
235235
{ documentId: "doc2", title: "English Title", locale: "en" },
236236
];
@@ -255,7 +255,7 @@ describe("strapiLoader - Extended Features", () => {
255255
});
256256
});
257257

258-
it("powinien obsłużyć wiele lokalizacji dla pojedynczego typu", async () => {
258+
it("should handle multiple locales for single type", async () => {
259259
const mockDataEN = { documentId: "home1", title: "English Title", locale: "en" };
260260
const mockDataDE = { documentId: "home1", title: "German Title", locale: "de" };
261261

@@ -283,7 +283,7 @@ describe("strapiLoader - Extended Features", () => {
283283
});
284284
});
285285

286-
it("powinien obsłużyć sytuację gdy wszystkie lokalizacje są puste", async () => {
286+
it("should handle situation when all locales are empty", async () => {
287287
(fetchContent as jest.Mock)
288288
.mockResolvedValueOnce({ data: [] })
289289
.mockResolvedValueOnce({ data: [] });
@@ -300,7 +300,7 @@ describe("strapiLoader - Extended Features", () => {
300300
);
301301
});
302302

303-
it("powinien łączyć locale z innymi parametrami zapytania", async () => {
303+
it("should combine locale with other query parameters", async () => {
304304
const mockData = [
305305
{ documentId: "pub1", title: "Published Title", locale: "en" },
306306
];
@@ -328,7 +328,7 @@ describe("strapiLoader - Extended Features", () => {
328328
});
329329

330330
describe("Combined Features", () => {
331-
it("powinien działać z niestandardowym ID, nazwą kolekcji i locale", async () => {
331+
it("should work with custom ID, collection name and locale", async () => {
332332
const mockDataEN = [
333333
{ documentId: "about-en", slug: "about", title: "About Us" },
334334
];
@@ -365,7 +365,7 @@ describe("strapiLoader - Extended Features", () => {
365365
});
366366
});
367367

368-
it("powinien działać z niestandardowym ID i pojedynczą locale", async () => {
368+
it("should work with custom ID and single locale", async () => {
369369
const mockData = [
370370
{ documentId: "post-doc-1", slug: "post-1", title: "Post 1" },
371371
];
@@ -393,7 +393,7 @@ describe("strapiLoader - Extended Features", () => {
393393
});
394394

395395
describe("Backward Compatibility", () => {
396-
it("powinien działać bez nowych opcji (kompatybilność wsteczna)", async () => {
396+
it("should work without new options (backward compatible)", async () => {
397397
const mockData = [
398398
{ documentId: "bc-1", title: "Test Title 1" },
399399
{ documentId: "bc-2", title: "Test Title 2" },

0 commit comments

Comments
 (0)