Skip to content

Commit 72de2fa

Browse files
itsmeadiaditya
andauthored
chore: ranking vars now accepts only json string (#616)
* chore: ranking vars now accept both string and maps * chore: ranking vars add tests --------- Co-authored-by: aditya <[email protected]>
1 parent fbad4a6 commit 72de2fa

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

src/client.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ export class StreamClient<StreamFeedGenerics extends DefaultGenerics = DefaultGe
503503
}
504504

505505
replaceReactionOptions = (options: {
506-
rankingVars?: Record<string, string | number>;
506+
rankingVars?: string | Record<string, string | number>;
507507
reactionKindsFilter?: string[];
508508
reactions?: Record<string, string | boolean | string[] | Record<string, string | number>>;
509509
withOwnChildren?: boolean;
@@ -522,7 +522,15 @@ export class StreamClient<StreamFeedGenerics extends DefaultGenerics = DefaultGe
522522
options.withRecentReactions = options.reactions.recent as boolean;
523523
}
524524
if (options.reactions.ranking_vars != null) {
525-
options.rankingVars = options.reactions.ranking_vars as Record<string, string | number>;
525+
if (typeof options.reactions.ranking_vars === 'object') {
526+
options.rankingVars = options.reactions.ranking_vars as Record<string, string | number>;
527+
} else if (typeof options.reactions.ranking_vars === 'string') {
528+
options.rankingVars = options.reactions.ranking_vars as string;
529+
}
530+
}
531+
// if ranking vars are Record, json stringify them
532+
if (options.rankingVars && typeof options.rankingVars === 'object') {
533+
options.rankingVars = JSON.stringify(options.rankingVars);
526534
}
527535
if (options.reactions.score_vars != null) {
528536
options.withScoreVars = options.reactions.score_vars as boolean;

src/feed.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export type FeedPaginationOptions = {
4141
export type RankedFeedOptions = {
4242
offset?: number;
4343
ranking?: string;
44-
rankingVars?: Record<string, string | number>;
44+
rankingVars?: string | Record<string, string | number>;
4545
session?: string;
4646
withScoreVars?: boolean;
4747
};

test/integration/cloud/reaction.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,36 @@ describe('Reaction pagination', () => {
6666
});
6767
});
6868

69+
describe('#withRankingVarsAsMap', () => {
70+
ctx.requestShouldNotError(async () => {
71+
ctx.response = await ctx.bob.feed('user', ctx.alice.userId).get({
72+
reactions: { score_vars: true },
73+
limit: 4,
74+
offset: 2,
75+
rankingVars: { popular: 1, music: 4 },
76+
});
77+
});
78+
});
79+
describe('#withRankingVarsShouldErr', () => {
80+
ctx.requestShouldError(400, async () => {
81+
ctx.response = await ctx.bob.feed('user', ctx.alice.userId).get({
82+
reactions: { score_vars: true },
83+
limit: 4,
84+
offset: 2,
85+
rankingVars: ['popular', 1],
86+
});
87+
});
88+
});
89+
describe('#withRankingVarsAsString', () => {
90+
ctx.requestShouldNotError(async () => {
91+
ctx.response = await ctx.bob.feed('user', ctx.alice.userId).get({
92+
reactions: { score_vars: true },
93+
limit: 4,
94+
offset: 2,
95+
rankingVars: '{"sports":1,"music":4}',
96+
});
97+
});
98+
});
6999
describe('When bob reads alice her feed with all enrichment enabled', () => {
70100
ctx.requestShouldNotError(async () => {
71101
ctx.response = await ctx.bob.feed('user', ctx.alice.userId).get({

0 commit comments

Comments
 (0)