@spica-devkit/storage 0.13.1 → 0.18.1-pre2

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
@@ -49,6 +49,7 @@ class Axios {
49
49
  }
50
50
  setBaseUrl(url) {
51
51
  this.instance.defaults.baseURL = url;
52
+ this.baseUrl = url;
52
53
  }
53
54
  setWriteDefaults(writeDefaults) {
54
55
  for (const [header, value] of Object.entries(writeDefaults.headers)) {
@@ -59,6 +60,9 @@ class Axios {
59
60
  setAuthorization(authorization) {
60
61
  this.instance.defaults.headers["Authorization"] = authorization;
61
62
  }
63
+ getAuthorization() {
64
+ return this.instance.defaults.headers["Authorization"].toString();
65
+ }
62
66
  get(url, config) {
63
67
  return this.instance.get(url, config);
64
68
  }
@@ -156,23 +160,28 @@ function initialize$1(options) {
156
160
  else if ("identity" in options) {
157
161
  authorization = `IDENTITY ${options.identity}`;
158
162
  }
159
- checkInitialized(authorization);
163
+ else if ("user" in options) {
164
+ authorization = `USER ${options.user}`;
165
+ }
160
166
  const publicUrl = options.publicUrl || getPublicUrl();
161
167
  if (!publicUrl) {
162
168
  throw new Error("Public url must be provided.");
163
169
  }
164
170
  if (!service$1) {
165
- service$1 = new Axios({ baseURL: publicUrl, headers: { Authorization: authorization } });
171
+ service$1 = new Axios({});
166
172
  }
167
- else {
168
- service$1.setBaseUrl(publicUrl);
173
+ service$1.setBaseUrl(publicUrl);
174
+ if (authorization) {
169
175
  service$1.setAuthorization(authorization);
170
176
  }
171
177
  return { authorization, publicUrl, service: service$1 };
172
178
  }
173
- function checkInitialized(authorization) {
174
- if (!authorization) {
175
- throw new Error("You should call initialize method with a valid apikey or identity token.");
179
+ function checkInitialized(authorization, service, options = { skipAuthCheck: false }) {
180
+ if (!authorization && !options.skipAuthCheck) {
181
+ throw new Error("You should call initialize method with a valid credentials.");
182
+ }
183
+ if (!service) {
184
+ throw new Error("You should call initialize method with a valid publicUrl.");
176
185
  }
177
186
  }
178
187
  function getPublicUrl() {
@@ -182,6 +191,76 @@ function isPlatformBrowser() {
182
191
  return typeof window !== "undefined";
183
192
  }
184
193
 
194
+ var Batch;
195
+ (function (Batch) {
196
+ function prepareInsertRequest(resources, url, auth, headers) {
197
+ return {
198
+ requests: resources.map((resource, i) => {
199
+ headers = addAuthHeader(headers, auth);
200
+ return {
201
+ id: i.toString(),
202
+ body: resource,
203
+ method: "POST",
204
+ url: url,
205
+ headers: headers
206
+ };
207
+ })
208
+ };
209
+ }
210
+ Batch.prepareInsertRequest = prepareInsertRequest;
211
+ function prepareRemoveRequest(ids, url, auth, headers) {
212
+ return {
213
+ requests: ids.map((id, i) => {
214
+ headers = addAuthHeader(headers, auth);
215
+ return {
216
+ id: i.toString(),
217
+ body: undefined,
218
+ method: "DELETE",
219
+ url: `${url}/${id}`,
220
+ headers: headers
221
+ };
222
+ })
223
+ };
224
+ }
225
+ Batch.prepareRemoveRequest = prepareRemoveRequest;
226
+ function handleBatchResponse({ requests }, { responses }) {
227
+ const sortById = (a, b) => Number(a.id) - Number(b.id);
228
+ const successResponses = responses
229
+ .sort(sortById)
230
+ .filter(r => r.status >= 200 && r.status < 300);
231
+ const failureResponses = responses
232
+ .sort(sortById)
233
+ .filter(r => r.status >= 400 && r.status <= 500);
234
+ const successes = successResponses.map(sr => {
235
+ const req = requests.find(r => r.id == sr.id);
236
+ return {
237
+ request: (req.body || req.url),
238
+ response: sr.body
239
+ };
240
+ });
241
+ const failures = failureResponses.map(fr => {
242
+ const req = requests.find(r => r.id == fr.id);
243
+ return {
244
+ request: (req.body || req.url),
245
+ response: {
246
+ error: fr.body["error"],
247
+ message: fr.body["message"]
248
+ }
249
+ };
250
+ });
251
+ return {
252
+ successes,
253
+ failures
254
+ };
255
+ }
256
+ Batch.handleBatchResponse = handleBatchResponse;
257
+ })(Batch || (Batch = {}));
258
+ function addAuthHeader(headers, auth) {
259
+ headers = headers || {};
260
+ headers["Authorization"] = auth;
261
+ return headers;
262
+ }
263
+
185
264
  function getDefaultExportFromCjs (x) {
186
265
  return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
187
266
  }
@@ -12094,61 +12173,70 @@ function initialize(options) {
12094
12173
  authorization = _authorization;
12095
12174
  service = _service;
12096
12175
  }
12097
- async function insert(object, onUploadProgress) {
12098
- checkInitialized(authorization);
12099
- const { body, headers } = await preparePostBody([object]);
12176
+ async function insert(object, onUploadProgress, headers = {}) {
12177
+ checkInitialized(authorization, service);
12178
+ const postBody = await preparePostBody([object]);
12100
12179
  return service
12101
- .post("storage", body, {
12180
+ .post("storage", postBody.body, {
12102
12181
  onUploadProgress,
12103
- headers
12182
+ headers: { ...postBody.headers, ...headers }
12104
12183
  })
12105
12184
  .then(([r]) => r);
12106
12185
  }
12107
- async function insertMany(objects, onUploadProgress) {
12108
- checkInitialized(authorization);
12109
- const { body, headers } = await preparePostBody(objects);
12110
- return service.post("storage", body, {
12186
+ async function insertMany(objects, onUploadProgress, headers = {}) {
12187
+ checkInitialized(authorization, service);
12188
+ const postBody = await preparePostBody(objects);
12189
+ return service.post("storage", postBody.body, {
12111
12190
  onUploadProgress,
12112
- headers
12191
+ headers: { ...postBody.headers, ...headers }
12113
12192
  });
12114
12193
  }
12115
- function get(id) {
12116
- checkInitialized(authorization);
12117
- return service.get(`storage/${id}`);
12194
+ function get(id, headers) {
12195
+ checkInitialized(authorization, service);
12196
+ return service.get(`storage/${id}`, { headers });
12118
12197
  }
12119
12198
  function download(id, options = {}) {
12120
- checkInitialized(authorization);
12199
+ checkInitialized(authorization, service);
12121
12200
  return service.get(`storage/${id}/view`, {
12122
12201
  headers: options.headers,
12123
12202
  onDownloadProgress: options.onDownloadProgress,
12124
12203
  responseType: isPlatformBrowser() ? "blob" : "stream"
12125
12204
  });
12126
12205
  }
12127
- function getAll(queryParams) {
12128
- checkInitialized(authorization);
12206
+ function getAll(queryParams, headers) {
12207
+ checkInitialized(authorization, service);
12129
12208
  return service.get(`storage`, {
12130
- params: queryParams
12209
+ params: queryParams,
12210
+ headers
12131
12211
  });
12132
12212
  }
12133
- async function update(id, object, onUploadProgress) {
12134
- checkInitialized(authorization);
12135
- const { body, headers } = await preparePutBody(object);
12136
- return service.put(`storage/${id}`, body, {
12213
+ async function update(id, object, onUploadProgress, headers = {}) {
12214
+ checkInitialized(authorization, service);
12215
+ const putBody = await preparePutBody(object);
12216
+ return service.put(`storage/${id}`, putBody.body, {
12137
12217
  onUploadProgress,
12138
- headers
12218
+ headers: { ...putBody.headers, ...headers }
12139
12219
  });
12140
12220
  }
12141
- async function updateMeta(id, meta) {
12142
- checkInitialized(authorization);
12221
+ async function updateMeta(id, meta, headers = {}) {
12222
+ checkInitialized(authorization, service);
12143
12223
  return service.patch(`storage/${id}`, meta, {
12144
12224
  headers: {
12145
- "Content-Type": "application/json"
12225
+ "Content-Type": "application/json",
12226
+ ...headers
12146
12227
  }
12147
12228
  });
12148
12229
  }
12149
- function remove(id) {
12150
- checkInitialized(authorization);
12151
- return service.delete(`storage/${id}`);
12230
+ function remove(id, headers) {
12231
+ checkInitialized(authorization, service);
12232
+ return service.delete(`storage/${id}`, { headers });
12233
+ }
12234
+ function removeMany(ids, headers) {
12235
+ checkInitialized(authorization, service);
12236
+ const batchReqs = Batch.prepareRemoveRequest(ids, "storage", service.getAuthorization(), headers);
12237
+ return service
12238
+ .post("batch", batchReqs, { headers })
12239
+ .then(response => Batch.handleBatchResponse(batchReqs, response));
12152
12240
  }
12153
12241
 
12154
12242
  exports.download = download;
@@ -12158,6 +12246,7 @@ exports.initialize = initialize;
12158
12246
  exports.insert = insert;
12159
12247
  exports.insertMany = insertMany;
12160
12248
  exports.remove = remove;
12249
+ exports.removeMany = removeMany;
12161
12250
  exports.update = update;
12162
12251
  exports.updateMeta = updateMeta;
12163
12252
  //# sourceMappingURL=index.js.map