Skip to content

Commit

Permalink
Extend Client with tokens and currency options
Browse files Browse the repository at this point in the history
  • Loading branch information
letelete committed Aug 19, 2022
1 parent c31ae1d commit 7866580
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 63 deletions.
128 changes: 79 additions & 49 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
}
}

Expand Down
23 changes: 17 additions & 6 deletions src/Http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ResponseType = JsonApiResponse>(
method: HttpMethod,
url: string,
tokens: IToken = {},
userTokens: IToken = {},
userParams: any = {},
responseParsing: ResponseParsing = 'automatic',
responseParsing: ResponseParsing = 'automatic'
): Promise<ResultResponse<ResponseType>> {
try {
const headers = this.spreeOrderHeaders(tokens)
const headers = this.spreeOrderHeaders(userTokens)
const params = this.spreeParams(userParams)

const fetchOptions: FetchConfig = {
Expand Down Expand Up @@ -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
Expand All @@ -121,6 +131,7 @@ export default class Http {
protected spreeParams(userParams: any): Record<string, any> {
const params = {
locale: this.locale,
currency: this.currency,
...userParams
}

Expand Down
1 change: 1 addition & 0 deletions src/interfaces/Currency.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Currency = string
1 change: 1 addition & 0 deletions src/interfaces/Locale.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Locale = string
20 changes: 12 additions & 8 deletions src/interfaces/Token.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { ResultResponse } from './ResultResponse'

export type BearerToken = string

export type OrderToken = string

/**
* @deprecated Use
* {@link RequiredAnyToken},
Expand All @@ -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
Expand Down

0 comments on commit 7866580

Please sign in to comment.