-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Client.ts
540 lines (503 loc) · 19.9 KB
/
Client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
import {
ApiVersion,
CommentField,
ContainerField,
ContentPublishingLimitFields,
DayMetric,
HashtagMediaField,
LifetimeMetric,
MediaField,
PageField,
PublicMediaField,
SimplePostMetric,
StoryMetric,
WeekAndMonthMetric,
} from './Enums';
import { DeleteCommentRequest } from './requests/comment/DeleteCommentRequest';
import { GetCommentRequest } from './requests/comment/GetCommentRequest';
import { PostHideCommentRequest } from './requests/comment/PostHideCommentRequest';
import { GetRepliesRequest } from './requests/comment/replies/GetRepliesRequest';
import { PostReplyRequest } from './requests/comment/replies/PostReplyRequest';
import { GetContainerRequest } from './requests/container/GetContainerRequest';
import { GetHashtagRecentMediaRequest } from './requests/hashtag/media/GetHashtagRecentMediaRequest';
import { GetHashtagTopMediaRequest } from './requests/hashtag/media/GetHashtagTopMediaRequest';
import { GetHashtagIdRequest } from './requests/hashtag/search/GetHashtagIdRequest';
import { GetMediaChildrenRequest } from './requests/media/children/GetMediaChildrenRequest';
import { GetMediaCommentsRequest } from './requests/media/comments/GetMediaCommentsRequest';
import { PostMediaCommentRequest } from './requests/media/comments/PostMediaCommentRequest';
import { GetMediaInfoRequest } from './requests/media/info/GetMediaInfoRequest';
import { GetSimplePostMediaInsightsRequest } from './requests/media/insights/GetSimplePostMediaInsightsRequest';
import { GetStoryMediaInsightsRequest } from './requests/media/insights/GetStoryMediaInsightsRequest';
import { GetContentPublishingLimitRequest } from './requests/page/content_publishing_limit/GetContentPublishingLimitRequest';
import { GetPageInfoRequest } from './requests/page/info/GetPageInfoRequest';
import { GetPageDayInsightsRequest } from './requests/page/insights/GetPageDayInsightsRequest';
import { GetPageLifetimeInsightsRequest } from './requests/page/insights/GetPageLifetimeInsightsRequest';
import { GetPageMonthInsightsRequest } from './requests/page/insights/GetPageMonthInsightsRequest';
import { GetPageWeekInsightsRequest } from './requests/page/insights/GetPageWeekInsightsRequest';
import { GetInstagramAccountInfoRequest } from './requests/page/instagram_account_info/GetInstagramAccountInfoRequest';
import { GetPageMediaRequest } from './requests/page/media/GetPageMediaRequest';
import { PostPageCarouselMediaRequest } from './requests/page/media/PostPageCarouselMediaRequest';
import { PostPagePhotoMediaRequest } from './requests/page/media/PostPagePhotoMediaRequest';
import { PostPageVideoMediaRequest } from './requests/page/media/PostPageVideoMediaRequest';
import { PostPublishMediaRequest } from './requests/page/media_publish/PostPublishMediaRequest';
import { GetPageRecentlySearchedHashtagsRequest } from './requests/page/recently_searched_hashtags/GetPageRecentlySearchedHashtagsRequest';
import { GetPageStoriesRequest } from './requests/page/stories/GetPageStoriesRequest';
import { GetTagsRequest } from './requests/page/tags/GetTagsRequest';
import { UserTag } from './requests/Params';
import { PostPageReelMediaRequest } from './requests/page/media/PostPageReelMediaRequest';
/**
* A client that creating requests.
*
* @author Tiago Grosso <tiagogrosso99@gmail.com>
* @since 0.2.0
*/
export class Client {
/**
* The API access token.
*/
private readonly accessToken: string;
/**
* The id of the page. This is used on page related requests, so it should be the page for which the access token has permissions.
*/
private readonly pageId: string;
/**
* The API version to use on requests.
*/
private readonly apiVersion?: ApiVersion;
/**
* The constructor.
*
* @param accessToken the access token.
* @param pageId The id of the page.
* @param apiVersion the API version
*/
constructor(accessToken: string, pageId: string, apiVersion?: ApiVersion) {
this.accessToken = accessToken;
this.pageId = pageId;
this.apiVersion = apiVersion;
}
/**
* Gets the access token.
*
* @returns the access token.
*/
public getAccessToken(): string {
return this.accessToken;
}
/**
* Gets the page id.
*
* @returns the page id.
*/
public getPageId(): string {
return this.pageId;
}
/**
* Builds a new {@link GetPageInfoRequest}.
*
* @param fields the fields to retrieve from the API. If no field is specified, all are retrieved.
*
* @returns a new {@link GetPageInfoRequest}.
*/
public newGetPageInfoRequest(...fields: PageField[]): GetPageInfoRequest {
return new GetPageInfoRequest(this.accessToken, this.pageId, ...fields).withApiVersion(this.apiVersion);
}
/**
* Builds a new {@link GetPageMediaRequest}.
*
* @param fields the fields to retrieve from the API for each media object. If no field is specified, all are retrieved.
*
* @returns a new {@link GetPageMediaRequest}.
*/
public newGetPageMediaRequest(...fields: MediaField[]): GetPageMediaRequest {
return new GetPageMediaRequest(this.accessToken, this.pageId, ...fields).withApiVersion(this.apiVersion);
}
/**
* Builds a new {@link GetPageLifetimeInsightsRequest}.
*
* @param metrics the metrics to retrieve from the API. If no metric is specified, all are retrieved.
*
* @returns a new {@link GetPageLifetimeInsightsRequest}.
*/
public newGetPageLifetimeInsightsRequest(...metrics: LifetimeMetric[]): GetPageLifetimeInsightsRequest {
return new GetPageLifetimeInsightsRequest(this.accessToken, this.pageId, ...metrics).withApiVersion(
this.apiVersion
);
}
/**
* Builds a new {@link GetPageDayInsightsRequest}.
*
* @param metrics the metrics to retrieve from the API. If no metric is specified, all are retrieved.
*
* @returns a new {@link GetPageDayInsightsRequest}.
*/
public newGetPageDayInsightsRequest(...metrics: DayMetric[]): GetPageDayInsightsRequest {
return new GetPageDayInsightsRequest(this.accessToken, this.pageId, ...metrics).withApiVersion(this.apiVersion);
}
/**
* Builds a new {@link GetPageWeekInsightsRequest}.
*
* @param metrics the metrics to retrieve from the API. If no metric is specified, all are retrieved.
*
* @returns a new {@link GetPageWeekInsightsRequest}.
*/
public newGetPageWeekInsightsRequest(...metrics: WeekAndMonthMetric[]): GetPageWeekInsightsRequest {
return new GetPageWeekInsightsRequest(this.accessToken, this.pageId, ...metrics).withApiVersion(
this.apiVersion
);
}
/**
* Builds a new {@link GetPageMonthInsightsRequest}.
*
* @param metrics the metrics to retrieve from the API. If no metric is specified, all are retrieved.
*
* @returns a new {@link GetPageMonthInsightsRequest}.
*/
public newGetPageMonthInsightsRequest(...metrics: WeekAndMonthMetric[]): GetPageMonthInsightsRequest {
return new GetPageMonthInsightsRequest(this.accessToken, this.pageId, ...metrics).withApiVersion(
this.apiVersion
);
}
/**
* Builds a new {@link GetMediaInfoRequest}.
*
* @param mediaId the media object id.
* @param fields the fields to retrieve from the API. If no field is specified, all are retrieved.
*
* @returns a new {@link GetMediaInfoRequest}.
*/
public newGetMediaInfoRequest(mediaId: string, ...fields: MediaField[]): GetMediaInfoRequest {
return new GetMediaInfoRequest(this.accessToken, mediaId, ...fields).withApiVersion(this.apiVersion);
}
/**
* Builds a new {@link GetMediaChildrenRequest}.
*
* @param mediaId the media object id.
*
* @returns a new {@link GetMediaChildrenRequest}.
*/
public newGetMediaChildrenRequest(mediaId: string): GetMediaChildrenRequest {
return new GetMediaChildrenRequest(this.accessToken, mediaId).withApiVersion(this.apiVersion);
}
/**
* Builds a new {@link GetMediaCommentsRequest}.
*
* @param mediaId the media object id.
* @param fields the fields to retrieve from the API. If no field is specified, all are retrieved.
*
* @returns a new {@link GetMediaCommentsRequest}.
*/
public newGetMediaCommentsRequest(mediaId: string, ...fields: CommentField[]): GetMediaCommentsRequest {
return new GetMediaCommentsRequest(this.accessToken, mediaId, ...fields).withApiVersion(this.apiVersion);
}
/**
* Builds a new {@link PostMediaCommentRequest}.
*
* @param mediaId the media object id.
* @param text the text of the comment,
*
* @returns a new {@link PostMediaCommentRequest}.
*/
public newPostMediaCommentRequest(mediaId: string, text: string): PostMediaCommentRequest {
return new PostMediaCommentRequest(this.accessToken, mediaId, text).withApiVersion(this.apiVersion);
}
/**
* Builds a new {@link GetSimplePostMediaInsightsRequest}.
*
* @param mediaId the media object id (must be of type 'Photo' or 'Video').
* @param metrics the metrics to retrieve from the API. If no metric is specified, all are retrieved.
*
* @returns a new {@link GetSimplePostMediaInsightsRequest}.
*/
public newGetSimplePostMediaInsightsRequest(
mediaId: string,
...metrics: SimplePostMetric[]
): GetSimplePostMediaInsightsRequest {
return new GetSimplePostMediaInsightsRequest(this.accessToken, mediaId, ...metrics).withApiVersion(
this.apiVersion
);
}
/**
* Builds a new {@link GetStoryMediaInsightsRequest}.
*
* @param mediaId the media object id (must be of type 'Story').
* @param metrics the metrics to retrieve from the API. If no metric is specified, all are retrieved.
*
* @returns a new {@link GetStoryMediaInsightsRequest}.
*/
public newGetStoryMediaInsightsRequest(mediaId: string, ...metrics: StoryMetric[]): GetStoryMediaInsightsRequest {
return new GetStoryMediaInsightsRequest(this.accessToken, mediaId, ...metrics).withApiVersion(this.apiVersion);
}
/**
* Builds a new {@link GetHashtagIdRequest}.
*
* @param hashtag the hashtag.
* @param userId (optional) the id of the user making the request. If omitted, the page id is used.
*
* @returns a new {@link GetHashtagIdRequest}.
*/
public newGetHashtagIdRequest(hashtag: string, userId: string = this.pageId): GetHashtagIdRequest {
return new GetHashtagIdRequest(this.accessToken, userId, hashtag).withApiVersion(this.apiVersion);
}
/**
* Builds a new {@link GetHashtagRecentMediaRequest}.
*
* @param hashtagId the hashtag id.
* @param userId (optional) the id of the user making the request. If omitted, the page id is used.
* @param fields the media fields to retrieve from the API. If no field is specified, all are retrieved.
*
* @returns a new {@link GetHashtagRecentMediaRequest}.
*/
public newGetHashtagRecentMediaRequest(
hashtagId: string,
userId: string = this.pageId,
...fields: HashtagMediaField[]
): GetHashtagRecentMediaRequest {
return new GetHashtagRecentMediaRequest(this.accessToken, hashtagId, userId, ...fields).withApiVersion(
this.apiVersion
);
}
/**
* Builds a new {@link GetHashtagTopMediaRequest}.
*
* @param hashtagId the hashtag id.
* @param userId (optional) the id of the user making the request. If omitted, the page id is used.
* @param fields the media fields to retrieve from the API. If no field is specified, all are retrieved.
*
* @returns a new {@link GetHashtagTopMediaRequest}.
*/
public newGetHashtagTopMediaRequest(
hashtagId: string,
userId: string = this.pageId,
...fields: HashtagMediaField[]
): GetHashtagTopMediaRequest {
return new GetHashtagTopMediaRequest(this.accessToken, hashtagId, userId, ...fields).withApiVersion(
this.apiVersion
);
}
/**
* Builds a new {@link GetCommentRequest}.
*
* @param commentId the comment id.
* @param fields the comment fields to retrieve from the API. If no field is specified, all are retrieved.
*
* @returns a new {@link GetCommentRequest}.
*/
public newGetCommentRequest(commentId: string, ...fields: CommentField[]): GetCommentRequest {
return new GetCommentRequest(this.accessToken, commentId, ...fields).withApiVersion(this.apiVersion);
}
/**
* Builds a new {@link PostHideCommentRequest}.
*
* @param commentId the comment id.
* @param hide whether to hide or show the comment (default: true).
*
* @returns a new {@link PostHideCommentRequest}.
*/
public newPostHideCommentRequest(commentId: string, hide = true): PostHideCommentRequest {
return new PostHideCommentRequest(this.accessToken, commentId, hide).withApiVersion(this.apiVersion);
}
/**
* Builds a new {@link DeleteCommentRequest}.
*
* @param commentId the comment id.
*
* @returns a new {@link DeleteCommentRequest}.
*/
public newDeleteCommentRequest(commentId: string): DeleteCommentRequest {
return new DeleteCommentRequest(this.accessToken, commentId).withApiVersion(this.apiVersion);
}
/**
* Builds a new {@link GetRepliesRequest}.
*
* @param commentId the comment id.
* @param fields the comment fields to retrieve from the API. If no field is specified, all are retrieved.
*
* @returns a new {@link GetRepliesRequest}.
*/
public newGetRepliesRequest(commentId: string, ...fields: CommentField[]): GetRepliesRequest {
return new GetRepliesRequest(this.accessToken, commentId, ...fields).withApiVersion(this.apiVersion);
}
/**
* Builds a new {@link PostReplyRequest}.
*
* @param commentId the comment id.
* @param text the text of the reply.
*
* @returns a new {@link PostReplyRequest}.
*/
public newPostReplyRequest(commentId: string, text: string): PostReplyRequest {
return new PostReplyRequest(this.accessToken, commentId, text).withApiVersion(this.apiVersion);
}
/**
* Builds a new {@link GetTagsRequest}.
*
* @param fields the fields to retrieve from the API for each media object. If no field is specified, all are retrieved.
*
* @returns a new {@link GetTagsRequest}.
*/
public newGetTagsRequest(...fields: PublicMediaField[]): GetTagsRequest {
return new GetTagsRequest(this.accessToken, this.pageId, ...fields).withApiVersion(this.apiVersion);
}
/**
* Builds a new {@link GetContainerRequest}.
*
* @param containerId the container id.
* @param fields the fields to retrieve from the API.
*
* @returns a new {@link GetCommentRequest}.
*/
public newGetContainerRequest(containerId: string, ...fields: ContainerField[]): GetContainerRequest {
return new GetContainerRequest(this.accessToken, containerId, ...fields).withApiVersion(this.apiVersion);
}
/**
* Build a new {@link PostPagePhotoMediaRequest}.
*
* @param imageUrl the image URL.
* @param caption the caption.
* @param locationId the location id.
* @param userTags the user_tags.
*
* @returns a new {@link PostPagePhotoMediaRequest}.
*/
public newPostPagePhotoMediaRequest(
imageUrl: string,
caption?: string,
locationId?: string,
userTags?: UserTag[]
): PostPagePhotoMediaRequest {
return new PostPagePhotoMediaRequest(
this.accessToken,
this.pageId,
imageUrl,
caption,
locationId,
userTags
).withApiVersion(this.apiVersion);
}
/**
* Build a new {@link PostPageVideoMediaRequest}.
*
* @param videoUrl the image URL.
* @param caption the caption.
* @param thumbOffset the thumbnail offset time in milliseconds.
* @param locationId the location id.
*
* @returns a new {@link PostPageVideoMediaRequest}.
*/
public newPostPageVideoMediaRequest(
videoUrl: string,
caption?: string,
thumbOffset?: number,
locationId?: string
): PostPageVideoMediaRequest {
return new PostPageVideoMediaRequest(
this.accessToken,
this.pageId,
videoUrl,
caption,
thumbOffset,
locationId
).withApiVersion(this.apiVersion);
}
/**
* Build a new {@link PostPageReelMediaRequest}.
*
* @param videoUrl the video URL.
* @param caption the caption.
* @param thumbOffset the thumbnail offset.
* @param shareToFeed whether the reel should be shared in the feed as well.
* @param locationId the location id.
*
* @returns a new {@link PostPageReelMediaRequest}.
*/
public newPostPageReelMediaRequest(
videoUrl: string,
caption?: string,
thumbOffset?: number,
shareToFeed?: boolean,
locationId?: string
): PostPageReelMediaRequest {
return new PostPageReelMediaRequest(
this.accessToken,
this.pageId,
videoUrl,
caption,
thumbOffset,
shareToFeed,
locationId
).withApiVersion(this.apiVersion);
}
/**
* Build a new {@link PostPageCarouselMediaRequest}.
*
* @param caption the caption.
* @param locationId the location id.
* @param children the carousel children.
*
* @returns a new {@link PostPageCarouselMediaRequest}.
*/
public newPostPageCarouselMediaRequest(
children?: string[],
caption?: string,
locationId?: string
): PostPageCarouselMediaRequest {
return new PostPageCarouselMediaRequest(
this.accessToken,
this.pageId,
children,
caption,
locationId
).withApiVersion(this.apiVersion);
}
/**
* Builds a new {@link PostPublishMediaRequest}.
*
* @param containerId the container id.
*
* @returns a new {@link PostPublishMediaRequest}.
*/
public newPostPublishMediaRequest(containerId: string): PostPublishMediaRequest {
return new PostPublishMediaRequest(this.accessToken, this.pageId, containerId).withApiVersion(this.apiVersion);
}
/**
* Builds a new {@link GetPageStoriesRequest}.
*
* @returns a new {@link GetPageStoriesRequest}.
*/
public newGetPageStoriesRequest(): GetPageStoriesRequest {
return new GetPageStoriesRequest(this.accessToken, this.pageId).withApiVersion(this.apiVersion);
}
/**
* Builds a new {@link GetPageRecentlySearchedHashtagsRequest}.
*
* @returns a new {@link GetPageRecentlySearchedHashtagsRequest}.
*/
public newGetPageRecentlySearchedHashtagsRequest(): GetPageRecentlySearchedHashtagsRequest {
return new GetPageRecentlySearchedHashtagsRequest(this.accessToken, this.pageId).withApiVersion(
this.apiVersion
);
}
/**
* Builds a new {@link GetContentPublishingLimitRequest}.
*
* @param fields the fields to retrieve.
*
* @returns a new {@link GetContentPublishingLimitRequest}.
*/
public newGetContentPublishingLimitRequest(
...fields: ContentPublishingLimitFields[]
): GetContentPublishingLimitRequest {
return new GetContentPublishingLimitRequest(this.accessToken, this.pageId, ...fields).withApiVersion(
this.apiVersion
);
}
/**
* Builds a new {@link GetInstagramAccountInfoRequest}.
*
* @returns a new {@link GetInstagramAccountInfoRequest}.
*/
public newGetInstagramAccountInfoRequest(): GetInstagramAccountInfoRequest {
return new GetInstagramAccountInfoRequest(this.accessToken, this.pageId).withApiVersion(this.apiVersion);
}
}