Skip to content

Commit f8d4892

Browse files
authored
Remove required runtime dependencies (#293)
* Remove lodash/get dependency * Remove qs dependency * Add axios and node-fetch to dev dependencies and peer dependencies Also loads axios dynamically based on runtime environment * Explicitly build Spree SDK for Cypress * Install optional peerDependencies when running e2e tests * Include axios and node-fetch as devDependencies * Use npm pack instead of npm link to reference SDK in tests * Replace DeleteBeforeEmit with DeleteBeforeRun * Prepare an intermiediate Docker image with packed Spree SDK for tests * Build local images before attempting to pull * Update path to browser SDK script in tests * Remove docker-compose-cypress images pull * Separate test-runtime from other test Docker services * Include browser axios before running tests * Skip running Spree worker for tests * Adjust docker-host configuration * Replace wait-for-it with a more robust implementation
1 parent 0e8b9ce commit f8d4892

29 files changed

+803
-420
lines changed

.dockerignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
**/node_modules
2+
dist
3+
types
4+
*.tgz

Dockerfile.test-runtime

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
FROM node:14.15.4
2+
3+
WORKDIR /sdk
4+
5+
COPY . /sdk
6+
7+
RUN npm install
8+
RUN npm run build
9+
RUN npm pack
10+
11+
FROM alpine
12+
13+
WORKDIR /sdk
14+
15+
COPY --from=0 /sdk/spree-storefront-api-v2-sdk-*.tgz /sdk/storefront-api-v2-sdk.tgz
16+
17+
COPY ./wait-for-it.sh /sdk/

README.md

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ Install the NPM package:
9898
npm install @spree/storefront-api-v2-sdk --save
9999
```
100100

101+
Install a HTTP client - axios or node-fetch. For example:
102+
103+
```
104+
npm install axios
105+
```
106+
107+
<aside><p>For details about HTTP clients, read the <a href="#switching-the-fetcher">Switching the fetcher</a> section.</p></aside>
108+
101109
Create a client:
102110

103111
```js
@@ -195,21 +203,60 @@ Identifies a user for a single operation. For example, to reset their account's
195203

196204
## Switching the fetcher
197205

198-
By default, the Spree SDK uses [Axios][8] to communicate with Spree. Another built-in option is [fetch][9]. It can be set as follows:
206+
Spree SDK does not come bundled with a HTTP client. A HTTP client may have to be installed before using the library. Out of the box, Spree SDK supports using [Axios][8] and [fetch][9] to communicate with Spree.
207+
208+
**Option A - RECOMMENDED: Spree SDK in NodeJS using Axios**
209+
210+
To use Spree SDK with Axios in NodeJS, install Axios using NPM:
211+
212+
```
213+
npm install axios
214+
```
215+
216+
Spree SDK will automatically detect that Axios is available and use it to make requests to Spree.
217+
218+
**Option B - Spree SDK in the browser using Axios**
219+
220+
To use Spree SDK with Axios in the browser, include axios as a `<script>` tag before using the SDK:
221+
222+
```html
223+
<script src="https://unpkg.com/[email protected]/dist/axios.min.js"></script>
224+
<script src="https://unpkg.com/@spree/[email protected]/dist/client/index.js"></script>
225+
```
226+
227+
Again, Spree SDK will automatically detect that Axios is available and use it to make requests to Spree.
228+
229+
**Option C - Spree SDK in NodeJS using fetch**
230+
231+
Another supported HTTP client is [fetch][9]. It can be setup in NodeJS as follows:
232+
233+
```
234+
npm install node-fetch
235+
```
236+
237+
To have Spree SDK use fetch instead of Axios, set `fetcherType` to `'fetch'` when creating the Spree SDK Client:
238+
239+
```js
240+
makeClient({ fetcherType: 'fetch', host: ... })
241+
```
242+
243+
**Option D - Spree SDK in the browser using fetch**
244+
245+
Modern web browsers include fetch natively. To use Spree SDK with native fetch, it's enough to set `fetcherType` to `'fetch'` when creating the Spree SDK Client:
199246

200247
```js
201-
createClient({ fetcherType: 'fetch', host: ... })
248+
makeClient({ fetcherType: 'fetch', host: ... })
202249
```
203250

204-
The `'fetch'` option will look for global `fetch` and `Request` values and fallback to `node-fetch` during runtime.
251+
**Option E - ADVANCED: Supply a custom HTTP client.**
205252

206-
A custom fetcher can be used like so:
253+
To have full control over requests and responses, a custom fetcher can be used like so:
207254

208255
```js
209-
createClient({ fetcherType: 'custom', createFetcher: ... })
256+
makeClient({ fetcherType: 'custom', createFetcher: ... })
210257
```
211258

212-
To create a custom fetcher which uses a fetch-compatible interface, use the `createCustomizedFetchFetcher` function.
259+
<aside><p>To create a custom fetcher which uses a fetch-compatible interface, use the `createCustomizedFetchFetcher` function.</p></aside>
213260

214261
## Endpoints
215262

docker-compose-cypress.yml

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,14 @@ version: '3.7'
22

33
services:
44
express:
5-
image: node:14.15.4
6-
volumes:
7-
- .:/app
8-
- /app/node_modules
9-
- /app/docker/express/node_modules
10-
entrypoint: ['/app/docker/express/express-docker-entrypoint.sh']
5+
build:
6+
context: .
7+
dockerfile: ./docker/express/Dockerfile.express
118
cypress:
12-
image: cypress/included:8.3.0
13-
volumes:
14-
- .:/sdk
15-
- /sdk/node_modules
16-
- /sdk/tests/node_modules
17-
entrypoint: ['/sdk/tests/cypress-docker-entrypoint.sh']
9+
build:
10+
context: .
11+
dockerfile: ./tests/Dockerfile.cypress
1812
docker-host:
1913
image: qoomon/docker-host:3.0.3
2014
cap_add: ['NET_ADMIN', 'NET_RAW']
21-
deploy:
22-
resources:
23-
limits:
24-
memory: 8M
2515
restart: on-failure

docker-compose-spree.yml

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,6 @@ services:
3434
SECRET_KEY_BASE: skb
3535
ALLOWED_ORIGIN_HOSTS: '*'
3636
RACK_CORS_DEBUG: 'true'
37-
worker:
38-
depends_on:
39-
- 'postgres'
40-
- 'redis'
41-
image: sparksolutions/spree-commerce:latest
42-
command: bundle exec sidekiq -C config/sidekiq.yml
43-
volumes:
44-
- 'spree:/app/'
45-
- 'bundle_cache:/bundle'
46-
environment:
47-
REDIS_URL: redis://redis:6379/0
48-
DB_HOST: postgres
49-
DB_PORT: 5432
50-
DISABLE_SPRING: 1
51-
BROWSER: /dev/null
52-
SECRET_KEY_BASE: skb
5337

5438
volumes:
5539
spree:

docker-compose-test-runtime.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
version: '3.7'
2+
3+
services:
4+
test-runtime:
5+
image: spree/test-runtime
6+
build:
7+
context: .
8+
dockerfile: ./Dockerfile.test-runtime

docker/express/Dockerfile.express

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM spree/test-runtime
2+
3+
FROM node:14.15.4
4+
5+
COPY --from=0 /sdk /sdk
6+
7+
WORKDIR /app
8+
9+
COPY ./docker/express /app
10+
11+
RUN npm install
12+
13+
ENTRYPOINT ./express-docker-entrypoint.sh

docker/express/express-docker-entrypoint.sh

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,6 @@
22

33
set -e
44

5-
cd /app
5+
/sdk/wait-for-it.sh http://docker-host:3000/api/v2/storefront/products?per_page=1 -t 300
66

7-
npm install
8-
npm run build
9-
npm link
10-
11-
cd /app/docker/express
12-
13-
npm install
14-
npm link @spree/storefront-api-v2-sdk
15-
16-
/app/wait-for-it.sh -s docker-host:3000
17-
18-
node ./index.js
7+
node /app/index.js

docker/express/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ const { makeClient, result } = sdk
88

99
app.use(bodyParser.json())
1010

11+
app.get('/ping', (_request, response) => {
12+
response.send('up')
13+
})
14+
1115
app.all('/', async (request, response, next) => {
1216
try {
1317
console.log('The Express server received a new request. Arguments: ', request.body)

docker/express/package-lock.json

Lines changed: 39 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)