UNPKG

@apicart/store-sdk

Version:

Apicart SDK for integrating store into any web application

509 lines (435 loc) 11.5 kB
import Apicart from '@apicart/core-sdk'; import Cart from '../Entity/Cart/Cart'; import CartItemParameter from '../Entity/Cart/CartItemParameter'; import Order from '../Entity/Order/Order'; import { DataSourceInterface } from '../Contract/DataSource/DataSourceInterface'; import Customer from '../Entity/Customer/Customer'; import PaymentMethod from '../Entity/Payment/PaymentMethod'; import ShippingMethod from '../Entity/Shipping/ShippingMethod'; export default class Api implements DataSourceInterface { private _token: string; private _parameters: any = {}; constructor(token: string, parameters: Record<string, any> = {}) { if (!Apicart.Utils.Objects.keyExists(parameters, 'url')) { throw 'Parameter "dataSource.url" is missing.'; } this._token = token; this._parameters = parameters; } /** @inheritdoc */ public async findCart(hash: string): Promise<Cart | null> { const query = ` query findCart($input: FindCartQueryInput!){ findCart(input: $input) { cart { all } } }` ; const response = await this.call(query, { input: { cartHash: hash } }); const responseData = this.getResponseData(response, 'findCart.cart.all'); return responseData ? Cart.deserialize(responseData) : null; } /** @inheritdoc */ public async fullCartSynchronization(cart: Cart): Promise<boolean> { const query = ` mutation fullCartSynchronization($input: FullCartSynchronizationMutationInput!){ fullCartSynchronization(input: $input) { result } }` ; const response = await this.call(query, { input: { cart: cart.serializeForGraphQL() } }); return this.isSuccessResult(response, 'fullCartSynchronization'); } /** @inheritdoc */ public async addCartItem( cartHash: string, dataUrl: string, quantity = 1, parameters: [] = [] ): Promise<boolean> { const query = ` mutation addCartItems($input: AddCartItemsMutationInput!){ addCartItems(input: $input) { message, result } }` ; const cartItemParameters = {}; Apicart.Utils.Loops.forEach(parameters, (parameter: CartItemParameter): void => { cartItemParameters[parameter.getKey()] = parameter.getValue(); }); const response = await this.call(query, { input: { cartHash: cartHash, items: [ { dataUrl: dataUrl, quantity: quantity, parameters: cartItemParameters } ] } }); return this.isSuccessResult(response, 'addCartItems'); } /** @inheritdoc */ public async removeCartItem(cartHash: string, dataUrl: string, quantity: number | null = null): Promise<any> { const query = ` mutation removeCartItems($input: RemoveCartItemsMutationInput!){ removeCartItems(input: $input) { message, result } }` ; const response = await this.call(query, { input: { cartHash: cartHash, items: [ { dataUrl: dataUrl, quantity: quantity } ] } }); return this.isSuccessResult(response, 'removeCartItems'); } /** @inheritdoc */ public async addCartParameters(cartHash: string, parameters: Record<string, any>): Promise<boolean> { const query = ` mutation addCartParameters($input: AddCartParametersMutationInput!){ addCartParameters(input: $input) { message, result } }` ; const response = await this.call(query, { input: { cartHash: cartHash, parameters: parameters } }); return this.isSuccessResult(response, 'addCartParameters'); } /** @inheritdoc */ public async removeCartParameters(cartHash: string, keys: []): Promise<boolean> { const query = ` mutation removeCartParameters($input: RemoveCartParametersMutationInput!){ removeCartParameters(input: $input) { message, result } }` ; const response = await this.call(query, { input: { cartHash: cartHash, parameters: keys } }); return this.isSuccessResult(response, 'removeCartParameters'); } /** @inheritdoc */ public async finishCart(cartHash: string): Promise<Order | null> { const query = ` mutation finishCart($input: FinishCartMutationInput!){ finishCart(input: $input) { order { all } } }` ; const response = await this.call(query, { input: { cartHash: cartHash } }); const responseData = this.getResponseData(response, 'finishCart.order.all'); return responseData ? Order.deserialize(responseData) : null; } /** @inheritdoc */ public async findCustomer(hash: string): Promise<Customer | null> { const query = ` query findCustomer($input: FindCustomerQueryInput!){ findCustomer(input: $input) { customer { all } } }` ; const response = await this.call(query, { input: { customerHash: hash } }); const responseData = this.getResponseData(response, 'findCustomer.customer.all'); return responseData ? Customer.deserialize(responseData) : null; } /** @inheritdoc */ public async fullCustomerSynchronization(customer: Customer): Promise<boolean> { const query = ` mutation fullCustomerSynchronization($input: FullCustomerSynchronizationMutationInput!){ fullCustomerSynchronization(input: $input) { result } }` ; const response = await this.call(query, { input: { customer: customer.serializeForGraphQL() } }); return this.isSuccessResult(response, 'fullCustomerSynchronization'); } /** @inheritdoc */ public async addCustomerParameters(customerHash: string, parameters: Record<string, any>): Promise<boolean> { const query = ` mutation addCustomerParameters($input: AddCustomerParametersMutationInput!){ addCustomerParameters(input: $input) { result } }` ; const response = await this.call(query, { input: { customerHash: customerHash, parameters: parameters } }); return this.isSuccessResult(response, 'addCustomerParameters'); } /** @inheritdoc */ public async removeCustomerParameters(customerHash: string, keys: string[]): Promise<boolean> { const query = ` mutation removeCustomerParameters($input: RemoveCustomerParametersMutationInput!){ removeCustomerParameters(input: $input) { result } }` ; const response = await this.call(query, { input: { customerHash: customerHash, parameters: keys } }); return this.isSuccessResult(response, 'removeCustomerParameters'); } /** @inheritdoc */ public async findOrder(orderHash: string): Promise<Order | null> { const query = ` query findOrder($input: FindOrderQueryInput!){ findOrder(input: $input) { order { all } } }` ; const response = await this.call(query, { input: { orderHash: orderHash } }); const responseData = this.getResponseData(response, 'findOrder.order.all'); return responseData ? Order.deserialize(responseData) : null; } /** @inheritdoc */ public async changeOrderState(orderHash: string, orderStateCode: string): Promise<boolean> { const query = ` mutation changeOrderState($input: ChangeOrderStateMutationInput!){ changeOrderState(input: $input) { result } } `; const response = await this.call(query, { input: { orderHash: orderHash, orderStateCode: orderStateCode } }); return this.isSuccessResult(response, 'changeOrderState'); } /** @inheritdoc */ public async addOrderParameters(orderHash: string, parameters: []): Promise<boolean> { const query = ` mutation addOrderParameters($input: AddOrderParametersMutationInput!){ addOrderParameters(input: $input) { message, result } }` ; const response = await this.call(query, { input: { orderHash: orderHash, orderStateCode: parameters } }); return this.isSuccessResult(response, 'addOrderParameters'); } /** @inheritdoc */ public async removeOrderParameters(orderHash: string, keys: []): Promise<boolean> { const query = ` mutation removeOrderParameters($input: RemoveOrderParametersMutationInput!){ removeOrderParameters(input: $input) { message, result } }` ; const response = await this.call(query, { input: { orderHash: orderHash, orderStateCode: keys } }); return this.isSuccessResult(response, 'removeOrderParameters'); } /** @inheritdoc */ public async findPaymentMethod(id: number): Promise<PaymentMethod | null> { const query = ` query findPaymentMethods($input: FindPaymentMethodsQueryInput!){ findPaymentMethods(input: $input) { paymentMethods { all } } }` ; const response = await this.call(query, { input: { id: id } }); const responseData = this.getResponseData(response, 'findPaymentMethods.paymentMethods.0'); return responseData ? PaymentMethod.deserialize(responseData) : null; } /** @inheritdoc */ public async findPaymentMethods(onlyEnabled: boolean): Promise<PaymentMethod[] | null> { const query = ` query findPaymentMethods($input: FindPaymentMethodsQueryInput!){ findPaymentMethods(input: $input) { paymentMethods { all } } }` ; const response = await this.call(query, { input: { onlyEnabled: onlyEnabled } }); const responseData = this.getResponseData(response, 'findPaymentMethods.paymentMethods'); if (responseData) { const paymentMethods: PaymentMethod[] = []; Apicart.Utils.Loops.forEach( responseData, (paymentMethodData: any): void => { paymentMethods.push(PaymentMethod.deserialize(paymentMethodData.all)); } ); return paymentMethods; } return null; } /** @inheritdoc */ public async findShippingMethod(id: number): Promise<ShippingMethod | null> { const query = ` query findShippingMethods($input: FindShippingMethodsQueryInput!){ findShippingMethods(input: $input) { shippingMethods { all } } }` ; const response = await this.call(query, { input: { id: id } }); const responseData = this.getResponseData(response, 'findShippingMethods.shippingMethods.all'); return responseData ? ShippingMethod.deserialize(responseData) : null; } /** @inheritdoc */ public async findShippingMethods(onlyEnabled: boolean): Promise<ShippingMethod[]> { const query = ` query findShippingMethods($input: FindShippingMethodsQueryInput!){ findShippingMethods(input: $input) { shippingMethods { all } } }` ; const response = await this.call(query, { input: { onlyEnabled: onlyEnabled } }); const responseData = this.getResponseData(response, 'findShippingMethods.shippingMethods'); if (responseData) { const shippingMethods: ShippingMethod[] = []; Apicart.Utils.Loops.forEach( responseData, (shippingMethodData: any): void => { shippingMethods.push(ShippingMethod.deserialize(shippingMethodData.all)); } ); return shippingMethods; } return null; } private call(query: string, variables: any): Promise<any> { variables.input.token = this._token; return Apicart.ApiCommunicator.call( this._parameters.url, query, variables, this._parameters.connectTimeout, this._parameters.withCredentials ); } private getResponseData(response: any|null, keyPath: string): any|null { return Apicart.Utils.Objects.find(response, 'data.data.' + keyPath); } private isSuccessResult(response: any | null = null, key: string, throwOnFailed = true): boolean { return Apicart.ApiCommunicator.isSuccessResult(response, key, throwOnFailed); } }