monime-package 1.0.4 → 1.0.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.
package/README.md CHANGED
@@ -1,8 +1,10 @@
1
1
 
2
2
  # monime-package
3
3
 
4
- A **TypeScript SDK for Monime**, providing a lightweight, typed client for common Monime endpoints. Simplifies requests, headers, and error handling so your code stays clean and type-safe.
4
+ Official, lightweight TypeScript SDK for Monime. It provides a typed client for common Monime endpoints with consistent results and simple ergonomics.
5
5
 
6
+ ![npm version](https://img.shields.io/npm/v/monime-package.svg)
7
+ ![npm downloads](https://img.shields.io/npm/dm/monime-package.svg)
6
8
  ![TypeScript](https://img.shields.io/badge/TypeScript-4.9-blue.svg)
7
9
  ![Node.js](https://img.shields.io/badge/Node.js-%3E=14-green.svg)
8
10
  ![License](https://img.shields.io/badge/license-MIT-lightgrey.svg)
@@ -13,27 +15,33 @@ Package: `monime-package`
13
15
 
14
16
  ## Table of Contents
15
17
 
16
- * [Features](#features)
17
- * [Installation](#installation)
18
- * [Environment Variables](#environment-variables)
19
- * [Quick Start](#quick-start)
20
- * [Client API](#client-api)
21
- * [Examples](#examples)
22
- * [Migration from Old Helpers](#migration-from-old-helpers)
23
- * [Folder Structure](#folder-structure)
24
- * [Error Handling](#error-handling)
25
- * [Contributing](#contributing)
26
- * [License](#license)
18
+ - **[Features](#features)**
19
+ - **[Installation](#installation)**
20
+ - **[Environment Variables](#environment-variables)**
21
+ - **[Quick Start](#quick-start)**
22
+ - **[Client API](#client-api)**
23
+ - **[Configuration](#configuration)**
24
+ - **[Examples](#examples)**
25
+ - **[Idempotency](#idempotency)**
26
+ - **[Pagination](#pagination)**
27
+ - **[Migration from Old Helpers](#migration-from-old-helpers)**
28
+ - **[Folder Structure](#folder-structure)**
29
+ - **[Error Handling](#error-handling)**
30
+ - **[Security](#security)**
31
+ - **[Versioning](#versioning)**
32
+ - **[Support](#support)**
33
+ - **[Return Types (Appendix)](#return-types-appendix)**
34
+ - **[Contributing](#contributing)**
35
+ - **[License](#license)**
27
36
 
28
37
  ---
29
38
 
30
39
  ## Features
31
40
 
32
- * Full **TypeScript support** with typed request/response objects
33
- * Lightweight client wrapper around Monime endpoints
34
- * Predictable return shape: `{ success: boolean, data?, error? }`
35
- * Single client instance handles credentials automatically
36
- * Minimal dependencies (`axios` only)
41
+ - **Typed** request/response objects for safer integrations
42
+ - **Predictable** return shape: `{ success, data?, error? }`
43
+ - **Client-based** auth: set credentials once per instance
44
+ - **Minimal deps** (`axios`) and small surface area
37
45
 
38
46
  ---
39
47
 
@@ -65,72 +73,118 @@ You can also pass credentials directly when creating the client.
65
73
  ### Create a client
66
74
 
67
75
  ```ts
68
- import { createClient } from 'monime-package'
76
+ import { createClient } from "monime-package";
69
77
 
70
78
  const client = createClient({
71
- monimeSpaceeId: process.env.MONIME_SPACE_ID!,
79
+ monimeSpaceId: process.env.MONIME_SPACE_ID!,
72
80
  accessToken: process.env.MONIME_ACCESS_TOKEN!,
73
- })
81
+ });
74
82
  ```
75
83
 
76
- Now all methods automatically use the client’s credentials.
84
+ Now all methods use the client’s credentials automatically.
85
+
86
+ ### Import styles
87
+
88
+ ```ts
89
+ // ESM / TypeScript
90
+ import { createClient, type DestinationOption } from "monime-package";
91
+
92
+ // CommonJS
93
+ // const { createClient } = require("monime-package");
94
+ ```
77
95
 
78
96
  ---
79
97
 
80
98
  ## Client API
81
99
 
82
- All methods return:
100
+ All methods return the same envelope:
83
101
 
84
102
  ```ts
85
103
  type Result<T> = {
86
- success: boolean
87
- data?: T
88
- error?: Error
89
- }
104
+ success: boolean;
105
+ data?: T;
106
+ error?: Error;
107
+ };
90
108
  ```
91
109
 
92
- ### Financial Accounts
110
+ The client exposes namespaced APIs under `client.<module>`:
111
+
112
+ ### financialAccount
93
113
 
94
114
  ```ts
95
- client.createFinancialAccount(name: string): Promise<Result<CreateFinancialAccount>>
96
- client.getFinancialAccount(financialAccountId: string): Promise<Result<GetFinancialAccount>>
115
+ client.financialAccount.create(name: string): Promise<Result<CreateFinancialAccount>>
116
+ client.financialAccount.get(financialAccountId: string): Promise<Result<GetFinancialAccount>>
117
+ client.financialAccount.getAll(): Promise<Result<AllFinancialAccount>>
97
118
  ```
98
119
 
99
- ### Internal Transfers
120
+ ### internalTransfer
100
121
 
101
122
  ```ts
102
- client.createInternalTransfer(
123
+ client.internalTransfer.create(
103
124
  sourceAccount: string,
104
125
  destinationAccount: string,
105
- amount: number
126
+ amount: number,
106
127
  ): Promise<Result<CreateInternalTransfer>>
128
+
129
+ client.internalTransfer.get(internalTransferId: string): Promise<Result<InternalTransfer>>
130
+ client.internalTransfer.getAll(): Promise<Result<AllInternalTransfers>>
131
+ client.internalTransfer.delete(internalTransferId: string): Promise<{ success: boolean; error?: Error }>
107
132
  ```
108
133
 
109
- ### Payment Codes
134
+ ### paymentCode
110
135
 
111
136
  ```ts
112
- client.createPaymentCode(
137
+ client.paymentCode.create(
113
138
  paymentName: string,
114
139
  amount: number,
115
140
  financialAccount: string,
116
141
  username: string,
117
- phoneNumber: string
142
+ phoneNumber: string,
118
143
  ): Promise<Result<CreatePaymentCode>>
119
144
 
120
- client.deletePaymentCode(paymentCodeId: string): Promise<Result<DeletePaymentCode>>
145
+ client.paymentCode.delete(paymentCodeId: string): Promise<{ success: boolean; error?: Error }>
146
+ client.paymentCode.getAll(): Promise<Result<GetAllPaymentCode>>
147
+ client.paymentCode.get(paymentCodeId: string): Promise<Result<GetOne>>
121
148
  ```
122
149
 
123
- ### Payouts
150
+ ### payout
124
151
 
125
152
  ```ts
126
- client.createPayout(
153
+ import type { DestinationOption } from "monime-package";
154
+
155
+ client.payout.create(
127
156
  amount: number,
128
- phoneNumber: string,
129
- sourceAccount: string
157
+ destination: DestinationOption,
158
+ sourceAccount: string,
130
159
  ): Promise<Result<CreatePayout>>
160
+
161
+ client.payout.get(): Promise<Result<GetAll>>
162
+ client.payout.getOne(payoutId: string): Promise<Result<GetOnePayout>>
163
+ client.payout.delete(payoutId: string): Promise<{ success: boolean; error?: Error }>
164
+ ```
165
+
166
+ ### financialTransaction
167
+
168
+ ```ts
169
+ client.financialTransaction.get(transactionId: string): Promise<Result<GetTransaction>>
170
+ client.financialTransaction.getAll(): Promise<Result<AllTransaction>>
131
171
  ```
132
172
 
133
- > Provider is inferred automatically from the phone number prefix.
173
+ ---
174
+
175
+ ## Configuration
176
+
177
+ The client accepts the following options (see `src/client.ts`):
178
+
179
+ ```ts
180
+ type ClientOptions = {
181
+ monimeSpaceId: string; // Your Monime Space ID
182
+ accessToken: string; // Your Monime API token
183
+ };
184
+ ```
185
+
186
+ - **Authentication**: Both values are required. Prefer environment variables.
187
+ - **Headers**: SDK automatically sets `Authorization` and `Monime-Space-Id` for each call.
134
188
 
135
189
  ---
136
190
 
@@ -139,64 +193,118 @@ client.createPayout(
139
193
  ### Create a financial account
140
194
 
141
195
  ```ts
142
- const account = await client.createFinancialAccount('App Wallet')
143
- if (!account.success) throw account.error
144
- console.log(account.data)
196
+ const account = await client.financialAccount.create("App Wallet");
197
+ if (!account.success) throw account.error;
198
+ console.log(account.data);
145
199
  ```
146
200
 
147
201
  ### Get account details
148
202
 
149
203
  ```ts
150
- const details = await client.getFinancialAccount(account.data!.result.id)
151
- console.log(details)
204
+ const details = await client.financialAccount.get(account.data!.result.id);
205
+ console.log(details);
206
+ ```
207
+
208
+ ### List financial accounts
209
+
210
+ ```ts
211
+ const all = await client.financialAccount.getAll();
212
+ console.log(all.data?.result.length);
152
213
  ```
153
214
 
154
215
  ### Internal transfer
155
216
 
156
217
  ```ts
157
- const transfer = await client.createInternalTransfer('acc-src', 'acc-dest', 5000)
158
- if (!transfer.success) console.error(transfer.error)
159
- else console.log(transfer.data)
218
+ const transfer = await client.internalTransfer.create("acc-src", "acc-dest", 5000);
219
+ if (!transfer.success) console.error(transfer.error);
220
+ else console.log(transfer.data);
160
221
  ```
161
222
 
162
- ### Create payment code
223
+ ### Payment code lifecycle
163
224
 
164
225
  ```ts
165
- const code = await client.createPaymentCode('Order-001', 5, 'financial-account-id', 'Alice', '0771234567')
166
- if (!code.success) console.error(code.error)
167
- else console.log('Payment code:', code.data)
226
+ // create
227
+ const code = await client.paymentCode.create(
228
+ "Order-001",
229
+ 5,
230
+ "financial-account-id",
231
+ "Alice",
232
+ "0771234567",
233
+ );
234
+ if (!code.success) console.error(code.error);
235
+ else console.log("Payment code:", code.data);
236
+
237
+ // get
238
+ const one = await client.paymentCode.get("payment-code-id");
239
+
240
+ // list
241
+ const codes = await client.paymentCode.getAll();
242
+
243
+ // delete
244
+ const deleted = await client.paymentCode.delete("payment-code-id");
245
+ console.log(deleted.success);
168
246
  ```
169
247
 
170
- ### Delete payment code
248
+ ### Payouts
171
249
 
172
250
  ```ts
173
- const deleted = await client.deletePaymentCode('payment-code-id')
174
- console.log(deleted.success)
251
+ import type { DestinationOption } from "monime-package";
252
+
253
+ const destination: DestinationOption = {
254
+ type: "momo",
255
+ providerId: "m17", // eg. provider code
256
+ phoneNumber: "0771234567",
257
+ };
258
+
259
+ const payout = await client.payout.create(1000, destination, "source-account-id");
260
+ if (!payout.success) console.error(payout.error);
261
+ else console.log("Payout:", payout.data);
262
+
263
+ // list all payouts
264
+ const payouts = await client.payout.get();
265
+
266
+ // get one
267
+ const single = await client.payout.getOne("payout-id");
268
+
269
+ // delete
270
+ const removed = await client.payout.delete("payout-id");
175
271
  ```
176
272
 
177
- ### Payout (mobile money)
273
+ ### Financial transactions
178
274
 
179
275
  ```ts
180
- const payout = await client.createPayout(1000, '0771234567', 'source-account-id')
181
- if (!payout.success) console.error(payout.error)
182
- else console.log('Payout:', payout.data)
276
+ const tx = await client.financialTransaction.get("transaction-id");
277
+ const txs = await client.financialTransaction.getAll();
183
278
  ```
184
279
 
185
280
  ---
186
281
 
282
+ ## Idempotency
283
+
284
+ For POST endpoints, the SDK automatically attaches an `Idempotency-Key` header. This helps prevent duplicate requests if you retry the same call. Keys are generated per module instance.
285
+
286
+ ---
287
+
288
+ ## Pagination
289
+
290
+ List endpoints return a `pagination` object in `data?.pagination` when available, including a `next` cursor/URL. The SDK currently returns this information as-is; use the `next` value to fetch subsequent pages if needed.
291
+
292
+ ---
293
+
187
294
  ## Migration from Old Helpers
188
295
 
189
- Previously, you had to pass `monimeSpaceId` and `accessToken` into every function:
296
+ Previously, you called standalone helpers and passed credentials on every call:
190
297
 
191
298
  ```ts
192
- createFinancialAccount('name', MONIME_SPACE_ID, MONIME_ACCESS_TOKEN)
299
+ // old (example)
300
+ createFinancialAccount("name", MONIME_SPACE_ID, MONIME_ACCESS_TOKEN);
193
301
  ```
194
302
 
195
- Now, create a client once:
303
+ Now, instantiate a client once and use namespaced methods. Credentials are stored internally:
196
304
 
197
305
  ```ts
198
- const client = createClient({ monimeSpaceeId, accessToken })
199
- client.createFinancialAccount('name') // credentials auto-applied
306
+ const client = createClient({ monimeSpaceId, accessToken });
307
+ await client.financialAccount.create("name");
200
308
  ```
201
309
 
202
310
  ---
@@ -211,24 +319,116 @@ client.createFinancialAccount('name') // credentials auto-applied
211
319
  ├── tsconfig.json
212
320
  ├── tsup.config.ts
213
321
  ├── src
214
- │ ├── index.ts # entry point
215
- └── modules
216
- ├── financialAccount.ts
217
- │ ├── financialAccountTypes.ts
218
- │ ├── internalTransfer.ts
219
- ├── internalTransferTypes.ts
220
- │ ├── paymentCode.ts
221
- │ ├── paymentCodeTypes.ts
222
- ├── payout.ts
223
- └── payoutTypes.ts
322
+ │ ├── index.ts # entry point (exports createClient, types)
323
+ ├── client.ts # MonimeClient with namespaced modules
324
+ └── modules/
325
+ │ ├── financialAccount/
326
+ ├── financialAccount.ts
327
+ │ └── index.ts
328
+ │ ├── financialTransaction/
329
+ ├── financialTransaction.ts
330
+ │ └── index.ts
331
+ ├── internalTransfer/
332
+ │ │ ├── internalTransfer.ts
333
+ │ │ └── index.ts
334
+ │ ├── paymentCode/
335
+ │ │ ├── paymentCode.ts
336
+ │ │ └── index.ts
337
+ │ ├── payout/
338
+ │ │ ├── payout.ts
339
+ │ │ └── index.ts
340
+ │ └── types.ts
224
341
  ```
225
342
 
226
343
  ---
227
344
 
228
345
  ## Error Handling
229
346
 
230
- * Network/API errors are returned as `error` in the `{ success, data?, error? }` object.
231
- * Use `axios.isAxiosError(error)` to inspect response details if needed.
347
+ - **Standard envelope**: every call returns `{ success, data?, error? }`.
348
+ - **Validation**: inputs are validated (e.g. non-empty IDs, positive amounts) and will short-circuit with `success: false` + `Error`.
349
+ - **Axios errors**: when available, `axios` errors are propagated. You can check details with `axios.isAxiosError(error)`.
350
+
351
+ ---
352
+
353
+ ## Security
354
+
355
+ - Do not commit tokens. Use environment variables or a secret manager.
356
+ - Restrict tokens to the least privileges necessary.
357
+
358
+ ---
359
+
360
+ ## Versioning
361
+
362
+ This project follows semantic versioning (SemVer). See releases for notable changes.
363
+
364
+ ---
365
+
366
+ ## Support
367
+
368
+ - File issues and feature requests at the repository: `https://github.com/Walon-Foundation/monime-package/issues`.
369
+ - For production incidents, rotate credentials if you suspect exposure.
370
+
371
+ ---
372
+
373
+ ## Return Types (Appendix)
374
+
375
+ All result payload types are exported from the package, so you can import them directly:
376
+
377
+ ```ts
378
+ import type {
379
+ // Financial Account
380
+ CreateFinancialAccount,
381
+ GetFinancialAccount,
382
+ AllFinancialAccount,
383
+
384
+ // Internal Transfer
385
+ CreateInternalTransfer,
386
+ InternalTransfer,
387
+ AllInternalTransfers,
388
+
389
+ // Payment Code
390
+ CreatePaymentCode,
391
+ GetAllPaymentCode,
392
+ GetOne,
393
+
394
+ // Payout
395
+ DestinationOption,
396
+ CreatePayout,
397
+ GetAll,
398
+ GetOnePayout,
399
+
400
+ // Financial Transaction
401
+ GetTransaction,
402
+ AllTransaction,
403
+ } from "monime-package";
404
+ ```
405
+
406
+ - **financialAccount**
407
+ - `CreateFinancialAccount`: `{ success, messages, result: { id, name, currency, balance, ... } }`
408
+ - `GetFinancialAccount`: same `result` shape as above
409
+ - `AllFinancialAccount`: `{ result: Result[], pagination: { count, next } }`
410
+
411
+ - **internalTransfer**
412
+ - `CreateInternalTransfer`: `{ result: { id, status, amount, sourceFinancialAccount, destinationFinancialAccount, ... } }`
413
+ - `InternalTransfer`: same `result` shape for a single transfer
414
+ - `AllInternalTransfers`: `{ result: Result[], pagination }`
415
+
416
+ - **paymentCode**
417
+ - `CreatePaymentCode`: created payment code details
418
+ - `GetAllPaymentCode`: list of codes with pagination
419
+ - `GetOne`: details for a specific payment code
420
+
421
+ - **payout**
422
+ - `DestinationOption`: union for destination types (`momo`, `bank`, `wallet`)
423
+ - `CreatePayout`: created payout info with `fees`, `destination`, etc.
424
+ - `GetAll`: list of payouts with pagination
425
+ - `GetOnePayout`: details for a specific payout
426
+
427
+ - **financialTransaction**
428
+ - `GetTransaction`: details for a transaction (amount, reference, ownershipGraph, ...)
429
+ - `AllTransaction`: list of transactions with pagination
430
+
431
+ These types mirror Monime API responses and are kept minimal and typed for convenience.
232
432
 
233
433
  ---
234
434