Skip to content

Commit 7866580

Browse files
committed
Extend Client with tokens and currency options
1 parent c31ae1d commit 7866580

File tree

5 files changed

+110
-63
lines changed

5 files changed

+110
-63
lines changed

src/Client.ts

Lines changed: 79 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import {
1515
} from './endpoints'
1616
import { EndpointOptions } from './Http'
1717
import type { CreateFetcherConfig, Fetcher, IClientConfig } from './interfaces/ClientConfig'
18-
18+
import { Currency } from './interfaces/Currency'
19+
import { Locale } from './interfaces/Locale'
20+
import { BearerToken, OrderToken } from './interfaces/Token'
1921
class Client {
2022
public account: Account
2123
public authentication: Authentication
@@ -33,7 +35,10 @@ class Client {
3335

3436
protected host: string
3537
protected fetcher: Fetcher
36-
private locale: string | undefined
38+
39+
private tokens: EndpointOptions['tokens'] = {}
40+
private locale: EndpointOptions['locale'] | undefined
41+
private currency: EndpointOptions['currency'] | undefined
3742

3843
constructor(customOptions: IClientConfig) {
3944
const spreeHostEnvironmentValue: string | null = (globalThis.process && globalThis.process.env.SPREE_HOST) || null
@@ -50,90 +55,115 @@ class Client {
5055
const fetcherOptions: CreateFetcherConfig = { host: options.host }
5156

5257
this.fetcher = options.createFetcher(fetcherOptions)
53-
5458
this.addEndpoints()
5559
}
5660

57-
public withLocale(locale: string): this {
58-
this.locale = locale
59-
this.addEndpoints()
60-
return this
61+
public withOrderToken(orderToken: OrderToken): this {
62+
return this.withEndpointBuilder(() => {
63+
this.tokens.orderToken = orderToken
64+
})
65+
}
66+
67+
public withBearerToken(bearerToken: BearerToken): this {
68+
return this.withEndpointBuilder(() => {
69+
this.tokens.bearerToken = bearerToken
70+
})
6171
}
6272

63-
protected endpointOptions(): EndpointOptions {
73+
public withLocale(locale: Locale): this {
74+
return this.withEndpointBuilder(() => {
75+
this.locale = locale
76+
})
77+
}
78+
79+
public withCurrency(currency: Currency): this {
80+
return this.withEndpointBuilder(() => {
81+
this.currency = currency
82+
})
83+
}
84+
85+
protected getEndpointOptions(): EndpointOptions {
6486
return {
6587
fetcher: this.fetcher,
66-
locale: this.locale
88+
tokens: this.tokens,
89+
locale: this.locale,
90+
currency: this.currency
6791
}
6892
}
6993

7094
protected addEndpoints(): void {
71-
this.account = this.makeAccount()
72-
this.authentication = this.makeAuthentication()
73-
this.cart = this.makeCart()
74-
this.checkout = this.makeCheckout()
75-
this.countries = this.makeCountries()
76-
this.digitalAssets = this.makeDigitalAssets()
77-
this.menus = this.makeMenus()
78-
this.order = this.makeOrder()
79-
this.pages = this.makePages()
80-
this.products = this.makeProducts()
81-
this.taxons = this.makeTaxons()
82-
this.vendors = this.makeVendors()
83-
this.wishlists = this.makeWishlists()
95+
const options = this.getEndpointOptions()
96+
this.account = this.makeAccount(options)
97+
this.authentication = this.makeAuthentication(options)
98+
this.cart = this.makeCart(options)
99+
this.checkout = this.makeCheckout(options)
100+
this.countries = this.makeCountries(options)
101+
this.digitalAssets = this.makeDigitalAssets(options)
102+
this.menus = this.makeMenus(options)
103+
this.order = this.makeOrder(options)
104+
this.pages = this.makePages(options)
105+
this.products = this.makeProducts(options)
106+
this.taxons = this.makeTaxons(options)
107+
this.vendors = this.makeVendors(options)
108+
this.wishlists = this.makeWishlists(options)
109+
}
110+
111+
protected withEndpointBuilder(fn: () => void): this {
112+
fn()
113+
this.addEndpoints()
114+
return this
84115
}
85116

86-
87-
protected makeAccount(): Account {
88-
return new Account({ ...this.endpointOptions() })
117+
protected makeAccount(options: EndpointOptions): Account {
118+
return new Account(options)
89119
}
90120

91-
protected makeAuthentication(): Authentication {
92-
return new Authentication({ ...this.endpointOptions() })
121+
protected makeAuthentication(options: EndpointOptions): Authentication {
122+
return new Authentication(options)
93123
}
94124

95-
protected makeCart(): Cart {
96-
return new Cart({ ...this.endpointOptions() })
125+
protected makeCart(options: EndpointOptions): Cart {
126+
return new Cart(options)
97127
}
98128

99-
protected makeCheckout(): Checkout {
100-
return new Checkout({ ...this.endpointOptions() })
129+
protected makeCheckout(options: EndpointOptions): Checkout {
130+
return new Checkout(options)
101131
}
102132

103-
protected makeCountries(): Countries {
104-
return new Countries({ ...this.endpointOptions() })
133+
protected makeCountries(options: EndpointOptions): Countries {
134+
return new Countries(options)
105135
}
106136

107-
protected makeOrder(): Order {
108-
return new Order({ ...this.endpointOptions() })
137+
protected makeOrder(options: EndpointOptions): Order {
138+
return new Order(options)
109139
}
110140

111-
protected makePages(): Pages {
112-
return new Pages({ ...this.endpointOptions() })
141+
protected makePages(options: EndpointOptions): Pages {
142+
return new Pages(options)
113143
}
114144

115-
protected makeProducts(): Products {
116-
return new Products({ ...this.endpointOptions() })
145+
protected makeProducts(options: EndpointOptions): Products {
146+
return new Products(options)
117147
}
118148

119-
protected makeTaxons(): Taxons {
120-
return new Taxons({ ...this.endpointOptions() })
149+
protected makeTaxons(options: EndpointOptions): Taxons {
150+
return new Taxons(options)
121151
}
122152

123-
protected makeDigitalAssets(): DigitalAssets {
124-
return new DigitalAssets({ ...this.endpointOptions() })
153+
protected makeDigitalAssets(options: EndpointOptions): DigitalAssets {
154+
return new DigitalAssets(options)
125155
}
126156

127-
protected makeMenus(): Menus {
128-
return new Menus({ ...this.endpointOptions() })
157+
protected makeMenus(options: EndpointOptions): Menus {
158+
return new Menus(options)
129159
}
130160

131-
protected makeVendors(): Vendors {
132-
return new Vendors({ ...this.endpointOptions() })
161+
protected makeVendors(options: EndpointOptions): Vendors {
162+
return new Vendors(options)
133163
}
134164

135-
protected makeWishlists(): Wishlists {
136-
return new Wishlists({ ...this.endpointOptions() })
165+
protected makeWishlists(options: EndpointOptions): Wishlists {
166+
return new Wishlists(options)
137167
}
138168
}
139169

src/Http.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,33 @@ import type { IToken } from './interfaces/Token'
1717

1818
export type EndpointOptions = {
1919
fetcher: Fetcher
20+
tokens?: IToken
2021
locale?: string
22+
currency?: string
2123
}
2224

2325
export default class Http {
2426
public fetcher: Fetcher
25-
public locale: string | undefined
27+
public tokens: EndpointOptions['tokens']
28+
public locale: EndpointOptions['locale'] | undefined
29+
public currency: EndpointOptions['currency'] | undefined
2630

27-
constructor({ fetcher, locale }: EndpointOptions) {
31+
constructor({ fetcher, tokens, locale, currency }: EndpointOptions) {
2832
this.fetcher = fetcher
33+
this.tokens = tokens || {}
2934
this.locale = locale
35+
this.currency = currency
3036
}
3137

3238
protected async spreeResponse<ResponseType = JsonApiResponse>(
3339
method: HttpMethod,
3440
url: string,
35-
tokens: IToken = {},
41+
userTokens: IToken = {},
3642
userParams: any = {},
37-
responseParsing: ResponseParsing = 'automatic',
43+
responseParsing: ResponseParsing = 'automatic'
3844
): Promise<ResultResponse<ResponseType>> {
3945
try {
40-
const headers = this.spreeOrderHeaders(tokens)
46+
const headers = this.spreeOrderHeaders(userTokens)
4147
const params = this.spreeParams(userParams)
4248

4349
const fetchOptions: FetchConfig = {
@@ -104,8 +110,12 @@ export default class Http {
104110
}
105111
}
106112

107-
protected spreeOrderHeaders(tokens: IToken): { [headerName: string]: string } {
113+
protected spreeOrderHeaders(userTokens: IToken): { [headerName: string]: string } {
108114
const header = {}
115+
const tokens = {
116+
...this.tokens,
117+
...userTokens
118+
}
109119

110120
if (tokens.orderToken) {
111121
header['X-Spree-Order-Token'] = tokens.orderToken
@@ -121,6 +131,7 @@ export default class Http {
121131
protected spreeParams(userParams: any): Record<string, any> {
122132
const params = {
123133
locale: this.locale,
134+
currency: this.currency,
124135
...userParams
125136
}
126137

src/interfaces/Currency.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type Currency = string

src/interfaces/Locale.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type Locale = string

src/interfaces/Token.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { ResultResponse } from './ResultResponse'
22

3+
export type BearerToken = string
4+
5+
export type OrderToken = string
6+
37
/**
48
* @deprecated Use
59
* {@link RequiredAnyToken},
@@ -10,21 +14,21 @@ import { ResultResponse } from './ResultResponse'
1014
* instead.
1115
*/
1216
export interface IToken {
13-
orderToken?: string
14-
bearerToken?: string
17+
orderToken?: OrderToken
18+
bearerToken?: BearerToken
1519
}
1620

1721
export type RequiredAnyToken =
18-
| { order_token: string; bearer_token?: never }
19-
| { order_token?: never; bearer_token: string }
22+
| { order_token: OrderToken; bearer_token?: never }
23+
| { order_token?: never; bearer_token: BearerToken }
2024

2125
export type OptionalAnyToken =
22-
| { order_token?: string; bearer_token?: never }
23-
| { order_token?: never; bearer_token?: string }
26+
| { order_token?: OrderToken; bearer_token?: never }
27+
| { order_token?: never; bearer_token?: BearerToken }
2428

25-
export type RequiredAccountToken = { bearer_token: string }
29+
export type RequiredAccountToken = { bearer_token: BearerToken }
2630

27-
export type OptionalAccountToken = { bearer_token?: string }
31+
export type OptionalAccountToken = { bearer_token?: BearerToken }
2832

2933
export interface IOAuthToken {
3034
access_token: string

0 commit comments

Comments
 (0)