Skip to content
This repository was archived by the owner on Feb 21, 2024. It is now read-only.

Commit b68915d

Browse files
committed
add options
1 parent 227a4b8 commit b68915d

File tree

4 files changed

+54
-17
lines changed

4 files changed

+54
-17
lines changed

Diff for: .eleventy.js

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ module.exports = (eleventyConfig, _options) => {
99
allowSystem: false,
1010
onItem: item => item,
1111
filterCollection: ({collection, meta}) => true,
12+
sequencial: false,
13+
recursions: 7,
1214
}
1315

1416
const options = {
@@ -23,6 +25,8 @@ module.exports = (eleventyConfig, _options) => {
2325
login: options.login,
2426
token: options.token,
2527
auth: options.auth,
28+
sequencial: options.sequencial,
29+
recursions: options.recursions,
2630
})
2731

2832
// Add global data and init

Diff for: README.md

+2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ For this plugin to retrieve **private** data, use the `skd.authenticated` option
8383
| filterCollection | | |
8484
| allowHidden | Keep hidden collections in directus.collections.all | `true` |
8585
| allowSystem | Keep system collections in directus.collections.all | `true` |
86+
| sequencial | Should all collections and their items be fetched sequencially or in parallel | `false` |
87+
| recursions | How many levels of recursions do you need? This uses the `fields` field of requests. | `7` |
8688

8789
### Config Examples
8890

Diff for: _scripts/client.js

+26-12
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ const OPTION_LIMIT = (limit = -1) => ({
55
limit,
66
})
77

8-
const OPTION_WITH_FIELDS = () => ({
9-
fields: [
10-
'*.*.*.*.*.*.*', // this is for all fields on multiple levels
11-
],
12-
})
13-
148
const DIRECTUS_UPLOAD_FOLDER = process.env.DIRECTUS_UPLOAD_FOLDER
159

10+
async function allSync(promises) {
11+
if (promises.length === 0) return []
12+
const [firstElement, ...rest] = promises
13+
return [await firstElement, ...(await allSync(rest))]
14+
}
15+
1616
/**
1717
* @class Retrieve content from Directus
1818
*/
@@ -24,6 +24,8 @@ class Client {
2424
token,
2525
allowHidden,
2626
allowSystem,
27+
sequencial,
28+
recursions,
2729
}) {
2830
// init attributes
2931
this.url = url
@@ -33,6 +35,13 @@ class Client {
3335
this.eleventyCollections = null
3436
this.allowHidden = allowHidden
3537
this.allowSystem = allowSystem
38+
this.sequencial = sequencial
39+
this.fields = {
40+
// Get all fields on multiple levels
41+
fields: [
42+
'*'.repeat(recursions).split('').join('.'),
43+
],
44+
}
3645

3746
// init directus client
3847
this.directus = new Directus(url, { auth })
@@ -56,7 +65,7 @@ class Client {
5665
*/
5766
async init(onItem = item => item, filterCollection = () => true) {
5867
if(this.initDone) {
59-
return;
68+
return
6069
}
6170
this.initDone = true
6271
this.login && await this.directus.auth.login(this.login)
@@ -92,7 +101,7 @@ class Client {
92101
// const collections = await this.directus.collections()
93102
const { data } = await this.directus.items('directus_collections').readByQuery({
94103
...OPTION_LIMIT(),
95-
...OPTION_WITH_FIELDS(),
104+
...this.fields,
96105
...options,
97106
})
98107
return data
@@ -114,7 +123,7 @@ class Client {
114123
try {
115124
const { data } = await this.directus.items(name).readByQuery({
116125
...OPTION_LIMIT(),
117-
...OPTION_WITH_FIELDS(),
126+
...this.fields,
118127
...options,
119128
})
120129
return data
@@ -189,7 +198,7 @@ class Client {
189198
*/
190199
async get11tyCollectionsExpanded(onItem, filterCollection) {
191200
const collections = await this.get11tyCollections(filterCollection)
192-
return Promise.all(collections.map(async collection => {
201+
const promises = collections.map(async collection => {
193202
const data = await this.get11tyCollection(collection)
194203
if(!data) {
195204
console.warn('WARN: directus did not return any data for collection', collection)
@@ -200,11 +209,16 @@ class Client {
200209
return data
201210
}
202211

203-
let res = await Promise.all(data.map(onItem));
212+
const onItemCbks = data.map(onItem)
213+
const res = this.sequencial ?
214+
await allSync(onItemCbks) :
215+
await Promise.all(onItemCbks)
216+
204217
res.__metadata = collection // FIXME: this adds a property to the array
205218

206219
return res
207-
}))
220+
})
221+
return this.sequencial ? allSync(promises) : Promise.all(promises)
208222
.then(collections => collections
209223
.filter(collection => !!collection) // filter the cases when directus errored
210224
)

Diff for: _scripts/client.test.js

+22-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const { start, stop } = require('./directus-server/')
44
const port = 8056
55
const OPTIONS = {
66
url: `http://localhost:${port}`,
7+
sequencial: true,
8+
recursions: 7,
79
}
810
const NUM_COLLECTIONS = 5 // page, post, settings, multilingual, languages
911

@@ -63,7 +65,6 @@ describe('API', () => {
6365
expect(page).not.toBeUndefined()
6466
expect(page).toBeInstanceOf(Array)
6567
expect(page.length).toBeGreaterThan(0)
66-
console.log(page)
6768
expect(page[0].text).toBe('Home page')
6869
expect(post).not.toBeUndefined()
6970
expect(post).toBeInstanceOf(Array)
@@ -73,10 +74,8 @@ describe('API', () => {
7374
expect(settings).toBeInstanceOf(Object)
7475
expect(settings.test_property).toBe('test value')
7576
})
76-
test('getAll', async () => {
77-
})
78-
test('collections', async () => {
79-
})
77+
// TODO: test('getAll', async () => {})
78+
// TODO: test('collections', async () => {})
8079
test('not multilingual', async () => {
8180
const {page} = await client.getCollections()
8281
expect(client.translate(page[0], null, 'en-US').lang).toBeUndefined()
@@ -96,5 +95,23 @@ describe('API', () => {
9695
expect(translated.text).not.toBeUndefined()
9796
expect(translated.text.startsWith('text '))
9897
})
98+
test('options recursions', async () => {
99+
// With the default recursions
100+
let result = await client.getCollections()
101+
expect(result.page).toBeInstanceOf(Array)
102+
result.page.forEach(p => {
103+
expect(p.user_created).toBeUndefined() // users is not public
104+
})
105+
client = await createClient({
106+
...OPTIONS,
107+
recursions: 1,
108+
})
109+
await client.init()
110+
result = await client.getCollections()
111+
expect(result.page).toBeInstanceOf(Array)
112+
result.page.forEach(p => {
113+
expect(typeof p.user_created).toBe('string')
114+
})
115+
})
99116
})
100117

0 commit comments

Comments
 (0)