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

Commit 1c6f886

Browse files
authored
Merge pull request #4 from roast-cms/feature/bulk-request
Feature/bulk request
2 parents 603b3a6 + 435d06d commit 1c6f886

File tree

5 files changed

+54
-5
lines changed

5 files changed

+54
-5
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ For this example to work, MongoDB collection `links` should have the following d
7070

7171
Returns all links for a given `tag` param.
7272

73+
#### GET `/recommends/bulk?links=widget,toy,fancy-garmet`
74+
75+
Returns all links (including all vendors) from array inferred from `links` param (uses `array.split(",")`).
76+
7377
## Usage (with React):
7478

7579
This an example using React framework, however, the idea would be the same in any kind of project (both on server and on the browser):

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@roast-cms/links",
3-
"version": "0.3.0",
3+
"version": "0.4.0",
44
"description": "A Node.js API for managing affiliate links.",
55
"main": "dist/index.js",
66
"repository": "https://github.com/roast-cms/links.git",

src/index.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,62 @@ const tool = ({ pathName }: { pathName: string | undefined }) => {
4545
const linkID = req.params.link;
4646
const linkTag = req.query.tag;
4747
const linkVendors = req.query.vendors;
48+
const bulkLinkList = req.query.links;
49+
50+
/**
51+
Bulk request for multiple link IDs.
52+
*/
53+
if (linkID === "bulk" && bulkLinkList) {
54+
const linkIDs: string[] = bulkLinkList.split(",");
55+
if (!linkIDs.length)
56+
return res.json({
57+
status: 500,
58+
message: "No link IDs found.",
59+
});
60+
61+
let status = 200;
62+
let message = undefined;
63+
64+
if (linkIDs.length > 10) {
65+
status = 206;
66+
message = "Trimmed link ID list to the maximum of 10.";
67+
linkIDs = arr.slice(0, 10);
68+
}
69+
70+
const dbDocuments =
71+
(await Links.find(
72+
{
73+
link: { $in: linkIDs },
74+
},
75+
{ _id: 0 } // remove _id keys for the first level
76+
)
77+
.cache(60 * 10) // cache links for 10 min
78+
.exec()) || [];
79+
80+
return res.json({
81+
status,
82+
message,
83+
group: dbDocuments.map(({ vendors, link, tags }) => ({
84+
link,
85+
vendors: (vendors || []).map(({ name, url, value }) => ({
86+
// explicitly define return keys
87+
name,
88+
url,
89+
value,
90+
})),
91+
tags,
92+
})),
93+
});
94+
}
4895

4996
/**
5097
Group/list of links with a particular tag.
5198
*/
5299
const isGroupRequest = linkTag && linkID === "group";
53100
const dbDocument = isGroupRequest
54101
? await Links.find({ tags: { $in: [linkTag] } })
102+
.cache(60 * 10) // cache links for 10 min
103+
.exec()
55104
: await Links.findOne({ link: linkID })
56105
.cache(60 * 10) // cache links for 10 min
57106
.exec();

src/utils/mongoose.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@ require("dotenv").config();
44
const databaseURI = process.env.DATABASE_URI || "";
55
const cachegoose = require("recachegoose");
66

7-
console.log("databaseURI", databaseURI);
8-
97
try {
108
const client = require("./redis").default;
11-
console.log("client.options", client.options);
129
cachegoose(mongoose, {
1310
engine: "redis",
1411
port: client.options.port,

src/utils/redis.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import * as bluebird from "bluebird";
33

44
require("dotenv").config();
55
const redisURL = process.env.REDIS_URL || "";
6-
console.log("redisURL", redisURL);
76
const client = redis.createClient({ url: redisURL });
87
bluebird.promisifyAll(redis.RedisClient.prototype);
98
bluebird.promisifyAll(redis.Multi.prototype);

0 commit comments

Comments
 (0)