celitech-sdk 1.3.3 → 1.3.7

Sign up to get free protection for your applications and to get access to all the features.
package/README.md CHANGED
@@ -1,11 +1,11 @@
1
- # Celitech TypeScript SDK 1.3.3
1
+ # Celitech TypeScript SDK 1.3.7
2
2
 
3
3
  Welcome to the Celitech SDK documentation. This guide will help you get started with integrating and using the Celitech SDK in your project.
4
4
 
5
5
  ## Versions
6
6
 
7
7
  - API version: `1.1.0`
8
- - SDK version: `1.3.3`
8
+ - SDK version: `1.3.7`
9
9
 
10
10
  ## About the API
11
11
 
package/dist/index.d.ts CHANGED
@@ -169,17 +169,11 @@ interface ValidationOptions {
169
169
  responseValidation?: boolean;
170
170
  }
171
171
 
172
- declare class HttpError extends Error {
173
- readonly error: string;
174
- readonly metadata: HttpMetadata;
175
- readonly raw?: ArrayBuffer;
176
- constructor(metadata: HttpMetadata, raw?: ArrayBuffer, error?: string);
177
- }
178
-
179
172
  declare class CustomHook implements Hook {
173
+ getToken(clientId: string, clientSecret: string): Promise<any>;
180
174
  beforeRequest(request: HttpRequest, params: Map<string, string>): Promise<HttpRequest>;
181
175
  afterResponse(request: HttpRequest, response: HttpResponse$1<any>, params: Map<string, string>): Promise<HttpResponse$1<any>>;
182
- onError(request: HttpRequest, response: HttpResponse$1<any>, params: Map<string, string>): Promise<HttpError>;
176
+ onError(request: HttpRequest, response: HttpResponse$1<any>, params: Map<string, string>): Promise<HttpError$1>;
183
177
  }
184
178
 
