Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement fluent interface #361

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 80 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/sdk-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,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-node-externals": "^3.0.0"
Expand Down
33 changes: 0 additions & 33 deletions packages/sdk-core/src/Client.ts

This file was deleted.

43 changes: 35 additions & 8 deletions packages/sdk-core/src/Http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,46 @@ import {
SpreeSDKError
} from './errors'
import FetchError from './errors/FetchError'
import { isFetchError } from './helpers/typeguards/isFetchError'
import * as result from './helpers/result'
import { ClientBuilderOptions } from './interfaces/ClientBuilderOptions'
import type { Fetcher } from './interfaces/ClientConfig'
import type { ErrorType } from './interfaces/ErrorType'
import type { FetchConfig, HttpMethod, ResponseParsing } from './interfaces/FetchConfig'
import type { JsonApiResponse } from './interfaces/JsonApi'
import type { ResultResponse } from './interfaces/ResultResponse'
import type { IToken } from './interfaces/Token'
import { SpreeOrderHeaders } from './interfaces/SpreeOrderHeaders'
import type { BearerToken, IToken } from './interfaces/Token'

export type EndpointOptions = {
export type EndpointOptions = ClientBuilderOptions & {
fetcher: Fetcher
}

export default class Http {
public fetcher: Fetcher
public bearerToken: BearerToken | undefined
public orderToken: BearerToken | undefined
public locale: EndpointOptions['locale'] | undefined
public currency: EndpointOptions['currency'] | undefined

constructor({ fetcher }: EndpointOptions) {
constructor({ fetcher, bearer_token, order_token, locale, currency }: EndpointOptions) {
this.fetcher = fetcher
this.bearerToken = bearer_token
this.orderToken = order_token
this.locale = locale
this.currency = currency
}

protected async spreeResponse<ResponseType = JsonApiResponse>(
method: HttpMethod,
url: string,
tokens: IToken = {},
params: any = {},
userTokens: IToken = {},
userParams: any = {},
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 = {
url,
Expand Down Expand Up @@ -101,8 +113,13 @@ export default class Http {
}
}

protected spreeOrderHeaders(tokens: IToken): { [headerName: string]: string } {
const header = {}
protected spreeOrderHeaders(userTokens: IToken): SpreeOrderHeaders {
const header: SpreeOrderHeaders = {}
const tokens = {
orderToken: this.orderToken,
bearerToken: this.bearerToken,
...userTokens
}

if (tokens.orderToken) {
header['X-Spree-Order-Token'] = tokens.orderToken
Expand All @@ -114,4 +131,14 @@ export default class Http {

return header
}

protected spreeParams(userParams: any): Record<string, any> {
const params = {
locale: this.locale,
currency: this.currency,
...userParams
}

return params
}
}
2 changes: 1 addition & 1 deletion packages/sdk-core/src/errors/FetchError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default class FetchError extends SpreeSDKError {
public data?: any

constructor(response?: RawFetchResponse, request?: unknown, data?: unknown, message?: string) {
super(message)
super(message || '')
Object.setPrototypeOf(this, FetchError.prototype)
this.name = 'FetchError'
this.response = response
Expand Down
5 changes: 3 additions & 2 deletions packages/sdk-core/src/helpers/jsonApi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { JsonApiDocument, JsonApiResponse } from '../interfaces/JsonApi'
import type { RelationType } from '../interfaces/Relationships'
import { DocumentRelationshipError } from '../errors'
import { isNotNull } from './typeguards/isNotNull'

const findDocument = <DocumentType extends JsonApiDocument>(
spreeSuccessResponse: JsonApiResponse,
Expand Down Expand Up @@ -41,8 +42,8 @@ const findRelationshipDocuments = <DocumentType extends JsonApiDocument>(
}

return documentReferences
.map<DocumentType>((relationType: RelationType) => findDocument<DocumentType>(spreeSuccessResponse, relationType))
.filter(Boolean)
.map<DocumentType | null>((relationType) => findDocument<DocumentType>(spreeSuccessResponse, relationType))
.filter(isNotNull)
}

const findSingleRelationshipDocument = <DocumentType extends JsonApiDocument>(
Expand Down
Loading