@blocklet/payment-js 1.19.8 → 1.19.9

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/lib/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { ensureStart } from './resource';
1
2
  declare const _default: {
2
3
  checkout: {
3
4
  sessions: {
@@ -425,6 +426,8 @@ declare const _default: {
425
426
  message: string;
426
427
  }>;
427
428
  };
429
+ isPaymentRunning: () => boolean;
430
+ ensureStart: typeof ensureStart;
428
431
  };
429
432
  export default _default;
430
433
  export * from '@blocklet/payment-types';
package/lib/index.js CHANGED
@@ -66,5 +66,7 @@ exports.default = {
66
66
  environments: resource_1.environments,
67
67
  refunds: refund_1.default,
68
68
  settings: setting_1.default,
69
+ isPaymentRunning: resource_1.isPaymentRunning,
70
+ ensureStart: resource_1.ensureStart,
69
71
  };
70
72
  __exportStar(require("@blocklet/payment-types"), exports);
package/lib/resource.d.ts CHANGED
@@ -2,6 +2,7 @@ type MethodSpec = {
2
2
  method: 'GET' | 'POST' | 'PUT' | 'DELETE';
3
3
  path: string;
4
4
  };
5
+ export declare const isPaymentRunning: () => boolean;
5
6
  export declare const environments: {
6
7
  getLivemode: () => boolean;
7
8
  setLivemode: (value: boolean) => void;
@@ -11,4 +12,5 @@ export declare const environments: {
11
12
  export declare function ensureComponentRunning(): Promise<void>;
12
13
  export declare function createResourceMethod<ResponseType = any, DataType = any, ParamType = any>(spec: MethodSpec): (_params: ParamType, data?: DataType) => Promise<ResponseType>;
13
14
  export declare function createResourceCreateMethod<ResponseType = any, DataType = any, ParamType = any>(spec: MethodSpec): (data: DataType, params?: ParamType) => Promise<ResponseType>;
15
+ export declare function ensureStart(callback: () => void | Promise<void>, wait?: boolean): Promise<void>;
14
16
  export {};
package/lib/resource.js CHANGED
@@ -1,15 +1,46 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
27
  };
5
28
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.environments = void 0;
29
+ exports.environments = exports.isPaymentRunning = void 0;
7
30
  exports.ensureComponentRunning = ensureComponentRunning;
8
31
  exports.createResourceMethod = createResourceMethod;
9
32
  exports.createResourceCreateMethod = createResourceCreateMethod;
33
+ exports.ensureStart = ensureStart;
10
34
  /* eslint-disable import/prefer-default-export */
11
35
  const component_1 = __importDefault(require("@blocklet/sdk/lib/component"));
36
+ const config_1 = __importStar(require("@blocklet/sdk/lib/config"));
37
+ const notification_1 = __importDefault(require("@blocklet/sdk/lib/service/notification"));
38
+ const constant_1 = require("@blocklet/constant");
12
39
  const PAYMENT_KIT_DID = 'z2qaCNvKMv5GjouKdcDWexv6WqtHbpNPQDnAk';
40
+ const isPaymentRunning = () => {
41
+ return !!config_1.default.components.find((i) => i.did === PAYMENT_KIT_DID && i.status === constant_1.BlockletStatus.running);
42
+ };
43
+ exports.isPaymentRunning = isPaymentRunning;
13
44
  // global config
14
45
  let livemode = true;
15
46
  exports.environments = {
@@ -27,12 +58,18 @@ async function ensureComponentRunning() {
27
58
  if (running) {
28
59
  return;
29
60
  }
30
- await component_1.default.waitForComponentRunning(PAYMENT_KIT_DID);
31
- running = true;
61
+ running = (0, exports.isPaymentRunning)();
62
+ if (!running) {
63
+ await component_1.default.waitForComponentRunning(PAYMENT_KIT_DID);
64
+ running = true;
65
+ }
32
66
  }
33
67
  function createResourceMethod(spec) {
34
68
  return async (_params, data) => {
35
69
  await ensureComponentRunning();
70
+ if (!running) {
71
+ throw new Error('Payment Kit is not running. Please ensure the Payment Kit component is started.');
72
+ }
36
73
  const path = typeof _params === 'string' ? spec.path.replace('{id}', _params) : spec.path;
37
74
  const params = typeof _params === 'string' ? {} : _params;
38
75
  const result = await component_1.default.call({
@@ -48,6 +85,9 @@ function createResourceMethod(spec) {
48
85
  function createResourceCreateMethod(spec) {
49
86
  return async (data, params) => {
50
87
  await ensureComponentRunning();
88
+ if (!running) {
89
+ throw new Error('Payment Kit is not running. Please ensure the Payment Kit component is started.');
90
+ }
51
91
  const result = await component_1.default.call({
52
92
  name: PAYMENT_KIT_DID,
53
93
  method: spec.method,
@@ -58,3 +98,43 @@ function createResourceCreateMethod(spec) {
58
98
  return result.data;
59
99
  };
60
100
  }
101
+ function setupAsyncComponentListener(callback) {
102
+ return new Promise((resolve) => {
103
+ const checkAndExecute = async ({ components }) => {
104
+ if (components.find((i) => i.did === PAYMENT_KIT_DID)) {
105
+ running = true;
106
+ config_1.logger.info('Payment Kit component started via notification in ensureStart');
107
+ notification_1.default.off(constant_1.BlockletInternalEvents.componentStarted, checkAndExecute);
108
+ try {
109
+ await callback();
110
+ }
111
+ catch (error) {
112
+ config_1.logger.error('Error executing callback in ensureStart:', error);
113
+ }
114
+ finally {
115
+ resolve();
116
+ }
117
+ }
118
+ };
119
+ notification_1.default.on(constant_1.BlockletInternalEvents.componentStarted, checkAndExecute);
120
+ });
121
+ }
122
+ async function ensureStart(callback, wait = false) {
123
+ if (running) {
124
+ await callback();
125
+ return;
126
+ }
127
+ running = (0, exports.isPaymentRunning)();
128
+ if (running) {
129
+ await callback();
130
+ return;
131
+ }
132
+ if (wait) {
133
+ await component_1.default.waitForComponentRunning(PAYMENT_KIT_DID);
134
+ running = true;
135
+ await callback();
136
+ return;
137
+ }
138
+ config_1.logger.info('ensureStart: Payment Kit is not running, setting up async listener');
139
+ setupAsyncComponentListener(callback);
140
+ }
@@ -22,6 +22,7 @@ export interface UpdateMeterData {
22
22
  description?: string;
23
23
  status?: 'active' | 'inactive';
24
24
  metadata?: Record<string, any>;
25
+ unit?: string;
25
26
  }
26
27
  type ListParams = Pagination<{
27
28
  event_name?: string;
@@ -8,14 +8,14 @@ exports.default = {
8
8
  }),
9
9
  retrieve: (0, resource_1.createResourceMethod)({
10
10
  method: 'GET',
11
- path: '/api/settings/{mountLocationOrId}',
11
+ path: '/api/settings/{id}',
12
12
  }),
13
13
  update: (0, resource_1.createResourceMethod)({
14
14
  method: 'PUT',
15
- path: '/api/settings/{mountLocationOrId}',
15
+ path: '/api/settings/{id}',
16
16
  }),
17
17
  del: (0, resource_1.createResourceMethod)({
18
18
  method: 'DELETE',
19
- path: '/api/settings/{mountLocationOrId}',
19
+ path: '/api/settings/{id}',
20
20
  }),
21
21
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blocklet/payment-js",
3
- "version": "1.19.8",
3
+ "version": "1.19.9",
4
4
  "description": "Node.js client for Payment Kit",
5
5
  "keywords": [
6
6
  "types",
@@ -36,7 +36,8 @@
36
36
  "url": "https://github.com/blocklet/payment-kit/issues"
37
37
  },
38
38
  "dependencies": {
39
- "@blocklet/payment-types": "1.19.8",
39
+ "@blocklet/constant": "^1.16.46",
40
+ "@blocklet/payment-types": "1.19.9",
40
41
  "@blocklet/sdk": "^1.16.46"
41
42
  },
42
43
  "importSort": {
@@ -63,5 +64,5 @@
63
64
  "type-fest": "^4.41.0",
64
65
  "typescript": "5.5.4"
65
66
  },
66
- "gitHead": "59916e7b537b714eb9614365ca144704e7566ad8"
67
+ "gitHead": "a65b166cee114b432be7c1e95b8ef7bc88b30fbd"
67
68
  }