@christianriedl/rest 1.0.58 → 1.0.60

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,18 @@
1
+ import { IRest, IRestResult, ICrudRest, RepairFunction } from './iRest';
2
+ import { ILogger, IEntity } from '@christianriedl/utils';
3
+ type Dictionary<T> = {
4
+ [key: string]: T;
5
+ };
6
+ export default class CrudRest implements ICrudRest<IEntity> {
7
+ rest: IRest;
8
+ logger: ILogger;
9
+ repairFunctions: Dictionary<RepairFunction<IEntity>>;
10
+ constructor(rest: IRest, logger: ILogger);
11
+ getEntities(table: string, params?: Dictionary<any>): Promise<IRestResult<IEntity[]>>;
12
+ updateEntity(table: string, entity: IEntity): Promise<IRestResult<IEntity>>;
13
+ addEntity(table: string, entity: IEntity): Promise<IRestResult<IEntity>>;
14
+ deleteEntity(table: string, id: number): Promise<IRestResult<IEntity>>;
15
+ adjustResult(result: IRestResult<IEntity>): void;
16
+ addRepairFunction(table: string, func: RepairFunction<IEntity>): void;
17
+ }
18
+ export {};
@@ -0,0 +1,60 @@
1
+ export default class CrudRest {
2
+ rest;
3
+ logger;
4
+ repairFunctions = {};
5
+ constructor(rest, logger) {
6
+ this.logger = logger;
7
+ this.rest = rest;
8
+ }
9
+ getEntities(table, params) {
10
+ const self = this;
11
+ return this.rest.getData('api/' + table, params, (result) => {
12
+ if (result.result) {
13
+ if (result.statusCode === 200) {
14
+ const entities = result.result;
15
+ const repair = self.repairFunctions[table];
16
+ if (repair) {
17
+ for (let i = 0; i < entities.length; i++)
18
+ repair(entities[i]);
19
+ }
20
+ self.logger.trace(`getEntities ${table} ${JSON.stringify(params)} : ${entities.length}`);
21
+ }
22
+ else {
23
+ self.logger.trace(`getEntities ${table}: ${JSON.stringify(result)}`);
24
+ result.ok = false;
25
+ }
26
+ }
27
+ });
28
+ }
29
+ async updateEntity(table, entity) {
30
+ const result = await this.rest.putData(`api/${table}/${entity.id}`, entity);
31
+ this.logger.trace(`updateEntity ${table}: ${JSON.stringify(entity)} : ${result}`);
32
+ this.adjustResult(result);
33
+ return result;
34
+ }
35
+ async addEntity(table, entity) {
36
+ const result = await this.rest.postDataEx(`api/${table}`, entity);
37
+ this.logger.trace(`addEntity ${table}: ${JSON.stringify(entity)} : ${JSON.stringify(result)}`);
38
+ this.adjustResult(result);
39
+ return result;
40
+ }
41
+ async deleteEntity(table, id) {
42
+ const result = await this.rest.deleteData(`api/${table}/${id}`);
43
+ this.logger.trace(`deleteEntity ${table}: ${id} : ${JSON.stringify(result)}`);
44
+ this.adjustResult(result);
45
+ return result;
46
+ }
47
+ adjustResult(result) {
48
+ if (result.statusCode !== 200 && result.statusCode !== 201 && result.statusCode !== 204) { // OK or Resource created or No Content
49
+ result.ok = false;
50
+ if (result.statusCode === 202) { // Accepted
51
+ const problemResult = result.result;
52
+ result.statusText = `${problemResult.title} ${problemResult.type} : ${problemResult.detail}`;
53
+ }
54
+ }
55
+ }
56
+ addRepairFunction(table, func) {
57
+ this.repairFunctions[table] = func;
58
+ }
59
+ }
60
+ //# sourceMappingURL=crudRest.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"crudRest.js","sourceRoot":"","sources":["../src/crudRest.ts"],"names":[],"mappings":"AAKA,MAAM,CAAC,OAAO,OAAO,QAAQ;IACzB,IAAI,CAAQ;IACZ,MAAM,CAAU;IAChB,eAAe,GAAwC,EAAE,CAAC;IAE1D,YAAY,IAAW,EAAE,MAAe;QACpC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACrB,CAAC;IACD,WAAW,CAAC,KAAa,EAAE,MAAwB;QAC/C,MAAM,IAAI,GAAG,IAAI,CAAC;QAClB,OAAO,IAAI,CAAC,IAAI,CAAC,OAAO,CAAY,MAAM,GAAG,KAAK,EAAE,MAAM,EAAE,CAAC,MAAM,EAAE,EAAE;YACnE,IAAI,MAAM,CAAC,MAAM,EAAE,CAAC;g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C,KAAK,CAAC,GAAG,IAAI,CAAC;IACvC,CAAC;CACJ"}
package/dist/iRest.d.ts CHANGED
@@ -3,7 +3,7 @@ type Dictionary<T> = {
3
3
  };
