@tanglemedia/svelte-starter-directus-api 0.0.16 → 0.1.1
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/adapter/api-adapter.d.ts +22 -0
- package/dist/adapter/api-adapter.js +100 -0
- package/dist/{auth.js → auth.deprecated.js} +3 -42
- package/dist/{client.js → client.deprecated.js} +3 -36
- package/dist/index.d.ts +9 -4
- package/dist/index.js +8 -4
- package/dist/{load-directus-api-povider.d.ts → load-directus-api-povider.deprecated.d.ts} +3 -2
- package/dist/{load-directus-api-povider.js → load-directus-api-povider.deprecated.js} +16 -11
- package/dist/provider/api-adapter.provider.d.ts +7 -0
- package/dist/provider/api-adapter.provider.js +21 -0
- package/dist/provider/directus.factory.d.ts +6 -0
- package/dist/provider/directus.factory.js +41 -0
- package/dist/services/auth.d.ts +14 -0
- package/dist/services/auth.js +85 -0
- package/dist/services/register.d.ts +16 -0
- package/dist/services/register.js +18 -0
- package/dist/static.deprecated.d.ts +22 -0
- package/dist/static.deprecated.js +58 -0
- package/dist/storage/local-storage.d.ts +9 -0
- package/dist/storage/local-storage.js +30 -0
- package/dist/types/adapter.types.d.ts +23 -0
- package/dist/types/adapter.types.js +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js +1 -0
- package/package.json +10 -10
- package/src/adapter/api-adapter.ts +164 -0
- package/src/{auth.ts → auth.deprecated.ts} +10 -65
- package/src/{static.ts → client.deprecated.ts} +31 -33
- package/src/index.ts +12 -4
- package/src/{load-directus-api-povider.ts → load-directus-api-povider.deprecated.ts} +21 -14
- package/src/provider/api-adapter.provider.ts +35 -0
- package/src/provider/directus.factory.ts +69 -0
- package/src/services/auth.ts +92 -0
- package/src/services/register.ts +28 -0
- package/src/static.deprecated.ts +77 -0
- package/src/storage/local-storage.ts +33 -0
- package/src/types/adapter.types.ts +30 -0
- package/src/types/index.ts +1 -0
- package/dist/static.d.ts +0 -31
- package/dist/static.js +0 -151
- package/src/client.ts +0 -254
- /package/dist/{auth.d.ts → auth.deprecated.d.ts} +0 -0
- /package/dist/{client.d.ts → client.deprecated.d.ts} +0 -0
package/src/client.ts
DELETED
|
@@ -1,254 +0,0 @@
|
|
|
1
|
-
import type { AuthenticationConfig, AuthenticationData } from '@directus/sdk';
|
|
2
|
-
import {
|
|
3
|
-
aggregate,
|
|
4
|
-
authentication,
|
|
5
|
-
createDirectus,
|
|
6
|
-
createItem,
|
|
7
|
-
deleteItem,
|
|
8
|
-
readItem,
|
|
9
|
-
readItems,
|
|
10
|
-
rest,
|
|
11
|
-
updateItem,
|
|
12
|
-
uploadFiles,
|
|
13
|
-
memoryStorage
|
|
14
|
-
} from '@directus/sdk';
|
|
15
|
-
import type { DirectusClient, RestClient } from '@directus/sdk';
|
|
16
|
-
import type {
|
|
17
|
-
AnyObject,
|
|
18
|
-
ApiAdapterInterface,
|
|
19
|
-
ApiAdapterRequestConfig,
|
|
20
|
-
BaseApiAdapterConfig
|
|
21
|
-
} from '@tanglemedia/svelte-starter-core';
|
|
22
|
-
|
|
23
|
-
import { browser } from '$app/environment';
|
|
24
|
-
|
|
25
|
-
import cookie from 'js-cookie';
|
|
26
|
-
|
|
27
|
-
const localDirectusStorage = (storageKey: string = 'access_token_directus') => {
|
|
28
|
-
if (!browser || !window.localStorage) {
|
|
29
|
-
console.warn('Defaulting to memory storage');
|
|
30
|
-
return memoryStorage();
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const storage = window.localStorage;
|
|
34
|
-
|
|
35
|
-
return {
|
|
36
|
-
get: async (): Promise<AuthenticationData | null> => {
|
|
37
|
-
const data = storage.getItem(storageKey);
|
|
38
|
-
return data ? (JSON.parse(data) as AuthenticationData) : null;
|
|
39
|
-
},
|
|
40
|
-
set: async (value: AuthenticationData | null): Promise<void> => {
|
|
41
|
-
if (value === null) {
|
|
42
|
-
storage.removeItem(storageKey);
|
|
43
|
-
} else {
|
|
44
|
-
storage.setItem(storageKey, JSON.stringify(value));
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
// const cookieDirectusStorage = (cookieKey: string = 'access_token_directus') => {
|
|
51
|
-
// return {
|
|
52
|
-
// get: async (): Promise<AuthenticationData | null> => {
|
|
53
|
-
// const data = JSON.parse(cookie.get(cookieKey)) || '{}';
|
|
54
|
-
// return data ? (data as AuthenticationData) : null;
|
|
55
|
-
|
|
56
|
-
// },
|
|
57
|
-
// set: async (value: AuthenticationData | null): Promise<void> => {
|
|
58
|
-
// cookie.set(cookieKey, JSON.stringify(value))
|
|
59
|
-
// }
|
|
60
|
-
// };
|
|
61
|
-
// }
|
|
62
|
-
|
|
63
|
-
const authenticationConfig: AuthenticationConfig = {
|
|
64
|
-
storage: localDirectusStorage(),
|
|
65
|
-
autoRefresh: true,
|
|
66
|
-
msRefreshBeforeExpires: 30000,
|
|
67
|
-
credentials: 'include'
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
// const authenticationCookieConfig: AuthenticationConfig = {
|
|
71
|
-
// storage: cookieDirectusStorage(),
|
|
72
|
-
// autoRefresh: true,
|
|
73
|
-
// msRefreshBeforeExpires: 30000,
|
|
74
|
-
// credentials: 'include'
|
|
75
|
-
// };
|
|
76
|
-
|
|
77
|
-
type BaseApiMethods = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
78
|
-
|
|
79
|
-
type ApiId = string | number;
|
|
80
|
-
|
|
81
|
-
class ApiClientDirectus implements ApiAdapterInterface {
|
|
82
|
-
private baseURL: string;
|
|
83
|
-
private storageMode: 'json' | 'cookie';
|
|
84
|
-
private directus: DirectusClient<object> & RestClient<object>;
|
|
85
|
-
|
|
86
|
-
constructor(baseURL: string, storageMode: 'json' | 'cookie' = 'json') {
|
|
87
|
-
this.baseURL = baseURL;
|
|
88
|
-
this.storageMode = storageMode;
|
|
89
|
-
this.init();
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
private init<T extends object>(): void {
|
|
93
|
-
try {
|
|
94
|
-
if(this.storageMode === 'json'){
|
|
95
|
-
this.directus = createDirectus<T>(this.baseURL)
|
|
96
|
-
.with(rest({ credentials: 'include' }))
|
|
97
|
-
.with(authentication('json', authenticationConfig));
|
|
98
|
-
}else {
|
|
99
|
-
this.directus = createDirectus<T>(this.baseURL)
|
|
100
|
-
.with(authentication('cookie', { credentials: 'include' }))
|
|
101
|
-
.with(rest({ credentials: 'include' }));
|
|
102
|
-
}
|
|
103
|
-
} catch (error) {
|
|
104
|
-
console.error(`Error initializing Directus:`, error);
|
|
105
|
-
throw error;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
public getConfig<T extends object>(configuration?: unknown): BaseApiAdapterConfig {
|
|
110
|
-
// return this.directus as DirectusClient<T> & RestClient<object>;
|
|
111
|
-
const client = this.directus as DirectusClient<T> & RestClient<object>;
|
|
112
|
-
|
|
113
|
-
// Add the 'configuration' property to the returned object
|
|
114
|
-
const config: BaseApiAdapterConfig = {
|
|
115
|
-
configuration: configuration ?? {} // Use the provided configuration or an empty object if not provided
|
|
116
|
-
};
|
|
117
|
-
|
|
118
|
-
return Object.assign(client, config);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
public async request<T>(
|
|
122
|
-
method: BaseApiMethods,
|
|
123
|
-
url: string,
|
|
124
|
-
query?: Record<string, unknown>
|
|
125
|
-
): Promise<T> {
|
|
126
|
-
try {
|
|
127
|
-
const response = await this.directus.request<T>(() => {
|
|
128
|
-
const params = JSON.stringify(query);
|
|
129
|
-
return {
|
|
130
|
-
path: `${url}?${params}`,
|
|
131
|
-
method: method
|
|
132
|
-
};
|
|
133
|
-
});
|
|
134
|
-
return response;
|
|
135
|
-
} catch (error) {
|
|
136
|
-
console.error(`Error request:`, error);
|
|
137
|
-
throw error;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
public async find<T>(collection: string, query?: Record<string, unknown>): Promise<T> {
|
|
142
|
-
// console.log('in find');
|
|
143
|
-
try {
|
|
144
|
-
// console.log(`Find ${collection}`)
|
|
145
|
-
const response = await this.directus.request<T>(readItems(collection, query));
|
|
146
|
-
return response;
|
|
147
|
-
} catch (error) {
|
|
148
|
-
console.error(`Error fetching all ${collection}:`, error);
|
|
149
|
-
throw error;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
public async findOne<T>(
|
|
154
|
-
collection: string,
|
|
155
|
-
key?: ApiId,
|
|
156
|
-
query?: Record<string, unknown>
|
|
157
|
-
): Promise<T> {
|
|
158
|
-
try {
|
|
159
|
-
const response = await this.directus.request<T>(readItem(collection, key, query));
|
|
160
|
-
return response;
|
|
161
|
-
} catch (error) {
|
|
162
|
-
console.error(`Error fetching ${collection}:`, error);
|
|
163
|
-
throw error;
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
public async aggregate<T>(collection: string, query?: Record<string, unknown>): Promise<T> {
|
|
168
|
-
try {
|
|
169
|
-
const response = await this.directus.request<T>(
|
|
170
|
-
aggregate(collection, {
|
|
171
|
-
aggregate: {
|
|
172
|
-
count: 'id'
|
|
173
|
-
},
|
|
174
|
-
query
|
|
175
|
-
})
|
|
176
|
-
);
|
|
177
|
-
return response;
|
|
178
|
-
} catch (error) {
|
|
179
|
-
console.error(`Error fetching total of ${collection}:`, error);
|
|
180
|
-
throw error;
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
public async patch<T>(
|
|
185
|
-
collection: string,
|
|
186
|
-
key: ApiId,
|
|
187
|
-
query?: Record<string, unknown>
|
|
188
|
-
): Promise<T> {
|
|
189
|
-
try {
|
|
190
|
-
const response = await this.directus.request<T>(updateItem(collection, key, query));
|
|
191
|
-
return response;
|
|
192
|
-
} catch (error) {
|
|
193
|
-
console.error(`Error updating all ${collection}:`, error);
|
|
194
|
-
throw error;
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
public async post<T>(
|
|
199
|
-
collection: string,
|
|
200
|
-
data: AnyObject,
|
|
201
|
-
query?: Record<string, unknown>
|
|
202
|
-
): Promise<T> {
|
|
203
|
-
try {
|
|
204
|
-
const response = await this.directus.request<T>(createItem(collection, data, query));
|
|
205
|
-
return response;
|
|
206
|
-
} catch (error) {
|
|
207
|
-
console.error(`Error creating items ${collection}:`, error);
|
|
208
|
-
throw error;
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
public async delete<T>(collection: string, key?: ApiId): Promise<T> {
|
|
213
|
-
try {
|
|
214
|
-
const response = await this.directus.request<T>(deleteItem(collection, key));
|
|
215
|
-
return response;
|
|
216
|
-
} catch (error) {
|
|
217
|
-
console.error(`Error deleting items ${collection}:`, error);
|
|
218
|
-
throw error;
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
public async put<T>(
|
|
223
|
-
collection: string,
|
|
224
|
-
payload: AnyObject,
|
|
225
|
-
config?: ApiAdapterRequestConfig,
|
|
226
|
-
key?: ApiId
|
|
227
|
-
): Promise<T> {
|
|
228
|
-
try {
|
|
229
|
-
if (key) {
|
|
230
|
-
const response = await this.directus.request<T>(updateItem(collection, key, payload));
|
|
231
|
-
return response;
|
|
232
|
-
} else {
|
|
233
|
-
console.error(`Error updating all ${collection}: no key specified`);
|
|
234
|
-
throw new Error('No key specified');
|
|
235
|
-
}
|
|
236
|
-
} catch (error) {
|
|
237
|
-
console.error(`Error updating all ${collection}:`, error);
|
|
238
|
-
throw error;
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
243
|
-
public async upload<T>(path: string = '', data: FormData): Promise<T> {
|
|
244
|
-
try {
|
|
245
|
-
const response = await this.directus.request<T>(uploadFiles(data));
|
|
246
|
-
return response;
|
|
247
|
-
} catch (error) {
|
|
248
|
-
console.error(`Error uploading file:`, error);
|
|
249
|
-
throw error;
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
export { ApiClientDirectus };
|
|
File without changes
|
|
File without changes
|