@tanglemedia/svelte-starter-directus-api 2.1.0 → 4.0.0

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.
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Directus-api
1
+ # [@tanglemedia/svelte-starter-directus-api](https://tanglemedia-svelte-docs.netlify.app/docs/directus-api-package/functionality)
2
2
 
3
3
  <p align="center" style="align: center;">
4
4
  <a href="https://npm.im/@tanglemedia/svelte-starter-directus-api">
@@ -260,3 +260,4 @@ export const formsServiceClient = configureService<FormInterface>(Forms, {
260
260
  adapterKey: 'directus-client'
261
261
  });
262
262
  ```
263
+ ### For mode demos and to check out all the components, please visit our [documentation website](https://tanglemedia-svelte-docs.netlify.app/docs/directus-api-package/functionality)
@@ -1,15 +1,16 @@
1
1
  import { type AllCollections, type DirectusClient, type RegularCollections, type RestClient } from '@directus/sdk';
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';
2
+ import { ApiAdapterAbstract, type AnyObject, type ApiAdapterHandle, type ApiAdapterRequestConfig, type ApiAggregateQuery, type ApiCreateManyQuery, type ApiDeleteManyQuery, type ApiFindQuery, type ApiId, type ApiResponse, type ApiUpdateManyQuery, type BaseApiMethods, type HandleEvent } 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
5
  /**
6
6
  * TODO: Various types fixes. The current adapter doesn't correctly follow the types defined under the ApiAdapterAbstract.
7
7
  * See the fetch adapter under the core
8
8
  */
9
- export declare class DirectusApiAdapter<Schema extends SchemaShape = SchemaShape, Path extends RegularCollections<Schema> = RegularCollections<Schema>> extends ApiAdapterAbstract<DirectusConfig, ApiAdapterRequestConfig, Path, AdapterClient> {
9
+ export declare class DirectusApiAdapter<Schema extends SchemaShape = SchemaShape, Path extends RegularCollections<Schema> = RegularCollections<Schema>> extends ApiAdapterAbstract<DirectusConfig, ApiAdapterRequestConfig, Path, AdapterClient> implements ApiAdapterHandle {
10
10
  protected config: DirectusConfig;
11
11
  private readonly directus;
12
12
  constructor(config: DirectusConfig, directus: AdapterClient<Schema>);
13
+ handle(event: HandleEvent): Promise<void>;
13
14
  getAdapterClient(): AdapterClient<Schema> | null;
14
15
  normalizeMeta(response: AnyObject): {
15
16
  total?: undefined;
@@ -12,6 +12,10 @@ export class DirectusApiAdapter extends ApiAdapterAbstract {
12
12
  this.config = config;
13
13
  this.directus = directus;
14
14
  }
15
+ async handle(event) {
16
+ const f = event.fetch;
17
+ this.directus.globals.fetch = f;
18
+ }
15
19
  getAdapterClient() {
16
20
  return this.directus;
17
21
  }
@@ -34,11 +38,14 @@ export class DirectusApiAdapter extends ApiAdapterAbstract {
34
38
  return null;
35
39
  }
36
40
  const { filter, search } = query || {};
41
+ // default to countDistinct. Note that there is a bug in directus where
42
+ // the wrong counts are being returned if there are nested filters.
43
+ const k = this.config.configuration.totalAggregate || 'countDistinct';
37
44
  const data = await this.directus.request(aggregate(collection, {
38
- aggregate: { count: '*' },
45
+ aggregate: { [k]: '*' },
39
46
  query: {
40
47
  ...(filter ? { filter } : {}),
41
- ...(search ? { search } : {}),
48
+ ...(search ? { search } : {})
42
49
  }
43
50
  }));
44
51
  if (data && data.length === 1) {
@@ -81,7 +88,7 @@ export class DirectusApiAdapter extends ApiAdapterAbstract {
81
88
  }
82
89
  }
83
90
  async find(collection, query) {
84
- const response = await this.directus.request(readItems(collection, query));
91
+ const response = (await this.directus.request(readItems(collection, query)));
85
92
  const total = await this.includeTotals(collection, query);
86
93
  if (null === total) {
87
94
  return this.transformResponse({ data: response });
@@ -94,11 +101,13 @@ export class DirectusApiAdapter extends ApiAdapterAbstract {
94
101
  return this.transformResponse({ data });
95
102
  }
96
103
  async aggregate(collection, query) {
97
- const { aggregate: aggregate_, ...rest } = (query || {});
104
+ const { aggregate: aggregate_, ...rest } = query || {};
98
105
  const data = await this.directus.request(aggregate(collection, {
99
- aggregate: aggregate_ ? aggregate_ : {
100
- count: 'id'
101
- },
106
+ aggregate: aggregate_
107
+ ? aggregate_
108
+ : {
109
+ count: 'id'
110
+ },
102
111
  query: rest
103
112
  }));
104
113
  return this.transformResponse({ data });
@@ -117,7 +126,7 @@ export class DirectusApiAdapter extends ApiAdapterAbstract {
117
126
  }
118
127
  async put(collection, key, payload) {
119
128
  if (key) {
120
- const data = await this.directus.request(updateItem(collection, key, payload));
129
+ const data = (await this.directus.request(updateItem(collection, key, payload)));
121
130
  return this.transformResponse({ data }, 201);
122
131
  }
123
132
  else {
@@ -19,6 +19,7 @@ export type DirectusApiAdapterOptions = {
19
19
  credentials?: RequestCredentials;
20
20
  };
21
21
  includeTotals?: boolean;
22
+ totalAggregate?: 'count' | 'countDistinct';
22
23
  };
23
24
  export type DirectusConfig = BaseApiAdapterConfig<DirectusApiAdapterOptions>;
24
25
  export type SchemaShape = Record<string, object>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanglemedia/svelte-starter-directus-api",
3
- "version": "2.1.0",
3
+ "version": "4.0.0",
4
4
  "main": "src/index.ts",
5
5
  "types": "src/index.ts",
6
6
  "description": "directus API wrapper for all the directus sdk functionality",
@@ -22,31 +22,31 @@
22
22
  }
23
23
  },
24
24
  "devDependencies": {
25
- "@directus/types": "^12.0.0",
26
- "@sveltejs/adapter-auto": "^3.2.4",
27
- "@sveltejs/package": "^2.3.4",
25
+ "@directus/types": "^12.1.0",
26
+ "@sveltejs/adapter-auto": "^3.2.5",
27
+ "@sveltejs/package": "^2.3.5",
28
28
  "@sveltejs/vite-plugin-svelte": "^3.1.2",
29
29
  "@testing-library/jest-dom": "^6.5.0",
30
- "@testing-library/svelte": "^5.2.1",
31
- "@vitest/coverage-v8": "^2.0.5",
32
- "jsdom": "^25.0.0",
33
- "msw": "^2.4.1",
30
+ "@testing-library/svelte": "^5.2.3",
31
+ "@vitest/coverage-v8": "^2.1.2",
32
+ "jsdom": "^25.0.1",
33
+ "msw": "^2.4.9",
34
34
  "svelte": "^4.2.19",
35
- "svelte-check": "^3.8.6",
36
- "vite": "^5.4.2",
37
- "vitest": "^2.0.5",
35
+ "svelte-check": "^4.0.4",
36
+ "vite": "^5.4.8",
37
+ "vitest": "^2.1.2",
38
38
  "@internal/eslint-config": "0.0.0",
39
- "@tanglemedia/svelte-starter-core": "0.2.2"
39
+ "@tanglemedia/svelte-starter-core": "0.5.0"
40
40
  },
41
41
  "dependencies": {
42
- "@directus/sdk": "^17.0.1",
42
+ "@directus/sdk": "^17.0.2",
43
43
  "@types/js-cookie": "^3.0.6",
44
44
  "esm-env": "^1.0.0",
45
45
  "js-cookie": "^3.0.5"
46
46
  },
47
47
  "peerDependencies": {
48
48
  "@sveltejs/kit": ">=2 <3",
49
- "@tanglemedia/svelte-starter-core": ">=0.2.2",
49
+ "@tanglemedia/svelte-starter-core": ">=0.5.0",
50
50
  "svelte": ">=4 <5"
51
51
  },
52
52
  "scripts": {
@@ -17,6 +17,7 @@ import {
17
17
  import {
18
18
  ApiAdapterAbstract,
19
19
  type AnyObject,
20
+ type ApiAdapterHandle,
20
21
  type ApiAdapterRequestConfig,
21
22
  type ApiAggregateQuery,
22
23
  type ApiCreateManyQuery,
@@ -25,7 +26,8 @@ import {
25
26
  type ApiId,
26
27
  type ApiResponse,
27
28
  type ApiUpdateManyQuery,
28
- type BaseApiMethods
29
+ type BaseApiMethods,
30
+ type HandleEvent
29
31
  } from '@tanglemedia/svelte-starter-core';
30
32
  import type { DirectusConfig, SchemaShape } from '../types/adapter.types';
31
33
 
@@ -39,7 +41,7 @@ export type AdapterClient<Schema extends SchemaShape = SchemaShape> = DirectusCl
39
41
  export class DirectusApiAdapter<
40
42
  Schema extends SchemaShape = SchemaShape,
41
43
  Path extends RegularCollections<Schema> = RegularCollections<Schema>
42
- > extends ApiAdapterAbstract<DirectusConfig, ApiAdapterRequestConfig, Path, AdapterClient> {
44
+ > extends ApiAdapterAbstract<DirectusConfig, ApiAdapterRequestConfig, Path, AdapterClient> implements ApiAdapterHandle {
43
45
  constructor(
44
46
  protected config: DirectusConfig,
45
47
  private readonly directus: AdapterClient<Schema>
@@ -47,6 +49,11 @@ export class DirectusApiAdapter<
47
49
  super(config);
48
50
  }
49
51
 
52
+ async handle(event: HandleEvent) {
53
+ const f = event.fetch;
54
+ this.directus.globals.fetch = f;
55
+ }
56
+
50
57
  getAdapterClient(): AdapterClient<Schema> | null {
51
58
  return this.directus;
52
59
  }
@@ -78,8 +85,12 @@ export class DirectusApiAdapter<
78
85
 
79
86
  const { filter, search } = query || {};
80
87
 
88
+ // default to countDistinct. Note that there is a bug in directus where
89
+ // the wrong counts are being returned if there are nested filters.
90
+ const k = this.config.configuration.totalAggregate || 'countDistinct';
91
+
81
92
  const data = await this.directus.request(aggregate(collection, {
82
- aggregate: { count: '*' },
93
+ aggregate: { [k]: '*' },
83
94
  query: {
84
95
  ...(filter ? { filter } : {}),
85
96
  ...(search ? { search } : {}),
@@ -25,6 +25,7 @@ export type DirectusApiAdapterOptions = {
25
25
  };
26
26
 
27
27
  includeTotals?: boolean;
28
+ totalAggregate?: 'count' | 'countDistinct';
28
29
  };
29
30
 
30
31
  export type DirectusConfig = BaseApiAdapterConfig<DirectusApiAdapterOptions>;