remult 0.16.0-exp.1 → 0.16.0-exp.10

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.
Files changed (43) hide show
  1. package/esm/index.d.ts +1 -1
  2. package/esm/postgres/postgres-data-provider.js +2 -2
  3. package/esm/remult-express.js +2 -1
  4. package/esm/server/expressBridge.d.ts +1 -0
  5. package/esm/server/expressBridge.js +27 -20
  6. package/esm/src/backend-tests/test-http-server.backend-spec.js +12 -9
  7. package/esm/src/context.d.ts +17 -21
  8. package/esm/src/context.js +45 -85
  9. package/esm/src/data-interfaces.d.ts +1 -2
  10. package/esm/src/data-providers/rest-data-provider.d.ts +4 -4
  11. package/esm/src/data-providers/rest-data-provider.js +20 -17
  12. package/esm/src/remult-proxy.d.ts +2 -1
  13. package/esm/src/remult-proxy.js +22 -25
  14. package/esm/src/remult3/RepositoryImplementation.d.ts +2 -1
  15. package/esm/src/remult3/RepositoryImplementation.js +5 -3
  16. package/esm/src/server-action.d.ts +9 -7
  17. package/esm/src/server-action.js +38 -32
  18. package/esm/src/shared-tests/db-tests-setup.js +1 -1
  19. package/esm/src/tests/createData.js +1 -1
  20. package/esm/src/tests/frontend-database-tests-setup.spec.js +3 -3
  21. package/esm/src/tests/testHelper.spec.js +16 -11
  22. package/index.d.ts +1 -1
  23. package/package.json +1 -1
  24. package/postgres/postgres-data-provider.js +2 -2
  25. package/remult-express.js +9 -1
  26. package/server/expressBridge.d.ts +1 -0
  27. package/server/expressBridge.js +55 -41
  28. package/src/backend-tests/test-http-server.backend-spec.js +11 -8
  29. package/src/context.d.ts +17 -21
  30. package/src/context.js +53 -106
  31. package/src/data-interfaces.d.ts +1 -2
  32. package/src/data-providers/rest-data-provider.d.ts +4 -4
  33. package/src/data-providers/rest-data-provider.js +21 -16
  34. package/src/remult-proxy.d.ts +2 -1
  35. package/src/remult-proxy.js +25 -40
  36. package/src/remult3/RepositoryImplementation.d.ts +2 -1
  37. package/src/remult3/RepositoryImplementation.js +5 -3
  38. package/src/server-action.d.ts +9 -7
  39. package/src/server-action.js +130 -119
  40. package/src/shared-tests/db-tests-setup.js +1 -1
  41. package/src/tests/createData.js +1 -1
  42. package/src/tests/frontend-database-tests-setup.spec.js +3 -3
  43. 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, isBackend, AllowedForInstance, EventDispatcher, EventSource, HttpProvider, Unobserve, UserInfo } from './src/context';
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.setDataProvider(db);
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.setDataProvider(sql);
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);
@@ -17,6 +17,7 @@ export function remultExpress(options) {
17
17
  return Object.assign(app, {
18
18
  getRemult: (req) => server.getRemult(req),
19
19
  openApiDoc: (options) => server.openApiDoc(options),
20
- registerRouter: x => server.registerRouter(x)
20
+ registerRouter: x => server.registerRouter(x),
21
+ withRemultMiddleware: (...args) => server.withRemultMiddleware(...args)
21
22
  });
22
23
  }
@@ -33,6 +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
+ withRemultMiddleware(req: GenericRequest, res: GenericResponse, next: VoidFunction): any;
36
37
  }
