Comparing version 0.1.3 to 0.1.4
const baseUrl = "https://api.truetoform.online/v1"; | ||
class ResponseError extends Error { | ||
constructor(response, message) { | ||
this.response = response; | ||
this.message = message || response.statusText; | ||
this.name = "ResponseError"; | ||
} | ||
} | ||
export const createClient = (apiKey) => { | ||
@@ -25,18 +17,8 @@ const headers = { | ||
const response = await fetch(`${baseUrl}/${endpoint}`, options); | ||
try { | ||
if (!response.ok) { | ||
const data = await response.json(); | ||
if (!response.ok) { | ||
throw new ResponseError(response, data.message); | ||
} | ||
return data; | ||
} catch (error) { | ||
if (error.name === "ResponseError") { | ||
throw new Error( | ||
`Error ${error.response.status}: ${error.response.message}` | ||
); | ||
} | ||
// If the response is not JSON, throw the original error | ||
return response; | ||
throw new Error(`Error ${response.status}: ${data.message}`); | ||
} | ||
const data = await response.json(); | ||
return data; | ||
}; | ||
@@ -43,0 +25,0 @@ |
@@ -1,1 +0,1 @@ | ||
!function(e,r){"object"==typeof exports&&"object"==typeof module?module.exports=r():"function"==typeof define&&define.amd?define([],r):"object"==typeof exports?exports.truetoformApi=r():e.truetoformApi=r()}(this,(()=>(()=>{"use strict";var e={d:(r,t)=>{for(var o in t)e.o(t,o)&&!e.o(r,o)&&Object.defineProperty(r,o,{enumerable:!0,get:t[o]})},o:(e,r)=>Object.prototype.hasOwnProperty.call(e,r),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},r={};e.r(r),e.d(r,{default:()=>o});class t extends Error{constructor(e,r){this.response=e,this.message=r||e.statusText,this.name="ResponseError"}}const o=e=>{const r=(e=>{const r={"Content-Type":"application/json","X-TTF-API-KEY":e},o=async(e,o,n=null)=>{const s={method:e,headers:r,body:n?JSON.stringify(n):null},i=await fetch(`https://api.truetoform.online/v1/${o}`,s);try{const e=await i.json();if(!i.ok)throw new t(i,e.message);return e}catch(e){if("ResponseError"===e.name)throw new Error(`Error ${e.response.status}: ${e.response.message}`);return i}};return{get:e=>o("GET",e),post:(e,r)=>o("POST",e,r),put:(e,r)=>o("PUT",e,r),delete:e=>o("DELETE",e)}})(e);return{getGarment:e=>{if(!e)throw new Error("garment ID is required");return r.get(`garments/${e}`)},getSurvey:e=>{if(!e)throw new Error("survey ID is required");return r.get(`surveys/${e}`)},getScan:e=>{if(!e)throw new Error("scan ID is required");return r.get(`scans/${e}`)},getPrediction:e=>{if(!e)throw new Error("prediction ID is required");return r.get(`predictions/${e}`)},createSession:()=>r.post("sessions"),createScanPrediction:(e,t)=>r.post("predictions/scan",{sessionId:t,garmentId:e}),createSurveyPrediction:(e,t)=>r.post("predictions/survey",{...t,garmentId:e})}};return r})())); | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.truetoformApi=t():e.truetoformApi=t()}(this,(()=>(()=>{"use strict";var e={d:(t,r)=>{for(var o in r)e.o(r,o)&&!e.o(t,o)&&Object.defineProperty(t,o,{enumerable:!0,get:r[o]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{default:()=>r});const r=e=>{const t=(e=>{const t={"Content-Type":"application/json","X-TTF-API-KEY":e},r=async(e,r,o=null)=>{const n={method:e,headers:t,body:o?JSON.stringify(o):null},i=await fetch(`https://api.truetoform.online/v1/${r}`,n);if(!i.ok){const e=await i.json();throw new Error(`Error ${i.status}: ${e.message}`)}return await i.json()};return{get:e=>r("GET",e),post:(e,t)=>r("POST",e,t),put:(e,t)=>r("PUT",e,t),delete:e=>r("DELETE",e)}})(e);return{getGarment:e=>{if(!e)throw new Error("garment ID is required");return t.get(`garments/${e}`)},getSurvey:e=>{if(!e)throw new Error("survey ID is required");return t.get(`surveys/${e}`)},getScan:e=>{if(!e)throw new Error("scan ID is required");return t.get(`scans/${e}`)},getPrediction:e=>{if(!e)throw new Error("prediction ID is required");return t.get(`predictions/${e}`)},createSession:()=>t.post("sessions"),createScanPrediction:(e,r)=>t.post("predictions/scan",{sessionId:r,garmentId:e}),createSurveyPrediction:(e,r)=>t.post("predictions/survey",{...r,garmentId:e})}};return t})())); |
{ | ||
"name": "ttf-api", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "The TrueToForm API SDK", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7215
10
162