diff --git a/packages/shopware-6-client/__tests__/services/contextService.spec.ts b/packages/shopware-6-client/__tests__/services/contextService.spec.ts new file mode 100644 index 000000000..dee2cefc0 --- /dev/null +++ b/packages/shopware-6-client/__tests__/services/contextService.spec.ts @@ -0,0 +1,58 @@ +import { ContextService } from "../../src/index"; + +describe("ContextService", () => { + describe("getCurrencies", () => { + it("should return all currencies", async () => { + try { + const result = await ContextService.getCurrencies(); + expect(result.data).toHaveLength(3); + } catch (e) { + console.error("Connection problem", e); + } + }); + }); + + describe("getLanguages", () => { + it("should return all languages", async () => { + try { + const result = await ContextService.getLanguages(); + expect(result.data).toHaveLength(2); + } catch (e) { + console.error("Connection problem", e); + } + }); + }); + + describe("getCountries", () => { + it("should return all countries", async () => { + try { + const result = await ContextService.getLanguages(); + expect(result.data).toHaveLength(2); + } catch (e) { + console.error("Connection problem", e); + } + }); + }); + + describe("getPaymentMethods", () => { + it("should return all payment methods", async () => { + try { + const result = await ContextService.getPaymentMethods(); + expect(result.data).toHaveLength(4); + } catch (e) { + console.error("Connection problem", e); + } + }); + }); + + describe("getShippingMethods", () => { + it("should return all shipping methods", async () => { + try { + const result = await ContextService.getShippingMethods(); + expect(result.data).toHaveLength(2); + } catch (e) { + console.error("Connection problem", e); + } + }); + }); +}); diff --git a/packages/shopware-6-client/src/index.ts b/packages/shopware-6-client/src/index.ts index 77d8d9879..f673f2c2c 100644 --- a/packages/shopware-6-client/src/index.ts +++ b/packages/shopware-6-client/src/index.ts @@ -4,6 +4,7 @@ import { reloadConfiguration } from "./apiService"; export { config } from "./settings"; export { Category, CategoryService } from "./categoryService"; export { ProductService } from "./services/productService"; +export { ContextService } from "./services/contextService"; export function setup(config: ClientSettings = {}): void { setupConfig(config); diff --git a/packages/shopware-6-client/src/services/contextService.ts b/packages/shopware-6-client/src/services/contextService.ts new file mode 100644 index 000000000..3fcc27afb --- /dev/null +++ b/packages/shopware-6-client/src/services/contextService.ts @@ -0,0 +1,75 @@ +import { Currency } from "../interfaces/models/system/currency/Currency"; +import { apiService } from "../apiService"; +import { config } from "../settings"; +import { + getContextCurrencyEndpoint, + getContextCountryEndpoint, + getContextPaymentMethodEndpoint, + getContextShippingMethodEndpoint, + getContextLanguageEndpoint +} from "../endpoints"; +import { Country } from "../interfaces/models/system/country/Country"; +import { ShippingMethod } from "../interfaces/models/checkout/shipping/ShippingMethod"; +import { PaymentMethod } from "../interfaces/models/checkout/payment/PaymentMethod"; +import { Language } from "../interfaces/models/framework/language/Language"; +import { SearchResult } from "../interfaces/response/SearchResult"; + +const getCurrencies = async function(): Promise> { + const resp = await apiService.get( + config.endpoint + getContextCurrencyEndpoint() + ); + + return resp.data; +}; + +const getLanguages = async function(): Promise> { + const resp = await apiService.get( + config.endpoint + getContextLanguageEndpoint() + ); + + return resp.data; +}; + +const getCountries = async function(): Promise> { + const resp = await apiService.get( + config.endpoint + getContextCountryEndpoint() + ); + + return resp.data; +}; + +const getPaymentMethods = async function(): Promise< + SearchResult +> { + const resp = await apiService.get( + config.endpoint + getContextPaymentMethodEndpoint() + ); + + return resp.data; +}; + +const getShippingMethods = async function(): Promise< + SearchResult +> { + const resp = await apiService.get( + config.endpoint + getContextShippingMethodEndpoint() + ); + + return resp.data; +}; + +interface ContextService { + getCurrencies: () => Promise>; + getLanguages: () => Promise>; + getCountries: () => Promise>; + getPaymentMethods: () => Promise>; + getShippingMethods: () => Promise>; +} + +export const ContextService: ContextService = { + getCurrencies, + getLanguages, + getCountries, + getPaymentMethods, + getShippingMethods +};