Skip to content

Commit 4c1a754

Browse files
authored
Merging pull request #18649
* Implement barcode lookup actions and utility functions; update version to 0.1.0 * pnpm update
1 parent da90665 commit 4c1a754

File tree

7 files changed

+261
-9
lines changed

7 files changed

+261
-9
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import app from "../../barcode_lookup.app.mjs";
2+
import { parseObject } from "../../common/utils.mjs";
3+
4+
export default {
5+
key: "barcode_lookup-batch-barcode-lookup",
6+
name: "Batch Barcode Lookup",
7+
description: "Get multiple products by barcode. [See the documentation](https://www.barcodelookup.com/api-documentation)",
8+
version: "0.0.1",
9+
type: "action",
10+
annotations: {
11+
destructiveHint: false,
12+
openWorldHint: true,
13+
readOnlyHint: true,
14+
},
15+
props: {
16+
app,
17+
barcodes: {
18+
type: "string[]",
19+
label: "Barcodes",
20+
description: "The barcodes to search for. You can also use a partial barcode followed by an asterisk (*).",
21+
},
22+
},
23+
async run({ $ }) {
24+
const response = await this.app.searchProducts({
25+
$,
26+
params: {
27+
barcode: parseObject(this.barcodes)?.join(","),
28+
},
29+
});
30+
$.export("$summary", "Successfully retrieved products by barcodes");
31+
return response;
32+
},
33+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import app from "../../barcode_lookup.app.mjs";
2+
3+
export default {
4+
key: "barcode_lookup-get-product-by-barcode",
5+
name: "Get Product by Barcode",
6+
description: "Get a product by barcode. [See the documentation](https://www.barcodelookup.com/api-documentation)",
7+
version: "0.0.1",
8+
type: "action",
9+
annotations: {
10+
destructiveHint: false,
11+
openWorldHint: true,
12+
readOnlyHint: true,
13+
},
14+
props: {
15+
app,
16+
barcode: {
17+
type: "string",
18+
label: "Barcode",
19+
description: "The barcode to search for. You can also use a partial barcode (6 digits minimum) followed by an asterisk (*).",
20+
},
21+
},
22+
async run({ $ }) {
23+
const response = await this.app.searchProducts({
24+
$,
25+
params: {
26+
barcode: this.barcode,
27+
},
28+
});
29+
$.export("$summary", "Successfully retrieved product by barcode");
30+
return response;
31+
},
32+
};
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import app from "../../barcode_lookup.app.mjs";
2+
3+
export default {
4+
key: "barcode_lookup-search-products-by-parameters",
5+
name: "Search Products by Parameters",
6+
description: "Search for products by parameters. [See the documentation](https://www.barcodelookup.com/api-documentation)",
7+
version: "0.0.1",
8+
type: "action",
9+
annotations: {
10+
destructiveHint: false,
11+
openWorldHint: true,
12+
readOnlyHint: true,
13+
},
14+
props: {
15+
app,
16+
mpn: {
17+
type: "string",
18+
label: "MPN",
19+
description: "MPN parameter is the manufacturer part number. `E.g. **LXCF9407**`",
20+
optional: true,
21+
},
22+
asin: {
23+
type: "string",
24+
label: "ASIN",
25+
description: "ASIN parameter is the Amazon Standard Identification Number. `E.g. **B079L4WR4T**`",
26+
optional: true,
27+
},
28+
title: {
29+
type: "string",
30+
label: "Title",
31+
description: "Search by product name (title) for any item. `E.g. **Red Running Shoes**`",
32+
optional: true,
33+
},
34+
category: {
35+
type: "string",
36+
label: "Category",
37+
description: "Search by category - based on [Google's product taxonomy](https://www.google.com/basepages/producttype/taxonomy.en-US.txt). `E.g. **Home & Garden > Decor**`",
38+
optional: true,
39+
},
40+
manufacturer: {
41+
type: "string",
42+
label: "Manufacturer",
43+
description: "search by manufacturer - use double quotes (\"\") around value for an exact match. `E.g. **\"Samsung\"**`",
44+
optional: true,
45+
},
46+
brand: {
47+
type: "string",
48+
label: "Brand",
49+
description: "Search by brand - use double quotes (\"\") around value for an exact match. `E.g. **\"Calvin Klein\"**`",
50+
optional: true,
51+
},
52+
search: {
53+
type: "string",
54+
label: "Search",
55+
description: "Query all the search fields including title, category, brand and MPN. `E.g. **\"Air Jordan Red Shoes Size 40\"**`",
56+
optional: true,
57+
},
58+
geo: {
59+
type: "string",
60+
label: "Geo",
61+
description: "Filter online store results by geographical location",
62+
options: [
63+
"us",
64+
"gb",
65+
"ca",
66+
"eu",
67+
],
68+
optional: true,
69+
},
70+
maxResults: {
71+
type: "integer",
72+
label: "Max Results",
73+
description: "The maximum number of results to return",
74+
optional: true,
75+
},
76+
},
77+
async run({ $ }) {
78+
try {
79+
const response = await this.app.paginate({
80+
$,
81+
fn: this.app.searchProducts,
82+
maxResults: this.maxResults,
83+
params: {
84+
mpn: this.mpn,
85+
asin: this.asin,
86+
title: this.title,
87+
category: this.category,
88+
manufacturer: this.manufacturer,
89+
brand: this.brand,
90+
search: this.search,
91+
metadata: "y",
92+
geo: this.geo,
93+
formatted: "y",
94+
},
95+
});
96+
97+
const responseArray = [];
98+
for await (const item of response) {
99+
responseArray.push(item);
100+
}
101+
102+
$.export("$summary", `Successfully retrieved ${responseArray.length} products`);
103+
return responseArray;
104+
} catch (error) {
105+
$.export("$summary", "Successfully retrieved 0 products");
106+
return [];
107+
}
108+
},
109+
};
Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,58 @@
1+
import { axios } from "@pipedream/platform";
2+
13
export default {
24
type: "app",
35
app: "barcode_lookup",
46
propDefinitions: {},
57
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
8+
_baseUrl() {
9+
return "https://api.barcodelookup.com/v3";
10+
},
11+
_params(params = {}) {
12+
return {
13+
...params,
14+
key: `${this.$auth.api_key}`,
15+
};
16+
},
17+
_makeRequest({
18+
$ = this, params, path, ...opts
19+
}) {
20+
return axios($, {
21+
url: this._baseUrl() + path,
22+
params: this._params(params),
23+
...opts,
24+
});
25+
},
26+
searchProducts(opts = {}) {
27+
return this._makeRequest({
28+
path: "/products",
29+
...opts,
30+
});
31+
},
32+
async *paginate({
33+
fn, params = {}, maxResults = null, ...opts
34+
}) {
35+
let hasMore = false;
36+
let count = 0;
37+
let page = 0;
38+
39+
do {
40+
params.page = ++page;
41+
const { products: data } = await fn({
42+
params,
43+
...opts,
44+
});
45+
for (const d of data) {
46+
yield d;
47+
48+
if (maxResults && ++count === maxResults) {
49+
return count;
50+
}
51+
}
52+
53+
hasMore = data?.length;
54+
55+
} while (hasMore);
956
},
1057
},
1158
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export const parseObject = (obj) => {
2+
if (!obj) return undefined;
3+
4+
if (Array.isArray(obj)) {
5+
return obj.map((item) => {
6+
if (typeof item === "string") {
7+
try {
8+
return JSON.parse(item);
9+
} catch (e) {
10+
return item;
11+
}
12+
}
13+
return item;
14+
});
15+
}
16+
if (typeof obj === "string") {
17+
try {
18+
return JSON.parse(obj);
19+
} catch (e) {
20+
return obj;
21+
}
22+
}
23+
return obj;
24+
};

components/barcode_lookup/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/barcode_lookup",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream Barcode Lookup Components",
55
"main": "barcode_lookup.app.mjs",
66
"keywords": [
@@ -11,5 +11,8 @@
1111
"author": "Pipedream <support@pipedream.com> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.1.0"
1417
}
1518
}

pnpm-lock.yaml

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

0 commit comments

Comments
 (0)