@napp/dti-client 4.4.4 → 4.4.5

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,14 @@
1
+ import { DtiAction, DtiRoute } from "@napp/dti-core";
2
+ import { DtiClient } from "./client";
3
+ export interface ODtiClientHeaderBuilder {
4
+ (action: DtiAction<any, any>): Record<string, string>;
5
+ }
6
+ export declare class DtiClientBuilder {
7
+ private _baseUrls;
8
+ private _headers;
9
+ baseUrl(route: DtiRoute, url: string): this;
10
+ getBaseUrl(name: string): string | undefined;
11
+ header(route: DtiRoute, builder: ODtiClientHeaderBuilder): this;
12
+ getHeader(name: string): ODtiClientHeaderBuilder | undefined;
13
+ build(): DtiClient;
14
+ }
package/esm/builder.js ADDED
@@ -0,0 +1,33 @@
1
+ import { TreeNamer } from "./tree";
2
+ import { DtiClient } from "./client";
3
+ export class DtiClientBuilder {
4
+ constructor() {
5
+ this._baseUrls = new TreeNamer("root");
6
+ this._headers = new TreeNamer("root");
7
+ }
8
+ baseUrl(route, url) {
9
+ this._baseUrls.set(route.getFullname(), url);
10
+ return this;
11
+ }
12
+ getBaseUrl(name) {
13
+ let note = this._baseUrls.findParent(name);
14
+ if (note) {
15
+ return note.getValue();
16
+ }
17
+ return undefined;
18
+ }
19
+ header(route, builder) {
20
+ this._headers.set(route.getFullname(), builder);
21
+ return this;
22
+ }
23
+ getHeader(name) {
24
+ let note = this._headers.findParent(name);
25
+ if (note) {
26
+ return note.getValue();
27
+ }
28
+ return undefined;
29
+ }
30
+ build() {
31
+ return new DtiClient(this);
32
+ }
33
+ }
@@ -0,0 +1,23 @@
1
+ import { DtiAction } from "@napp/dti-core";
2
+ import { DtiClientBuilder } from "./builder";
3
+ export interface BundleMeta<RESULT, PARAM> {
4
+ meta: DtiAction<RESULT, PARAM>;
5
+ param: PARAM;
6
+ }
7
+ export declare class DtiClientBandler {
8
+ private bundleMetas;
9
+ private builder;
10
+ private base62;
11
+ constructor(bundleMetas: Array<BundleMeta<any, any>>, builder: DtiClientBuilder);
12
+ validate(): void;
13
+ private getMethod;
14
+ private getBase;
15
+ getParam(): {
16
+ name: string;
17
+ param: any;
18
+ }[];
19
+ private getHeaders;
20
+ call(): Promise<any>;
21
+ private callGet;
22
+ private callPost;
23
+ }
package/esm/bundler.js ADDED
@@ -0,0 +1,93 @@
1
+ import { DtiMode, Base62 } from "@napp/dti-core";
2
+ import { Exception } from "@napp/exception";
3
+ import { fetch } from "cross-fetch";
4
+ import { responseHandle } from "./errorhandle";
5
+ export class DtiClientBandler {
6
+ constructor(bundleMetas, builder) {
7
+ this.bundleMetas = bundleMetas;
8
+ this.builder = builder;
9
+ this.base62 = new Base62();
10
+ }
11
+ validate() {
12
+ for (let it of this.bundleMetas) {
13
+ it.meta.validate(it.param);
14
+ }
15
+ }
16
+ getMethod() {
17
+ for (let it of this.bundleMetas) {
18
+ let m = it.meta.getMode();
19
+ if (m === DtiMode.BFrom || m === DtiMode.BJson) {
20
+ return 'post';
21
+ }
22
+ }
23
+ return 'get';
24
+ }
25
+ getBase() {
26
+ for (let it of this.bundleMetas) {
27
+ let base = this.builder.getBaseUrl(it.meta.getFullname());
28
+ if (base) {
29
+ return base;
30
+ }
31
+ }
32
+ return '';
33
+ }
34
+ getParam() {
35
+ let param = [];
36
+ for (let it of this.bundleMetas) {
37
+ param.push({
38
+ name: it.meta.getFullname(),
39
+ param: it.param
40
+ });
41
+ }
42
+ return param;
43
+ }
44
+ getHeaders() {
45
+ let headers = {};
46
+ for (let it of this.bundleMetas) {
47
+ let _headers = this.builder.getHeader(it.meta.getRoute().getFullname());
48
+ if (_headers) {
49
+ headers = { ...headers, ..._headers };
50
+ }
51
+ }
52
+ headers["Content-Type"] = "application/json";
53
+ return headers;
54
+ }
55
+ async call() {
56
+ this.validate();
57
+ let method = this.getMethod();
58
+ if (method === 'get') {
59
+ return await this.callGet();
60
+ }
61
+ return await this.callPost();
62
+ }
63
+ async callGet() {
64
+ try {
65
+ let baseUrl = this.getBase();
66
+ let param = this.getParam();
67
+ let headers = this.getHeaders();
68
+ let p = this.base62.encode(JSON.stringify(param));
69
+ let q = new URLSearchParams({ p }).toString();
70
+ let resp = await fetch(`${baseUrl}/__bundler_get__?${q}`, {
71
+ method: 'get', headers
72
+ });
73
+ return await responseHandle(resp);
74
+ }
75
+ catch (error) {
76
+ throw Exception.from(error);
77
+ }
78
+ }
79
+ async callPost() {
80
+ try {
81
+ let baseUrl = this.getBase();
82
+ let param = this.getParam();
83
+ let headers = this.getHeaders();
84
+ let resp = await fetch(`${baseUrl}/__bundler_post__`, {
85
+ method: 'post', headers, body: JSON.stringify(param)
86
+ });
87
+ return await responseHandle(resp);
88
+ }
89
+ catch (error) {
90
+ throw Exception.from(error);
91
+ }
92
+ }
93
+ }
@@ -0,0 +1,19 @@
1
+ import { DtiAction } from "@napp/dti-core";
2
+ import { DtiClientBuilder } from "./builder";
3
+ import { BundleMeta } from "./bundler";
4
+ export declare class DtiClientCaller<RESULT, PARAM> {
5
+ private meta;
6
+ private builder;
7
+ private base62;
8
+ private routeClient;
9
+ constructor(meta: DtiAction<RESULT, PARAM>, builder: DtiClientBuilder);
10
+ validate(param: PARAM): void;
11
+ private getMethod;
12
+ private getBody;
13
+ private getQeury;
14
+ private getHeaders;
15
+ private getUrl;
16
+ bundler(param: PARAM): BundleMeta<RESULT, PARAM>;
17
+ call(param: PARAM): Promise<RESULT>;
18
+ callRaw(param: PARAM): Promise<Response>;
19
+ }
package/esm/caller.js ADDED
@@ -0,0 +1,92 @@
1
+ import { DtiMode, Base62 } from "@napp/dti-core";
2
+ import { fetch } from "cross-fetch";
3
+ import { DtiClientRoute } from "./route";
4
+ import { responseHandle } from "./errorhandle";
5
+ export class DtiClientCaller {
6
+ constructor(meta, builder) {
7
+ this.meta = meta;
8
+ this.builder = builder;
9
+ this.base62 = new Base62();
10
+ this.routeClient = new DtiClientRoute(meta.getRoute(), builder);
11
+ }
12
+ validate(param) {
13
+ this.meta.validate(param);
14
+ }
15
+ getMethod() {
16
+ let m = this.meta.getMode();
17
+ if (m === DtiMode.QString || m === DtiMode.QJson) {
18
+ return 'get';
19
+ }
20
+ return 'post';
21
+ }
22
+ getBody(param) {
23
+ let m = this.meta.getMode();
24
+ if (m === DtiMode.BJson) {
25
+ if (param) {
26
+ return JSON.stringify(param);
27
+ }
28
+ }
29
+ if (m === DtiMode.BFrom) {
30
+ // const formData = new FormData();
31
+ // Object.keys(param as any).forEach(key => formData.append(key, (param as any)[key]));
32
+ if (param) {
33
+ return new URLSearchParams(param).toString();
34
+ }
35
+ }
36
+ return undefined;
37
+ }
38
+ getQeury(param) {
39
+ let m = this.meta.getMode();
40
+ if (m === DtiMode.QJson) {
41
+ if (param) {
42
+ let p = this.base62.encode(JSON.stringify(param));
43
+ return new URLSearchParams({ p }).toString();
44
+ }
45
+ }
46
+ else if (m === DtiMode.QString) {
47
+ if (param) {
48
+ let json = JSON.stringify(param);
49
+ let obj = JSON.parse(json);
50
+ return new URLSearchParams(obj).toString();
51
+ }
52
+ }
53
+ return undefined;
54
+ }
55
+ getHeaders(param) {
56
+ let m = this.meta.getMode();
57
+ let headers = {};
58
+ let confHeaderBuilder = this.builder.getHeader(this.meta.getRoute().getFullname());
59
+ if (confHeaderBuilder) {
60
+ let confHeaders = confHeaderBuilder(this.meta);
61
+ headers = { ...headers, ...confHeaders };
62
+ }
63
+ if (m === DtiMode.BFrom) {
64
+ headers["Content-Type"] = "application/x-www-form-urlencoded";
65
+ }
66
+ else {
67
+ headers["Content-Type"] = "application/json";
68
+ }
69
+ return headers;
70
+ }
71
+ getUrl() {
72
+ return this.routeClient.buildUrl(this.meta.getPath());
73
+ }
74
+ bundler(param) {
75
+ return { meta: this.meta, param };
76
+ }
77
+ async call(param) {
78
+ let resp = await this.callRaw(param);
79
+ return await responseHandle(resp);
80
+ }
81
+ async callRaw(param) {
82
+ this.validate(param);
83
+ let url = this.getUrl();
84
+ let query = this.getQeury(param);
85
+ let method = this.getMethod();
86
+ let headers = this.getHeaders(param);
87
+ let body = this.getBody(param);
88
+ return await fetch(url + (query ? `?${query}` : ''), {
89
+ method, headers, body
90
+ });
91
+ }
92
+ }
@@ -0,0 +1,11 @@
1
+ import { DtiAction, DtiRoute } from "@napp/dti-core";
2
+ import { DtiClientBuilder } from "./builder";
3
+ import { DtiClientCaller } from "./caller";
4
+ import { BundleMeta } from "./bundler";
5
+ export declare class DtiClient {
6
+ private builder;
7
+ constructor(builder: DtiClientBuilder);
8
+ dti<RESULT, PARAM>(meta: DtiAction<RESULT, PARAM>): DtiClientCaller<RESULT, PARAM>;
9
+ buildUrl(route: DtiRoute, actionPath: string): string;
10
+ bundle<R1, P1, R2, P2, R3, P3, R4, P4, R5, P5, R6, P6>(m1: BundleMeta<R1, P1>, m2?: BundleMeta<R2, P2>, m3?: BundleMeta<R3, P3>, m4?: BundleMeta<R4, P4>, m5?: BundleMeta<R5, P5>, m6?: BundleMeta<R6, P6>): Promise<[R1, R2, R3, R4, R5, R6]>;
11
+ }
package/esm/client.js ADDED
@@ -0,0 +1,33 @@
1
+ import { DtiClientRoute } from "./route";
2
+ import { DtiClientCaller } from "./caller";
3
+ import { DtiClientBandler } from "./bundler";
4
+ export class DtiClient {
5
+ constructor(builder) {
6
+ this.builder = builder;
7
+ }
8
+ dti(meta) {
9
+ return new DtiClientCaller(meta, this.builder);
10
+ }
11
+ buildUrl(route, actionPath) {
12
+ return new DtiClientRoute(route, this.builder).buildUrl(actionPath);
13
+ }
14
+ async bundle(m1, m2, m3, m4, m5, m6) {
15
+ let bundleMetas = [m1];
16
+ if (m2) {
17
+ bundleMetas.push(m2);
18
+ }
19
+ if (m3) {
20
+ bundleMetas.push(m3);
21
+ }
22
+ if (m4) {
23
+ bundleMetas.push(m4);
24
+ }
25
+ if (m5) {
26
+ bundleMetas.push(m5);
27
+ }
28
+ if (m6) {
29
+ bundleMetas.push(m6);
30
+ }
31
+ return await new DtiClientBandler(bundleMetas, this.builder).call();
32
+ }
33
+ }
@@ -0,0 +1,2 @@
1
+ import { Exception } from "@napp/exception";
2
+ export declare function responseHandle<T>(resp: Response, parser?: (errObject: any) => Exception | undefined): Promise<T>;
@@ -0,0 +1,50 @@
1
+ import { Exception } from "@napp/exception";
2
+ export async function responseHandle(resp, parser) {
3
+ try {
4
+ let rsu = await resp.text();
5
+ if (resp.ok) {
6
+ if (rsu) {
7
+ try {
8
+ let value = JSON.parse(rsu);
9
+ return value;
10
+ }
11
+ catch (error) {
12
+ throw new Exception(rsu, {
13
+ kind: 'serviceunavailable',
14
+ });
15
+ }
16
+ }
17
+ return void 0;
18
+ }
19
+ if (rsu) {
20
+ let err;
21
+ try {
22
+ let errObject = JSON.parse(rsu);
23
+ if (parser) {
24
+ let e1 = parser(errObject);
25
+ if (e1) {
26
+ err = e1;
27
+ }
28
+ else {
29
+ err = Exception.from(errObject);
30
+ }
31
+ }
32
+ else {
33
+ err = Exception.from(errObject);
34
+ }
35
+ }
36
+ catch (error) {
37
+ err = new Exception(rsu, {
38
+ kind: 'serviceunavailable',
39
+ });
40
+ }
41
+ throw err;
42
+ }
43
+ throw new Exception(`status=${resp.status}. ${resp.statusText}`, {
44
+ kind: 'serviceunavailable',
45
+ });
46
+ }
47
+ catch (error) {
48
+ throw Exception.from(error);
49
+ }
50
+ }
package/esm/index.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ export * from './builder';
2
+ export * from './caller';
3
+ export * from './client';
4
+ export * from './route';
5
+ export * from './tree';
6
+ export * from './errorhandle';
package/esm/index.js ADDED
@@ -0,0 +1,6 @@
1
+ export * from './builder';
2
+ export * from './caller';
3
+ export * from './client';
4
+ export * from './route';
5
+ export * from './tree';
6
+ export * from './errorhandle';
package/esm/route.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ import { DtiRoute } from "@napp/dti-core";
2
+ import { DtiClientBuilder } from "./builder";
3
+ export declare class DtiClientRoute {
4
+ private meta;
5
+ private builder;
6
+ constructor(meta: DtiRoute, builder: DtiClientBuilder);
7
+ buildUrl(action: string): string;
8
+ }
package/esm/route.js ADDED
@@ -0,0 +1,10 @@
1
+ export class DtiClientRoute {
2
+ constructor(meta, builder) {
3
+ this.meta = meta;
4
+ this.builder = builder;
5
+ }
6
+ buildUrl(action) {
7
+ let base = this.builder.getBaseUrl(this.meta.getFullname()) || '';
8
+ return base + this.meta.getPaths().join('') + action;
9
+ }
10
+ }
package/esm/tree.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ export interface ITreeNamer<T> {
2
+ name: string;
3
+ parent?: string;
4
+ value?: T;
5
+ childs?: ITreeNamer<T>[];
6
+ }
7
+ export declare class TreeNamer<T> {
8
+ private name;
9
+ private parent?;
10
+ constructor(name: string, parent?: TreeNamer<T> | undefined);
11
+ private value?;
12
+ private childs?;
13
+ set(name: string, value: T): this;
14
+ findEqual(name: string): TreeNamer<T> | undefined;
15
+ findParent(name: string): TreeNamer<T>;
16
+ getValue(): T | undefined;
17
+ private getOrCreate;
18
+ private toObj;
19
+ print(): string;
20
+ }
package/esm/tree.js ADDED
@@ -0,0 +1,84 @@
1
+ export class TreeNamer {
2
+ constructor(name, parent) {
3
+ this.name = name;
4
+ this.parent = parent;
5
+ }
6
+ set(name, value) {
7
+ let names = (name || '').split('.').filter(it => !!it);
8
+ let curr = this;
9
+ while (names.length > 0) {
10
+ let name = names.shift() || '';
11
+ curr = curr.getOrCreate(name);
12
+ }
13
+ curr.value = value;
14
+ return this;
15
+ }
16
+ findEqual(name) {
17
+ let names = (name || '').split('.').filter(it => !!it);
18
+ let curr = this;
19
+ while (names.length > 0) {
20
+ let name = names.shift() || '';
21
+ if (curr.childs) {
22
+ if (name in curr.childs) {
23
+ curr = curr.childs[name];
24
+ }
25
+ else {
26
+ return undefined;
27
+ }
28
+ }
29
+ }
30
+ return curr;
31
+ }
32
+ findParent(name) {
33
+ let names = (name || '').split('.').filter(it => !!it);
34
+ let curr = this;
35
+ while (names.length > 0) {
36
+ let name = names.shift() || '';
37
+ if (curr.childs) {
38
+ if (name in curr.childs) {
39
+ curr = curr.childs[name];
40
+ }
41
+ else {
42
+ return curr;
43
+ }
44
+ }
45
+ }
46
+ return curr;
47
+ }
48
+ getValue() {
49
+ if (this.value) {
50
+ return this.value;
51
+ }
52
+ if (this.parent) {
53
+ return this.parent.getValue();
54
+ }
55
+ return undefined;
56
+ }
57
+ getOrCreate(name) {
58
+ this.childs = this.childs || {};
59
+ if (name in this.childs) {
60
+ return this.childs[name];
61
+ }
62
+ return this.childs[name] = new TreeNamer(name, this);
63
+ }
64
+ toObj() {
65
+ let obj = {
66
+ name: this.name,
67
+ parent: this.parent?.name,
68
+ value: this.value
69
+ };
70
+ if (this.childs) {
71
+ obj.childs = [];
72
+ for (let p of Object.keys(this.childs)) {
73
+ let it = this.childs[p];
74
+ if (it && it.name && typeof it.toObj === 'function' && typeof it.getOrCreate === 'function') {
75
+ obj.childs.push(it.toObj());
76
+ }
77
+ }
78
+ }
79
+ return obj;
80
+ }
81
+ print() {
82
+ return JSON.stringify(this.toObj(), null, 4);
83
+ }
84
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@napp/dti-client",
3
- "version": "4.4.4",
3
+ "version": "4.4.5",
4
4
  "description": "data transaction interface client library",
5
5
  "repository": {
6
6
  "type": "git",
@@ -8,6 +8,14 @@
8
8
  },
9
9
  "types": "index.d.ts",
10
10
  "main": "index.js",
11
+ "module": "esm/index.js",
12
+ "exports": {
13
+ ".": {
14
+ "require": "./index.js",
15
+ "import": "./esm/index.js",
16
+ "types": "./index.d.ts"
17
+ }
18
+ },
11
19
  "keywords": [
12
20
  "napp",
13
21
  "Rest API",
@@ -16,7 +24,7 @@
16
24
  "author": "Farcek <farcek@gmail.com>",
17
25
  "license": "ISC",
18
26
  "dependencies": {
19
- "@napp/dti-core": "4.4.4"
27
+ "@napp/dti-core": "4.4.5"
20
28
  },
21
29
  "peerDependencies": {
22
30
  "@napp/exception": "^9.1.4",