Skip to content
This repository has been archived by the owner on Nov 22, 2022. It is now read-only.

Updating sdk methods #3

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
e869586
Fix for tests and build
aplegatt Jul 12, 2022
15d993e
Authentication methods update
aplegatt Jul 15, 2022
46db6cc
Account methods update
aplegatt Jul 15, 2022
ee8e762
Cart methods update
aplegatt Jul 15, 2022
2b70a6e
Add methods for brands
aplegatt Jul 25, 2022
3ef26df
Add methods for categories
aplegatt Jul 26, 2022
9773744
Creates new interfaces
aplegatt Jul 26, 2022
ea13e50
Order method update
aplegatt Jul 26, 2022
4d5b87a
Vendors methods update
aplegatt Jul 26, 2022
712358a
Countries methods update
aplegatt Jul 26, 2022
b17382f
Pages methods update
aplegatt Jul 26, 2022
7334b89
Menus methods update
aplegatt Jul 26, 2022
146aeb3
Digital assets method update
aplegatt Jul 26, 2022
5bbb0a5
Wishlists methods update
aplegatt Jul 27, 2022
34df0bd
Products methods update
aplegatt Jul 27, 2022
2062a11
Remove unused imports
aplegatt Jul 27, 2022
a3419bc
Remove taxons endpoint and methods
aplegatt Jul 28, 2022
8223526
Fixing lint issues
aplegatt Jul 28, 2022
80492e9
New params helper
aplegatt Jul 29, 2022
bce8b00
Fixing tests
aplegatt Aug 1, 2022
a66800f
Removing old helper
aplegatt Aug 1, 2022
98021a0
Cleaning depracated types
aplegatt Aug 1, 2022
36afcfa
Add Vendo tests for brands and categories
aplegatt Aug 2, 2022
186b118
Token refactor
aplegatt Aug 2, 2022
1a9a4c2
Removing more deprecates
aplegatt Aug 8, 2022
834c720
Fix relationships checking
aplegatt Aug 11, 2022
3283ada
Errors refactoring
aplegatt Aug 16, 2022
e29b8a1
Add brands and categories methods to readme
aplegatt Aug 17, 2022
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
1 change: 0 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ jobs:
name: Test
command: |
. .circleci/load-nvm.sh
echo "$DOCKERHUB_PASSWORD" | docker login -u "$DOCKERHUB_LOGIN" --password-stdin >/dev/null 2>&1
npm run test

