@tanglemedia/svelte-starter-directus-api 2.0.2 → 2.0.4

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 ApiAggregateQuery, 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>, filter?: Record<string, unknown>): Promise<number | null>;
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?: Record<string, unknown>): Promise<ApiResponse<T>>;
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
- aggregate<T>(collection: Path, query?: Record<string, unknown>): Promise<ApiResponse<T>>;
30
+ aggregate<T, Q = T, R = T>(collection: Path, query: ApiAggregateQuery<Q>): Promise<ApiResponse<R>>;
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<T>>;
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,17 @@ 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, filter) {
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: {
40
+ ...(filter ? { filter } : {}),
41
+ ...(search ? { search } : {}),
42
+ }
35
43
  }));
36
44
  if (data && data.length === 1) {
37
45
  return Number(data[0].count);
@@ -74,7 +82,7 @@ export class DirectusApiAdapter extends ApiAdapterAbstract {
74
82
  }
75
83
  async find(collection, query) {
76
84
  const response = await this.directus.request(readItems(collection, query));
77
- const total = await this.includeTotals(collection, query.filter);
85
+ const total = await this.includeTotals(collection, query);
78
86
  if (null === total) {
79
87
  return this.transformResponse({ data: response });
80
88
  }
@@ -86,11 +94,12 @@ export class DirectusApiAdapter extends ApiAdapterAbstract {
86
94
  return this.transformResponse({ data });
87
95
  }
88
96
  async aggregate(collection, query) {
97
+ const { aggregate: aggregate_, ...rest } = (query || {});
89
98
  const data = await this.directus.request(aggregate(collection, {
90
- aggregate: {
99
+ aggregate: aggregate_ ? aggregate_ : {
91
100
  count: 'id'
92
101
  },
93
- query
102
+ query: rest
94
103
  }));
95
104
  return this.transformResponse({ data });
96
105
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanglemedia/svelte-starter-directus-api",
3
- "version": "2.0.2",
3
+ "version": "2.0.4",
4
4
  "main": "src/index.ts",
5
5
  "types": "src/index.ts",
6
6
  "description": "directus API wrapper for all the directus sdk functionality",
@@ -18,8 +18,10 @@ import {
18
18
  ApiAdapterAbstract,
19
19
  type AnyObject,
20
20
  type ApiAdapterRequestConfig,
21
+ type ApiAggregateQuery,
21
22
  type ApiCreateManyQuery,
22
23
  type ApiDeleteManyQuery,
24
+ type ApiFindQuery,
23
25
  type ApiId,
24
26
  type ApiResponse,
25
27
  type ApiUpdateManyQuery,
@@ -30,6 +32,10 @@ import type { DirectusConfig, SchemaShape } from '../types/adapter.types';
30
32
  export type AdapterClient<Schema extends SchemaShape = SchemaShape> = DirectusClient<Schema> &
31
33
  RestClient<Schema>;
32
34
 
35
+ /**
36
+ * TODO: Various types fixes. The current adapter doesn't correctly follow the types defined under the ApiAdapterAbstract.
37
+ * See the fetch adapter under the core
38
+ */
33
39
  export class DirectusApiAdapter<
34
40
  Schema extends SchemaShape = SchemaShape,
35
41
  Path extends RegularCollections<Schema> = RegularCollections<Schema>
@@ -65,14 +71,19 @@ export class DirectusApiAdapter<
65
71
  return { total, displayed };
66
72
  }
67
73
 
68
- async includeTotals(collection: AllCollections<Schema>, filter?: Record<string, unknown>) {
74
+ async includeTotals(collection: AllCollections<Schema>, query?: ApiFindQuery<any>) {
69
75
  if (true !== this.config.configuration.includeTotals) {
70
76
  return null;
71
77
  }
72
78
 
79
+ const { filter, search } = query || {};
80
+
73
81
  const data = await this.directus.request(aggregate(collection, {
74
82
  aggregate: { count: '*' },
75
- query: { filter }
83
+ query: {
84
+ ...(filter ? { filter } : {}),
85
+ ...(search ? { search } : {}),
86
+ }
76
87
  }));
77
88
 
78
89
  if (data && data.length === 1) {
@@ -130,9 +141,9 @@ export class DirectusApiAdapter<
130
141
  }
131
142
  }
132
143
 
133
- public async find<T>(collection: Path, query?: Record<string, unknown>): Promise<ApiResponse<T>> {
134
- const response = await this.directus.request<T>(readItems(collection, query));
135
- const total = await this.includeTotals(collection, query.filter);
144
+ public async find<T, Q = T, R = T>(collection: Path, query: ApiFindQuery<Q>): Promise<ApiResponse<R[]>> {
145
+ const response = await this.directus.request<T>(readItems(collection, query)) as R[];
146
+ const total = await this.includeTotals(collection, query);
136
147
  if (null === total) {
137
148
  return this.transformResponse({ data: response });
138
149
  }
@@ -150,18 +161,21 @@ export class DirectusApiAdapter<
150
161
  return this.transformResponse({ data });
151
162
  }
152
163
 
153
- public async aggregate<T>(
164
+ public async aggregate<T, Q = T, R = T>(
154
165
  collection: Path,
155
- query?: Record<string, unknown>
156
- ): Promise<ApiResponse<T>> {
166
+ query: ApiAggregateQuery<Q>
167
+ ): Promise<ApiResponse<R>> {
168
+ const { aggregate: aggregate_, ...rest } = (query || {});
169
+
157
170
  const data = await this.directus.request<T>(
158
171
  aggregate(collection, {
159
- aggregate: {
172
+ aggregate: aggregate_ ? aggregate_ : {
160
173
  count: 'id'
161
174
  },
162
- query
175
+ query: rest
163
176
  })
164
177
  );
178
+
165
179
  return this.transformResponse({ data });
166
180
  }
167
181
 
@@ -188,9 +202,9 @@ export class DirectusApiAdapter<
188
202
  return this.transformResponse({ data }, 202);
189
203
  }
190
204
 
191
- public async put<T>(collection: Path, key?: ApiId, payload?: AnyObject): Promise<ApiResponse<T>> {
205
+ public async put<T extends object, R = T>(collection: Path, key?: ApiId, payload?: AnyObject): Promise<ApiResponse<R>> {
192
206
  if (key) {
193
- const data = await this.directus.request<T>(updateItem(collection, key, payload));
207
+ const data = await this.directus.request<T>(updateItem(collection, key, payload)) as R;
194
208
  return this.transformResponse({ data }, 201);
195
209
  } else {
196
210
  console.error(`Error updating all ${collection}: no key specified`);