Skip to content
This repository was archived by the owner on Oct 20, 2023. It is now read-only.

Commit 804b8b0

Browse files
antoinegrantrickitan
authored andcommitted
@shopify/theme-predictive-search (#86)
* theme-predictive-search * Improve error responses handling * Update README * Tidy up error objects. Updaate README * rename out_of_stock to unavailable_products * rename search_as_you_type to resources
1 parent c6a6cd7 commit 804b8b0

28 files changed

+3059
-44
lines changed

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ packages/theme-currency
77
packages/theme-images
88
packages/theme-rte
99
packages/theme-variants
10+
jest-setup.js

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@ Thumbs.db
4242
node_modules
4343
dist
4444
.vscode
45+
.nvmrc

README.md

+12-11
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@ Theme scripts are uncoupled from a particular UI. Typically, they are used along
99

1010
## Packages
1111

12-
| package | | |
13-
| ------------------ | ----------------------------------------------- | ---------------------------------------------------------------------------- |
14-
| theme-a11y | [README](packages/theme-a11y/README.md) | ![npm version](https://badge.fury.io/js/%40shopify%2Ftheme-a11y.svg) |
15-
| theme-addresses | [README](packages/theme-addresses/README.md) | ![npm version](https://badge.fury.io/js/%40shopify%2Ftheme-addresses.svg) |
16-
| theme-cart | [README](packages/theme-cart/README.md) | ![npm version](https://badge.fury.io/js/%40shopify%2Ftheme-cart.svg) |
17-
| theme-currency | [README](packages/theme-currency/README.md) | ![npm version](https://badge.fury.io/js/%40shopify%2Ftheme-currency.svg) |
18-
| theme-images | [README](packages/theme-images/README.md) | ![npm version](https://badge.fury.io/js/%40shopify%2Ftheme-images.svg) |
19-
| theme-product | [README](packages/theme-product/README.md) | ![npm version](https://badge.fury.io/js/%40shopify%2Ftheme-product.svg) |
20-
| theme-product-form | [README](packages/theme-product-form/README.md) | ![npm version](https://badge.fury.io/js/%40shopify%2Ftheme-product-form.svg) |
21-
| theme-rte | [README](packages/theme-rte/README.md) | ![npm version](https://badge.fury.io/js/%40shopify%2Ftheme-rte.svg) |
22-
| theme-sections | [README](packages/theme-sections/README.md) | ![npm version](https://badge.fury.io/js/%40shopify%2Ftheme-sections.svg) |
12+
| package | | |
13+
| ----------------------- | ---------------------------------------------------- | --------------------------------------------------------------------------------- |
14+
| theme-a11y | [README](packages/theme-a11y/README.md) | ![npm version](https://badge.fury.io/js/%40shopify%2Ftheme-a11y.svg) |
15+
| theme-addresses | [README](packages/theme-addresses/README.md) | ![npm version](https://badge.fury.io/js/%40shopify%2Ftheme-addresses.svg) |
16+
| theme-cart | [README](packages/theme-cart/README.md) | ![npm version](https://badge.fury.io/js/%40shopify%2Ftheme-cart.svg) |
17+
| theme-currency | [README](packages/theme-currency/README.md) | ![npm version](https://badge.fury.io/js/%40shopify%2Ftheme-currency.svg) |
18+
| theme-images | [README](packages/theme-images/README.md) | ![npm version](https://badge.fury.io/js/%40shopify%2Ftheme-images.svg) |
19+
| theme-predictive-search | [README](packages/theme-predictive-search/README.md) | ![npm version](https://badge.fury.io/js/%40shopify%2Ftheme-predictive-search.svg) |
20+
| theme-product | [README](packages/theme-product/README.md) | ![npm version](https://badge.fury.io/js/%40shopify%2Ftheme-product.svg) |
21+
| theme-product-form | [README](packages/theme-product-form/README.md) | ![npm version](https://badge.fury.io/js/%40shopify%2Ftheme-product-form.svg) |
22+
| theme-rte | [README](packages/theme-rte/README.md) | ![npm version](https://badge.fury.io/js/%40shopify%2Ftheme-rte.svg) |
23+
| theme-sections | [README](packages/theme-sections/README.md) | ![npm version](https://badge.fury.io/js/%40shopify%2Ftheme-sections.svg) |
2324

2425
## Contributing
2526

jest-setup.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
global.Promise = require("promise");

jest.config.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
module.exports = {
2-
preset: 'jest-puppeteer',
3-
setupTestFrameworkScriptFile: 'expect-puppeteer',
4-
testPathIgnorePatterns: ['.eslintrc.js'],
2+
preset: "jest-puppeteer",
3+
setupFiles: ["./jest-setup.js"],
4+
setupTestFrameworkScriptFile: "expect-puppeteer",
5+
testPathIgnorePatterns: [".eslintrc.js"],
56
transform: {
67
"^.+\\.js$": "babel-jest",
7-
'^.+\\.txt$': 'jest-raw-loader',
8-
'^.+\\.html$': 'jest-raw-loader'
8+
"^.+\\.txt$": "jest-raw-loader",
9+
"^.+\\.html$": "jest-raw-loader"
910
}
1011
};

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"jquery": "3.x",
3838
"lerna": "^2.9.0",
3939
"prettier": "^1.14.2",
40+
"promise": "^8.0.3",
4041
"puppeteer": "^1.4.0"
4142
},
4243
"workspaces": [
+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
## Shopify Predictive Search JS API
2+
3+
### Configuration options
4+
5+
<table>
6+
<tr>
7+
<th width="30%">Type of search</th>
8+
<th>Options</th>
9+
</tr>
10+
<tr>
11+
<td><code>resources</code></td>
12+
<td>
13+
<table>
14+
<tr>
15+
<th width="30%">Option</th>
16+
<th>Type</th>
17+
<th>Description</th>
18+
</tr>
19+
<tr>
20+
<td><code>types</code> (required)</td>
21+
<td>Array</td>
22+
<td>Specifies the type of results requested. Currently supports <code>product</code> only.</td>
23+
</tr>
24+
<tr>
25+
<td><code>fuzzy</code> (optional)</td>
26+
<td>Boolean</td>
27+
<td>Enables typo tolerance when searching. A typo will be tolerated after the fourth character. Default: <code>true</code>.</td>
28+
</tr>
29+
<tr>
30+
<td><code>unavailable_products</code> (optional)</td>
31+
<td>String</td>
32+
<td>Specifies whether to display <code>unavailable_products</code> results. The three possible options are <code>show</code>, <code>hide</code>, and <code>bury</code>. Burying out of stock products pushes them below other matching results. Default: <code>show</code>.</td>
33+
</tr>
34+
<tr>
35+
<td><code>limit</code> (optional)</td>
36+
<td>Integer</td>
37+
<td>Limits the number of results returned. Default: <code>10</code>. Min: <code>1</code>. Max: <code>10</code>.</td>
38+
</tr>
39+
</table>
40+
</td>
41+
</tr>
42+
</table>
43+
44+
See the [help docs for Predictive Search](https://help.shopify.com/en/themes/development/predictive-search) for more information.
45+
46+
---
47+
48+
### Getting started
49+
50+
```javascript
51+
import PredictiveSearch from "@shopify/theme-predictive-search";
52+
53+
var predictiveSearch = new PredictiveSearch({
54+
resources: {
55+
fuzzy: true,
56+
types: [PredictiveSearch.TYPES.PRODUCT],
57+
limit: 4,
58+
unavailable_products: "bury"
59+
}
60+
});
61+
62+
predictiveSearch.on("success", function(json) {
63+
// See "Success Response" section of this document
64+
});
65+
66+
predictiveSearch.on("error", function(error) {
67+
// See "HTTP status `3xx-4xx` Response" section of this document
68+
});
69+
70+
predictiveSearch.query("The Calling");
71+
```
72+
73+
---
74+
75+
### Success Response
76+
77+
```json
78+
// JSON Output
79+
{
80+
"resources": {
81+
"results": {
82+
"products": [
83+
{
84+
"id": 1,
85+
"title": "The Calling",
86+
"body": "<p>The Calling</p>",
87+
"handle": "calling",
88+
"image": "https://cdn.shopify.com/...",
89+
"type": "Bikes",
90+
"tags": ["bike", "dolphin"],
91+
"url": "/products/calling?variant_id=1",
92+
"price": "3099",
93+
"available": true,
94+
"variants": [
95+
{
96+
"title": "Large / Angry Dolphin",
97+
"url": "https://www.evil-bikes.com/products/calling",
98+
"image": "https://cdn.shopify.com/...",
99+
"price": "3099",
100+
"compare_at_price": "4099",
101+
"available": true,
102+
}
103+
]
104+
}
105+
]
106+
}
107+
}
108+
}
109+
```
110+
111+
### HTTP status `3xx-4xx` Response
112+
113+
```js
114+
predictiveSearch.on("error", function(error) {
115+
console.error(error.status); // Ex. output: 429
116+
console.error(error.name); // Ex. output: Throttled
117+
console.error(error.message); // Ex. output: Too Many Requests
118+
119+
// When the request response HTTP status is 429
120+
console.error(error.retryAfter); // Ex. output: 100
121+
});
122+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"resources": {
3+
"results": {
4+
"products": [
5+
{
6+
"title": "The Calling",
7+
"body": "<p>The Calling</p>",
8+
"handle": "calling",
9+
"image": "https://cdn.shopify.com/...",
10+
"url": "/products/calling?variant_id=1",
11+
"price": "3099",
12+
"variants": [
13+
{
14+
"title": "Large / Angry Dolphin",
15+
"url": "https://www.evil-bikes.com/products/calling",
16+
"image": "https://cdn.shopify.com/...",
17+
"price": "3099",
18+
"compare_at_price": "4099"
19+
}
20+
]
21+
}
22+
]
23+
}
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"resources": {
3+
"results": {
4+
"products": [
5+
{
6+
"title": "The Following",
7+
"body": "<p>The Following</p>",
8+
"handle": "following",
9+
"image": "https://cdn.shopify.com/...",
10+
"url": "/products/following?variant_id=1",
11+
"price": "3099",
12+
"variants": [
13+
{
14+
"title": "Large / Angry Dolphin",
15+
"url": "https://www.evil-bikes.com/products/following",
16+
"image": "https://cdn.shopify.com/...",
17+
"price": "3099",
18+
"compare_at_price": "4099"
19+
}
20+
]
21+
}
22+
]
23+
}
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"root": true,
3+
"extends": [
4+
"plugin:shopify/esnext",
5+
"plugin:shopify/node",
6+
"plugin:shopify/prettier"
7+
],
8+
"env": {
9+
"browser": true,
10+
"jest": true
11+
},
12+
"globals": {
13+
"page": true
14+
},
15+
"rules": {
16+
"shopify/strict-component-boundaries": "off",
17+
"babel/object-curly-spacing": 0
18+
}
19+
}

0 commit comments

Comments
 (0)