workflows:
Expand Down
60 changes: 42 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,12 @@ Developed and maintained by:
- [Products](#products)
- [list](#list)
- [show](#show-1)
- [Taxons](#taxons)
- [Brands](#brands)
- [list](#list-1)
- [show](#show-2)
- [Categories](#categories)
- [list](#list-7)
- [show](#show-8)
- [Vendors](#vendors)
- [list](#list-2)
- [show](#show-3)
Expand Down Expand Up @@ -217,11 +220,7 @@ Available `SpreeSDKError` subtypes:
| ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `MisconfigurationError` | Signifies the SDK's `Client` was created with improper options. Make sure the values of `host` and other options (if any) provided to `Client` have the correct format. |
| `NoResponseError` | Spree store could not be reached. Ensure it's running and available under the `host` address provided to the `Client` instance. |
| `SpreeError` | Spree responded with an error. To debug the issue, check the error's `serverResponse` field. It contains details about the response from Spree, such as the HTTP status code and headers. |
| `BasicSpreeError` | Extends `SpreeError` with a `summary` field provided by Spree and containing a summary of the issue. |
| `ExpandedSpreeError` | Extends `BasicSpreeError` with a `errors` field. `errors` contains a detailed explanation of the issue, ex. all the validation errors when trying to add shipping details to a Spree order. The `getErrors` method can be used to retrieve a concrete value inside `errors`, ex. `expSpreeError.getErrors(['bill_address', 'firstname'])`. |

The specific type of error returned by `fail()` can be determined using [`instanceof`][3], ex. `if(response.fail() instanceof BasicSpreeError){...}`.
| `SpreeError` | Spree responded with an error. To debug the issue, check the error's `serverResponse` field. It contains details about the response from Spree, such as the HTTP status code and error message. |

## Tokens

Expand Down Expand Up @@ -1630,11 +1629,11 @@ const response = await client.products.show({
})
```

## Taxons
## Brands

### [`list`][53]
### `list`

Returns a list of Taxons.
Returns a list of brands

**Success response schema:** [Success schema](#success-schema)

Expand All @@ -1643,29 +1642,55 @@ Returns a list of Taxons.
**Example:**

```ts
const response = await client.taxons.list()
const brands = await client.brands.list()
```

### [`show`][54]
### `show`

Returns a single Taxon.
Returns a single brand

**Parameters schema:**
**Success response schema:** [Success schema](#success-schema)

**Failure response schema:** [Error schema](#error-schema)

**Example:**

```ts
{
id: string
}
const brand = await client.brands.show({
brand_permalink: '980a80d5-f51b-4065-b3a0-622fc5cb6bf6'
})
```

## Categories

### `list`

Returns a list of categories

**Success response schema:** [Success schema](#success-schema)

**Failure response schema:** [Error schema](#error-schema)

**Example:**

```ts
const categories = await client.categories.list()
```

### `show`

Returns a single category

**Success response schema:** [Success schema](#success-schema)

**Failure response schema:** [Error schema](#error-schema)

**Example:**

```ts
const products = await client.taxons.show({ id: '1' })
const category = await client.categories.show({
category_permalink: '980a80d5-f51b-4065-b3a0-622fc5cb6bf6'
})
```

## Vendors
Expand Down Expand Up @@ -2396,7 +2421,6 @@ We are [available for hire][spark].
[50]: https://api.spreecommerce.org/docs/api-v2/b3A6MjYyODA2NTY-create-new-payment
[51]: https://api.spreecommerce.org/docs/api-v2/b3A6MzE0Mjc2Mg-list-all-products
[52]: https://api.spreecommerce.org/docs/api-v2/b3A6MTgwNTI4ODE-retrieve-a-product
[53]: https://api.spreecommerce.org/docs/api-v2/b3A6MzE0Mjc2NA-list-all-taxons
[54]: https://api.spreecommerce.org/docs/api-v2/b3A6MTgwNTI4ODM-retrieve-a-taxon
[55]: https://api.spreecommerce.org/docs/api-v2/b3A6MjE0NTY5Mzg-list-all-wishlists
[56]: https://api.spreecommerce.org/docs/api-v2/b3A6MjE0NTY5NDA-retrieve-a-wishlist
Expand Down
9 changes: 9 additions & 0 deletions docker-compose-cypress.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@ services:
build:
context: .
dockerfile: ./docker/express/Dockerfile.express
networks:
- vendo-dev
cypress:
build:
context: .
dockerfile: ./tests/Dockerfile.cypress
networks:
- vendo-dev
docker-host:
image: qoomon/docker-host:3.0.3
cap_add: ['NET_ADMIN', 'NET_RAW']
restart: on-failure
networks:
- vendo-dev

networks:
vendo-dev:
9 changes: 9 additions & 0 deletions docker-compose-spree.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ services:
- '5432:5432'
volumes:
- 'postgres:/var/lib/postgresql/data'
networks:
- vendo-dev
redis:
image: redis:6.2-alpine
volumes:
- 'redis:/data'
networks:
- vendo-dev
spree: # Runs production Spree
depends_on:
- 'postgres'
Expand All @@ -34,9 +38,14 @@ services:
SECRET_KEY_BASE: skb
ALLOWED_ORIGIN_HOSTS: '*'
RACK_CORS_DEBUG: 'true'
networks:
- vendo-dev

volumes:
spree:
bundle_cache:
redis:
postgres:

networks:
vendo-dev:
5 changes: 5 additions & 0 deletions docker-compose-test-runtime.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ services:
build:
context: .
dockerfile: ./Dockerfile.test-runtime
networks:
- vendo-dev

networks:
vendo-dev:
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/spark-solutions/spree-storefront-api-v2-js-sdk.git"
"url": "git+https://github.com/vendo-dev/js-sdk.git"
},
"author": "Spark Solutions <[email protected]> (https://sparksolutions.co)",
"license": "MIT",
"bugs": {
"url": "https://github.com/spark-solutions/spree-storefront-api-v2-js-sdk/issues"
"url": "https://github.com/vendo-dev/js-sdk/issues"
},
"homepage": "https://guides.spreecommerce.org/api/v2",
"devDependencies": {
Expand All @@ -55,7 +55,7 @@
"ts-loader": "^8.3.0",
"typescript": "^4.5.2",
"webpack": "^5.65.0",
"webpack-cli": "^4.9.1",
"webpack-cli": "^4.10.0",
"webpack-merge": "^5.8.0",
"webpack-node-externals": "^3.0.0"
},
Expand Down
21 changes: 14 additions & 7 deletions src/Client.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import {
Account,
Authentication,
Brands,
Cart,
Categories,
Checkout,
Countries,
DigitalAssets,
Menus,
Order,
Pages,
Products,
Taxons,
Vendors,
Wishlists
} from './endpoints'
Expand All @@ -18,15 +19,16 @@ import type { CreateFetcherConfig, Fetcher, IClientConfig } from './interfaces/C
class Client {
public account: Account
public authentication: Authentication
public brands: Brands
public cart: Cart
public categories: Categories
public checkout: Checkout
public countries: Countries
public digitalAssets: DigitalAssets
public menus: Menus
public order: Order
public pages: Pages
public products: Products
public taxons: Taxons
public vendors: Vendors
public wishlists: Wishlists

Expand Down Expand Up @@ -55,15 +57,16 @@ class Client {
protected addEndpoints(): void {
this.account = this.makeAccount()
this.authentication = this.makeAuthentication()
this.brands = this.makeBrands()
this.cart = this.makeCart()
this.categories = this.makeCategories()
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()
}
Expand All @@ -76,10 +79,18 @@ class Client {
return new Authentication({ fetcher: this.fetcher })
}

protected makeBrands(): Brands {
return new Brands({ fetcher: this.fetcher })
}

protected makeCart(): Cart {
return new Cart({ fetcher: this.fetcher })
}

protected makeCategories(): Categories {
return new Categories({ fetcher: this.fetcher })
}

protected makeCheckout(): Checkout {
return new Checkout({ fetcher: this.fetcher })
}
Expand All @@ -100,10 +111,6 @@ class Client {
return new Products({ fetcher: this.fetcher })
}

protected makeTaxons(): Taxons {
return new Taxons({ fetcher: this.fetcher })
}

protected makeDigitalAssets(): DigitalAssets {
return new DigitalAssets({ fetcher: this.fetcher })
}
Expand Down
50 changes: 13 additions & 37 deletions src/Http.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import {
BasicSpreeError,
ExpandedSpreeError,
MisconfigurationError,
NoResponseError,
SpreeError,
SpreeSDKError
} from './errors'
import FetchError from './errors/FetchError'
Expand All @@ -13,7 +10,8 @@ import type { ErrorType } from './interfaces/errors/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 type { OptionalAnyToken } from './interfaces/Token'
import type { SpreeError } from './errors'

export type EndpointOptions = {
fetcher: Fetcher
Expand All @@ -29,7 +27,7 @@ export default class Http {
protected async spreeResponse<ResponseType = JsonApiResponse>(
method: HttpMethod,
url: string,
tokens: IToken = {},
tokens: OptionalAnyToken = {},
params: any = {},
responseParsing: ResponseParsing = 'automatic'
): Promise<ResultResponse<ResponseType>> {
Expand All @@ -52,30 +50,15 @@ export default class Http {
}
}

/**
* The HTTP error code returned by Spree is not indicative of its response shape.
* This function determines the information provided by Spree and uses everything available.
*/
protected classifySpreeError(error: FetchError): ErrorType {
const { error: errorSummary, errors } = error.data

if (typeof errorSummary === 'string') {
if (typeof errors === 'object') {
return 'full'
}
return 'basic'
}
return 'limited'
}

protected processError(error: Error): SpreeSDKError {
if (error instanceof FetchError) {
if (error.response) {
protected processError(error: Error): any {
if ((error as FetchError)?.response) {
const fetchError = (error as FetchError)
if (fetchError.response) {
// Error from Spree outside HTTP 2xx codes
return this.processSpreeError(error)
return this.processSpreeError(fetchError)
}

if (error.request) {
if (fetchError.request) {
// No response received from Spree
return new NoResponseError()
}
Expand All @@ -88,19 +71,12 @@ export default class Http {
}

protected processSpreeError(error: FetchError): SpreeError {
const { error: errorSummary, errors } = error.data
const errorType = this.classifySpreeError(error)

if (errorType === 'full') {
return new ExpandedSpreeError(error.response, errorSummary, errors)
} else if (errorType === 'basic') {
return new BasicSpreeError(error.response, errorSummary)
} else {
return new SpreeError(error.response)
}
const { response: { status: code }, data: { error: message } } = error

return { code, message }
}

protected spreeOrderHeaders(tokens: IToken): { [headerName: string]: string } {
protected spreeOrderHeaders(tokens: OptionalAnyToken): { [headerName: string]: string } {
const header = {}

if (tokens.orderToken) {
Expand Down
Loading