@micro-cms/node-adapter 1.0.5 → 1.0.8

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.
@@ -0,0 +1,23 @@
1
+ import { DataProvider, Schema, CmsModule } from '@micro-cms/types';
2
+
3
+ interface NodeAdapterConfig {
4
+ apiUrl: string;
5
+ token?: string;
6
+ }
7
+ declare class NodeDataProvider implements DataProvider {
8
+ private config;
9
+ constructor(config: NodeAdapterConfig);
10
+ private fetchApi;
11
+ introspect(): Promise<Schema>;
12
+ find(entity: string, query?: {
13
+ page?: number;
14
+ limit?: number;
15
+ }): Promise<any>;
16
+ findById(entity: string, id: any): Promise<any>;
17
+ create(entity: string, data: any): Promise<any>;
18
+ update(entity: string, id: any, data: any): Promise<any>;
19
+ delete(entity: string, id: any): Promise<any>;
20
+ }
21
+ declare const nodeAdapterModule: CmsModule;
22
+
23
+ export { type NodeAdapterConfig, NodeDataProvider, nodeAdapterModule as default };
package/dist/index.d.ts CHANGED
@@ -1,9 +1,10 @@
1
- import { CmsModule, DataProvider, Schema } from '@micro-cms/types';
2
- export interface NodeAdapterConfig {
1
+ import { DataProvider, Schema, CmsModule } from '@micro-cms/types';
2
+
3
+ interface NodeAdapterConfig {
3
4
  apiUrl: string;
4
5
  token?: string;
5
6
  }
6
- export declare class NodeDataProvider implements DataProvider {
7
+ declare class NodeDataProvider implements DataProvider {
7
8
  private config;
8
9
  constructor(config: NodeAdapterConfig);
9
10
  private fetchApi;
@@ -18,5 +19,5 @@ export declare class NodeDataProvider implements DataProvider {
18
19
  delete(entity: string, id: any): Promise<any>;
19
20
  }
20
21
  declare const nodeAdapterModule: CmsModule;
21
- export default nodeAdapterModule;
22
- //# sourceMappingURL=index.d.ts.map
22
+
23
+ export { type NodeAdapterConfig, NodeDataProvider, nodeAdapterModule as default };
package/dist/index.js CHANGED
@@ -1,87 +1,113 @@
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
- // If the response is already in the Schema format (entities: []), return it
23
- if (response && Array.isArray(response.entities)) {
24
- return response;
25
- }
26
- // Fallback or old format support
27
- if (response && response.resources) {
28
- return {
29
- entities: Object.entries(response.resources).map(([name, def]) => ({
30
- name,
31
- fields: def.fields,
32
- ...def
33
- })),
34
- };
35
- }
36
- return { entities: [] };
37
- }
38
- async find(entity, query) {
39
- const queryString = query ? '?' + new URLSearchParams(query).toString() : '';
40
- const response = await this.fetchApi(`/admin/resources/${entity}${queryString}`);
41
- return response; // Assuming API returns { data: [], total: ..., page: ..., limit: ... }
42
- }
43
- async findById(entity, id) {
44
- return this.fetchApi(`/admin/resources/${entity}/${id}`);
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ NodeDataProvider: () => NodeDataProvider,
24
+ default: () => index_default
25
+ });
26
+ module.exports = __toCommonJS(index_exports);
27
+ var NodeDataProvider = class {
28
+ constructor(config) {
29
+ this.config = config;
30
+ }
31
+ async fetchApi(path, options = {}) {
32
+ const token = localStorage.getItem("adminToken") || this.config.token;
33
+ const response = await fetch(`${this.config.apiUrl}${path}`, {
34
+ ...options,
35
+ headers: {
36
+ "Content-Type": "application/json",
37
+ "Authorization": `Bearer ${token}`,
38
+ ...options.headers
39
+ }
40
+ });
41
+ if (!response.ok) {
42
+ throw new Error(`API Error: ${response.statusText}`);
45
43
  }
46
- async create(entity, data) {
47
- return this.fetchApi(`/admin/resources/${entity}`, {
48
- method: 'POST',
49
- body: JSON.stringify(data),
50
- });
44
+ return response.json();
45
+ }
46
+ async introspect() {
47
+ const response = await this.fetchApi("/admin/schema");
48
+ if (response && Array.isArray(response.entities)) {
49
+ return response;
51
50
  }
52
- async update(entity, id, data) {
53
- return this.fetchApi(`/admin/resources/${entity}/${id}`, {
54
- method: 'PATCH',
55
- body: JSON.stringify(data),
56
- });
51
+ if (response && response.resources) {
52
+ return {
53
+ entities: Object.entries(response.resources).map(([name, def]) => ({
54
+ name,
55
+ fields: def.fields,
56
+ ...def
57
+ }))
58
+ };
57
59
  }
58
- async delete(entity, id) {
59
- return this.fetchApi(`/admin/resources/${entity}/${id}`, {
60
- method: 'DELETE',
61
- });
60
+ return { entities: [] };
61
+ }
62
+ async find(entity, query) {
63
+ const queryString = query ? "?" + new URLSearchParams(query).toString() : "";
64
+ const response = await this.fetchApi(`/admin/resources/${entity}${queryString}`);
65
+ return response;
66
+ }
67
+ async findById(entity, id) {
68
+ return this.fetchApi(`/admin/resources/${entity}/${id}`);
69
+ }
70
+ async create(entity, data) {
71
+ return this.fetchApi(`/admin/resources/${entity}`, {
72
+ method: "POST",
73
+ body: JSON.stringify(data)
74
+ });
75
+ }
76
+ async update(entity, id, data) {
77
+ return this.fetchApi(`/admin/resources/${entity}/${id}`, {
78
+ method: "PATCH",
79
+ body: JSON.stringify(data)
80
+ });
81
+ }
82
+ async delete(entity, id) {
83
+ return this.fetchApi(`/admin/resources/${entity}/${id}`, {
84
+ method: "DELETE"
85
+ });
86
+ }
87
+ };
88
+ var nodeAdapterModule = {
89
+ manifest: {
90
+ name: "@micro-cms/node-adapter",
91
+ version: "0.0.1",
92
+ provides: ["database-adapter", "introspection"],
93
+ publishes: {
94
+ "database.schema": "The remote database schema"
62
95
  }
63
- }
64
- const nodeAdapterModule = {
65
- manifest: {
66
- name: '@micro-cms/node-adapter',
67
- version: '0.0.1',
68
- provides: ['database-adapter', 'introspection'],
69
- publishes: {
70
- 'database.schema': 'The remote database schema'
71
- }
72
- },
73
- async load(context) {
74
- const config = context.config;
75
- const provider = new NodeDataProvider(config);
76
- context.runtime.register('database-adapter', provider);
77
- try {
78
- const schema = await provider.introspect();
79
- context.context.publish('database.schema', schema);
80
- }
81
- catch (error) {
82
- console.error('NodeAdapter failed to introspect schema:', error);
83
- }
96
+ },
97
+ async load(context) {
98
+ const config = context.config;
99
+ const provider = new NodeDataProvider(config);
100
+ context.runtime.register("database-adapter", provider);
101
+ try {
102
+ const schema = await provider.introspect();
103
+ context.context.publish("database.schema", schema);
104
+ } catch (error) {
105
+ console.error("NodeAdapter failed to introspect schema:", error);
84
106
  }
107
+ }
85
108
  };
86
- export default nodeAdapterModule;
87
- //# sourceMappingURL=index.js.map
109
+ var index_default = nodeAdapterModule;
110
+ // Annotate the CommonJS export names for ESM import in node:
111
+ 0 && (module.exports = {
112
+ NodeDataProvider
113
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,88 @@
1
+ // src/index.ts
2
+ var NodeDataProvider = class {
3
+ constructor(config) {
4
+ this.config = config;
5
+ }
6
+ async fetchApi(path, options = {}) {
7
+ const token = localStorage.getItem("adminToken") || this.config.token;
8
+ const response = await fetch(`${this.config.apiUrl}${path}`, {
9
+ ...options,
10
+ headers: {
11
+ "Content-Type": "application/json",
12
+ "Authorization": `Bearer ${token}`,
13
+ ...options.headers
14
+ }
15
+ });
16
+ if (!response.ok) {
17
+ throw new Error(`API Error: ${response.statusText}`);
18
+ }
19
+ return response.json();
20
+ }
21
+ async introspect() {
22
+ const response = await this.fetchApi("/admin/schema");
23
+ if (response && Array.isArray(response.entities)) {
24
+ return response;
25
+ }
26
+ if (response && response.resources) {
27
+ return {
28
+ entities: Object.entries(response.resources).map(([name, def]) => ({
29
+ name,
30
+ fields: def.fields,
31
+ ...def
32
+ }))
33
+ };
34
+ }
35
+ return { entities: [] };
36
+ }
37
+ async find(entity, query) {
38
+ const queryString = query ? "?" + new URLSearchParams(query).toString() : "";
39
+ const response = await this.fetchApi(`/admin/resources/${entity}${queryString}`);
40
+ return response;
41
+ }
42
+ async findById(entity, id) {
43
+ return this.fetchApi(`/admin/resources/${entity}/${id}`);
44
+ }
45
+ async create(entity, data) {
46
+ return this.fetchApi(`/admin/resources/${entity}`, {
47
+ method: "POST",
48
+ body: JSON.stringify(data)
49
+ });
50
+ }
51
+ async update(entity, id, data) {
52
+ return this.fetchApi(`/admin/resources/${entity}/${id}`, {
53
+ method: "PATCH",
54
+ body: JSON.stringify(data)
55
+ });
56
+ }
57
+ async delete(entity, id) {
58
+ return this.fetchApi(`/admin/resources/${entity}/${id}`, {
59
+ method: "DELETE"
60
+ });
61
+ }
62
+ };
63
+ var nodeAdapterModule = {
64
+ manifest: {
65
+ name: "@micro-cms/node-adapter",
66
+ version: "0.0.1",
67
+ provides: ["database-adapter", "introspection"],
68
+ publishes: {
69
+ "database.schema": "The remote database schema"
70
+ }
71
+ },
72
+ async load(context) {
73
+ const config = context.config;
74
+ const provider = new NodeDataProvider(config);
75
+ context.runtime.register("database-adapter", provider);
76
+ try {
77
+ const schema = await provider.introspect();
78
+ context.context.publish("database.schema", schema);
79
+ } catch (error) {
80
+ console.error("NodeAdapter failed to introspect schema:", error);
81
+ }
82
+ }
83
+ };
84
+ var index_default = nodeAdapterModule;
85
+ export {
86
+ NodeDataProvider,
87
+ index_default as default
88
+ };
package/package.json CHANGED
@@ -1,9 +1,8 @@
1
1
  {
2
2
  "name": "@micro-cms/node-adapter",
3
- "version": "1.0.5",
4
- "type": "module",
3
+ "version": "1.0.8",
5
4
  "main": "dist/index.js",
6
- "module": "dist/index.js",
5
+ "module": "dist/index.mjs",
7
6
  "types": "dist/index.d.ts",
8
7
  "files": [
9
8
  "dist"
@@ -11,18 +10,20 @@
11
10
  "exports": {
12
11
  ".": {
13
12
  "types": "./dist/index.d.ts",
14
- "import": "./dist/index.js",
13
+ "import": "./dist/index.mjs",
14
+ "require": "./dist/index.js",
15
15
  "default": "./dist/index.js"
16
16
  }
17
17
  },
18
18
  "dependencies": {
19
- "@micro-cms/types": "1.0.5"
19
+ "@micro-cms/types": "^1.0.8"
20
20
  },
21
21
  "scripts": {
22
- "build": "tsc",
22
+ "build": "tsup src/index.ts --format cjs,esm --dts",
23
23
  "prepare": "pnpm build"
24
24
  },
25
25
  "devDependencies": {
26
- "typescript": "^5.0.0"
26
+ "typescript": "^5.0.0",
27
+ "tsup": "^8.0.2"
27
28
  }
28
29
  }