37
38
  export declare type GenericRouter = {
38
39
  route(path: string): SpecificRoute;
@@ -54,7 +54,7 @@ export function createRemultServer(options) {
54
54
  options.controllers = allControllers;
55
55
  }
56
56
  if (options.rootPath === undefined)
57
- options.rootPath = Remult.apiBaseUrl;
57
+ options.rootPath = '/api';
58
58
  actionInfo.runningOnServer = true;
59
59
  let bridge = new RemultServerImplementation(new inProcessQueueHandler(options.queueStorage), options, dataProvider);
60
60
  return bridge;
@@ -78,10 +78,13 @@ class RemultServerImplementation {
78
78
  if (r)
79
79
  return r;
80
80
  else
81
- throw "remult object was requested outside of a valid context, try running it within initApi or a remult request cycle";
81
+ throw new Error("remult object was requested outside of a valid context, try running it within initApi or a remult request cycle");
82
82
  };
83
83
  }
84
84
  }
85
+ withRemultMiddleware(req, res, next) {
86
+ this.process(async () => { next(); })(req, res);
87
+ }
85
88
  handle(req, gRes) {
86
89
  if (!this.routeImpl) {
87
90
  this.routeImpl = new RouteImplementation();
@@ -120,7 +123,8 @@ class RemultServerImplementation {
120
123
  else
121
124
  res.success(job.info);
122
125
  });
123
- }
126
+ },
127
+ doWork: undefined
124
128
  }, r);
125
129
  }
