@machhub-dev/sdk-ts 0.0.12 → 0.0.14

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
  }
@@ -97,7 +97,17 @@ class Collection {
97
97
  throw new Error("ID must be provided");
98
98
  }
99
99
  try {
100
- return await this.httpService.request.withJSON(data).put(id);
100
+ const formData = new FormData();
101
+ for (const [key, value] of Object.entries(data)) {
102
+ if (value instanceof File) {
103
+ formData.append(key, value, value.name);
104
+ data[key] = value.name;
105
+ }
106
+ }
107
+ formData.append("record", JSON.stringify(data));
108
+ return await this.httpService.request
109
+ .withBody(formData)
110
+ .put(id);
101
111
  }
102
112
  catch (error) {
103
113
  throw new CollectionError('update', this.collectionName, error);
@@ -114,5 +124,17 @@ class Collection {
114
124
  throw new CollectionError('delete', this.collectionName, error);
115
125
  }
116
126
  }
127
+ async getFile(fileName, fieldName) {
128
+ try {
129
+ return await this.httpService.request.withJSON({
130
+ fileName,
131
+ collectionName: this.collectionName,
132
+ fieldName
133
+ }).patch("file");
134
+ }
135
+ catch (error) {
136
+ throw new CollectionError('getFile', this.collectionName, error);
137
+ }
138
+ }
117
139
  }
118
140
  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
  }
@@ -93,7 +93,17 @@ export class Collection {
93
93
  throw new Error("ID must be provided");
94
94
  }
95
95
  try {
96
- return await this.httpService.request.withJSON(data).put(id);
96
+ const formData = new FormData();
97
+ for (const [key, value] of Object.entries(data)) {
98
+ if (value instanceof File) {
99
+ formData.append(key, value, value.name);
100
+ data[key] = value.name;
101
+ }
102
+ }
103
+ formData.append("record", JSON.stringify(data));
104
+ return await this.httpService.request
105
+ .withBody(formData)
106
+ .put(id);
97
107
  }
98
108
  catch (error) {
99
109
  throw new CollectionError('update', this.collectionName, error);
@@ -110,4 +120,16 @@ export class Collection {
110
120
  throw new CollectionError('delete', this.collectionName, error);
111
121
  }
112
122
  }
123
+ async getFile(fileName, fieldName) {
124
+ try {
125
+ return await this.httpService.request.withJSON({
126
+ fileName,
127
+ collectionName: this.collectionName,
128
+ fieldName
129
+ }).patch("file");
130
+ }
131
+ catch (error) {
132
+ throw new CollectionError('getFile', this.collectionName, error);
133
+ }
134
+ }
113
135
  }
@@ -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.14",
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
@@ -115,7 +115,19 @@ export class Collection {
115
115
  throw new Error("ID must be provided");
116
116
  }
117
117
  try {
118
- return await this.httpService.request.withJSON(data).put(id);
118
+ const formData = new FormData();
119
+
120
+ for (const [key, value] of Object.entries(data)) {
121
+ if (value instanceof File) {
122
+ formData.append(key, value, value.name);
123
+ data[key] = value.name;
124
+ }
125
+ }
126
+ formData.append("record", JSON.stringify(data));
127
+
128
+ return await this.httpService.request
129
+ .withBody(formData)
130
+ .put(id);
119
131
  } catch (error) {
120
132
  throw new CollectionError('update', this.collectionName, error as Error);
121
133
  }
@@ -131,4 +143,17 @@ export class Collection {
131
143
  throw new CollectionError('delete', this.collectionName, error as Error);
132
144
  }
133
145
  }
146
+
147
+ async getFile(fileName: string, fieldName: string): Promise<Blob> {
148
+ try {
149
+ return await this.httpService.request.withJSON({
150
+ fileName,
151
+ collectionName: this.collectionName,
152
+ fieldName
153
+ }).patch("file");
154
+ } catch (error) {
155
+ throw new CollectionError('getFile', this.collectionName, error as Error);
156
+ }
157
+ }
158
+
134
159
  }
@@ -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
  }