remult 0.16.0-exp.8 → 0.16.2-exp.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/esm/index.d.ts +1 -1
- package/esm/postgres/postgres-data-provider.js +2 -2
- package/esm/remult-express.js +1 -1
- package/esm/remult-fastify.js +2 -1
- package/esm/server/expressBridge.d.ts +1 -1
- package/esm/server/expressBridge.js +43 -18
- package/esm/src/backend-tests/test-http-server.backend-spec.js +15 -14
- package/esm/src/context.d.ts +17 -21
- package/esm/src/context.js +44 -84
- package/esm/src/data-interfaces.d.ts +1 -2
- package/esm/src/data-providers/rest-data-provider.d.ts +8 -5
- package/esm/src/data-providers/rest-data-provider.js +37 -27
- package/esm/src/entity.d.ts +1 -1
- package/esm/src/remult-proxy.js +22 -25
- package/esm/src/remult3/RepositoryImplementation.d.ts +1 -0
- package/esm/src/remult3/RepositoryImplementation.js +3 -3
- package/esm/src/remult3/remult3.d.ts +1 -1
- package/esm/src/server-action.d.ts +9 -7
- package/esm/src/server-action.js +38 -32
- package/esm/src/shared-tests/db-tests-setup.js +1 -1
- package/esm/src/tests/createData.js +1 -1
- package/esm/src/tests/frontend-database-tests-setup.spec.js +3 -3
- package/esm/src/tests/testHelper.spec.js +16 -11
- package/index.d.ts +1 -1
- package/package.json +13 -13
- package/postgres/postgres-data-provider.js +2 -2
- package/remult-express.js +2 -2
- package/remult-fastify.js +2 -1
- package/server/expressBridge.d.ts +1 -1
- package/server/expressBridge.js +59 -23
- package/src/backend-tests/test-http-server.backend-spec.js +17 -16
- package/src/context.d.ts +17 -21
- package/src/context.js +52 -105
- package/src/data-interfaces.d.ts +1 -2
- package/src/data-providers/rest-data-provider.d.ts +8 -5
- package/src/data-providers/rest-data-provider.js +49 -26
- package/src/entity.d.ts +1 -1
- package/src/remult-proxy.js +24 -39
- package/src/remult3/RepositoryImplementation.d.ts +1 -0
- package/src/remult3/RepositoryImplementation.js +3 -3
- package/src/remult3/remult3.d.ts +1 -1
- package/src/server-action.d.ts +9 -7
- package/src/server-action.js +130 -119
- package/src/shared-tests/db-tests-setup.js +1 -1
- package/src/tests/createData.js +1 -1
- package/src/tests/frontend-database-tests-setup.spec.js +3 -3
- package/src/tests/testHelper.spec.js +19 -14
package/esm/index.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ export { SqlDatabase, } from './src/data-providers/sql-database';
|
|
|
12
12
|
export { CustomSqlFilterObject, CustomSqlFilterBuilder } from './src/filter/filter-consumer-bridge-to-sql-request';
|
|
13
13
|
export { JsonDataProvider, JsonEntityStorage } from './src/data-providers/json-data-provider';
|
|
14
14
|
export { Controller, BackendMethodOptions, BackendMethod, ProgressListener } from './src/server-action';
|
|
15
|
-
export { Allowed, Allow, Remult, RemultContext, isBackend, AllowedForInstance, EventDispatcher, EventSource,
|
|
15
|
+
export { Allowed, Allow, Remult, RemultContext, ApiClient, isBackend, AllowedForInstance, EventDispatcher, EventSource, ExternalHttpProvider, Unobserve, UserInfo } from './src/context';
|
|
16
16
|
export { IdEntity } from './src/id-entity';
|
|
17
17
|
export { SortSegment, Sort } from './src/sort';
|
|
18
18
|
export { OneToMany, CompoundIdField } from './src/column';
|
|
@@ -81,14 +81,14 @@ export async function createPostgresConnection(options) {
|
|
|
81
81
|
}
|
|
82
82
|
const db = new SqlDatabase(new PostgresDataProvider(new Pool(config)));
|
|
83
83
|
let remult = new Remult();
|
|
84
|
-
remult.
|
|
84
|
+
remult.dataProvider = (db);
|
|
85
85
|
if (options.autoCreateTables === undefined || options.autoCreateTables)
|
|
86
86
|
await verifyStructureOfAllEntities(db, remult);
|
|
87
87
|
return db;
|
|
88
88
|
}
|
|
89
89
|
export async function preparePostgresQueueStorage(sql) {
|
|
90
90
|
let c = new Remult();
|
|
91
|
-
c.
|
|
91
|
+
c.dataProvider = (sql);
|
|
92
92
|
let JobsInQueueEntity = (await import('../server/expressBridge')).JobsInQueueEntity;
|
|
93
93
|
let e = c.repo(JobsInQueueEntity);
|
|
94
94
|
await new PostgresSchemaBuilder(sql).createIfNotExist(e.metadata);
|
package/esm/remult-express.js
CHANGED
|
@@ -18,6 +18,6 @@ export function remultExpress(options) {
|
|
|
18
18
|
getRemult: (req) => server.getRemult(req),
|
|
19
19
|
openApiDoc: (options) => server.openApiDoc(options),
|
|
20
20
|
registerRouter: x => server.registerRouter(x),
|
|
21
|
-
|
|
21
|
+
withRemult: (...args) => server.withRemult(...args)
|
|
22
22
|
});
|
|
23
23
|
}
|
package/esm/remult-fastify.js
CHANGED
|
@@ -47,6 +47,7 @@ export function remultFastify(options) {
|
|
|
47
47
|
return Object.assign(pluginFunction, {
|
|
48
48
|
getRemult: x => api.getRemult(x),
|
|
49
49
|
openApiDoc: x => api.openApiDoc(x),
|
|
50
|
-
handle: (req, res) => api.handle(req, res)
|
|
50
|
+
handle: (req, res) => api.handle(req, res),
|
|
51
|
+
withRemult: (req, res, next) => api.withRemult(req, res, next)
|
|
51
52
|
});
|
|
52
53
|
}
|
|
@@ -33,7 +33,7 @@ export interface RemultServer {
|
|
|
33
33
|
}): any;
|
|
34
34
|
registerRouter(r: GenericRouter): void;
|
|
35
35
|
handle(req: GenericRequest, gRes?: GenericResponse): Promise<ServerHandleResponse | undefined>;
|
|
36
|
-
|
|
36
|
+
withRemult(req: GenericRequest, res: GenericResponse, next: VoidFunction): any;
|
|
37
37
|
}
|
|
38
38
|
export declare type GenericRouter = {
|
|
39
39
|
route(path: string): SpecificRoute;
|
|
@@ -4,7 +4,6 @@ import { DataApi, serializeError } from '../src/data-api';
|
|
|
4
4
|
import { allEntities, Remult } from '../src/context';
|
|
5
5
|
import { Entity, Fields, getEntityKey } from '../src/remult3';
|
|
6
6
|
import { IdEntity } from '../src/id-entity';
|
|
7
|
-
import { AsyncLocalStorage } from 'async_hooks';
|
|
8
7
|
import { remult } from '../src/remult-proxy';
|
|
9
8
|
;
|
|
10
9
|
export function createRemultServer(options) {
|
|
@@ -31,6 +30,25 @@ export function createRemultServer(options) {
|
|
|
31
30
|
return dp;
|
|
32
31
|
return new (await import('./JsonEntityFileStorage')).JsonFileDataProvider('./db');
|
|
33
32
|
});
|
|
33
|
+
if (!remultObjectStorage) {
|
|
34
|
+
dataProvider = dataProvider.then(async (dp) => {
|
|
35
|
+
try {
|
|
36
|
+
const asyncHooks = await import('async_hooks');
|
|
37
|
+
remultObjectStorage = new RemultAsyncLocalStorage(new asyncHooks.AsyncLocalStorage());
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
remultObjectStorage = new RemultAsyncLocalStorage(undefined);
|
|
41
|
+
}
|
|
42
|
+
remult.remultFactory = () => {
|
|
43
|
+
const r = remultObjectStorage.getRemult();
|
|
44
|
+
if (r)
|
|
45
|
+
return r;
|
|
46
|
+
else
|
|
47
|
+
throw new Error("remult object was requested outside of a valid context, try running it within initApi or a remult request cycle");
|
|
48
|
+
};
|
|
49
|
+
return dp;
|
|
50
|
+
});
|
|
51
|
+
}
|
|
34
52
|
if (options.initApi) {
|
|
35
53
|
dataProvider = dataProvider.then(async (dp) => {
|
|
36
54
|
var remult = new Remult(dp);
|
|
@@ -54,14 +72,30 @@ export function createRemultServer(options) {
|
|
|
54
72
|
options.controllers = allControllers;
|
|
55
73
|
}
|
|
56
74
|
if (options.rootPath === undefined)
|
|
57
|
-
options.rootPath =
|
|
75
|
+
options.rootPath = '/api';
|
|
58
76
|
actionInfo.runningOnServer = true;
|
|
59
77
|
let bridge = new RemultServerImplementation(new inProcessQueueHandler(options.queueStorage), options, dataProvider);
|
|
60
78
|
return bridge;
|
|
61
79
|
}
|
|
62
80
|
;
|
|
63
|
-
|
|
64
|
-
|
|
81
|
+
class RemultAsyncLocalStorage {
|
|
82
|
+
constructor(remultObjectStorage) {
|
|
83
|
+
this.remultObjectStorage = remultObjectStorage;
|
|
84
|
+
}
|
|
85
|
+
run(remult, callback) {
|
|
86
|
+
if (this.remultObjectStorage)
|
|
87
|
+
this.remultObjectStorage.run(remult, callback);
|
|
88
|
+
else
|
|
89
|
+
callback();
|
|
90
|
+
}
|
|
91
|
+
getRemult() {
|
|
92
|
+
if (!this.remultObjectStorage) {
|
|
93
|
+
throw "can't use static remult in this environment, `async_hooks` were not found";
|
|
94
|
+
}
|
|
95
|
+
return this.remultObjectStorage.getStore();
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
let remultObjectStorage;
|
|
65
99
|
class RemultServerImplementation {
|
|
66
100
|
constructor(queue, options, dataProvider) {
|
|
67
101
|
this.queue = queue;
|
|
@@ -71,18 +105,8 @@ class RemultServerImplementation {
|
|
|
71
105
|
this.hasQueue = false;
|
|
72
106
|
/* internal */
|
|
73
107
|
this.backendMethodsOpenApi = [];
|
|
74
|
-
if (!remultObjectStorageWasSetup) {
|
|
75
|
-
remultObjectStorageWasSetup = true;
|
|
76
|
-
remult.remultFactory = () => {
|
|
77
|
-
const r = remultObjectStorage.getStore();
|
|
78
|
-
if (r)
|
|
79
|
-
return r;
|
|
80
|
-
else
|
|
81
|
-
throw new Error("remult object was requested outside of a valid context, try running it within initApi or a remult request cycle");
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
108
|
}
|
|
85
|
-
|
|
109
|
+
withRemult(req, res, next) {
|
|
86
110
|
this.process(async () => { next(); })(req, res);
|
|
87
111
|
}
|
|
88
112
|
handle(req, gRes) {
|
|
@@ -123,7 +147,8 @@ class RemultServerImplementation {
|
|
|
123
147
|
else
|
|
124
148
|
res.success(job.info);
|
|
125
149
|
});
|
|
126
|
-
}
|
|
150
|
+
},
|
|
151
|
+
doWork: undefined
|
|
127
152
|
}, r);
|
|
128
153
|
}
|
|
129
154
|
this.options.entities.forEach(e => {
|
|
@@ -155,7 +180,7 @@ class RemultServerImplementation {
|
|
|
155
180
|
let myReq = new ExpressRequestBridgeToDataApiRequest(req);
|
|
156
181
|
let myRes = new ExpressResponseBridgeToDataApiResponse(res, req);
|
|
157
182
|
let remult = new Remult();
|
|
158
|
-
remult.
|
|
183
|
+
remult.dataProvider = (await this.dataProvider);
|
|
159
184
|
await new Promise(res => {
|
|
160
185
|
remultObjectStorage.run(remult, async () => {
|
|
161
186
|
if (req) {
|
|
@@ -168,7 +193,7 @@ class RemultServerImplementation {
|
|
|
168
193
|
user = req['auth'];
|
|
169
194
|
}
|
|
170
195
|
if (user)
|
|
171
|
-
remult.
|
|
196
|
+
remult.user = (user);
|
|
172
197
|
}
|
|
173
198
|
if (this.options.initRequest) {
|
|
174
199
|
await this.options.initRequest(remult, req);
|
|
@@ -1,28 +1,28 @@
|
|
|
1
1
|
import { __decorate, __metadata } from "tslib";
|
|
2
2
|
import axios from 'axios';
|
|
3
3
|
import { remultFresh } from '../../remult-fresh';
|
|
4
|
-
import { HttpProviderBridgeToRestDataProviderHttpProvider, Remult } from '../context';
|
|
5
4
|
import { InMemoryDataProvider } from '../data-providers/in-memory-database';
|
|
5
|
+
import { remult } from '../remult-proxy';
|
|
6
6
|
import { Entity, Fields } from '../remult3';
|
|
7
7
|
import { BackendMethod } from '../server-action';
|
|
8
8
|
import { Categories } from '../tests/remult-3-entities';
|
|
9
|
-
|
|
10
|
-
let path =
|
|
9
|
+
remult.apiClient.url = 'http://localhost:3003/api';
|
|
10
|
+
let path = remult.apiClient.url + '/tasks';
|
|
11
11
|
const environments = [
|
|
12
12
|
// ["next", 3000],
|
|
13
13
|
// ["nest", 3001],
|
|
14
|
-
//
|
|
15
|
-
//
|
|
16
|
-
//
|
|
17
|
-
//
|
|
14
|
+
// ["koa", 3002],
|
|
15
|
+
// ["fastify", 3003],
|
|
16
|
+
// ["express", 3004],
|
|
17
|
+
// ["generic server", 3005],
|
|
18
18
|
// ["optine", 3006],
|
|
19
19
|
// ["fresh", 8000]
|
|
20
20
|
];
|
|
21
21
|
function test(name, test) {
|
|
22
22
|
for (const [env, port] of environments) {
|
|
23
23
|
const theTest = async () => {
|
|
24
|
-
|
|
25
|
-
path =
|
|
24
|
+
remult.apiClient.url = `http://localhost:${port}/api`;
|
|
25
|
+
path = remult.apiClient.url + '/tasks';
|
|
26
26
|
await test();
|
|
27
27
|
};
|
|
28
28
|
const testName = env + ` ${port}: ${name}`;
|
|
@@ -93,7 +93,7 @@ test("test http 201", async () => {
|
|
|
93
93
|
});
|
|
94
94
|
test("test regular api call", async () => {
|
|
95
95
|
await create3Tasks();
|
|
96
|
-
let result = await axios.get(
|
|
96
|
+
let result = await axios.get(remult.apiClient.url + "/test");
|
|
97
97
|
expect(result.data.result).toBe(3);
|
|
98
98
|
});
|
|
99
99
|
class FreshResponseTest {
|
|
@@ -106,9 +106,11 @@ class FreshResponseTest {
|
|
|
106
106
|
}
|
|
107
107
|
}
|
|
108
108
|
it("test fresh", async () => {
|
|
109
|
-
const api = remultFresh({
|
|
109
|
+
const api = remultFresh({
|
|
110
|
+
entities: [Categories],
|
|
110
111
|
dataProvider: new InMemoryDataProvider(),
|
|
111
|
-
rootPath: '/api'
|
|
112
|
+
rootPath: '/api'
|
|
113
|
+
}, FreshResponseTest);
|
|
112
114
|
const r = await api.handle({
|
|
113
115
|
url: '/api/Categories',
|
|
114
116
|
json() {
|
|
@@ -157,8 +159,7 @@ Task = __decorate([
|
|
|
157
159
|
], Task);
|
|
158
160
|
export { Task };
|
|
159
161
|
async function create3Tasks() {
|
|
160
|
-
|
|
161
|
-
Remult.defaultHttpProvider = new HttpProviderBridgeToRestDataProviderHttpProvider(axios);
|
|
162
|
+
remult.apiClient.httpClient = axios;
|
|
162
163
|
const taskRepo = remult.repo(Task);
|
|
163
164
|
for (const task of await taskRepo.find()) {
|
|
164
165
|
await taskRepo.delete(task);
|
package/esm/src/context.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { DataProvider, RestDataProviderHttpProvider } from "./data-interfaces";
|
|
2
|
+
import { RestDataProviderHttpProviderUsingFetch } from './data-providers/rest-data-provider';
|
|
2
3
|
import { EntityMetadata, EntityRef, FindOptions, Repository } from "./remult3";
|
|
3
4
|
import { ClassType } from "../classType";
|
|
4
|
-
export interface
|
|
5
|
+
export interface ExternalHttpProvider {
|
|
5
6
|
post(url: string, data: any): Promise<any> | {
|
|
6
7
|
toPromise(): Promise<any>;
|
|
7
8
|
};
|
|
@@ -17,7 +18,7 @@ export interface HttpProvider {
|
|
|
17
18
|
}
|
|
18
19
|
export declare class HttpProviderBridgeToRestDataProviderHttpProvider implements RestDataProviderHttpProvider {
|
|
19
20
|
private http;
|
|
20
|
-
constructor(http:
|
|
21
|
+
constructor(http: ExternalHttpProvider);
|
|
21
22
|
post(url: string, data: any): Promise<any>;
|
|
22
23
|
delete(url: string): Promise<void>;
|
|
23
24
|
put(url: string, data: any): Promise<any>;
|
|
@@ -37,10 +38,7 @@ export declare class Remult {
|
|
|
37
38
|
*
|
|
38
39
|
*/
|
|
39
40
|
repo<T>(entity: ClassType<T>, dataProvider?: DataProvider): Repository<T>;
|
|
40
|
-
|
|
41
|
-
get user(): UserInfo;
|
|
42
|
-
/** Set's the current user info */
|
|
43
|
-
setUser(info: UserInfo | undefined): Promise<void>;
|
|
41
|
+
user?: UserInfo;
|
|
44
42
|
/** Checks if a user was authenticated */
|
|
45
43
|
authenticated(): boolean;
|
|
46
44
|
/** checks if the user has any of the roles specified in the parameters
|
|
@@ -55,35 +53,32 @@ export declare class Remult {
|
|
|
55
53
|
* [Allowed](https://remult.dev/docs/allowed.html)
|
|
56
54
|
*/
|
|
57
55
|
isAllowedForInstance(instance: any, allowed?: AllowedForInstance<any>): boolean;
|
|
58
|
-
/** returns a dispatcher object that fires once a user has changed*/
|
|
59
|
-
get userChange(): EventDispatcher;
|
|
60
|
-
static defaultHttpProvider: RestDataProviderHttpProvider;
|
|
61
|
-
static setDefaultHttpProvider(provider: HttpProvider | typeof fetch): void;
|
|
62
56
|
/** Creates a new instance of the `remult` object.
|
|
63
57
|
*
|
|
64
58
|
* Can receive either an HttpProvider or a DataProvider as a parameter - which will be used to fetch data from.
|
|
65
59
|
*
|
|
66
60
|
* If no provider is specified, `fetch` will be used as an http provider
|
|
67
61
|
*/
|
|
68
|
-
constructor(
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
*/
|
|
73
|
-
static apiBaseUrl: string;
|
|
62
|
+
constructor(http: ExternalHttpProvider | typeof fetch | ApiClient);
|
|
63
|
+
constructor(p: DataProvider);
|
|
64
|
+
constructor();
|
|
65
|
+
call<T extends ((...args: any[]) => Promise<Y>), Y>(backendMethod: T, self?: any): T;
|
|
74
66
|
/** The current data provider */
|
|
75
|
-
|
|
76
|
-
/** sets the current data provider */
|
|
77
|
-
setDataProvider(dataProvider: DataProvider): void;
|
|
67
|
+
dataProvider: DataProvider;
|
|
78
68
|
/** A helper callback that can be used to debug and trace all find operations. Useful in debugging scenarios */
|
|
79
69
|
static onFind: (metadata: EntityMetadata, options: FindOptions<any>) => void;
|
|
80
70
|
clearAllCache(): any;
|
|
81
71
|
/** A helper callback that is called whenever an entity is created. */
|
|
82
72
|
static entityRefInit?: (ref: EntityRef<any>, row: any) => void;
|
|
83
73
|
readonly context: RemultContext;
|
|
74
|
+
apiClient: ApiClient;
|
|
84
75
|
}
|
|
85
76
|
export interface RemultContext {
|
|
86
77
|
}
|
|
78
|
+
export interface ApiClient {
|
|
79
|
+
httpClient?: ExternalHttpProvider | typeof fetch;
|
|
80
|
+
url?: string;
|
|
81
|
+
}
|
|
87
82
|
export declare const allEntities: ClassType<any>[];
|
|
88
83
|
export interface ControllerOptions {
|
|
89
84
|
key: string;
|
|
@@ -95,11 +90,12 @@ export declare class ClassHelper {
|
|
|
95
90
|
export declare class MethodHelper {
|
|
96
91
|
classes: Map<any, ControllerOptions>;
|
|
97
92
|
}
|
|
93
|
+
export declare function buildRestDataProvider(provider: ExternalHttpProvider | typeof fetch): RestDataProviderHttpProvider | RestDataProviderHttpProviderUsingFetch;
|
|
98
94
|
export declare function setControllerSettings(target: any, options: ControllerOptions): void;
|
|
99
95
|
export interface UserInfo {
|
|
100
96
|
id: string;
|
|
101
|
-
name
|
|
102
|
-
roles
|
|
97
|
+
name?: string;
|
|
98
|
+
roles?: string[];
|
|
103
99
|
}
|
|
104
100
|
export declare type Allowed = boolean | string | string[] | ((c: Remult) => boolean);
|
|
105
101
|
export declare type AllowedForInstance<T> = boolean | string | string[] | ((c: Remult, entity?: T) => boolean);
|
package/esm/src/context.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { actionInfo } from './server-action';
|
|
1
|
+
import { actionInfo, serverActionField } from './server-action';
|
|
2
2
|
import { RestDataProvider, RestDataProviderHttpProviderUsingFetch } from './data-providers/rest-data-provider';
|
|
3
3
|
import { RepositoryImplementation } from "./remult3/RepositoryImplementation";
|
|
4
4
|
export class HttpProviderBridgeToRestDataProviderHttpProvider {
|
|
@@ -75,6 +75,8 @@ export async function processHttpException(ex) {
|
|
|
75
75
|
message: error,
|
|
76
76
|
};
|
|
77
77
|
}
|
|
78
|
+
if (z.modelState)
|
|
79
|
+
error.modelState = z.modelState;
|
|
78
80
|
let httpStatusCode = z.status;
|
|
79
81
|
if (httpStatusCode === undefined)
|
|
80
82
|
httpStatusCode = z.response?.status;
|
|
@@ -90,27 +92,31 @@ export function isBackend() {
|
|
|
90
92
|
return actionInfo.runningOnServer;
|
|
91
93
|
}
|
|
92
94
|
export class Remult {
|
|
93
|
-
/** Creates a new instance of the `remult` object.
|
|
94
|
-
*
|
|
95
|
-
* Can receive either an HttpProvider or a DataProvider as a parameter - which will be used to fetch data from.
|
|
96
|
-
*
|
|
97
|
-
* If no provider is specified, `fetch` will be used as an http provider
|
|
98
|
-
*/
|
|
99
95
|
constructor(provider) {
|
|
100
|
-
/* @internal */
|
|
101
|
-
this._userChangeEvent = new EventSource();
|
|
102
96
|
/* @internal */
|
|
103
97
|
this.repCache = new Map();
|
|
98
|
+
/** The current data provider */
|
|
99
|
+
this.dataProvider = new RestDataProvider(() => this.apiClient);
|
|
104
100
|
this.context = {};
|
|
101
|
+
this.apiClient = {
|
|
102
|
+
url: '/api'
|
|
103
|
+
};
|
|
105
104
|
if (provider && provider.getEntityDataProvider) {
|
|
106
|
-
this.
|
|
105
|
+
this.dataProvider = provider;
|
|
107
106
|
return;
|
|
108
107
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
108
|
+
if (isExternalHttpProvider(provider)) {
|
|
109
|
+
this.apiClient.httpClient = provider;
|
|
110
|
+
}
|
|
111
|
+
else if (typeof (provider) === "function")
|
|
112
|
+
this.apiClient.httpClient = provider;
|
|
113
|
+
else if (provider) {
|
|
114
|
+
const apiClient = provider;
|
|
115
|
+
if (apiClient.httpClient)
|
|
116
|
+
this.apiClient.httpClient = apiClient.httpClient;
|
|
117
|
+
if (apiClient.url)
|
|
118
|
+
this.apiClient.url = apiClient.url;
|
|
112
119
|
}
|
|
113
|
-
this._dataSource = new RestDataProvider(null, httpDataProvider);
|
|
114
120
|
}
|
|
115
121
|
/**Return's a `Repository` of the specific entity type
|
|
116
122
|
* @example
|
|
@@ -120,7 +126,7 @@ export class Remult {
|
|
|
120
126
|
*/
|
|
121
127
|
repo(entity, dataProvider) {
|
|
122
128
|
if (dataProvider === undefined)
|
|
123
|
-
dataProvider = this.
|
|
129
|
+
dataProvider = this.dataProvider;
|
|
124
130
|
let dpCache = this.repCache.get(dataProvider);
|
|
125
131
|
if (!dpCache)
|
|
126
132
|
this.repCache.set(dataProvider, dpCache = new Map());
|
|
@@ -130,39 +136,10 @@ export class Remult {
|
|
|
130
136
|
}
|
|
131
137
|
return r;
|
|
132
138
|
}
|
|
133
|
-
|
|
134
|
-
get user() {
|
|
135
|
-
if (this._user === undefined) {
|
|
136
|
-
return {
|
|
137
|
-
id: undefined,
|
|
138
|
-
name: '',
|
|
139
|
-
roles: []
|
|
140
|
-
};
|
|
141
|
-
}
|
|
142
|
-
return this._user;
|
|
143
|
-
}
|
|
144
|
-
/** Set's the current user info */
|
|
145
|
-
async setUser(info) {
|
|
146
|
-
this._user = info;
|
|
147
|
-
if (this._user === null)
|
|
148
|
-
this._user = undefined;
|
|
149
|
-
let auth = info;
|
|
150
|
-
if (auth) {
|
|
151
|
-
if (!this._user.id && auth.sub)
|
|
152
|
-
this._user.id = auth.sub;
|
|
153
|
-
if (!this._user.roles && auth.permissions) {
|
|
154
|
-
this._user.roles = auth.permissions;
|
|
155
|
-
}
|
|
156
|
-
if (!this._user.name)
|
|
157
|
-
this._user.name = auth.username;
|
|
158
|
-
}
|
|
159
|
-
if (this._user && !this._user.roles)
|
|
160
|
-
this._user.roles = [];
|
|
161
|
-
await this._userChangeEvent.fire();
|
|
162
|
-
}
|
|
139
|
+
/* delete me */
|
|
163
140
|
/** Checks if a user was authenticated */
|
|
164
141
|
authenticated() {
|
|
165
|
-
return this.user
|
|
142
|
+
return this.user?.id !== undefined;
|
|
166
143
|
}
|
|
167
144
|
/** checks if the user has any of the roles specified in the parameters
|
|
168
145
|
* @example
|
|
@@ -187,7 +164,7 @@ export class Remult {
|
|
|
187
164
|
if (typeof roles === 'boolean')
|
|
188
165
|
return roles;
|
|
189
166
|
if (typeof roles === 'string')
|
|
190
|
-
if (this.user
|
|
167
|
+
if (this.user?.roles?.indexOf(roles.toString()) >= 0)
|
|
191
168
|
return true;
|
|
192
169
|
return false;
|
|
193
170
|
}
|
|
@@ -210,46 +187,22 @@ export class Remult {
|
|
|
210
187
|
else
|
|
211
188
|
return this.isAllowed(allowed);
|
|
212
189
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
Remult.defaultHttpProvider = r;
|
|
222
|
-
}
|
|
223
|
-
/** sets the current data provider */
|
|
224
|
-
setDataProvider(dataProvider) {
|
|
225
|
-
this._dataSource = dataProvider;
|
|
190
|
+
call(backendMethod, self) {
|
|
191
|
+
const z = (backendMethod[serverActionField]);
|
|
192
|
+
if (!z.doWork)
|
|
193
|
+
throw Error("The method received is not a valid backend method");
|
|
194
|
+
//@ts-ignore
|
|
195
|
+
return (...args) => {
|
|
196
|
+
return z.doWork(args, self, this.apiClient.url, buildRestDataProvider(this.apiClient.httpClient));
|
|
197
|
+
};
|
|
226
198
|
}
|
|
227
199
|
clearAllCache() {
|
|
228
200
|
this.repCache.clear();
|
|
229
201
|
}
|
|
230
202
|
}
|
|
231
|
-
Remult.defaultHttpProvider = new RestDataProviderHttpProviderUsingFetch();
|
|
232
|
-
/** The api Base Url to be used in all remult calls. by default it's set to `/api`.
|
|
233
|
-
*
|
|
234
|
-
* Set this property in case you want to determine a non relative api url
|
|
235
|
-
*/
|
|
236
|
-
Remult.apiBaseUrl = '/api';
|
|
237
203
|
/** A helper callback that can be used to debug and trace all find operations. Useful in debugging scenarios */
|
|
238
204
|
Remult.onFind = (metadata, options) => { };
|
|
239
|
-
|
|
240
|
-
post(url, data) {
|
|
241
|
-
return Remult.defaultHttpProvider.post(url, data);
|
|
242
|
-
}
|
|
243
|
-
delete(url) {
|
|
244
|
-
return Remult.defaultHttpProvider.delete(url);
|
|
245
|
-
}
|
|
246
|
-
put(url, data) {
|
|
247
|
-
return Remult.defaultHttpProvider.put(url, data);
|
|
248
|
-
}
|
|
249
|
-
get(url) {
|
|
250
|
-
return Remult.defaultHttpProvider.get(url);
|
|
251
|
-
}
|
|
252
|
-
}
|
|
205
|
+
;
|
|
253
206
|
export const allEntities = [];
|
|
254
207
|
export const classHelpers = new Map();
|
|
255
208
|
export class ClassHelper {
|
|
@@ -262,12 +215,19 @@ export class MethodHelper {
|
|
|
262
215
|
this.classes = new Map();
|
|
263
216
|
}
|
|
264
217
|
}
|
|
265
|
-
function
|
|
218
|
+
function isExternalHttpProvider(item) {
|
|
219
|
+
let http = item;
|
|
220
|
+
if (http && http.get && http.put && http.post && http.delete)
|
|
221
|
+
return true;
|
|
222
|
+
return false;
|
|
223
|
+
}
|
|
224
|
+
export function buildRestDataProvider(provider) {
|
|
225
|
+
if (!provider)
|
|
226
|
+
return new RestDataProviderHttpProviderUsingFetch();
|
|
266
227
|
let httpDataProvider;
|
|
267
228
|
if (!httpDataProvider) {
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
httpDataProvider = new HttpProviderBridgeToRestDataProviderHttpProvider(http);
|
|
229
|
+
if (isExternalHttpProvider(provider)) {
|
|
230
|
+
httpDataProvider = new HttpProviderBridgeToRestDataProviderHttpProvider(provider);
|
|
271
231
|
}
|
|
272
232
|
}
|
|
273
233
|
if (!httpDataProvider) {
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { Sort } from './sort';
|
|
2
2
|
import { Filter } from './filter/filter-interfaces';
|
|
3
3
|
import { EntityMetadata, OmitEB } from './remult3';
|
|
4
|
-
import { HttpProvider } from './context';
|
|
5
4
|
export interface DataProvider {
|
|
6
5
|
getEntityDataProvider(entity: EntityMetadata): EntityDataProvider;
|
|
7
6
|
transaction(action: (dataProvider: DataProvider) => Promise<void>): Promise<void>;
|
|
@@ -20,7 +19,7 @@ export interface EntityDataProviderFindOptions {
|
|
|
20
19
|
page?: number;
|
|
21
20
|
orderBy?: Sort;
|
|
22
21
|
}
|
|
23
|
-
export interface RestDataProviderHttpProvider
|
|
22
|
+
export interface RestDataProviderHttpProvider {
|
|
24
23
|
post(url: string, data: any): Promise<any>;
|
|
25
24
|
delete(url: string): Promise<void>;
|
|
26
25
|
put(url: string, data: any): Promise<any>;
|
|
@@ -2,10 +2,10 @@ import { EntityDataProvider, DataProvider, EntityDataProviderFindOptions, RestDa
|
|
|
2
2
|
import { UrlBuilder } from '../../urlBuilder';
|
|
3
3
|
import { Filter } from '../filter/filter-interfaces';
|
|
4
4
|
import { EntityMetadata } from '../remult3';
|
|
5
|
+
import { ApiClient } from '../context';
|
|
5
6
|
export declare class RestDataProvider implements DataProvider {
|
|
6
|
-
private
|
|
7
|
-
|
|
8
|
-
constructor(url: string | null, http: RestDataProviderHttpProvider);
|
|
7
|
+
private apiProvider;
|
|
8
|
+
constructor(apiProvider: () => ApiClient);
|
|
9
9
|
getEntityDataProvider(entity: EntityMetadata): EntityDataProvider;
|
|
10
10
|
transaction(action: (dataProvider: DataProvider) => Promise<void>): Promise<void>;
|
|
11
11
|
supportsCustomFilter: boolean;
|
|
@@ -14,7 +14,7 @@ export declare class RestEntityDataProvider implements EntityDataProvider {
|
|
|
14
14
|
private url;
|
|
15
15
|
private http;
|
|
16
16
|
private entity;
|
|
17
|
-
constructor(url: string, http: RestDataProviderHttpProvider, entity: EntityMetadata);
|
|
17
|
+
constructor(url: () => string, http: () => RestDataProviderHttpProvider, entity: EntityMetadata);
|
|
18
18
|
translateFromJson(row: any): {};
|
|
19
19
|
translateToJson(row: any): {};
|
|
20
20
|
count(where: Filter): Promise<number>;
|
|
@@ -30,6 +30,9 @@ export declare class RestDataProviderHttpProviderUsingFetch implements RestDataP
|
|
|
30
30
|
put(url: string, data: any): Promise<any>;
|
|
31
31
|
delete(url: string): Promise<any>;
|
|
32
32
|
post(url: string, data: any): Promise<any>;
|
|
33
|
-
myFetch(url: string,
|
|
33
|
+
myFetch(url: string, options?: {
|
|
34
|
+
method?: string;
|
|
35
|
+
body?: string;
|
|
36
|
+
}): Promise<any>;
|
|
34
37
|
}
|
|
35
38
|
export declare function addFilterToUrlAndReturnTrueIfSuccessful(filter: any, url: UrlBuilder): boolean;
|