126
130
  this.options.entities.forEach(e => {
@@ -152,24 +156,27 @@ class RemultServerImplementation {
152
156
  let myReq = new ExpressRequestBridgeToDataApiRequest(req);
153
157
  let myRes = new ExpressResponseBridgeToDataApiResponse(res, req);
154
158
  let remult = new Remult();
155
- remult.setDataProvider(await this.dataProvider);
156
- remultObjectStorage.run(remult, async () => {
157
- if (req) {
158
- let user;
159
- if (this.options.getUser)
160
- user = await this.options.getUser(req);
161
- else {
162
- user = req['user'];
163
- if (!user)
164
- user = req['auth'];
159
+ remult.dataProvider = (await this.dataProvider);
160
+ await new Promise(res => {
161
+ remultObjectStorage.run(remult, async () => {
162
+ if (req) {
163
+ let user;
164
+ if (this.options.getUser)
165
+ user = await this.options.getUser(req);
166
+ else {
167
+ user = req['user'];
168
+ if (!user)
169
+ user = req['auth'];
170
+ }
171
+ if (user)
172
+ remult.user = (user);
165
173
  }
166
- if (user)
167
- remult.setUser(user);
168
- }
169
- if (this.options.initRequest) {
170
- await this.options.initRequest(remult, req);
171
- }
172
- what(remult, myReq, myRes, req);
174
+ if (this.options.initRequest) {
175
+ await this.options.initRequest(remult, req);
176
+ }
177
+ await what(remult, myReq, myRes, req);
178
+ res({});
179
+ });
173
180
  });
174
181
  };
175
182
  }
@@ -1,13 +1,14 @@
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';
4
+ import { Remult } from '../context';
5
5
  import { InMemoryDataProvider } from '../data-providers/in-memory-database';
6
+ import { remult } from '../remult-proxy';
6
7
  import { Entity, Fields } from '../remult3';
7
8
  import { BackendMethod } from '../server-action';
8
9
  import { Categories } from '../tests/remult-3-entities';
9
- Remult.apiBaseUrl = 'http://localhost:3003/api';
10
- let path = Remult.apiBaseUrl + '/tasks';
10
+ remult.apiClient.url = 'http://localhost:3003/api';
11
+ let path = remult.apiClient.url + '/tasks';
11
12
  const environments = [
12
13
  // ["next", 3000],
13
14
  // ["nest", 3001],
@@ -21,8 +22,8 @@ const environments = [
21
22
  function test(name, test) {
22
23
  for (const [env, port] of environments) {
23
24
  const theTest = async () => {
24
- Remult.apiBaseUrl = `http://localhost:${port}/api`;
25
- path = Remult.apiBaseUrl + '/tasks';
25
+ remult.apiClient.url = `http://localhost:${port}/api`;
26
+ path = remult.apiClient.url + '/tasks';
26
27
  await test();
27
28
  };
28
29
  const testName = env + ` ${port}: ${name}`;
@@ -93,7 +94,7 @@ test("test http 201", async () => {
93
94
  });
94
95
  test("test regular api call", async () => {
95
96
  await create3Tasks();
96
- let result = await axios.get(Remult.apiBaseUrl + "/test");
97
+ let result = await axios.get(remult.apiClient.url + "/test");
97
98
  expect(result.data.result).toBe(3);
98
99
  });
99
100
  class FreshResponseTest {
@@ -106,9 +107,11 @@ class FreshResponseTest {
106
107
  }
107
108
  }
108
109
  it("test fresh", async () => {
109
- const api = remultFresh({ entities: [Categories],
110
+ const api = remultFresh({
111
+ entities: [Categories],
110
112
  dataProvider: new InMemoryDataProvider(),
111
- rootPath: '/api' }, FreshResponseTest);
113
+ rootPath: '/api'
114
+ }, FreshResponseTest);
112
115
  const r = await api.handle({
113
116
  url: '/api/Categories',
114
117
  json() {
@@ -158,7 +161,7 @@ Task = __decorate([
158
161
  export { Task };
159
162
  async function create3Tasks() {
160
163
  const remult = new Remult(axios);
161
- Remult.defaultHttpProvider = new HttpProviderBridgeToRestDataProviderHttpProvider(axios);
164
+ remult.apiClient.httpClient = axios;
162
165
  const taskRepo = remult.repo(Task);
163
166
  for (const task of await taskRepo.find()) {
164
167
  await taskRepo.delete(task);
@@ -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 HttpProvider {
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: HttpProvider);
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
- /** Returns the current user's info */
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,34 +53,31 @@ 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(provider?: HttpProvider | DataProvider | typeof fetch);
69
- /** The api Base Url to be used in all remult calls. by default it's set to `/api`.
70
- *
71
- * Set this property in case you want to determine a non relative api url
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
- _dataSource: DataProvider;
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
- readonly state: RemultState;
73
+ readonly context: RemultContext;
74
+ apiClient: ApiClient;
75
+ }
76
+ export interface RemultContext {
84
77
  }
85
- export interface RemultState {
78
+ export interface ApiClient {
79
+ httpClient?: ExternalHttpProvider | typeof fetch;
80
+ url?: string;
86
81
  }
87
82
  export declare const allEntities: ClassType<any>[];
88
83
  export interface ControllerOptions {
@@ -95,6 +90,7 @@ 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;
@@ -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();
104
- this.state = {};
98
+ /** The current data provider */
99
+ this.dataProvider = new RestDataProvider(() => this.apiClient);
100
+ this.context = {};
101
+ this.apiClient = {
102
+ url: '/api'
103
+ };
105
104
  if (provider && provider.getEntityDataProvider) {
106
- this._dataSource = provider;
105
+ this.dataProvider = provider;
107
106
  return;
108
107
  }
109
- let httpDataProvider = buildRestDataProvider(provider);
110
- if (!httpDataProvider) {
111
- httpDataProvider = new UseDefaultRestDataProviderHttpProvider();
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._dataSource;
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
- /** Returns the current user's info */
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.id !== undefined;
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.roles.indexOf(roles.toString()) >= 0)
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
- /** returns a dispatcher object that fires once a user has changed*/
214
- get userChange() {
215
- return this._userChangeEvent.dispatcher;
216
- }
217
- static setDefaultHttpProvider(provider) {
218
- const r = buildRestDataProvider(provider);
219
- if (!r)
220
- throw provider + " doesn't patch http provider or fetch interface";
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
- class UseDefaultRestDataProviderHttpProvider {
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 buildRestDataProvider(provider) {
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
- let http = provider;
269
- if (http && http.get && http.put && http.post && http.delete) {
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 extends HttpProvider {
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 url;
7
- private http;
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>;
@@ -1,17 +1,20 @@
1
1
  import { UrlBuilder } from '../../urlBuilder';
2
2
  import { customUrlToken } from '../filter/filter-interfaces';
3
- import { Remult, retry } from '../context';
3
+ import { buildRestDataProvider, retry } from '../context';
4
4
  export class RestDataProvider {
5
- constructor(url, http) {
6
- this.url = url;
7
- this.http = http;
5
+ constructor(apiProvider) {
6
+ this.apiProvider = apiProvider;
8
7
  this.supportsCustomFilter = true;
9
8
  }
10
9
  getEntityDataProvider(entity) {
11
- let url = this.url;
12
- if (url === undefined || url === null)
13
- url = Remult.apiBaseUrl;
14
- return new RestEntityDataProvider(url + '/' + entity.key, this.http, entity);
10
+ return new RestEntityDataProvider(() => {
11
+ let url = this.apiProvider()?.url;
12
+ if (url === undefined || url === null)
13
+ url = '/api';
14
+ return url + '/' + entity.key;
15
+ }, () => {
16
+ return buildRestDataProvider(this.apiProvider().httpClient);
17
+ }, entity);
15
18
  }
16
19
  async transaction(action) {
17
20
  throw new Error("Method not implemented.");
@@ -38,7 +41,7 @@ export class RestEntityDataProvider {
38
41
  return result;
39
42
  }
40
43
  async count(where) {
41
- let url = new UrlBuilder(this.url);
44
+ let url = new UrlBuilder(this.url());
42
45
  url.add("__action", "count");
43
46
  let filterObject;
44
47
  if (where) {
@@ -47,12 +50,12 @@ export class RestEntityDataProvider {
47
50
  filterObject = undefined;
48
51
  }
49
52
  if (filterObject)
50
- return this.http.post(url.url, filterObject).then(r => +(r.count));
53
+ return this.http().post(url.url, filterObject).then(r => +(r.count));
51
54
  else
52
- return this.http.get(url.url).then(r => +(r.count));
55
+ return this.http().get(url.url).then(r => +(r.count));
53
56
  }
54
57
  find(options) {
55
- let url = new UrlBuilder(this.url);
58
+ let url = new UrlBuilder(this.url());
56
59
  let filterObject;
57
60
  if (options) {
58
61
  if (options.where) {
@@ -81,10 +84,10 @@ export class RestEntityDataProvider {
81
84
  }
82
85
  if (filterObject) {
83
86
  url.add("__action", "get");
84
- return this.http.post(url.url, filterObject).then(x => x.map(y => this.translateFromJson(y)));
87
+ return this.http().post(url.url, filterObject).then(x => x.map(y => this.translateFromJson(y)));
85
88
  }
86
89
  else
87
- return this.http.get(url.url).then(x => x.map(y => this.translateFromJson(y)));
90
+ return this.http().get(url.url).then(x => x.map(y => this.translateFromJson(y)));
88
91
  ;
89
92
  }
90
93
  update(id, data) {
@@ -94,13 +97,13 @@ export class RestEntityDataProvider {
94
97
  if (keys.includes(col.key))
95
98
  result[col.key] = col.valueConverter.toJson(data[col.key]);
96
99
  }
97
- return this.http.put(this.url + '/' + encodeURIComponent(id), result).then(y => this.translateFromJson(y));
100
+ return this.http().put(this.url() + '/' + encodeURIComponent(id), result).then(y => this.translateFromJson(y));
98
101
  }
99
102
  delete(id) {
100
- return this.http.delete(this.url + '/' + encodeURIComponent(id));
103
+ return this.http().delete(this.url() + '/' + encodeURIComponent(id));
101
104
  }
102
105
  insert(data) {
103
- return this.http.post(this.url, this.translateToJson(data)).then(y => this.translateFromJson(y));
106
+ return this.http().post(this.url(), this.translateToJson(data)).then(y => this.translateFromJson(y));
104
107
  }
105
108
  }
106
109
  export class RestDataProviderHttpProviderUsingFetch {
@@ -1 +1,2 @@
1
- export declare const remult: RemultProxy;
1
+ import { Remult } from "./context";
2
+ export declare const remult: Remult;