UNPKG

toast-pos-sdk

Version:
1 lines 6.32 kB
import{default as t}from"axios";class e{constructor(e){this.token=e.token,this.axiosInstance=t.create({baseURL:e.host||"https://toast-api-server",timeout:e.timeout||3e4,headers:{"Content-Type":"application/json",Accept:"application/json"}}),this.axiosInstance.interceptors.request.use(t=>(this.token&&(t.headers.Authorization=`Bearer ${this.token}`),t)),this.axiosInstance.interceptors.response.use(t=>t,t=>{if(t.response&&t.response.status>=400){const e=t.response.data,s={message:e?.message||t.message||"An error occurred",code:e?.code||t.code,details:e};throw t.response.status,t.response.statusText,new Error(`Toast API Error (${t.response.status}): ${s.message}`)}throw t})}async request(t){try{const e=await this.axiosInstance.request({method:t.method,url:t.url,data:t.data,params:t.params,headers:t.headers});return{data:e.data,status:e.status,statusText:e.statusText}}catch(t){throw t}}async get(t,e){return this.request({method:"GET",url:t,params:e})}async post(t,e){return this.request({method:"POST",url:t,data:e})}async put(t,e){return this.request({method:"PUT",url:t,data:e})}async patch(t,e){return this.request({method:"PATCH",url:t,data:e})}async delete(t){return this.request({method:"DELETE",url:t})}}class s{constructor(t){this.httpClient=t}async listOrdersBulk(t){const{restaurantExternalId:e,...s}=t,a=new URLSearchParams;s.businessDate&&a.append("businessDate",s.businessDate),s.endDate&&a.append("endDate",s.endDate),void 0!==s.page&&a.append("page",s.page.toString()),void 0!==s.pageSize&&a.append("pageSize",s.pageSize.toString()),s.startDate&&a.append("startDate",s.startDate);const r=a.toString(),n="/orders/v2/ordersBulk"+(r?`?${r}`:"");return this.httpClient.request({method:"GET",url:n,headers:{"Toast-Restaurant-External-ID":e}})}async getOrder(t){const{guid:e,restaurantExternalId:s}=t;return this.httpClient.request({method:"GET",url:`/orders/v2/orders/${e}`,headers:{"Toast-Restaurant-External-ID":s}})}async getOrdersByDateRange(t,e,s,a={}){return this.listOrdersBulk({restaurantExternalId:t,startDate:e,endDate:s,...a})}async getOrdersByBusinessDate(t,e,s={}){return this.listOrdersBulk({restaurantExternalId:t,businessDate:e,...s})}async getAllOrders(t,e=10){const s=[];let a=t.page||1;for(;a<e;)try{const e=await this.listOrdersBulk({...t,page:a,pageSize:50});if(!e.data||0===e.data.length)break;if(s.push(...e.data),e.data.length<50)break;a++}catch(t){throw t}return s}}class a{constructor(t){this.httpClient=t}async listRestaurants(t={}){const e=new URLSearchParams;t.lastModified&&e.append("lastModified",t.lastModified);const s=e.toString(),a="/partners/v1/restaurants"+(s?`?${s}`:"");return this.httpClient.request({method:"GET",url:a})}async getAllRestaurants(){return this.listRestaurants()}async getRestaurantsModifiedSince(t){return this.listRestaurants({lastModified:t})}async findRestaurantsByName(t){const e=await this.listRestaurants();if(!e.data)return[];const s=t.toLowerCase();return e.data.filter(t=>t.restaurantName.toLowerCase().includes(s))}async getRestaurantByGuid(t){const e=await this.listRestaurants();return e.data&&e.data.find(e=>e.restaurantGuid===t)||null}async getActiveRestaurants(t={}){const e=await this.listRestaurants(t);return e.data?e.data.filter(t=>!t.deleted):[]}async getRestaurantsByManagementGroup(t){const e=await this.listRestaurants();return e.data?e.data.filter(e=>e.managementGroupGuid===t):[]}async getRestaurantById(t){const{restaurantGUID:e,restaurantExternalId:s,includeArchived:a}=t,r=new URLSearchParams;void 0!==a&&r.append("includeArchived",a.toString());const n=r.toString(),i=`/restaurants/v1/restaurants/${e}${n?`?${n}`:""}`;return this.httpClient.request({method:"GET",url:i,headers:{"Toast-Restaurant-External-ID":s}})}async getRestaurantsByManagementGroupId(t){const{managementGroupGUID:e,restaurantExternalId:s}=t;return this.httpClient.request({method:"GET",url:`/restaurants/v1/groups/${e}/restaurants`,headers:{"Toast-Restaurant-External-ID":s}})}async getMultipleRestaurantsById(t,e,s=!1){const a=[],r=t.map(t=>this.getRestaurantById({restaurantGUID:t,restaurantExternalId:e,includeArchived:s}));try{const t=await Promise.all(r);for(const e of t)e.data&&a.push(e.data)}catch(t){throw t}return a}async getDetailedRestaurantsByManagementGroup(t,e=!1){const s=await this.getRestaurantsByManagementGroupId(t);if(!s.data||0===s.data.length)return[];const a=s.data.map(t=>t.guid);return this.getMultipleRestaurantsById(a,t.restaurantExternalId,e)}}class r{constructor(t,r){if("string"==typeof t){if(!r)throw new Error("Host URL is required. Use ToastClient(host, token) or ToastClient(config)");this.config={host:t,token:r}}else{if(!t)throw new Error("Host URL and authentication token are required. Use ToastClient(host, token) or ToastClient(config)");this.config=t}if(!this.config.host)throw new Error("Host URL is required");if(!this.config.token)throw new Error("Authentication token is required");this.httpClient=new e(this.config),this._orders=new s(this.httpClient),this._restaurants=new a(this.httpClient)}getConfig(){return{...this.config}}getBaseUrl(){return this.config.host}setToken(t){this.config.token=t,this.httpClient=new e(this.config),this._orders=new s(this.httpClient),this._restaurants=new a(this.httpClient)}get http(){return this.httpClient}get orders(){return this._orders}get restaurants(){return this._restaurants}}class n{constructor(t){this.config=t}async getAccessToken(){if(!this.config.host)throw new Error("Host URL is required for authentication");const t=`${this.config.host}/authentication/v1/authentication/login`,e={clientId:this.config.clientId,clientSecret:this.config.clientSecret,userAccessType:"TOAST_MACHINE_CLIENT"};try{const s=await fetch(t,{method:"POST",headers:{"Content-Type":"application/json",Accept:"application/json"},body:JSON.stringify(e)});if(!s.ok){const t=await s.text();throw new Error(`Token request failed (${s.status}): ${t}`)}return(await s.json()).token.accessToken}catch(t){if(t instanceof Error)throw new Error(`Failed to obtain access token: ${t.message}`);throw new Error("Failed to obtain access token: Unknown error")}}static create(t){return new n(t)}}async function i(t){const e=new n(t);return await e.getAccessToken()}export{e as HttpClient,s as OrdersApi,a as RestaurantsApi,n as ToastAuth,r as ToastClient,r as default,i as getToastToken};