@medyll/idae-api 0.1.0 → 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/README.md +240 -0
- package/dist/client/IdaeApiClient.d.ts +21 -0
- package/dist/client/IdaeApiClient.js +31 -0
- package/dist/client/IdaeApiClientCollection.d.ts +14 -0
- package/dist/client/IdaeApiClientCollection.js +53 -0
- package/dist/client/IdaeApiClientConfig.d.ts +23 -0
- package/dist/client/IdaeApiClientConfig.js +43 -0
- package/dist/client/IdaeApiClientRequest.d.ts +24 -0
- package/dist/client/IdaeApiClientRequest.js +47 -0
- package/dist/config/routeDefinitions.d.ts +10 -0
- package/dist/config/routeDefinitions.js +48 -0
- package/dist/idae-api.d.ts +15 -0
- package/dist/index.d.ts +5 -1
- package/dist/index.js +5 -1
- package/dist/server/IdaeApi.d.ts +38 -0
- package/dist/server/IdaeApi.js +213 -0
- package/dist/server/adapters/MongoDBAdapter.d.ts +20 -0
- package/dist/server/adapters/MongoDBAdapter.js +66 -0
- package/dist/server/adapters/MySQLAdapter.d.ts +23 -0
- package/dist/server/adapters/MySQLAdapter.js +78 -0
- package/dist/server/adapters/types.d.ts +12 -0
- package/dist/server/engine/DatabaseManager.d.ts +26 -0
- package/dist/server/engine/DatabaseManager.js +60 -0
- package/dist/server/engine/mongooseConnectionManager.d.ts +11 -0
- package/dist/server/engine/mongooseConnectionManager.js +25 -0
- package/dist/server/engine/routeManager.d.ts +13 -0
- package/dist/server/engine/routeManager.js +38 -0
- package/dist/server/engine/types.d.ts +10 -0
- package/dist/server/engine/types.js +1 -0
- package/dist/server/middleware/authMiddleware.d.ts +15 -0
- package/dist/server/middleware/authMiddleware.js +83 -0
- package/dist/server/middleware/databaseMiddleware.d.ts +2 -0
- package/dist/server/middleware/databaseMiddleware.js +15 -0
- package/dist/server/services/AuthService.d.ts +13 -0
- package/dist/server/services/AuthService.js +56 -0
- package/dist/server/services/DBaseService.d.ts +17 -0
- package/dist/server/services/DBaseService.js +62 -0
- package/package.json +31 -20
- package/dist/ApiServe.d.ts +0 -10
- package/dist/ApiServe.js +0 -113
- package/dist/engine/DBaseManager.d.ts +0 -15
- package/dist/engine/DBaseManager.js +0 -49
- package/dist/engine/DBaseService.d.ts +0 -16
- package/dist/engine/DBaseService.js +0 -48
- package/dist/engine/tools.d.ts +0 -2
- package/dist/engine/tools.js +0 -11
- package/dist/engine/types.d.ts +0 -9
- /package/dist/{engine → server/adapters}/types.js +0 -0
package/README.md
CHANGED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
# @medyll/idae-api
|
|
2
|
+
|
|
3
|
+
A flexible and extensible API framework for Node.js, designed to work with multiple database types and configurations.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Modular architecture with clear separation of concerns
|
|
8
|
+
- Dynamic database connection management
|
|
9
|
+
- Flexible routing system
|
|
10
|
+
- Support for multiple database types (currently MongoDB, with easy extensibility for others)
|
|
11
|
+
- TypeScript support for improved robustness and maintainability
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
npm install @medyll/idae-api
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Quick Start
|
|
20
|
+
|
|
21
|
+
```javascript
|
|
22
|
+
import { idaeApi } from '@medyll/idae-api';
|
|
23
|
+
|
|
24
|
+
// Configure the server
|
|
25
|
+
idaeApi.setOptions({
|
|
26
|
+
port: 3000,
|
|
27
|
+
enableAuth: false,
|
|
28
|
+
onInUse: 'reboot'
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
// Start the server
|
|
32
|
+
idaeApi.start();
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Configuration
|
|
36
|
+
|
|
37
|
+
You can configure the API server using the `setOptions` method:
|
|
38
|
+
|
|
39
|
+
```javascript
|
|
40
|
+
idaeApi.setOptions({
|
|
41
|
+
port: 3000,
|
|
42
|
+
routes: customRoutes,
|
|
43
|
+
enableAuth: true,
|
|
44
|
+
jwtSecret: 'your-secret-key',
|
|
45
|
+
tokenExpiration: '1h',
|
|
46
|
+
onInUse: 'reboot'
|
|
47
|
+
});
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Custom Routes
|
|
51
|
+
|
|
52
|
+
You can add custom routes to the API:
|
|
53
|
+
|
|
54
|
+
```javascript
|
|
55
|
+
const customRoutes = [
|
|
56
|
+
{
|
|
57
|
+
method: 'get',
|
|
58
|
+
path: '/custom/hello',
|
|
59
|
+
handler: async () => ({ message: 'Hello from custom route!' }),
|
|
60
|
+
requiresAuth: false
|
|
61
|
+
}
|
|
62
|
+
];
|
|
63
|
+
|
|
64
|
+
idaeApi.router.addRoutes(customRoutes);
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Database Adapters
|
|
68
|
+
|
|
69
|
+
The API currently supports MongoDB out of the box. You can easily extend it to support other databases by implementing the `DatabaseAdapter` interface.
|
|
70
|
+
|
|
71
|
+
## Error Handling
|
|
72
|
+
|
|
73
|
+
The API includes built-in error handling middleware. You can customize error handling by modifying the `configureErrorHandling` method in the `IdaeApi` class.
|
|
74
|
+
|
|
75
|
+
## API Client Usage
|
|
76
|
+
|
|
77
|
+
The `@medyll/idae-api` package includes a flexible and powerful API client that allows you to interact with your API endpoints easily. Below is a detailed guide on how to use the `IdaeApiClient` and `IdaeApiClientCollection` classes.
|
|
78
|
+
|
|
79
|
+
### Configuration
|
|
80
|
+
|
|
81
|
+
First, configure the API client using the `IdaeApiClientConfig` singleton. This configuration will set up the host, port, method, default database, and other options for the client.
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
import { IdaeApiClientConfig } from '@medyll/idae-api';
|
|
85
|
+
|
|
86
|
+
IdaeApiClientConfig.setOptions({
|
|
87
|
+
host: 'localhost',
|
|
88
|
+
port: 3000,
|
|
89
|
+
method: 'http',
|
|
90
|
+
defaultDb: 'idae_base',
|
|
91
|
+
separator: '//'
|
|
92
|
+
});
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Creating a Client Instance
|
|
96
|
+
|
|
97
|
+
Create an instance of the `IdaeApiClient` class to start interacting with your API.
|
|
98
|
+
|
|
99
|
+
```typescript
|
|
100
|
+
import { IdaeApiClient } from '@medyll/idae-api';
|
|
101
|
+
|
|
102
|
+
const client = new IdaeApiClient();
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Interacting with Databases and Collections
|
|
106
|
+
|
|
107
|
+
You can interact with different databases and collections using the `db` and `collection` methods.
|
|
108
|
+
|
|
109
|
+
#### Getting the List of Databases
|
|
110
|
+
|
|
111
|
+
```typescript
|
|
112
|
+
async function listDatabases() {
|
|
113
|
+
try {
|
|
114
|
+
const dbList = await client.getDbList();
|
|
115
|
+
console.log('Databases:', dbList);
|
|
116
|
+
} catch (error) {
|
|
117
|
+
console.error('Error fetching database list:', error);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
listDatabases();
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
#### Getting the List of Collections in a Database
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
async function listCollections(dbName: string) {
|
|
128
|
+
try {
|
|
129
|
+
const collections = await client.getCollections(dbName);
|
|
130
|
+
console.log(`Collections in ${dbName}:`, collections);
|
|
131
|
+
} catch (error) {
|
|
132
|
+
console.error(`Error fetching collections for ${dbName}:`, error);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
listCollections('idae_base');
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
#### Performing CRUD Operations on a Collection
|
|
140
|
+
|
|
141
|
+
You can perform CRUD operations on a collection using the `IdaeApiClientCollection` class.
|
|
142
|
+
|
|
143
|
+
```typescript
|
|
144
|
+
async function performCrudOperations() {
|
|
145
|
+
const appConfCollection = client.collection('app_conf');
|
|
146
|
+
|
|
147
|
+
try {
|
|
148
|
+
// Create a new document
|
|
149
|
+
const newDoc = await appConfCollection.create({ name: 'Test Config', value: 'Test Value' });
|
|
150
|
+
console.log('Created document:', newDoc);
|
|
151
|
+
|
|
152
|
+
// Find all documents
|
|
153
|
+
const allDocs = await appConfCollection.findAll();
|
|
154
|
+
console.log('All documents:', allDocs);
|
|
155
|
+
|
|
156
|
+
// Find a specific document by ID
|
|
157
|
+
const foundDoc = await appConfCollection.findById(newDoc._id);
|
|
158
|
+
console.log('Found document:', foundDoc);
|
|
159
|
+
|
|
160
|
+
// Update the document
|
|
161
|
+
const updatedDoc = await appConfCollection.update(newDoc._id, { value: 'Updated Value' });
|
|
162
|
+
console.log('Updated document:', updatedDoc);
|
|
163
|
+
|
|
164
|
+
// Delete the document
|
|
165
|
+
const deleteResult = await appConfCollection.deleteById(newDoc._id);
|
|
166
|
+
console.log('Deleted document:', deleteResult);
|
|
167
|
+
} catch (error) {
|
|
168
|
+
console.error('Error performing CRUD operations:', error);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
performCrudOperations();
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
#### Using a Specific Database
|
|
176
|
+
|
|
177
|
+
You can also specify a database explicitly when working with collections.
|
|
178
|
+
|
|
179
|
+
```typescript
|
|
180
|
+
async function useSpecificDatabase() {
|
|
181
|
+
const appSchemeCollection = client.db('idae_base').collection('appscheme');
|
|
182
|
+
|
|
183
|
+
try {
|
|
184
|
+
const appSchemeDocs = await appSchemeCollection.findAll();
|
|
185
|
+
console.log('Documents in appscheme:', appSchemeDocs);
|
|
186
|
+
} catch (error) {
|
|
187
|
+
console.error('Error fetching documents from appscheme:', error);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
useSpecificDatabase();
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### Classes and Methods
|
|
195
|
+
|
|
196
|
+
#### `IdaeApiClient`
|
|
197
|
+
|
|
198
|
+
- `constructor(clientConfig?: IdaeApiClientConfigCoreOptions & { baseUrl?: string })`
|
|
199
|
+
- `getDbList(): Promise<Response>`
|
|
200
|
+
- `getCollections(dbName: string): Promise<Response>`
|
|
201
|
+
- `db(dbName: string): { collection: (collectionName: string) => IdaeApiClientCollection; getCollections: () => Promise<Response> }`
|
|
202
|
+
- `collection(collectionName: string, dbName?: string): IdaeApiClientCollection`
|
|
203
|
+
|
|
204
|
+
#### `IdaeApiClientCollection`
|
|
205
|
+
|
|
206
|
+
- `constructor(apiClient: IdaeApiClient, dbName: string, collectionName: string)`
|
|
207
|
+
- `findAll<T>(params?: RequestParams): Promise<Response>`
|
|
208
|
+
- `findById<T>(id: string): Promise<Response>`
|
|
209
|
+
- `create<T>(body: T): Promise<Response>`
|
|
210
|
+
- `update<T>(id: string, body: T): Promise<Response>`
|
|
211
|
+
- `deleteById<T>(id: string): Promise<Response>`
|
|
212
|
+
- `deleteManyByQuery<T>(params: RequestParams): Promise<Response>`
|
|
213
|
+
|
|
214
|
+
#### `IdaeApiClientConfig`
|
|
215
|
+
|
|
216
|
+
- `setOptions(options: Partial<IdaeApiClientConfigCoreOptions> | undefined = {}): void`
|
|
217
|
+
- `get baseUrl(): string`
|
|
218
|
+
|
|
219
|
+
#### `IdaeApiClientRequest`
|
|
220
|
+
|
|
221
|
+
- `constructor(clientConfig: IdaeApiClientConfigCore)`
|
|
222
|
+
- `doRequest<T, R = any>(options: RequestOptions<T>): Promise<R>`
|
|
223
|
+
- `private buildUrl(params: UrlParams): string`
|
|
224
|
+
|
|
225
|
+
### Types
|
|
226
|
+
|
|
227
|
+
- `RequestParams`: `Record<string, unknown>`
|
|
228
|
+
- `HttpMethod`: `'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'`
|
|
229
|
+
- `RouteNamespace`: `` `methods/${'dbs' | 'collections'}` ``
|
|
230
|
+
- `UrlParams`: `{ dbName?: string; collectionName?: string; slug?: string; params?: Record<string, string>; routeNamespace?: RouteNamespace }`
|
|
231
|
+
- `RequestOptions<T>`: `UrlParams & { baseUrl?: string; method?: HttpMethod; body?: T; headers?: RequestInit['headers'] }`
|
|
232
|
+
|
|
233
|
+
## Contributing
|
|
234
|
+
|
|
235
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
236
|
+
|
|
237
|
+
## License
|
|
238
|
+
|
|
239
|
+
This project is licensed under the MIT License.
|
|
240
|
+
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type IdaeApiClientConfigCoreOptions } from './IdaeApiClientConfig';
|
|
2
|
+
import { IdaeApiClientCollection } from './IdaeApiClientCollection';
|
|
3
|
+
import { IdaeApiClientRequest } from './IdaeApiClientRequest';
|
|
4
|
+
type IdaeApiClientRequestParams<T = any> = Record<string, T>;
|
|
5
|
+
declare class IdaeApiClient {
|
|
6
|
+
private clientConfig;
|
|
7
|
+
private _request;
|
|
8
|
+
constructor(clientConfig?: IdaeApiClientConfigCoreOptions & {
|
|
9
|
+
baseUrl?: string;
|
|
10
|
+
});
|
|
11
|
+
get request(): IdaeApiClientRequest;
|
|
12
|
+
getDbList(): Promise<Response>;
|
|
13
|
+
getCollections(dbName: string): Promise<Response>;
|
|
14
|
+
db(dbName: string): {
|
|
15
|
+
collection: (collectionName: string) => IdaeApiClientCollection;
|
|
16
|
+
getCollections: () => Promise<Response>;
|
|
17
|
+
};
|
|
18
|
+
collection(collectionName: string, dbName?: string): IdaeApiClientCollection;
|
|
19
|
+
}
|
|
20
|
+
export { IdaeApiClient };
|
|
21
|
+
export type { IdaeApiClientRequestParams };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// packages\idae-api\src\lib\client\IdaeApiClient.ts
|
|
2
|
+
import { IdaeApiClientConfig, IdaeApiClientConfigCore } from './IdaeApiClientConfig';
|
|
3
|
+
import { IdaeApiClientCollection } from './IdaeApiClientCollection';
|
|
4
|
+
import { IdaeApiClientRequest } from './IdaeApiClientRequest';
|
|
5
|
+
class IdaeApiClient {
|
|
6
|
+
constructor(clientConfig) {
|
|
7
|
+
this.clientConfig = IdaeApiClientConfig;
|
|
8
|
+
this.clientConfig.setOptions(clientConfig);
|
|
9
|
+
this._request = new IdaeApiClientRequest(this.clientConfig);
|
|
10
|
+
}
|
|
11
|
+
get request() {
|
|
12
|
+
return this._request;
|
|
13
|
+
}
|
|
14
|
+
async getDbList() {
|
|
15
|
+
return this._request.doRequest({ routeNamespace: 'methods/dbs' });
|
|
16
|
+
}
|
|
17
|
+
async getCollections(dbName) {
|
|
18
|
+
return this._request.doRequest({ dbName, routeNamespace: 'methods/collections' });
|
|
19
|
+
}
|
|
20
|
+
db(dbName) {
|
|
21
|
+
return {
|
|
22
|
+
collection: (collectionName) => new IdaeApiClientCollection(this, dbName, collectionName),
|
|
23
|
+
getCollections: () => this.getCollections(dbName)
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
collection(collectionName, dbName) {
|
|
27
|
+
dbName = dbName || this.clientConfig.defaultDb;
|
|
28
|
+
return new IdaeApiClientCollection(this, dbName, collectionName);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export { IdaeApiClient };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { IdaeApiClient } from './IdaeApiClient';
|
|
2
|
+
import type { IdaeApiClientRequestParams } from './IdaeApiClient';
|
|
3
|
+
declare class IdaeApiClientCollection {
|
|
4
|
+
private apiClient;
|
|
5
|
+
private meta;
|
|
6
|
+
constructor(apiClient: IdaeApiClient, dbName: string, collectionName: string);
|
|
7
|
+
findAll<T>(params?: IdaeApiClientRequestParams): Promise<Response>;
|
|
8
|
+
findById<T>(id: string): Promise<Response>;
|
|
9
|
+
create<T>(body: T): Promise<Response>;
|
|
10
|
+
update<T>(id: string, body: T): Promise<Response>;
|
|
11
|
+
deleteById<T>(id: string): Promise<Response>;
|
|
12
|
+
deleteManyByQuery<T>(params: IdaeApiClientRequestParams): Promise<Response>;
|
|
13
|
+
}
|
|
14
|
+
export { IdaeApiClientCollection };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// packages\idae-api\src\lib\client\IdaeApiClientCollection.ts
|
|
2
|
+
import { IdaeApiClient } from './IdaeApiClient';
|
|
3
|
+
class IdaeApiClientCollection {
|
|
4
|
+
constructor(apiClient, dbName, collectionName) {
|
|
5
|
+
this.apiClient = apiClient;
|
|
6
|
+
this.meta = {
|
|
7
|
+
dbName,
|
|
8
|
+
collectionName
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
async findAll(params) {
|
|
12
|
+
return this.apiClient.request.doRequest({
|
|
13
|
+
...this.meta,
|
|
14
|
+
params
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
async findById(id) {
|
|
18
|
+
return this.apiClient.request.doRequest({
|
|
19
|
+
...this.meta,
|
|
20
|
+
slug: id
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
async create(body) {
|
|
24
|
+
return this.apiClient.request.doRequest({
|
|
25
|
+
method: 'POST',
|
|
26
|
+
...this.meta,
|
|
27
|
+
body
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
async update(id, body) {
|
|
31
|
+
return this.apiClient.request.doRequest({
|
|
32
|
+
method: 'PUT',
|
|
33
|
+
...this.meta,
|
|
34
|
+
body,
|
|
35
|
+
slug: id
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
async deleteById(id) {
|
|
39
|
+
return this.apiClient.request.doRequest({
|
|
40
|
+
method: 'DELETE',
|
|
41
|
+
...this.meta,
|
|
42
|
+
slug: id
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
async deleteManyByQuery(params) {
|
|
46
|
+
return this.apiClient.request.doRequest({
|
|
47
|
+
method: 'DELETE',
|
|
48
|
+
...this.meta,
|
|
49
|
+
params
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
export { IdaeApiClientCollection };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export interface IdaeApiClientConfigCoreOptions {
|
|
2
|
+
host: string;
|
|
3
|
+
port: number;
|
|
4
|
+
method: 'http' | 'https';
|
|
5
|
+
defaultDb: string;
|
|
6
|
+
separator: string;
|
|
7
|
+
}
|
|
8
|
+
export declare class IdaeApiClientConfigCore {
|
|
9
|
+
private static instance;
|
|
10
|
+
private options;
|
|
11
|
+
private _baseUrl;
|
|
12
|
+
private constructor();
|
|
13
|
+
static getInstance(): IdaeApiClientConfigCore;
|
|
14
|
+
setOptions(options?: Partial<IdaeApiClientConfigCoreOptions> | undefined): void;
|
|
15
|
+
get baseUrl(): string;
|
|
16
|
+
private forgeBaseUrl;
|
|
17
|
+
get host(): string;
|
|
18
|
+
get port(): number;
|
|
19
|
+
get method(): 'http' | 'https';
|
|
20
|
+
get separator(): string;
|
|
21
|
+
get defaultDb(): string;
|
|
22
|
+
}
|
|
23
|
+
export declare const IdaeApiClientConfig: IdaeApiClientConfigCore;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export class IdaeApiClientConfigCore {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.options = {
|
|
4
|
+
host: 'localhost',
|
|
5
|
+
port: 3000,
|
|
6
|
+
method: 'https',
|
|
7
|
+
defaultDb: 'idaenext_sitebase_app',
|
|
8
|
+
separator: '//'
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
static getInstance() {
|
|
12
|
+
if (!IdaeApiClientConfigCore.instance) {
|
|
13
|
+
IdaeApiClientConfigCore.instance = new IdaeApiClientConfigCore();
|
|
14
|
+
}
|
|
15
|
+
return IdaeApiClientConfigCore.instance;
|
|
16
|
+
}
|
|
17
|
+
setOptions(options = {}) {
|
|
18
|
+
this.options = { ...this.options, ...options };
|
|
19
|
+
this._baseUrl = this.forgeBaseUrl();
|
|
20
|
+
}
|
|
21
|
+
get baseUrl() {
|
|
22
|
+
return this._baseUrl;
|
|
23
|
+
}
|
|
24
|
+
forgeBaseUrl() {
|
|
25
|
+
return `${this.method}:${this.separator}${this.host}:${this.port}`;
|
|
26
|
+
}
|
|
27
|
+
get host() {
|
|
28
|
+
return this.options.host;
|
|
29
|
+
}
|
|
30
|
+
get port() {
|
|
31
|
+
return this.options.port;
|
|
32
|
+
}
|
|
33
|
+
get method() {
|
|
34
|
+
return this.options.method;
|
|
35
|
+
}
|
|
36
|
+
get separator() {
|
|
37
|
+
return this.options.separator;
|
|
38
|
+
}
|
|
39
|
+
get defaultDb() {
|
|
40
|
+
return this.options.defaultDb;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
export const IdaeApiClientConfig = IdaeApiClientConfigCore.getInstance();
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { IdaeApiClientConfigCore } from './IdaeApiClientConfig';
|
|
2
|
+
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
3
|
+
type RouteNamespace = `methods/${'dbs' | 'collections'}` | undefined;
|
|
4
|
+
interface UrlParams {
|
|
5
|
+
dbName?: string;
|
|
6
|
+
collectionName?: string;
|
|
7
|
+
slug?: string;
|
|
8
|
+
params?: Record<string, string>;
|
|
9
|
+
routeNamespace?: RouteNamespace;
|
|
10
|
+
}
|
|
11
|
+
interface RequestOptions<T> extends UrlParams {
|
|
12
|
+
baseUrl?: string;
|
|
13
|
+
method?: HttpMethod;
|
|
14
|
+
body?: T;
|
|
15
|
+
headers?: RequestInit['headers'];
|
|
16
|
+
}
|
|
17
|
+
declare class IdaeApiClientRequest {
|
|
18
|
+
private baseUrl;
|
|
19
|
+
private clientConfig;
|
|
20
|
+
constructor(clientConfig: IdaeApiClientConfigCore);
|
|
21
|
+
doRequest<T>({ baseUrl, method, body, headers, dbName, collectionName, slug, params, routeNamespace }: RequestOptions<T>): Promise<Response>;
|
|
22
|
+
private buildUrl;
|
|
23
|
+
}
|
|
24
|
+
export { IdaeApiClientRequest, type HttpMethod, type RouteNamespace, type UrlParams, type RequestOptions };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// packages\idae-api\src\lib\client\IdaeApiClientRequest.ts
|
|
2
|
+
import { IdaeApiClientConfigCore } from './IdaeApiClientConfig';
|
|
3
|
+
class IdaeApiClientRequest {
|
|
4
|
+
constructor(clientConfig) {
|
|
5
|
+
this.clientConfig = clientConfig;
|
|
6
|
+
this.baseUrl = `${this.clientConfig.method}:${this.clientConfig.separator}${this.clientConfig.host}:${this.clientConfig.port}`;
|
|
7
|
+
}
|
|
8
|
+
async doRequest({ baseUrl = this.baseUrl, method = 'GET', body, headers = {
|
|
9
|
+
'Content-Type': 'application/json'
|
|
10
|
+
}, dbName = this.clientConfig.defaultDb, collectionName, slug, params, routeNamespace }) {
|
|
11
|
+
const url = this.buildUrl({
|
|
12
|
+
routeNamespace,
|
|
13
|
+
dbName,
|
|
14
|
+
collectionName,
|
|
15
|
+
slug,
|
|
16
|
+
params
|
|
17
|
+
}).replace('//', '/');
|
|
18
|
+
const response = await fetch(`${baseUrl}${url}`, {
|
|
19
|
+
method,
|
|
20
|
+
headers,
|
|
21
|
+
body: body ? JSON.stringify(body) : undefined
|
|
22
|
+
});
|
|
23
|
+
if (!response.ok) {
|
|
24
|
+
throw new Error(`HTTP error! status: ${response.status}`);
|
|
25
|
+
}
|
|
26
|
+
try {
|
|
27
|
+
return response.json();
|
|
28
|
+
}
|
|
29
|
+
catch (e) {
|
|
30
|
+
console.error(e);
|
|
31
|
+
throw new Error(`Invalid returned type`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
buildUrl({ dbName, collectionName, slug, params, routeNamespace }) {
|
|
35
|
+
const urls = [`/`];
|
|
36
|
+
if (routeNamespace)
|
|
37
|
+
urls.push(routeNamespace);
|
|
38
|
+
if (dbName ?? collectionName)
|
|
39
|
+
urls.push([dbName, collectionName].filter((x) => x).join('.'));
|
|
40
|
+
if (slug)
|
|
41
|
+
urls.push(slug);
|
|
42
|
+
if (params)
|
|
43
|
+
urls.push(`?${new URLSearchParams(params).toString()}`);
|
|
44
|
+
return urls.join('/').replace('//', '/');
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
export { IdaeApiClientRequest };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { DBaseService } from '../server/services/DBaseService';
|
|
2
|
+
type RouteHandler = (service: DBaseService<any>, params: any, body?: any) => Promise<any>;
|
|
3
|
+
export interface RouteDefinition {
|
|
4
|
+
method: string | string[];
|
|
5
|
+
path: string;
|
|
6
|
+
handler: RouteHandler;
|
|
7
|
+
disabled?: boolean;
|
|
8
|
+
}
|
|
9
|
+
export declare const routes: RouteDefinition[];
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// packages\idae-api\src\lib\config\routeDefinitions.ts
|
|
2
|
+
import { DBaseService } from '../server/services/DBaseService';
|
|
3
|
+
export const routes = [
|
|
4
|
+
{
|
|
5
|
+
method: 'get',
|
|
6
|
+
path: '/:collectionName',
|
|
7
|
+
handler: async (service, params) => service.where(params)
|
|
8
|
+
},
|
|
9
|
+
{
|
|
10
|
+
method: 'get',
|
|
11
|
+
path: '/:collectionName/:id',
|
|
12
|
+
handler: async (service, params) => service.findById(params.id)
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
method: 'post',
|
|
16
|
+
path: '/:collectionName',
|
|
17
|
+
handler: async (service, params, body) => service.create(body)
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
method: 'put',
|
|
21
|
+
path: '/:collectionName/:id',
|
|
22
|
+
handler: async (service, params, body) => service.update(params.id, body)
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
method: 'delete',
|
|
26
|
+
path: '/:collectionName/:id',
|
|
27
|
+
handler: async (service, params) => service.deleteById(params.id)
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
method: 'delete',
|
|
31
|
+
path: '/:collectionName',
|
|
32
|
+
handler: async (service, params) => service.deleteManyByQuery(params)
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
method: ['where', 'create', 'update', 'deleteById', 'deleteManyByQuery'], // default method is then GET or OPTIONS (set further)
|
|
36
|
+
path: '/query/:collectionName/:command/:parameters?',
|
|
37
|
+
handler: async (service, params, body) => {
|
|
38
|
+
const decodedParams = params.body ? service.decodeUrlParams(params.body) : {};
|
|
39
|
+
console.log(params.command, 'params --- ', { body });
|
|
40
|
+
return service?.[params.command]?.({ query: body });
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
method: ['dbs', 'collections'], // default method is then GET or OPTIONS (set further)
|
|
45
|
+
path: '/methods/:methodName/:params?',
|
|
46
|
+
handler: async (service, params) => { }
|
|
47
|
+
}
|
|
48
|
+
];
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// packages\idae-api\src\lib\idae-api.d.ts
|
|
2
|
+
|
|
3
|
+
import mongoose from 'mongoose';
|
|
4
|
+
|
|
5
|
+
declare global {
|
|
6
|
+
namespace Express {
|
|
7
|
+
interface Request {
|
|
8
|
+
dbConnection?: mongoose.Connection;
|
|
9
|
+
collectionName?: string;
|
|
10
|
+
dbName?: string;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export {};
|
package/dist/index.d.ts
CHANGED
|
@@ -1 +1,5 @@
|
|
|
1
|
-
export
|
|
1
|
+
export * from './server/IdaeApi.js';
|
|
2
|
+
export * from './client/IdaeApiClient.js';
|
|
3
|
+
export * from './client/IdaeApiClientCollection.js';
|
|
4
|
+
export * from './client/IdaeApiClientConfig.js';
|
|
5
|
+
export * from './client/IdaeApiClientRequest.js';
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,6 @@
|
|
|
1
1
|
// Reexport your entry components here
|
|
2
|
-
export
|
|
2
|
+
export * from './server/IdaeApi.js';
|
|
3
|
+
export * from './client/IdaeApiClient.js';
|
|
4
|
+
export * from './client/IdaeApiClientCollection.js';
|
|
5
|
+
export * from './client/IdaeApiClientConfig.js';
|
|
6
|
+
export * from './client/IdaeApiClientRequest.js';
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import { type RouteDefinition } from '../config/routeDefinitions';
|
|
3
|
+
import { RouteManager } from './engine/routeManager';
|
|
4
|
+
interface IdaeApiOptions {
|
|
5
|
+
port?: number;
|
|
6
|
+
routes?: RouteDefinition[];
|
|
7
|
+
onInUse?: 'reboot' | 'fail' | 'replace';
|
|
8
|
+
enableAuth?: boolean;
|
|
9
|
+
jwtSecret?: string;
|
|
10
|
+
tokenExpiration?: string;
|
|
11
|
+
}
|
|
12
|
+
declare class IdaeApi {
|
|
13
|
+
private static instance;
|
|
14
|
+
private _app;
|
|
15
|
+
private options;
|
|
16
|
+
private routeManager;
|
|
17
|
+
private serverInstance;
|
|
18
|
+
private _state;
|
|
19
|
+
private authMiddleware;
|
|
20
|
+
private constructor();
|
|
21
|
+
static getInstance(): IdaeApi;
|
|
22
|
+
get state(): 'stopped' | 'running';
|
|
23
|
+
get app(): express.Express;
|
|
24
|
+
setOptions(options: IdaeApiOptions): void;
|
|
25
|
+
private initializeAuth;
|
|
26
|
+
configureIdaeApi(): void;
|
|
27
|
+
private configureMiddleware;
|
|
28
|
+
private configureRoutes;
|
|
29
|
+
private configureErrorHandling;
|
|
30
|
+
start(): void;
|
|
31
|
+
private handleServerError;
|
|
32
|
+
stop(): void;
|
|
33
|
+
private addRouteToExpress;
|
|
34
|
+
private handleRequest;
|
|
35
|
+
get router(): RouteManager;
|
|
36
|
+
}
|
|
37
|
+
declare const idaeApi: IdaeApi;
|
|
38
|
+
export { idaeApi };
|