@oumla/sdk 0.0.3 → 0.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/dist/index.js CHANGED
@@ -63,74 +63,6 @@ __export(src_exports, {
63
63
  });
64
64
  module.exports = __toCommonJS(src_exports);
65
65
 
66
- // src/httpError.ts
67
- var HttpError = class extends Error {
68
- constructor(error) {
69
- super(error.message || "An HTTP error occurred");
70
- Object.setPrototypeOf(this, new.target.prototype);
71
- this.name = "HttpError";
72
- this.status = error.status || 502;
73
- this.success = error.success || false;
74
- this.path = error.path || "/";
75
- Error.captureStackTrace(this, this.constructor);
76
- }
77
- toString() {
78
- return `${this.name}: ${this.message} - Status: ${this.status} - Success: ${this.success}`;
79
- }
80
- };
81
-
82
- // src/base.ts
83
- var Base = class {
84
- constructor(configs) {
85
- this.apiKey = configs.apiKey;
86
- this.baseUrl = configs.baseUrl || "https://sandbox.oumla.com";
87
- this.env = configs.env || "testnet";
88
- }
89
- /**
90
- *
91
- * A generic method to make requests to the Oumla API.
92
- * Keep it simple for now, could be enhanced later.
93
- *
94
- */
95
- httpRequest(args) {
96
- return __async(this, null, function* () {
97
- var _a;
98
- if (args.body && args.schema) {
99
- this.parseInput(args.body, args.schema);
100
- }
101
- const paginationOpts = args.pagination ? `?skip=${args.pagination.skip || 0}&take=${args.pagination.take || 10}` : "";
102
- const URL2 = `${this.baseUrl}${args.path}${paginationOpts}`;
103
- const config = {
104
- headers: {
105
- "x-api-key": `Bearer ${this.apiKey}`,
106
- "Content-Type": "application/json"
107
- },
108
- method: args.method || void 0,
109
- // GET is the default in fetch
110
- body: args.body ? JSON.stringify(args.body) : void 0
111
- };
112
- const res = yield fetch(URL2, config);
113
- if (!res.ok) {
114
- if ((_a = res.headers.get("content-type")) == null ? void 0 : _a.includes("application/json")) {
115
- const errorResponse2 = yield res.json();
116
- throw new HttpError(errorResponse2);
117
- }
118
- const errorResponse = {
119
- message: yield res.text(),
120
- status: res.status,
121
- success: false,
122
- path: args.path
123
- };
124
- throw new HttpError(errorResponse);
125
- }
126
- return yield res.json();
127
- });
128
- }
129
- parseInput(input, schema) {
130
- schema.parse(input);
131
- }
132
- };
133
-
134
66
  // node_modules/.pnpm/zod@3.22.4/node_modules/zod/lib/index.mjs
135
67
  var util;
