@svadmin/strapi 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/strapi",
3
+ "version": "0.3.0",
4
+ "description": "Strapi v4/v5 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,144 @@
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 mapOperator(operator: string): string {
8
+ switch (operator) {
9
+ case 'eq': return '$eq';
10
+ case 'ne': return '$ne';
11
+ case 'lt': return '$lt';
12
+ case 'gt': return '$gt';
13
+ case 'lte': return '$lte';
14
+ case 'gte': return '$gte';
15
+ case 'contains': return '$containsi'; // case-insensitive contains
16
+ case 'in': return '$in';
17
+ case 'null': return '$null';
18
+ default: return '$eq';
19
+ }
20
+ }
21
+
22
+ function generateQueryParams(
23
+ pagination?: { current?: number; pageSize?: number; mode?: string },
24
+ sorters?: Sort[],
25
+ filters?: Filter[]
26
+ ): string {
27
+ const params = new URLSearchParams();
28
+
29
+ if (pagination && pagination.mode !== 'off') {
30
+ if (pagination.current) params.set('pagination[page]', String(pagination.current));
31
+ if (pagination.pageSize) params.set('pagination[pageSize]', String(pagination.pageSize));
32
+ }
33
+
34
+ if (sorters && sorters.length > 0) {
35
+ sorters.forEach((sort, index) => {
36
+ params.append(`sort[${index}]`, `${sort.field}:${sort.order}`);
37
+ });
38
+ }
39
+
40
+ if (filters && filters.length > 0) {
41
+ filters.forEach((f, index) => {
42
+ if ('field' in f) {
43
+ params.append(`filters[${f.field}][${mapOperator(f.operator as string)}]`, String(f.value));
44
+ }
45
+ });
46
+ }
47
+
48
+ return params.toString();
49
+ }
50
+
51
+ export function createStrapiDataProvider(apiUrl: string, httpClient: typeof fetch = fetch): DataProvider {
52
+ return {
53
+ getApiUrl: () => apiUrl,
54
+
55
+ async getList<T>({ resource, pagination, sorters, filters, meta }: GetListParams): Promise<GetListResult<T>> {
56
+ const url = new URL(`${apiUrl}/${resource}`);
57
+ url.search = generateQueryParams(pagination, sorters, filters);
58
+ if (meta?.populate) url.searchParams.append('populate', String(meta.populate));
59
+
60
+ const response = await httpClient(url.toString(), {
61
+ headers: {
62
+ 'Content-Type': 'application/json',
63
+ },
64
+ });
65
+
66
+ if (!response.ok) throw new Error(`HTTP error: ${response.status}`);
67
+ const data = await response.json();
68
+
69
+ return {
70
+ data: data.data || [],
71
+ total: data.meta?.pagination?.total || data.data?.length || 0,
72
+ };
73
+ },
74
+
75
+ async getOne<T>({ resource, id, meta }: GetOneParams): Promise<GetOneResult<T>> {
76
+ const url = new URL(`${apiUrl}/${resource}/${id}`);
77
+ if (meta?.populate) url.searchParams.append('populate', String(meta.populate));
78
+
79
+ const response = await httpClient(url.toString());
80
+ if (!response.ok) throw new Error(`HTTP error: ${response.status}`);
81
+ const data = await response.json();
82
+ return { data: data.data || data };
83
+ },
84
+
85
+ async create<T>({ resource, variables }: CreateParams): Promise<CreateResult<T>> {
86
+ const response = await httpClient(`${apiUrl}/${resource}`, {
87
+ method: 'POST',
88
+ headers: { 'Content-Type': 'application/json' },
89
+ body: JSON.stringify({ data: variables }), // Strapi wraps payload in 'data'
90
+ });
91
+ if (!response.ok) throw new Error(`HTTP error: ${response.status}`);
92
+ const result = await response.json();
93
+ return { data: result.data || result };
94
+ },
95
+
96
+ async update<T>({ resource, id, variables }: UpdateParams): Promise<UpdateResult<T>> {
97
+ const response = await httpClient(`${apiUrl}/${resource}/${id}`, {
98
+ method: 'PUT',
99
+ headers: { 'Content-Type': 'application/json' },
100
+ body: JSON.stringify({ data: variables }),
101
+ });
102
+ if (!response.ok) throw new Error(`HTTP error: ${response.status}`);
103
+ const result = await response.json();
104
+ return { data: result.data || result };
105
+ },
106
+
107
+ async deleteOne<T>({ resource, id }: DeleteParams): Promise<DeleteResult<T>> {
108
+ const response = await httpClient(`${apiUrl}/${resource}/${id}`, { method: 'DELETE' });
109
+ if (!response.ok) throw new Error(`HTTP error: ${response.status}`);
110
+ const result = await response.json();
111
+ return { data: result.data || result };
112
+ },
113
+
114
+ async getMany<T>({ resource, ids, meta }: GetManyParams): Promise<GetManyResult<T>> {
115
+ const url = new URL(`${apiUrl}/${resource}`);
116
+ ids.forEach((id: string | number, index: number) => url.searchParams.append(`filters[id][$in][${index}]`, String(id)));
117
+ if (meta?.populate) url.searchParams.append('populate', String(meta.populate));
118
+
119
+ const response = await httpClient(url.toString());
120
+ if (!response.ok) throw new Error(`HTTP error: ${response.status}`);
121
+ const result = await response.json();
122
+ return { data: result.data || [] };
123
+ },
124
+
125
+ async custom<T>({ url, method, payload, headers, query }: CustomParams): Promise<CustomResult<T>> {
126
+ let requestUrl = url;
127
+ if (query) {
128
+ const params = new URLSearchParams();
129
+ Object.entries(query).forEach(([k, v]) => params.append(k, String(v)));
130
+ requestUrl = `${url}?${params.toString()}`;
131
+ }
132
+
133
+ const response = await httpClient(requestUrl, {
134
+ method: method.toUpperCase(),
135
+ headers: { 'Content-Type': 'application/json', ...headers },
136
+ body: payload ? JSON.stringify(payload) : undefined,
137
+ });
138
+
139
+ if (!response.ok) throw new Error(`Custom request failed: ${response.status}`);
140
+ const data = await response.json();
141
+ return { data: data as T };
142
+ },
143
+ };
144
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export { createStrapiDataProvider } from './data-provider';