Skip to content

Commit

Permalink
Setting up environment of SDK to work with creted type system
Browse files Browse the repository at this point in the history
  • Loading branch information
letelete committed Aug 30, 2022
1 parent e84961f commit e89687b
Show file tree
Hide file tree
Showing 12 changed files with 2,417 additions and 99 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"progress-bar-webpack-plugin": "^2.1.0",
"source-map-loader": "^2.0.2",
"ts-loader": "^8.3.0",
"typescript": "^4.5.2",
"typescript": "4.7.4",
"webpack": "^5.65.0",
"webpack-cli": "^4.9.1",
"webpack-merge": "^5.8.0",
Expand Down
94 changes: 19 additions & 75 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
Vendors,
Wishlists
} from './endpoints'
import { EndpointOptions } from './Http'
import { AllowedClientBuilderOptions, DefaultBuilderOptions } from './interfaces/ClientBuilderOptions'
import type { CreateFetcherConfig, Fetcher, IClientConfig } from './interfaces/ClientConfig'
import { Currency } from './interfaces/Currency'
Expand All @@ -38,8 +37,7 @@ class Client<ClientOptions extends AllowedClientBuilderOptions = DefaultBuilderO

protected host: string
protected fetcher: Fetcher

private config: IClientConfig
protected config: IClientConfig

constructor(customOptions: IClientConfig) {
const spreeHostEnvironmentValue: string | null = (globalThis.process && globalThis.process.env.SPREE_HOST) || null
Expand All @@ -49,6 +47,7 @@ class Client<ClientOptions extends AllowedClientBuilderOptions = DefaultBuilderO
}

this.config = { ...defaultOptions, ...customOptions }
this.host = this.config.host

const fetcherOptions: CreateFetcherConfig = { host: this.config.host }

Expand All @@ -61,7 +60,20 @@ class Client<ClientOptions extends AllowedClientBuilderOptions = DefaultBuilderO
locale: this.config.locale,
currency: this.config.currency
}
this.addEndpoints(endpointOptions)

this.account = new Account(endpointOptions)
this.authentication = new Authentication(endpointOptions)
this.cart = new Cart(endpointOptions)
this.checkout = new Checkout(endpointOptions)
this.countries = new Countries(endpointOptions)
this.digitalAssets = new DigitalAssets(endpointOptions)
this.menus = new Menus(endpointOptions)
this.order = new Order(endpointOptions)
this.pages = new Pages(endpointOptions)
this.products = new Products(endpointOptions)
this.taxons = new Taxons(endpointOptions)
this.vendors = new Vendors(endpointOptions)
this.wishlists = new Wishlists(endpointOptions)
}

public withOrderToken(order_token: OrderToken) {
Expand All @@ -80,78 +92,10 @@ class Client<ClientOptions extends AllowedClientBuilderOptions = DefaultBuilderO
return this.builderInstance<SetProperty<ClientOptions, 'currency', true>>({ currency })
}