4
4
  export interface IProblemResult {
5
5
  statusCode: number;
6
- errors: Dictionary<string> | string;
6
+ errors: Dictionary<string[] | string>;
7
7
  detail: string;
8
8
  instance: string;
9
9
  title: string;
@@ -34,4 +34,18 @@ export interface IRest {
34
34
  toValue<T>(res: IRestResult<IValueResult<T>>): T | undefined;
35
35
  toStringValue<T>(res: IRestResult<IValueResult<T>>): string;
36
36
  }
37
+ export type RepairFunction<T> = (entity: T) => void;
38
+ export declare enum ECrudAction {
39
+ None = 0,
40
+ Insert = 1,
41
+ Update = 2,
42
+ Delete = 3
43
+ }
44
+ export interface ICrudRest<T> {
45
+ addEntity(table: string, entity: T): Promise<IRestResult<T>>;
46
+ updateEntity(table: string, entity: T): Promise<IRestResult<T>>;
47
+ deleteEntity(table: string, id: unknown): Promise<IRestResult<T>>;
48
+ getEntities(table: string, search?: Dictionary<any>): Promise<IRestResult<T[]>>;
49
+ addRepairFunction(table: string, func: RepairFunction<T>): void;
50
+ }
37
51
  export {};
package/dist/iRest.js CHANGED
@@ -1,2 +1,8 @@
1
- export {};
1
+ export var ECrudAction;
2
+ (function (ECrudAction) {
3
+ ECrudAction[ECrudAction["None"] = 0] = "None";
4
+ ECrudAction[ECrudAction["Insert"] = 1] = "Insert";
5
+ ECrudAction[ECrudAction["Update"] = 2] = "Update";
6
+ ECrudAction[ECrudAction["Delete"] = 3] = "Delete";
7
+ })(ECrudAction || (ECrudAction = {}));
2
8
  //# sourceMappingURL=iRest.js.map
package/dist/iRest.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"iRest.js","sourceRoot":"","sources":["../src/iRest.ts"],"names":[],"mappings":""}
1
+ {"version":3,"file":"iRest.js","sourceRoot":"","sources":["../src/iRest.ts"],"names":[],"mappings":"AAqCA,MAAM,CAAN,IAAY,WAAgD;AAA5D,WAAY,WAAW;IAAG,6CAAQ,CAAA;IAAE,iDAAM,CAAA;IAAE,iDAAM,CAAA;IAAE,iDAAM,CAAA;AAAC,CAAC,EAAhD,WAAW,KAAX,WAAW,QAAqC"}
package/dist/index.d.ts CHANGED
@@ -2,3 +2,4 @@ export * from './rest';
2
2
  export * from './oauth';
3
3
  export * from './dummyOauth';
4
4
  export * from './authorize';
5
+ export * from './crudRest';
package/dist/index.js CHANGED
@@ -2,4 +2,5 @@ export * from './rest';
2
2
  export * from './oauth';
3
3
  export * from './dummyOauth';
4
4
  export * from './authorize';
5
+ export * from './crudRest';
5
6
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC;AACxB,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC;AACxB,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@christianriedl/rest",
3
- "version": "1.0.58",
3
+ "version": "1.0.60",
4
4
  "description": "REST and OAuth library based on fetch API",
5
5
 
6
6
  "main": "dist/index.js",
@@ -17,7 +17,7 @@
17
17
  "author": "Christian Riedl",
18
18
  "license": "ISC",
19
19
  "dependencies": {
20
- "@christianriedl/utils": "^1.0.101"
20
+ "@christianriedl/utils": "^1.0.108"
21
21
  },
22
22
  "devDependencies": {
23
23
  "@types/node": "^20.14.9",