UNPKG

@nhost/nhost-js

Version:

Nhost JavaScript SDK

2 lines 5.57 kB
import{c as t,s as e}from"../fetch-CaGArzje.js";var s=(s,a=[])=>{let n=t(a);return{baseURL:s,pushChainFunction:e=>{a.push(e),n=t(a)},uploadFiles:async(t,a)=>{const r=`${s}/files`,d=new FormData,i="undefined"!=typeof navigator&&"ReactNative"===navigator.product;void 0!==t["bucket-id"]&&d.append("bucket-id",t["bucket-id"]),void 0!==t["metadata[]"]&&t["metadata[]"].forEach((t=>{i?d.append("metadata[]",{string:JSON.stringify(t),type:"application/json",name:""}):d.append("metadata[]",new Blob([JSON.stringify(t)],{type:"application/json"}),"")})),void 0!==t["file[]"]&&t["file[]"].forEach((t=>{d.append("file[]",t)}));const o=await n(r,{...a,method:"POST",body:d});if(o.status>=300){const t=[412].includes(o.status)?null:await o.text();throw new e(t?JSON.parse(t):{},o.status,o.headers)}const u=[204,205,304].includes(o.status)?null:await o.text();return{body:u?JSON.parse(u):{},status:o.status,headers:o.headers}},deleteFile:async(t,a)=>{const r=`${s}/files/${t}`,d=await n(r,{...a,method:"DELETE",headers:{...a?.headers}});if(d.status>=300){const t=[412].includes(d.status)?null:await d.text();throw new e(t?JSON.parse(t):{},d.status,d.headers)}return{body:void 0,status:d.status,headers:d.headers}},getFile:async(t,a,r)=>{const d=a&&Object.entries(a).flatMap((([t,e])=>{const s=Array.isArray(e)?e.join(","):"object"==typeof e&&null!==e?JSON.stringify(e):String(e);return[`${t}=${encodeURIComponent(s)}`]})).join("&"),i=d?`${s}/files/${t}?${d}`:`${s}/files/${t}`,o=await n(i,{...r,method:"GET",headers:{...r?.headers}});if(o.status>=300){const t=[412].includes(o.status)?null:await o.text();throw new e(t?JSON.parse(t):{},o.status,o.headers)}return{body:await o.blob(),status:o.status,headers:o.headers}},getFileMetadataHeaders:async(t,a,r)=>{const d=a&&Object.entries(a).flatMap((([t,e])=>{const s=Array.isArray(e)?e.join(","):"object"==typeof e&&null!==e?JSON.stringify(e):String(e);return[`${t}=${encodeURIComponent(s)}`]})).join("&"),i=d?`${s}/files/${t}?${d}`:`${s}/files/${t}`,o=await n(i,{...r,method:"HEAD",headers:{...r?.headers}});if(o.status>=300){const t=[412].includes(o.status)?null:await o.text();throw new e(t?JSON.parse(t):{},o.status,o.headers)}return{body:void 0,status:o.status,headers:o.headers}},replaceFile:async(t,a,r)=>{const d=`${s}/files/${t}`,i=new FormData,o="undefined"!=typeof navigator&&"ReactNative"===navigator.product;void 0!==a.metadata&&(o?i.append("metadata",{string:JSON.stringify(a.metadata),type:"application/json",name:""}):i.append("metadata",new Blob([JSON.stringify(a.metadata)],{type:"application/json"}),"")),void 0!==a.file&&i.append("file",a.file);const u=await n(d,{...r,method:"PUT",body:i});if(u.status>=300){const t=[412].includes(u.status)?null:await u.text();throw new e(t?JSON.parse(t):{},u.status,u.headers)}const l=[204,205,304].includes(u.status)?null:await u.text();return{body:l?JSON.parse(l):{},status:u.status,headers:u.headers}},getFilePresignedURL:async(t,a)=>{const r=`${s}/files/${t}/presignedurl`,d=await n(r,{...a,method:"GET",headers:{...a?.headers}});if(d.status>=300){const t=[412].includes(d.status)?null:await d.text();throw new e(t?JSON.parse(t):{},d.status,d.headers)}const i=[204,205,304].includes(d.status)?null:await d.text();return{body:i?JSON.parse(i):{},status:d.status,headers:d.headers}},deleteBrokenMetadata:async t=>{const a=`${s}/ops/delete-broken-metadata`,r=await n(a,{...t,method:"POST",headers:{...t?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text();throw new e(t?JSON.parse(t):{},r.status,r.headers)}const d=[204,205,304].includes(r.status)?null:await r.text();return{body:d?JSON.parse(d):{},status:r.status,headers:r.headers}},deleteOrphanedFiles:async t=>{const a=`${s}/ops/delete-orphans`,r=await n(a,{...t,method:"POST",headers:{...t?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text();throw new e(t?JSON.parse(t):{},r.status,r.headers)}const d=[204,205,304].includes(r.status)?null:await r.text();return{body:d?JSON.parse(d):{},status:r.status,headers:r.headers}},listBrokenMetadata:async t=>{const a=`${s}/ops/list-broken-metadata`,r=await n(a,{...t,method:"POST",headers:{...t?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text();throw new e(t?JSON.parse(t):{},r.status,r.headers)}const d=[204,205,304].includes(r.status)?null:await r.text();return{body:d?JSON.parse(d):{},status:r.status,headers:r.headers}},listFilesNotUploaded:async t=>{const a=`${s}/ops/list-not-uploaded`,r=await n(a,{...t,method:"POST",headers:{...t?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text();throw new e(t?JSON.parse(t):{},r.status,r.headers)}const d=[204,205,304].includes(r.status)?null:await r.text();return{body:d?JSON.parse(d):{},status:r.status,headers:r.headers}},listOrphanedFiles:async t=>{const a=`${s}/ops/list-orphans`,r=await n(a,{...t,method:"POST",headers:{...t?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text();throw new e(t?JSON.parse(t):{},r.status,r.headers)}const d=[204,205,304].includes(r.status)?null:await r.text();return{body:d?JSON.parse(d):{},status:r.status,headers:r.headers}},getVersion:async t=>{const a=`${s}/version`,r=await n(a,{...t,method:"GET",headers:{...t?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text();throw new e(t?JSON.parse(t):{},r.status,r.headers)}const d=[204,205,304].includes(r.status)?null:await r.text();return{body:d?JSON.parse(d):{},status:r.status,headers:r.headers}}}};export{s as createAPIClient}; //# sourceMappingURL=storage.js.map