@machhub-dev/sdk-ts 0.0.12 → 0.0.13

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.
@@ -26,4 +26,5 @@ export declare class Collection {
26
26
  create(data: Record<string, any>): Promise<any>;
27
27
  update(id: string, data: Record<string, any>): Promise<any>;
28
28
  delete(id: string): Promise<any>;
29
+ getFile(fileName: string, fieldName: string): Promise<Blob>;
29
30
  }
@@ -114,5 +114,17 @@ class Collection {
114
114
  throw new CollectionError('delete', this.collectionName, error);
115
115
  }
116
116
  }
117
+ async getFile(fileName, fieldName) {
118
+ try {
119
+ return await this.httpService.request.withJSON({
120
+ fileName,
121
+ collectionName: this.collectionName,
122
+ fieldName
123
+ }).patch("file");
124
+ }
125
+ catch (error) {
126
+ throw new CollectionError('getFile', this.collectionName, error);
127
+ }
128
+ }
117
129
  }
118
130
  exports.Collection = Collection;
@@ -200,6 +200,12 @@ class RequestParameters {
200
200
  if (!response.ok) {
201
201
  throw new HTTPException(response.status, response.statusText, await response.text());
202
202
  }
203
- return response.json();
203
+ const contentType = response.headers.get('content-type');
204
+ if (contentType && contentType.includes('application/json')) {
205
+ return response.json();
206
+ }
207
+ else {
208
+ return response.blob();
209
+ }
204
210
  }
205
211
  }
@@ -26,4 +26,5 @@ export declare class Collection {
26
26
  create(data: Record<string, any>): Promise<any>;
27
27
  update(id: string, data: Record<string, any>): Promise<any>;
28
28
  delete(id: string): Promise<any>;
29
+ getFile(fileName: string, fieldName: string): Promise<Blob>;
29
30
  }
@@ -110,4 +110,16 @@ export class Collection {
110
110
  throw new CollectionError('delete', this.collectionName, error);
111
111
  }
112
112
  }
113
+ async getFile(fileName, fieldName) {
114
+ try {
115
+ return await this.httpService.request.withJSON({
116
+ fileName,
117
+ collectionName: this.collectionName,
118
+ fieldName
119
+ }).patch("file");
120
+ }
121
+ catch (error) {
122
+ throw new CollectionError('getFile', this.collectionName, error);
123
+ }
124
+ }
113
125
  }
@@ -195,6 +195,12 @@ class RequestParameters {
195
195
  if (!response.ok) {
196
196
  throw new HTTPException(response.status, response.statusText, await response.text());
197
197
  }
198
- return response.json();
198
+ const contentType = response.headers.get('content-type');
199
+ if (contentType && contentType.includes('application/json')) {
200
+ return response.json();
201
+ }
202
+ else {
203
+ return response.blob();
204
+ }
199
205
  }
200
206
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@machhub-dev/sdk-ts",
3
- "version": "0.0.12",
3
+ "version": "0.0.13",
4
4
  "description": "MACHHUB TYPESCRIPT SDK",
5
5
  "keywords": [
6
6
  "machhub",
@@ -52,7 +52,7 @@ export class Collection {
52
52
  this.queryParams.expand = Array.isArray(fields) ? fields.join(",") : fields;
53
53
  return this;
54
54
  }
55
-
55
+
56
56
  private applyOptions(options?: Record<string, any>) {
57
57
  if (!options) return;
58
58
 
@@ -67,7 +67,7 @@ export class Collection {
67
67
  const results = await this.limit(1).getAll();
68
68
  return results[0] ?? null
69
69
  }
70
-
70
+
71
71
  async getAll(options?: { expand?: string | string[] }): Promise<any[]> {
72
72
  try {
73
73
  this.applyOptions(options)
@@ -99,7 +99,7 @@ export class Collection {
99
99
  if (value instanceof File) {
100
100
  formData.append(key, value, value.name);
101
101
  data[key] = value.name
102
- }
102
+ }
103
103
  }
104
104
  formData.append("record", JSON.stringify(data))
105
105
  return await this.httpService.request
@@ -131,4 +131,17 @@ export class Collection {
131
131
  throw new CollectionError('delete', this.collectionName, error as Error);
132
132
  }
133
133
  }
134
+
135
+ async getFile(fileName: string, fieldName: string): Promise<Blob> {
136
+ try {
137
+ return await this.httpService.request.withJSON({
138
+ fileName,
139
+ collectionName: this.collectionName,
140
+ fieldName
141
+ }).patch("file");
142
+ } catch (error) {
143
+ throw new CollectionError('getFile', this.collectionName, error as Error);
144
+ }
145
+ }
146
+
134
147
  }
@@ -148,9 +148,9 @@ class RequestParameters {
148
148
 
149
149
  public withAccessToken(): RequestParameters {
150
150
  const rawAppID = this.applicationID.replace("domains:", "");
151
- const storageKey = rawAppID
152
- ? `x-machhub-auth-tkn-${rawAppID}`
153
- : `x-machhub-auth-tkn`;
151
+ const storageKey = rawAppID
152
+ ? `x-machhub-auth-tkn-${rawAppID}`
153
+ : `x-machhub-auth-tkn`;
154
154
  const tkn = localStorage.getItem(storageKey);
155
155
  if (tkn) this.setHeader("Authorization", `Bearer ${tkn}`);
156
156
  return this;
@@ -266,6 +266,12 @@ class RequestParameters {
266
266
  await response.text(),
267
267
  );
268
268
  }
269
- return response.json() as ReturnType;
269
+
270
+ const contentType = response.headers.get('content-type');
271
+ if (contentType && contentType.includes('application/json')) {
272
+ return response.json() as ReturnType;
273
+ } else {
274
+ return response.blob() as unknown as ReturnType;
275
+ }
270
276
  }
271
277
  }