@tanglemedia/svelte-starter-directus-api 0.1.0 → 0.1.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.
@@ -17,6 +17,6 @@ export declare class DirectusApiAdapter<Schema extends SchemaShape = SchemaShape
17
17
  patch<T>(collection: Path, key: ApiId, query?: Record<string, unknown>): Promise<ApiResponse<T>>;
18
18
  post<T>(collection: Path, data: AnyObject, query?: Record<string, unknown>): Promise<ApiResponse<T>>;
19
19
  delete<T>(collection: Path, key?: ApiId): Promise<ApiResponse<T>>;
20
- put<T>(collection: Path, key?: ApiId, payload?: AnyObject, config?: ApiAdapterRequestConfig): Promise<ApiResponse<T>>;
20
+ put<T>(collection: Path, key?: ApiId, payload?: AnyObject): Promise<ApiResponse<T>>;
21
21
  upload<T>(path: string | undefined, data: FormData): Promise<ApiResponse<T>>;
22
22
  }
@@ -1,4 +1,4 @@
1
- import { aggregate, createItem, deleteItem, readItem, readItems, updateItem, uploadFiles } from '@directus/sdk';
1
+ import { aggregate, createItem, deleteItem, readFiles, readItem, readItems, updateItem, uploadFiles } from '@directus/sdk';
2
2
  import { ApiAdapterAbstract } from '@tanglemedia/svelte-starter-core';
