@tanglemedia/svelte-starter-directus-api 4.0.0 → 4.0.2
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.
|
@@ -38,20 +38,24 @@ export class DirectusApiAdapter extends ApiAdapterAbstract {
|
|
|
38
38
|
return null;
|
|
39
39
|
}
|
|
40
40
|
const { filter, search } = query || {};
|
|
41
|
-
// default to
|
|
41
|
+
// default to count. Note that there is a bug in directus where
|
|
42
42
|
// the wrong counts are being returned if there are nested filters.
|
|
43
|
-
|
|
43
|
+
// A quick fix is to set totalAggregate to countDistinct
|
|
44
|
+
const k = this.config.configuration.totalAggregate || 'count';
|
|
45
|
+
const f = this.config.configuration.totalField || 'id';
|
|
44
46
|
const data = await this.directus.request(aggregate(collection, {
|
|
45
|
-
aggregate: { [k]:
|
|
47
|
+
aggregate: { [k]: f },
|
|
46
48
|
query: {
|
|
47
49
|
...(filter ? { filter } : {}),
|
|
48
|
-
...(search ? { search } : {})
|
|
50
|
+
...(search ? { search } : {}),
|
|
49
51
|
}
|
|
50
52
|
}));
|
|
53
|
+
let total = 0;
|
|
51
54
|
if (data && data.length === 1) {
|
|
52
|
-
|
|
55
|
+
const c = data[0][k];
|
|
56
|
+
total = c && typeof c === 'object' ? Number(c[f]) : Number(c);
|
|
53
57
|
}
|
|
54
|
-
return 0;
|
|
58
|
+
return isNaN(total) ? 0 : total;
|
|
55
59
|
}
|
|
56
60
|
async transformResponse(res, status = 200) {
|
|
57
61
|
return {
|
|
@@ -88,7 +92,7 @@ export class DirectusApiAdapter extends ApiAdapterAbstract {
|
|
|
88
92
|
}
|
|
89
93
|
}
|
|
90
94
|
async find(collection, query) {
|
|
91
|
-
const response =
|
|
95
|
+
const response = await this.directus.request(readItems(collection, query));
|
|
92
96
|
const total = await this.includeTotals(collection, query);
|
|
93
97
|
if (null === total) {
|
|
94
98
|
return this.transformResponse({ data: response });
|
|
@@ -101,13 +105,11 @@ export class DirectusApiAdapter extends ApiAdapterAbstract {
|
|
|
101
105
|
return this.transformResponse({ data });
|
|
102
106
|
}
|
|
103
107
|
async aggregate(collection, query) {
|
|
104
|
-
const { aggregate: aggregate_, ...rest } = query || {};
|
|
108
|
+
const { aggregate: aggregate_, ...rest } = (query || {});
|
|
105
109
|
const data = await this.directus.request(aggregate(collection, {
|
|
106
|
-
aggregate: aggregate_
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
count: 'id'
|
|
110
|
-
},
|
|
110
|
+
aggregate: aggregate_ ? aggregate_ : {
|
|
111
|
+
count: 'id'
|
|
112
|
+
},
|
|
111
113
|
query: rest
|
|
112
114
|
}));
|
|
113
115
|
return this.transformResponse({ data });
|
|
@@ -126,7 +128,7 @@ export class DirectusApiAdapter extends ApiAdapterAbstract {
|
|
|
126
128
|
}
|
|
127
129
|
async put(collection, key, payload) {
|
|
128
130
|
if (key) {
|
|
129
|
-
const data =
|
|
131
|
+
const data = await this.directus.request(updateItem(collection, key, payload));
|
|
130
132
|
return this.transformResponse({ data }, 201);
|
|
131
133
|
}
|
|
132
134
|
else {
|
|
@@ -20,6 +20,7 @@ export type DirectusApiAdapterOptions = {
|
|
|
20
20
|
};
|
|
21
21
|
includeTotals?: boolean;
|
|
22
22
|
totalAggregate?: 'count' | 'countDistinct';
|
|
23
|
+
totalField?: string;
|
|
23
24
|
};
|
|
24
25
|
export type DirectusConfig = BaseApiAdapterConfig<DirectusApiAdapterOptions>;
|
|
25
26
|
export type SchemaShape = Record<string, object>;
|
package/package.json
CHANGED
|
@@ -85,23 +85,28 @@ export class DirectusApiAdapter<
|
|
|
85
85
|
|
|
86
86
|
const { filter, search } = query || {};
|
|
87
87
|
|
|
88
|
-
// default to
|
|
88
|
+
// default to count. Note that there is a bug in directus where
|
|
89
89
|
// the wrong counts are being returned if there are nested filters.
|
|
90
|
-
|
|
90
|
+
// A quick fix is to set totalAggregate to countDistinct
|
|
91
|
+
const k = this.config.configuration.totalAggregate || 'count';
|
|
92
|
+
const f = this.config.configuration.totalField || 'id';
|
|
91
93
|
|
|
92
94
|
const data = await this.directus.request(aggregate(collection, {
|
|
93
|
-
aggregate: { [k]:
|
|
95
|
+
aggregate: { [k]: f },
|
|
94
96
|
query: {
|
|
95
97
|
...(filter ? { filter } : {}),
|
|
96
98
|
...(search ? { search } : {}),
|
|
97
99
|
}
|
|
98
100
|
}));
|
|
99
101
|
|
|
102
|
+
let total = 0;
|
|
103
|
+
|
|
100
104
|
if (data && data.length === 1) {
|
|
101
|
-
|
|
105
|
+
const c = data[0][k];
|
|
106
|
+
total = c && typeof c === 'object' ? Number(c[f]) : Number(c);
|
|
102
107
|
}
|
|
103
108
|
|
|
104
|
-
return 0;
|
|
109
|
+
return isNaN(total) ? 0 : total;
|
|
105
110
|
}
|
|
106
111
|
|
|
107
112
|
async transformResponse<T, M extends object = AnyObject>(
|