Skip to content

Commit 28c87b0

Browse files
authored
feat: add clients for ads and ads sandbox (#566)
1 parent d6ff320 commit 28c87b0

File tree

9 files changed

+115
-7
lines changed

9 files changed

+115
-7
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@ using descriptive typings for read/write/DMs rights, request parameters and resp
7272

7373
A small feature comparison with other libs:
7474

75-
| Package | API version(s) | Response typings | Media helpers | Pagination | Subdeps | Size (gzip) | Install size |
76-
| -------------- | -------------- | ---------------- | ------------- | ---------- | --------------- | -------------:| -------------:|
77-
| twitter-api-v2 | v1.1, v2, labs |||| 0 | ~23 kB | [![twitter-api-v2 install size badge](https://badgen.net/packagephobia/install/twitter-api-v2)](https://packagephobia.com/result?p=twitter-api-v2) |
78-
| twit | v1.1 |||| 51 | ~214.5 kB | [![twit install size badge](https://badgen.net/packagephobia/install/twit)](https://packagephobia.com/result?p=twit) |
79-
| twitter | v1.1 |||| 50 | ~182.1 kB | [![twitter install size badge](https://badgen.net/packagephobia/install/twitter)](https://packagephobia.com/result?p=twitter) |
80-
| twitter-lite | v1.1, v2 |||| 4 | ~5.3 kB | [![twitter-lite install size badge](https://badgen.net/packagephobia/install/twitter-lite)](https://packagephobia.com/result?p=twitter-lite) |
81-
| twitter-v2 | v2 |||| 7 | ~4.5 kB | [![twitter-v2 install size badge](https://badgen.net/packagephobia/install/twitter-v2)](https://packagephobia.com/result?p=twitter-v2) |
75+
| Package | API version(s) | Response typings | Media helpers | Pagination | Subdeps | Size (gzip) | Install size |
76+
| -------------- |---------------------| ---------------- | ------------- | ---------- | --------------- | -------------:| -------------:|
77+
| twitter-api-v2 | v1.1, v2, labs, ads |||| 0 | ~23 kB | [![twitter-api-v2 install size badge](https://badgen.net/packagephobia/install/twitter-api-v2)](https://packagephobia.com/result?p=twitter-api-v2) |
78+
| twit | v1.1 |||| 51 | ~214.5 kB | [![twit install size badge](https://badgen.net/packagephobia/install/twit)](https://packagephobia.com/result?p=twit) |
79+
| twitter | v1.1 |||| 50 | ~182.1 kB | [![twitter install size badge](https://badgen.net/packagephobia/install/twitter)](https://packagephobia.com/result?p=twitter) |
80+
| twitter-lite | v1.1, v2 |||| 4 | ~5.3 kB | [![twitter-lite install size badge](https://badgen.net/packagephobia/install/twitter-lite)](https://packagephobia.com/result?p=twitter-lite) |
81+
| twitter-v2 | v2 |||| 7 | ~4.5 kB | [![twitter-v2 install size badge](https://badgen.net/packagephobia/install/twitter-v2)](https://packagephobia.com/result?p=twitter-v2) |
8282

8383
## Features
8484

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import TwitterApiSubClient from '../client.subclient';
2+
import { API_ADS_SANDBOX_PREFIX } from '../globals';
3+
4+
/**
5+
* Base Twitter ads sandbox client with only read rights.
6+
*/
7+
export default class TwitterAdsSandboxReadOnly extends TwitterApiSubClient {
8+
protected _prefix = API_ADS_SANDBOX_PREFIX;
9+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { API_ADS_SANDBOX_PREFIX } from '../globals';
2+
import TwitterAdsSandboxReadWrite from './client.ads-sandbox.write';
3+
4+
/**
5+
* Twitter ads sandbox client with all rights (read/write)
6+
*/
7+
export class TwitterAdsSandbox extends TwitterAdsSandboxReadWrite {
8+
protected _prefix = API_ADS_SANDBOX_PREFIX;
9+
10+
/**
11+
* Get a client with read/write rights.
12+
*/
13+
public get readWrite() {
14+
return this as TwitterAdsSandboxReadWrite;
15+
}
16+
}
17+
18+
export default TwitterAdsSandbox;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { API_ADS_SANDBOX_PREFIX } from '../globals';
2+
import TwitterAdsSandboxReadOnly from './client.ads-sandbox.read';
3+
4+
/**
5+
* Base Twitter ads sandbox client with read/write rights.
6+
*/
7+
export default class TwitterAdsSandboxReadWrite extends TwitterAdsSandboxReadOnly {
8+
protected _prefix = API_ADS_SANDBOX_PREFIX;
9+
10+
/**
11+
* Get a client with only read rights.
12+
*/
13+
public get readOnly() {
14+
return this as TwitterAdsSandboxReadOnly;
15+
}
16+
}

src/ads/client.ads.read.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import TwitterApiSubClient from '../client.subclient';
2+
import { API_ADS_PREFIX } from '../globals';
3+
4+
/**
5+
* Base Twitter ads client with only read rights.
6+
*/
7+
export default class TwitterAdsReadOnly extends TwitterApiSubClient {
8+
protected _prefix = API_ADS_PREFIX;
9+
}

src/ads/client.ads.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { API_ADS_PREFIX } from '../globals';
2+
import TwitterAdsReadWrite from './client.ads.write';
3+
import TwitterAdsSandbox from '../ads-sandbox/client.ads-sandbox';
4+
5+
/**
6+
* Twitter ads client with all rights (read/write)
7+
*/
8+
export class TwitterAds extends TwitterAdsReadWrite {
9+
protected _prefix = API_ADS_PREFIX;
10+
protected _sandbox?: TwitterAdsSandbox;
11+
12+
/**
13+
* Get a client with read/write rights.
14+
*/
15+
public get readWrite() {
16+
return this as TwitterAdsReadWrite;
17+
}
18+
19+
/**
20+
* Get Twitter Ads Sandbox API client
21+
*/
22+
public get sandbox() {
23+
if (this._sandbox) return this._sandbox;
24+
return this._sandbox = new TwitterAdsSandbox(this);
25+
}
26+
}
27+
28+
export default TwitterAds;

src/ads/client.ads.write.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { API_ADS_PREFIX } from '../globals';
2+
import TwitterAdsReadOnly from './client.ads.read';
3+
4+
/**
5+
* Base Twitter ads client with read/write rights.
6+
*/
7+
export default class TwitterAdsReadWrite extends TwitterAdsReadOnly {
8+
protected _prefix = API_ADS_PREFIX;
9+
10+
/**
11+
* Get a client with only read rights.
12+
*/
13+
public get readOnly() {
14+
return this as TwitterAdsReadOnly;
15+
}
16+
}

src/client/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import TwitterApiv1 from '../v1/client.v1';
22
import TwitterApiv2 from '../v2/client.v2';
33
import { TwitterApiError } from '../types';
44
import TwitterApiReadWrite from './readwrite';
5+
import TwitterAds from '../ads/client.ads';
56

67

78
// "Real" exported client for usage of TwitterApi.
@@ -11,6 +12,7 @@ import TwitterApiReadWrite from './readwrite';
1112
export class TwitterApi extends TwitterApiReadWrite {
1213
protected _v1?: TwitterApiv1;
1314
protected _v2?: TwitterApiv2;
15+
protected _ads?: TwitterAds;
1416

1517
/* Direct access to subclients */
1618
public get v1() {
@@ -32,6 +34,14 @@ export class TwitterApi extends TwitterApiReadWrite {
3234
return this as TwitterApiReadWrite;
3335
}
3436

37+
/**
38+
* Get Twitter Ads API client
39+
*/
40+
public get ads() {
41+
if (this._ads) return this._ads;
42+
return this._ads = new TwitterAds(this);
43+
}
44+
3545
/* Static helpers */
3646
public static getErrors(error: any) {
3747
if (typeof error !== 'object')

src/globals.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ export const API_V2_LABS_PREFIX = 'https://api.x.com/labs/2/';
33
export const API_V1_1_PREFIX = 'https://api.x.com/1.1/';
44
export const API_V1_1_UPLOAD_PREFIX = 'https://upload.x.com/1.1/';
55
export const API_V1_1_STREAM_PREFIX = 'https://stream.x.com/1.1/';
6+
export const API_ADS_PREFIX = 'https://ads-api.x.com/12/';
7+
export const API_ADS_SANDBOX_PREFIX = 'https://ads-api-sandbox.twitter.com/12/';

0 commit comments

Comments
 (0)