@micro-cms/node-adapter 0.0.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/index.d.ts +17 -0
- package/dist/index.js +77 -0
- package/package.json +26 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { CmsModule, DataProvider, Schema } from '@micro-cms/types';
|
|
2
|
+
export interface NodeAdapterConfig {
|
|
3
|
+
apiUrl: string;
|
|
4
|
+
token?: string;
|
|
5
|
+
}
|
|
6
|
+
export declare class NodeDataProvider implements DataProvider {
|
|
7
|
+
private config;
|
|
8
|
+
constructor(config: NodeAdapterConfig);
|
|
9
|
+
private fetchApi;
|
|
10
|
+
introspect(): Promise<Schema>;
|
|
11
|
+
find(entity: string, query?: any): Promise<any>;
|
|
12
|
+
create(entity: string, data: any): Promise<any>;
|
|
13
|
+
update(entity: string, id: any, data: any): Promise<any>;
|
|
14
|
+
delete(entity: string, id: any): Promise<any>;
|
|
15
|
+
}
|
|
16
|
+
declare const nodeAdapterModule: CmsModule;
|
|
17
|
+
export default nodeAdapterModule;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
export class NodeDataProvider {
|
|
2
|
+
constructor(config) {
|
|
3
|
+
this.config = config;
|
|
4
|
+
}
|
|
5
|
+
async fetchApi(path, options = {}) {
|
|
6
|
+
const token = localStorage.getItem('adminToken') || this.config.token;
|
|
7
|
+
const response = await fetch(`${this.config.apiUrl}${path}`, {
|
|
8
|
+
...options,
|
|
9
|
+
headers: {
|
|
10
|
+
'Content-Type': 'application/json',
|
|
11
|
+
'Authorization': `Bearer ${token}`,
|
|
12
|
+
...options.headers,
|
|
13
|
+
},
|
|
14
|
+
});
|
|
15
|
+
if (!response.ok) {
|
|
16
|
+
throw new Error(`API Error: ${response.statusText}`);
|
|
17
|
+
}
|
|
18
|
+
return response.json();
|
|
19
|
+
}
|
|
20
|
+
async introspect() {
|
|
21
|
+
const response = await this.fetchApi('/admin/schema');
|
|
22
|
+
return {
|
|
23
|
+
entities: Object.entries(response.resources).map(([name, def]) => ({
|
|
24
|
+
name,
|
|
25
|
+
fields: def.fields,
|
|
26
|
+
displayField: def.displayField,
|
|
27
|
+
primaryKey: def.primaryKey,
|
|
28
|
+
label: def.label,
|
|
29
|
+
})),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
async find(entity, query) {
|
|
33
|
+
const queryString = query ? '?' + new URLSearchParams(query).toString() : '';
|
|
34
|
+
const response = await this.fetchApi(`/admin/resources/${entity}${queryString}`);
|
|
35
|
+
return response;
|
|
36
|
+
}
|
|
37
|
+
async create(entity, data) {
|
|
38
|
+
return this.fetchApi(`/admin/resources/${entity}`, {
|
|
39
|
+
method: 'POST',
|
|
40
|
+
body: JSON.stringify(data),
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
async update(entity, id, data) {
|
|
44
|
+
return this.fetchApi(`/admin/resources/${entity}/${id}`, {
|
|
45
|
+
method: 'PATCH',
|
|
46
|
+
body: JSON.stringify(data),
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
async delete(entity, id) {
|
|
50
|
+
return this.fetchApi(`/admin/resources/${entity}/${id}`, {
|
|
51
|
+
method: 'DELETE',
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const nodeAdapterModule = {
|
|
56
|
+
manifest: {
|
|
57
|
+
name: '@micro-cms/node-adapter',
|
|
58
|
+
version: '0.0.1',
|
|
59
|
+
provides: ['database-adapter', 'introspection'],
|
|
60
|
+
publishes: {
|
|
61
|
+
'database.schema': 'The remote database schema'
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
async load(context) {
|
|
65
|
+
const config = context.config;
|
|
66
|
+
const provider = new NodeDataProvider(config);
|
|
67
|
+
context.runtime.register('database-adapter', provider);
|
|
68
|
+
try {
|
|
69
|
+
const schema = await provider.introspect();
|
|
70
|
+
context.context.publish('database.schema', schema);
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
console.error('NodeAdapter failed to introspect schema:', error);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
export default nodeAdapterModule;
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@micro-cms/node-adapter",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"files": ["dist"],
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@micro-cms/types": "workspace:*"
|
|
18
|
+
},
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsc",
|
|
21
|
+
"prepare": "pnpm build"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"typescript": "^5.0.0"
|
|
25
|
+
}
|
|
26
|
+
}
|