185
179
  declare class HttpClient {
@@ -1607,6 +1601,13 @@ declare const getEsimMacOkResponseEsim: z.ZodLazy<z.ZodObject<{
1607
1601
  */
1608
1602
  type GetEsimMacOkResponseEsim = z.infer<typeof getEsimMacOkResponseEsim>;
1609
1603
 
1604
+ declare class HttpError extends Error {
1605
+ readonly error: string;
1606
+ readonly metadata: HttpMetadata;
1607
+ readonly raw?: ArrayBuffer;
1608
+ constructor(metadata: HttpMetadata, raw?: ArrayBuffer, error?: string);
1609
+ }
1610
+
1610
1611
  declare class Celitech {
1611
1612
  config: SdkConfig;
1612
1613
  readonly oAuth: OAuthService;
package/dist/index.js CHANGED
@@ -60,26 +60,63 @@ var RequestHandlerChain = class {
60
60
  }
61
61
  };
62
62
 
63
- // src/http/error.ts
64
- var HttpError = class extends Error {
65
- constructor(metadata, raw, error) {
66
- super(error);
67
- this.error = metadata.statusText;
68
- this.metadata = metadata;
69
- this.raw = raw;
70
- }
71
- };
72
-
73
63
  // src/http/hooks/custom-hook.ts
64
+ var CURRENT_TOKEN = "";
65
+ var CURRENT_EXPIRY = -1;
74
66
  var CustomHook = class {
67
+ async getToken(clientId, clientSecret) {
68
+ const tokenUrl = "https://auth.celitech.net/oauth2/token";
69
+ const headers = {
70
+ "Content-Type": "application/x-www-form-urlencoded"
71
+ };
72
+ const body = {
73
+ client_id: clientId,
74
+ client_secret: clientSecret,
75
+ grant_type: "client_credentials"
76
+ };
77
+ const response = await fetch(tokenUrl, {
78
+ method: "POST",
79
+ headers,
80
+ body: new URLSearchParams(body)
81
+ });
82
+ return response.json();
83
+ }
75
84
  async beforeRequest(request, params) {
85
+ const clientId = params.get("clientId") || "";
86
+ const clientSecret = params.get("clientSecret") || "";
87
+ if (!clientId || !clientSecret) {
88
+ throw new Error("Missing clientId and/or clientSecret constructor parameters");
89
+ }
90
+ if (!CURRENT_TOKEN || CURRENT_EXPIRY < Date.now()) {
91
+ const tokenResponse = await this.getToken(clientId, clientSecret);
92
+ if (tokenResponse.error) {
93
+ throw new Error(tokenResponse.error);
94
+ }
95
+ const { expires_in, access_token } = tokenResponse;
96
+ if (!expires_in || !access_token) {
97
+ throw new Error("There is an issue with getting the oauth token");
98
+ }
99
+ CURRENT_EXPIRY = Date.now() + expires_in * 1e3;
100
+ CURRENT_TOKEN = access_token;
101
+ }
102
+ const authorization = `Bearer ${CURRENT_TOKEN}`;
103
+ if (!request.headers) {
104
+ request.headers = /* @__PURE__ */ new Map();
105
+ }
106
+ request.headers.set("Authorization", authorization);
76
107
  return request;
77
108
  }
78
109
  async afterResponse(request, response, params) {
79
110
  return response;
80
111
  }
81
112
  async onError(request, response, params) {
82
- return new HttpError(response.metadata, response.raw);
113
+ return new CustomHttpError("a custom error message", response.metadata);
114
+ }
115
+ };
116
+ var CustomHttpError = class {
117
+ constructor(error, metadata) {
118
+ this.error = error;
119
+ this.metadata = metadata;
83
120
  }
84
121
  };
85
122
 
@@ -484,6 +521,16 @@ var TerminatingHandler = class {
484
521
  }
485
522
  };
486
523
 
524
+ // src/http/error.ts
525
+ var HttpError = class extends Error {
526
+ constructor(metadata, raw, error) {
527
+ super(error);
528
+ this.error = metadata.statusText;
529
+ this.metadata = metadata;
530
+ this.raw = raw;
531
+ }
532
+ };
533
+
487
534
  // src/http/handlers/retry-handler.ts
488
535
  var RetryHandler = class {
489
536
  async handle(request) {
package/dist/index.mjs CHANGED
@@ -18,26 +18,63 @@ var RequestHandlerChain = class {
18
18
  }
19
19
  };
20
20
 
21
- // src/http/error.ts
22
- var HttpError = class extends Error {
23
- constructor(metadata, raw, error) {
24
- super(error);
25
- this.error = metadata.statusText;
26
- this.metadata = metadata;
27
- this.raw = raw;
28
- }
29
- };
30
-
31
21
  // src/http/hooks/custom-hook.ts
22
+ var CURRENT_TOKEN = "";
23
+ var CURRENT_EXPIRY = -1;
32
24
  var CustomHook = class {
25
+ async getToken(clientId, clientSecret) {
26
+ const tokenUrl = "https://auth.celitech.net/oauth2/token";
27
+ const headers = {
28
+ "Content-Type": "application/x-www-form-urlencoded"
29
+ };
30
+ const body = {
31
+ client_id: clientId,
32
+ client_secret: clientSecret,
33
+ grant_type: "client_credentials"
34
+ };
35
+ const response = await fetch(tokenUrl, {
36
+ method: "POST",
37
+ headers,
38
+ body: new URLSearchParams(body)
39
+ });
40
+ return response.json();
41
+ }
33
42
  async beforeRequest(request, params) {
43
+ const clientId = params.get("clientId") || "";
44
+ const clientSecret = params.get("clientSecret") || "";
45
+ if (!clientId || !clientSecret) {
46
+ throw new Error("Missing clientId and/or clientSecret constructor parameters");
47
+ }
48
+ if (!CURRENT_TOKEN || CURRENT_EXPIRY < Date.now()) {
49
+ const tokenResponse = await this.getToken(clientId, clientSecret);
50
+ if (tokenResponse.error) {
51
+ throw new Error(tokenResponse.error);
52
+ }
53
+ const { expires_in, access_token } = tokenResponse;
54
+ if (!expires_in || !access_token) {
55
+ throw new Error("There is an issue with getting the oauth token");
56
+ }
57
+ CURRENT_EXPIRY = Date.now() + expires_in * 1e3;
58
+ CURRENT_TOKEN = access_token;
59
+ }
60
+ const authorization = `Bearer ${CURRENT_TOKEN}`;
61
+ if (!request.headers) {
62
+ request.headers = /* @__PURE__ */ new Map();
63
+ }
64
+ request.headers.set("Authorization", authorization);
34
65
  return request;
35
66
  }
36
67
  async afterResponse(request, response, params) {
37
68
  return response;
38
69
  }
39
70
  async onError(request, response, params) {
40
- return new HttpError(response.metadata, response.raw);
71
+ return new CustomHttpError("a custom error message", response.metadata);
72
+ }
73
+ };
74
+ var CustomHttpError = class {
75
+ constructor(error, metadata) {
76
+ this.error = error;
77
+ this.metadata = metadata;
41
78
  }
42
79
  };
43
80
 
@@ -442,6 +479,16 @@ var TerminatingHandler = class {
442
479
  }
443
480
  };
444
481
 
482
+ // src/http/error.ts
483
+ var HttpError = class extends Error {
484
+ constructor(metadata, raw, error) {
485
+ super(error);
486
+ this.error = metadata.statusText;
487
+ this.metadata = metadata;
488
+ this.raw = raw;
489
+ }
490
+ };
491
+
445
492
  // src/http/handlers/retry-handler.ts
446
493
  var RetryHandler = class {
447
494
  async handle(request) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "celitech-sdk",
3
- "version": "1.3.3",
3
+ "version": "1.3.7",
4
4
  "description": "Welcome to the CELITECH API documentation! Useful links: [Homepage](https://www.celitech.com) | [Support email](mailto:support@celitech.com) | [Blog](https://www.celitech.com/blog/) ",
5
5
  "source": "./src/index.ts",
6
6
  "main": "./dist/index.js",