@tanglemedia/svelte-starter-directus-api 0.0.1 → 0.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.
- package/package.json +2 -4
- package/dist/auth.d.ts +0 -15
- package/dist/auth.js +0 -117
- package/dist/client.d.ts +0 -20
- package/dist/client.js +0 -161
- package/dist/index.d.ts +0 -4
- package/dist/index.js +0 -4
- package/dist/load-directus-api-povider.d.ts +0 -9
- package/dist/load-directus-api-povider.js +0 -43
- package/dist/static.d.ts +0 -31
- package/dist/static.js +0 -149
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanglemedia/svelte-starter-directus-api",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"main": "src/index.ts",
|
|
5
5
|
"types": "src/index.ts",
|
|
6
6
|
"description": "directus API wrapper for all the directus sdk functionality",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"@directus/sdk": "^13.0.2",
|
|
40
|
-
"@tanglemedia/svelte-starter-core": "^0.0.
|
|
40
|
+
"@tanglemedia/svelte-starter-core": "^0.0.7",
|
|
41
41
|
"flowbite-svelte": "^0.44.19",
|
|
42
42
|
"fontawesome-svelte": "^2.0.1",
|
|
43
43
|
"svelte-fa": "^3.0.4",
|
|
@@ -58,8 +58,6 @@
|
|
|
58
58
|
"lint": "prettier --check --ignore-path=../../.prettierignore . && eslint \".\" --ignore-path=../../.prettierignore",
|
|
59
59
|
"format": "prettier --write --ignore-path=../../.prettierignore . && eslint \".\" --ignore-path=../../.prettierignore",
|
|
60
60
|
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
|
|
61
|
-
"test": "vitest",
|
|
62
|
-
"test:debug": "vitest --test-timeout=0 --reporter=verbose --coverage",
|
|
63
61
|
"clean": "rimraf ./dist && rimraf ./coverage"
|
|
64
62
|
}
|
|
65
63
|
}
|
package/dist/auth.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { DirectusClient, RestClient } from '@directus/sdk';
|
|
2
|
-
declare class ApiAuthDirectus {
|
|
3
|
-
private baseURL;
|
|
4
|
-
private directus;
|
|
5
|
-
constructor(baseURL: string);
|
|
6
|
-
private init;
|
|
7
|
-
getConfig<T extends object>(): DirectusClient<T> & RestClient<object>;
|
|
8
|
-
refresh<T>(): Promise<T>;
|
|
9
|
-
getToken<T>(): Promise<T>;
|
|
10
|
-
login<T>(email: string, password: string): Promise<T>;
|
|
11
|
-
logout<T>(): Promise<T>;
|
|
12
|
-
getCurrentUser<T>(fields?: string[] | null): Promise<T>;
|
|
13
|
-
updateCurrentUser<T>(data: object): Promise<T>;
|
|
14
|
-
}
|
|
15
|
-
export { ApiAuthDirectus };
|
package/dist/auth.js
DELETED
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
import { createDirectus, rest, authentication, refresh, readMe, updateMe } from '@directus/sdk';
|
|
2
|
-
import { browser } from '$app/environment';
|
|
3
|
-
const localDirectusStorage = (storageKey = 'access_token_directus') => {
|
|
4
|
-
if (!browser || !window.localStorage) {
|
|
5
|
-
console.warn('Defaulting to memory storage');
|
|
6
|
-
return {
|
|
7
|
-
get: async () => null,
|
|
8
|
-
set: async (value) => { }
|
|
9
|
-
};
|
|
10
|
-
}
|
|
11
|
-
const storage = window.localStorage;
|
|
12
|
-
return {
|
|
13
|
-
get: async () => {
|
|
14
|
-
const data = storage.getItem(storageKey);
|
|
15
|
-
return data ? JSON.parse(data) : null;
|
|
16
|
-
},
|
|
17
|
-
set: async (value) => {
|
|
18
|
-
if (value === null) {
|
|
19
|
-
storage.removeItem(storageKey);
|
|
20
|
-
}
|
|
21
|
-
else {
|
|
22
|
-
storage.setItem(storageKey, JSON.stringify(value));
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
};
|
|
27
|
-
const authenticationConfig = {
|
|
28
|
-
storage: localDirectusStorage(),
|
|
29
|
-
autoRefresh: true,
|
|
30
|
-
msRefreshBeforeExpires: 30000,
|
|
31
|
-
credentials: 'include'
|
|
32
|
-
};
|
|
33
|
-
class ApiAuthDirectus {
|
|
34
|
-
baseURL;
|
|
35
|
-
directus;
|
|
36
|
-
constructor(baseURL) {
|
|
37
|
-
this.baseURL = baseURL;
|
|
38
|
-
this.init();
|
|
39
|
-
}
|
|
40
|
-
init() {
|
|
41
|
-
try {
|
|
42
|
-
this.directus = createDirectus(this.baseURL).with(rest({ credentials: 'include' })).with(authentication('json', authenticationConfig));
|
|
43
|
-
}
|
|
44
|
-
catch (error) {
|
|
45
|
-
console.error(`Error initializing Directus:`, error);
|
|
46
|
-
throw error;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
getConfig() {
|
|
50
|
-
return this.directus;
|
|
51
|
-
}
|
|
52
|
-
async refresh() {
|
|
53
|
-
try {
|
|
54
|
-
const response = await this.directus.request(refresh());
|
|
55
|
-
return response;
|
|
56
|
-
}
|
|
57
|
-
catch (error) {
|
|
58
|
-
console.error(`Error refreshing :`, error);
|
|
59
|
-
throw error;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
async getToken() {
|
|
63
|
-
try {
|
|
64
|
-
const response = await this.directus.getToken();
|
|
65
|
-
return response;
|
|
66
|
-
}
|
|
67
|
-
catch (error) {
|
|
68
|
-
console.error(`Error refreshing :`, error);
|
|
69
|
-
throw error;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
async login(email, password) {
|
|
73
|
-
try {
|
|
74
|
-
const response = await this.directus.login(email, password);
|
|
75
|
-
return response;
|
|
76
|
-
}
|
|
77
|
-
catch (error) {
|
|
78
|
-
console.error(`Error login :`, error);
|
|
79
|
-
throw error;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
async logout() {
|
|
83
|
-
try {
|
|
84
|
-
const response = await this.directus.logout();
|
|
85
|
-
return response;
|
|
86
|
-
}
|
|
87
|
-
catch (error) {
|
|
88
|
-
console.error(`Error refreshing :`, error);
|
|
89
|
-
throw error;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
async getCurrentUser(fields = null) {
|
|
93
|
-
try {
|
|
94
|
-
if (!this.getToken())
|
|
95
|
-
await this.refresh();
|
|
96
|
-
const response = await this.directus.request(readMe({ fields: fields ? fields : ['*', 'roles.*'] }));
|
|
97
|
-
return response;
|
|
98
|
-
}
|
|
99
|
-
catch (error) {
|
|
100
|
-
console.error(`Error refreshing :`, error);
|
|
101
|
-
throw error;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
async updateCurrentUser(data) {
|
|
105
|
-
try {
|
|
106
|
-
if (!this.getToken())
|
|
107
|
-
await this.refresh();
|
|
108
|
-
const response = await this.directus.request(updateMe(data));
|
|
109
|
-
return response;
|
|
110
|
-
}
|
|
111
|
-
catch (error) {
|
|
112
|
-
console.error(`Error refreshing :`, error);
|
|
113
|
-
throw error;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
export { ApiAuthDirectus };
|
package/dist/client.d.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import type { ApiAdapterInterface, ApiAdapterRequestConfig, AnyObject, BaseApiAdapterConfig } from '@tanglemedia/svelte-starter-core';
|
|
2
|
-
type BaseApiMethods = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
3
|
-
type ApiId = string | number;
|
|
4
|
-
declare class ApiClientDirectus implements ApiAdapterInterface {
|
|
5
|
-
private baseURL;
|
|
6
|
-
private directus;
|
|
7
|
-
constructor(baseURL: string);
|
|
8
|
-
private init;
|
|
9
|
-
getConfig<T extends object>(configuration?: unknown): BaseApiAdapterConfig;
|
|
10
|
-
request<T>(method: BaseApiMethods, url: string, queryParams?: Record<string, any>): Promise<T>;
|
|
11
|
-
find<T>(collection: string, queryParams?: Record<string, unknown>): Promise<T>;
|
|
12
|
-
findOne<T>(collection: string, key?: ApiId, queryParams?: Record<string, unknown>): Promise<T>;
|
|
13
|
-
aggregate<T>(collection: string, queryParams?: Record<string, unknown>): Promise<T>;
|
|
14
|
-
patch<T>(collection: string, key: ApiId, queryParams?: Record<string, unknown>): Promise<T>;
|
|
15
|
-
post<T>(collection: string, data: AnyObject, queryParams?: Record<string, unknown>): Promise<T>;
|
|
16
|
-
delete<T>(collection: string, key?: ApiId): Promise<T>;
|
|
17
|
-
put<T>(collection: string, payload: AnyObject, config?: ApiAdapterRequestConfig, key?: ApiId): Promise<T>;
|
|
18
|
-
upload<T>(path: string | undefined, data: FormData): Promise<T>;
|
|
19
|
-
}
|
|
20
|
-
export { ApiClientDirectus };
|
package/dist/client.js
DELETED
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
import { createDirectus, rest, authentication, readItems, readItem, updateItem, updateItems, createItem, createItems, deleteItem, deleteItems, aggregate, uploadFiles } from '@directus/sdk';
|
|
2
|
-
import { browser } from '$app/environment';
|
|
3
|
-
const localDirectusStorage = (storageKey = 'access_token_directus') => {
|
|
4
|
-
if (!browser || !window.localStorage) {
|
|
5
|
-
console.warn('Defaulting to memory storage');
|
|
6
|
-
return {
|
|
7
|
-
get: async () => null,
|
|
8
|
-
set: async (value) => { }
|
|
9
|
-
};
|
|
10
|
-
}
|
|
11
|
-
const storage = window.localStorage;
|
|
12
|
-
return {
|
|
13
|
-
get: async () => {
|
|
14
|
-
const data = storage.getItem(storageKey);
|
|
15
|
-
return data ? JSON.parse(data) : null;
|
|
16
|
-
},
|
|
17
|
-
set: async (value) => { }
|
|
18
|
-
};
|
|
19
|
-
};
|
|
20
|
-
const authenticationConfig = {
|
|
21
|
-
storage: localDirectusStorage(),
|
|
22
|
-
autoRefresh: true,
|
|
23
|
-
msRefreshBeforeExpires: 30000,
|
|
24
|
-
credentials: 'include'
|
|
25
|
-
};
|
|
26
|
-
class ApiClientDirectus {
|
|
27
|
-
baseURL;
|
|
28
|
-
directus;
|
|
29
|
-
constructor(baseURL) {
|
|
30
|
-
this.baseURL = baseURL;
|
|
31
|
-
this.init();
|
|
32
|
-
}
|
|
33
|
-
init() {
|
|
34
|
-
try {
|
|
35
|
-
this.directus = createDirectus(this.baseURL).with(rest({ credentials: 'include' })).with(authentication('json', authenticationConfig));
|
|
36
|
-
}
|
|
37
|
-
catch (error) {
|
|
38
|
-
console.error(`Error initializing Directus:`, error);
|
|
39
|
-
throw error;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
getConfig(configuration) {
|
|
43
|
-
// return this.directus as DirectusClient<T> & RestClient<object>;
|
|
44
|
-
const client = this.directus;
|
|
45
|
-
// Add the 'configuration' property to the returned object
|
|
46
|
-
const config = {
|
|
47
|
-
configuration: configuration ?? {} // Use the provided configuration or an empty object if not provided
|
|
48
|
-
};
|
|
49
|
-
return Object.assign(client, config);
|
|
50
|
-
}
|
|
51
|
-
async request(method, url, queryParams) {
|
|
52
|
-
try {
|
|
53
|
-
const response = await this.directus.request(() => {
|
|
54
|
-
const params = JSON.stringify(queryParams);
|
|
55
|
-
return {
|
|
56
|
-
path: `${url}?${params}`,
|
|
57
|
-
method: method
|
|
58
|
-
};
|
|
59
|
-
});
|
|
60
|
-
return response;
|
|
61
|
-
}
|
|
62
|
-
catch (error) {
|
|
63
|
-
console.error(`Error request:`, error);
|
|
64
|
-
throw error;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
async find(collection, queryParams) {
|
|
68
|
-
console.log('in find');
|
|
69
|
-
try {
|
|
70
|
-
// console.log(`Find ${collection}`)
|
|
71
|
-
const response = await this.directus.request(readItems(collection, queryParams));
|
|
72
|
-
return response;
|
|
73
|
-
}
|
|
74
|
-
catch (error) {
|
|
75
|
-
console.error(`Error fetching all ${collection}:`, error);
|
|
76
|
-
throw error;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
async findOne(collection, key, queryParams) {
|
|
80
|
-
try {
|
|
81
|
-
const response = await this.directus.request(readItem(collection, key, queryParams));
|
|
82
|
-
return response;
|
|
83
|
-
}
|
|
84
|
-
catch (error) {
|
|
85
|
-
console.error(`Error fetching ${collection}:`, error);
|
|
86
|
-
throw error;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
async aggregate(collection, queryParams) {
|
|
90
|
-
try {
|
|
91
|
-
const response = await this.directus.request(aggregate(collection, {
|
|
92
|
-
aggregate: {
|
|
93
|
-
count: 'id'
|
|
94
|
-
},
|
|
95
|
-
queryParams
|
|
96
|
-
}));
|
|
97
|
-
return response;
|
|
98
|
-
}
|
|
99
|
-
catch (error) {
|
|
100
|
-
console.error(`Error fetching total of ${collection}:`, error);
|
|
101
|
-
throw error;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
async patch(collection, key, queryParams) {
|
|
105
|
-
try {
|
|
106
|
-
const response = await this.directus.request(updateItem(collection, key, queryParams));
|
|
107
|
-
return response;
|
|
108
|
-
}
|
|
109
|
-
catch (error) {
|
|
110
|
-
console.error(`Error updating all ${collection}:`, error);
|
|
111
|
-
throw error;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
async post(collection, data, queryParams) {
|
|
115
|
-
try {
|
|
116
|
-
const response = await this.directus.request(createItem(collection, data, queryParams));
|
|
117
|
-
return response;
|
|
118
|
-
}
|
|
119
|
-
catch (error) {
|
|
120
|
-
console.error(`Error creating items ${collection}:`, error);
|
|
121
|
-
throw error;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
async delete(collection, key) {
|
|
125
|
-
try {
|
|
126
|
-
const response = await this.directus.request(deleteItem(collection, key));
|
|
127
|
-
return response;
|
|
128
|
-
}
|
|
129
|
-
catch (error) {
|
|
130
|
-
console.error(`Error deleting items ${collection}:`, error);
|
|
131
|
-
throw error;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
async put(collection, payload, config, key) {
|
|
135
|
-
try {
|
|
136
|
-
if (key) {
|
|
137
|
-
const response = await this.directus.request(updateItem(collection, key, payload));
|
|
138
|
-
return response;
|
|
139
|
-
}
|
|
140
|
-
else {
|
|
141
|
-
console.error(`Error updating all ${collection}: no key specified`);
|
|
142
|
-
throw new Error('No key specified');
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
catch (error) {
|
|
146
|
-
console.error(`Error updating all ${collection}:`, error);
|
|
147
|
-
throw error;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
async upload(path = '', data) {
|
|
151
|
-
try {
|
|
152
|
-
const response = await this.directus.request(uploadFiles(data));
|
|
153
|
-
return response;
|
|
154
|
-
}
|
|
155
|
-
catch (error) {
|
|
156
|
-
console.error(`Error uploading file:`, error);
|
|
157
|
-
throw error;
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
export { ApiClientDirectus };
|
package/dist/index.d.ts
DELETED
package/dist/index.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { type ApiAdapterProviderInterface, type ApiAdapterInterface, type ApiAdapterRequestConfig, type BaseApiAdapterConfig, type Config, type ApiConfigSchema } from '@tanglemedia/svelte-starter-core';
|
|
2
|
-
declare class LoadDirectusApiProvider implements ApiAdapterProviderInterface {
|
|
3
|
-
private readonly configLoader;
|
|
4
|
-
constructor(configLoader: () => Promise<Config<{
|
|
5
|
-
api?: ApiConfigSchema;
|
|
6
|
-
}>>);
|
|
7
|
-
loadAdapter(key?: string | undefined): Promise<ApiAdapterInterface<BaseApiAdapterConfig, ApiAdapterRequestConfig, string>>;
|
|
8
|
-
}
|
|
9
|
-
export { LoadDirectusApiProvider };
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { ApiClientDirectus } from './client';
|
|
2
|
-
import { ApiStaticDirectus } from './static';
|
|
3
|
-
import {} from '@tanglemedia/svelte-starter-core';
|
|
4
|
-
class LoadDirectusApiProvider {
|
|
5
|
-
configLoader;
|
|
6
|
-
constructor(configLoader) {
|
|
7
|
-
this.configLoader = configLoader;
|
|
8
|
-
}
|
|
9
|
-
async loadAdapter(key) {
|
|
10
|
-
const conf = await this.configLoader();
|
|
11
|
-
const config = (p) => conf.config(p);
|
|
12
|
-
const apiAdapterKey = key || config('api.default');
|
|
13
|
-
const directusType = config(`api.adapters.${apiAdapterKey}.type`) || 'client';
|
|
14
|
-
console.log(`api.apaters.${apiAdapterKey}.type`);
|
|
15
|
-
if (!apiAdapterKey) {
|
|
16
|
-
throw new Error(`No adapter key specified`);
|
|
17
|
-
}
|
|
18
|
-
console.log(apiAdapterKey);
|
|
19
|
-
console.log(directusType);
|
|
20
|
-
if (directusType) {
|
|
21
|
-
if (directusType === 'client') {
|
|
22
|
-
const protocol = config(`api.adapters.${apiAdapterKey}.protocol`) || '';
|
|
23
|
-
const host = config(`api.adapters.${apiAdapterKey}.host`) || '';
|
|
24
|
-
return new ApiClientDirectus(`${protocol}://${host}`.trim());
|
|
25
|
-
}
|
|
26
|
-
else {
|
|
27
|
-
console.log('static');
|
|
28
|
-
const protocol = config(`api.adapters.${apiAdapterKey}.protocol`) || '';
|
|
29
|
-
const host = config(`api.adapters.${apiAdapterKey}.host`) || '';
|
|
30
|
-
const accessToken = config(`api.adapters.${apiAdapterKey}.access_token`) || '';
|
|
31
|
-
return new ApiStaticDirectus(`${protocol}://${host}`.trim(), accessToken);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
else {
|
|
35
|
-
console.log('client');
|
|
36
|
-
const protocol = config(`api.adapters.${apiAdapterKey}.protocol`) || '';
|
|
37
|
-
const host = config(`api.adapters.${apiAdapterKey}.host`) || '';
|
|
38
|
-
return new ApiClientDirectus(`${protocol}://${host}`.trim());
|
|
39
|
-
}
|
|
40
|
-
console.log('This should be unreachable');
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
export { LoadDirectusApiProvider };
|
package/dist/static.d.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import type { ApiAdapterInterface, ApiAdapterRequestConfig, AnyObject, BaseApiAdapterConfig } from '@tanglemedia/svelte-starter-core';
|
|
2
|
-
type BaseApiMethods = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
3
|
-
type ApiId = string | number;
|
|
4
|
-
declare class ApiStaticDirectus implements ApiAdapterInterface {
|
|
5
|
-
private baseURL;
|
|
6
|
-
private directusAccessToken;
|
|
7
|
-
private directus;
|
|
8
|
-
constructor(baseURL: string, directusAccessToken: string);
|
|
9
|
-
private init;
|
|
10
|
-
getConfig<T extends object>(configuration?: unknown): BaseApiAdapterConfig;
|
|
11
|
-
request<T>(method: BaseApiMethods, url: string, queryParams?: Record<string, any>): Promise<T>;
|
|
12
|
-
createUser(data: {
|
|
13
|
-
email: string;
|
|
14
|
-
password: string;
|
|
15
|
-
first_name: string;
|
|
16
|
-
last_name: string;
|
|
17
|
-
role: string;
|
|
18
|
-
} | object): Promise<{
|
|
19
|
-
message: string;
|
|
20
|
-
type: string;
|
|
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>;
|
|
27
|
-
delete<T>(collection: string, keys: ApiId): Promise<T>;
|
|
28
|
-
put<T>(collection: string, payload: AnyObject, config?: ApiAdapterRequestConfig, key?: ApiId): Promise<T>;
|
|
29
|
-
upload<T>(path: string | undefined, data: FormData): Promise<T>;
|
|
30
|
-
}
|
|
31
|
-
export { ApiStaticDirectus };
|
package/dist/static.js
DELETED
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
import { createDirectus, rest, staticToken, readItems, readItem, updateItem, updateItems, createItem, createUser, deleteItem, aggregate, uploadFiles } from '@directus/sdk';
|
|
2
|
-
class ApiStaticDirectus {
|
|
3
|
-
baseURL;
|
|
4
|
-
directusAccessToken;
|
|
5
|
-
directus;
|
|
6
|
-
constructor(baseURL, directusAccessToken) {
|
|
7
|
-
this.baseURL = baseURL;
|
|
8
|
-
this.directusAccessToken = directusAccessToken;
|
|
9
|
-
this.init();
|
|
10
|
-
}
|
|
11
|
-
init() {
|
|
12
|
-
try {
|
|
13
|
-
this.directus = createDirectus(this.baseURL)
|
|
14
|
-
.with(rest())
|
|
15
|
-
.with(staticToken(this.directusAccessToken));
|
|
16
|
-
}
|
|
17
|
-
catch (error) {
|
|
18
|
-
console.error(`Error initializing Directus admin:`, error);
|
|
19
|
-
throw error;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
getConfig(configuration) {
|
|
23
|
-
// return this.directus as DirectusClient<T> & RestClient<object>;
|
|
24
|
-
const client = this.directus;
|
|
25
|
-
// Add the 'configuration' property to the returned object
|
|
26
|
-
const config = {
|
|
27
|
-
configuration: configuration ?? {} // Use the provided configuration or an empty object if not provided
|
|
28
|
-
};
|
|
29
|
-
return Object.assign(client, config);
|
|
30
|
-
}
|
|
31
|
-
async request(method, url, queryParams) {
|
|
32
|
-
try {
|
|
33
|
-
const response = await this.directus.request(() => {
|
|
34
|
-
const params = JSON.stringify(queryParams);
|
|
35
|
-
return {
|
|
36
|
-
path: `${url}?${params}`,
|
|
37
|
-
method: method
|
|
38
|
-
};
|
|
39
|
-
});
|
|
40
|
-
return response;
|
|
41
|
-
}
|
|
42
|
-
catch (error) {
|
|
43
|
-
console.error(`Error request:`, error);
|
|
44
|
-
throw error;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
async createUser(data) {
|
|
48
|
-
try {
|
|
49
|
-
await this.directus.request(createUser(data));
|
|
50
|
-
return { message: 'You have successfully registered your account', type: 'success' };
|
|
51
|
-
}
|
|
52
|
-
catch (error) {
|
|
53
|
-
console.error(`Error creating a user:`, error);
|
|
54
|
-
throw error;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
async find(collection, queryParams) {
|
|
58
|
-
try {
|
|
59
|
-
const response = await this.directus.request(readItems(collection, queryParams));
|
|
60
|
-
return response;
|
|
61
|
-
}
|
|
62
|
-
catch (error) {
|
|
63
|
-
console.error(`Error fetching all ${collection}:`, error);
|
|
64
|
-
throw error;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
async findOne(collection, key, queryParams) {
|
|
68
|
-
try {
|
|
69
|
-
const response = await this.directus.request(readItem(collection, key, queryParams));
|
|
70
|
-
return response;
|
|
71
|
-
}
|
|
72
|
-
catch (error) {
|
|
73
|
-
console.error(`Error fetching ${collection}:`, error);
|
|
74
|
-
throw error;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
async aggregate(collection, queryParams) {
|
|
78
|
-
try {
|
|
79
|
-
const response = await this.directus.request(aggregate(collection, {
|
|
80
|
-
aggregate: {
|
|
81
|
-
count: 'id'
|
|
82
|
-
},
|
|
83
|
-
queryParams
|
|
84
|
-
}));
|
|
85
|
-
return response;
|
|
86
|
-
}
|
|
87
|
-
catch (error) {
|
|
88
|
-
console.error(`Error fetching total of ${collection}:`, error);
|
|
89
|
-
throw error;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
async patch(collection, key, queryParams) {
|
|
93
|
-
try {
|
|
94
|
-
const response = await this.directus.request(updateItems(collection, key, queryParams));
|
|
95
|
-
return response;
|
|
96
|
-
}
|
|
97
|
-
catch (error) {
|
|
98
|
-
console.error(`Error updating all ${collection}:`, error);
|
|
99
|
-
throw error;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
async post(collection, data, queryParams) {
|
|
103
|
-
try {
|
|
104
|
-
const response = await this.directus.request(createItem(collection, data, queryParams));
|
|
105
|
-
return response;
|
|
106
|
-
}
|
|
107
|
-
catch (error) {
|
|
108
|
-
console.error(`Error creating items ${collection}:`, error);
|
|
109
|
-
throw error;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
async delete(collection, keys) {
|
|
113
|
-
try {
|
|
114
|
-
const response = await this.directus.request(deleteItem(collection, key));
|
|
115
|
-
return response;
|
|
116
|
-
}
|
|
117
|
-
catch (error) {
|
|
118
|
-
console.error(`Error deleting items ${collection}:`, error);
|
|
119
|
-
throw error;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
async put(collection, payload, config, key) {
|
|
123
|
-
try {
|
|
124
|
-
if (key) {
|
|
125
|
-
const response = await this.directus.request(updateItem(collection, key, payload));
|
|
126
|
-
return response;
|
|
127
|
-
}
|
|
128
|
-
else {
|
|
129
|
-
console.error(`Error updating all ${collection}: no key specified`);
|
|
130
|
-
throw new Error('No key specified');
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
catch (error) {
|
|
134
|
-
console.error(`Error updating all ${collection}:`, error);
|
|
135
|
-
throw error;
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
async upload(path = '', data) {
|
|
139
|
-
try {
|
|
140
|
-
const response = await this.directus.request(uploadFiles(data));
|
|
141
|
-
return response;
|
|
142
|
-
}
|
|
143
|
-
catch (error) {
|
|
144
|
-
console.error(`Error uploading file:`, error);
|
|
145
|
-
throw error;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
export { ApiStaticDirectus };
|