@@ -15,7 +15,9 @@ import {
1515} from './endpoints'
1616import { EndpointOptions } from './Http'
1717import 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'
1921class 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
0 commit comments