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

Commit 25a2c07

Browse files
authored
Merge pull request #2 from roast-cms/feature/vendors-docs
update docs, and add ability to fetch all vendors for a link
2 parents b5d23d6 + fc877ed commit 25a2c07

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
lines changed

README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,16 @@ Complete example can be found in `./example-server.ts` and can be run with `yarn
3232

3333
## API:
3434

35-
#### GET `/recommends/widget?locale=us`
35+
#### GET `/recommends/widget`
3636

3737
```json
3838
{
39-
"link": "https://shop.com/us/widget?referral=you"
39+
"link": "widget",
40+
"status": 200,
41+
"vendor": {
42+
"name": "eBay",
43+
"url": "https://ebay.com/us/widget?referral=you"
44+
}
4045
}
4146
```
4247

@@ -56,6 +61,15 @@ For this example to work, MongoDB collection `links` should have the following d
5661
}
5762
```
5863

64+
##### Parameters:
65+
66+
- `locale` - Not implemented but planned. Would prioritize local vendors.
67+
- `vendors` - Select vendor groups. Currently implemented: `"ALL"`, which lists all vendors for the link.
68+
69+
#### GET `/recommends/group?tag=cameras`
70+
71+
Returns all links for a given `tag` param.
72+
5973
## Usage (with React):
6074

6175
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):

src/index.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,17 @@ const tool = ({ pathName }: { pathName: string | undefined }) => {
4444
// query
4545
const linkID = req.params.link;
4646
const linkTag = req.query.tag;
47+
const linkVendors = req.query.vendors;
48+
49+
/**
50+
Group/list of links with a particular tag.
51+
*/
4752
const isGroupRequest = linkTag && linkID === "group";
4853
const dbDocument = isGroupRequest
4954
? await Links.find({ tags: { $in: [linkTag] } })
5055
: await Links.findOne({ link: linkID })
5156
.cache(60 * 10) // cache links for 10 min
5257
.exec();
53-
54-
// group/list of links with a particular tag
5558
if (isGroupRequest && dbDocument.length) {
5659
return res.json({
5760
status: 200,
@@ -73,6 +76,22 @@ const tool = ({ pathName }: { pathName: string | undefined }) => {
7376
// unwraps the response
7477
const doc = Object.assign({}, dbDocument._doc);
7578

79+
/**
80+
Shows all vendors.
81+
*/
82+
if (linkVendors === "all")
83+
return res.json({
84+
status: 200,
85+
link: doc.link,
86+
tags: doc.tags,
87+
vendors: doc.vendors?.map(({ name, url, value, locale }) => ({
88+
name,
89+
url,
90+
value,
91+
locale,
92+
})),
93+
});
94+
7695
/**
7796
Sorts vendors based on value you assign to them & pics top-valued one.
7897
*/
@@ -84,6 +103,13 @@ const tool = ({ pathName }: { pathName: string | undefined }) => {
84103
}
85104
)[0];
86105

106+
/**
107+
TODO:
108+
Add a condition where if the locale is specified, and it exists
109+
in the list of vendors, matching results get bumped above other vendors
110+
in the list.
111+
*/
112+
87113
// successful API response
88114
return res.json({
89115
status: 200,
@@ -92,6 +118,7 @@ const tool = ({ pathName }: { pathName: string | undefined }) => {
92118
url: topValuedVendor.url,
93119
locale: topValuedVendor.locale,
94120
name: topValuedVendor.name,
121+
tags: topValuedVendor.tags,
95122
// you may not want to share the vale you place on various vendors
96123
},
97124
});

src/utils/mongoose.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ import * as mongoose from "mongoose";
33
require("dotenv").config();
44
const databaseURI = process.env.DATABASE_URI || "";
55
const cachegoose = require("recachegoose");
6+
7+
console.log("databaseURI", databaseURI);
8+
69
try {
710
const client = require("./redis").default;
11+
console.log("client.options", client.options);
812
cachegoose(mongoose, {
913
engine: "redis",
1014
port: client.options.port,

src/utils/redis.ts

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

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

0 commit comments

Comments
 (0)