136
68
  (function(util2) {
@@ -3769,8 +3701,129 @@ var z = /* @__PURE__ */ Object.freeze({
3769
3701
  ZodError
3770
3702
  });
3771
3703
 
3704
+ // src/httpError.ts
3705
+ var HttpError = class extends Error {
3706
+ constructor(error) {
3707
+ super(error.message || "An HTTP error occurred");
3708
+ Object.setPrototypeOf(this, new.target.prototype);
3709
+ this.name = "HttpError";
3710
+ this.status = error.status || 502;
3711
+ this.success = error.success || false;
3712
+ this.path = error.path || "/";
3713
+ Error.captureStackTrace(this, this.constructor);
3714
+ }
3715
+ toString() {
3716
+ return `${this.name}: ${this.message} - Status: ${this.status} - Success: ${this.success}`;
3717
+ }
3718
+ };
3719
+
3720
+ // src/base.ts
3721
+ var Base = class {
3722
+ constructor(configs) {
3723
+ this.apiKey = configs.apiKey;
3724
+ this.baseUrl = configs.baseUrl || "https://sandbox.oumla.com";
3725
+ this.env = configs.env || "testnet";
3726
+ }
3727
+ getCustomHeaders() {
3728
+ return {};
3729
+ }
3730
+ httpRequest(args) {
3731
+ return __async(this, null, function* () {
3732
+ try {
3733
+ if (args.body && args.schema) {
3734
+ this.parseInput(args.body, args.schema);
3735
+ }
3736
+ const paginationOpts = args.pagination ? `?skip=${args.pagination.skip || 0}&take=${args.pagination.take || 10}` : "";
3737
+ const URL2 = `${this.baseUrl}${args.path}${paginationOpts}`;
3738
+ const headers = __spreadValues(__spreadValues({
3739
+ "x-api-key": `Bearer ${this.apiKey}`,
3740
+ "Content-Type": "application/json"
3741
+ }, this.getCustomHeaders()), args.headers);
3742
+ const config = {
3743
+ headers,
3744
+ method: args.method || "GET",
3745
+ body: args.body ? JSON.stringify(args.body) : void 0
3746
+ };
3747
+ const res = yield fetch(URL2, config);
3748
+ if (!res.ok) {
3749
+ const structuredError = yield this.handleHttpError(res, args.path);
3750
+ throw new HttpError(structuredError);
3751
+ }
3752
+ return yield res.json();
3753
+ } catch (error) {
3754
+ if (error instanceof HttpError) {
3755
+ throw error;
3756
+ } else if (error instanceof z.ZodError) {
3757
+ throw this.createStructuredError(
3758
+ "ValidationError",
3759
+ "Input validation failed",
3760
+ 400,
3761
+ args.path,
3762
+ error.errors
3763
+ );
3764
+ } else {
3765
+ throw this.createStructuredError(
3766
+ "UnknownError",
3767
+ "An unexpected error occurred",
3768
+ 500,
3769
+ args.path,
3770
+ error
3771
+ );
3772
+ }
3773
+ }
3774
+ });
3775
+ }
3776
+ handleHttpError(res, path) {
3777
+ return __async(this, null, function* () {
3778
+ var _a;
3779
+ const status = res.status;
3780
+ let errorType;
3781
+ let message;
3782
+ let details;
3783
+ if ((_a = res.headers.get("content-type")) == null ? void 0 : _a.includes("application/json")) {
3784
+ const errorResponse = yield res.json();
3785
+ message = errorResponse.message;
3786
+ details = errorResponse;
3787
+ } else {
3788
+ message = yield res.text();
3789
+ }
3790
+ switch (status) {
3791
+ case 400:
3792
+ case 402:
3793
+ errorType = "ValidationError";
3794
+ break;
3795
+ case 401:
3796
+ case 403:
3797
+ errorType = "AuthenticationError";
3798
+ break;
3799
+ case 404:
3800
+ errorType = "NotFoundError";
3801
+ break;
3802
+ case 500:
3803
+ errorType = "ServerError";
3804
+ break;
3805
+ default:
3806
+ errorType = "UnknownError";
3807
+ }
3808
+ return this.createStructuredError(
3809
+ errorType,
3810
+ message,
3811
+ status,
3812
+ path,
3813
+ details
3814
+ );
3815
+ });
3816
+ }
3817
+ createStructuredError(type, message, status, path, details) {
3818
+ return { type, message, status, path, details };
3819
+ }
3820
+ parseInput(input, schema) {
3821
+ schema.parse(input);
3822
+ }
3823
+ };
3824
+
3772
3825
  // src/schemas.ts
3773
- var NetworkSchema = z.enum(["BTC", "ETH"]);
3826
+ var NetworkSchema = z.enum(["BTC", "ETH", "tBTC", "tETH"]);
3774
3827
  var ProfileTypeSchema = z.enum(["User", "Department", "Merchant"]);
3775
3828
  var GetWalletsSchema = z.object({
3776
3829
  reference: z.string()
@@ -3804,7 +3857,8 @@ var GetTransactionsSchema = z.object({
3804
3857
  }).partial();
3805
3858
  var GenerateAddressSchema = z.object({
3806
3859
  network: NetworkSchema,
3807
- reference: z.string()
3860
+ reference: z.string(),
3861
+ clientShare: z.string()
3808
3862
  });
3809
3863
  var GetAddressesSchema = z.object({
3810
3864
  baseUrl: z.string(),
@@ -3834,9 +3888,10 @@ var GetInsightsSchema = z.object({
3834
3888
  });
3835
3889
  var CreateTransactionSchema = z.object({
3836
3890
  to: z.string(),
3837
- amount: z.number(),
3891
+ amount: z.string(),
3838
3892
  from: z.array(z.string()),
3839
- network: NetworkSchema
3893
+ network: NetworkSchema,
3894
+ clientShare: z.string()
3840
3895
  });
3841
3896
  var GetOrganizationSchema = z.object({
3842
3897
  baseUrl: z.string(),
@@ -3848,335 +3903,155 @@ var GetOrganizationDashboardSchema = z.object({
3848
3903
  });
3849
3904
 
3850
3905
  // src/client.ts
3851
- var Oumla = class extends Base {
3852
- constructor(configs) {
3853
- super(configs);
3854
- }
3855
- /**
3856
- *
3857
- * Generate a wallet for a profile
3858
- *
3859
- * @param {string} args.reference - The reference of the profile
3860
- * @param {string} args.network - The network to generate a wallet for
3861
- *
3862
- * @example
3863
- * ``` ts
3864
- *
3865
- * const wallet = await client.generateWallet({
3866
- * reference: 'PROFILE_REFERENCE',
3867
- * network: 'BTC',
3868
- * });
3869
- *
3870
- * ```
3871
- *
3872
- * @returns {Types.TGenerateWalletResponse} - The generated wallet or an error
3873
- *
3874
- */
3875
- generateWallet(args) {
3876
- return __async(this, null, function* () {
3877
- return yield this.httpRequest({
3878
- method: "POST",
3879
- path: "/api/v1/wallets/generate",
3880
- body: args,
3881
- schema: GenerateWalletSchema
3882
- });
3906
+ var _Oumla = class extends Base {
3907
+ // 1 hour
3908
+ constructor(opts) {
3909
+ super({
3910
+ apiKey: opts.apiKey,
3911
+ baseUrl: opts.baseUrl,
3912
+ env: opts.env
3883
3913
  });
3884
- }
3885
- /**
3886
- *
3887
- * Get wallets for an organization
3888
- *
3889
- * @param {string} args.reference - The reference of the profile
3890
- * @param {Types.TPagination} [pagination] - Pagination options
3891
- *
3892
- * @example
3893
- * ``` ts
3894
- *
3895
- * const wallets = await client.getWallets({
3896
- * skip: 0,
3897
- * take: 3,
3898
- * });
3899
- *
3900
- * ```
3901
- *
3902
- * @returns {Types.TGetWalletsResponse} - The wallets for the organization
3903
- *
3904
- */
3905
- getWallets(args, pagination) {
3906
- return __async(this, null, function* () {
3907
- if (args == null ? void 0 : args.reference) {
3908
- return yield this.httpRequest({
3909
- path: `/api/v1/wallets/profile/${args.reference}`,
3914
+ // Update this with each release
3915
+ this.lastUpdateCheck = 0;
3916
+ this.UPDATE_CHECK_INTERVAL = 60 * 60 * 1e3;
3917
+ this.wallets = {
3918
+ generate: (args) => __async(this, null, function* () {
3919
+ return this.httpRequest({
3920
+ path: "/api/v1/wallets/generate",
3921
+ method: "POST",
3922
+ body: args,
3923
+ schema: GenerateWalletSchema
3924
+ });
3925
+ }),
3926
+ get: (args, pagination) => __async(this, null, function* () {
3927
+ const path = (args == null ? void 0 : args.reference) ? `/api/v1/wallets/profile/${args.reference}` : "/api/v1/wallets/organization";
3928
+ return this.httpRequest({
3929
+ path,
3930
+ method: "GET",
3910
3931
  pagination
3911
3932
  });
3912
- } else {
3913
- return yield this.httpRequest({
3914
- path: "/api/v1/wallets/organization",
3933
+ })
3934
+ };
3935
+ this.addresses = {
3936
+ generate: (args) => __async(this, null, function* () {
3937
+ return this.httpRequest({
3938
+ path: "/api/v1/address/generate",
3939
+ method: "POST",
3940
+ body: args,
3941
+ schema: GenerateAddressSchema
3942
+ });
3943
+ }),
3944
+ get: (args, pagination) => __async(this, null, function* () {
3945
+ const path = (args == null ? void 0 : args.reference) ? `/api/v1/addresses/${args.reference}` : "/api/v1/addresses/organization";
3946
+ return this.httpRequest({
3947
+ path,
3948
+ method: "GET",
3915
3949
  pagination
3916
3950
  });
3917
- }
3918
- });
3919
- }
3920
- /**
3921
- *
3922
- * Generate an address for a profile
3923
- *
3924
- * @param {string} args.reference - The reference of the profile
3925
- * @param {string} args.network - The network to generate an address for
3926
- *
3927
- * @example
3928
- * ``` ts
3929
- *
3930
- * const address = await client.generateAddress({
3931
- * reference: 'PROFILE_REFERENCE',
3932
- * network: 'BTC',
3933
- * });
3934
- *
3935
- * ```
3936
- *
3937
- * @returns {Types.TGenerateAddressResponse} - The generated address
3938
- */
3939
- generateAddress(args) {
3940
- return __async(this, null, function* () {
3941
- return yield this.httpRequest({
3942
- method: "POST",
3943
- path: "/api/v1/address/generate",
3944
- body: args,
3945
- schema: GenerateAddressSchema
3946
- });
3947
- });
3948
- }
3949
- /**
3950
- *
3951
- * Get addresses
3952
- *
3953
- * @param {string} args.reference - The reference of the profile, Optional
3954
- * @param {Types.TPagination} [pagination] - Pagination options
3955
- * @example
3956
- * ``` ts
3957
- *
3958
- * const addresses = await client.getAddresses({
3959
- * reference: 'PROFILE_REFERENCE',
3960
- * },
3961
- * {
3962
- * skip: 0,
3963
- * take: 3,
3964
- * }
3965
- * );
3966
- *
3967
- * ```
3968
- *
3969
- * @returns {Types.TGetProfileAddressesResponse} - The addresses for the profile
3970
- */
3971
- getAddresses(args, pagination) {
3972
- return __async(this, null, function* () {
3973
- if (args == null ? void 0 : args.reference) {
3974
- return yield this.httpRequest({
3975
- path: `/api/v1/addresses/${args.reference}`,
3951
+ })
3952
+ };
3953
+ this.profiles = {
3954
+ create: (args) => __async(this, null, function* () {
3955
+ return this.httpRequest({
3956
+ path: "/api/v1/profiles",
3957
+ method: "POST",
3958
+ body: args,
3959
+ schema: CreateProfileSchema
3960
+ });
3961
+ }),
3962
+ get: (pagination) => __async(this, null, function* () {
3963
+ return this.httpRequest({
3964
+ path: "/api/v1/profiles",
3965
+ method: "GET",
3976
3966
  pagination
3977
3967
  });
3978
- } else {
3979
- return yield this.httpRequest({
3980
- path: "/api/v1/addresses/organization",
3968
+ })
3969
+ };
3970
+ this.organization = {
3971
+ get: () => __async(this, null, function* () {
3972
+ return this.httpRequest({
3973
+ path: "/api/v1/organizations",
3974
+ method: "GET"
3975
+ });
3976
+ }),
3977
+ volume: () => __async(this, null, function* () {
3978
+ return this.httpRequest({
3979
+ path: "/api/v1/statistics/organization/volume",
3980
+ method: "GET"
3981
+ });
3982
+ }),
3983
+ insights: () => __async(this, null, function* () {
3984
+ return this.httpRequest({
3985
+ path: "/api/v1/statistics/organization/insights",
3986
+ method: "GET"
3987
+ });
3988
+ })
3989
+ };
3990
+ this.transactions = {
3991
+ create: (args) => __async(this, null, function* () {
3992
+ return this.httpRequest({
3993
+ path: "/api/v1/withdraw/address",
3994
+ method: "POST",
3995
+ body: args,
3996
+ schema: CreateTransactionSchema
3997
+ });
3998
+ }),
3999
+ get: (args, pagination) => __async(this, null, function* () {
4000
+ let path = "/api/v1/transactions/organization";
4001
+ if (args == null ? void 0 : args.address) {
4002
+ path = `/api/v1/transactions/address/${args.address}`;
4003
+ } else if (args == null ? void 0 : args.wallet) {
4004
+ path = `/api/v1/transactions/wallet/${args.wallet}`;
4005
+ } else if (args == null ? void 0 : args.reference) {
4006
+ path = `/api/v1/transactions/profile/${args.reference}`;
4007
+ }
4008
+ return this.httpRequest({
4009
+ path,
4010
+ method: "GET",
3981
4011
  pagination
3982
4012
  });
3983
- }
3984
- });
3985
- }
3986
- /**
3987
- *
3988
- * Get the organization's information
3989
- *
3990
- * @example
3991
- * ``` ts
3992
- *
3993
- * const organization = await client.getOrganization();
3994
- *
3995
- * ```
3996
- *
3997
- * @returns {Types.TGetOrganizationResponse} - The organization's information
3998
- */
3999
- getOrganization() {
4000
- return __async(this, null, function* () {
4001
- return yield this.httpRequest({
4002
- path: "/api/v1/organizations"
4003
- });
4004
- });
4005
- }
4006
- /**
4007
- *
4008
- * Create a profile
4009
- *
4010
- * @param {string} args.reference - The reference of the profile
4011
- * @param {Types.TCreateProfileArgs['type']} args.type - The type of the profile
4012
- *
4013
- * @example
4014
- * ``` ts
4015
- *
4016
- * const profile = await client.createProfile({
4017
- * reference: 'PROFILE_REFERENCE',
4018
- * type: 'User',
4019
- * });
4020
- *
4021
- * ```
4022
- *
4023
- * @returns {Types.TCreateProfileResponse} - The created profile
4024
- */
4025
- createProfile(args) {
4026
- return __async(this, null, function* () {
4027
- return yield this.httpRequest({
4028
- method: "POST",
4029
- path: "/api/v1/profiles",
4030
- body: args,
4031
- schema: CreateProfileSchema
4032
- });
4033
- });
4034
- }
4035
- /**
4036
- *
4037
- * Get the profiles for an organization
4038
- *
4039
- * @param {Types.TPagination} [pagination] - Pagination options
4040
- *
4041
- * @example
4042
- * ``` ts
4043
- *
4044
- * const profiles = await client.getProfiles({
4045
- * skip: 0,
4046
- * take: 3,
4047
- * });
4048
- *
4049
- * ```
4050
- *
4051
- * @returns {Types.TGetProfilesResponse} - The profiles for the organization
4052
- */
4053
- getProfiles(pagination) {
4054
- return __async(this, null, function* () {
4055
- return yield this.httpRequest({
4056
- path: "/api/v1/profiles",
4057
- pagination
4058
- });
4059
- });
4013
+ })
4014
+ };
4015
+ this.apiKey = opts.apiKey;
4016
+ this.baseUrl = opts.baseUrl || "https://api.oumla.com";
4017
+ this.env = opts.env || "testnet";
4018
+ this.initialize();
4060
4019
  }
4061
- /**
4062
- *
4063
- * Get the volume for an organization
4064
- *
4065
- * @example
4066
- * ``` ts
4067
- *
4068
- * const volume = await client.getVolume();
4069
- *
4070
- * ```
4071
- *
4072
- * @returns {Types.TGetVolumeResponse} - The volume of the organization
4073
- */
4074
- getVolume() {
4020
+ initialize() {
4075
4021
  return __async(this, null, function* () {
4076
- return yield this.httpRequest({
4077
- path: "/api/v1/statistics/organization/volume"
4078
- });
4022
+ yield this.checkForUpdates();
4079
4023
  });
4080
4024
  }
4081
- /**
4082
- *
4083
- * Get the insights for an organization
4084
- *
4085
- * @example
4086
- * ``` ts
4087
- *
4088
- * const insights = await client.getInsights();
4089
- *
4090
- * ```
4091
- *
4092
- * @returns {Types.TGetInsightsResponse} - The insights of the organization
4093
- */
4094
- getInsights() {
4095
- return __async(this, null, function* () {
4096
- return yield this.httpRequest({
4097
- path: "/api/v1/statistics/organization/insights"
4098
- });
4099
- });
4025
+ getCustomHeaders() {
4026
+ return {
4027
+ "x-sdk-version": _Oumla.CURRENT_VERSION
4028
+ };
4100
4029
  }
4101
- /**
4102
- *
4103
- * Get the all the transactions or transactions for an address, wallet or reference
4104
- *
4105
- * By providing more than one option, only one will be used.
4106
- *
4107
- * @param {string} [args.address] - The address to get transactions for
4108
- * @param {string} [args.wallet] - The wallet to get transactions for
4109
- * @param {string} [args.reference] - The reference to get transactions for
4110
- * @param {Types.TPagination} [pagination] - Pagination options
4111
- *
4112
- * @example
4113
- * ``` ts
4114
- *
4115
- * const transactionsByAddress = await client.getTransactions({
4116
- * address: 'ADDRESS',
4117
- * });
4118
- *
4119
- * ```
4120
- *
4121
- * @returns {Types.TGetTransactionsResponse} - The transactions
4122
- *
4123
- */
4124
- getTransactions(args, pagination) {
4030
+ checkForUpdates() {
4125
4031
  return __async(this, null, function* () {
4126
- if (args == null ? void 0 : args.address) {
4127
- return yield this.httpRequest({
4128
- path: `/api/v1/transactions/address/${args.address}`,
4129
- pagination
4130
- });
4131
- } else if (args == null ? void 0 : args.wallet) {
4132
- return yield this.httpRequest({
4133
- path: `/api/v1/transactions/wallet/${args.wallet}`,
4134
- pagination
4135
- });
4136
- } else if (args == null ? void 0 : args.reference) {
4137
- return yield this.httpRequest({
4138
- path: `/api/v1/transactions/profile/${args.reference}`,
4139
- pagination
4140
- });
4141
- } else {
4142
- return yield this.httpRequest({
4143
- path: "/api/v1/transactions/organization",
4144
- pagination
4032
+ const now = Date.now();
4033
+ if (now - this.lastUpdateCheck < this.UPDATE_CHECK_INTERVAL) {
4034
+ return;
4035
+ }
4036
+ try {
4037
+ const response = yield this.httpRequest({
4038
+ path: "/api/v1/sdk-version",
4039
+ method: "GET"
4145
4040
  });
4041
+ if (response.latestVersion !== _Oumla.CURRENT_VERSION) {
4042
+ console.log(
4043
+ `A new version (${response.latestVersion}) of the Oumla SDK is available. Please update.`
4044
+ );
4045
+ }
4046
+ this.lastUpdateCheck = now;
4047
+ } catch (error) {
4048
+ console.error("Failed to check for updates:", error);
4146
4049
  }
4147
4050
  });
4148
4051
  }
4149
- /**
4150
- * create transaction
4151
- *
4152
- * @param args.from - the address or addresses to transfer from
4153
- * @param args.to - The desired address to transfer to
4154
- * @param args.amount - The amount to transfer
4155
- *
4156
- * @example
4157
- *
4158
- * ``` ts
4159
- *
4160
- * const transfer = await client.transfer({
4161
- * from: '0x...',
4162
- * to: '0x...',
4163
- * reference: '0x...',
4164
- * amount: 100,
4165
- * });
4166
- *
4167
- * ```
4168
- */
4169
- createTransaction(args) {
4170
- return __async(this, null, function* () {
4171
- return yield this.httpRequest({
4172
- method: "POST",
4173
- path: "/api/v1/withdraw/address",
4174
- body: args,
4175
- schema: CreateTransactionSchema
4176
- });
4177
- });
4178
- }
4179
4052
  };
4053
+ var Oumla = _Oumla;
4054
+ Oumla.CURRENT_VERSION = "1.0.0";
4180
4055
 
4181
4056
  // src/types.ts
4182
4057
  var ProfileType = /* @__PURE__ */ ((ProfileType2) => {