@tanglemedia/svelte-starter-directus-api 0.0.6 → 0.0.8

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/dist/auth.js CHANGED
@@ -1,12 +1,9 @@
1
- import { createDirectus, rest, authentication, refresh, readMe, updateMe } from '@directus/sdk';
1
+ import { authentication, createDirectus, readMe, refresh, rest, updateMe, memoryStorage } from '@directus/sdk';
2
2
  import { browser } from '$app/environment';
3
3
  const localDirectusStorage = (storageKey = 'access_token_directus') => {
4
4
  if (!browser || !window.localStorage) {
5
5
  console.warn('Defaulting to memory storage');
6
- return {
7
- get: async () => null,
8
- set: async (value) => { }
9
- };
6
+ return memoryStorage();
10
7
  }
11
8
  const storage = window.localStorage;
12
9
  return {
@@ -39,7 +36,9 @@ class ApiAuthDirectus {
39
36
  }
40
37
  init() {
41
38
  try {
42
- this.directus = createDirectus(this.baseURL).with(rest({ credentials: 'include' })).with(authentication('json', authenticationConfig));
39
+ this.directus = createDirectus(this.baseURL)
40
+ .with(rest({ credentials: 'include' }))
41
+ .with(authentication('json', authenticationConfig));
43
42
  }
44
43
  catch (error) {
45
44
  console.error(`Error initializing Directus:`, error);
package/dist/client.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { ApiAdapterInterface, ApiAdapterRequestConfig, AnyObject, BaseApiAdapterConfig } from '@tanglemedia/svelte-starter-core';
1
+ import type { AnyObject, ApiAdapterInterface, ApiAdapterRequestConfig, BaseApiAdapterConfig } from '@tanglemedia/svelte-starter-core';
2
2
  type BaseApiMethods = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
3
3
  type ApiId = string | number;
4
4
  declare class ApiClientDirectus implements ApiAdapterInterface {
@@ -7,7 +7,7 @@ declare class ApiClientDirectus implements ApiAdapterInterface {
7
7
  constructor(baseURL: string);
8
8
  private init;
9
9
  getConfig<T extends object>(configuration?: unknown): BaseApiAdapterConfig;
10
- request<T>(method: BaseApiMethods, url: string, queryParams?: Record<string, any>): Promise<T>;
10
+ request<T>(method: BaseApiMethods, url: string, queryParams?: Record<string, unknown>): Promise<T>;
11
11
  find<T>(collection: string, queryParams?: Record<string, unknown>): Promise<T>;
12
12
  findOne<T>(collection: string, key?: ApiId, queryParams?: Record<string, unknown>): Promise<T>;
13
13
  aggregate<T>(collection: string, queryParams?: Record<string, unknown>): Promise<T>;
package/dist/client.js CHANGED
@@ -1,12 +1,9 @@
1
- import { createDirectus, rest, authentication, readItems, readItem, updateItem, updateItems, createItem, createItems, deleteItem, deleteItems, aggregate, uploadFiles } from '@directus/sdk';
1
+ import { aggregate, authentication, createDirectus, createItem, deleteItem, readItem, readItems, rest, updateItem, uploadFiles, memoryStorage } from '@directus/sdk';
2
2
  import { browser } from '$app/environment';
3
3
  const localDirectusStorage = (storageKey = 'access_token_directus') => {
4
4
  if (!browser || !window.localStorage) {
5
5
  console.warn('Defaulting to memory storage');
6
- return {
7
- get: async () => null,
8
- set: async (value) => { }
9
- };
6
+ return memoryStorage();
10
7
  }
11
8
  const storage = window.localStorage;
12
9
  return {
@@ -14,7 +11,14 @@ const localDirectusStorage = (storageKey = 'access_token_directus') => {
14
11
  const data = storage.getItem(storageKey);
15
12
  return data ? JSON.parse(data) : null;
16
13
  },
17
- set: async (value) => { }
14
+ set: async (value) => {
15
+ if (value === null) {
16
+ storage.removeItem(storageKey);
17
+ }
18
+ else {
19
+ storage.setItem(storageKey, JSON.stringify(value));
20
+ }
21
+ }
18
22
  };
19
23
  };
20
24
  const authenticationConfig = {
@@ -32,7 +36,9 @@ class ApiClientDirectus {
32
36
  }
33
37
  init() {
34
38
  try {
35
- this.directus = createDirectus(this.baseURL).with(rest({ credentials: 'include' })).with(authentication('json', authenticationConfig));
39
+ this.directus = createDirectus(this.baseURL)
40
+ .with(rest({ credentials: 'include' }))
41
+ .with(authentication('json', authenticationConfig));
36
42
  }
37
43
  catch (error) {
38
44
  console.error(`Error initializing Directus:`, error);
@@ -65,7 +71,7 @@ class ApiClientDirectus {
65
71
  }
66
72
  }
67
73
  async find(collection, queryParams) {
68
- console.log('in find');
74
+ // console.log('in find');
69
75
  try {
70
76
  // console.log(`Find ${collection}`)
71
77
  const response = await this.directus.request(readItems(collection, queryParams));
@@ -147,6 +153,7 @@ class ApiClientDirectus {
147
153
  throw error;
148
154
  }
149
155
  }
156
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
150
157
  async upload(path = '', data) {
151
158
  try {
152
159
  const response = await this.directus.request(uploadFiles(data));
package/dist/static.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { ApiAdapterInterface, ApiAdapterRequestConfig, AnyObject, BaseApiAdapterConfig } from '@tanglemedia/svelte-starter-core';
1
+ import type { AnyObject, ApiAdapterInterface, ApiAdapterRequestConfig, BaseApiAdapterConfig } from '@tanglemedia/svelte-starter-core';
2
2
  type BaseApiMethods = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
3
3
  type ApiId = string | number;
4
4
  declare class ApiStaticDirectus implements ApiAdapterInterface {
@@ -8,7 +8,7 @@ declare class ApiStaticDirectus implements ApiAdapterInterface {
8
8
  constructor(baseURL: string, directusAccessToken: string);
9
9
  private init;
10
10
  getConfig<T extends object>(configuration?: unknown): BaseApiAdapterConfig;
11
- request<T>(method: BaseApiMethods, url: string, queryParams?: Record<string, any>): Promise<T>;
11
+ request<T>(method: BaseApiMethods, url: string, queryParams?: Record<string, unknown>): Promise<T>;
12
12
  createUser(data: {
13
13
  email: string;
14
14
  password: string;
@@ -19,11 +19,11 @@ declare class ApiStaticDirectus implements ApiAdapterInterface {
19
19
  message: string;
20
20
  type: string;
21
21
  }>;
22
- find<T>(collection: string, queryParams?: Record<string, any>): Promise<T>;
23
- findOne<T>(collection: string, key: ApiId, queryParams?: Record<string, any>): Promise<T>;
24
- aggregate<T>(collection: string, queryParams?: Record<string, any>): Promise<T>;
25
- patch<T>(collection: string, key: ApiId, queryParams?: Record<string, any>): Promise<T>;
26
- post<T>(collection: string, data: AnyObject, queryParams?: Record<string, any>): Promise<T>;
22
+ find<T>(collection: string, queryParams?: Record<string, unknown>): Promise<T>;
23
+ findOne<T>(collection: string, key: ApiId, queryParams?: Record<string, unknown>): Promise<T>;
24
+ aggregate<T>(collection: string, queryParams?: Record<string, unknown>): Promise<T>;
25
+ patch<T>(collection: string, key: ApiId, queryParams?: Record<string, unknown>): Promise<T>;
26
+ post<T>(collection: string, data: AnyObject, queryParams?: Record<string, unknown>): Promise<T>;
27
27
  delete<T>(collection: string, keys: ApiId): Promise<T>;
28
28
  put<T>(collection: string, payload: AnyObject, config?: ApiAdapterRequestConfig, key?: ApiId): Promise<T>;
29
29
  upload<T>(path: string | undefined, data: FormData): Promise<T>;
package/dist/static.js CHANGED
@@ -1,4 +1,4 @@
1
- import { createDirectus, rest, staticToken, readItems, readItem, updateItem, updateItems, createItem, createUser, deleteItem, aggregate, uploadFiles } from '@directus/sdk';
1
+ import { aggregate, createDirectus, createItem, createUser, deleteItem, readItem, readItems, rest, staticToken, updateItem, updateItems, uploadFiles } from '@directus/sdk';
2
2
  class ApiStaticDirectus {
3
3
  baseURL;
4
4
  directusAccessToken;
@@ -109,6 +109,7 @@ class ApiStaticDirectus {
109
109
  throw error;
110
110
  }
111
111
  }
112
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
112
113
  async delete(collection, keys) {
113
114
  try {
114
115
  const response = await this.directus.request(deleteItem(collection, key));
@@ -135,6 +136,7 @@ class ApiStaticDirectus {
135
136
  throw error;
136
137
  }
137
138
  }
139
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
138
140
  async upload(path = '', data) {
139
141
  try {
140
142
  const response = await this.directus.request(uploadFiles(data));
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@tanglemedia/svelte-starter-directus-api",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "main": "src/index.ts",
5
5
  "types": "src/index.ts",
6
6
  "description": "directus API wrapper for all the directus sdk functionality",
7
7
  "type": "module",
8
- "module": "src/index.ts",
9
- "svelte": "src/index.ts",
8
+ "module": "./dist/index.d.ts",
9
+ "svelte": "./dist/index.js",
10
10
  "license": "MIT",
11
11
  "files": [
12
12
  "dist",
@@ -22,36 +22,28 @@
22
22
  }
23
23
  },
24
24
  "devDependencies": {
25
- "@sveltejs/adapter-auto": "^2.1.1",
26
- "@sveltejs/package": "^2.2.2",
27
- "@testing-library/jest-dom": "^6.1.4",
25
+ "@sveltejs/adapter-auto": "^3.0.1",
26
+ "@sveltejs/package": "^2.2.5",
27
+ "@sveltejs/vite-plugin-svelte": "^3.0.1",
28
+ "@testing-library/jest-dom": "^6.1.6",
28
29
  "@testing-library/svelte": "^4.0.5",
29
- "@vitest/coverage-v8": "^0.34.6",
30
- "jsdom": "^22.1.0",
31
- "msw": "^2.0.8",
32
- "svelte": "^4.2.7",
33
- "svelte-check": "^3.6.0",
34
- "vite": "^4.5.0",
35
- "vitest": "^0.34.6",
30
+ "@vitest/coverage-v8": "^1.1.1",
31
+ "jsdom": "^23.0.1",
32
+ "msw": "^2.0.11",
33
+ "svelte": "^4.2.8",
34
+ "svelte-check": "^3.6.2",
35
+ "vite": "^5.0.10",
36
+ "vitest": "^1.1.1",
36
37
  "eslint-config-custom": "0.0.0"
37
38
  },
38
39
  "dependencies": {
39
- "@directus/sdk": "^13.0.2",
40
- "@tanglemedia/svelte-starter-core": "^0.0.10",
41
- "flowbite-svelte": "^0.44.19",
42
- "fontawesome-svelte": "^2.0.1",
43
- "svelte-fa": "^3.0.4",
44
- "tailwind-merge": "^2.0.0"
40
+ "@directus/sdk": "^14.0.0",
41
+ "@tanglemedia/svelte-starter-core": "0.0.12"
45
42
  },
46
43
  "peerDependencies": {
47
44
  "@sveltejs/kit": ">=1.20 <2",
48
45
  "svelte": ">=4 <5"
49
46
  },
50
- "babelMacros": {
51
- "fontawesome-svg-core": {
52
- "license": "free"
53
- }
54
- },
55
47
  "scripts": {
56
48
  "build": "svelte-package --input ./src --output ./dist",
57
49
  "dev": "npm run build -- --watch",
package/src/auth.ts CHANGED
@@ -1,12 +1,13 @@
1
+ import type { AuthenticationConfig, AuthenticationData } from '@directus/sdk';
1
2
  import {
2
- createDirectus,
3
- rest,
4
3
  authentication,
5
- refresh,
4
+ createDirectus,
6
5
  readMe,
7
- updateMe
6
+ refresh,
7
+ rest,
8
+ updateMe,
9
+ memoryStorage
8
10
  } from '@directus/sdk';
9
- import type { AuthenticationData, AuthenticationConfig } from '@directus/sdk';
10
11
 
11
12
  import type { DirectusClient, RestClient } from '@directus/sdk';
12
13
 
@@ -15,10 +16,7 @@ import { browser } from '$app/environment';
15
16
  const localDirectusStorage = (storageKey: string = 'access_token_directus') => {
16
17
  if (!browser || !window.localStorage) {
17
18
  console.warn('Defaulting to memory storage');
18
- return {
19
- get: async (): Promise<AuthenticationData | null> => null,
20
- set: async (value: AuthenticationData | null): Promise<void> => {}
21
- };
19
+ return memoryStorage();
22
20
  }
23
21
 
24
22
  const storage = window.localStorage;
@@ -56,7 +54,9 @@ class ApiAuthDirectus {
56
54
 
57
55
  private init<T extends object>(): void {
58
56
  try {
59
- this.directus = createDirectus<T>(this.baseURL).with(rest({credentials: 'include'})).with(authentication('json', authenticationConfig))
57
+ this.directus = createDirectus<T>(this.baseURL)
58
+ .with(rest({ credentials: 'include' }))
59
+ .with(authentication('json', authenticationConfig));
60
60
  } catch (error) {
61
61
  console.error(`Error initializing Directus:`, error);
62
62
  throw error;
@@ -110,7 +110,9 @@ class ApiAuthDirectus {
110
110
  public async getCurrentUser<T>(fields: string[] | null = null): Promise<T> {
111
111
  try {
112
112
  if (!this.getToken()) await this.refresh();
113
- const response = await this.directus.request<T>(readMe({fields: fields ? fields : ['*', 'roles.*']}));
113
+ const response = await this.directus.request<T>(
114
+ readMe({ fields: fields ? fields : ['*', 'roles.*'] })
115
+ );
114
116
  return response;
115
117
  } catch (error) {
116
118
  console.error(`Error refreshing :`, error);
@@ -130,4 +132,4 @@ class ApiAuthDirectus {
130
132
  }
131
133
  }
132
134
 
133
- export { ApiAuthDirectus };
135
+ export { ApiAuthDirectus };
package/src/client.ts CHANGED
@@ -1,33 +1,31 @@
1
+ import type { AuthenticationConfig, AuthenticationData } from '@directus/sdk';
1
2
  import {
2
- createDirectus,
3
- rest,
3
+ aggregate,
4
4
  authentication,
5
- readItems,
6
- readItem,
7
- updateItem,
8
- updateItems,
5
+ createDirectus,
9
6
  createItem,
10
- createItems,
11
7
  deleteItem,
12
- deleteItems,
13
- aggregate,
14
- uploadFiles
8
+ readItem,
9
+ readItems,
10
+ rest,
11
+ updateItem,
12
+ uploadFiles,
13
+ memoryStorage
15
14
  } from '@directus/sdk';
16
- import type { AuthenticationData, AuthenticationConfig } from '@directus/sdk';
17
-
18
15
  import type { DirectusClient, RestClient } from '@directus/sdk';
19
-
20
- import type { ApiAdapterInterface, ApiAdapterRequestConfig, AnyObject, BaseApiAdapterConfig } from '@tanglemedia/svelte-starter-core';
16
+ import type {
17
+ AnyObject,
18
+ ApiAdapterInterface,
19
+ ApiAdapterRequestConfig,
20
+ BaseApiAdapterConfig
21
+ } from '@tanglemedia/svelte-starter-core';
21
22
 
22
23
  import { browser } from '$app/environment';
23
24
 
24
25
  const localDirectusStorage = (storageKey: string = 'access_token_directus') => {
25
26
  if (!browser || !window.localStorage) {
26
27
  console.warn('Defaulting to memory storage');
27
- return {
28
- get: async (): Promise<AuthenticationData | null> => null,
29
- set: async (value: AuthenticationData | null): Promise<void> => {}
30
- };
28
+ return memoryStorage();
31
29
  }
32
30
 
33
31
  const storage = window.localStorage;
@@ -37,7 +35,13 @@ const localDirectusStorage = (storageKey: string = 'access_token_directus') => {
37
35
  const data = storage.getItem(storageKey);
38
36
  return data ? (JSON.parse(data) as AuthenticationData) : null;
39
37
  },
40
- set: async (value: AuthenticationData | null): Promise<void> => {}
38
+ set: async (value: AuthenticationData | null): Promise<void> => {
39
+ if (value === null) {
40
+ storage.removeItem(storageKey);
41
+ } else {
42
+ storage.setItem(storageKey, JSON.stringify(value));
43
+ }
44
+ }
41
45
  };
42
46
  };
43
47
 
@@ -63,7 +67,9 @@ class ApiClientDirectus implements ApiAdapterInterface {
63
67
 
64
68
  private init<T extends object>(): void {
65
69
  try {
66
- this.directus = createDirectus<T>(this.baseURL).with(rest({credentials: 'include'})).with(authentication('json', authenticationConfig))
70
+ this.directus = createDirectus<T>(this.baseURL)
71
+ .with(rest({ credentials: 'include' }))
72
+ .with(authentication('json', authenticationConfig));
67
73
  } catch (error) {
68
74
  console.error(`Error initializing Directus:`, error);
69
75
  throw error;
@@ -82,7 +88,11 @@ class ApiClientDirectus implements ApiAdapterInterface {
82
88
  return Object.assign(client, config);
83
89
  }
84
90
 
85
- public async request<T>(method: BaseApiMethods, url: string, queryParams?: Record<string, any>): Promise<T> {
91
+ public async request<T>(
92
+ method: BaseApiMethods,
93
+ url: string,
94
+ queryParams?: Record<string, unknown>
95
+ ): Promise<T> {
86
96
  try {
87
97
  const response = await this.directus.request<T>(() => {
88
98
  const params = JSON.stringify(queryParams);
@@ -99,7 +109,7 @@ class ApiClientDirectus implements ApiAdapterInterface {
99
109
  }
100
110
 
101
111
  public async find<T>(collection: string, queryParams?: Record<string, unknown>): Promise<T> {
102
- console.log('in find')
112
+ // console.log('in find');
103
113
  try {
104
114
  // console.log(`Find ${collection}`)
105
115
  const response = await this.directus.request<T>(readItems(collection, queryParams));
@@ -110,7 +120,11 @@ class ApiClientDirectus implements ApiAdapterInterface {
110
120
  }
111
121
  }
112
122
 
113
- public async findOne<T>(collection: string, key?: ApiId, queryParams?: Record<string, unknown>): Promise<T> {
123
+ public async findOne<T>(
124
+ collection: string,
125
+ key?: ApiId,
126
+ queryParams?: Record<string, unknown>
127
+ ): Promise<T> {
114
128
  try {
115
129
  const response = await this.directus.request<T>(readItem(collection, key, queryParams));
116
130
  return response;
@@ -122,12 +136,14 @@ class ApiClientDirectus implements ApiAdapterInterface {
122
136
 
123
137
  public async aggregate<T>(collection: string, queryParams?: Record<string, unknown>): Promise<T> {
124
138
  try {
125
- const response = await this.directus.request<T>(aggregate(collection, {
126
- aggregate: {
127
- count: 'id'
128
- },
129
- queryParams
130
- }));
139
+ const response = await this.directus.request<T>(
140
+ aggregate(collection, {
141
+ aggregate: {
142
+ count: 'id'
143
+ },
144
+ queryParams
145
+ })
146
+ );
131
147
  return response;
132
148
  } catch (error) {
133
149
  console.error(`Error fetching total of ${collection}:`, error);
@@ -135,7 +151,11 @@ class ApiClientDirectus implements ApiAdapterInterface {
135
151
  }
136
152
  }
137
153
 
138
- public async patch<T>(collection: string, key: ApiId, queryParams?: Record<string, unknown>): Promise<T> {
154
+ public async patch<T>(
155
+ collection: string,
156
+ key: ApiId,
157
+ queryParams?: Record<string, unknown>
158
+ ): Promise<T> {
139
159
  try {
140
160
  const response = await this.directus.request<T>(updateItem(collection, key, queryParams));
141
161
  return response;
@@ -145,7 +165,11 @@ class ApiClientDirectus implements ApiAdapterInterface {
145
165
  }
146
166
  }
147
167
 
148
- public async post<T>(collection: string, data: AnyObject, queryParams?: Record<string, unknown>): Promise<T> {
168
+ public async post<T>(
169
+ collection: string,
170
+ data: AnyObject,
171
+ queryParams?: Record<string, unknown>
172
+ ): Promise<T> {
149
173
  try {
150
174
  const response = await this.directus.request<T>(createItem(collection, data, queryParams));
151
175
  return response;
@@ -165,12 +189,17 @@ class ApiClientDirectus implements ApiAdapterInterface {
165
189
  }
166
190
  }
167
191
 
168
- public async put<T>(collection: string, payload: AnyObject, config?: ApiAdapterRequestConfig, key?: ApiId): Promise<T> {
192
+ public async put<T>(
193
+ collection: string,
194
+ payload: AnyObject,
195
+ config?: ApiAdapterRequestConfig,
196
+ key?: ApiId
197
+ ): Promise<T> {
169
198
  try {
170
- if(key){
199
+ if (key) {
171
200
  const response = await this.directus.request<T>(updateItem(collection, key, payload));
172
201
  return response;
173
- }else {
202
+ } else {
174
203
  console.error(`Error updating all ${collection}: no key specified`);
175
204
  throw new Error('No key specified');
176
205
  }
@@ -179,8 +208,8 @@ class ApiClientDirectus implements ApiAdapterInterface {
179
208
  throw error;
180
209
  }
181
210
  }
182
-
183
211
 
212
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
184
213
  public async upload<T>(path: string = '', data: FormData): Promise<T> {
185
214
  try {
186
215
  const response = await this.directus.request<T>(uploadFiles(data));
@@ -190,7 +219,6 @@ class ApiClientDirectus implements ApiAdapterInterface {
190
219
  throw error;
191
220
  }
192
221
  }
193
-
194
222
  }
195
223
 
196
- export { ApiClientDirectus };
224
+ export { ApiClientDirectus };
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  export * from './static';
2
2
  export * from './client';
3
3
  export * from './auth';
4
- export * from './load-directus-api-povider';
4
+ export * from './load-directus-api-povider';
@@ -1,26 +1,32 @@
1
1
  import { ApiClientDirectus } from './client';
2
2
  import { ApiStaticDirectus } from './static';
3
- import { type ApiAdapterProviderInterface, type ApiAdapterInterface, type ApiAdapterRequestConfig, type BaseApiAdapterConfig, type Config, type ApiConfigSchema } from '@tanglemedia/svelte-starter-core';
3
+ import {
4
+ type ApiAdapterProviderInterface,
5
+ type ApiAdapterInterface,
6
+ type ApiAdapterRequestConfig,
7
+ type BaseApiAdapterConfig,
8
+ type Config,
9
+ type ApiConfigSchema
10
+ } from '@tanglemedia/svelte-starter-core';
4
11
 
5
12
  class LoadDirectusApiProvider implements ApiAdapterProviderInterface {
13
+ constructor(private readonly configLoader: () => Promise<Config<{ api?: ApiConfigSchema }>>) {}
6
14
 
7
- constructor(
8
- private readonly configLoader: () => Promise<Config<{ api?: ApiConfigSchema }>>,
9
- ) { }
10
-
11
- async loadAdapter(key?: string | undefined): Promise<ApiAdapterInterface<BaseApiAdapterConfig, ApiAdapterRequestConfig, string>> {
15
+ async loadAdapter(
16
+ key?: string | undefined
17
+ ): Promise<ApiAdapterInterface<BaseApiAdapterConfig, ApiAdapterRequestConfig, string>> {
12
18
  const conf = await this.configLoader();
13
19
  const config = (p: string) => conf.config(p);
14
20
 
15
21
  const apiAdapterKey = key || (config('api.default') as string);
16
22
  const directusType = (config(`api.adapters.${apiAdapterKey}.type`) as string) || 'client';
17
- console.log(`api.apaters.${apiAdapterKey}.type`)
23
+ console.log(`api.apaters.${apiAdapterKey}.type`);
18
24
 
19
25
  if (!apiAdapterKey) {
20
26
  throw new Error(`No adapter key specified`);
21
27
  }
22
- console.log(apiAdapterKey)
23
- console.log(directusType)
28
+ console.log(apiAdapterKey);
29
+ console.log(directusType);
24
30
  if (directusType) {
25
31
  if (directusType === 'client') {
26
32
  const protocol: string = config(`api.adapters.${apiAdapterKey}.protocol`) || '';
@@ -40,8 +46,8 @@ class LoadDirectusApiProvider implements ApiAdapterProviderInterface {
40
46
  return new ApiClientDirectus(`${protocol}://${host}`.trim());
41
47
  }
42
48
 
43
- console.log('This should be unreachable')
49
+ console.log('This should be unreachable');
44
50
  }
45
51
  }
46
52
 
47
- export { LoadDirectusApiProvider }
53
+ export { LoadDirectusApiProvider };
package/src/static.ts CHANGED
@@ -1,21 +1,24 @@
1
1
  import {
2
+ aggregate,
2
3
  createDirectus,
4
+ createItem,
5
+ createUser,
6
+ deleteItem,
7
+ readItem,
8
+ readItems,
3
9
  rest,
4
10
  staticToken,
5
- readItems,
6
- readItem,
7
11
  updateItem,
8
12
  updateItems,
9
- createItem,
10
- createUser,
11
- deleteItem,
12
- aggregate,
13
13
  uploadFiles
14
14
  } from '@directus/sdk';
15
-
16
15
  import type { DirectusClient, RestClient, StaticTokenClient } from '@directus/sdk';
17
-
18
- import type { ApiAdapterInterface, ApiAdapterRequestConfig, AnyObject, BaseApiAdapterConfig } from '@tanglemedia/svelte-starter-core';
16
+ import type {
17
+ AnyObject,
18
+ ApiAdapterInterface,
19
+ ApiAdapterRequestConfig,
20
+ BaseApiAdapterConfig
21
+ } from '@tanglemedia/svelte-starter-core';
19
22
 
20
23
  type BaseApiMethods = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
21
24
 
@@ -24,7 +27,7 @@ type ApiId = string | number;
24
27
  class ApiStaticDirectus implements ApiAdapterInterface {
25
28
  private baseURL: string;
26
29
  private directusAccessToken: string;
27
- private directus: DirectusClient<any> & RestClient<object> & StaticTokenClient<object>;
30
+ private directus: DirectusClient<AnyObject> & RestClient<object> & StaticTokenClient<object>;
28
31
 
29
32
  constructor(baseURL: string, directusAccessToken: string) {
30
33
  this.baseURL = baseURL;
@@ -55,7 +58,11 @@ class ApiStaticDirectus implements ApiAdapterInterface {
55
58
  return Object.assign(client, config);
56
59
  }
57
60
 
58
- public async request<T>(method: BaseApiMethods, url: string, queryParams?: Record<string, any>): Promise<T> {
61
+ public async request<T>(
62
+ method: BaseApiMethods,
63
+ url: string,
64
+ queryParams?: Record<string, unknown>
65
+ ): Promise<T> {
59
66
  try {
60
67
  const response = await this.directus.request<T>(() => {
61
68
  const params = JSON.stringify(queryParams);
@@ -71,10 +78,14 @@ class ApiStaticDirectus implements ApiAdapterInterface {
71
78
  }
72
79
  }
73
80
 
74
- public async createUser(data: {email: string, password: string, first_name: string, last_name: string, role: string} | object): Promise<{ message: string; type: string; }> {
81
+ public async createUser(
82
+ data:
83
+ | { email: string; password: string; first_name: string; last_name: string; role: string }
84
+ | object
85
+ ): Promise<{ message: string; type: string }> {
75
86
  try {
76
87
  await this.directus.request(createUser(data));
77
-
88
+
78
89
  return { message: 'You have successfully registered your account', type: 'success' };
79
90
  } catch (error) {
80
91
  console.error(`Error creating a user:`, error);
@@ -82,7 +93,7 @@ class ApiStaticDirectus implements ApiAdapterInterface {
82
93
  }
83
94
  }
84
95
 
85
- public async find<T>(collection: string, queryParams?: Record<string, any>): Promise<T> {
96
+ public async find<T>(collection: string, queryParams?: Record<string, unknown>): Promise<T> {
86
97
  try {
87
98
  const response = await this.directus.request<T>(readItems(collection, queryParams));
88
99
  return response;
@@ -92,7 +103,11 @@ class ApiStaticDirectus implements ApiAdapterInterface {
92
103
  }
93
104
  }
94
105
 
95
- public async findOne<T>(collection: string, key: ApiId, queryParams?: Record<string, any>): Promise<T> {
106
+ public async findOne<T>(
107
+ collection: string,
108
+ key: ApiId,
109
+ queryParams?: Record<string, unknown>
110
+ ): Promise<T> {
96
111
  try {
97
112
  const response = await this.directus.request<T>(readItem(collection, key, queryParams));
98
113
  return response;
@@ -102,14 +117,16 @@ class ApiStaticDirectus implements ApiAdapterInterface {
102
117
  }
103
118
  }
104
119
 
105
- public async aggregate<T>(collection: string, queryParams?: Record<string, any>): Promise<T> {
120
+ public async aggregate<T>(collection: string, queryParams?: Record<string, unknown>): Promise<T> {
106
121
  try {
107
- const response = await this.directus.request<T>(aggregate(collection, {
108
- aggregate: {
109
- count: 'id'
110
- },
111
- queryParams
112
- }));
122
+ const response = await this.directus.request<T>(
123
+ aggregate(collection, {
124
+ aggregate: {
125
+ count: 'id'
126
+ },
127
+ queryParams
128
+ })
129
+ );
113
130
  return response;
114
131
  } catch (error) {
115
132
  console.error(`Error fetching total of ${collection}:`, error);
@@ -117,7 +134,11 @@ class ApiStaticDirectus implements ApiAdapterInterface {
117
134
  }
118
135
  }
119
136
 
120
- public async patch<T>(collection: string, key: ApiId, queryParams?: Record<string, any>): Promise<T> {
137
+ public async patch<T>(
138
+ collection: string,
139
+ key: ApiId,
140
+ queryParams?: Record<string, unknown>
141
+ ): Promise<T> {
121
142
  try {
122
143
  const response = await this.directus.request<T>(updateItems(collection, key, queryParams));
123
144
  return response;
@@ -127,7 +148,11 @@ class ApiStaticDirectus implements ApiAdapterInterface {
127
148
  }
128
149
  }
129
150
 
130
- public async post<T>(collection: string, data: AnyObject, queryParams?: Record<string, any>): Promise<T> {
151
+ public async post<T>(
152
+ collection: string,
153
+ data: AnyObject,
154
+ queryParams?: Record<string, unknown>
155
+ ): Promise<T> {
131
156
  try {
132
157
  const response = await this.directus.request<T>(createItem(collection, data, queryParams));
133
158
  return response;
@@ -137,6 +162,7 @@ class ApiStaticDirectus implements ApiAdapterInterface {
137
162
  }
138
163
  }
139
164
 
165
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
140
166
  public async delete<T>(collection: string, keys: ApiId): Promise<T> {
141
167
  try {
142
168
  const response = await this.directus.request<T>(deleteItem(collection, key));
@@ -147,12 +173,17 @@ class ApiStaticDirectus implements ApiAdapterInterface {
147
173
  }
148
174
  }
149
175
 
150
- public async put<T>(collection: string, payload: AnyObject, config?: ApiAdapterRequestConfig, key?: ApiId): Promise<T> {
176
+ public async put<T>(
177
+ collection: string,
178
+ payload: AnyObject,
179
+ config?: ApiAdapterRequestConfig,
180
+ key?: ApiId
181
+ ): Promise<T> {
151
182
  try {
152
- if(key){
183
+ if (key) {
153
184
  const response = await this.directus.request<T>(updateItem(collection, key, payload));
154
185
  return response;
155
- }else {
186
+ } else {
156
187
  console.error(`Error updating all ${collection}: no key specified`);
157
188
  throw new Error('No key specified');
158
189
  }
@@ -162,6 +193,7 @@ class ApiStaticDirectus implements ApiAdapterInterface {
162
193
  }
163
194
  }
164
195
 
196
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
165
197
  public async upload<T>(path: string = '', data: FormData): Promise<T> {
166
198
  try {
167
199
  const response = await this.directus.request<T>(uploadFiles(data));
@@ -173,4 +205,4 @@ class ApiStaticDirectus implements ApiAdapterInterface {
173
205
  }
174
206
  }
175
207
 
176
- export { ApiStaticDirectus };
208
+ export { ApiStaticDirectus };