@commercetools/frontend-sdk
Advanced tools
Comparing version 2.0.0-alpha.0 to 2.1.0-alpha.0
## Version 2.0.0-alpha.0 (2022-12-06) | ||
## Version 2.1.0-alpha.0 (2022-12-09) | ||
* Fixed major NPM deployment issue | ||
Updated return type so cannot be void | ||
* Event names modified | ||
* Updated fomatting of CHANGELOG.md | ||
## Version 1.1.3-alpha.0 (2022-12-06) | ||
* Fixed dependency issues | ||
## Version 2.0.0-alpha.0 (2022-12-06) | ||
* Fixed major NPM deployment issue | ||
* Updated script | ||
## Version 1.1.3-alpha.0 (2022-12-06) | ||
## Version 1.1.2-alpha.0 (2022-12-06) | ||
* Updated prepublishOnly script | ||
## Version 1.1.1-alpha.0 (2022-12-06) | ||
* Updated name of emitter class and export type | ||
@@ -22,0 +23,0 @@ |
@@ -1,1 +0,2 @@ | ||
export declare const fetcher: <T>(url: string, options?: RequestInit) => Promise<T>; | ||
import { FetchError } from "../library/FetchError"; | ||
export declare const fetcher: <T>(url: string, options?: RequestInit) => Promise<FetchError | T>; |
@@ -5,3 +5,4 @@ import { rememberMeCookie } from "./helpers/cookieManagement"; | ||
import Event from "./library/Event"; | ||
import { FetchError } from "./library/FetchError"; | ||
declare const sdk: SDK; | ||
export { sdk, SDK, Extension, Event, rememberMeCookie }; | ||
export { sdk, SDK, Extension, Event, FetchError, rememberMeCookie }; |
@@ -1,3 +0,3 @@ | ||
"use strict";var g=Object.defineProperty;var I=Object.getOwnPropertyDescriptor;var N=Object.getOwnPropertyNames;var P=Object.prototype.hasOwnProperty;var D=(o,e)=>{for(var t in e)g(o,t,{get:e[t],enumerable:!0})},k=(o,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of N(e))!P.call(o,r)&&r!==t&&g(o,r,{get:()=>e[r],enumerable:!(n=I(e,r))||n.enumerable});return o};var T=o=>k(g({},"__esModule",{value:!0}),o);var L={};D(L,{Event:()=>d,Extension:()=>f,SDK:()=>l,rememberMeCookie:()=>u,sdk:()=>H});module.exports=T(L);var E="__rememberMe",u={get:function(){return!!(typeof window<"u"&&window.localStorage.getItem(E))},set:function(o){typeof window<"u"&&(o&&window.localStorage.setItem(E,"1"),this.remove())},remove:function(){typeof window<"u"&&window.localStorage.removeItem(E)}};var f=class{sdk;constructor(e){this.sdk=e}};function p(o){for(var e=1;e<arguments.length;e++){var t=arguments[e];for(var n in t)o[n]=t[n]}return o}var S={read:function(o){return o[0]==='"'&&(o=o.slice(1,-1)),o.replace(/(%[\dA-F]{2})+/gi,decodeURIComponent)},write:function(o){return encodeURIComponent(o).replace(/%(2[346BF]|3[AC-F]|40|5[BDE]|60|7[BCD])/g,decodeURIComponent)}};function y(o,e){function t(r,i,s){if(!(typeof document>"u")){s=p({},e,s),typeof s.expires=="number"&&(s.expires=new Date(Date.now()+s.expires*864e5)),s.expires&&(s.expires=s.expires.toUTCString()),r=encodeURIComponent(r).replace(/%(2[346B]|5E|60|7C)/g,decodeURIComponent).replace(/[()]/g,escape);var a="";for(var c in s)!s[c]||(a+="; "+c,s[c]!==!0&&(a+="="+s[c].split(";")[0]));return document.cookie=r+"="+o.write(i,r)+a}}function n(r){if(!(typeof document>"u"||arguments.length&&!r)){for(var i=document.cookie?document.cookie.split("; "):[],s={},a=0;a<i.length;a++){var c=i[a].split("="),b=c.slice(1).join("=");try{var m=decodeURIComponent(c[0]);if(s[m]=o.read(b,m),r===m)break}catch{}}return r?s[r]:s}}return Object.create({set:t,get:n,remove:function(r,i){t(r,"",p({},i,{expires:-1}))},withAttributes:function(r){return y(this.converter,p({},this.attributes,r))},withConverter:function(r){return y(p({},this.converter,r),this.attributes)}},{attributes:{value:Object.freeze(e)},converter:{value:Object.freeze(o)}})}var A=y(S,{path:"/"}),C=A;var x=C.withAttributes({path:"/"}),w=async(o,e={})=>{let t=x.get("frontastic-session");e.headers={"Content-Type":"application/json",Accept:"application/json","X-Frontastic-Access-Token":"APIKEY",...e.headers||{},...t?{"Frontastic-Session":t}:{}};let n=await fetch(o,e);if(typeof window<"u"&&n.ok&&n.headers.has("Frontastic-Session")){let i=u.get(),s;i&&(s=new Date(Date.now()+1e3*60*60*24*30*3)),x.set("frontastic-session",n.headers.get("Frontastic-Session"),{expires:s})}if(n.ok)return n.json();let r;try{r=await n.clone().json()}catch{r=await n.text()}if(r.error)throw new Error(r.errorCode);return r};var v=class{#n=[];#e=!1;#t=!1;add(e){return new Promise((t,n)=>{this.#n.push({promise:e,resolve:t,reject:n}),this.#r()})}stop(){this.#t=!0}restart(){this.#t=!1,this.#r()}#r(){if(this.#e||this.#t)return;let e=this.#n.shift();if(!!e)try{this.#e=!0,e.promise().then(t=>this.#o(()=>e.resolve(t))).catch(t=>this.#o(()=>e.reject(t)))}catch(t){this.#o(()=>e.reject(t))}}#o(e){this.#e=!1,e(),this.#r()}};var h=class{eventHandlers;constructor(){this.eventHandlers={}}getEventHandlers(e){let t=this.eventHandlers[e];return t===void 0&&(t=[],this.eventHandlers[e]=t),t}on(e,t){this.getEventHandlers(e).push(t)}offForEvent(e){this.eventHandlers[e]=[]}off(e,t){let n=this.getEventHandlers(e);n.splice(n.indexOf(t),1)}clearAllHandlers(){this.eventHandlers={}}trigger(e){for(let t of this.getEventHandlers(e.eventName))t(e)}};var l=class extends h{#n;#e;#t;#r;#o;#s;set endpoint(e){this.#e=e}get endpoint(){return this.#e}set locale(e){typeof e=="string"?this.#t=new Intl.Locale(e):this.#t=e}get locale(){return this.#t}get APILocale(){let e=this.locale.baseName.slice(0,5).replace("-","_");return this.#o?`${e}@${this.currency}`:e}set currency(e){this.#r=e}get currency(){return this.#r}constructor(){super(),this.#n=!1,this.#s=new v}#a(){if(!this.#n)throw new Error(`The SDK has not been configured. | ||
Please call .configure before you call any other methods.`)}#i=e=>e.split("//").reduce((t,n)=>n==="http:"||n==="https:"?n+="/":`${t}/${n}`,"");configure(e){this.endpoint=e.endpoint,this.locale=new Intl.Locale(e.locale),this.currency=e.currency,this.#o=e.useCurrencyInLocale??!1,this.#n=!0}async callAction(e,t,n){this.#a();let r="";n&&(r=Object.keys(n).reduce((i,s)=>n[s]?i+`${s}=${n[s]}&`:i,"?").slice(0,r.length-1));try{return await this.#s.add(()=>w(this.#i(`${this.#e}/frontastic/action/${e}${r}`),{method:"POST",body:JSON.stringify(t),headers:{"Frontastic-Locale":this.APILocale}}))}catch(i){return typeof i=="string"?new Error(i):i}}async getPage(e){let t={headers:{"Frontastic-Path":e,"Frontastic-Locale":this.APILocale}};try{return w(this.#i(`${this.#e}/page`),t)}catch(n){return typeof n=="string"?new Error(n):n}}};var d=class{eventName;data;isDefaultPrevented;isCancelled;isPropagationStopped;constructor(e){this.eventName=e.eventName,this.data=e.data,this.isCancelled=!1,this.isDefaultPrevented=!1,this.isPropagationStopped=!1}preventDefault(){this.isDefaultPrevented=!0}cancel(){this.isCancelled=!0}stopPropagation(){this.isPropagationStopped=!0}};var H=new l; | ||
"use strict";var g=Object.defineProperty;var N=Object.getOwnPropertyDescriptor;var P=Object.getOwnPropertyNames;var T=Object.prototype.hasOwnProperty;var k=(n,e)=>{for(var t in e)g(n,t,{get:e[t],enumerable:!0})},D=(n,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of P(e))!T.call(n,o)&&o!==t&&g(n,o,{get:()=>e[o],enumerable:!(r=N(e,o))||r.enumerable});return n};var F=n=>D(g({},"__esModule",{value:!0}),n);var L={};k(L,{Event:()=>u,Extension:()=>p,FetchError:()=>c,SDK:()=>d,rememberMeCookie:()=>f,sdk:()=>H});module.exports=F(L);var y="__rememberMe",f={get:function(){return!!(typeof window<"u"&&window.localStorage.getItem(y))},set:function(n){typeof window<"u"&&(n&&window.localStorage.setItem(y,"1"),this.remove())},remove:function(){typeof window<"u"&&window.localStorage.removeItem(y)}};var p=class{sdk;constructor(e){this.sdk=e}};function h(n){for(var e=1;e<arguments.length;e++){var t=arguments[e];for(var r in t)n[r]=t[r]}return n}var S={read:function(n){return n[0]==='"'&&(n=n.slice(1,-1)),n.replace(/(%[\dA-F]{2})+/gi,decodeURIComponent)},write:function(n){return encodeURIComponent(n).replace(/%(2[346BF]|3[AC-F]|40|5[BDE]|60|7[BCD])/g,decodeURIComponent)}};function w(n,e){function t(o,i,s){if(!(typeof document>"u")){s=h({},e,s),typeof s.expires=="number"&&(s.expires=new Date(Date.now()+s.expires*864e5)),s.expires&&(s.expires=s.expires.toUTCString()),o=encodeURIComponent(o).replace(/%(2[346B]|5E|60|7C)/g,decodeURIComponent).replace(/[()]/g,escape);var a="";for(var l in s)!s[l]||(a+="; "+l,s[l]!==!0&&(a+="="+s[l].split(";")[0]));return document.cookie=o+"="+n.write(i,o)+a}}function r(o){if(!(typeof document>"u"||arguments.length&&!o)){for(var i=document.cookie?document.cookie.split("; "):[],s={},a=0;a<i.length;a++){var l=i[a].split("="),I=l.slice(1).join("=");try{var E=decodeURIComponent(l[0]);if(s[E]=n.read(I,E),o===E)break}catch{}}return o?s[o]:s}}return Object.create({set:t,get:r,remove:function(o,i){t(o,"",h({},i,{expires:-1}))},withAttributes:function(o){return w(this.converter,h({},this.attributes,o))},withConverter:function(o){return w(h({},this.converter,o),this.attributes)}},{attributes:{value:Object.freeze(e)},converter:{value:Object.freeze(n)}})}var A=w(S,{path:"/"}),x=A;var c=class extends Error{constructor(e){super(),this.isError=!0,typeof e=="string"?this.message=e:Object.keys(e).forEach(t=>{this[t]=e[t]})}isError;message};var C=x.withAttributes({path:"/"}),b=async(n,e={})=>{let t=C.get("frontastic-session");e.headers={"Content-Type":"application/json",Accept:"application/json","X-Frontastic-Access-Token":"APIKEY",...e.headers||{},...t?{"Frontastic-Session":t}:{}};let r=await fetch(n,e);if(typeof window<"u"&&r.ok&&r.headers.has("Frontastic-Session")){let i=f.get(),s;i&&(s=new Date(Date.now()+1e3*60*60*24*30*3)),C.set("frontastic-session",r.headers.get("Frontastic-Session"),{expires:s})}if(r.ok)return r.json();let o;try{o=await r.clone().json()}catch{o=await r.text()}return new c(o)};var v=class{#r=[];#e=!1;#t=!1;add(e){return new Promise((t,r)=>{this.#r.push({promise:e,resolve:t,reject:r}),this.#n()})}stop(){this.#t=!0}restart(){this.#t=!1,this.#n()}#n(){if(this.#e||this.#t)return;let e=this.#r.shift();if(!!e)try{this.#e=!0,e.promise().then(t=>this.#o(()=>e.resolve(t))).catch(t=>this.#o(()=>e.reject(t)))}catch(t){this.#o(()=>e.reject(t))}}#o(e){this.#e=!1,e(),this.#n()}};var m=class{eventHandlers;constructor(){this.eventHandlers={}}getEventHandlers(e){let t=this.eventHandlers[e];return t===void 0&&(t=[],this.eventHandlers[e]=t),t}on(e,t){this.getEventHandlers(e).push(t)}offForEvent(e){this.eventHandlers[e]=[]}off(e,t){let r=this.getEventHandlers(e);r.splice(r.indexOf(t),1)}clearAllHandlers(){this.eventHandlers={}}trigger(e){for(let t of this.getEventHandlers(e.eventName))t(e)}};var d=class extends m{#r;#e;#t;#n;#o;#s;set endpoint(e){this.#e=e}get endpoint(){return this.#e}set locale(e){typeof e=="string"?this.#t=new Intl.Locale(e):this.#t=e}get locale(){return this.#t}get APILocale(){let e=this.locale.baseName.slice(0,5).replace("-","_");return this.#o?`${e}@${this.currency}`:e}set currency(e){this.#n=e}get currency(){return this.#n}constructor(){super(),this.#r=!1,this.#s=new v}#c(){if(!this.#r)throw new Error(`The SDK has not been configured. | ||
Please call .configure before you call any other methods.`)}#i=e=>e.split("//").reduce((t,r)=>r==="http:"||r==="https:"?r+="/":`${t}/${r}`,"");configure(e){this.endpoint=e.endpoint,this.locale=new Intl.Locale(e.locale),this.currency=e.currency,this.#o=e.useCurrencyInLocale??!1,this.#r=!0}#a(e){return!e}async callAction(e,t={},r){this.#c();let o="";r&&(o=Object.keys(r).reduce((s,a)=>r[a]?s+`${a}=${r[a]}&`:s,"?").slice(0,o.length-1));let i=await this.#s.add(()=>b(this.#i(`${this.#e}/frontastic/action/${e}${o}`),{method:"POST",body:JSON.stringify(t),headers:{"Frontastic-Locale":this.APILocale}})).catch(s=>new c(s));return this.#a(i)?{}:i}async getPage(e){let t={headers:{"Frontastic-Path":e,"Frontastic-Locale":this.APILocale}},r=await b(this.#i(`${this.#e}/page`),t).catch(o=>new c(o));return this.#a(r)?{}:r}};var u=class{eventName;data;isDefaultPrevented;isCancelled;isPropagationStopped;constructor(e){this.eventName=e.eventName,this.data=e.data,this.isCancelled=!1,this.isDefaultPrevented=!1,this.isPropagationStopped=!1}preventDefault(){this.isDefaultPrevented=!0}cancel(){this.isCancelled=!0}stopPropagation(){this.isPropagationStopped=!0}};var H=new d; | ||
/*! js-cookie v3.0.1 | MIT */ |
import { EventManager } from "./EventManager"; | ||
import { Currency, DynamicEvent, StandardEvents } from "./types"; | ||
import { FetchError } from "./FetchError"; | ||
export declare class SDK extends EventManager<StandardEvents & DynamicEvent> { | ||
@@ -19,6 +20,10 @@ #private; | ||
}): void; | ||
callAction<T>(actionName: string, payload: unknown, query?: { | ||
callAction<T>(actionName: string, payload?: unknown, query?: { | ||
[key: string]: string | number | boolean; | ||
}): Promise<T | Error>; | ||
getPage<T>(path: string): Promise<Error | T>; | ||
}): Promise<T & { | ||
isError?: boolean; | ||
} | FetchError>; | ||
getPage<T>(path: string): Promise<T & { | ||
isError?: boolean; | ||
} | FetchError>; | ||
} |
@@ -1,3 +0,3 @@ | ||
export declare type Currency = "AED" | "AFN" | "ALL" | "AMD" | "ANG" | "AOA" | "ARS" | "AUD" | "AWG" | "AZN" | "BAM" | "BBD" | "BDT" | "BGN" | "BHD" | "BIF" | "BMD" | "BND" | "BOB" | "BRL" | "BSD" | "BTN" | "BWP" | "BYR" | "BZD" | "CAD" | "CDF" | "CHF" | "CLP" | "CNY" | "COP" | "CRC" | "CUC" | "CUP" | "CVE" | "CZK" | "DJF" | "DKK" | "DOP" | "DZD" | "EGP" | "ERN" | "ETB" | "EUR" | "FJD" | "FKP" | "GBP" | "GEL" | "GGP" | "GHS" | "GIP" | "GMD" | "GNF" | "GTQ" | "GYD" | "HKD" | "HNL" | "HRK" | "HTG" | "HUF" | "IDR" | "ILS" | "IMP" | "INR" | "IQD" | "IRR" | "ISK" | "JEP" | "JMD" | "JOD" | "JPY" | "KES" | "KGS" | "KHR" | "KMF" | "KPW" | "KRW" | "KWD" | "KYD" | "KZT" | "LAK" | "LBP" | "LKR" | "LRD" | "LSL" | "LYD" | "MAD" | "MDL" | "MGA" | "MKD" | "MMK" | "MNT" | "MOP" | "MRO" | "MUR" | "MVR" | "MWK" | "MXN" | "MYR" | "MZN" | "NAD" | "NGN" | "NIO" | "NOK" | "NPR" | "NZD" | "OMR" | "PAB" | "PEN" | "PGK" | "PHP" | "PKR" | "PLN" | "PYG" | "QAR" | "RON" | "RSD" | "RUB" | "RWF" | "SAR" | "SBD" | "SCR" | "SDG" | "SEK" | "SGD" | "SHP" | "SLL" | "SOS" | "SPL" | "SRD" | "STD" | "SVC" | "SYP" | "SZL" | "THB" | "TJS" | "TMT" | "TND" | "TOP" | "TRY" | "TTD" | "TVD" | "TWD" | "TZS" | "UAH" | "UGX" | "USD" | "UYU" | "UZS" | "VEF" | "VND" | "VUV" | "WST" | "XAF" | "XCD" | "XDR" | "XOF" | "XPF" | "YER" | "ZAR" | "ZMW" | "ZWD"; | ||
export declare type StandardEvents = { | ||
export type Currency = "AED" | "AFN" | "ALL" | "AMD" | "ANG" | "AOA" | "ARS" | "AUD" | "AWG" | "AZN" | "BAM" | "BBD" | "BDT" | "BGN" | "BHD" | "BIF" | "BMD" | "BND" | "BOB" | "BRL" | "BSD" | "BTN" | "BWP" | "BYR" | "BZD" | "CAD" | "CDF" | "CHF" | "CLP" | "CNY" | "COP" | "CRC" | "CUC" | "CUP" | "CVE" | "CZK" | "DJF" | "DKK" | "DOP" | "DZD" | "EGP" | "ERN" | "ETB" | "EUR" | "FJD" | "FKP" | "GBP" | "GEL" | "GGP" | "GHS" | "GIP" | "GMD" | "GNF" | "GTQ" | "GYD" | "HKD" | "HNL" | "HRK" | "HTG" | "HUF" | "IDR" | "ILS" | "IMP" | "INR" | "IQD" | "IRR" | "ISK" | "JEP" | "JMD" | "JOD" | "JPY" | "KES" | "KGS" | "KHR" | "KMF" | "KPW" | "KRW" | "KWD" | "KYD" | "KZT" | "LAK" | "LBP" | "LKR" | "LRD" | "LSL" | "LYD" | "MAD" | "MDL" | "MGA" | "MKD" | "MMK" | "MNT" | "MOP" | "MRO" | "MUR" | "MVR" | "MWK" | "MXN" | "MYR" | "MZN" | "NAD" | "NGN" | "NIO" | "NOK" | "NPR" | "NZD" | "OMR" | "PAB" | "PEN" | "PGK" | "PHP" | "PKR" | "PLN" | "PYG" | "QAR" | "RON" | "RSD" | "RUB" | "RWF" | "SAR" | "SBD" | "SCR" | "SDG" | "SEK" | "SGD" | "SHP" | "SLL" | "SOS" | "SPL" | "SRD" | "STD" | "SVC" | "SYP" | "SZL" | "THB" | "TJS" | "TMT" | "TND" | "TOP" | "TRY" | "TTD" | "TVD" | "TWD" | "TZS" | "UAH" | "UGX" | "USD" | "UYU" | "UZS" | "VEF" | "VND" | "VUV" | "WST" | "XAF" | "XCD" | "XDR" | "XOF" | "XPF" | "YER" | "ZAR" | "ZMW" | "ZWD"; | ||
export type StandardEvents = { | ||
productAddedToCart: { | ||
@@ -13,7 +13,21 @@ product: unknown; | ||
product: unknown; | ||
event?: unknown; | ||
newQuantity: number; | ||
}; | ||
getCart: { | ||
cartFetched: { | ||
cart: unknown; | ||
}; | ||
cartUpdated: { | ||
account?: { | ||
email: string; | ||
}; | ||
shipping?: unknown; | ||
billing?: unknown; | ||
}; | ||
shippingMethodsFetched: { | ||
shippingMethods: unknown[]; | ||
}; | ||
shippingMethodUpdated: { | ||
shippingMethod: unknown; | ||
event?: unknown; | ||
}; | ||
discountCodeRedeemed: { | ||
@@ -27,13 +41,6 @@ discountCode: string; | ||
}; | ||
getShippingMethods: { | ||
shippingMethods: unknown[]; | ||
}; | ||
shippingMethodUpdated: { | ||
shippingMethod: unknown; | ||
event?: unknown; | ||
}; | ||
cartCheckedOut: { | ||
cartId?: string; | ||
}; | ||
getOrder: { | ||
orderFetched: { | ||
order: unknown; | ||
@@ -44,3 +51,3 @@ }; | ||
}; | ||
getOrderHistory: { | ||
orderHistoryFetched: { | ||
orders: unknown[]; | ||
@@ -59,3 +66,3 @@ }; | ||
}; | ||
getAccountInfo: { | ||
accountInfoFetched: { | ||
userInfo: unknown; | ||
@@ -109,9 +116,9 @@ }; | ||
}; | ||
getWishlist: { | ||
wishlistFetched: { | ||
wishlist: unknown; | ||
}; | ||
}; | ||
export declare type DynamicEvent = { | ||
export type DynamicEvent = { | ||
[key: string]: unknown; | ||
}; | ||
export declare type StandardAction = "account/getAccount" | "account/register" | "account/requestConfirmationEmail" | "account/confirm" | "account/login" | "account/logout" | "account/password" | "account/requestReset" | "account/reset" | "account/update" | "account/addAddress" | "account/updateAddress" | "account/removeAddress" | "account/setDefaultBillingAddress" | "account/setDefaultShippingAddress" | "cart/getCart" | "cart/addToCart" | "cart/updateLineItem" | "cart/removeLineItem" | "cart/updateCart" | "cart/checkout" | "cart/getOrders" | "cart/getShippingMethods" | "cart/getAvailableShippingMethods" | "cart/setShippingMethod" | "cart/addPaymentByInvoice" | "cart/updatePayment" | "cart/redeemDiscount" | "cart/removeDiscount" | "product/getProduct" | "product/query" | "product/queryCategories" | "product/searchableAttributes" | "wishlist/getWishlist" | "wishlist/createWishlist" | "wishlist/addToWishlist" | "wishlist/removeLineItem" | "wishlist/updateLineItemCount" | "project/getProjectSettings" | "payment/createSession" | "payment/notifications"; | ||
export type StandardAction = "account/getAccount" | "account/register" | "account/requestConfirmationEmail" | "account/confirm" | "account/login" | "account/logout" | "account/password" | "account/requestReset" | "account/reset" | "account/update" | "account/addAddress" | "account/updateAddress" | "account/removeAddress" | "account/setDefaultBillingAddress" | "account/setDefaultShippingAddress" | "cart/getCart" | "cart/addToCart" | "cart/updateLineItem" | "cart/removeLineItem" | "cart/updateCart" | "cart/checkout" | "cart/getOrders" | "cart/getShippingMethods" | "cart/getAvailableShippingMethods" | "cart/setShippingMethod" | "cart/addPaymentByInvoice" | "cart/updatePayment" | "cart/redeemDiscount" | "cart/removeDiscount" | "product/getProduct" | "product/query" | "product/queryCategories" | "product/searchableAttributes" | "wishlist/getWishlist" | "wishlist/createWishlist" | "wishlist/addToWishlist" | "wishlist/removeLineItem" | "wishlist/updateLineItemCount" | "project/getProjectSettings" | "payment/createSession" | "payment/notifications"; |
{ | ||
"name": "@commercetools/frontend-sdk", | ||
"version": "2.0.0-alpha.0", | ||
"version": "2.1.0-alpha.0", | ||
"license": "UNLICENSED", | ||
@@ -16,4 +16,3 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"js-cookie": "3.0.1", | ||
"typescript": "4.8.3" | ||
"js-cookie": "3.0.1" | ||
}, | ||
@@ -26,4 +25,5 @@ "devDependencies": { | ||
"esbuild": "0.15.8", | ||
"prettier": "2.7.1" | ||
"prettier": "2.7.1", | ||
"typescript": "^4.9.3" | ||
} | ||
} |
import Cookies from "js-cookie"; | ||
import { rememberMeCookie } from "../helpers/cookieManagement"; | ||
import { FetchError } from "../library/FetchError"; | ||
@@ -12,3 +13,3 @@ const cookiesApi = Cookies.withAttributes({ path: "/" }); | ||
options: RequestInit = {}, | ||
): Promise<T> => { | ||
): Promise<T | FetchError> => { | ||
const sessionCookie = cookiesApi.get("frontastic-session"); | ||
@@ -51,3 +52,3 @@ | ||
let error: any | string; | ||
let error: Error | string; | ||
@@ -60,7 +61,3 @@ try { | ||
if (error.error) { | ||
throw new Error(error.errorCode); | ||
} | ||
return error; | ||
return new FetchError(error); | ||
}; |
@@ -5,2 +5,3 @@ import { rememberMeCookie } from "./helpers/cookieManagement"; | ||
import Event from "./library/Event"; | ||
import { FetchError } from "./library/FetchError"; | ||
@@ -14,3 +15,4 @@ const sdk = new SDK(); | ||
Event, | ||
FetchError, | ||
rememberMeCookie | ||
}; |
@@ -5,2 +5,3 @@ import { fetcher } from "../helpers/fetcher"; | ||
import { Currency, DynamicEvent, StandardEvents } from "./types"; | ||
import { FetchError } from "./FetchError"; | ||
@@ -94,9 +95,13 @@ export class SDK extends EventManager<StandardEvents & DynamicEvent> { | ||
#isVoid(option: void | {}): option is void { | ||
return !!!option; | ||
} | ||
async callAction<T>( | ||
actionName: string, | ||
payload: unknown, | ||
payload: unknown = {}, | ||
query?: { | ||
[key: string]: string | number | boolean | ||
} | ||
): Promise<T | Error> { | ||
): Promise<T & { isError?: boolean } | FetchError> { | ||
this.#throwIfNotConfigured(); | ||
@@ -114,26 +119,26 @@ let params = ""; | ||
} | ||
try { | ||
const result = await this.#actionQueue.add<T>(() => { | ||
return fetcher<T>( | ||
this.#normaliseUrl(`${this.#endpoint}/frontastic/action/${actionName}${params}`), | ||
{ | ||
method: "POST", | ||
body: JSON.stringify(payload), | ||
headers: { | ||
'Frontastic-Locale': this.APILocale, | ||
//'Commercetools-Locale': this.APILocale // TODO: unsupported, needs backend work | ||
} | ||
}, | ||
); | ||
}); | ||
return result; | ||
} catch (error) { | ||
if (typeof error === "string") { | ||
return new Error(error); | ||
} | ||
return error as Error; | ||
const result = await this.#actionQueue.add<T | FetchError>(() => { | ||
return fetcher<T>( | ||
this.#normaliseUrl(`${this.#endpoint}/frontastic/action/${actionName}${params}`), | ||
{ | ||
method: "POST", | ||
body: JSON.stringify(payload), | ||
headers: { | ||
'Frontastic-Locale': this.APILocale, | ||
//'Commercetools-Locale': this.APILocale // TODO: unsupported, needs backend work | ||
} | ||
}, | ||
); | ||
}).catch(error => { | ||
return new FetchError(error as string | Error); | ||
}); | ||
if (this.#isVoid(result as any)) { | ||
return {} as T & { isError?: boolean }; | ||
} | ||
return result as T & { isError?: boolean } | FetchError; | ||
} | ||
async getPage<T>(path: string) { | ||
async getPage<T>(path: string): Promise<T & { isError?: boolean } | FetchError> { | ||
const options = { | ||
@@ -148,15 +153,14 @@ headers: { | ||
try { | ||
const result = fetcher<T>( | ||
this.#normaliseUrl(`${this.#endpoint}/page`), | ||
options | ||
); | ||
return result | ||
} catch (error) { | ||
if (typeof error === "string") { | ||
return new Error(error); | ||
} | ||
return error as Error; | ||
const result = await fetcher<T>( | ||
this.#normaliseUrl(`${this.#endpoint}/page`), | ||
options | ||
).catch(error => { | ||
return new FetchError(error as string | Error); | ||
}); | ||
if (this.#isVoid(result as any)) { | ||
return {} as T & { isError?: boolean }; | ||
} | ||
return result as T & { isError?: boolean } | FetchError; | ||
} | ||
} |
@@ -166,14 +166,21 @@ export type Currency = | ||
export type StandardEvents = { | ||
productAddedToCart: { product: unknown; quantity: number }; | ||
productRemovedFromCart: { product: unknown; quantity: number }; | ||
productUpdatedInCart: { product: unknown; event?: unknown }; | ||
getCart: { cart: unknown }; | ||
productAddedToCart: { product: unknown; quantity: number }; //done | ||
productRemovedFromCart: { product: unknown; quantity: number }; //done | ||
productUpdatedInCart: { product: unknown; newQuantity: number }; //done | ||
cartFetched: { cart: unknown }; //done | ||
cartUpdated: { | ||
account?: { | ||
email: string; | ||
}; | ||
shipping?: unknown; | ||
billing?: unknown; | ||
}; | ||
shippingMethodsFetched: { shippingMethods: unknown[] }; | ||
shippingMethodUpdated: { shippingMethod: unknown; event?: unknown }; | ||
discountCodeRedeemed: { discountCode: string; cart?: unknown }; | ||
discountCodeRemoved: { discountCode: string; cart?: unknown }; | ||
getShippingMethods: { shippingMethods: unknown[] }; | ||
shippingMethodUpdated: { shippingMethod: unknown; event?: unknown }; | ||
cartCheckedOut: { cartId?: string }; | ||
getOrder: { order: unknown }; | ||
orderFetched: { order: unknown }; | ||
orderPlaced: { order: unknown }; | ||
getOrderHistory: { orders: unknown[] }; | ||
orderHistoryFetched: { orders: unknown[] }; | ||
orderHistoryUpdated: { order: unknown; event?: unknown }; | ||
@@ -183,3 +190,3 @@ userLoggedIn: { userInfo: unknown }; | ||
userRegistered: { email: string }; | ||
getAccountInfo: { userInfo: unknown }; | ||
accountInfoFetched: { userInfo: unknown }; | ||
accountConfirmed?: { email: string }; | ||
@@ -207,3 +214,3 @@ accountConfirmationEmailRequested?: { email: string }; | ||
productRemovedFromWishlist: { productId: string }; | ||
getWishlist: { wishlist: unknown }; | ||
wishlistFetched: { wishlist: unknown }; | ||
}; | ||
@@ -210,0 +217,0 @@ |
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
33360
1
27
882
7
- Removedtypescript@4.8.3
- Removedtypescript@4.8.3(transitive)