@feelflow/ffid-sdk 0.1.0 → 0.3.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/dist/agency/index.cjs +322 -0
- package/dist/agency/index.d.cts +321 -0
- package/dist/agency/index.d.ts +321 -0
- package/dist/agency/index.js +316 -0
- package/dist/announcements/index.cjs +109 -0
- package/dist/announcements/index.d.cts +165 -0
- package/dist/announcements/index.d.ts +165 -0
- package/dist/announcements/index.js +106 -0
- package/dist/{chunk-YCMQXJOS.cjs → chunk-A6YJDYIX.cjs} +526 -0
- package/dist/chunk-P4MLCG4T.js +4 -0
- package/dist/chunk-P5PPUZGX.cjs +6 -0
- package/dist/{chunk-A63MX52D.js → chunk-VXBUXOLF.js} +522 -1
- package/dist/components/index.cjs +13 -5
- package/dist/components/index.d.cts +1 -1
- package/dist/components/index.d.ts +1 -1
- package/dist/components/index.js +1 -1
- package/dist/constants-BeWMWOOd.d.cts +10 -0
- package/dist/constants-BeWMWOOd.d.ts +10 -0
- package/dist/{index-CtBBLbTn.d.cts → index-DBp3Ulyl.d.cts} +246 -1
- package/dist/{index-CtBBLbTn.d.ts → index-DBp3Ulyl.d.ts} +246 -1
- package/dist/index.cjs +31 -11
- package/dist/index.d.cts +59 -4
- package/dist/index.d.ts +59 -4
- package/dist/index.js +2 -2
- package/dist/legal/index.cjs +6 -4
- package/dist/legal/index.d.cts +2 -9
- package/dist/legal/index.d.ts +2 -9
- package/dist/legal/index.js +3 -3
- package/dist/webhooks/index.cjs +251 -0
- package/dist/webhooks/index.d.cts +298 -0
- package/dist/webhooks/index.d.ts +298 -0
- package/dist/webhooks/index.js +237 -0
- package/package.json +26 -3
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
export { D as DEFAULT_API_BASE_URL } from '../constants-BeWMWOOd.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* FFID Agency SDK Type Definitions
|
|
5
|
+
*
|
|
6
|
+
* Types for the FeelFlow ID Agency Management SDK.
|
|
7
|
+
* Used for managing agencies, members, organizations, branding,
|
|
8
|
+
* domain, email settings, billing, and hierarchy.
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Configuration for the FFID Agency Client
|
|
12
|
+
*/
|
|
13
|
+
interface FFIDAgencyClientConfig {
|
|
14
|
+
/** FFID API base URL (defaults to DEFAULT_API_BASE_URL) */
|
|
15
|
+
apiBaseUrl?: string;
|
|
16
|
+
/** Enable debug logging */
|
|
17
|
+
debug?: boolean;
|
|
18
|
+
/** Custom logger */
|
|
19
|
+
logger?: FFIDAgencyLogger;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Logger interface for Agency SDK
|
|
23
|
+
*/
|
|
24
|
+
interface FFIDAgencyLogger {
|
|
25
|
+
debug: (...args: unknown[]) => void;
|
|
26
|
+
info: (...args: unknown[]) => void;
|
|
27
|
+
warn: (...args: unknown[]) => void;
|
|
28
|
+
error: (...args: unknown[]) => void;
|
|
29
|
+
}
|
|
30
|
+
type FFIDAgencyStatus = 'active' | 'suspended' | 'inactive';
|
|
31
|
+
type FFIDAgencyBillingType = 'direct' | 'consolidated' | 'markup';
|
|
32
|
+
type FFIDAgencyMemberRole = 'owner' | 'admin' | 'sales' | 'support';
|
|
33
|
+
type FFIDAgencyMemberStatus = 'active' | 'invited' | 'suspended';
|
|
34
|
+
type FFIDAgencyBillingOverride = 'default' | 'direct' | 'exempt';
|
|
35
|
+
type FFIDSslStatus = 'pending' | 'active' | 'failed';
|
|
36
|
+
interface FFIDAgency {
|
|
37
|
+
id: string;
|
|
38
|
+
name: string;
|
|
39
|
+
slug: string;
|
|
40
|
+
isPlatformOwner: boolean;
|
|
41
|
+
parentAgencyId: string | null;
|
|
42
|
+
status: FFIDAgencyStatus;
|
|
43
|
+
settings: FFIDAgencySettings;
|
|
44
|
+
billingType: FFIDAgencyBillingType;
|
|
45
|
+
contactEmail: string | null;
|
|
46
|
+
createdAt: string;
|
|
47
|
+
updatedAt: string;
|
|
48
|
+
}
|
|
49
|
+
interface FFIDAgencySettings {
|
|
50
|
+
branding?: FFIDAgencyBrandingSettings;
|
|
51
|
+
domain?: FFIDAgencyDomainSettings;
|
|
52
|
+
email?: FFIDAgencyEmailSettings;
|
|
53
|
+
}
|
|
54
|
+
interface FFIDAgencyBrandingSettings {
|
|
55
|
+
logoUrl?: string;
|
|
56
|
+
faviconUrl?: string;
|
|
57
|
+
primaryColor?: string;
|
|
58
|
+
secondaryColor?: string;
|
|
59
|
+
companyName?: string;
|
|
60
|
+
}
|
|
61
|
+
interface FFIDAgencyDnsRecord {
|
|
62
|
+
type: string;
|
|
63
|
+
name: string;
|
|
64
|
+
value: string;
|
|
65
|
+
}
|
|
66
|
+
interface FFIDAgencyDomainSettings {
|
|
67
|
+
customDomain?: string;
|
|
68
|
+
sslStatus?: FFIDSslStatus;
|
|
69
|
+
verifiedAt?: string;
|
|
70
|
+
dnsRecords?: FFIDAgencyDnsRecord[];
|
|
71
|
+
}
|
|
72
|
+
interface FFIDAgencyEmailSettings {
|
|
73
|
+
fromName?: string;
|
|
74
|
+
fromEmail?: string;
|
|
75
|
+
replyTo?: string;
|
|
76
|
+
}
|
|
77
|
+
interface FFIDAgencyMember {
|
|
78
|
+
userId: string;
|
|
79
|
+
email: string;
|
|
80
|
+
displayName: string | null;
|
|
81
|
+
avatarUrl: string | null;
|
|
82
|
+
role: FFIDAgencyMemberRole;
|
|
83
|
+
status: FFIDAgencyMemberStatus;
|
|
84
|
+
joinedAt: string | null;
|
|
85
|
+
invitedAt: string | null;
|
|
86
|
+
}
|
|
87
|
+
interface FFIDAddAgencyMemberRequest {
|
|
88
|
+
email: string;
|
|
89
|
+
role?: FFIDAgencyMemberRole;
|
|
90
|
+
}
|
|
91
|
+
interface FFIDUpdateAgencyMemberRequest {
|
|
92
|
+
role: FFIDAgencyMemberRole;
|
|
93
|
+
}
|
|
94
|
+
interface FFIDAgencyOrganization {
|
|
95
|
+
id: string;
|
|
96
|
+
agencyId: string;
|
|
97
|
+
organizationId: string;
|
|
98
|
+
organizationName: string;
|
|
99
|
+
organizationSlug: string;
|
|
100
|
+
billingOverride: FFIDAgencyBillingOverride;
|
|
101
|
+
commissionRate: number | null;
|
|
102
|
+
markupRate: number | null;
|
|
103
|
+
markupFixed: number | null;
|
|
104
|
+
notes: string | null;
|
|
105
|
+
createdAt: string;
|
|
106
|
+
}
|
|
107
|
+
interface FFIDLinkOrganizationRequest {
|
|
108
|
+
organizationId: string;
|
|
109
|
+
billingOverride?: FFIDAgencyBillingOverride;
|
|
110
|
+
commissionRate?: number;
|
|
111
|
+
notes?: string;
|
|
112
|
+
}
|
|
113
|
+
interface FFIDCreateSubAgencyRequest {
|
|
114
|
+
name: string;
|
|
115
|
+
contactEmail?: string;
|
|
116
|
+
settings?: FFIDAgencySettings;
|
|
117
|
+
}
|
|
118
|
+
interface FFIDAgencyHierarchyNode {
|
|
119
|
+
agency: FFIDAgency;
|
|
120
|
+
children: FFIDAgencyHierarchyNode[];
|
|
121
|
+
level: number;
|
|
122
|
+
}
|
|
123
|
+
interface FFIDAgencyHierarchyResponse {
|
|
124
|
+
root: FFIDAgencyHierarchyNode;
|
|
125
|
+
totalLevels: number;
|
|
126
|
+
totalAgencies: number;
|
|
127
|
+
}
|
|
128
|
+
type FFIDAgencyAssetType = 'logo' | 'favicon';
|
|
129
|
+
interface FFIDAgencyBillingConfig {
|
|
130
|
+
billingType: FFIDAgencyBillingType;
|
|
131
|
+
stripeCustomerId: string | null;
|
|
132
|
+
invoiceEmail: string | null;
|
|
133
|
+
paymentTerms: number | null;
|
|
134
|
+
defaultMarkupRate: number | null;
|
|
135
|
+
defaultMarkupFixed: number | null;
|
|
136
|
+
}
|
|
137
|
+
interface FFIDUpdateAgencyBillingRequest {
|
|
138
|
+
billingType?: FFIDAgencyBillingType;
|
|
139
|
+
invoiceEmail?: string;
|
|
140
|
+
paymentTerms?: number;
|
|
141
|
+
defaultMarkupRate?: number;
|
|
142
|
+
defaultMarkupFixed?: number;
|
|
143
|
+
}
|
|
144
|
+
interface FFIDAgencyBillingSummary {
|
|
145
|
+
agencyId: string;
|
|
146
|
+
totalOrganizations: number;
|
|
147
|
+
totalRevenue: number;
|
|
148
|
+
period: string;
|
|
149
|
+
}
|
|
150
|
+
type FFIDAgencyInvoiceStatus = 'draft' | 'open' | 'paid' | 'void' | 'uncollectible';
|
|
151
|
+
interface FFIDAgencyInvoice {
|
|
152
|
+
id: string;
|
|
153
|
+
amount: number;
|
|
154
|
+
currency: string;
|
|
155
|
+
status: FFIDAgencyInvoiceStatus;
|
|
156
|
+
periodStart: string;
|
|
157
|
+
periodEnd: string;
|
|
158
|
+
createdAt: string;
|
|
159
|
+
}
|
|
160
|
+
interface FFIDAgencyRevenue {
|
|
161
|
+
agencyId: string;
|
|
162
|
+
totalRevenue: number;
|
|
163
|
+
breakdown: Record<string, number>;
|
|
164
|
+
period: string;
|
|
165
|
+
}
|
|
166
|
+
interface FFIDSetupDomainRequest {
|
|
167
|
+
customDomain: string;
|
|
168
|
+
}
|
|
169
|
+
interface FFIDUpdateEmailSettingsRequest {
|
|
170
|
+
fromName?: string;
|
|
171
|
+
fromEmail?: string;
|
|
172
|
+
replyTo?: string;
|
|
173
|
+
}
|
|
174
|
+
interface FFIDUpdateAgencyRequest {
|
|
175
|
+
name?: string;
|
|
176
|
+
contactEmail?: string;
|
|
177
|
+
settings?: FFIDAgencySettings;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Public interface for the FFID Agency API client.
|
|
181
|
+
* Obtain an instance via `createFFIDAgencyClient()`.
|
|
182
|
+
*/
|
|
183
|
+
interface FFIDAgencyClient {
|
|
184
|
+
getAgencies(): Promise<FFIDAgencyApiResponse<{
|
|
185
|
+
agencies: FFIDAgency[];
|
|
186
|
+
count: number;
|
|
187
|
+
}>>;
|
|
188
|
+
getAgency(agencyId: string): Promise<FFIDAgencyApiResponse<{
|
|
189
|
+
agency: FFIDAgency;
|
|
190
|
+
}>>;
|
|
191
|
+
updateAgency(agencyId: string, data: FFIDUpdateAgencyRequest): Promise<FFIDAgencyApiResponse<{
|
|
192
|
+
agency: FFIDAgency;
|
|
193
|
+
}>>;
|
|
194
|
+
getMembers(agencyId: string): Promise<FFIDAgencyApiResponse<{
|
|
195
|
+
members: FFIDAgencyMember[];
|
|
196
|
+
count: number;
|
|
197
|
+
}>>;
|
|
198
|
+
addMember(agencyId: string, data: FFIDAddAgencyMemberRequest): Promise<FFIDAgencyApiResponse<{
|
|
199
|
+
member: FFIDAgencyMember;
|
|
200
|
+
}>>;
|
|
201
|
+
updateMember(agencyId: string, userId: string, data: FFIDUpdateAgencyMemberRequest): Promise<FFIDAgencyApiResponse<{
|
|
202
|
+
member: FFIDAgencyMember;
|
|
203
|
+
}>>;
|
|
204
|
+
removeMember(agencyId: string, userId: string): Promise<FFIDAgencyApiResponse<{
|
|
205
|
+
message: string;
|
|
206
|
+
}>>;
|
|
207
|
+
getOrganizations(agencyId: string): Promise<FFIDAgencyApiResponse<{
|
|
208
|
+
organizations: FFIDAgencyOrganization[];
|
|
209
|
+
count: number;
|
|
210
|
+
}>>;
|
|
211
|
+
linkOrganization(agencyId: string, data: FFIDLinkOrganizationRequest): Promise<FFIDAgencyApiResponse<{
|
|
212
|
+
organization: FFIDAgencyOrganization;
|
|
213
|
+
}>>;
|
|
214
|
+
unlinkOrganization(agencyId: string, orgId: string): Promise<FFIDAgencyApiResponse<{
|
|
215
|
+
message: string;
|
|
216
|
+
}>>;
|
|
217
|
+
getSubAgencies(agencyId: string): Promise<FFIDAgencyApiResponse<{
|
|
218
|
+
agencies: FFIDAgency[];
|
|
219
|
+
count: number;
|
|
220
|
+
}>>;
|
|
221
|
+
createSubAgency(agencyId: string, data: FFIDCreateSubAgencyRequest): Promise<FFIDAgencyApiResponse<{
|
|
222
|
+
agency: FFIDAgency;
|
|
223
|
+
}>>;
|
|
224
|
+
getHierarchy(agencyId: string): Promise<FFIDAgencyApiResponse<FFIDAgencyHierarchyResponse>>;
|
|
225
|
+
getBranding(agencyId: string): Promise<FFIDAgencyApiResponse<{
|
|
226
|
+
branding: FFIDAgencyBrandingSettings;
|
|
227
|
+
}>>;
|
|
228
|
+
updateBranding(agencyId: string, data: FFIDAgencyBrandingSettings): Promise<FFIDAgencyApiResponse<{
|
|
229
|
+
branding: FFIDAgencyBrandingSettings;
|
|
230
|
+
}>>;
|
|
231
|
+
uploadAsset(agencyId: string, file: File, type: FFIDAgencyAssetType): Promise<FFIDAgencyApiResponse<{
|
|
232
|
+
url: string;
|
|
233
|
+
}>>;
|
|
234
|
+
getDomain(agencyId: string): Promise<FFIDAgencyApiResponse<{
|
|
235
|
+
domain: FFIDAgencyDomainSettings;
|
|
236
|
+
}>>;
|
|
237
|
+
setupDomain(agencyId: string, data: FFIDSetupDomainRequest): Promise<FFIDAgencyApiResponse<{
|
|
238
|
+
domain: FFIDAgencyDomainSettings;
|
|
239
|
+
}>>;
|
|
240
|
+
removeDomain(agencyId: string): Promise<FFIDAgencyApiResponse<{
|
|
241
|
+
message: string;
|
|
242
|
+
}>>;
|
|
243
|
+
verifyDomain(agencyId: string): Promise<FFIDAgencyApiResponse<{
|
|
244
|
+
domain: FFIDAgencyDomainSettings;
|
|
245
|
+
}>>;
|
|
246
|
+
getEmailSettings(agencyId: string): Promise<FFIDAgencyApiResponse<{
|
|
247
|
+
emailSettings: FFIDAgencyEmailSettings;
|
|
248
|
+
}>>;
|
|
249
|
+
updateEmailSettings(agencyId: string, data: FFIDUpdateEmailSettingsRequest): Promise<FFIDAgencyApiResponse<{
|
|
250
|
+
emailSettings: FFIDAgencyEmailSettings;
|
|
251
|
+
}>>;
|
|
252
|
+
getBilling(agencyId: string): Promise<FFIDAgencyApiResponse<{
|
|
253
|
+
billing: FFIDAgencyBillingConfig;
|
|
254
|
+
}>>;
|
|
255
|
+
updateBilling(agencyId: string, data: FFIDUpdateAgencyBillingRequest): Promise<FFIDAgencyApiResponse<{
|
|
256
|
+
billing: FFIDAgencyBillingConfig;
|
|
257
|
+
}>>;
|
|
258
|
+
getBillingSummary(agencyId: string): Promise<FFIDAgencyApiResponse<FFIDAgencyBillingSummary>>;
|
|
259
|
+
getInvoices(agencyId: string): Promise<FFIDAgencyApiResponse<{
|
|
260
|
+
invoices: FFIDAgencyInvoice[];
|
|
261
|
+
count: number;
|
|
262
|
+
}>>;
|
|
263
|
+
getRevenue(agencyId: string): Promise<FFIDAgencyApiResponse<FFIDAgencyRevenue>>;
|
|
264
|
+
}
|
|
265
|
+
type FFIDAgencySdkErrorCode = 'NETWORK_ERROR' | 'PARSE_ERROR' | 'UNKNOWN_ERROR' | 'VALIDATION_ERROR';
|
|
266
|
+
type FFIDAgencyServerErrorCode = 'UNAUTHORIZED' | 'FORBIDDEN' | 'NOT_FOUND' | 'VALIDATION_ERROR' | 'INTERNAL_ERROR' | 'CONFLICT';
|
|
267
|
+
type FFIDAgencyErrorCode = FFIDAgencySdkErrorCode | FFIDAgencyServerErrorCode;
|
|
268
|
+
interface FFIDAgencyServerError {
|
|
269
|
+
code: FFIDAgencyErrorCode;
|
|
270
|
+
message: string;
|
|
271
|
+
details?: Record<string, unknown>;
|
|
272
|
+
}
|
|
273
|
+
type FFIDAgencyServerResponse<T> = {
|
|
274
|
+
success: true;
|
|
275
|
+
data: T;
|
|
276
|
+
error?: undefined;
|
|
277
|
+
} | {
|
|
278
|
+
success: false;
|
|
279
|
+
data?: undefined;
|
|
280
|
+
error: FFIDAgencyServerError;
|
|
281
|
+
};
|
|
282
|
+
interface FFIDAgencyError {
|
|
283
|
+
code: FFIDAgencyErrorCode;
|
|
284
|
+
message: string;
|
|
285
|
+
details?: Record<string, unknown>;
|
|
286
|
+
}
|
|
287
|
+
type FFIDAgencyApiResponse<T> = {
|
|
288
|
+
data: T;
|
|
289
|
+
error?: undefined;
|
|
290
|
+
} | {
|
|
291
|
+
data?: undefined;
|
|
292
|
+
error: FFIDAgencyError;
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* FFID Agency API Client
|
|
297
|
+
*
|
|
298
|
+
* Handles API communication for agency management operations.
|
|
299
|
+
* Uses Cookie-based authentication (credentials: 'include')
|
|
300
|
+
* for browser-based communication.
|
|
301
|
+
*
|
|
302
|
+
* @example
|
|
303
|
+
* ```typescript
|
|
304
|
+
* import { createFFIDAgencyClient } from '@feelflow/ffid-sdk/agency'
|
|
305
|
+
*
|
|
306
|
+
* const agency = createFFIDAgencyClient()
|
|
307
|
+
*
|
|
308
|
+
* // Get agency details
|
|
309
|
+
* const { data, error } = await agency.getAgency('agency-id')
|
|
310
|
+
* ```
|
|
311
|
+
*/
|
|
312
|
+
|
|
313
|
+
declare const FFID_AGENCY_ERROR_CODES: {
|
|
314
|
+
readonly NETWORK_ERROR: "NETWORK_ERROR";
|
|
315
|
+
readonly PARSE_ERROR: "PARSE_ERROR";
|
|
316
|
+
readonly UNKNOWN_ERROR: "UNKNOWN_ERROR";
|
|
317
|
+
readonly VALIDATION_ERROR: "VALIDATION_ERROR";
|
|
318
|
+
};
|
|
319
|
+
declare function createFFIDAgencyClient(config?: FFIDAgencyClientConfig): FFIDAgencyClient;
|
|
320
|
+
|
|
321
|
+
export { type FFIDAddAgencyMemberRequest, type FFIDAgency, type FFIDAgencyApiResponse, type FFIDAgencyAssetType, type FFIDAgencyBillingConfig, type FFIDAgencyBillingOverride, type FFIDAgencyBillingSummary, type FFIDAgencyBillingType, type FFIDAgencyBrandingSettings, type FFIDAgencyClient, type FFIDAgencyClientConfig, type FFIDAgencyDnsRecord, type FFIDAgencyDomainSettings, type FFIDAgencyEmailSettings, type FFIDAgencyError, type FFIDAgencyErrorCode, type FFIDAgencyHierarchyNode, type FFIDAgencyHierarchyResponse, type FFIDAgencyInvoice, type FFIDAgencyInvoiceStatus, type FFIDAgencyLogger, type FFIDAgencyMember, type FFIDAgencyMemberRole, type FFIDAgencyMemberStatus, type FFIDAgencyOrganization, type FFIDAgencyRevenue, type FFIDAgencySdkErrorCode, type FFIDAgencyServerError, type FFIDAgencyServerErrorCode, type FFIDAgencyServerResponse, type FFIDAgencySettings, type FFIDAgencyStatus, type FFIDCreateSubAgencyRequest, type FFIDLinkOrganizationRequest, type FFIDSetupDomainRequest, type FFIDSslStatus, type FFIDUpdateAgencyBillingRequest, type FFIDUpdateAgencyMemberRequest, type FFIDUpdateAgencyRequest, type FFIDUpdateEmailSettingsRequest, FFID_AGENCY_ERROR_CODES, createFFIDAgencyClient };
|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
import { DEFAULT_API_BASE_URL } from '../chunk-P4MLCG4T.js';
|
|
2
|
+
export { DEFAULT_API_BASE_URL } from '../chunk-P4MLCG4T.js';
|
|
3
|
+
|
|
4
|
+
// src/agency/ffid-agency-client.ts
|
|
5
|
+
var API_PREFIX = "/api/v1/agencies";
|
|
6
|
+
var SDK_LOG_PREFIX = "[FFID Agency SDK]";
|
|
7
|
+
var FFID_AGENCY_ERROR_CODES = {
|
|
8
|
+
NETWORK_ERROR: "NETWORK_ERROR",
|
|
9
|
+
PARSE_ERROR: "PARSE_ERROR",
|
|
10
|
+
UNKNOWN_ERROR: "UNKNOWN_ERROR",
|
|
11
|
+
VALIDATION_ERROR: "VALIDATION_ERROR"
|
|
12
|
+
};
|
|
13
|
+
var noopLogger = {
|
|
14
|
+
debug: () => {
|
|
15
|
+
},
|
|
16
|
+
info: () => {
|
|
17
|
+
},
|
|
18
|
+
warn: (...args) => console.warn(SDK_LOG_PREFIX, ...args),
|
|
19
|
+
error: (...args) => console.error(SDK_LOG_PREFIX, ...args)
|
|
20
|
+
};
|
|
21
|
+
var consoleLogger = {
|
|
22
|
+
debug: (...args) => console.debug(SDK_LOG_PREFIX, ...args),
|
|
23
|
+
info: (...args) => console.info(SDK_LOG_PREFIX, ...args),
|
|
24
|
+
warn: (...args) => console.warn(SDK_LOG_PREFIX, ...args),
|
|
25
|
+
error: (...args) => console.error(SDK_LOG_PREFIX, ...args)
|
|
26
|
+
};
|
|
27
|
+
function createFFIDAgencyClient(config = {}) {
|
|
28
|
+
const baseUrl = config.apiBaseUrl ?? DEFAULT_API_BASE_URL;
|
|
29
|
+
const logger = config.logger ?? (config.debug ? consoleLogger : noopLogger);
|
|
30
|
+
function validateIds(...pairs) {
|
|
31
|
+
for (const [value, name] of pairs) {
|
|
32
|
+
if (!value) {
|
|
33
|
+
return {
|
|
34
|
+
error: {
|
|
35
|
+
code: FFID_AGENCY_ERROR_CODES.VALIDATION_ERROR,
|
|
36
|
+
message: `${name} is required`
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
async function fetchWithCredentials(endpoint, options = {}) {
|
|
44
|
+
const url = `${baseUrl}${API_PREFIX}${endpoint}`;
|
|
45
|
+
logger.debug("Fetching:", url);
|
|
46
|
+
let response;
|
|
47
|
+
try {
|
|
48
|
+
response = await fetch(url, {
|
|
49
|
+
...options,
|
|
50
|
+
credentials: "include",
|
|
51
|
+
headers: {
|
|
52
|
+
// FormData: let browser set Content-Type with multipart boundary automatically
|
|
53
|
+
...options.body instanceof FormData ? {} : { "Content-Type": "application/json" },
|
|
54
|
+
...options.headers
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
} catch (error) {
|
|
58
|
+
logger.error("Network error:", { url, error });
|
|
59
|
+
return {
|
|
60
|
+
error: {
|
|
61
|
+
code: FFID_AGENCY_ERROR_CODES.NETWORK_ERROR,
|
|
62
|
+
message: error instanceof Error ? error.message : "\u30CD\u30C3\u30C8\u30EF\u30FC\u30AF\u30A8\u30E9\u30FC\u304C\u767A\u751F\u3057\u307E\u3057\u305F"
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
let data;
|
|
67
|
+
try {
|
|
68
|
+
data = await response.json();
|
|
69
|
+
} catch (parseError) {
|
|
70
|
+
logger.error("Parse error:", { url, status: response.status, parseError });
|
|
71
|
+
return {
|
|
72
|
+
error: {
|
|
73
|
+
code: FFID_AGENCY_ERROR_CODES.PARSE_ERROR,
|
|
74
|
+
message: `\u30B5\u30FC\u30D0\u30FC\u304B\u3089\u4E0D\u6B63\u306A\u30EC\u30B9\u30DD\u30F3\u30B9\u3092\u53D7\u4FE1\u3057\u307E\u3057\u305F (status: ${response.status})`
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
logger.debug("Response:", response.status, data);
|
|
79
|
+
if (!response.ok || !data.success) {
|
|
80
|
+
return {
|
|
81
|
+
error: data.error ?? {
|
|
82
|
+
code: FFID_AGENCY_ERROR_CODES.UNKNOWN_ERROR,
|
|
83
|
+
message: "\u4E0D\u660E\u306A\u30A8\u30E9\u30FC\u304C\u767A\u751F\u3057\u307E\u3057\u305F"
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
if (data.data === void 0) {
|
|
88
|
+
return {
|
|
89
|
+
error: {
|
|
90
|
+
code: FFID_AGENCY_ERROR_CODES.UNKNOWN_ERROR,
|
|
91
|
+
message: "\u30B5\u30FC\u30D0\u30FC\u304B\u3089\u30C7\u30FC\u30BF\u304C\u8FD4\u3055\u308C\u307E\u305B\u3093\u3067\u3057\u305F"
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
return { data: data.data };
|
|
96
|
+
}
|
|
97
|
+
async function getAgencies() {
|
|
98
|
+
return fetchWithCredentials("");
|
|
99
|
+
}
|
|
100
|
+
async function getAgency(agencyId) {
|
|
101
|
+
const err = validateIds([agencyId, "agencyId"]);
|
|
102
|
+
if (err) return err;
|
|
103
|
+
return fetchWithCredentials(`/${encodeURIComponent(agencyId)}`);
|
|
104
|
+
}
|
|
105
|
+
async function updateAgency(agencyId, data) {
|
|
106
|
+
const err = validateIds([agencyId, "agencyId"]);
|
|
107
|
+
if (err) return err;
|
|
108
|
+
return fetchWithCredentials(`/${encodeURIComponent(agencyId)}`, {
|
|
109
|
+
method: "PUT",
|
|
110
|
+
body: JSON.stringify(data)
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
async function getMembers(agencyId) {
|
|
114
|
+
const err = validateIds([agencyId, "agencyId"]);
|
|
115
|
+
if (err) return err;
|
|
116
|
+
return fetchWithCredentials(`/${encodeURIComponent(agencyId)}/members`);
|
|
117
|
+
}
|
|
118
|
+
async function addMember(agencyId, data) {
|
|
119
|
+
const err = validateIds([agencyId, "agencyId"]);
|
|
120
|
+
if (err) return err;
|
|
121
|
+
return fetchWithCredentials(`/${encodeURIComponent(agencyId)}/members`, {
|
|
122
|
+
method: "POST",
|
|
123
|
+
body: JSON.stringify(data)
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
async function updateMember(agencyId, userId, data) {
|
|
127
|
+
const err = validateIds([agencyId, "agencyId"], [userId, "userId"]);
|
|
128
|
+
if (err) return err;
|
|
129
|
+
return fetchWithCredentials(
|
|
130
|
+
`/${encodeURIComponent(agencyId)}/members/${encodeURIComponent(userId)}`,
|
|
131
|
+
{ method: "PUT", body: JSON.stringify(data) }
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
async function removeMember(agencyId, userId) {
|
|
135
|
+
const err = validateIds([agencyId, "agencyId"], [userId, "userId"]);
|
|
136
|
+
if (err) return err;
|
|
137
|
+
return fetchWithCredentials(
|
|
138
|
+
`/${encodeURIComponent(agencyId)}/members/${encodeURIComponent(userId)}`,
|
|
139
|
+
{ method: "DELETE" }
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
async function getOrganizations(agencyId) {
|
|
143
|
+
const err = validateIds([agencyId, "agencyId"]);
|
|
144
|
+
if (err) return err;
|
|
145
|
+
return fetchWithCredentials(`/${encodeURIComponent(agencyId)}/organizations`);
|
|
146
|
+
}
|
|
147
|
+
async function linkOrganization(agencyId, data) {
|
|
148
|
+
const err = validateIds([agencyId, "agencyId"]);
|
|
149
|
+
if (err) return err;
|
|
150
|
+
return fetchWithCredentials(`/${encodeURIComponent(agencyId)}/organizations`, {
|
|
151
|
+
method: "POST",
|
|
152
|
+
body: JSON.stringify(data)
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
async function unlinkOrganization(agencyId, orgId) {
|
|
156
|
+
const err = validateIds([agencyId, "agencyId"], [orgId, "orgId"]);
|
|
157
|
+
if (err) return err;
|
|
158
|
+
return fetchWithCredentials(
|
|
159
|
+
`/${encodeURIComponent(agencyId)}/organizations/${encodeURIComponent(orgId)}`,
|
|
160
|
+
{ method: "DELETE" }
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
async function getSubAgencies(agencyId) {
|
|
164
|
+
const err = validateIds([agencyId, "agencyId"]);
|
|
165
|
+
if (err) return err;
|
|
166
|
+
return fetchWithCredentials(`/${encodeURIComponent(agencyId)}/sub-agencies`);
|
|
167
|
+
}
|
|
168
|
+
async function createSubAgency(agencyId, data) {
|
|
169
|
+
const err = validateIds([agencyId, "agencyId"]);
|
|
170
|
+
if (err) return err;
|
|
171
|
+
return fetchWithCredentials(`/${encodeURIComponent(agencyId)}/sub-agencies`, {
|
|
172
|
+
method: "POST",
|
|
173
|
+
body: JSON.stringify(data)
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
async function getHierarchy(agencyId) {
|
|
177
|
+
const err = validateIds([agencyId, "agencyId"]);
|
|
178
|
+
if (err) return err;
|
|
179
|
+
return fetchWithCredentials(`/${encodeURIComponent(agencyId)}/hierarchy`);
|
|
180
|
+
}
|
|
181
|
+
async function getBranding(agencyId) {
|
|
182
|
+
const err = validateIds([agencyId, "agencyId"]);
|
|
183
|
+
if (err) return err;
|
|
184
|
+
return fetchWithCredentials(`/${encodeURIComponent(agencyId)}/branding`);
|
|
185
|
+
}
|
|
186
|
+
async function updateBranding(agencyId, data) {
|
|
187
|
+
const err = validateIds([agencyId, "agencyId"]);
|
|
188
|
+
if (err) return err;
|
|
189
|
+
return fetchWithCredentials(`/${encodeURIComponent(agencyId)}/branding`, {
|
|
190
|
+
method: "PUT",
|
|
191
|
+
body: JSON.stringify(data)
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
async function uploadAsset(agencyId, file, type) {
|
|
195
|
+
const validationErr = validateIds([agencyId, "agencyId"]);
|
|
196
|
+
if (validationErr) return validationErr;
|
|
197
|
+
const formData = new FormData();
|
|
198
|
+
formData.append("file", file);
|
|
199
|
+
formData.append("type", type);
|
|
200
|
+
return fetchWithCredentials(
|
|
201
|
+
`/${encodeURIComponent(agencyId)}/branding/upload`,
|
|
202
|
+
{
|
|
203
|
+
method: "POST",
|
|
204
|
+
body: formData
|
|
205
|
+
}
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
async function getDomain(agencyId) {
|
|
209
|
+
const err = validateIds([agencyId, "agencyId"]);
|
|
210
|
+
if (err) return err;
|
|
211
|
+
return fetchWithCredentials(`/${encodeURIComponent(agencyId)}/domain`);
|
|
212
|
+
}
|
|
213
|
+
async function setupDomain(agencyId, data) {
|
|
214
|
+
const err = validateIds([agencyId, "agencyId"]);
|
|
215
|
+
if (err) return err;
|
|
216
|
+
return fetchWithCredentials(`/${encodeURIComponent(agencyId)}/domain`, {
|
|
217
|
+
method: "POST",
|
|
218
|
+
body: JSON.stringify(data)
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
async function removeDomain(agencyId) {
|
|
222
|
+
const err = validateIds([agencyId, "agencyId"]);
|
|
223
|
+
if (err) return err;
|
|
224
|
+
return fetchWithCredentials(`/${encodeURIComponent(agencyId)}/domain`, {
|
|
225
|
+
method: "DELETE"
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
async function verifyDomain(agencyId) {
|
|
229
|
+
const err = validateIds([agencyId, "agencyId"]);
|
|
230
|
+
if (err) return err;
|
|
231
|
+
return fetchWithCredentials(`/${encodeURIComponent(agencyId)}/domain/verify`, {
|
|
232
|
+
method: "POST"
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
async function getEmailSettings(agencyId) {
|
|
236
|
+
const err = validateIds([agencyId, "agencyId"]);
|
|
237
|
+
if (err) return err;
|
|
238
|
+
return fetchWithCredentials(`/${encodeURIComponent(agencyId)}/email-settings`);
|
|
239
|
+
}
|
|
240
|
+
async function updateEmailSettings(agencyId, data) {
|
|
241
|
+
const err = validateIds([agencyId, "agencyId"]);
|
|
242
|
+
if (err) return err;
|
|
243
|
+
return fetchWithCredentials(`/${encodeURIComponent(agencyId)}/email-settings`, {
|
|
244
|
+
method: "PUT",
|
|
245
|
+
body: JSON.stringify(data)
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
async function getBilling(agencyId) {
|
|
249
|
+
const err = validateIds([agencyId, "agencyId"]);
|
|
250
|
+
if (err) return err;
|
|
251
|
+
return fetchWithCredentials(`/${encodeURIComponent(agencyId)}/billing`);
|
|
252
|
+
}
|
|
253
|
+
async function updateBilling(agencyId, data) {
|
|
254
|
+
const err = validateIds([agencyId, "agencyId"]);
|
|
255
|
+
if (err) return err;
|
|
256
|
+
return fetchWithCredentials(`/${encodeURIComponent(agencyId)}/billing`, {
|
|
257
|
+
method: "PUT",
|
|
258
|
+
body: JSON.stringify(data)
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
async function getBillingSummary(agencyId) {
|
|
262
|
+
const err = validateIds([agencyId, "agencyId"]);
|
|
263
|
+
if (err) return err;
|
|
264
|
+
return fetchWithCredentials(`/${encodeURIComponent(agencyId)}/billing-summary`);
|
|
265
|
+
}
|
|
266
|
+
async function getInvoices(agencyId) {
|
|
267
|
+
const err = validateIds([agencyId, "agencyId"]);
|
|
268
|
+
if (err) return err;
|
|
269
|
+
return fetchWithCredentials(`/${encodeURIComponent(agencyId)}/invoices`);
|
|
270
|
+
}
|
|
271
|
+
async function getRevenue(agencyId) {
|
|
272
|
+
const err = validateIds([agencyId, "agencyId"]);
|
|
273
|
+
if (err) return err;
|
|
274
|
+
return fetchWithCredentials(`/${encodeURIComponent(agencyId)}/revenue`);
|
|
275
|
+
}
|
|
276
|
+
return {
|
|
277
|
+
// Agency CRUD
|
|
278
|
+
getAgencies,
|
|
279
|
+
getAgency,
|
|
280
|
+
updateAgency,
|
|
281
|
+
// Members
|
|
282
|
+
getMembers,
|
|
283
|
+
addMember,
|
|
284
|
+
updateMember,
|
|
285
|
+
removeMember,
|
|
286
|
+
// Organizations
|
|
287
|
+
getOrganizations,
|
|
288
|
+
linkOrganization,
|
|
289
|
+
unlinkOrganization,
|
|
290
|
+
// Sub-Agencies
|
|
291
|
+
getSubAgencies,
|
|
292
|
+
createSubAgency,
|
|
293
|
+
// Hierarchy
|
|
294
|
+
getHierarchy,
|
|
295
|
+
// Branding
|
|
296
|
+
getBranding,
|
|
297
|
+
updateBranding,
|
|
298
|
+
uploadAsset,
|
|
299
|
+
// Domain
|
|
300
|
+
getDomain,
|
|
301
|
+
setupDomain,
|
|
302
|
+
removeDomain,
|
|
303
|
+
verifyDomain,
|
|
304
|
+
// Email Settings
|
|
305
|
+
getEmailSettings,
|
|
306
|
+
updateEmailSettings,
|
|
307
|
+
// Billing
|
|
308
|
+
getBilling,
|
|
309
|
+
updateBilling,
|
|
310
|
+
getBillingSummary,
|
|
311
|
+
getInvoices,
|
|
312
|
+
getRevenue
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
export { FFID_AGENCY_ERROR_CODES, createFFIDAgencyClient };
|