@adport/provider-reddit 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/dist/index.d.ts +105 -0
- package/dist/index.js +640 -0
- package/dist/index.js.map +1 -0
- package/package.json +34 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Yannick Westermann
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { AdProvider, ProviderCapabilities, StandardActions, Account, NormalizedQuery, Report, WriteOperation, WriteGuard, WritePreview, WriteResult, AnyToolDefinition, CredentialStore, ProviderModule } from '@adport/core';
|
|
2
|
+
|
|
3
|
+
interface RedditCredentials {
|
|
4
|
+
clientId: string;
|
|
5
|
+
clientSecret: string;
|
|
6
|
+
refreshToken: string;
|
|
7
|
+
/** Reddit requires an honest, uniquely identifying User-Agent on every request. */
|
|
8
|
+
userAgent: string;
|
|
9
|
+
onRefreshToken?: (refreshToken: string) => Promise<void>;
|
|
10
|
+
}
|
|
11
|
+
declare const REDDIT_API_VERSION = "v3";
|
|
12
|
+
declare const REDDIT_API_BASE = "https://ads-api.reddit.com/api/v3";
|
|
13
|
+
declare const REDDIT_TOKEN_URL = "https://www.reddit.com/api/v1/access_token";
|
|
14
|
+
interface RedditEnvelope<T> {
|
|
15
|
+
data: T;
|
|
16
|
+
pagination?: {
|
|
17
|
+
next_url?: string;
|
|
18
|
+
previous_url?: string;
|
|
19
|
+
page_index?: number;
|
|
20
|
+
total_count?: number;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/** OAuth-refreshing Reddit Ads API v3 client with exact next_url pagination. */
|
|
24
|
+
declare class RedditAdsClient {
|
|
25
|
+
private readonly credentials;
|
|
26
|
+
private readonly fetchImpl;
|
|
27
|
+
private token?;
|
|
28
|
+
private refreshToken;
|
|
29
|
+
constructor(credentials: RedditCredentials, fetchImpl?: typeof fetch);
|
|
30
|
+
get<T>(pathOrUrl: string, params?: Record<string, unknown>): Promise<RedditEnvelope<T>>;
|
|
31
|
+
post<T>(pathOrUrl: string, body: Record<string, unknown>): Promise<RedditEnvelope<T>>;
|
|
32
|
+
patch<T>(pathOrUrl: string, body: Record<string, unknown>): Promise<RedditEnvelope<T>>;
|
|
33
|
+
delete<T>(pathOrUrl: string): Promise<RedditEnvelope<T>>;
|
|
34
|
+
getPaged<T>(path: string, params?: Record<string, unknown>, limit?: number): Promise<T[]>;
|
|
35
|
+
private url;
|
|
36
|
+
private send;
|
|
37
|
+
private accessToken;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Reddit budget, bid, spend, CPC, and CPM fields are integer micros. */
|
|
41
|
+
declare const REDDIT_MICROS = 1000000;
|
|
42
|
+
interface RedditCampaign {
|
|
43
|
+
id: string;
|
|
44
|
+
ad_account_id: string;
|
|
45
|
+
name: string;
|
|
46
|
+
configured_status: string;
|
|
47
|
+
effective_status?: string;
|
|
48
|
+
objective?: string;
|
|
49
|
+
funding_instrument_id?: string;
|
|
50
|
+
goal_type?: string;
|
|
51
|
+
goal_value?: number;
|
|
52
|
+
spend_cap?: number;
|
|
53
|
+
is_campaign_budget_optimization?: boolean;
|
|
54
|
+
}
|
|
55
|
+
declare class RedditAdsProvider implements AdProvider {
|
|
56
|
+
private readonly client;
|
|
57
|
+
readonly id = "reddit";
|
|
58
|
+
constructor(client: RedditAdsClient);
|
|
59
|
+
capabilities(): ProviderCapabilities;
|
|
60
|
+
standardActions(): StandardActions;
|
|
61
|
+
listAccounts(): Promise<Account[]>;
|
|
62
|
+
report(query: NormalizedQuery): Promise<Report>;
|
|
63
|
+
rawReport(input: {
|
|
64
|
+
account_id: string;
|
|
65
|
+
breakdowns?: string[];
|
|
66
|
+
fields: string[];
|
|
67
|
+
starts_at: string;
|
|
68
|
+
ends_at: string;
|
|
69
|
+
time_zone_id?: string;
|
|
70
|
+
filter?: string;
|
|
71
|
+
page_size?: number;
|
|
72
|
+
}): Promise<{
|
|
73
|
+
metrics: Array<Record<string, unknown>>;
|
|
74
|
+
metrics_updated_at?: string;
|
|
75
|
+
}>;
|
|
76
|
+
apiRead(input: {
|
|
77
|
+
account_id: string;
|
|
78
|
+
path: string;
|
|
79
|
+
method?: 'GET' | 'POST';
|
|
80
|
+
params?: Record<string, unknown>;
|
|
81
|
+
body?: Record<string, unknown>;
|
|
82
|
+
limit?: number;
|
|
83
|
+
}): Promise<unknown>;
|
|
84
|
+
listCampaigns(accountId: string, ids?: string[]): Promise<RedditCampaign[]>;
|
|
85
|
+
previewWrite(op: WriteOperation, guard: WriteGuard): Promise<WritePreview>;
|
|
86
|
+
applyWrite(op: WriteOperation, guard: WriteGuard): Promise<WriteResult>;
|
|
87
|
+
private fetchReportPages;
|
|
88
|
+
private toReportRow;
|
|
89
|
+
private plan;
|
|
90
|
+
private planCreateCampaign;
|
|
91
|
+
private planSetStatus;
|
|
92
|
+
private planSetBudget;
|
|
93
|
+
private planApiCreate;
|
|
94
|
+
private planApiUpdate;
|
|
95
|
+
private planApiDelete;
|
|
96
|
+
private getOwnedCampaign;
|
|
97
|
+
private assertOwnedResource;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
declare function redditTools(provider: RedditAdsProvider): AnyToolDefinition[];
|
|
101
|
+
|
|
102
|
+
declare function resolveRedditCredentials(store: CredentialStore): Promise<RedditCredentials | undefined>;
|
|
103
|
+
declare function createRedditModule(store: CredentialStore): Promise<ProviderModule | undefined>;
|
|
104
|
+
|
|
105
|
+
export { REDDIT_API_BASE, REDDIT_API_VERSION, REDDIT_MICROS, REDDIT_TOKEN_URL, RedditAdsClient, RedditAdsProvider, type RedditCampaign, type RedditCredentials, type RedditEnvelope, createRedditModule, redditTools, resolveRedditCredentials };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,640 @@
|
|
|
1
|
+
// src/client.ts
|
|
2
|
+
import { AdportError } from "@adport/core";
|
|
3
|
+
var REDDIT_API_VERSION = "v3";
|
|
4
|
+
var REDDIT_API_BASE = `https://ads-api.reddit.com/api/${REDDIT_API_VERSION}`;
|
|
5
|
+
var REDDIT_TOKEN_URL = "https://www.reddit.com/api/v1/access_token";
|
|
6
|
+
var RedditAdsClient = class {
|
|
7
|
+
constructor(credentials, fetchImpl = fetch) {
|
|
8
|
+
this.credentials = credentials;
|
|
9
|
+
this.fetchImpl = fetchImpl;
|
|
10
|
+
this.refreshToken = credentials.refreshToken;
|
|
11
|
+
}
|
|
12
|
+
credentials;
|
|
13
|
+
fetchImpl;
|
|
14
|
+
token;
|
|
15
|
+
refreshToken;
|
|
16
|
+
async get(pathOrUrl, params = {}) {
|
|
17
|
+
const url = this.url(pathOrUrl, params);
|
|
18
|
+
return this.send(url, { method: "GET" });
|
|
19
|
+
}
|
|
20
|
+
async post(pathOrUrl, body) {
|
|
21
|
+
return this.send(this.url(pathOrUrl), { method: "POST", body: JSON.stringify(body) });
|
|
22
|
+
}
|
|
23
|
+
async patch(pathOrUrl, body) {
|
|
24
|
+
return this.send(this.url(pathOrUrl), { method: "PATCH", body: JSON.stringify(body) });
|
|
25
|
+
}
|
|
26
|
+
async delete(pathOrUrl) {
|
|
27
|
+
return this.send(this.url(pathOrUrl), { method: "DELETE" });
|
|
28
|
+
}
|
|
29
|
+
async getPaged(path, params = {}, limit = 1e3) {
|
|
30
|
+
const rows = [];
|
|
31
|
+
let page = await this.get(path, params);
|
|
32
|
+
while (true) {
|
|
33
|
+
rows.push(...page.data.slice(0, Math.max(0, limit - rows.length)));
|
|
34
|
+
if (rows.length >= limit || !page.pagination?.next_url) return rows;
|
|
35
|
+
page = await this.get(page.pagination.next_url);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
url(pathOrUrl, params = {}) {
|
|
39
|
+
const normalized = pathOrUrl.trim();
|
|
40
|
+
const url = normalized.startsWith("https://") ? new URL(normalized) : new URL(`${REDDIT_API_BASE}/${normalized.replace(/^\/+/, "")}`);
|
|
41
|
+
if (url.origin !== "https://ads-api.reddit.com" || !url.pathname.startsWith("/api/v3/")) {
|
|
42
|
+
throw new AdportError("INVALID_INPUT", "reddit: pagination or API URL must stay within https://ads-api.reddit.com/api/v3/");
|
|
43
|
+
}
|
|
44
|
+
for (const [key, value] of Object.entries(params)) {
|
|
45
|
+
if (value === void 0) continue;
|
|
46
|
+
url.searchParams.set(key, Array.isArray(value) ? value.join(",") : String(value));
|
|
47
|
+
}
|
|
48
|
+
return url.toString();
|
|
49
|
+
}
|
|
50
|
+
async send(url, init) {
|
|
51
|
+
const accessToken = await this.accessToken();
|
|
52
|
+
const response = await this.fetchImpl(url, {
|
|
53
|
+
method: init.method,
|
|
54
|
+
headers: {
|
|
55
|
+
authorization: `Bearer ${accessToken}`,
|
|
56
|
+
"user-agent": this.credentials.userAgent,
|
|
57
|
+
accept: "application/json",
|
|
58
|
+
...init.body ? { "content-type": "application/json" } : {}
|
|
59
|
+
},
|
|
60
|
+
body: init.body
|
|
61
|
+
});
|
|
62
|
+
const raw = await response.text();
|
|
63
|
+
const parsed = raw ? safeJson(raw) : { data: {} };
|
|
64
|
+
if (!response.ok) {
|
|
65
|
+
throw new AdportError(
|
|
66
|
+
"PROVIDER_ERROR",
|
|
67
|
+
`Reddit Ads API ${response.status} ${init.method} ${new URL(url).pathname}: ${redditErrorMessage(parsed)}`,
|
|
68
|
+
parsed
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
return parsed;
|
|
72
|
+
}
|
|
73
|
+
async accessToken() {
|
|
74
|
+
if (this.token && this.token.expiresAt > Date.now() + 6e4) return this.token.accessToken;
|
|
75
|
+
const response = await this.fetchImpl(REDDIT_TOKEN_URL, {
|
|
76
|
+
method: "POST",
|
|
77
|
+
headers: {
|
|
78
|
+
authorization: `Basic ${Buffer.from(`${this.credentials.clientId}:${this.credentials.clientSecret}`).toString("base64")}`,
|
|
79
|
+
"content-type": "application/x-www-form-urlencoded",
|
|
80
|
+
"user-agent": this.credentials.userAgent
|
|
81
|
+
},
|
|
82
|
+
body: new URLSearchParams({ grant_type: "refresh_token", refresh_token: this.refreshToken })
|
|
83
|
+
});
|
|
84
|
+
const data = await response.json();
|
|
85
|
+
if (!response.ok || !data.access_token) {
|
|
86
|
+
throw new AdportError(
|
|
87
|
+
"PROVIDER_ERROR",
|
|
88
|
+
`Reddit OAuth refresh failed${data.error ? ` (${data.error})` : ""}: ${data.message ?? "no access token returned"}`,
|
|
89
|
+
data
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
if (data.refresh_token && data.refresh_token !== this.refreshToken) {
|
|
93
|
+
this.refreshToken = data.refresh_token;
|
|
94
|
+
await this.credentials.onRefreshToken?.(data.refresh_token);
|
|
95
|
+
}
|
|
96
|
+
this.token = {
|
|
97
|
+
accessToken: data.access_token,
|
|
98
|
+
expiresAt: Date.now() + (data.expires_in ?? 3600) * 1e3
|
|
99
|
+
};
|
|
100
|
+
return data.access_token;
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
function safeJson(raw) {
|
|
104
|
+
try {
|
|
105
|
+
return JSON.parse(raw);
|
|
106
|
+
} catch {
|
|
107
|
+
return { message: raw };
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
function redditErrorMessage(value) {
|
|
111
|
+
if (!value || typeof value !== "object") return String(value);
|
|
112
|
+
const record = value;
|
|
113
|
+
return String(record.message ?? record.error ?? record.detail ?? "unknown provider error");
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// src/provider.ts
|
|
117
|
+
import {
|
|
118
|
+
AdportError as AdportError2,
|
|
119
|
+
resolveDateRange
|
|
120
|
+
} from "@adport/core";
|
|
121
|
+
var REDDIT_MICROS = 1e6;
|
|
122
|
+
var BREAKDOWN = { account: void 0, campaign: "campaign_id", ad_group: "ad_group_id", ad: "ad_id" };
|
|
123
|
+
var RedditAdsProvider = class {
|
|
124
|
+
constructor(client) {
|
|
125
|
+
this.client = client;
|
|
126
|
+
}
|
|
127
|
+
client;
|
|
128
|
+
id = "reddit";
|
|
129
|
+
capabilities() {
|
|
130
|
+
return { serverDryRun: false };
|
|
131
|
+
}
|
|
132
|
+
standardActions() {
|
|
133
|
+
return {
|
|
134
|
+
pauseCampaign: (accountId, campaignId) => ({
|
|
135
|
+
tool: "reddit_set_campaign_status",
|
|
136
|
+
input: { account_id: accountId, campaign_id: campaignId, configured_status: "PAUSED" }
|
|
137
|
+
})
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
async listAccounts() {
|
|
141
|
+
const businesses = await this.client.getPaged("me/businesses", { "page.size": 100 }, 1e3);
|
|
142
|
+
const byId = /* @__PURE__ */ new Map();
|
|
143
|
+
for (const business of businesses) {
|
|
144
|
+
const accounts = await this.client.getPaged(
|
|
145
|
+
`businesses/${encodeURIComponent(business.id)}/ad_accounts`,
|
|
146
|
+
{ "page.size": 100 },
|
|
147
|
+
1e3
|
|
148
|
+
);
|
|
149
|
+
for (const account of accounts) byId.set(account.id, account);
|
|
150
|
+
}
|
|
151
|
+
return [...byId.values()].map((account) => ({
|
|
152
|
+
provider: this.id,
|
|
153
|
+
id: account.id,
|
|
154
|
+
name: account.name ?? account.id,
|
|
155
|
+
currency: account.currency,
|
|
156
|
+
status: account.suspension_reason ? `SUSPENDED:${account.suspension_reason}` : account.admin_approval
|
|
157
|
+
}));
|
|
158
|
+
}
|
|
159
|
+
async report(query) {
|
|
160
|
+
const range = resolveDateRange(query.dateRange);
|
|
161
|
+
const accountIds = query.accountIds ?? (await this.listAccounts()).map((account) => account.id);
|
|
162
|
+
const rows = [];
|
|
163
|
+
for (const accountId of accountIds) {
|
|
164
|
+
const breakdown = BREAKDOWN[query.level];
|
|
165
|
+
const body = {
|
|
166
|
+
data: {
|
|
167
|
+
...breakdown ? { breakdowns: [breakdown] } : {},
|
|
168
|
+
fields: [
|
|
169
|
+
"spend",
|
|
170
|
+
"impressions",
|
|
171
|
+
"clicks",
|
|
172
|
+
"conversion_purchase_clicks",
|
|
173
|
+
"conversion_purchase_views",
|
|
174
|
+
"conversion_purchase_total_value"
|
|
175
|
+
],
|
|
176
|
+
starts_at: `${range.start}T00:00:00Z`,
|
|
177
|
+
ends_at: `${range.end}T23:59:59Z`,
|
|
178
|
+
time_zone_id: "UTC"
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
const metrics = await this.fetchReportPages(accountId, body, query.limit ?? 1e3);
|
|
182
|
+
for (const metric of metrics) rows.push(this.toReportRow(metric, accountId, query, breakdown));
|
|
183
|
+
}
|
|
184
|
+
return { rows };
|
|
185
|
+
}
|
|
186
|
+
async rawReport(input) {
|
|
187
|
+
const body = {
|
|
188
|
+
data: {
|
|
189
|
+
...input.breakdowns?.length ? { breakdowns: input.breakdowns } : {},
|
|
190
|
+
fields: input.fields,
|
|
191
|
+
starts_at: input.starts_at,
|
|
192
|
+
ends_at: input.ends_at,
|
|
193
|
+
time_zone_id: input.time_zone_id ?? "UTC",
|
|
194
|
+
...input.filter ? { filter: input.filter } : {}
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
const first = await this.client.post(
|
|
198
|
+
`ad_accounts/${encodeURIComponent(input.account_id)}/reports?page.size=${Math.min(input.page_size ?? 200, 1e3)}`,
|
|
199
|
+
body
|
|
200
|
+
);
|
|
201
|
+
const metrics = [...first.data.metrics ?? []];
|
|
202
|
+
let next = first.pagination?.next_url;
|
|
203
|
+
while (next && metrics.length < (input.page_size ?? 200)) {
|
|
204
|
+
const page = await this.client.get(next);
|
|
205
|
+
metrics.push(...page.data.metrics ?? []);
|
|
206
|
+
next = page.pagination?.next_url;
|
|
207
|
+
}
|
|
208
|
+
return { metrics: metrics.slice(0, input.page_size ?? 200), metrics_updated_at: first.data.metrics_updated_at };
|
|
209
|
+
}
|
|
210
|
+
async apiRead(input) {
|
|
211
|
+
const path = validateRedditPath(input.path);
|
|
212
|
+
assertReadScoped(path, input.account_id);
|
|
213
|
+
if (input.method === "POST") {
|
|
214
|
+
if (!/(?:\/reports|\/history|\/query|\/search)$/.test(path)) {
|
|
215
|
+
throw new AdportError2("INVALID_INPUT", "reddit: read-only POST is limited to documented reports, history, query, and search endpoints");
|
|
216
|
+
}
|
|
217
|
+
return this.client.post(path, input.body ?? {});
|
|
218
|
+
}
|
|
219
|
+
if (input.limit) return this.client.getPaged(path, input.params, input.limit);
|
|
220
|
+
return this.client.get(path, input.params);
|
|
221
|
+
}
|
|
222
|
+
async listCampaigns(accountId, ids) {
|
|
223
|
+
return this.client.getPaged(
|
|
224
|
+
`ad_accounts/${encodeURIComponent(accountId)}/campaigns`,
|
|
225
|
+
{ ...ids?.length ? { id: ids } : {}, "page.size": Math.min(ids?.length ?? 100, 100) },
|
|
226
|
+
ids?.length ?? 1e3
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
async previewWrite(op, guard) {
|
|
230
|
+
const plan = await this.plan(op, guard);
|
|
231
|
+
return {
|
|
232
|
+
summary: plan.summary,
|
|
233
|
+
changes: plan.changes,
|
|
234
|
+
coercions: plan.coercions,
|
|
235
|
+
budgetDeltas: plan.budgetDeltas,
|
|
236
|
+
serverValidated: false
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
async applyWrite(op, guard) {
|
|
240
|
+
const plan = await this.plan(op, guard);
|
|
241
|
+
return { applied: true, resourceIds: await plan.execute() };
|
|
242
|
+
}
|
|
243
|
+
async fetchReportPages(accountId, body, limit) {
|
|
244
|
+
const first = await this.client.post(
|
|
245
|
+
`ad_accounts/${encodeURIComponent(accountId)}/reports?page.size=${Math.min(limit, 1e3)}`,
|
|
246
|
+
body
|
|
247
|
+
);
|
|
248
|
+
const rows = [...first.data.metrics ?? []];
|
|
249
|
+
let next = first.pagination?.next_url;
|
|
250
|
+
while (next && rows.length < limit) {
|
|
251
|
+
const page = await this.client.get(next);
|
|
252
|
+
rows.push(...page.data.metrics ?? []);
|
|
253
|
+
next = page.pagination?.next_url;
|
|
254
|
+
}
|
|
255
|
+
return rows.slice(0, limit);
|
|
256
|
+
}
|
|
257
|
+
toReportRow(raw, accountId, query, breakdown) {
|
|
258
|
+
const value = (key) => Number(raw[key] ?? 0);
|
|
259
|
+
const spend = value("spend") / REDDIT_MICROS;
|
|
260
|
+
const impressions = value("impressions");
|
|
261
|
+
const clicks = value("clicks");
|
|
262
|
+
const conversions = value("conversion_purchase_clicks") + value("conversion_purchase_views");
|
|
263
|
+
const conversionValue = value("conversion_purchase_total_value") / 100;
|
|
264
|
+
const all = {
|
|
265
|
+
spend: round2(spend),
|
|
266
|
+
impressions,
|
|
267
|
+
clicks,
|
|
268
|
+
conversions,
|
|
269
|
+
conversion_value: round2(conversionValue),
|
|
270
|
+
ctr: impressions ? round2(clicks / impressions * 100) : 0,
|
|
271
|
+
cpc: clicks ? round2(spend / clicks) : 0,
|
|
272
|
+
cpm: impressions ? round2(spend / impressions * 1e3) : 0,
|
|
273
|
+
cpa: conversions ? round2(spend / conversions) : 0,
|
|
274
|
+
roas: spend ? round2(conversionValue / spend) : 0
|
|
275
|
+
};
|
|
276
|
+
const entityId = breakdown ? String(raw[breakdown] ?? "") : accountId;
|
|
277
|
+
return {
|
|
278
|
+
provider: this.id,
|
|
279
|
+
accountId,
|
|
280
|
+
entity: { level: query.level, id: entityId, name: entityId },
|
|
281
|
+
metrics: Object.fromEntries(query.metrics.map((metric) => [metric, all[metric]]))
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
async plan(op, guard) {
|
|
285
|
+
const payload = op.payload;
|
|
286
|
+
switch (op.tool) {
|
|
287
|
+
case "reddit_create_campaign":
|
|
288
|
+
return this.planCreateCampaign(op.accountId, payload, guard);
|
|
289
|
+
case "reddit_set_campaign_status":
|
|
290
|
+
return this.planSetStatus(op.accountId, payload);
|
|
291
|
+
case "reddit_set_budget":
|
|
292
|
+
return this.planSetBudget(op.accountId, payload);
|
|
293
|
+
case "reddit_api_create":
|
|
294
|
+
return this.planApiCreate(op.accountId, payload, guard);
|
|
295
|
+
case "reddit_api_update":
|
|
296
|
+
return this.planApiUpdate(op.accountId, payload);
|
|
297
|
+
case "reddit_api_delete":
|
|
298
|
+
return this.planApiDelete(op.accountId, payload);
|
|
299
|
+
default:
|
|
300
|
+
throw new AdportError2("PROVIDER_ERROR", `reddit: unsupported write tool ${op.tool}`);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
async planCreateCampaign(accountId, payload, guard) {
|
|
304
|
+
if (payload.budget_micros && !payload.conversion_pixel_id) {
|
|
305
|
+
throw new AdportError2("INVALID_INPUT", "reddit: conversion_pixel_id is required for CBO campaigns (effective July 13, 2026)");
|
|
306
|
+
}
|
|
307
|
+
const requested = payload.configured_status ?? "PAUSED";
|
|
308
|
+
const status2 = guard.forcePausedCreation ? "PAUSED" : requested;
|
|
309
|
+
const coercions = requested !== status2 ? ["configured_status coerced to PAUSED by policy (paused_creation)"] : [];
|
|
310
|
+
const data = {
|
|
311
|
+
name: payload.name,
|
|
312
|
+
configured_status: status2,
|
|
313
|
+
objective: payload.objective,
|
|
314
|
+
funding_instrument_id: payload.funding_instrument_id,
|
|
315
|
+
is_campaign_budget_optimization: payload.budget_micros !== void 0,
|
|
316
|
+
...payload.budget_micros ? {
|
|
317
|
+
goal_type: payload.budget_type ?? "DAILY_SPEND",
|
|
318
|
+
goal_value: payload.budget_micros,
|
|
319
|
+
conversion_pixel_id: payload.conversion_pixel_id
|
|
320
|
+
} : {},
|
|
321
|
+
...payload.spend_cap_micros ? { spend_cap: payload.spend_cap_micros } : {}
|
|
322
|
+
};
|
|
323
|
+
const budgetDeltas = [];
|
|
324
|
+
if (payload.budget_micros) budgetDeltas.push({ target: `new campaign "${payload.name}" budget`, toMicros: payload.budget_micros });
|
|
325
|
+
if (payload.spend_cap_micros) budgetDeltas.push({ target: `new campaign "${payload.name}" spend cap`, toMicros: payload.spend_cap_micros });
|
|
326
|
+
return {
|
|
327
|
+
summary: `Create Reddit campaign "${payload.name}" (${payload.objective}, ${status2.toLowerCase()})`,
|
|
328
|
+
changes: [`+ POST /ad_accounts/${accountId}/campaigns ${JSON.stringify({ data })}`],
|
|
329
|
+
coercions,
|
|
330
|
+
budgetDeltas,
|
|
331
|
+
execute: async () => extractIds(await this.client.post(`ad_accounts/${encodeURIComponent(accountId)}/campaigns`, { data }))
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
async planSetStatus(accountId, payload) {
|
|
335
|
+
const campaign = await this.getOwnedCampaign(accountId, payload.campaign_id);
|
|
336
|
+
return {
|
|
337
|
+
summary: `Set Reddit campaign "${campaign.name}" \u2192 ${payload.configured_status.toLowerCase()}`,
|
|
338
|
+
changes: [`~ campaign ${campaign.id} configured_status ${campaign.configured_status} \u2192 ${payload.configured_status}`],
|
|
339
|
+
coercions: [],
|
|
340
|
+
budgetDeltas: [],
|
|
341
|
+
execute: async () => extractIds(await this.client.patch(`campaigns/${encodeURIComponent(campaign.id)}`, {
|
|
342
|
+
data: { configured_status: payload.configured_status }
|
|
343
|
+
}))
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
async planSetBudget(accountId, payload) {
|
|
347
|
+
const campaign = await this.getOwnedCampaign(accountId, payload.campaign_id);
|
|
348
|
+
if (!campaign.is_campaign_budget_optimization) {
|
|
349
|
+
throw new AdportError2("INVALID_INPUT", `reddit: campaign "${campaign.name}" is not a CBO campaign; budget its ad groups instead`);
|
|
350
|
+
}
|
|
351
|
+
return {
|
|
352
|
+
summary: `Change Reddit campaign "${campaign.name}" budget ${campaign.goal_value ?? "?"} \u2192 ${payload.budget_micros} micros`,
|
|
353
|
+
changes: [`~ campaign ${campaign.id} ${campaign.goal_type ?? "goal"} ${campaign.goal_value ?? "?"} \u2192 ${payload.budget_micros}`],
|
|
354
|
+
coercions: [],
|
|
355
|
+
budgetDeltas: [{ target: `campaign "${campaign.name}" budget`, fromMicros: campaign.goal_value, toMicros: payload.budget_micros }],
|
|
356
|
+
execute: async () => extractIds(await this.client.patch(`campaigns/${encodeURIComponent(campaign.id)}`, {
|
|
357
|
+
data: { goal_value: payload.budget_micros, ...payload.budget_type ? { goal_type: payload.budget_type } : {} }
|
|
358
|
+
}))
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
async planApiCreate(accountId, payload, guard) {
|
|
362
|
+
const path = validateRedditPath(payload.path);
|
|
363
|
+
if (!path.startsWith(`ad_accounts/${accountId}/`)) {
|
|
364
|
+
throw new AdportError2("INVALID_INPUT", "reddit: create path must be under the selected ad account");
|
|
365
|
+
}
|
|
366
|
+
const body = structuredClone(payload.body);
|
|
367
|
+
const coercions = [];
|
|
368
|
+
if (guard.forcePausedCreation) {
|
|
369
|
+
coerceCreatedStatuses(body, coercions, /\/(?:campaigns|ad_groups|ads)$/.test(path));
|
|
370
|
+
}
|
|
371
|
+
return {
|
|
372
|
+
summary: `Create Reddit resource via /${path}`,
|
|
373
|
+
changes: [`+ POST /${path} ${JSON.stringify(body)}`],
|
|
374
|
+
coercions,
|
|
375
|
+
budgetDeltas: collectMicros(body),
|
|
376
|
+
execute: async () => extractIds(await this.client.post(path, body))
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
async planApiUpdate(accountId, payload) {
|
|
380
|
+
const path = validateRedditPath(payload.path);
|
|
381
|
+
await this.assertOwnedResource(path, accountId);
|
|
382
|
+
if (containsMoney(payload.body)) {
|
|
383
|
+
throw new AdportError2("INVALID_INPUT", "reddit: budget, bid, goal, and spend-cap updates require a typed budget tool for policy checks");
|
|
384
|
+
}
|
|
385
|
+
return {
|
|
386
|
+
summary: `Update Reddit resource via /${path}`,
|
|
387
|
+
changes: [`~ PATCH /${path} ${JSON.stringify(payload.body)}`],
|
|
388
|
+
coercions: [],
|
|
389
|
+
budgetDeltas: [],
|
|
390
|
+
execute: async () => extractIds(await this.client.patch(path, payload.body))
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
async planApiDelete(accountId, payload) {
|
|
394
|
+
const path = validateRedditPath(payload.path);
|
|
395
|
+
await this.assertOwnedResource(path, accountId);
|
|
396
|
+
return {
|
|
397
|
+
summary: `Permanently delete Reddit resource via /${path}`,
|
|
398
|
+
changes: [`- DELETE /${path}`],
|
|
399
|
+
coercions: [],
|
|
400
|
+
budgetDeltas: [],
|
|
401
|
+
execute: async () => extractIds(await this.client.delete(path))
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
async getOwnedCampaign(accountId, campaignId) {
|
|
405
|
+
const campaign = (await this.client.get(`campaigns/${encodeURIComponent(campaignId)}`)).data;
|
|
406
|
+
if (campaign.ad_account_id !== accountId) {
|
|
407
|
+
throw new AdportError2("INVALID_INPUT", `reddit: campaign ${campaignId} does not belong to selected account ${accountId}`);
|
|
408
|
+
}
|
|
409
|
+
return campaign;
|
|
410
|
+
}
|
|
411
|
+
async assertOwnedResource(path, accountId) {
|
|
412
|
+
const resource = (await this.client.get(path)).data;
|
|
413
|
+
if (String(resource.ad_account_id ?? "") !== accountId) {
|
|
414
|
+
throw new AdportError2("INVALID_INPUT", `reddit: /${path} does not prove ownership by selected account ${accountId}`);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
};
|
|
418
|
+
function validateRedditPath(path) {
|
|
419
|
+
const normalized = path.replace(/^\/+|\/+$/g, "").trim();
|
|
420
|
+
if (!/^[A-Za-z0-9_.~/-]{3,220}$/.test(normalized) || normalized.includes("..") || normalized.includes("//")) {
|
|
421
|
+
throw new AdportError2("INVALID_INPUT", `reddit: invalid Ads API v3 path "${path}"`);
|
|
422
|
+
}
|
|
423
|
+
return normalized;
|
|
424
|
+
}
|
|
425
|
+
function assertReadScoped(path, accountId) {
|
|
426
|
+
if (path === `ad_accounts/${accountId}` || path.startsWith(`ad_accounts/${accountId}/`)) return;
|
|
427
|
+
if (/^(campaigns|ad_groups|ads|structured_posts)\/[^/]+$/.test(path)) return;
|
|
428
|
+
throw new AdportError2("INVALID_INPUT", "reddit: read path must be selected-account scoped or a single campaign/ad-group/ad/post resource");
|
|
429
|
+
}
|
|
430
|
+
function isRecord(value) {
|
|
431
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
432
|
+
}
|
|
433
|
+
function coerceCreatedStatuses(value, coercions, statusBearingPath) {
|
|
434
|
+
const data = isRecord(value.data) ? value.data : value;
|
|
435
|
+
const visit = (item, path = "body") => {
|
|
436
|
+
if (Array.isArray(item)) return item.forEach((child, index) => visit(child, `${path}[${index}]`));
|
|
437
|
+
if (!isRecord(item)) return;
|
|
438
|
+
for (const [key, child] of Object.entries(item)) {
|
|
439
|
+
if (key === "configured_status" && child !== "PAUSED") {
|
|
440
|
+
item[key] = "PAUSED";
|
|
441
|
+
coercions.push(`${path}.${key} coerced to PAUSED by policy (paused_creation)`);
|
|
442
|
+
} else visit(child, `${path}.${key}`);
|
|
443
|
+
}
|
|
444
|
+
};
|
|
445
|
+
visit(value);
|
|
446
|
+
if (statusBearingPath && data.configured_status === void 0) {
|
|
447
|
+
data.configured_status = "PAUSED";
|
|
448
|
+
coercions.push("body.data.configured_status set to PAUSED by policy (paused_creation)");
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
var MONEY_KEY = /(?:goal_value|spend_cap|bid_value|budget(?:_micros)?|daily_budget|lifetime_budget)$/i;
|
|
452
|
+
function containsMoney(value) {
|
|
453
|
+
if (Array.isArray(value)) return value.some(containsMoney);
|
|
454
|
+
if (!isRecord(value)) return false;
|
|
455
|
+
return Object.entries(value).some(([key, child]) => MONEY_KEY.test(key) || containsMoney(child));
|
|
456
|
+
}
|
|
457
|
+
function collectMicros(value, path = "body") {
|
|
458
|
+
if (Array.isArray(value)) return value.flatMap((child, index) => collectMicros(child, `${path}[${index}]`));
|
|
459
|
+
if (!isRecord(value)) return [];
|
|
460
|
+
const deltas = [];
|
|
461
|
+
for (const [key, child] of Object.entries(value)) {
|
|
462
|
+
if (MONEY_KEY.test(key) && Number.isInteger(child) && Number(child) > 0) {
|
|
463
|
+
deltas.push({ target: `${path}.${key}`, toMicros: Number(child) });
|
|
464
|
+
}
|
|
465
|
+
deltas.push(...collectMicros(child, `${path}.${key}`));
|
|
466
|
+
}
|
|
467
|
+
return deltas;
|
|
468
|
+
}
|
|
469
|
+
function extractIds(envelope) {
|
|
470
|
+
const ids = [];
|
|
471
|
+
const visit = (value) => {
|
|
472
|
+
if (Array.isArray(value)) return value.forEach(visit);
|
|
473
|
+
if (!isRecord(value)) return;
|
|
474
|
+
if (typeof value.id === "string" || typeof value.id === "number") ids.push(String(value.id));
|
|
475
|
+
for (const child of Object.values(value)) visit(child);
|
|
476
|
+
};
|
|
477
|
+
visit(envelope.data);
|
|
478
|
+
return [...new Set(ids)];
|
|
479
|
+
}
|
|
480
|
+
function round2(value) {
|
|
481
|
+
return Math.round(value * 100) / 100;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
// src/tools.ts
|
|
485
|
+
import { defineTool, guardedWriteTool } from "@adport/core";
|
|
486
|
+
import { z } from "zod";
|
|
487
|
+
var status = z.enum(["ACTIVE", "PAUSED"]);
|
|
488
|
+
function redditTools(provider) {
|
|
489
|
+
return [
|
|
490
|
+
defineTool({
|
|
491
|
+
name: "reddit_api_read",
|
|
492
|
+
namespace: "reddit",
|
|
493
|
+
description: "Call a documented Reddit Ads API v3 GET or read-only reports/history/query/search POST endpoint, scoped to the selected ad account.",
|
|
494
|
+
input: z.object({
|
|
495
|
+
account_id: z.string(),
|
|
496
|
+
path: z.string().min(3).describe("Relative v3 path, e.g. ad_accounts/{id}/ad_groups or campaigns/{campaign_id}"),
|
|
497
|
+
method: z.enum(["GET", "POST"]).default("GET"),
|
|
498
|
+
params: z.record(z.string(), z.unknown()).optional(),
|
|
499
|
+
body: z.record(z.string(), z.unknown()).optional(),
|
|
500
|
+
limit: z.number().int().positive().max(5e3).optional().describe("Follow list pagination for GET responses")
|
|
501
|
+
}),
|
|
502
|
+
annotations: { readOnly: true },
|
|
503
|
+
handler: (input) => provider.apiRead(input)
|
|
504
|
+
}),
|
|
505
|
+
defineTool({
|
|
506
|
+
name: "reddit_campaigns",
|
|
507
|
+
namespace: "reddit",
|
|
508
|
+
description: "List Reddit campaigns and their native configured/effective status, CBO budget, objective, and funding instrument.",
|
|
509
|
+
input: z.object({ account_id: z.string(), campaign_ids: z.array(z.string()).optional() }),
|
|
510
|
+
annotations: { readOnly: true },
|
|
511
|
+
async handler(input) {
|
|
512
|
+
return { campaigns: await provider.listCampaigns(input.account_id, input.campaign_ids) };
|
|
513
|
+
}
|
|
514
|
+
}),
|
|
515
|
+
defineTool({
|
|
516
|
+
name: "reddit_report",
|
|
517
|
+
namespace: "reddit",
|
|
518
|
+
description: "Query Reddit Ads API v3 reporting directly. Spend, CPC, CPM, and applicable conversion fields are returned by Reddit in micros.",
|
|
519
|
+
input: z.object({
|
|
520
|
+
account_id: z.string(),
|
|
521
|
+
breakdowns: z.array(z.string()).max(4).optional(),
|
|
522
|
+
fields: z.array(z.string()).min(1).default(["spend", "impressions", "clicks"]),
|
|
523
|
+
starts_at: z.string().datetime({ offset: true }),
|
|
524
|
+
ends_at: z.string().datetime({ offset: true }),
|
|
525
|
+
time_zone_id: z.string().default("UTC"),
|
|
526
|
+
filter: z.string().optional(),
|
|
527
|
+
page_size: z.number().int().positive().max(1e3).default(200)
|
|
528
|
+
}),
|
|
529
|
+
annotations: { readOnly: true },
|
|
530
|
+
handler: (input) => provider.rawReport(input)
|
|
531
|
+
}),
|
|
532
|
+
guardedWriteTool({
|
|
533
|
+
name: "reddit_create_campaign",
|
|
534
|
+
namespace: "reddit",
|
|
535
|
+
description: "Create a Reddit campaign through API v3. Campaigns are forced PAUSED. CBO budgets and spend caps are integer micros.",
|
|
536
|
+
provider: "reddit",
|
|
537
|
+
kind: "create",
|
|
538
|
+
payload: z.object({
|
|
539
|
+
name: z.string().min(1),
|
|
540
|
+
objective: z.string().min(1).default("TRAFFIC").describe("Current Reddit objective enum; Reddit has announced an enum migration for Sep 30, 2026"),
|
|
541
|
+
funding_instrument_id: z.string().min(1),
|
|
542
|
+
configured_status: status.optional(),
|
|
543
|
+
budget_micros: z.number().int().positive().optional().describe("Sets CBO goal_value; requires conversion_pixel_id"),
|
|
544
|
+
budget_type: z.enum(["DAILY_SPEND", "LIFETIME_SPEND"]).default("DAILY_SPEND"),
|
|
545
|
+
conversion_pixel_id: z.string().optional(),
|
|
546
|
+
spend_cap_micros: z.number().int().positive().optional()
|
|
547
|
+
})
|
|
548
|
+
}),
|
|
549
|
+
guardedWriteTool({
|
|
550
|
+
name: "reddit_set_campaign_status",
|
|
551
|
+
namespace: "reddit",
|
|
552
|
+
description: "Activate or pause a Reddit campaign after verifying ad-account ownership.",
|
|
553
|
+
provider: "reddit",
|
|
554
|
+
kind: "update",
|
|
555
|
+
payload: z.object({ campaign_id: z.string(), configured_status: status })
|
|
556
|
+
}),
|
|
557
|
+
guardedWriteTool({
|
|
558
|
+
name: "reddit_set_budget",
|
|
559
|
+
namespace: "reddit",
|
|
560
|
+
description: "Change a Reddit CBO campaign goal_value in integer micros, with current-value lookup and policy checks.",
|
|
561
|
+
provider: "reddit",
|
|
562
|
+
kind: "update",
|
|
563
|
+
payload: z.object({
|
|
564
|
+
campaign_id: z.string(),
|
|
565
|
+
budget_micros: z.number().int().positive(),
|
|
566
|
+
budget_type: z.enum(["DAILY_SPEND", "LIFETIME_SPEND"]).optional()
|
|
567
|
+
})
|
|
568
|
+
}),
|
|
569
|
+
guardedWriteTool({
|
|
570
|
+
name: "reddit_api_create",
|
|
571
|
+
namespace: "reddit",
|
|
572
|
+
description: "Create via an account-scoped Reddit Ads API v3 endpoint. Campaign status is forced PAUSED and native micros fields are policy-checked.",
|
|
573
|
+
provider: "reddit",
|
|
574
|
+
kind: "create",
|
|
575
|
+
payload: z.object({ path: z.string().min(3), body: z.record(z.string(), z.unknown()) })
|
|
576
|
+
}),
|
|
577
|
+
guardedWriteTool({
|
|
578
|
+
name: "reddit_api_update",
|
|
579
|
+
namespace: "reddit",
|
|
580
|
+
description: "PATCH non-budget fields on a Reddit Ads API v3 resource after verifying ad-account ownership.",
|
|
581
|
+
provider: "reddit",
|
|
582
|
+
kind: "update",
|
|
583
|
+
payload: z.object({ path: z.string().min(3), body: z.record(z.string(), z.unknown()) })
|
|
584
|
+
}),
|
|
585
|
+
guardedWriteTool({
|
|
586
|
+
name: "reddit_api_delete",
|
|
587
|
+
namespace: "reddit",
|
|
588
|
+
description: "Permanently DELETE a Reddit Ads API v3 resource after verifying ad-account ownership.",
|
|
589
|
+
provider: "reddit",
|
|
590
|
+
kind: "remove",
|
|
591
|
+
destructive: true,
|
|
592
|
+
payload: z.object({ path: z.string().min(3) })
|
|
593
|
+
})
|
|
594
|
+
];
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
// src/index.ts
|
|
598
|
+
async function resolveRedditCredentials(store) {
|
|
599
|
+
const record = await store.get("reddit");
|
|
600
|
+
const data = record?.data;
|
|
601
|
+
if (data?.client_id && data.client_secret && data.refresh_token && data.user_agent) {
|
|
602
|
+
const source = record?.source ?? "byo";
|
|
603
|
+
return {
|
|
604
|
+
clientId: data.client_id,
|
|
605
|
+
clientSecret: data.client_secret,
|
|
606
|
+
refreshToken: data.refresh_token,
|
|
607
|
+
userAgent: data.user_agent,
|
|
608
|
+
onRefreshToken: async (refreshToken) => {
|
|
609
|
+
await store.set({ provider: "reddit", source, data: { ...data, refresh_token: refreshToken } });
|
|
610
|
+
}
|
|
611
|
+
};
|
|
612
|
+
}
|
|
613
|
+
if (process.env.REDDIT_ADS_CLIENT_ID && process.env.REDDIT_ADS_CLIENT_SECRET && process.env.REDDIT_ADS_REFRESH_TOKEN && process.env.REDDIT_ADS_USER_AGENT) {
|
|
614
|
+
return {
|
|
615
|
+
clientId: process.env.REDDIT_ADS_CLIENT_ID,
|
|
616
|
+
clientSecret: process.env.REDDIT_ADS_CLIENT_SECRET,
|
|
617
|
+
refreshToken: process.env.REDDIT_ADS_REFRESH_TOKEN,
|
|
618
|
+
userAgent: process.env.REDDIT_ADS_USER_AGENT
|
|
619
|
+
};
|
|
620
|
+
}
|
|
621
|
+
return void 0;
|
|
622
|
+
}
|
|
623
|
+
async function createRedditModule(store) {
|
|
624
|
+
const credentials = await resolveRedditCredentials(store);
|
|
625
|
+
if (!credentials) return void 0;
|
|
626
|
+
const provider = new RedditAdsProvider(new RedditAdsClient(credentials));
|
|
627
|
+
return { provider, tools: redditTools(provider) };
|
|
628
|
+
}
|
|
629
|
+
export {
|
|
630
|
+
REDDIT_API_BASE,
|
|
631
|
+
REDDIT_API_VERSION,
|
|
632
|
+
REDDIT_MICROS,
|
|
633
|
+
REDDIT_TOKEN_URL,
|
|
634
|
+
RedditAdsClient,
|
|
635
|
+
RedditAdsProvider,
|
|
636
|
+
createRedditModule,
|
|
637
|
+
redditTools,
|
|
638
|
+
resolveRedditCredentials
|
|
639
|
+
};
|
|
640
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/client.ts","../src/provider.ts","../src/tools.ts","../src/index.ts"],"sourcesContent":["import { AdportError } from '@adport/core';\n\nexport interface RedditCredentials {\n clientId: string;\n clientSecret: string;\n refreshToken: string;\n /** Reddit requires an honest, uniquely identifying User-Agent on every request. */\n userAgent: string;\n onRefreshToken?: (refreshToken: string) => Promise<void>;\n}\n\nexport const REDDIT_API_VERSION = 'v3';\nexport const REDDIT_API_BASE = `https://ads-api.reddit.com/api/${REDDIT_API_VERSION}`;\nexport const REDDIT_TOKEN_URL = 'https://www.reddit.com/api/v1/access_token';\n\ninterface TokenState {\n accessToken: string;\n expiresAt: number;\n}\n\nexport interface RedditEnvelope<T> {\n data: T;\n pagination?: { next_url?: string; previous_url?: string; page_index?: number; total_count?: number };\n}\n\n/** OAuth-refreshing Reddit Ads API v3 client with exact next_url pagination. */\nexport class RedditAdsClient {\n private token?: TokenState;\n private refreshToken: string;\n\n constructor(\n private readonly credentials: RedditCredentials,\n private readonly fetchImpl: typeof fetch = fetch,\n ) {\n this.refreshToken = credentials.refreshToken;\n }\n\n async get<T>(pathOrUrl: string, params: Record<string, unknown> = {}): Promise<RedditEnvelope<T>> {\n const url = this.url(pathOrUrl, params);\n return this.send<T>(url, { method: 'GET' });\n }\n\n async post<T>(pathOrUrl: string, body: Record<string, unknown>): Promise<RedditEnvelope<T>> {\n return this.send<T>(this.url(pathOrUrl), { method: 'POST', body: JSON.stringify(body) });\n }\n\n async patch<T>(pathOrUrl: string, body: Record<string, unknown>): Promise<RedditEnvelope<T>> {\n return this.send<T>(this.url(pathOrUrl), { method: 'PATCH', body: JSON.stringify(body) });\n }\n\n async delete<T>(pathOrUrl: string): Promise<RedditEnvelope<T>> {\n return this.send<T>(this.url(pathOrUrl), { method: 'DELETE' });\n }\n\n async getPaged<T>(path: string, params: Record<string, unknown> = {}, limit = 1000): Promise<T[]> {\n const rows: T[] = [];\n let page = await this.get<T[]>(path, params);\n while (true) {\n rows.push(...page.data.slice(0, Math.max(0, limit - rows.length)));\n if (rows.length >= limit || !page.pagination?.next_url) return rows;\n page = await this.get<T[]>(page.pagination.next_url);\n }\n }\n\n private url(pathOrUrl: string, params: Record<string, unknown> = {}): string {\n const normalized = pathOrUrl.trim();\n const url = normalized.startsWith('https://')\n ? new URL(normalized)\n : new URL(`${REDDIT_API_BASE}/${normalized.replace(/^\\/+/, '')}`);\n if (url.origin !== 'https://ads-api.reddit.com' || !url.pathname.startsWith('/api/v3/')) {\n throw new AdportError('INVALID_INPUT', 'reddit: pagination or API URL must stay within https://ads-api.reddit.com/api/v3/');\n }\n for (const [key, value] of Object.entries(params)) {\n if (value === undefined) continue;\n url.searchParams.set(key, Array.isArray(value) ? value.join(',') : String(value));\n }\n return url.toString();\n }\n\n private async send<T>(url: string, init: { method: string; body?: string }): Promise<RedditEnvelope<T>> {\n const accessToken = await this.accessToken();\n const response = await this.fetchImpl(url, {\n method: init.method,\n headers: {\n authorization: `Bearer ${accessToken}`,\n 'user-agent': this.credentials.userAgent,\n accept: 'application/json',\n ...(init.body ? { 'content-type': 'application/json' } : {}),\n },\n body: init.body,\n });\n const raw = await response.text();\n const parsed = raw ? safeJson(raw) : { data: {} };\n if (!response.ok) {\n throw new AdportError(\n 'PROVIDER_ERROR',\n `Reddit Ads API ${response.status} ${init.method} ${new URL(url).pathname}: ${redditErrorMessage(parsed)}`,\n parsed,\n );\n }\n return parsed as RedditEnvelope<T>;\n }\n\n private async accessToken(): Promise<string> {\n if (this.token && this.token.expiresAt > Date.now() + 60_000) return this.token.accessToken;\n const response = await this.fetchImpl(REDDIT_TOKEN_URL, {\n method: 'POST',\n headers: {\n authorization: `Basic ${Buffer.from(`${this.credentials.clientId}:${this.credentials.clientSecret}`).toString('base64')}`,\n 'content-type': 'application/x-www-form-urlencoded',\n 'user-agent': this.credentials.userAgent,\n },\n body: new URLSearchParams({ grant_type: 'refresh_token', refresh_token: this.refreshToken }),\n });\n const data = (await response.json()) as {\n access_token?: string;\n expires_in?: number;\n refresh_token?: string;\n error?: string;\n message?: string;\n };\n if (!response.ok || !data.access_token) {\n throw new AdportError(\n 'PROVIDER_ERROR',\n `Reddit OAuth refresh failed${data.error ? ` (${data.error})` : ''}: ${data.message ?? 'no access token returned'}`,\n data,\n );\n }\n if (data.refresh_token && data.refresh_token !== this.refreshToken) {\n this.refreshToken = data.refresh_token;\n await this.credentials.onRefreshToken?.(data.refresh_token);\n }\n this.token = {\n accessToken: data.access_token,\n expiresAt: Date.now() + (data.expires_in ?? 3600) * 1000,\n };\n return data.access_token;\n }\n}\n\nfunction safeJson(raw: string): unknown {\n try {\n return JSON.parse(raw);\n } catch {\n return { message: raw };\n }\n}\n\nfunction redditErrorMessage(value: unknown): string {\n if (!value || typeof value !== 'object') return String(value);\n const record = value as Record<string, unknown>;\n return String(record.message ?? record.error ?? record.detail ?? 'unknown provider error');\n}\n","import {\n AdportError,\n resolveDateRange,\n type Account,\n type AdProvider,\n type MetricName,\n type NormalizedQuery,\n type ProviderCapabilities,\n type Report,\n type ReportRow,\n type StandardActions,\n type WriteGuard,\n type WriteOperation,\n type WritePreview,\n type WriteResult,\n} from '@adport/core';\nimport { RedditAdsClient, type RedditEnvelope } from './client.js';\n\n/** Reddit budget, bid, spend, CPC, and CPM fields are integer micros. */\nexport const REDDIT_MICROS = 1_000_000;\n\ninterface RedditBusiness { id: string; name?: string }\ninterface RedditAdAccount {\n id: string;\n name?: string;\n currency?: string;\n admin_approval?: string;\n suspension_reason?: string;\n}\nexport interface RedditCampaign {\n id: string;\n ad_account_id: string;\n name: string;\n configured_status: string;\n effective_status?: string;\n objective?: string;\n funding_instrument_id?: string;\n goal_type?: string;\n goal_value?: number;\n spend_cap?: number;\n is_campaign_budget_optimization?: boolean;\n}\n\ninterface WritePlan {\n summary: string;\n changes: string[];\n coercions: string[];\n budgetDeltas: WritePreview['budgetDeltas'];\n execute: () => Promise<string[]>;\n}\n\nconst BREAKDOWN = { account: undefined, campaign: 'campaign_id', ad_group: 'ad_group_id', ad: 'ad_id' } as const;\n\nexport class RedditAdsProvider implements AdProvider {\n readonly id = 'reddit';\n\n constructor(private readonly client: RedditAdsClient) {}\n\n capabilities(): ProviderCapabilities {\n return { serverDryRun: false };\n }\n\n standardActions(): StandardActions {\n return {\n pauseCampaign: (accountId, campaignId) => ({\n tool: 'reddit_set_campaign_status',\n input: { account_id: accountId, campaign_id: campaignId, configured_status: 'PAUSED' },\n }),\n };\n }\n\n async listAccounts(): Promise<Account[]> {\n const businesses = await this.client.getPaged<RedditBusiness>('me/businesses', { 'page.size': 100 }, 1000);\n const byId = new Map<string, RedditAdAccount>();\n for (const business of businesses) {\n const accounts = await this.client.getPaged<RedditAdAccount>(\n `businesses/${encodeURIComponent(business.id)}/ad_accounts`,\n { 'page.size': 100 },\n 1000,\n );\n for (const account of accounts) byId.set(account.id, account);\n }\n return [...byId.values()].map((account) => ({\n provider: this.id,\n id: account.id,\n name: account.name ?? account.id,\n currency: account.currency,\n status: account.suspension_reason ? `SUSPENDED:${account.suspension_reason}` : account.admin_approval,\n }));\n }\n\n async report(query: NormalizedQuery): Promise<Report> {\n const range = resolveDateRange(query.dateRange);\n const accountIds = query.accountIds ?? (await this.listAccounts()).map((account) => account.id);\n const rows: ReportRow[] = [];\n for (const accountId of accountIds) {\n const breakdown = BREAKDOWN[query.level];\n const body = {\n data: {\n ...(breakdown ? { breakdowns: [breakdown] } : {}),\n fields: [\n 'spend',\n 'impressions',\n 'clicks',\n 'conversion_purchase_clicks',\n 'conversion_purchase_views',\n 'conversion_purchase_total_value',\n ],\n starts_at: `${range.start}T00:00:00Z`,\n ends_at: `${range.end}T23:59:59Z`,\n time_zone_id: 'UTC',\n },\n };\n const metrics = await this.fetchReportPages(accountId, body, query.limit ?? 1000);\n for (const metric of metrics) rows.push(this.toReportRow(metric, accountId, query, breakdown));\n }\n return { rows };\n }\n\n async rawReport(input: {\n account_id: string;\n breakdowns?: string[];\n fields: string[];\n starts_at: string;\n ends_at: string;\n time_zone_id?: string;\n filter?: string;\n page_size?: number;\n }): Promise<{ metrics: Array<Record<string, unknown>>; metrics_updated_at?: string }> {\n const body = {\n data: {\n ...(input.breakdowns?.length ? { breakdowns: input.breakdowns } : {}),\n fields: input.fields,\n starts_at: input.starts_at,\n ends_at: input.ends_at,\n time_zone_id: input.time_zone_id ?? 'UTC',\n ...(input.filter ? { filter: input.filter } : {}),\n },\n };\n const first = await this.client.post<{ metrics?: Array<Record<string, unknown>>; metrics_updated_at?: string }>(\n `ad_accounts/${encodeURIComponent(input.account_id)}/reports?page.size=${Math.min(input.page_size ?? 200, 1000)}`,\n body,\n );\n const metrics = [...(first.data.metrics ?? [])];\n let next = first.pagination?.next_url;\n while (next && metrics.length < (input.page_size ?? 200)) {\n const page = await this.client.get<{ metrics?: Array<Record<string, unknown>> }>(next);\n metrics.push(...(page.data.metrics ?? []));\n next = page.pagination?.next_url;\n }\n return { metrics: metrics.slice(0, input.page_size ?? 200), metrics_updated_at: first.data.metrics_updated_at };\n }\n\n async apiRead(input: {\n account_id: string;\n path: string;\n method?: 'GET' | 'POST';\n params?: Record<string, unknown>;\n body?: Record<string, unknown>;\n limit?: number;\n }): Promise<unknown> {\n const path = validateRedditPath(input.path);\n assertReadScoped(path, input.account_id);\n if (input.method === 'POST') {\n if (!/(?:\\/reports|\\/history|\\/query|\\/search)$/.test(path)) {\n throw new AdportError('INVALID_INPUT', 'reddit: read-only POST is limited to documented reports, history, query, and search endpoints');\n }\n return this.client.post(path, input.body ?? {});\n }\n if (input.limit) return this.client.getPaged(path, input.params, input.limit);\n return this.client.get(path, input.params);\n }\n\n async listCampaigns(accountId: string, ids?: string[]): Promise<RedditCampaign[]> {\n return this.client.getPaged<RedditCampaign>(\n `ad_accounts/${encodeURIComponent(accountId)}/campaigns`,\n { ...(ids?.length ? { id: ids } : {}), 'page.size': Math.min(ids?.length ?? 100, 100) },\n ids?.length ?? 1000,\n );\n }\n\n async previewWrite(op: WriteOperation, guard: WriteGuard): Promise<WritePreview> {\n const plan = await this.plan(op, guard);\n return {\n summary: plan.summary,\n changes: plan.changes,\n coercions: plan.coercions,\n budgetDeltas: plan.budgetDeltas,\n serverValidated: false,\n };\n }\n\n async applyWrite(op: WriteOperation, guard: WriteGuard): Promise<WriteResult> {\n const plan = await this.plan(op, guard);\n return { applied: true, resourceIds: await plan.execute() };\n }\n\n private async fetchReportPages(\n accountId: string,\n body: Record<string, unknown>,\n limit: number,\n ): Promise<Array<Record<string, unknown>>> {\n const first = await this.client.post<{ metrics?: Array<Record<string, unknown>> }>(\n `ad_accounts/${encodeURIComponent(accountId)}/reports?page.size=${Math.min(limit, 1000)}`,\n body,\n );\n const rows = [...(first.data.metrics ?? [])];\n let next = first.pagination?.next_url;\n while (next && rows.length < limit) {\n const page = await this.client.get<{ metrics?: Array<Record<string, unknown>> }>(next);\n rows.push(...(page.data.metrics ?? []));\n next = page.pagination?.next_url;\n }\n return rows.slice(0, limit);\n }\n\n private toReportRow(\n raw: Record<string, unknown>,\n accountId: string,\n query: NormalizedQuery,\n breakdown?: string,\n ): ReportRow {\n const value = (key: string) => Number(raw[key] ?? 0);\n const spend = value('spend') / REDDIT_MICROS;\n const impressions = value('impressions');\n const clicks = value('clicks');\n const conversions = value('conversion_purchase_clicks') + value('conversion_purchase_views');\n const conversionValue = value('conversion_purchase_total_value') / 100;\n const all: Record<MetricName, number> = {\n spend: round2(spend), impressions, clicks, conversions, conversion_value: round2(conversionValue),\n ctr: impressions ? round2((clicks / impressions) * 100) : 0,\n cpc: clicks ? round2(spend / clicks) : 0,\n cpm: impressions ? round2((spend / impressions) * 1000) : 0,\n cpa: conversions ? round2(spend / conversions) : 0,\n roas: spend ? round2(conversionValue / spend) : 0,\n };\n const entityId = breakdown ? String(raw[breakdown] ?? '') : accountId;\n return {\n provider: this.id,\n accountId,\n entity: { level: query.level, id: entityId, name: entityId },\n metrics: Object.fromEntries(query.metrics.map((metric) => [metric, all[metric]])),\n };\n }\n\n private async plan(op: WriteOperation, guard: WriteGuard): Promise<WritePlan> {\n const payload = op.payload as never;\n switch (op.tool) {\n case 'reddit_create_campaign': return this.planCreateCampaign(op.accountId, payload, guard);\n case 'reddit_set_campaign_status': return this.planSetStatus(op.accountId, payload);\n case 'reddit_set_budget': return this.planSetBudget(op.accountId, payload);\n case 'reddit_api_create': return this.planApiCreate(op.accountId, payload, guard);\n case 'reddit_api_update': return this.planApiUpdate(op.accountId, payload);\n case 'reddit_api_delete': return this.planApiDelete(op.accountId, payload);\n default: throw new AdportError('PROVIDER_ERROR', `reddit: unsupported write tool ${op.tool}`);\n }\n }\n\n private async planCreateCampaign(\n accountId: string,\n payload: {\n name: string;\n objective: string;\n funding_instrument_id: string;\n configured_status?: 'ACTIVE' | 'PAUSED';\n budget_micros?: number;\n budget_type?: 'DAILY_SPEND' | 'LIFETIME_SPEND';\n conversion_pixel_id?: string;\n spend_cap_micros?: number;\n },\n guard: WriteGuard,\n ): Promise<WritePlan> {\n if (payload.budget_micros && !payload.conversion_pixel_id) {\n throw new AdportError('INVALID_INPUT', 'reddit: conversion_pixel_id is required for CBO campaigns (effective July 13, 2026)');\n }\n const requested = payload.configured_status ?? 'PAUSED';\n const status = guard.forcePausedCreation ? 'PAUSED' : requested;\n const coercions = requested !== status\n ? ['configured_status coerced to PAUSED by policy (paused_creation)']\n : [];\n const data: Record<string, unknown> = {\n name: payload.name,\n configured_status: status,\n objective: payload.objective,\n funding_instrument_id: payload.funding_instrument_id,\n is_campaign_budget_optimization: payload.budget_micros !== undefined,\n ...(payload.budget_micros ? {\n goal_type: payload.budget_type ?? 'DAILY_SPEND',\n goal_value: payload.budget_micros,\n conversion_pixel_id: payload.conversion_pixel_id,\n } : {}),\n ...(payload.spend_cap_micros ? { spend_cap: payload.spend_cap_micros } : {}),\n };\n const budgetDeltas: WritePreview['budgetDeltas'] = [];\n if (payload.budget_micros) budgetDeltas.push({ target: `new campaign \"${payload.name}\" budget`, toMicros: payload.budget_micros });\n if (payload.spend_cap_micros) budgetDeltas.push({ target: `new campaign \"${payload.name}\" spend cap`, toMicros: payload.spend_cap_micros });\n return {\n summary: `Create Reddit campaign \"${payload.name}\" (${payload.objective}, ${status.toLowerCase()})`,\n changes: [`+ POST /ad_accounts/${accountId}/campaigns ${JSON.stringify({ data })}`],\n coercions,\n budgetDeltas,\n execute: async () => extractIds(await this.client.post<RedditCampaign>(`ad_accounts/${encodeURIComponent(accountId)}/campaigns`, { data })),\n };\n }\n\n private async planSetStatus(\n accountId: string,\n payload: { campaign_id: string; configured_status: 'ACTIVE' | 'PAUSED' },\n ): Promise<WritePlan> {\n const campaign = await this.getOwnedCampaign(accountId, payload.campaign_id);\n return {\n summary: `Set Reddit campaign \"${campaign.name}\" → ${payload.configured_status.toLowerCase()}`,\n changes: [`~ campaign ${campaign.id} configured_status ${campaign.configured_status} → ${payload.configured_status}`],\n coercions: [], budgetDeltas: [],\n execute: async () => extractIds(await this.client.patch<RedditCampaign>(`campaigns/${encodeURIComponent(campaign.id)}`, {\n data: { configured_status: payload.configured_status },\n })),\n };\n }\n\n private async planSetBudget(\n accountId: string,\n payload: { campaign_id: string; budget_micros: number; budget_type?: 'DAILY_SPEND' | 'LIFETIME_SPEND' },\n ): Promise<WritePlan> {\n const campaign = await this.getOwnedCampaign(accountId, payload.campaign_id);\n if (!campaign.is_campaign_budget_optimization) {\n throw new AdportError('INVALID_INPUT', `reddit: campaign \"${campaign.name}\" is not a CBO campaign; budget its ad groups instead`);\n }\n return {\n summary: `Change Reddit campaign \"${campaign.name}\" budget ${campaign.goal_value ?? '?'} → ${payload.budget_micros} micros`,\n changes: [`~ campaign ${campaign.id} ${campaign.goal_type ?? 'goal'} ${campaign.goal_value ?? '?'} → ${payload.budget_micros}`],\n coercions: [],\n budgetDeltas: [{ target: `campaign \"${campaign.name}\" budget`, fromMicros: campaign.goal_value, toMicros: payload.budget_micros }],\n execute: async () => extractIds(await this.client.patch<RedditCampaign>(`campaigns/${encodeURIComponent(campaign.id)}`, {\n data: { goal_value: payload.budget_micros, ...(payload.budget_type ? { goal_type: payload.budget_type } : {}) },\n })),\n };\n }\n\n private async planApiCreate(\n accountId: string,\n payload: { path: string; body: Record<string, unknown> },\n guard: WriteGuard,\n ): Promise<WritePlan> {\n const path = validateRedditPath(payload.path);\n if (!path.startsWith(`ad_accounts/${accountId}/`)) {\n throw new AdportError('INVALID_INPUT', 'reddit: create path must be under the selected ad account');\n }\n const body = structuredClone(payload.body);\n const coercions: string[] = [];\n if (guard.forcePausedCreation) {\n coerceCreatedStatuses(body, coercions, /\\/(?:campaigns|ad_groups|ads)$/.test(path));\n }\n return {\n summary: `Create Reddit resource via /${path}`,\n changes: [`+ POST /${path} ${JSON.stringify(body)}`],\n coercions,\n budgetDeltas: collectMicros(body),\n execute: async () => extractIds(await this.client.post(path, body)),\n };\n }\n\n private async planApiUpdate(\n accountId: string,\n payload: { path: string; body: Record<string, unknown> },\n ): Promise<WritePlan> {\n const path = validateRedditPath(payload.path);\n await this.assertOwnedResource(path, accountId);\n if (containsMoney(payload.body)) {\n throw new AdportError('INVALID_INPUT', 'reddit: budget, bid, goal, and spend-cap updates require a typed budget tool for policy checks');\n }\n return {\n summary: `Update Reddit resource via /${path}`,\n changes: [`~ PATCH /${path} ${JSON.stringify(payload.body)}`],\n coercions: [], budgetDeltas: [],\n execute: async () => extractIds(await this.client.patch(path, payload.body)),\n };\n }\n\n private async planApiDelete(accountId: string, payload: { path: string }): Promise<WritePlan> {\n const path = validateRedditPath(payload.path);\n await this.assertOwnedResource(path, accountId);\n return {\n summary: `Permanently delete Reddit resource via /${path}`,\n changes: [`- DELETE /${path}`],\n coercions: [], budgetDeltas: [],\n execute: async () => extractIds(await this.client.delete(path)),\n };\n }\n\n private async getOwnedCampaign(accountId: string, campaignId: string): Promise<RedditCampaign> {\n const campaign = (await this.client.get<RedditCampaign>(`campaigns/${encodeURIComponent(campaignId)}`)).data;\n if (campaign.ad_account_id !== accountId) {\n throw new AdportError('INVALID_INPUT', `reddit: campaign ${campaignId} does not belong to selected account ${accountId}`);\n }\n return campaign;\n }\n\n private async assertOwnedResource(path: string, accountId: string): Promise<void> {\n const resource = (await this.client.get<Record<string, unknown>>(path)).data;\n if (String(resource.ad_account_id ?? '') !== accountId) {\n throw new AdportError('INVALID_INPUT', `reddit: /${path} does not prove ownership by selected account ${accountId}`);\n }\n }\n}\n\nfunction validateRedditPath(path: string): string {\n const normalized = path.replace(/^\\/+|\\/+$/g, '').trim();\n if (!/^[A-Za-z0-9_.~/-]{3,220}$/.test(normalized) || normalized.includes('..') || normalized.includes('//')) {\n throw new AdportError('INVALID_INPUT', `reddit: invalid Ads API v3 path \"${path}\"`);\n }\n return normalized;\n}\n\nfunction assertReadScoped(path: string, accountId: string): void {\n if (path === `ad_accounts/${accountId}` || path.startsWith(`ad_accounts/${accountId}/`)) return;\n if (/^(campaigns|ad_groups|ads|structured_posts)\\/[^/]+$/.test(path)) return;\n throw new AdportError('INVALID_INPUT', 'reddit: read path must be selected-account scoped or a single campaign/ad-group/ad/post resource');\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return Boolean(value) && typeof value === 'object' && !Array.isArray(value);\n}\n\nfunction coerceCreatedStatuses(value: Record<string, unknown>, coercions: string[], statusBearingPath: boolean): void {\n const data = isRecord(value.data) ? value.data : value;\n const visit = (item: unknown, path = 'body'): void => {\n if (Array.isArray(item)) return item.forEach((child, index) => visit(child, `${path}[${index}]`));\n if (!isRecord(item)) return;\n for (const [key, child] of Object.entries(item)) {\n if (key === 'configured_status' && child !== 'PAUSED') {\n item[key] = 'PAUSED';\n coercions.push(`${path}.${key} coerced to PAUSED by policy (paused_creation)`);\n } else visit(child, `${path}.${key}`);\n }\n };\n visit(value);\n if (statusBearingPath && data.configured_status === undefined) {\n data.configured_status = 'PAUSED';\n coercions.push('body.data.configured_status set to PAUSED by policy (paused_creation)');\n }\n}\n\nconst MONEY_KEY = /(?:goal_value|spend_cap|bid_value|budget(?:_micros)?|daily_budget|lifetime_budget)$/i;\n\nfunction containsMoney(value: unknown): boolean {\n if (Array.isArray(value)) return value.some(containsMoney);\n if (!isRecord(value)) return false;\n return Object.entries(value).some(([key, child]) => MONEY_KEY.test(key) || containsMoney(child));\n}\n\nfunction collectMicros(value: unknown, path = 'body'): WritePreview['budgetDeltas'] {\n if (Array.isArray(value)) return value.flatMap((child, index) => collectMicros(child, `${path}[${index}]`));\n if (!isRecord(value)) return [];\n const deltas: WritePreview['budgetDeltas'] = [];\n for (const [key, child] of Object.entries(value)) {\n if (MONEY_KEY.test(key) && Number.isInteger(child) && Number(child) > 0) {\n deltas.push({ target: `${path}.${key}`, toMicros: Number(child) });\n }\n deltas.push(...collectMicros(child, `${path}.${key}`));\n }\n return deltas;\n}\n\nfunction extractIds(envelope: RedditEnvelope<unknown>): string[] {\n const ids: string[] = [];\n const visit = (value: unknown): void => {\n if (Array.isArray(value)) return value.forEach(visit);\n if (!isRecord(value)) return;\n if (typeof value.id === 'string' || typeof value.id === 'number') ids.push(String(value.id));\n for (const child of Object.values(value)) visit(child);\n };\n visit(envelope.data);\n return [...new Set(ids)];\n}\n\nfunction round2(value: number): number {\n return Math.round(value * 100) / 100;\n}\n","import { defineTool, guardedWriteTool, type AnyToolDefinition } from '@adport/core';\nimport { z } from 'zod';\nimport type { RedditAdsProvider } from './provider.js';\n\nconst status = z.enum(['ACTIVE', 'PAUSED']);\n\nexport function redditTools(provider: RedditAdsProvider): AnyToolDefinition[] {\n return [\n defineTool({\n name: 'reddit_api_read',\n namespace: 'reddit',\n description: 'Call a documented Reddit Ads API v3 GET or read-only reports/history/query/search POST endpoint, scoped to the selected ad account.',\n input: z.object({\n account_id: z.string(),\n path: z.string().min(3).describe('Relative v3 path, e.g. ad_accounts/{id}/ad_groups or campaigns/{campaign_id}'),\n method: z.enum(['GET', 'POST']).default('GET'),\n params: z.record(z.string(), z.unknown()).optional(),\n body: z.record(z.string(), z.unknown()).optional(),\n limit: z.number().int().positive().max(5000).optional().describe('Follow list pagination for GET responses'),\n }),\n annotations: { readOnly: true },\n handler: (input) => provider.apiRead(input),\n }),\n defineTool({\n name: 'reddit_campaigns',\n namespace: 'reddit',\n description: 'List Reddit campaigns and their native configured/effective status, CBO budget, objective, and funding instrument.',\n input: z.object({ account_id: z.string(), campaign_ids: z.array(z.string()).optional() }),\n annotations: { readOnly: true },\n async handler(input) { return { campaigns: await provider.listCampaigns(input.account_id, input.campaign_ids) }; },\n }),\n defineTool({\n name: 'reddit_report',\n namespace: 'reddit',\n description: 'Query Reddit Ads API v3 reporting directly. Spend, CPC, CPM, and applicable conversion fields are returned by Reddit in micros.',\n input: z.object({\n account_id: z.string(),\n breakdowns: z.array(z.string()).max(4).optional(),\n fields: z.array(z.string()).min(1).default(['spend', 'impressions', 'clicks']),\n starts_at: z.string().datetime({ offset: true }),\n ends_at: z.string().datetime({ offset: true }),\n time_zone_id: z.string().default('UTC'),\n filter: z.string().optional(),\n page_size: z.number().int().positive().max(1000).default(200),\n }),\n annotations: { readOnly: true },\n handler: (input) => provider.rawReport(input),\n }),\n guardedWriteTool({\n name: 'reddit_create_campaign',\n namespace: 'reddit',\n description: 'Create a Reddit campaign through API v3. Campaigns are forced PAUSED. CBO budgets and spend caps are integer micros.',\n provider: 'reddit', kind: 'create',\n payload: z.object({\n name: z.string().min(1),\n objective: z.string().min(1).default('TRAFFIC').describe('Current Reddit objective enum; Reddit has announced an enum migration for Sep 30, 2026'),\n funding_instrument_id: z.string().min(1),\n configured_status: status.optional(),\n budget_micros: z.number().int().positive().optional().describe('Sets CBO goal_value; requires conversion_pixel_id'),\n budget_type: z.enum(['DAILY_SPEND', 'LIFETIME_SPEND']).default('DAILY_SPEND'),\n conversion_pixel_id: z.string().optional(),\n spend_cap_micros: z.number().int().positive().optional(),\n }),\n }),\n guardedWriteTool({\n name: 'reddit_set_campaign_status', namespace: 'reddit',\n description: 'Activate or pause a Reddit campaign after verifying ad-account ownership.',\n provider: 'reddit', kind: 'update',\n payload: z.object({ campaign_id: z.string(), configured_status: status }),\n }),\n guardedWriteTool({\n name: 'reddit_set_budget', namespace: 'reddit',\n description: 'Change a Reddit CBO campaign goal_value in integer micros, with current-value lookup and policy checks.',\n provider: 'reddit', kind: 'update',\n payload: z.object({\n campaign_id: z.string(),\n budget_micros: z.number().int().positive(),\n budget_type: z.enum(['DAILY_SPEND', 'LIFETIME_SPEND']).optional(),\n }),\n }),\n guardedWriteTool({\n name: 'reddit_api_create', namespace: 'reddit',\n description: 'Create via an account-scoped Reddit Ads API v3 endpoint. Campaign status is forced PAUSED and native micros fields are policy-checked.',\n provider: 'reddit', kind: 'create',\n payload: z.object({ path: z.string().min(3), body: z.record(z.string(), z.unknown()) }),\n }),\n guardedWriteTool({\n name: 'reddit_api_update', namespace: 'reddit',\n description: 'PATCH non-budget fields on a Reddit Ads API v3 resource after verifying ad-account ownership.',\n provider: 'reddit', kind: 'update',\n payload: z.object({ path: z.string().min(3), body: z.record(z.string(), z.unknown()) }),\n }),\n guardedWriteTool({\n name: 'reddit_api_delete', namespace: 'reddit',\n description: 'Permanently DELETE a Reddit Ads API v3 resource after verifying ad-account ownership.',\n provider: 'reddit', kind: 'remove', destructive: true,\n payload: z.object({ path: z.string().min(3) }),\n }),\n ];\n}\n","import type { CredentialStore, ProviderModule } from '@adport/core';\nimport { RedditAdsClient, type RedditCredentials } from './client.js';\nimport { RedditAdsProvider } from './provider.js';\nimport { redditTools } from './tools.js';\n\nexport { RedditAdsClient, REDDIT_API_BASE, REDDIT_API_VERSION, REDDIT_TOKEN_URL, type RedditCredentials, type RedditEnvelope } from './client.js';\nexport { RedditAdsProvider, REDDIT_MICROS, type RedditCampaign } from './provider.js';\nexport { redditTools } from './tools.js';\n\nexport async function resolveRedditCredentials(store: CredentialStore): Promise<RedditCredentials | undefined> {\n const record = await store.get('reddit');\n const data = record?.data;\n if (data?.client_id && data.client_secret && data.refresh_token && data.user_agent) {\n const source = record?.source ?? 'byo';\n return {\n clientId: data.client_id,\n clientSecret: data.client_secret,\n refreshToken: data.refresh_token,\n userAgent: data.user_agent,\n onRefreshToken: async (refreshToken) => {\n await store.set({ provider: 'reddit', source, data: { ...data, refresh_token: refreshToken } });\n },\n };\n }\n if (process.env.REDDIT_ADS_CLIENT_ID && process.env.REDDIT_ADS_CLIENT_SECRET && process.env.REDDIT_ADS_REFRESH_TOKEN && process.env.REDDIT_ADS_USER_AGENT) {\n return {\n clientId: process.env.REDDIT_ADS_CLIENT_ID,\n clientSecret: process.env.REDDIT_ADS_CLIENT_SECRET,\n refreshToken: process.env.REDDIT_ADS_REFRESH_TOKEN,\n userAgent: process.env.REDDIT_ADS_USER_AGENT,\n };\n }\n return undefined;\n}\n\nexport async function createRedditModule(store: CredentialStore): Promise<ProviderModule | undefined> {\n const credentials = await resolveRedditCredentials(store);\n if (!credentials) return undefined;\n const provider = new RedditAdsProvider(new RedditAdsClient(credentials));\n return { provider, tools: redditTools(provider) };\n}\n"],"mappings":";AAAA,SAAS,mBAAmB;AAWrB,IAAM,qBAA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KAAK;AAAA,MACnB,WAAW,KAAK;AAAA,MAChB,gBAAgB,OAAO,iBAAiB;AACtC,cAAM,MAAM,IAAI,EAAE,UAAU,UAAU,QAAQ,MAAM,EAAE,GAAG,MAAM,eAAe,aAAa,EAAE,CAAC;AAAA,MAChG;AAAA,IACF;AAAA,EACF;AACA,MAAI,QAAQ,IAAI,wBAAwB,QAAQ,IAAI,4BAA4B,QAAQ,IAAI,4BAA4B,QAAQ,IAAI,uBAAuB;AACzJ,WAAO;AAAA,MACL,UAAU,QAAQ,IAAI;AAAA,MACtB,cAAc,QAAQ,IAAI;AAAA,MAC1B,cAAc,QAAQ,IAAI;AAAA,MAC1B,WAAW,QAAQ,IAAI;AAAA,IACzB;AAAA,EACF;AACA,SAAO;AACT;AAEA,eAAsB,mBAAmB,OAA6D;AACpG,QAAM,cAAc,MAAM,yBAAyB,KAAK;AACxD,MAAI,CAAC,YAAa,QAAO;AACzB,QAAM,WAAW,IAAI,kBAAkB,IAAI,gBAAgB,WAAW,CAAC;AACvE,SAAO,EAAE,UAAU,OAAO,YAAY,QAAQ,EAAE;AAClD;","names":["AdportError","status"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@adport/provider-reddit",
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "Reddit Ads provider for adport — Reddit Ads API v3",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"zod": "^4.4.3",
|
|
20
|
+
"@adport/core": "0.4.0"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@types/node": "^26.1.2",
|
|
24
|
+
"tsup": "^8.3.0",
|
|
25
|
+
"typescript": "^5.8.0",
|
|
26
|
+
"vitest": "^4.1.10"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "tsup src/index.ts --format esm --dts --sourcemap --clean",
|
|
30
|
+
"test": "vitest run",
|
|
31
|
+
"typecheck": "tsc --noEmit",
|
|
32
|
+
"clean": "rm -rf dist"
|
|
33
|
+
}
|
|
34
|
+
}
|