Skip to content

Commit 3851c58

Browse files
committed
Update Storefront API version to 2020-01 and add new client language header option
Update README with new client set up for translated content Update unit tests Refactor tests
1 parent 76cd637 commit 3851c58

8 files changed

+77
-14
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,23 @@ import Client from 'shopify-buy/index.unoptimized.umd';
9696
## Examples
9797

9898
### Initializing the Client
99+
If your store supports multiple languages, Storefront API can return translated resource types and fields. Learn more about [translating content](https://help.shopify.com/en/api/guides/multi-language/translating-content-api).
100+
99101
```javascript
100102
import Client from 'shopify-buy';
101103

104+
// Initializing a client
102105
const client = Client.buildClient({
103106
domain: 'your-shop-name.myshopify.com',
104107
storefrontAccessToken: 'your-storefront-access-token'
105108
});
109+
110+
// Initializing a client to return translated content
111+
const clientWithTranslatedContent = Client.buildClient({
112+
domain: 'your-shop-name.myshopify.com',
113+
storefrontAccessToken: 'your-storefront-access-token',
114+
language: 'ja-JP'
115+
});
106116
```
107117

108118
### Fetching Products

docs/config.js.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ <h1 class="page-title">Source: config.js</h1>
8585
if (attrs.hasOwnProperty('apiVersion')) {
8686
this.apiVersion = attrs.apiVersion;
8787
} else {
88-
this.apiVersion = '2019-10';
88+
this.apiVersion = '2020-01';
8989
}
9090

9191
if (attrs.hasOwnProperty('source')) {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"lint": "eslint --max-warnings 0 -c .eslintrc.json $(yarn run lint:reporter-args 2>&1 >/dev/null) src/ test/",
2929
"lint:reporter-args": "test -n \"${CI}\" && >&2 echo -o $CIRCLE_TEST_REPORTS/junit/eslint.xml -f junit",
3030
"print-start-message": "wait-on file:.tmp/test/index.html file:.tmp/test/tests.js tcp:35729 tcp:4200 && echo \"\n\n⚡️⚡️⚡️ Good to go at http://localhost:4200 ⚡️⚡️⚡️\"",
31-
"schema:fetch": "graphql-js-schema-fetch --url 'https://graphql.myshopify.com/api/2019-10/graphql' --header 'Authorization: Basic MzUxYzEyMjAxN2QwZjJhOTU3ZDMyYWU3MjhhZDc0OWM=' | jq '.' > schema.json",
31+
"schema:fetch": "graphql-js-schema-fetch --url 'https://graphql.myshopify.com/api/2020-01/graphql' --header 'Authorization: Basic MzUxYzEyMjAxN2QwZjJhOTU3ZDMyYWU3MjhhZDc0OWM=' | jq '.' > schema.json",
3232
"minify-umd:optimized": "echo \"/*\n$(cat LICENSE.txt)\n*/\" > index.umd.min.js && babel-minify index.umd.js >> index.umd.min.js",
3333
"minify-umd:unoptimized": "echo \"/*\n$(cat LICENSE.txt)\n*/\" > index.unoptimized.umd.min.js && babel-minify index.unoptimized.umd.js >> index.unoptimized.umd.min.js"
3434
},

src/client.js

+4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ class Client {
5151
headers['X-SDK-Variant-Source'] = config.source;
5252
}
5353

54+
if (config.language) {
55+
headers['Accept-Language'] = config.language;
56+
}
57+
5458
if (fetchFunction) {
5559
headers['Content-Type'] = 'application/json';
5660
headers.Accept = 'application/json';

src/config.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,16 @@ class Config {
5757
if (attrs.hasOwnProperty('apiVersion')) {
5858
this.apiVersion = attrs.apiVersion;
5959
} else {
60-
this.apiVersion = '2019-10';
60+
this.apiVersion = '2020-01';
6161
}
6262

6363
if (attrs.hasOwnProperty('source')) {
6464
this.source = attrs.source;
6565
}
66+
67+
if (attrs.hasOwnProperty('language')) {
68+
this.language = attrs.language;
69+
}
6670
}
6771
}
6872

test/client-test.js

+39-10
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ suite('client-test', () => {
99
const config = {
1010
domain: 'sendmecats.myshopify.com',
1111
storefrontAccessToken: 'abc123',
12-
apiVersion: '2019-10'
12+
apiVersion: '2020-01'
1313
};
1414

15-
test('it instantiates a GraphQL client with the given config and without custom source header when no source config is provided', () => {
15+
test('it instantiates a GraphQL client with the given config', () => {
1616
let passedTypeBundle;
1717
let passedUrl;
1818
let passedFetcherOptions;
@@ -28,7 +28,7 @@ suite('client-test', () => {
2828
new Client(new Config(config), FakeGraphQLJSClient); // eslint-disable-line no-new
2929

3030
assert.deepEqual(passedTypeBundle, types);
31-
assert.equal(passedUrl, `https://sendmecats.myshopify.com/api/${config.apiVersion}/graphql`);
31+
assert.equal(passedUrl, `https://${config.domain}/api/${config.apiVersion}/graphql`);
3232
assert.deepEqual(passedFetcherOptions, {
3333
headers: {
3434
'X-SDK-Variant': 'javascript',
@@ -51,26 +51,55 @@ suite('client-test', () => {
5151
}
5252
}
5353

54-
const withSourceConfig = {
55-
domain: 'sendmecats.myshopify.com',
56-
storefrontAccessToken: 'abc123',
54+
const withSourceConfig = Object.assign({}, config, {
5755
source: 'buy-button-js'
58-
};
56+
});
5957

6058
new Client(new Config(withSourceConfig), FakeGraphQLJSClient); // eslint-disable-line no-new
6159

6260
assert.deepEqual(passedTypeBundle, types);
63-
assert.equal(passedUrl, `https://sendmecats.myshopify.com/api/${config.apiVersion}/graphql`);
61+
assert.equal(passedUrl, `https://${withSourceConfig.domain}/api/${withSourceConfig.apiVersion}/graphql`);
6462
assert.deepEqual(passedFetcherOptions, {
6563
headers: {
6664
'X-SDK-Variant': 'javascript',
6765
'X-SDK-Version': version,
68-
'X-Shopify-Storefront-Access-Token': config.storefrontAccessToken,
66+
'X-Shopify-Storefront-Access-Token': withSourceConfig.storefrontAccessToken,
6967
'X-SDK-Variant-Source': withSourceConfig.source
7068
}
7169
});
7270
});
7371

72+
test('it instantiates a GraphQL client with the given config and language header when a language config is provided', () => {
73+
let passedTypeBundle;
74+
let passedUrl;
75+
let passedFetcherOptions;
76+
77+
class FakeGraphQLJSClient {
78+
constructor(typeBundle, {url, fetcherOptions}) {
79+
passedTypeBundle = typeBundle;
80+
passedUrl = url;
81+
passedFetcherOptions = fetcherOptions;
82+
}
83+
}
84+
85+
const withLanguageConfig = Object.assign({}, config, {
86+
language: 'ja-JP'
87+
});
88+
89+
new Client(new Config(withLanguageConfig), FakeGraphQLJSClient); // eslint-disable-line no-new
90+
91+
assert.deepEqual(passedTypeBundle, types);
92+
assert.equal(passedUrl, `https://${withLanguageConfig.domain}/api/${withLanguageConfig.apiVersion}/graphql`);
93+
assert.deepEqual(passedFetcherOptions, {
94+
headers: {
95+
'X-SDK-Variant': 'javascript',
96+
'X-SDK-Version': version,
97+
'X-Shopify-Storefront-Access-Token': withLanguageConfig.storefrontAccessToken,
98+
'Accept-Language': withLanguageConfig.language
99+
}
100+
});
101+
});
102+
74103
test('it creates a fetcher from the fetch function provided', () => {
75104
let passedFetcher;
76105
let passedUrl;
@@ -98,7 +127,7 @@ suite('client-test', () => {
98127
new Client(new Config(config), FakeGraphQLJSClient, fetchFunction); // eslint-disable-line no-new
99128

100129
return passedFetcher({data: 'body'}).then(() => {
101-
assert.equal(passedUrl, `https://sendmecats.myshopify.com/api/${config.apiVersion}/graphql`);
130+
assert.equal(passedUrl, `https://${config.domain}/api/${config.apiVersion}/graphql`);
102131
assert.equal(passedBody, JSON.stringify({data: 'body'}));
103132
assert.equal(passedMethod, 'POST');
104133
assert.equal(passedMode, 'cors');

test/config-test.js

+16
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,20 @@ suite('config-test', () => {
7070
assert.equal(config.storefrontAccessToken, storefrontAccessToken, 'storefrontAccessToken should match');
7171
assert.equal(config.source, source, 'source should match');
7272
});
73+
74+
test('it sets language property when a language is provided', () => {
75+
const domain = 'website.com';
76+
const storefrontAccessToken = 123;
77+
const language = 'ja-JP';
78+
79+
const config = new Config({
80+
domain,
81+
storefrontAccessToken,
82+
language
83+
});
84+
85+
assert.equal(config.domain, domain, 'domain should match');
86+
assert.equal(config.storefrontAccessToken, storefrontAccessToken, 'storefrontAccessToken should match');
87+
assert.equal(config.language, language, 'language should match');
88+
});
7389
});

test/product-helpers-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import singleProductFixture from '../fixtures/product-fixture';
66
import types from '../schema.json';
77

88
suite('product-helpers-test', () => {
9-
const graphQLClient = new GraphQLJSClient(types, {url: 'https://sendmecats.myshopify.com/api/2019-10/graphql'});
9+
const graphQLClient = new GraphQLJSClient(types, {url: 'https://sendmecats.myshopify.com/api/2020-01/graphql'});
1010
const query = productNodeQuery(graphQLClient)
1111
.definitions
1212
.find((definition) => definition.operationType === 'query');

0 commit comments

Comments
 (0)