3
3
  export class DirectusApiAdapter extends ApiAdapterAbstract {
4
4
  config;
@@ -82,7 +82,7 @@ export class DirectusApiAdapter extends ApiAdapterAbstract {
82
82
  const response = await this.directus.request(deleteItem(collection, key));
83
83
  return this.transformResponse(response, 202);
84
84
  }
85
- async put(collection, key, payload, config) {
85
+ async put(collection, key, payload) {
86
86
  if (key) {
87
87
  const response = await this.directus.request(updateItem(collection, key, payload));
88
88
  return this.transformResponse(response, 201);
@@ -23,18 +23,13 @@ export const createDirectusClientFactory = ({ baseUrl, configuration: { protocol
23
23
  }
24
24
  let client = createDirectus(url, options).with(rest(rs));
25
25
  if (auth) {
26
- if (auth.config?.storage === 'localStorage') {
27
- const authenticationConfig = {
28
- storage: new AuthLocalStorage(),
29
- autoRefresh: auth.config.autoRefresh || false,
30
- msRefreshBeforeExpires: auth.config.msRefreshBeforeExpires || 0,
31
- credentials: auth.config.credentials || 'include'
32
- };
33
- client = client.with(authentication(auth.mode, authenticationConfig));
34
- }
35
- else {
36
- client = client.with(authentication(auth.mode));
26
+ const { config: c, mode } = auth;
27
+ const { storage, ...config } = { ...authConfigDefaults, ...(c || {}) };
28
+ const authConfig = config;
29
+ if (storage === 'localStorage') {
30
+ authConfig.storage = new AuthLocalStorage();
37
31
  }
32
+ client = client.with(authentication(mode, authConfig));
38
33
  }
39
34
  if (token) {
40
35
  client = client.with(staticToken(token));
@@ -3,11 +3,12 @@ import type { DirectusClient, RestClient } from '@directus/sdk';
3
3
  import { ServiceAdapterAbstract } from '@tanglemedia/svelte-starter-core';
4
4
  declare class DirectusAuthServices extends ServiceAdapterAbstract {
5
5
  getDirectus(): Promise<DirectusClient<object> & RestClient<object> & AuthenticationClient<object>>;
6
- refresh<T>(): Promise<T>;
6
+ refresh(): Promise<import("@directus/sdk/dist/login-M53C1LeV").a>;
7
7
  getToken(): Promise<string | null>;
8
8
  login(email: string, password: string): Promise<import("@directus/sdk/dist/login-M53C1LeV").a>;
9
9
  logout(): Promise<void>;
10
10
  getCurrentUser<T>(fields?: string[] | null): Promise<T>;
11
11
  updateCurrentUser<T>(data: object): Promise<T>;
12
+ readFile<T>(query?: Record<string, unknown>): Promise<T>;
12
13
  }
13
14
  export { DirectusAuthServices };
@@ -1,4 +1,4 @@
1
- import { readMe, refresh, updateMe } from '@directus/sdk';
1
+ import { readFiles, readMe, updateMe } from '@directus/sdk';
2
2
  import { ServiceAdapterAbstract } from '@tanglemedia/svelte-starter-core';
3
3
  class DirectusAuthServices extends ServiceAdapterAbstract {
4
4
  getDirectus() {
@@ -9,11 +9,11 @@ class DirectusAuthServices extends ServiceAdapterAbstract {
9
9
  // }
10
10
  async refresh() {
11
11
  try {
12
- const response = await (await this.getDirectus()).request(refresh());
12
+ const response = await (await this.getDirectus()).refresh();
13
13
  return response;
14
14
  }
15
15
  catch (error) {
16
- console.error(`Error refreshing :`, error);
16
+ console.error(`Error on directus sdk refresh() :`, error);
17
17
  throw error;
18
18
  }
19
19
  }
@@ -23,7 +23,7 @@ class DirectusAuthServices extends ServiceAdapterAbstract {
23
23
  return response;
24
24
  }
25
25
  catch (error) {
26
- console.error(`Error refreshing :`, error);
26
+ console.error(`Error on directus sdk getToken() :`, error);
27
27
  throw error;
28
28
  }
29
29
  }
@@ -33,7 +33,7 @@ class DirectusAuthServices extends ServiceAdapterAbstract {
33
33
  return response;
34
34
  }
35
35
  catch (error) {
36
- console.error(`Error login :`, error);
36
+ console.error(`Error on directus sdk login() :`, error);
37
37
  throw error;
38
38
  }
39
39
  }
@@ -43,31 +43,41 @@ class DirectusAuthServices extends ServiceAdapterAbstract {
43
43
  return response;
44
44
  }
45
45
  catch (error) {
46
- console.error(`Error refreshing :`, error);
46
+ console.error(`Error on directus sdk logout() :`, error);
47
47
  throw error;
48
48
  }
49
49
  }
50
50
  async getCurrentUser(fields = null) {
51
51
  try {
52
- if (!this.getToken())
52
+ if (!await this.getToken())
53
53
  await this.refresh();
54
54
  const response = await (await this.getDirectus()).request(readMe({ fields: fields ? fields : ['*', 'roles.*'] }));
55
55
  return response;
56
56
  }
57
57
  catch (error) {
58
- console.error(`Error refreshing :`, error);
58
+ console.error(`Error on directus sdk readMe() :`, error);
59
59
  throw error;
60
60
  }
61
61
  }
62
62
  async updateCurrentUser(data) {
63
63
  try {
64
- if (!this.getToken())
64
+ if (!await this.getToken())
65
65
  await this.refresh();
66
66
  const response = await (await this.getDirectus()).request(updateMe(data));
67
67
  return response;
68
68
  }
69
69
  catch (error) {
70
- console.error(`Error refreshing :`, error);
70
+ console.error(`Error on directus sdk updateMe() :`, error);
71
+ throw error;
72
+ }
73
+ }
74
+ async readFile(query) {
75
+ try {
76
+ const response = await (await this.getDirectus()).request(readFiles(query));
77
+ return response;
78
+ }
79
+ catch (error) {
80
+ console.error('Error on directus sdk readFiles():', error);
71
81
  throw error;
72
82
  }
73
83
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanglemedia/svelte-starter-directus-api",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
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,18 +22,18 @@
22
22
  }
23
23
  },
24
24
  "devDependencies": {
25
- "@sveltejs/adapter-auto": "^3.1.0",
25
+ "@sveltejs/adapter-auto": "^3.1.1",
26
26
  "@sveltejs/package": "^2.2.6",
27
- "@sveltejs/vite-plugin-svelte": "^3.0.1",
28
- "@testing-library/jest-dom": "^6.2.0",
29
- "@testing-library/svelte": "^4.0.5",
30
- "@vitest/coverage-v8": "^1.2.1",
31
- "jsdom": "^23.2.0",
32
- "msw": "^2.1.2",
27
+ "@sveltejs/vite-plugin-svelte": "^3.0.2",
28
+ "@testing-library/jest-dom": "^6.4.0",
29
+ "@testing-library/svelte": "^4.1.0",
30
+ "@vitest/coverage-v8": "^1.2.2",
31
+ "jsdom": "^24.0.0",
32
+ "msw": "^2.1.5",
33
33
  "svelte": "^4.2.9",
34
34
  "svelte-check": "^3.6.3",
35
35
  "vite": "^5.0.12",
36
- "vitest": "^1.2.1",
36
+ "vitest": "^1.2.2",
37
37
  "eslint-config-custom": "0.0.0"
38
38
  },
39
39
  "dependencies": {
@@ -41,7 +41,7 @@
41
41
  "@types/js-cookie": "^3.0.6",
42
42
  "esm-env": "^1.0.0",
43
43
  "js-cookie": "^3.0.5",
44
- "@tanglemedia/svelte-starter-core": "0.0.17"
44
+ "@tanglemedia/svelte-starter-core": "0.0.18"
45
45
  },
46
46
  "peerDependencies": {
47
47
  "@sveltejs/kit": ">=2 <3",
@@ -2,6 +2,7 @@ import {
2
2
  aggregate,
3
3
  createItem,
4
4
  deleteItem,
5
+ readFiles,
5
6
  readItem,
6
7
  readItems,
7
8
  updateItem,
@@ -145,12 +146,7 @@ export class DirectusApiAdapter<
145
146
  return this.transformResponse(response, 202);
146
147
  }
147
148
 
148
- public async put<T>(
149
- collection: Path,
150
- key?: ApiId,
151
- payload?: AnyObject,
152
- config?: ApiAdapterRequestConfig
153
- ): Promise<ApiResponse<T>> {
149
+ public async put<T>(collection: Path, key?: ApiId, payload?: AnyObject): Promise<ApiResponse<T>> {
154
150
  if (key) {
155
151
  const response = await this.directus.request<T>(updateItem(collection, key, payload));
156
152
  return this.transformResponse(response, 201);
@@ -45,17 +45,16 @@ export const createDirectusClientFactory = <T extends object>(
45
45
  let client = createDirectus<T>(url, options).with(rest(rs));
46
46
 
47
47
  if (auth) {
48
- if (auth.config?.storage === 'localStorage') {
49
- const authenticationConfig: AuthenticationConfig = {
50
- storage: new AuthLocalStorage(),
51
- autoRefresh: auth.config.autoRefresh || false,
52
- msRefreshBeforeExpires: auth.config.msRefreshBeforeExpires || 0,
53
- credentials: auth.config.credentials || 'include'
54
- };
55
- client = client.with(authentication(auth.mode, authenticationConfig));
56
- } else {
57
- client = client.with(authentication(auth.mode));
48
+ const { config: c, mode } = auth;
49
+ const { storage, ...config } = { ...authConfigDefaults, ...(c || {}) };
50
+
51
+ const authConfig: AuthenticationConfig = config;
52
+
53
+ if (storage === 'localStorage') {
54
+ authConfig.storage = new AuthLocalStorage();
58
55
  }
56
+
57
+ client = client.with(authentication(mode, authConfig));
59
58
  }
60
59
 
61
60
  if (token) {
@@ -1,5 +1,5 @@
1
1
  import type { AuthenticationClient } from '@directus/sdk';
2
- import { readMe, refresh, updateMe } from '@directus/sdk';
2
+ import { readFiles, readMe, updateMe } from '@directus/sdk';
3
3
  import type { DirectusClient, RestClient } from '@directus/sdk';
4
4
  import { ServiceAdapterAbstract } from '@tanglemedia/svelte-starter-core';
5
5
 
@@ -14,12 +14,12 @@ class DirectusAuthServices extends ServiceAdapterAbstract {
14
14
  // return this.directus as DirectusClient<T> & RestClient<object>;
15
15
  // }
16
16
 
17
- public async refresh<T>(): Promise<T> {
17
+ public async refresh() {
18
18
  try {
19
- const response = await (await this.getDirectus()).request<T>(refresh());
19
+ const response = await (await this.getDirectus()).refresh();
20
20
  return response;
21
21
  } catch (error) {
22
- console.error(`Error refreshing :`, error);
22
+ console.error(`Error on directus sdk refresh() :`, error);
23
23
  throw error;
24
24
  }
25
25
  }
@@ -29,7 +29,7 @@ class DirectusAuthServices extends ServiceAdapterAbstract {
29
29
  const response = await (await this.getDirectus()).getToken();
30
30
  return response;
31
31
  } catch (error) {
32
- console.error(`Error refreshing :`, error);
32
+ console.error(`Error on directus sdk getToken() :`, error);
33
33
  throw error;
34
34
  }
35
35
  }
@@ -39,7 +39,7 @@ class DirectusAuthServices extends ServiceAdapterAbstract {
39
39
  const response = await (await this.getDirectus()).login(email, password);
40
40
  return response;
41
41
  } catch (error) {
42
- console.error(`Error login :`, error);
42
+ console.error(`Error on directus sdk login() :`, error);
43
43
  throw error;
44
44
  }
45
45
  }
@@ -49,31 +49,41 @@ class DirectusAuthServices extends ServiceAdapterAbstract {
49
49
  const response = await (await this.getDirectus()).logout();
50
50
  return response;
51
51
  } catch (error) {
52
- console.error(`Error refreshing :`, error);
52
+ console.error(`Error on directus sdk logout() :`, error);
53
53
  throw error;
54
54
  }
55
55
  }
56
56
 
57
57
  public async getCurrentUser<T>(fields: string[] | null = null): Promise<T> {
58
58
  try {
59
- if (!this.getToken()) await this.refresh();
59
+ if (!await this.getToken()) await this.refresh();
60
60
  const response = await (
61
61
  await this.getDirectus()
62
62
  ).request<T>(readMe({ fields: fields ? fields : ['*', 'roles.*'] }));
63
63
  return response;
64
64
  } catch (error) {
65
- console.error(`Error refreshing :`, error);
65
+ console.error(`Error on directus sdk readMe() :`, error);
66
66
  throw error;
67
67
  }
68
68
  }
69
69
 
70
70
  public async updateCurrentUser<T>(data: object): Promise<T> {
71
71
  try {
72
- if (!this.getToken()) await this.refresh();
72
+ if (!await this.getToken()) await this.refresh();
73
73
  const response = await (await this.getDirectus()).request<T>(updateMe(data));
74
74
  return response;
75
75
  } catch (error) {
76
- console.error(`Error refreshing :`, error);
76
+ console.error(`Error on directus sdk updateMe() :`, error);
77
+ throw error;
78
+ }
79
+ }
80
+
81
+ public async readFile<T>(query?: Record<string, unknown>): Promise<T> {
82
+ try {
83
+ const response = await (await this.getDirectus()).request<T>(readFiles(query));
84
+ return response;
85
+ } catch (error) {
86
+ console.error('Error on directus sdk readFiles():', error);
77
87
  throw error;
78
88
  }
79
89
  }