@tanglemedia/svelte-starter-directus-api 0.0.15 → 0.1.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/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 +46 -0
- package/dist/services/auth.d.ts +13 -0
- package/dist/services/auth.js +75 -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 +3 -3
- package/src/adapter/api-adapter.ts +168 -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 +70 -0
- package/src/services/auth.ts +82 -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
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type DirectusClient, type RestClient } from '@directus/sdk';
|
|
2
|
+
import { ApiAdapterAbstract, type AnyObject, type ApiAdapterRequestConfig, type ApiId, type ApiResponse, type BaseApiMethods } from '@tanglemedia/svelte-starter-core';
|
|
3
|
+
import type { DirectusConfig, SchemaShape } from '../types/adapter.types';
|
|
4
|
+
export type AdapterClient<Schema extends SchemaShape = SchemaShape> = DirectusClient<Schema> & RestClient<Schema>;
|
|
5
|
+
export declare class DirectusApiAdapter<Schema extends SchemaShape = SchemaShape, Path extends string = keyof SchemaShape> extends ApiAdapterAbstract<DirectusConfig, ApiAdapterRequestConfig, Path, AdapterClient> {
|
|
6
|
+
protected config: DirectusConfig;
|
|
7
|
+
private readonly directus;
|
|
8
|
+
constructor(config: DirectusConfig, directus: AdapterClient<Schema>);
|
|
9
|
+
getAdapterClient(): AdapterClient<Schema> | null;
|
|
10
|
+
normalizeMeta(payload: AnyObject): AnyObject;
|
|
11
|
+
transformResponse<T, M extends object = AnyObject>(res: T, status?: number): Promise<ApiResponse<T, Response, M>>;
|
|
12
|
+
getConfig(configuration?: unknown): DirectusConfig;
|
|
13
|
+
request<T>(method: BaseApiMethods, url: string, query?: Record<string, unknown>): Promise<T>;
|
|
14
|
+
find<T>(collection: Path, query?: Record<string, unknown>): Promise<ApiResponse<T>>;
|
|
15
|
+
findOne<T>(collection: Path, key?: ApiId, query?: Record<string, unknown>): Promise<ApiResponse<T>>;
|
|
16
|
+
aggregate<T>(collection: Path, query?: Record<string, unknown>): Promise<ApiResponse<T>>;
|
|
17
|
+
patch<T>(collection: Path, key: ApiId, query?: Record<string, unknown>): Promise<ApiResponse<T>>;
|
|
18
|
+
post<T>(collection: Path, data: AnyObject, query?: Record<string, unknown>): Promise<ApiResponse<T>>;
|
|
19
|
+
delete<T>(collection: Path, key?: ApiId): Promise<ApiResponse<T>>;
|
|
20
|
+
put<T>(collection: Path, key?: ApiId, payload?: AnyObject, config?: ApiAdapterRequestConfig): Promise<ApiResponse<T>>;
|
|
21
|
+
upload<T>(path: string | undefined, data: FormData): Promise<ApiResponse<T>>;
|
|
22
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { aggregate, createItem, deleteItem, readItem, readItems, updateItem, uploadFiles } from '@directus/sdk';
|
|
2
|
+
import { ApiAdapterAbstract } from '@tanglemedia/svelte-starter-core';
|
|
3
|
+
export class DirectusApiAdapter extends ApiAdapterAbstract {
|
|
4
|
+
config;
|
|
5
|
+
directus;
|
|
6
|
+
constructor(config, directus) {
|
|
7
|
+
super(config);
|
|
8
|
+
this.config = config;
|
|
9
|
+
this.directus = directus;
|
|
10
|
+
}
|
|
11
|
+
getAdapterClient() {
|
|
12
|
+
return this.directus;
|
|
13
|
+
}
|
|
14
|
+
normalizeMeta(payload) {
|
|
15
|
+
// todo: Transform or attempt to get meta information
|
|
16
|
+
// consider pulling count based on existing filters in order to determine pagination
|
|
17
|
+
return payload;
|
|
18
|
+
}
|
|
19
|
+
async transformResponse(res, status = 200) {
|
|
20
|
+
// const payload = (await res.json()) as T;
|
|
21
|
+
return {
|
|
22
|
+
body: await this.envelopeResponse(res.data || res
|
|
23
|
+
// this.normalizeMeta(payload as AnyObject) as M
|
|
24
|
+
),
|
|
25
|
+
status: status,
|
|
26
|
+
statusText: 'OK',
|
|
27
|
+
headers: {},
|
|
28
|
+
adapterResponse: null
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
getConfig(configuration) {
|
|
32
|
+
const client = this.directus;
|
|
33
|
+
// Add the 'configuration' property to the returned object
|
|
34
|
+
const config = {
|
|
35
|
+
...this.config,
|
|
36
|
+
configuration: configuration ?? {} // Use the provided configuration or an empty object if not provided
|
|
37
|
+
};
|
|
38
|
+
return Object.assign(client, config);
|
|
39
|
+
}
|
|
40
|
+
async request(method, url, query) {
|
|
41
|
+
try {
|
|
42
|
+
const response = await this.directus.request(() => {
|
|
43
|
+
const params = JSON.stringify(query);
|
|
44
|
+
return {
|
|
45
|
+
path: `${url}?${params}`,
|
|
46
|
+
method: method
|
|
47
|
+
};
|
|
48
|
+
});
|
|
49
|
+
return response;
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
console.error(`Error request:`, error);
|
|
53
|
+
throw error;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
async find(collection, query) {
|
|
57
|
+
const response = await this.directus.request(readItems(collection, query));
|
|
58
|
+
return this.transformResponse(response);
|
|
59
|
+
}
|
|
60
|
+
async findOne(collection, key, query) {
|
|
61
|
+
const response = await this.directus.request(readItem(collection, key, query));
|
|
62
|
+
return this.transformResponse(response);
|
|
63
|
+
}
|
|
64
|
+
async aggregate(collection, query) {
|
|
65
|
+
const response = await this.directus.request(aggregate(collection, {
|
|
66
|
+
aggregate: {
|
|
67
|
+
count: 'id'
|
|
68
|
+
},
|
|
69
|
+
query
|
|
70
|
+
}));
|
|
71
|
+
return this.transformResponse(response);
|
|
72
|
+
}
|
|
73
|
+
async patch(collection, key, query) {
|
|
74
|
+
const response = await this.directus.request(updateItem(collection, key, query));
|
|
75
|
+
return this.transformResponse(response);
|
|
76
|
+
}
|
|
77
|
+
async post(collection, data, query) {
|
|
78
|
+
const response = await this.directus.request(createItem(collection, data, query));
|
|
79
|
+
return this.transformResponse(response, 201);
|
|
80
|
+
}
|
|
81
|
+
async delete(collection, key) {
|
|
82
|
+
const response = await this.directus.request(deleteItem(collection, key));
|
|
83
|
+
return this.transformResponse(response, 202);
|
|
84
|
+
}
|
|
85
|
+
async put(collection, key, payload, config) {
|
|
86
|
+
if (key) {
|
|
87
|
+
const response = await this.directus.request(updateItem(collection, key, payload));
|
|
88
|
+
return this.transformResponse(response, 201);
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
console.error(`Error updating all ${collection}: no key specified`);
|
|
92
|
+
throw new Error('No key specified');
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
96
|
+
async upload(path = '', data) {
|
|
97
|
+
const response = await this.directus.request(uploadFiles(data));
|
|
98
|
+
return this.transformResponse(response, 201);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
@@ -1,50 +1,11 @@
|
|
|
1
|
-
import { authentication, createDirectus, readMe, refresh, rest, updateMe
|
|
2
|
-
import {
|
|
3
|
-
import cookie from 'js-cookie';
|
|
4
|
-
const localDirectusStorage = (storageKey = 'access_token_directus') => {
|
|
5
|
-
if (!BROWSER || !window.localStorage) {
|
|
6
|
-
console.warn('Defaulting to memory storage');
|
|
7
|
-
return memoryStorage();
|
|
8
|
-
}
|
|
9
|
-
const storage = window.localStorage;
|
|
10
|
-
return {
|
|
11
|
-
get: async () => {
|
|
12
|
-
const data = storage.getItem(storageKey);
|
|
13
|
-
return data ? JSON.parse(data) : null;
|
|
14
|
-
},
|
|
15
|
-
set: async (value) => {
|
|
16
|
-
if (value === null) {
|
|
17
|
-
storage.removeItem(storageKey);
|
|
18
|
-
}
|
|
19
|
-
else {
|
|
20
|
-
storage.setItem(storageKey, JSON.stringify(value));
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
};
|
|
24
|
-
};
|
|
25
|
-
// const cookieDirectusStorage = (cookieKey: string = 'access_token_directus') => {
|
|
26
|
-
// return {
|
|
27
|
-
// get: async (): Promise<AuthenticationData | null> => {
|
|
28
|
-
// const data = JSON.parse(cookie.get(cookieKey)) || '{}';
|
|
29
|
-
// return data ? (data as AuthenticationData) : null;
|
|
30
|
-
// },
|
|
31
|
-
// set: async (value: AuthenticationData | null): Promise<void> => {
|
|
32
|
-
// cookie.set(cookieKey, JSON.stringify(value))
|
|
33
|
-
// }
|
|
34
|
-
// };
|
|
35
|
-
// }
|
|
1
|
+
import { authentication, createDirectus, readMe, refresh, rest, updateMe } from '@directus/sdk';
|
|
2
|
+
import { AuthLocalStorage } from './storage/local-storage';
|
|
36
3
|
const authenticationConfig = {
|
|
37
|
-
storage:
|
|
4
|
+
storage: new AuthLocalStorage(),
|
|
38
5
|
autoRefresh: true,
|
|
39
6
|
msRefreshBeforeExpires: 30000,
|
|
40
7
|
credentials: 'include'
|
|
41
8
|
};
|
|
42
|
-
// const authenticationCookieConfig: AuthenticationConfig = {
|
|
43
|
-
// storage: cookieDirectusStorage(),
|
|
44
|
-
// autoRefresh: true,
|
|
45
|
-
// msRefreshBeforeExpires: 30000,
|
|
46
|
-
// credentials: 'include'
|
|
47
|
-
// };
|
|
48
9
|
class ApiAuthDirectus {
|
|
49
10
|
baseURL;
|
|
50
11
|
storageMode;
|
|
@@ -1,40 +1,7 @@
|
|
|
1
|
-
import { aggregate, authentication, createDirectus, createItem, deleteItem, readItem, readItems, rest, updateItem, uploadFiles
|
|
2
|
-
import {
|
|
3
|
-
import cookie from 'js-cookie';
|
|
4
|
-
const localDirectusStorage = (storageKey = 'access_token_directus') => {
|
|
5
|
-
if (!browser || !window.localStorage) {
|
|
6
|
-
console.warn('Defaulting to memory storage');
|
|
7
|
-
return memoryStorage();
|
|
8
|
-
}
|
|
9
|
-
const storage = window.localStorage;
|
|
10
|
-
return {
|
|
11
|
-
get: async () => {
|
|
12
|
-
const data = storage.getItem(storageKey);
|
|
13
|
-
return data ? JSON.parse(data) : null;
|
|
14
|
-
},
|
|
15
|
-
set: async (value) => {
|
|
16
|
-
if (value === null) {
|
|
17
|
-
storage.removeItem(storageKey);
|
|
18
|
-
}
|
|
19
|
-
else {
|
|
20
|
-
storage.setItem(storageKey, JSON.stringify(value));
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
};
|
|
24
|
-
};
|
|
25
|
-
// const cookieDirectusStorage = (cookieKey: string = 'access_token_directus') => {
|
|
26
|
-
// return {
|
|
27
|
-
// get: async (): Promise<AuthenticationData | null> => {
|
|
28
|
-
// const data = JSON.parse(cookie.get(cookieKey)) || '{}';
|
|
29
|
-
// return data ? (data as AuthenticationData) : null;
|
|
30
|
-
// },
|
|
31
|
-
// set: async (value: AuthenticationData | null): Promise<void> => {
|
|
32
|
-
// cookie.set(cookieKey, JSON.stringify(value))
|
|
33
|
-
// }
|
|
34
|
-
// };
|
|
35
|
-
// }
|
|
1
|
+
import { aggregate, authentication, createDirectus, createItem, deleteItem, readItem, readItems, rest, updateItem, uploadFiles } from '@directus/sdk';
|
|
2
|
+
import { AuthLocalStorage } from './storage/local-storage';
|
|
36
3
|
const authenticationConfig = {
|
|
37
|
-
storage:
|
|
4
|
+
storage: new AuthLocalStorage(),
|
|
38
5
|
autoRefresh: true,
|
|
39
6
|
msRefreshBeforeExpires: 30000,
|
|
40
7
|
credentials: 'include'
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
export * from './static';
|
|
2
|
-
export * from './client';
|
|
3
|
-
export * from './auth';
|
|
4
|
-
export * from './load-directus-api-povider';
|
|
1
|
+
export * from './static.deprecated';
|
|
2
|
+
export * from './client.deprecated';
|
|
3
|
+
export * from './auth.deprecated';
|
|
4
|
+
export * from './load-directus-api-povider.deprecated';
|
|
5
|
+
export * from './provider/api-adapter.provider';
|
|
6
|
+
export * from './provider/directus.factory';
|
|
7
|
+
export * from './services/auth';
|
|
8
|
+
export * from './services/register';
|
|
9
|
+
export type * from './types';
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
export * from './static';
|
|
2
|
-
export * from './client';
|
|
3
|
-
export * from './auth';
|
|
4
|
-
export * from './load-directus-api-povider';
|
|
1
|
+
export * from './static.deprecated';
|
|
2
|
+
export * from './client.deprecated';
|
|
3
|
+
export * from './auth.deprecated';
|
|
4
|
+
export * from './load-directus-api-povider.deprecated';
|
|
5
|
+
export * from './provider/api-adapter.provider';
|
|
6
|
+
export * from './provider/directus.factory';
|
|
7
|
+
export * from './services/auth';
|
|
8
|
+
export * from './services/register';
|
|
@@ -2,9 +2,10 @@ import { type ApiAdapterProviderInterface, type ApiAdapterInterface, type ApiAda
|
|
|
2
2
|
declare class LoadDirectusApiProvider implements ApiAdapterProviderInterface {
|
|
3
3
|
private readonly configLoader;
|
|
4
4
|
private storageMode;
|
|
5
|
+
private key?;
|
|
5
6
|
constructor(configLoader: () => Promise<Config<{
|
|
6
7
|
api?: ApiConfigSchema;
|
|
7
|
-
}>>, storageMode?: 'json' | 'cookie');
|
|
8
|
-
loadAdapter(
|
|
8
|
+
}>>, storageMode?: 'json' | 'cookie', key?: string | undefined);
|
|
9
|
+
loadAdapter(): Promise<ApiAdapterInterface<BaseApiAdapterConfig, ApiAdapterRequestConfig, string>>;
|
|
9
10
|
}
|
|
10
11
|
export { LoadDirectusApiProvider };
|
|
@@ -1,32 +1,39 @@
|
|
|
1
|
-
import { ApiClientDirectus } from './client';
|
|
2
|
-
import { ApiStaticDirectus } from './static';
|
|
1
|
+
import { ApiClientDirectus } from './client.deprecated';
|
|
2
|
+
import { ApiStaticDirectus } from './static.deprecated';
|
|
3
|
+
import { ApiAuthDirectus } from './auth.deprecated';
|
|
3
4
|
import {} from '@tanglemedia/svelte-starter-core';
|
|
4
5
|
class LoadDirectusApiProvider {
|
|
5
6
|
configLoader;
|
|
6
7
|
storageMode;
|
|
7
|
-
|
|
8
|
+
key;
|
|
9
|
+
constructor(configLoader, storageMode = 'json', key) {
|
|
8
10
|
this.configLoader = configLoader;
|
|
9
11
|
this.storageMode = storageMode;
|
|
12
|
+
this.key = key;
|
|
10
13
|
}
|
|
11
|
-
async loadAdapter(
|
|
14
|
+
async loadAdapter() {
|
|
12
15
|
const conf = await this.configLoader();
|
|
13
16
|
const config = (p) => conf.config(p);
|
|
14
|
-
const apiAdapterKey = key || config('api.default');
|
|
15
|
-
const directusType =
|
|
17
|
+
const apiAdapterKey = this.key === 'auth' ? 'auth' : this.key || config('api.default');
|
|
18
|
+
const directusType = apiAdapterKey === 'auth'
|
|
19
|
+
? this.key
|
|
20
|
+
: config(`api.adapters.${apiAdapterKey}.type`) || 'client';
|
|
16
21
|
console.log(`api.apaters.${apiAdapterKey}.type`);
|
|
17
22
|
if (!apiAdapterKey) {
|
|
18
23
|
throw new Error(`No adapter key specified`);
|
|
19
24
|
}
|
|
20
|
-
console.log(apiAdapterKey);
|
|
21
|
-
console.log(directusType);
|
|
22
25
|
if (directusType) {
|
|
23
26
|
if (directusType === 'client') {
|
|
24
27
|
const protocol = config(`api.adapters.${apiAdapterKey}.protocol`) || '';
|
|
25
28
|
const host = config(`api.adapters.${apiAdapterKey}.host`) || '';
|
|
26
29
|
return new ApiClientDirectus(`${protocol}://${host}`.trim(), this.storageMode);
|
|
27
30
|
}
|
|
31
|
+
else if (directusType === 'auth') {
|
|
32
|
+
const protocol = config(`api.adapters.${apiAdapterKey}.protocol`) || '';
|
|
33
|
+
const host = config(`api.adapters.${apiAdapterKey}.host`) || '';
|
|
34
|
+
return new ApiAuthDirectus(`${protocol}://${host}`.trim(), this.storageMode);
|
|
35
|
+
}
|
|
28
36
|
else {
|
|
29
|
-
console.log('static');
|
|
30
37
|
const protocol = config(`api.adapters.${apiAdapterKey}.protocol`) || '';
|
|
31
38
|
const host = config(`api.adapters.${apiAdapterKey}.host`) || '';
|
|
32
39
|
const accessToken = config(`api.adapters.${apiAdapterKey}.access_token`) || '';
|
|
@@ -34,12 +41,10 @@ class LoadDirectusApiProvider {
|
|
|
34
41
|
}
|
|
35
42
|
}
|
|
36
43
|
else {
|
|
37
|
-
console.log('client');
|
|
38
44
|
const protocol = config(`api.adapters.${apiAdapterKey}.protocol`) || '';
|
|
39
45
|
const host = config(`api.adapters.${apiAdapterKey}.host`) || '';
|
|
40
46
|
return new ApiClientDirectus(`${protocol}://${host}`.trim(), this.storageMode);
|
|
41
47
|
}
|
|
42
|
-
console.log('This should be unreachable');
|
|
43
48
|
}
|
|
44
49
|
}
|
|
45
50
|
export { LoadDirectusApiProvider };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { AdapterProviderRegister, ApiAdapterInterface, ApiAdapterProviderInterface } from '@tanglemedia/svelte-starter-core';
|
|
2
|
+
import type { DirectusConfig } from '../types/adapter.types';
|
|
3
|
+
export declare class DirectusApiProvider implements ApiAdapterProviderInterface<DirectusConfig> {
|
|
4
|
+
loadAdapter(key?: string, config?: DirectusConfig): Promise<ApiAdapterInterface<DirectusConfig>>;
|
|
5
|
+
createDirectusClient<T extends object>(config: DirectusConfig): import("@directus/sdk/dist/client-pzVdw9Ub").D<T> & import("@directus/sdk/dist/login-M53C1LeV").e<T>;
|
|
6
|
+
}
|
|
7
|
+
export declare const registerDirectusProvider: () => AdapterProviderRegister;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { DirectusApiAdapter } from '../adapter/api-adapter';
|
|
2
|
+
import { createDirectusClientFactory } from './directus.factory';
|
|
3
|
+
export class DirectusApiProvider {
|
|
4
|
+
async loadAdapter(key, config) {
|
|
5
|
+
if (!config) {
|
|
6
|
+
throw new Error('Missing adapter configuration');
|
|
7
|
+
}
|
|
8
|
+
const client = this.createDirectusClient(config);
|
|
9
|
+
return new DirectusApiAdapter(config, client);
|
|
10
|
+
}
|
|
11
|
+
createDirectusClient(config) {
|
|
12
|
+
// todo: Determine if we can overwrite the fetch globals during runtime
|
|
13
|
+
return createDirectusClientFactory(config);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export const registerDirectusProvider = () => {
|
|
17
|
+
return {
|
|
18
|
+
name: 'directus',
|
|
19
|
+
provider: new DirectusApiProvider()
|
|
20
|
+
};
|
|
21
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type ClientOptions } from '@directus/sdk';
|
|
2
|
+
import type { DirectusConfig } from '../types/adapter.types';
|
|
3
|
+
/**
|
|
4
|
+
* Creates a directus client based on the application configuration
|
|
5
|
+
*/
|
|
6
|
+
export declare const createDirectusClientFactory: <T extends object>({ baseUrl, configuration: { protocol, host, auth, staticToken: token, graphql: gql, rest: rs } }: DirectusConfig, options?: ClientOptions) => import("@directus/sdk/dist/client-pzVdw9Ub").D<T> & import("@directus/sdk/dist/login-M53C1LeV").e<T>;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { authentication, createDirectus, graphql, rest, staticToken } from '@directus/sdk';
|
|
2
|
+
import { AuthLocalStorage } from '../storage/local-storage';
|
|
3
|
+
const authConfigDefaults = {
|
|
4
|
+
autoRefresh: false,
|
|
5
|
+
msRefreshBeforeExpires: 0,
|
|
6
|
+
credentials: 'include'
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Creates a directus client based on the application configuration
|
|
10
|
+
*/
|
|
11
|
+
export const createDirectusClientFactory = ({ baseUrl, configuration: { protocol = 'https', host, auth, staticToken: token, graphql: gql, rest: rs } }, options) => {
|
|
12
|
+
let url = baseUrl;
|
|
13
|
+
if (host) {
|
|
14
|
+
url = `${protocol}://${host}`;
|
|
15
|
+
}
|
|
16
|
+
if (!url) {
|
|
17
|
+
throw new Error('Unable to resolve base url for directus client');
|
|
18
|
+
}
|
|
19
|
+
// if there is going to be authentication and no rest config has been specified.
|
|
20
|
+
// set the correct defaults
|
|
21
|
+
if (!rs && auth) {
|
|
22
|
+
rs = { credentials: 'include' };
|
|
23
|
+
}
|
|
24
|
+
let client = createDirectus(url, options).with(rest(rs));
|
|
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));
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (token) {
|
|
40
|
+
client = client.with(staticToken(token));
|
|
41
|
+
}
|
|
42
|
+
if (gql) {
|
|
43
|
+
client = client.with(graphql(gql));
|
|
44
|
+
}
|
|
45
|
+
return client;
|
|
46
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { AuthenticationClient } from '@directus/sdk';
|
|
2
|
+
import type { DirectusClient, RestClient } from '@directus/sdk';
|
|
3
|
+
import { ServiceAdapterAbstract } from '@tanglemedia/svelte-starter-core';
|
|
4
|
+
declare class DirectusAuthServices extends ServiceAdapterAbstract {
|
|
5
|
+
getDirectus(): Promise<DirectusClient<object> & RestClient<object> & AuthenticationClient<object>>;
|
|
6
|
+
refresh<T>(): Promise<T>;
|
|
7
|
+
getToken(): Promise<string | null>;
|
|
8
|
+
login(email: string, password: string): Promise<import("@directus/sdk/dist/login-M53C1LeV").a>;
|
|
9
|
+
logout(): Promise<void>;
|
|
10
|
+
getCurrentUser<T>(fields?: string[] | null): Promise<T>;
|
|
11
|
+
updateCurrentUser<T>(data: object): Promise<T>;
|
|
12
|
+
}
|
|
13
|
+
export { DirectusAuthServices };
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { readMe, refresh, updateMe } from '@directus/sdk';
|
|
2
|
+
import { ServiceAdapterAbstract } from '@tanglemedia/svelte-starter-core';
|
|
3
|
+
class DirectusAuthServices extends ServiceAdapterAbstract {
|
|
4
|
+
getDirectus() {
|
|
5
|
+
return this.getAdapterClient();
|
|
6
|
+
}
|
|
7
|
+
// public getConfig<T extends object>(): DirectusClient<T> & RestClient<object> {
|
|
8
|
+
// return this.directus as DirectusClient<T> & RestClient<object>;
|
|
9
|
+
// }
|
|
10
|
+
async refresh() {
|
|
11
|
+
try {
|
|
12
|
+
const response = await (await this.getDirectus()).request(refresh());
|
|
13
|
+
return response;
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
console.error(`Error refreshing :`, error);
|
|
17
|
+
throw error;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
async getToken() {
|
|
21
|
+
try {
|
|
22
|
+
const response = await (await this.getDirectus()).getToken();
|
|
23
|
+
return response;
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
console.error(`Error refreshing :`, error);
|
|
27
|
+
throw error;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
async login(email, password) {
|
|
31
|
+
try {
|
|
32
|
+
const response = await (await this.getDirectus()).login(email, password);
|
|
33
|
+
return response;
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
console.error(`Error login :`, error);
|
|
37
|
+
throw error;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
async logout() {
|
|
41
|
+
try {
|
|
42
|
+
const response = await (await this.getDirectus()).logout();
|
|
43
|
+
return response;
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
console.error(`Error refreshing :`, error);
|
|
47
|
+
throw error;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
async getCurrentUser(fields = null) {
|
|
51
|
+
try {
|
|
52
|
+
if (!this.getToken())
|
|
53
|
+
await this.refresh();
|
|
54
|
+
const response = await (await this.getDirectus()).request(readMe({ fields: fields ? fields : ['*', 'roles.*'] }));
|
|
55
|
+
return response;
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
console.error(`Error refreshing :`, error);
|
|
59
|
+
throw error;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
async updateCurrentUser(data) {
|
|
63
|
+
try {
|
|
64
|
+
if (!this.getToken())
|
|
65
|
+
await this.refresh();
|
|
66
|
+
const response = await (await this.getDirectus()).request(updateMe(data));
|
|
67
|
+
return response;
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
console.error(`Error refreshing :`, error);
|
|
71
|
+
throw error;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
export { DirectusAuthServices };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { AuthenticationClient, DirectusClient, RestClient } from '@directus/sdk';
|
|
2
|
+
import { ServiceAdapterAbstract } from '@tanglemedia/svelte-starter-core';
|
|
3
|
+
declare class DirectusStaticServices extends ServiceAdapterAbstract {
|
|
4
|
+
getDirectus(): Promise<DirectusClient<object> & RestClient<object> & AuthenticationClient<object>>;
|
|
5
|
+
createUser(data: {
|
|
6
|
+
email: string;
|
|
7
|
+
password: string;
|
|
8
|
+
first_name: string;
|
|
9
|
+
last_name: string;
|
|
10
|
+
role: string;
|
|
11
|
+
} | object): Promise<{
|
|
12
|
+
message: string;
|
|
13
|
+
type: string;
|
|
14
|
+
}>;
|
|
15
|
+
}
|
|
16
|
+
export { DirectusStaticServices };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { createUser } from '@directus/sdk';
|
|
2
|
+
import { ServiceAdapterAbstract } from '@tanglemedia/svelte-starter-core';
|
|
3
|
+
class DirectusStaticServices extends ServiceAdapterAbstract {
|
|
4
|
+
getDirectus() {
|
|
5
|
+
return this.getAdapterClient();
|
|
6
|
+
}
|
|
7
|
+
async createUser(data) {
|
|
8
|
+
try {
|
|
9
|
+
await (await this.getDirectus()).request(createUser(data));
|
|
10
|
+
return { message: 'You have successfully registered your account', type: 'success' };
|
|
11
|
+
}
|
|
12
|
+
catch (error) {
|
|
13
|
+
console.error(`Error creating a user:`, error);
|
|
14
|
+
throw error;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export { DirectusStaticServices };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { BaseApiAdapterConfig } from '@tanglemedia/svelte-starter-core';
|
|
2
|
+
type BaseApiMethods = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
3
|
+
declare class ApiStaticDirectus {
|
|
4
|
+
private baseURL;
|
|
5
|
+
private directusAccessToken;
|
|
6
|
+
private directus;
|
|
7
|
+
constructor(baseURL: string, directusAccessToken: string);
|
|
8
|
+
private init;
|
|
9
|
+
getConfig<T extends object>(configuration?: unknown): BaseApiAdapterConfig;
|
|
10
|
+
request<T>(method: BaseApiMethods, url: string, query?: Record<string, unknown>): Promise<T>;
|
|
11
|
+
createUser(data: {
|
|
12
|
+
email: string;
|
|
13
|
+
password: string;
|
|
14
|
+
first_name: string;
|
|
15
|
+
last_name: string;
|
|
16
|
+
role: string;
|
|
17
|
+
} | object): Promise<{
|
|
18
|
+
message: string;
|
|
19
|
+
type: string;
|
|
20
|
+
}>;
|
|
21
|
+
}
|
|
22
|
+
export { ApiStaticDirectus };
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { createDirectus, createUser, rest, staticToken } 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, query) {
|
|
32
|
+
try {
|
|
33
|
+
const response = await this.directus.request(() => {
|
|
34
|
+
const params = JSON.stringify(query);
|
|
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
|
+
}
|
|
58
|
+
export { ApiStaticDirectus };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { AuthenticationStorage, AuthenticationData } from '@directus/sdk';
|
|
2
|
+
export declare class AuthLocalStorage implements AuthenticationStorage {
|
|
3
|
+
#private;
|
|
4
|
+
private readonly storageKey;
|
|
5
|
+
constructor(storageKey?: string);
|
|
6
|
+
get key(): string;
|
|
7
|
+
get(): Promise<AuthenticationData | null>;
|
|
8
|
+
set(value: AuthenticationData | null): Promise<void>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { BROWSER } from 'esm-env';
|
|
2
|
+
import { LocalStoragePolyFill } from '@tanglemedia/svelte-starter-core';
|
|
3
|
+
const loadStorage = () => {
|
|
4
|
+
return BROWSER && window && window.localStorage
|
|
5
|
+
? window.localStorage
|
|
6
|
+
: new LocalStoragePolyFill();
|
|
7
|
+
};
|
|
8
|
+
export class AuthLocalStorage {
|
|
9
|
+
storageKey;
|
|
10
|
+
#storage = loadStorage();
|
|
11
|
+
constructor(storageKey = 'access_token_directus') {
|
|
12
|
+
this.storageKey = storageKey;
|
|
13
|
+
}
|
|
14
|
+
get key() {
|
|
15
|
+
return this.storageKey;
|
|
16
|
+
}
|
|
17
|
+
// get: () => a | Promise<a | null> | null;
|
|
18
|
+
async get() {
|
|
19
|
+
const data = this.#storage.getItem(this.key);
|
|
20
|
+
return data ? JSON.parse(data) : null;
|
|
21
|
+
}
|
|
22
|
+
async set(value) {
|
|
23
|
+
if (value === null) {
|
|
24
|
+
this.#storage.removeItem(this.key);
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
this.#storage.setItem(this.key, JSON.stringify(value));
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|