imt-bridge-api-client-ts 0.0.28

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,17 @@
1
+ interface BridgeApiConfig {
2
+ headers: {
3
+ [key: string]: string;
4
+ };
5
+ timeout: number;
6
+ }
7
+ export declare class BridgeApi {
8
+ config: BridgeApiConfig;
9
+ private constructor();
10
+ static create(config?: BridgeApiConfig): BridgeApi;
11
+ get<ResT>(pathAndQuery: string, body?: any, timeout?: number): Promise<ResT>;
12
+ post<ResT>(pathAndQuery: string, body?: any, timeout?: number): Promise<ResT>;
13
+ put<ResT>(pathAndQuery: string, body?: any, timeout?: number): Promise<ResT>;
14
+ delete<ResT>(pathAndQuery: string, body?: any, timeout?: number): Promise<ResT>;
15
+ patch<ResT>(pathAndQuery: string, body?: any, timeout?: number): Promise<ResT>;
16
+ }
17
+ export {};
@@ -0,0 +1,116 @@
1
+ import { MethodType } from "./enums/MethodType";
2
+ const promiseMap = new Map();
3
+ const generateUniqueCallbackId = () => {
4
+ // 현재 시간을 밀리초 단위로 가져온 후, 16진수 문자열로 변환
5
+ const timestamp = Date.now().toString(16);
6
+ // 무작위 수를 생성하고, 이를 16진수 문자열로 변환
7
+ const random = Math.floor(Math.random() * 0xffff).toString(16);
8
+ // 두 값을 결합하여 유니크한 ID 생성
9
+ return `promise_${timestamp}_${random}`;
10
+ };
11
+ const callBridgeApiFunction = async (apiCommonRequest, timeout = 5000) => {
12
+ const promiseId = generateUniqueCallbackId();
13
+ return new Promise((resolve, reject) => {
14
+ const timeoutId = setTimeout(() => {
15
+ promiseMap.delete(promiseId);
16
+ reject(new Error(`Timeout after ${timeout}ms for request: ${apiCommonRequest.body}`));
17
+ }, timeout);
18
+ try {
19
+ console.log(`Calling bridge api promiseId: ${promiseId}, request: `, apiCommonRequest);
20
+ const jsonString = JSON.stringify(apiCommonRequest);
21
+ window.BridgeApi.bridgeRequest(promiseId, jsonString);
22
+ promiseMap.set(promiseId, {
23
+ resolve: (result) => {
24
+ clearTimeout(timeoutId);
25
+ resolve(result);
26
+ },
27
+ reject: (e) => {
28
+ clearTimeout(timeoutId);
29
+ reject(e);
30
+ }
31
+ });
32
+ }
33
+ catch (error) {
34
+ clearTimeout(timeoutId);
35
+ promiseMap.delete(promiseId);
36
+ console.error(`Error calling bridge api(request: ${apiCommonRequest}):`, error);
37
+ reject(new Error(`Failed to call bridge api(request: ${apiCommonRequest.body})`));
38
+ }
39
+ });
40
+ };
41
+ window.resolveAsyncPromise = (id, result) => {
42
+ if (!promiseMap.has(id)) {
43
+ console.warn(`Promise with ID ${id} not found in promiseMap on executing callAndroidAsyncBridgeFunction`);
44
+ return;
45
+ }
46
+ const { resolve } = promiseMap.get(id);
47
+ const jsonResult = JSON.parse(JSON.stringify(result));
48
+ console.log(`Resolving promise with ID ${id} with response result:`, jsonResult);
49
+ resolve(jsonResult);
50
+ promiseMap.delete(id);
51
+ };
52
+ window.rejectAsyncPromise = (id, error) => {
53
+ if (!promiseMap.has(id)) {
54
+ console.warn(`Promise with ID ${id} not found in promiseMap on executing callAndroidAsyncBridgeFunction`);
55
+ return;
56
+ }
57
+ const { reject } = promiseMap.get(id);
58
+ const errorJson = JSON.parse(JSON.stringify(error));
59
+ console.error(`Rejecting promise with ID ${id} with response error:`, errorJson);
60
+ reject(errorJson);
61
+ promiseMap.delete(id);
62
+ };
63
+ export class BridgeApi {
64
+ constructor(config) {
65
+ Object.defineProperty(this, "config", {
66
+ enumerable: true,
67
+ configurable: true,
68
+ writable: true,
69
+ value: void 0
70
+ });
71
+ this.config = config ?? { headers: {}, timeout: 5000 };
72
+ }
73
+ static create(config) {
74
+ return new BridgeApi(config);
75
+ }
76
+ get(pathAndQuery, body = '', timeout = this.config.timeout) {
77
+ return callBridgeApiFunction({
78
+ pathAndQuery: pathAndQuery,
79
+ method: MethodType.GET,
80
+ headers: this.config.headers,
81
+ body: body
82
+ }, timeout ?? this.config.timeout);
83
+ }
84
+ post(pathAndQuery, body = '', timeout = this.config.timeout) {
85
+ return callBridgeApiFunction({
86
+ pathAndQuery: pathAndQuery,
87
+ method: MethodType.POST,
88
+ headers: this.config.headers,
89
+ body: body,
90
+ }, timeout);
91
+ }
92
+ put(pathAndQuery, body = '', timeout = this.config.timeout) {
93
+ return callBridgeApiFunction({
94
+ pathAndQuery: pathAndQuery,
95
+ method: MethodType.PUT,
96
+ headers: this.config.headers,
97
+ body: body
98
+ }, timeout);
99
+ }
100
+ delete(pathAndQuery, body = '', timeout = this.config.timeout) {
101
+ return callBridgeApiFunction({
102
+ pathAndQuery: pathAndQuery,
103
+ method: MethodType.DELETE,
104
+ headers: this.config.headers,
105
+ body: body
106
+ }, timeout);
107
+ }
108
+ patch(pathAndQuery, body = '', timeout = this.config.timeout) {
109
+ return callBridgeApiFunction({
110
+ pathAndQuery: pathAndQuery,
111
+ method: MethodType.PATCH,
112
+ headers: this.config.headers,
113
+ body: body
114
+ }, timeout);
115
+ }
116
+ }
@@ -0,0 +1,7 @@
1
+ export declare enum MethodType {
2
+ GET = "GET",
3
+ POST = "POST",
4
+ PUT = "PUT",
5
+ DELETE = "DELETE",
6
+ PATCH = "PATCH"
7
+ }
@@ -0,0 +1,8 @@
1
+ export var MethodType;
2
+ (function (MethodType) {
3
+ MethodType["GET"] = "GET";
4
+ MethodType["POST"] = "POST";
5
+ MethodType["PUT"] = "PUT";
6
+ MethodType["DELETE"] = "DELETE";
7
+ MethodType["PATCH"] = "PATCH";
8
+ })(MethodType || (MethodType = {}));
@@ -0,0 +1,3 @@
1
+ export { BridgeApi } from "./BridgeApi";
2
+ export { MethodType } from "./enums/MethodType";
3
+ export type { ApiCommonRequest } from "./type/ApiCommonRequest";
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { BridgeApi } from "./BridgeApi";
2
+ export { MethodType } from "./enums/MethodType";
@@ -0,0 +1,9 @@
1
+ import { MethodType } from "../enums/MethodType";
2
+ export interface ApiCommonRequest<T> {
3
+ pathAndQuery: string;
4
+ method: MethodType;
5
+ headers: {
6
+ [key: string]: string;
7
+ };
8
+ body: T;
9
+ }
@@ -0,0 +1 @@
1
+ export {};
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "imt-bridge-api-client-ts",
3
+ "private": false,
4
+ "version": "0.0.28",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "scripts": {
12
+ "dev": "vite",
13
+ "build": "tsc",
14
+ "preview": "vite preview"
15
+ },
16
+ "devDependencies": {
17
+ "typescript": "^5.2.2",
18
+ "vite": "^5.3.1"
19
+ }
20
+ }