@svadmin/directus 0.3.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/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "@svadmin/directus",
3
+ "version": "0.3.0",
4
+ "description": "Directus DataProvider for svadmin",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "files": ["src/**/*"],
8
+ "main": "src/index.ts",
9
+ "types": "src/index.ts",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./src/index.ts",
13
+ "default": "./src/index.ts"
14
+ }
15
+ },
16
+ "peerDependencies": {
17
+ "@svadmin/core": "^0.3.0"
18
+ },
19
+ "license": "MIT"
20
+ }
@@ -0,0 +1,80 @@
1
+ import type {
2
+ DataProvider, GetListParams, GetListResult, GetOneParams, GetOneResult,
3
+ CreateParams, CreateResult, UpdateParams, UpdateResult, DeleteParams, DeleteResult,
4
+ GetManyParams, GetManyResult, CustomParams, CustomResult, Sort, Filter
5
+ } from '@svadmin/core';
6
+
7
+ function buildDirectusFilter(filters?: Filter[]): Record<string, unknown> {
8
+ if (!filters?.length) return {};
9
+ const filter: Record<string, unknown> = {};
10
+ const opMap: Record<string, string> = { eq: '_eq', ne: '_neq', lt: '_lt', gt: '_gt', lte: '_lte', gte: '_gte', contains: '_contains', in: '_in', null: '_null' };
11
+ for (const f of filters) {
12
+ if ('field' in f) filter[f.field as string] = { [opMap[f.operator as string] ?? '_eq']: f.value };
13
+ }
14
+ return filter;
15
+ }
16
+
17
+ export function createDirectusDataProvider(apiUrl: string, token?: string): DataProvider {
18
+ const headers: Record<string, string> = { 'Content-Type': 'application/json' };
19
+ if (token) headers['Authorization'] = `Bearer ${token}`;
20
+
21
+ async function request<T>(path: string, opts?: RequestInit): Promise<T> {
22
+ const res = await fetch(`${apiUrl}${path}`, { ...opts, headers: { ...headers, ...opts?.headers } });
23
+ if (!res.ok) throw new Error(`Directus error: ${res.status}`);
24
+ return res.json();
25
+ }
26
+
27
+ return {
28
+ getApiUrl: () => apiUrl,
29
+
30
+ async getList<T>({ resource, pagination, sorters, filters, meta }: GetListParams): Promise<GetListResult<T>> {
31
+ const { current = 1, pageSize = 10 } = pagination ?? {};
32
+ const params = new URLSearchParams();
33
+ params.set('limit', String(pageSize));
34
+ params.set('offset', String((current - 1) * pageSize));
35
+ params.set('meta', 'total_count');
36
+ if (meta?.fields) params.set('fields', (meta.fields as string[]).join(','));
37
+ if (sorters?.length) params.set('sort', sorters.map((s: Sort) => `${s.order === 'desc' ? '-' : ''}${s.field}`).join(','));
38
+ const filter = buildDirectusFilter(filters);
39
+ if (Object.keys(filter).length) params.set('filter', JSON.stringify(filter));
40
+
41
+ const data = await request<any>(`/items/${resource}?${params}`);
42
+ return { data: data.data ?? [], total: data.meta?.total_count ?? 0 };
43
+ },
44
+
45
+ async getOne<T>({ resource, id, meta }: GetOneParams): Promise<GetOneResult<T>> {
46
+ const params = meta?.fields ? `?fields=${(meta.fields as string[]).join(',')}` : '';
47
+ const data = await request<any>(`/items/${resource}/${id}${params}`);
48
+ return { data: data.data as T };
49
+ },
50
+
51
+ async create<T>({ resource, variables }: CreateParams): Promise<CreateResult<T>> {
52
+ const data = await request<any>(`/items/${resource}`, { method: 'POST', body: JSON.stringify(variables) });
53
+ return { data: data.data as T };
54
+ },
55
+
56
+ async update<T>({ resource, id, variables }: UpdateParams): Promise<UpdateResult<T>> {
57
+ const data = await request<any>(`/items/${resource}/${id}`, { method: 'PATCH', body: JSON.stringify(variables) });
58
+ return { data: data.data as T };
59
+ },
60
+
61
+ async deleteOne<T>({ resource, id }: DeleteParams): Promise<DeleteResult<T>> {
62
+ await request(`/items/${resource}/${id}`, { method: 'DELETE' });
63
+ return { data: { id } as T };
64
+ },
65
+
66
+ async getMany<T>({ resource, ids, meta }: GetManyParams): Promise<GetManyResult<T>> {
67
+ const params = new URLSearchParams();
68
+ params.set('filter', JSON.stringify({ id: { _in: ids } }));
69
+ if (meta?.fields) params.set('fields', (meta.fields as string[]).join(','));
70
+ const data = await request<any>(`/items/${resource}?${params}`);
71
+ return { data: data.data ?? [] };
72
+ },
73
+
74
+ async custom<T>({ url, method, payload, headers: h }: CustomParams): Promise<CustomResult<T>> {
75
+ const res = await fetch(url, { method: method.toUpperCase(), headers: { ...headers, ...h }, body: payload ? JSON.stringify(payload) : undefined });
76
+ if (!res.ok) throw new Error(`Custom request failed: ${res.status}`);
77
+ return { data: (await res.json()) as T };
78
+ },
79
+ };
80
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export { createDirectusDataProvider } from './data-provider';