Skip to content

Commit 1a60498

Browse files
Added comments and reactions to fetchPost
1 parent 98d16f0 commit 1a60498

File tree

4 files changed

+132
-18
lines changed

4 files changed

+132
-18
lines changed

examples/fetch-post.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import LinkedApi, { LinkedApiError } from 'linkedapi-node';
1+
import LinkedApi, { LinkedApiError, POST_COMMENTS_SORT } from 'linkedapi-node';
22

33
async function fetchPostExample(): Promise<void> {
44
const linkedapi = new LinkedApi({
@@ -21,7 +21,16 @@ async function fetchPostExample(): Promise<void> {
2121

2222
async function standardExample(linkedapi: LinkedApi): Promise<void> {
2323
const workflowId = await linkedapi.fetchPost.execute({
24-
postUrl: 'https://www.linkedin.com/posts/post-url'
24+
postUrl: 'https://www.linkedin.com/posts/post-url',
25+
retrieveComments: true,
26+
retrieveReactions: true,
27+
commentsRetrievalConfig: {
28+
limit: 25,
29+
sort: POST_COMMENTS_SORT.mostRelevant,
30+
},
31+
reactionsRetrievalConfig: {
32+
limit: 100,
33+
},
2534
});
2635
console.log('🔍 Workflow started:', workflowId);
2736
const postResult = await linkedapi.fetchPost.result(workflowId);
@@ -39,6 +48,8 @@ async function standardExample(linkedapi: LinkedApi): Promise<void> {
3948
console.log(`👍 Reactions: ${post.reactionsCount}`);
4049
console.log(`💬 Comments: ${post.commentsCount}`);
4150
console.log(`🔄 Reposts: ${post.repostsCount}`);
51+
console.log(`👍 Reactions: ${post.reactions?.length || 0}`);
52+
console.log(`💬 Comments: ${post.comments?.length || 0}`);
4253
}
4354
if (postResult.errors.length > 0) {
4455
console.error('🚨 Errors:', JSON.stringify(postResult.errors, null, 2));

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "linkedapi-node",
3-
"version": "1.2.6",
3+
"version": "1.2.7",
44
"description": "Official TypeScript SDK for Linked API",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/operations/fetch-post.ts

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,54 @@
11
import { Operation, TOperationName } from '../core';
2-
import { SimpleWorkflowMapper } from '../mappers';
3-
import { TFetchPostParams, TFetchPostResult } from '../types';
2+
import { TActionConfig, ThenWorkflowMapper } from '../mappers';
3+
import { TBaseFetchPostParams, TFetchPostParams, TFetchPostResult } from '../types';
44

5-
export class FetchPost extends Operation<TFetchPostParams, TFetchPostResult> {
5+
export class FetchPost extends Operation<TBaseFetchPostParams, TFetchPostResult> {
66
public override readonly operationName: TOperationName = 'fetchPost';
7-
protected override readonly mapper = new SimpleWorkflowMapper<TFetchPostParams, TFetchPostResult>(
8-
{
9-
actionType: 'st.openPost',
7+
protected override readonly mapper = new FetchPostMapper();
8+
9+
public override async execute<TParams extends TBaseFetchPostParams>(
10+
params: TFetchPostParams<TParams>,
11+
): Promise<string> {
12+
return super.execute(params);
13+
}
14+
}
15+
16+
const FETCH_POST_ACTIONS: TActionConfig[] = [
17+
{
18+
paramName: 'retrieveComments',
19+
actionType: 'st.retrievePostComments',
20+
configSource: 'commentsRetrievalConfig',
21+
},
22+
{
23+
paramName: 'retrieveReactions',
24+
actionType: 'st.retrievePostReactions',
25+
configSource: 'reactionsRetrievalConfig',
26+
},
27+
];
28+
29+
const RESPONSE_MAPPINGS = [
30+
{
31+
actionType: 'st.retrievePostComments',
32+
targetProperty: 'comments',
33+
},
34+
{
35+
actionType: 'st.retrievePostReactions',
36+
targetProperty: 'reactions',
37+
},
38+
];
39+
40+
export class FetchPostMapper<TParams extends TBaseFetchPostParams> extends ThenWorkflowMapper<
41+
TParams,
42+
TFetchPostResult
43+
> {
44+
constructor() {
45+
super({
46+
actionConfigs: FETCH_POST_ACTIONS,
47+
responseMappings: RESPONSE_MAPPINGS,
48+
baseActionType: 'st.openPost',
1049
defaultParams: {
1150
basicInfo: true,
1251
},
13-
},
14-
);
52+
});
53+
}
1554
}

src/types/actions/post.ts

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TBaseActionParams } from '../params';
1+
import { TBaseActionParams, TLimitParams } from '../params';
22

33
export interface TPost {
44
url: string;
@@ -12,6 +12,8 @@ export interface TPost {
1212
reactionsCount: number;
1313
commentsCount: number;
1414
repostsCount: number;
15+
comments?: ReadonlyArray<TPostComment>;
16+
reactions?: ReadonlyArray<TPostReaction>;
1517
}
1618

1719
export const POST_TYPE = {
@@ -20,12 +22,6 @@ export const POST_TYPE = {
2022
} as const;
2123
export type TPostType = (typeof POST_TYPE)[keyof typeof POST_TYPE];
2224

23-
export interface TFetchPostParams extends TBaseActionParams {
24-
postUrl: string;
25-
}
26-
27-
export type TFetchPostResult = TPost;
28-
2925
export interface TReaction {
3026
postUrl: string;
3127
time: string;
@@ -59,3 +55,71 @@ export interface TCommentOnPostParams extends TBaseActionParams {
5955
postUrl: string;
6056
text: string;
6157
}
58+
59+
export const POST_COMMENTER_TYPE = {
60+
person: 'person',
61+
company: 'company',
62+
} as const;
63+
export type TPostCommenterType = (typeof POST_COMMENTER_TYPE)[keyof typeof POST_COMMENTER_TYPE];
64+
65+
export interface TPostComment {
66+
commenterUrl: string;
67+
commenterName: string;
68+
commenterHeadline: string;
69+
commenterType: TPostCommenterType;
70+
time: string;
71+
text: string | null;
72+
image: string | null;
73+
isReply: boolean;
74+
reactionsCount: number;
75+
repliesCount: number;
76+
}
77+
78+
export const POST_ENGAGER_TYPE = {
79+
person: 'person',
80+
company: 'company',
81+
} as const;
82+
export type TPostEngagerType = (typeof POST_ENGAGER_TYPE)[keyof typeof POST_ENGAGER_TYPE];
83+
84+
export interface TPostReaction {
85+
engagerUrl: string;
86+
engagerName: string;
87+
engagerHeadline: string;
88+
engagerType: TPostEngagerType;
89+
type: TReactionType;
90+
}
91+
92+
export const POST_COMMENTS_SORT = {
93+
mostRelevant: 'mostRelevant',
94+
mostRecent: 'mostRecent',
95+
} as const;
96+
export type TPostCommentsSort = (typeof POST_COMMENTS_SORT)[keyof typeof POST_COMMENTS_SORT];
97+
98+
export interface TPostCommentsRetrievalConfig extends TLimitParams {
99+
replies?: boolean;
100+
sort?: TPostCommentsSort;
101+
}
102+
103+
export type TPostReactionsRetrievalConfig = TLimitParams;
104+
105+
export interface TBaseFetchPostParams extends TBaseActionParams {
106+
postUrl: string;
107+
retrieveComments?: boolean;
108+
retrieveReactions?: boolean;
109+
}
110+
111+
export interface TBaseFetchPostParamsWide extends TBaseFetchPostParams {
112+
retrieveComments: true;
113+
retrieveReactions: true;
114+
}
115+
116+
export type TFetchPostParams<T extends TBaseFetchPostParams = TBaseFetchPostParams> = T & {
117+
commentsRetrievalConfig?: T['retrieveComments'] extends true
118+
? TPostCommentsRetrievalConfig | undefined
119+
: never;
120+
reactionsRetrievalConfig?: T['retrieveReactions'] extends true
121+
? TPostReactionsRetrievalConfig | undefined
122+
: never;
123+
};
124+
125+
export type TFetchPostResult = TPost;

0 commit comments

Comments
 (0)