protected builderInstance<NewClientInstanceType extends AllowedClientBuilderOptions>(
protected builderInstance<T extends AllowedClientBuilderOptions = ClientOptions>(
config: Partial<IClientConfig> = {}
) {
return new Client<NewClientInstanceType>({ ...this.config, ...config })
}

protected addEndpoints(options: EndpointOptions): void {
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 makeAccount(options: EndpointOptions): Account<ClientOptions> {
return new Account<ClientOptions>(options)
}

protected makeAuthentication(options: EndpointOptions): Authentication {
return new Authentication(options)
}

protected makeCart(options: EndpointOptions): Cart {
return new Cart(options)
}

protected makeCheckout(options: EndpointOptions): Checkout {
return new Checkout(options)
}

protected makeCountries(options: EndpointOptions): Countries {
return new Countries(options)
}

protected makeOrder(options: EndpointOptions): Order {
return new Order(options)
}

protected makePages(options: EndpointOptions): Pages {
return new Pages(options)
}

protected makeProducts(options: EndpointOptions): Products {
return new Products(options)
}

protected makeTaxons(options: EndpointOptions): Taxons {
return new Taxons(options)
}

protected makeDigitalAssets(options: EndpointOptions): DigitalAssets {
return new DigitalAssets(options)
}

protected makeMenus(options: EndpointOptions): Menus {
return new Menus(options)
}

protected makeVendors(options: EndpointOptions): Vendors {
return new Vendors(options)
}

protected makeWishlists(options: EndpointOptions): Wishlists {
return new Wishlists(options)
): Client<T> {
return new Client<T>({ ...this.config, ...config })
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/endpoints/Account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import type {
ShowAddressOptions,
CreateAddressOptions,
RemoveAddressOptions,
UpdateAddressOptions
UpdateAddressOptions,
AccountContractTestOptions
} from '../interfaces/Account'
import { AllowedClientBuilderOptions } from '../interfaces/ClientBuilderOptions'
import type { ICreditCard, ICreditCardResult, ICreditCards, ICreditCardsResult } from '../interfaces/CreditCard'
Expand All @@ -38,6 +39,10 @@ import type { IToken } from '../interfaces/Token'
import routes from '../routes'

export default class Account<ClientOptions extends AllowedClientBuilderOptions> extends Http {
public async contractTest(options: AccountContractTestOptions<ClientOptions>): Promise<string> {
return options.locale
}

public async accountInfo(options: AccountInfoOptions<ClientOptions>): Promise<IAccountResult>
/**
* @deprecated Use the combined options signature instead.
Expand Down
14 changes: 10 additions & 4 deletions src/interfaces/Account.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { IAddress } from './attributes/Address'
import { AllowedClientBuilderOptions, ClientBuilderOptions } from './ClientBuilderOptions'
import type { JsonApiDocument, JsonApiListResponse, JsonApiSingleResponse } from './JsonApi'
import { PickAsOptional } from './PickAsOptional'
import { MakeOptional } from './MakeOptional'
import { MakeRequired } from './MakeRequired'
import type { IQuery } from './Query'
import type { IRelationships } from './Relationships'
import type { ResultResponse } from './ResultResponse'
import { WithBuilderOptions } from './WithClientBuilderOptions'
import { WithClientBuilderOptions } from './WithClientBuilderOptions'
import type { WithCommonOptions } from './WithCommonOptions'

export interface AccountAttr extends JsonApiDocument {
Expand Down Expand Up @@ -78,9 +79,14 @@ export interface AccountAddressResult extends ResultResponse<AccountAddressRespo

export interface AccountAddressesResult extends ResultResponse<AccountAddressesResponse> {}

export type AccountInfoOptions<ClientOptions extends AllowedClientBuilderOptions> = WithBuilderOptions<
export type AccountContractTestOptions<ClientOptions extends AllowedClientBuilderOptions> = WithClientBuilderOptions<
ClientOptions,
ClientBuilderOptions & WithCommonOptions<{ suggestToken: true; suggestQuery: true }>
MakeRequired<Partial<ClientBuilderOptions>, 'locale'>
>

export type AccountInfoOptions<ClientOptions extends AllowedClientBuilderOptions> = WithClientBuilderOptions<
ClientOptions,
MakeRequired<ClientBuilderOptions, 'bearer_token'>
>

export type CreditCardsListOptions = WithCommonOptions<{
Expand Down
14 changes: 5 additions & 9 deletions src/interfaces/ClientBuilderOptions.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import { Currency } from './Currency'
import { Locale } from './Locale'
import { BearerToken, OrderToken } from './Token'

export type ClientBuilderOptions = Partial<{
order_token: OrderToken
bearer_token: BearerToken
locale: Locale
currency: Currency
order_token: string
bearer_token: string
locale: string
currency: string
}>

export type AllowedClientBuilderOptions = {
[K in keyof Required<ClientBuilderOptions>]: boolean
[K in keyof ClientBuilderOptions]-?: boolean
}

export type DefaultBuilderOptions = AllowedClientBuilderOptions & { [K in keyof AllowedClientBuilderOptions]: false }
4 changes: 4 additions & 0 deletions src/interfaces/MakeOptional.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Transforms given Union of T keys into partial
*/
export type MakeOptional<T, Union extends keyof T> = Omit<T, Union> & Partial<Pick<T, Union>>
4 changes: 4 additions & 0 deletions src/interfaces/MakeRequired.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Transforms given Union of T keys into required
*/
export type MakeRequired<T, Union extends keyof T> = Omit<T, Union> & Required<Pick<T, Union>>
4 changes: 0 additions & 4 deletions src/interfaces/PickAsOptional.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/interfaces/SetProperty.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
* Sets a property with key {@link Key} of type {@link Type} to value {@link Value}
* Sets a property with key {@link Key} to value {@link Value} on type {@link Type}
*/
export type SetProperty<Type, Key extends keyof Type, Value> = Omit<Type, Key> & Record<Key, Value>
5 changes: 2 additions & 3 deletions src/interfaces/WithClientBuilderOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { FilterRequiredKeys } from './FilterRequiredKeys'
* // "locale" MyUserOptions's locale is required, but Options' locale transforms into optional, because MyClientOptions's locale is true
* // "currency" stays required, because MyClientOptions's locale is false, and MyUserOptions's currency is required.
*/
export type WithBuilderOptions<
export type WithClientBuilderOptions<
ClientOptions extends AllowedClientBuilderOptions,
UserOptions extends ClientBuilderOptions,
UserCustomOptionsOnly extends Record<string, unknown> = Omit<UserOptions, keyof ClientBuilderOptions>,
Expand All @@ -27,8 +27,7 @@ export type WithBuilderOptions<
RequiredEndpointOptionsOnly extends ClientBuilderOptions = FilterRequiredKeys<UserEndpointOptionsOnly>
> = UserCustomOptionsOnly &
OptionalEndpointOptionsOnly &
Partial<RequiredEndpointOptionsOnly> &
{
Partial<RequiredEndpointOptionsOnly> & {
[K in keyof Required<ClientBuilderOptions> as RequiredEndpointOptionsOnly extends Pick<
Required<ClientBuilderOptions>,
K
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"esModuleInterop": true,
"declaration": true,
"declarationDir": "./types",
"importsNotUsedAsValues": "preserve"
"importsNotUsedAsValues": "preserve",
"strict": true,
}
}
Loading

0 comments on commit e89687b

Please sign in to comment.