@msssystems/mss-link-sdk 0.2.3 → 0.2.4

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.
@@ -2,7 +2,7 @@ export interface HttpClientOptions {
2
2
  baseUrl: string;
3
3
  token?: string | (() => string | Promise<string>) | undefined;
4
4
  fetch?: typeof fetch;
5
- onUnauthorized?: (() => Promise<void>) | undefined;
5
+ onUnauthorized?: (() => Promise<string | null | void>) | undefined;
6
6
  }
7
7
  export declare class HttpClient {
8
8
  private baseUrl;
@@ -12,6 +12,12 @@ export declare class HttpClient {
12
12
  constructor(options: HttpClientOptions);
13
13
  setToken(token: string): Promise<void>;
14
14
  private getHeaders;
15
+ requestRaw(method: string, path: string, options?: {
16
+ body?: any;
17
+ params?: Record<string, any>;
18
+ headers?: Record<string, string>;
19
+ responseType?: 'json' | 'blob';
20
+ }): Promise<Response>;
15
21
  request<T>(method: string, path: string, options?: {
16
22
  body?: any;
17
23
  params?: Record<string, any>;
@@ -27,7 +27,7 @@ export class HttpClient {
27
27
  }
28
28
  return headers;
29
29
  }
30
- async request(method, path, options = {}) {
30
+ async requestRaw(method, path, options = {}) {
31
31
  const url = new URL(`${this.baseUrl}${path}`);
32
32
  if (options.params) {
33
33
  for (const [key, value] of Object.entries(options.params)) {
@@ -47,7 +47,10 @@ export class HttpClient {
47
47
  let response = await this.customFetch(url.toString(), fetchOptions);
48
48
  if (response.status === 401 && this.onUnauthorized) {
49
49
  try {
50
- await this.onUnauthorized();
50
+ const newToken = await this.onUnauthorized();
51
+ if (typeof newToken === 'string') {
52
+ this.setToken(newToken);
53
+ }
51
54
  // Update headers with new token
52
55
  fetchOptions.headers = await this.getHeaders(options.headers);
53
56
  response = await this.customFetch(url.toString(), fetchOptions);
@@ -56,6 +59,10 @@ export class HttpClient {
56
59
  // If refresh fails, let the original 401 error throw below
57
60
  }
58
61
  }
62
+ return response;
63
+ }
64
+ async request(method, path, options = {}) {
65
+ const response = await this.requestRaw(method, path, options);
59
66
  if (!response.ok) {
60
67
  let errorPayload;
61
68
  try {
@@ -31,8 +31,8 @@ export class MessagesClient {
31
31
  return this.http.get(`/link/v1/media/assets/${assetId}/url`);
32
32
  }
33
33
  async downloadMediaAsset(assetId) {
34
- const res = await fetch(`${this.http['baseUrl']}/link/v1/media/assets/${assetId}/download`, {
35
- headers: await this.http['getHeaders']()
34
+ const res = await this.http.requestRaw('GET', `/link/v1/media/assets/${assetId}/download`, {
35
+ responseType: 'blob'
36
36
  });
37
37
  if (!res.ok)
38
38
  throw new Error('Download failed');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@msssystems/mss-link-sdk",
3
- "version": "0.2.3",
3
+ "version": "0.2.4",
4
4
  "description": "Official managed application SDK for MSS ecosystem",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",