diff --git a/src/Client.ts b/src/Client.ts index 7cdd644a..d8c6d9f0 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -15,7 +15,9 @@ import { } from './endpoints' import { EndpointOptions } from './Http' import type { CreateFetcherConfig, Fetcher, IClientConfig } from './interfaces/ClientConfig' - +import { Currency } from './interfaces/Currency' +import { Locale } from './interfaces/Locale' +import { BearerToken, OrderToken } from './interfaces/Token' class Client { public account: Account public authentication: Authentication @@ -33,7 +35,10 @@ class Client { protected host: string protected fetcher: Fetcher - private locale: string | undefined + + private tokens: EndpointOptions['tokens'] = {} + private locale: EndpointOptions['locale'] | undefined + private currency: EndpointOptions['currency'] | undefined constructor(customOptions: IClientConfig) { const spreeHostEnvironmentValue: string | null = (globalThis.process && globalThis.process.env.SPREE_HOST) || null @@ -50,90 +55,115 @@ class Client { const fetcherOptions: CreateFetcherConfig = { host: options.host } this.fetcher = options.createFetcher(fetcherOptions) - this.addEndpoints() } - public withLocale(locale: string): this { - this.locale = locale - this.addEndpoints() - return this + public withOrderToken(orderToken: OrderToken): this { + return this.withEndpointBuilder(() => { + this.tokens.orderToken = orderToken + }) + } + + public withBearerToken(bearerToken: BearerToken): this { + return this.withEndpointBuilder(() => { + this.tokens.bearerToken = bearerToken + }) } - protected endpointOptions(): EndpointOptions { + public withLocale(locale: Locale): this { + return this.withEndpointBuilder(() => { + this.locale = locale + }) + } + + public withCurrency(currency: Currency): this { + return this.withEndpointBuilder(() => { + this.currency = currency + }) + } + + protected getEndpointOptions(): EndpointOptions { return { fetcher: this.fetcher, - locale: this.locale + tokens: this.tokens, + locale: this.locale, + currency: this.currency } } protected addEndpoints(): void { - this.account = this.makeAccount() - this.authentication = this.makeAuthentication() - this.cart = this.makeCart() - this.checkout = this.makeCheckout() - this.countries = this.makeCountries() - this.digitalAssets = this.makeDigitalAssets() - this.menus = this.makeMenus() - this.order = this.makeOrder() - this.pages = this.makePages() - this.products = this.makeProducts() - this.taxons = this.makeTaxons() - this.vendors = this.makeVendors() - this.wishlists = this.makeWishlists() + const options = this.getEndpointOptions() + this.account = this.makeAccount(options) + this.authentication = this.makeAuthentication(options) + this.cart = this.makeCart(options) + this.checkout = this.makeCheckout(options) + this.countries = this.makeCountries(options) + this.digitalAssets = this.makeDigitalAssets(options) + this.menus = this.makeMenus(options) + this.order = this.makeOrder(options) + this.pages = this.makePages(options) + this.products = this.makeProducts(options) + this.taxons = this.makeTaxons(options) + this.vendors = this.makeVendors(options) + this.wishlists = this.makeWishlists(options) + } + + protected withEndpointBuilder(fn: () => void): this { + fn() + this.addEndpoints() + return this } - - protected makeAccount(): Account { - return new Account({ ...this.endpointOptions() }) + protected makeAccount(options: EndpointOptions): Account { + return new Account(options) } - protected makeAuthentication(): Authentication { - return new Authentication({ ...this.endpointOptions() }) + protected makeAuthentication(options: EndpointOptions): Authentication { + return new Authentication(options) } - protected makeCart(): Cart { - return new Cart({ ...this.endpointOptions() }) + protected makeCart(options: EndpointOptions): Cart { + return new Cart(options) } - protected makeCheckout(): Checkout { - return new Checkout({ ...this.endpointOptions() }) + protected makeCheckout(options: EndpointOptions): Checkout { + return new Checkout(options) } - protected makeCountries(): Countries { - return new Countries({ ...this.endpointOptions() }) + protected makeCountries(options: EndpointOptions): Countries { + return new Countries(options) } - protected makeOrder(): Order { - return new Order({ ...this.endpointOptions() }) + protected makeOrder(options: EndpointOptions): Order { + return new Order(options) } - protected makePages(): Pages { - return new Pages({ ...this.endpointOptions() }) + protected makePages(options: EndpointOptions): Pages { + return new Pages(options) } - protected makeProducts(): Products { - return new Products({ ...this.endpointOptions() }) + protected makeProducts(options: EndpointOptions): Products { + return new Products(options) } - protected makeTaxons(): Taxons { - return new Taxons({ ...this.endpointOptions() }) + protected makeTaxons(options: EndpointOptions): Taxons { + return new Taxons(options) } - protected makeDigitalAssets(): DigitalAssets { - return new DigitalAssets({ ...this.endpointOptions() }) + protected makeDigitalAssets(options: EndpointOptions): DigitalAssets { + return new DigitalAssets(options) } - protected makeMenus(): Menus { - return new Menus({ ...this.endpointOptions() }) + protected makeMenus(options: EndpointOptions): Menus { + return new Menus(options) } - protected makeVendors(): Vendors { - return new Vendors({ ...this.endpointOptions() }) + protected makeVendors(options: EndpointOptions): Vendors { + return new Vendors(options) } - protected makeWishlists(): Wishlists { - return new Wishlists({ ...this.endpointOptions() }) + protected makeWishlists(options: EndpointOptions): Wishlists { + return new Wishlists(options) } } diff --git a/src/Http.ts b/src/Http.ts index def6655f..3adf280b 100644 --- a/src/Http.ts +++ b/src/Http.ts @@ -17,27 +17,33 @@ import type { IToken } from './interfaces/Token' export type EndpointOptions = { fetcher: Fetcher + tokens?: IToken locale?: string + currency?: string } export default class Http { public fetcher: Fetcher - public locale: string | undefined + public tokens: EndpointOptions['tokens'] + public locale: EndpointOptions['locale'] | undefined + public currency: EndpointOptions['currency'] | undefined - constructor({ fetcher, locale }: EndpointOptions) { + constructor({ fetcher, tokens, locale, currency }: EndpointOptions) { this.fetcher = fetcher + this.tokens = tokens || {} this.locale = locale + this.currency = currency } protected async spreeResponse( method: HttpMethod, url: string, - tokens: IToken = {}, + userTokens: IToken = {}, userParams: any = {}, - responseParsing: ResponseParsing = 'automatic', + responseParsing: ResponseParsing = 'automatic' ): Promise> { try { - const headers = this.spreeOrderHeaders(tokens) + const headers = this.spreeOrderHeaders(userTokens) const params = this.spreeParams(userParams) const fetchOptions: FetchConfig = { @@ -104,8 +110,12 @@ export default class Http { } } - protected spreeOrderHeaders(tokens: IToken): { [headerName: string]: string } { + protected spreeOrderHeaders(userTokens: IToken): { [headerName: string]: string } { const header = {} + const tokens = { + ...this.tokens, + ...userTokens + } if (tokens.orderToken) { header['X-Spree-Order-Token'] = tokens.orderToken @@ -121,6 +131,7 @@ export default class Http { protected spreeParams(userParams: any): Record { const params = { locale: this.locale, + currency: this.currency, ...userParams } diff --git a/src/interfaces/Currency.ts b/src/interfaces/Currency.ts new file mode 100644 index 00000000..622314e3 --- /dev/null +++ b/src/interfaces/Currency.ts @@ -0,0 +1 @@ +export type Currency = string diff --git a/src/interfaces/Locale.ts b/src/interfaces/Locale.ts new file mode 100644 index 00000000..eb85ed60 --- /dev/null +++ b/src/interfaces/Locale.ts @@ -0,0 +1 @@ +export type Locale = string diff --git a/src/interfaces/Token.ts b/src/interfaces/Token.ts index 92871937..68ceba83 100644 --- a/src/interfaces/Token.ts +++ b/src/interfaces/Token.ts @@ -1,5 +1,9 @@ import { ResultResponse } from './ResultResponse' +export type BearerToken = string + +export type OrderToken = string + /** * @deprecated Use * {@link RequiredAnyToken}, @@ -10,21 +14,21 @@ import { ResultResponse } from './ResultResponse' * instead. */ export interface IToken { - orderToken?: string - bearerToken?: string + orderToken?: OrderToken + bearerToken?: BearerToken } export type RequiredAnyToken = - | { order_token: string; bearer_token?: never } - | { order_token?: never; bearer_token: string } + | { order_token: OrderToken; bearer_token?: never } + | { order_token?: never; bearer_token: BearerToken } export type OptionalAnyToken = - | { order_token?: string; bearer_token?: never } - | { order_token?: never; bearer_token?: string } + | { order_token?: OrderToken; bearer_token?: never } + | { order_token?: never; bearer_token?: BearerToken } -export type RequiredAccountToken = { bearer_token: string } +export type RequiredAccountToken = { bearer_token: BearerToken } -export type OptionalAccountToken = { bearer_token?: string } +export type OptionalAccountToken = { bearer_token?: BearerToken } export interface IOAuthToken { access_token: string