@tanglemedia/svelte-starter-directus-api 2.0.2 → 2.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.
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import { type AllCollections, type DirectusClient, type RegularCollections, type RestClient } from '@directus/sdk';
|
|
2
|
-
import { ApiAdapterAbstract, type AnyObject, type ApiAdapterRequestConfig, type ApiCreateManyQuery, type ApiDeleteManyQuery, type ApiId, type ApiResponse, type ApiUpdateManyQuery, type BaseApiMethods } from '@tanglemedia/svelte-starter-core';
|
|
2
|
+
import { ApiAdapterAbstract, type AnyObject, type ApiAdapterRequestConfig, type ApiCreateManyQuery, type ApiDeleteManyQuery, type ApiFindQuery, type ApiId, type ApiResponse, type ApiUpdateManyQuery, type BaseApiMethods } from '@tanglemedia/svelte-starter-core';
|
|
3
3
|
import type { DirectusConfig, SchemaShape } from '../types/adapter.types';
|
|
4
4
|
export type AdapterClient<Schema extends SchemaShape = SchemaShape> = DirectusClient<Schema> & RestClient<Schema>;
|
|
5
|
+
/**
|
|
6
|
+
* TODO: Various types fixes. The current adapter doesn't correctly follow the types defined under the ApiAdapterAbstract.
|
|
7
|
+
* See the fetch adapter under the core
|
|
8
|
+
*/
|
|
5
9
|
export declare class DirectusApiAdapter<Schema extends SchemaShape = SchemaShape, Path extends RegularCollections<Schema> = RegularCollections<Schema>> extends ApiAdapterAbstract<DirectusConfig, ApiAdapterRequestConfig, Path, AdapterClient> {
|
|
6
10
|
protected config: DirectusConfig;
|
|
7
11
|
private readonly directus;
|
|
@@ -14,20 +18,20 @@ export declare class DirectusApiAdapter<Schema extends SchemaShape = SchemaShape
|
|
|
14
18
|
total: number;
|
|
15
19
|
displayed: number;
|
|
16
20
|
};
|
|
17
|
-
includeTotals(collection: AllCollections<Schema>,
|
|
21
|
+
includeTotals(collection: AllCollections<Schema>, query?: ApiFindQuery<any>): Promise<number | null>;
|
|
18
22
|
transformResponse<T, M extends object = AnyObject>(res: {
|
|
19
23
|
data: T;
|
|
20
24
|
meta?: AnyObject;
|
|
21
25
|
}, status?: number): Promise<ApiResponse<T, Response, M>>;
|
|
22
26
|
getConfig(configuration?: unknown): DirectusConfig;
|
|
23
27
|
request<T>(method: BaseApiMethods, url: string, query?: Record<string, unknown>): Promise<T>;
|
|
24
|
-
find<T>(collection: Path, query
|
|
28
|
+
find<T, Q = T, R = T>(collection: Path, query: ApiFindQuery<Q>): Promise<ApiResponse<R[]>>;
|
|
25
29
|
findOne<T>(collection: Path, key?: ApiId, query?: Record<string, unknown>): Promise<ApiResponse<T>>;
|
|
26
30
|
aggregate<T>(collection: Path, query?: Record<string, unknown>): Promise<ApiResponse<T>>;
|
|
27
31
|
patch<T>(collection: Path, key: ApiId, query?: Record<string, unknown>): Promise<ApiResponse<T>>;
|
|
28
32
|
post<T>(collection: Path, data: AnyObject, query?: Record<string, unknown>): Promise<ApiResponse<T>>;
|
|
29
33
|
delete<T>(collection: Path, key?: ApiId): Promise<ApiResponse<T>>;
|
|
30
|
-
put<T>(collection: Path, key?: ApiId, payload?: AnyObject): Promise<ApiResponse<
|
|
34
|
+
put<T extends object, R = T>(collection: Path, key?: ApiId, payload?: AnyObject): Promise<ApiResponse<R>>;
|
|
31
35
|
upload<T>(path: string | undefined, data: FormData): Promise<ApiResponse<T>>;
|
|
32
36
|
createMany<T, R = T>(path: Path, body: ApiCreateManyQuery<T>): Promise<ApiResponse<R[], Response, AnyObject>>;
|
|
33
37
|
updateMany<T, R = T>(path: Path, body: ApiUpdateManyQuery<T>): Promise<ApiResponse<R[], Response, AnyObject>>;
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { aggregate, createItem, createItems, deleteItem, deleteItems, readItem, readItems, updateItem, updateItems, uploadFiles } from '@directus/sdk';
|
|
2
2
|
import { ApiAdapterAbstract } from '@tanglemedia/svelte-starter-core';
|
|
3
|
+
/**
|
|
4
|
+
* TODO: Various types fixes. The current adapter doesn't correctly follow the types defined under the ApiAdapterAbstract.
|
|
5
|
+
* See the fetch adapter under the core
|
|
6
|
+
*/
|
|
3
7
|
export class DirectusApiAdapter extends ApiAdapterAbstract {
|
|
4
8
|
config;
|
|
5
9
|
directus;
|
|
@@ -25,13 +29,14 @@ export class DirectusApiAdapter extends ApiAdapterAbstract {
|
|
|
25
29
|
// consider pulling count based on existing filters in order to determine pagination
|
|
26
30
|
return { total, displayed };
|
|
27
31
|
}
|
|
28
|
-
async includeTotals(collection,
|
|
32
|
+
async includeTotals(collection, query) {
|
|
29
33
|
if (true !== this.config.configuration.includeTotals) {
|
|
30
34
|
return null;
|
|
31
35
|
}
|
|
36
|
+
const { filter, search } = query || {};
|
|
32
37
|
const data = await this.directus.request(aggregate(collection, {
|
|
33
38
|
aggregate: { count: '*' },
|
|
34
|
-
query: { filter }
|
|
39
|
+
query: { filter, search }
|
|
35
40
|
}));
|
|
36
41
|
if (data && data.length === 1) {
|
|
37
42
|
return Number(data[0].count);
|
|
@@ -74,7 +79,7 @@ export class DirectusApiAdapter extends ApiAdapterAbstract {
|
|
|
74
79
|
}
|
|
75
80
|
async find(collection, query) {
|
|
76
81
|
const response = await this.directus.request(readItems(collection, query));
|
|
77
|
-
const total = await this.includeTotals(collection, query
|
|
82
|
+
const total = await this.includeTotals(collection, query);
|
|
78
83
|
if (null === total) {
|
|
79
84
|
return this.transformResponse({ data: response });
|
|
80
85
|
}
|
package/package.json
CHANGED
|
@@ -20,6 +20,7 @@ import {
|
|
|
20
20
|
type ApiAdapterRequestConfig,
|
|
21
21
|
type ApiCreateManyQuery,
|
|
22
22
|
type ApiDeleteManyQuery,
|
|
23
|
+
type ApiFindQuery,
|
|
23
24
|
type ApiId,
|
|
24
25
|
type ApiResponse,
|
|
25
26
|
type ApiUpdateManyQuery,
|
|
@@ -30,6 +31,10 @@ import type { DirectusConfig, SchemaShape } from '../types/adapter.types';
|
|
|
30
31
|
export type AdapterClient<Schema extends SchemaShape = SchemaShape> = DirectusClient<Schema> &
|
|
31
32
|
RestClient<Schema>;
|
|
32
33
|
|
|
34
|
+
/**
|
|
35
|
+
* TODO: Various types fixes. The current adapter doesn't correctly follow the types defined under the ApiAdapterAbstract.
|
|
36
|
+
* See the fetch adapter under the core
|
|
37
|
+
*/
|
|
33
38
|
export class DirectusApiAdapter<
|
|
34
39
|
Schema extends SchemaShape = SchemaShape,
|
|
35
40
|
Path extends RegularCollections<Schema> = RegularCollections<Schema>
|
|
@@ -65,14 +70,16 @@ export class DirectusApiAdapter<
|
|
|
65
70
|
return { total, displayed };
|
|
66
71
|
}
|
|
67
72
|
|
|
68
|
-
async includeTotals(collection: AllCollections<Schema>,
|
|
73
|
+
async includeTotals(collection: AllCollections<Schema>, query?: ApiFindQuery<any>) {
|
|
69
74
|
if (true !== this.config.configuration.includeTotals) {
|
|
70
75
|
return null;
|
|
71
76
|
}
|
|
72
77
|
|
|
78
|
+
const { filter, search } = query || {};
|
|
79
|
+
|
|
73
80
|
const data = await this.directus.request(aggregate(collection, {
|
|
74
81
|
aggregate: { count: '*' },
|
|
75
|
-
query: { filter }
|
|
82
|
+
query: { filter, search }
|
|
76
83
|
}));
|
|
77
84
|
|
|
78
85
|
if (data && data.length === 1) {
|
|
@@ -130,9 +137,9 @@ export class DirectusApiAdapter<
|
|
|
130
137
|
}
|
|
131
138
|
}
|
|
132
139
|
|
|
133
|
-
public async find<T>(collection: Path, query
|
|
134
|
-
const response = await this.directus.request<T>(readItems(collection, query));
|
|
135
|
-
const total = await this.includeTotals(collection, query
|
|
140
|
+
public async find<T, Q = T, R = T>(collection: Path, query: ApiFindQuery<Q>): Promise<ApiResponse<R[]>> {
|
|
141
|
+
const response = await this.directus.request<T>(readItems(collection, query)) as R[];
|
|
142
|
+
const total = await this.includeTotals(collection, query);
|
|
136
143
|
if (null === total) {
|
|
137
144
|
return this.transformResponse({ data: response });
|
|
138
145
|
}
|
|
@@ -188,9 +195,9 @@ export class DirectusApiAdapter<
|
|
|
188
195
|
return this.transformResponse({ data }, 202);
|
|
189
196
|
}
|
|
190
197
|
|
|
191
|
-
public async put<T>(collection: Path, key?: ApiId, payload?: AnyObject): Promise<ApiResponse<
|
|
198
|
+
public async put<T extends object, R = T>(collection: Path, key?: ApiId, payload?: AnyObject): Promise<ApiResponse<R>> {
|
|
192
199
|
if (key) {
|
|
193
|
-
const data = await this.directus.request<T>(updateItem(collection, key, payload));
|
|
200
|
+
const data = await this.directus.request<T>(updateItem(collection, key, payload)) as R;
|
|
194
201
|
return this.transformResponse({ data }, 201);
|
|
195
202
|
} else {
|
|
196
203
|
console.error(`Error updating all ${collection}: no key specified`);
|