@betterstore/sdk 0.7.1 → 0.7.2

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
@@ -26,27 +26,35 @@ __export(index_exports, {
26
26
  module.exports = __toCommonJS(index_exports);
27
27
 
28
28
  // src/base-client.ts
29
+ var DEFAULT_BASE_URL = "https://v1.betterstore.io";
29
30
  var BaseClient = class {
30
31
  constructor(config) {
31
32
  this.config = config;
33
+ this.headers = {
34
+ "Content-Type": "application/json",
35
+ Authorization: `Bearer ${this.config.secret}`
36
+ };
32
37
  }
38
+ headers;
33
39
  async get(path, params) {
34
- const url = new URL(path, this.config.baseUrl);
40
+ const url = `${this.config.baseUrl}${path}`;
41
+ const searchParams = new URLSearchParams();
35
42
  if (params) {
36
43
  Object.entries(params).forEach(([key, val]) => {
37
- if (val !== void 0) url.searchParams.set(key, String(val));
44
+ if (val !== void 0) searchParams.set(key, val);
38
45
  });
39
46
  }
40
- const res = await fetch(url.toString(), {
47
+ console.log(`${url}?${searchParams.toString()}`);
48
+ const res = await fetch(`${url}?${searchParams.toString()}`, {
41
49
  method: "GET",
42
- headers: this.config.headers
50
+ headers: this.headers
43
51
  });
44
52
  return this.handleResponse(res);
45
53
  }
46
54
  async post(path, body) {
47
55
  const res = await fetch(`${this.config.baseUrl}${path}`, {
48
56
  method: "POST",
49
- headers: this.config.headers,
57
+ headers: this.headers,
50
58
  body: body ? JSON.stringify(body) : void 0
51
59
  });
52
60
  return this.handleResponse(res);
@@ -54,13 +62,14 @@ var BaseClient = class {
54
62
  async delete(path) {
55
63
  const res = await fetch(`${this.config.baseUrl}${path}`, {
56
64
  method: "DELETE",
57
- headers: this.config.headers
65
+ headers: this.headers
58
66
  });
59
67
  return this.handleResponse(res);
60
68
  }
61
69
  async handleResponse(res) {
62
70
  if (!res.ok) {
63
- const error = await res.json().catch(() => ({ message: res.statusText }));
71
+ const error = await res.json().catch(() => ({ message: res.statusText, data: res.body }));
72
+ console.log(error);
64
73
  throw new Error(error.message || `HTTP ${res.status}`);
65
74
  }
66
75
  return res.json();
@@ -157,13 +166,10 @@ var ClientUtilsNamespace = class {
157
166
  };
158
167
 
159
168
  // src/client-factory.ts
160
- function createBetterStoreClient(clientSecret, config) {
169
+ function createBetterStoreClient(checkoutSecret, config) {
161
170
  const baseClient = new BaseClient({
162
- baseUrl: config?.proxy || "https://api.betterstore.com",
163
- headers: {
164
- Authorization: `Bearer ${clientSecret}`,
165
- "Content-Type": "application/json"
166
- }
171
+ baseUrl: config?.proxy || DEFAULT_BASE_URL,
172
+ secret: checkoutSecret
167
173
  });
168
174
  return {
169
175
  checkout: new ClientCheckoutNamespace(baseClient),
@@ -338,11 +344,8 @@ var UtilsNamespace = class {
338
344
  // src/main-factory.ts
339
345
  function createBetterStore(secret, config) {
340
346
  const baseClient = new BaseClient({
341
- baseUrl: config?.proxy || "https://v1.betterstore.io",
342
- headers: {
343
- Authorization: `Bearer ${secret}`,
344
- "Content-Type": "application/json"
345
- }
347
+ baseUrl: config?.proxy || DEFAULT_BASE_URL,
348
+ secret
346
349
  });
347
350
  return {
348
351
  products: new ProductsNamespace(baseClient),
package/dist/index.mjs CHANGED
@@ -1,25 +1,33 @@
1
1
  // src/base-client.ts
2
+ var DEFAULT_BASE_URL = "https://v1.betterstore.io";
2
3
  var BaseClient = class {
3
4
  constructor(config) {
4
5
  this.config = config;
6
+ this.headers = {
7
+ "Content-Type": "application/json",
8
+ Authorization: `Bearer ${this.config.secret}`
9
+ };
5
10
  }
11
+ headers;
6
12
  async get(path, params) {
7
- const url = new URL(path, this.config.baseUrl);
13
+ const url = `${this.config.baseUrl}${path}`;
14
+ const searchParams = new URLSearchParams();
8
15
  if (params) {
9
16
  Object.entries(params).forEach(([key, val]) => {
10
- if (val !== void 0) url.searchParams.set(key, String(val));
17
+ if (val !== void 0) searchParams.set(key, val);
11
18
  });
12
19
  }
13
- const res = await fetch(url.toString(), {
20
+ console.log(`${url}?${searchParams.toString()}`);
21
+ const res = await fetch(`${url}?${searchParams.toString()}`, {
14
22
  method: "GET",
15
- headers: this.config.headers
23
+ headers: this.headers
16
24
  });
17
25
  return this.handleResponse(res);
18
26
  }
19
27
  async post(path, body) {
20
28
  const res = await fetch(`${this.config.baseUrl}${path}`, {
21
29
  method: "POST",
22
- headers: this.config.headers,
30
+ headers: this.headers,
23
31
  body: body ? JSON.stringify(body) : void 0
24
32
  });
25
33
  return this.handleResponse(res);
@@ -27,13 +35,14 @@ var BaseClient = class {
27
35
  async delete(path) {
28
36
  const res = await fetch(`${this.config.baseUrl}${path}`, {
29
37
  method: "DELETE",
30
- headers: this.config.headers
38
+ headers: this.headers
31
39
  });
32
40
  return this.handleResponse(res);
33
41
  }
34
42
  async handleResponse(res) {
35
43
  if (!res.ok) {
36
- const error = await res.json().catch(() => ({ message: res.statusText }));
44
+ const error = await res.json().catch(() => ({ message: res.statusText, data: res.body }));
45
+ console.log(error);
37
46
  throw new Error(error.message || `HTTP ${res.status}`);
38
47
  }
39
48
  return res.json();
@@ -130,13 +139,10 @@ var ClientUtilsNamespace = class {
130
139
  };
131
140
 
132
141
  // src/client-factory.ts
133
- function createBetterStoreClient(clientSecret, config) {
142
+ function createBetterStoreClient(checkoutSecret, config) {
134
143
  const baseClient = new BaseClient({
135
- baseUrl: config?.proxy || "https://api.betterstore.com",
136
- headers: {
137
- Authorization: `Bearer ${clientSecret}`,
138
- "Content-Type": "application/json"
139
- }
144
+ baseUrl: config?.proxy || DEFAULT_BASE_URL,
145
+ secret: checkoutSecret
140
146
  });
141
147
  return {
142
148
  checkout: new ClientCheckoutNamespace(baseClient),
@@ -311,11 +317,8 @@ var UtilsNamespace = class {
311
317
  // src/main-factory.ts
312
318
  function createBetterStore(secret, config) {
313
319
  const baseClient = new BaseClient({
314
- baseUrl: config?.proxy || "https://v1.betterstore.io",
315
- headers: {
316
- Authorization: `Bearer ${secret}`,
317
- "Content-Type": "application/json"
318
- }
320
+ baseUrl: config?.proxy || DEFAULT_BASE_URL,
321
+ secret
319
322
  });
320
323
  return {
321
324
  products: new ProductsNamespace(baseClient),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@betterstore/sdk",
3
- "version": "0.7.1",
3
+ "version": "0.7.2",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",