@paid-ai/paid-node 0.4.1 → 0.5.0-alpha2
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/cjs/Client.js +2 -2
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/index.js +2 -1
- package/dist/cjs/integrations/adapters/nextjs/base-adapter.d.ts +10 -0
- package/dist/cjs/integrations/adapters/nextjs/base-adapter.js +58 -0
- package/dist/cjs/integrations/adapters/nextjs/index.d.ts +5 -0
- package/dist/cjs/integrations/adapters/nextjs/index.js +17 -0
- package/dist/cjs/integrations/adapters/nextjs/routes.d.ts +225 -0
- package/dist/cjs/integrations/adapters/nextjs/routes.js +246 -0
- package/dist/cjs/integrations/client.d.ts +49 -0
- package/dist/cjs/integrations/client.js +93 -0
- package/dist/cjs/integrations/controllers/billing.d.ts +196 -0
- package/dist/cjs/integrations/controllers/billing.js +267 -0
- package/dist/cjs/integrations/controllers/contacts.d.ts +66 -0
- package/dist/cjs/integrations/controllers/contacts.js +145 -0
- package/dist/cjs/integrations/controllers/customers.d.ts +96 -0
- package/dist/cjs/integrations/controllers/customers.js +206 -0
- package/dist/cjs/integrations/controllers/index.d.ts +6 -0
- package/dist/cjs/integrations/controllers/index.js +30 -0
- package/dist/cjs/integrations/controllers/invoices.d.ts +40 -0
- package/dist/cjs/integrations/controllers/invoices.js +72 -0
- package/dist/cjs/integrations/controllers/orders.d.ts +72 -0
- package/dist/cjs/integrations/controllers/orders.js +194 -0
- package/dist/cjs/integrations/controllers/provision-users.d.ts +62 -0
- package/dist/cjs/integrations/controllers/provision-users.js +130 -0
- package/dist/cjs/integrations/index.d.ts +29 -0
- package/dist/cjs/integrations/index.js +44 -0
- package/dist/cjs/integrations/types.d.ts +119 -0
- package/dist/cjs/integrations/types.js +2 -0
- package/dist/cjs/integrations/utils/api-fetch.d.ts +18 -0
- package/dist/cjs/integrations/utils/api-fetch.js +53 -0
- package/dist/cjs/integrations/utils/base-handler.d.ts +79 -0
- package/dist/cjs/integrations/utils/base-handler.js +168 -0
- package/dist/cjs/integrations/utils/create-adapter.d.ts +29 -0
- package/dist/cjs/integrations/utils/create-adapter.js +56 -0
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/esm/Client.mjs +2 -2
- package/dist/esm/index.d.mts +1 -0
- package/dist/esm/index.mjs +1 -0
- package/dist/esm/integrations/adapters/nextjs/base-adapter.d.mts +10 -0
- package/dist/esm/integrations/adapters/nextjs/base-adapter.mjs +55 -0
- package/dist/esm/integrations/adapters/nextjs/index.d.mts +5 -0
- package/dist/esm/integrations/adapters/nextjs/index.mjs +5 -0
- package/dist/esm/integrations/adapters/nextjs/routes.d.mts +225 -0
- package/dist/esm/integrations/adapters/nextjs/routes.mjs +235 -0
- package/dist/esm/integrations/client.d.mts +49 -0
- package/dist/esm/integrations/client.mjs +89 -0
- package/dist/esm/integrations/controllers/billing.d.mts +196 -0
- package/dist/esm/integrations/controllers/billing.mjs +259 -0
- package/dist/esm/integrations/controllers/contacts.d.mts +66 -0
- package/dist/esm/integrations/controllers/contacts.mjs +140 -0
- package/dist/esm/integrations/controllers/customers.d.mts +96 -0
- package/dist/esm/integrations/controllers/customers.mjs +199 -0
- package/dist/esm/integrations/controllers/index.d.mts +6 -0
- package/dist/esm/integrations/controllers/index.mjs +6 -0
- package/dist/esm/integrations/controllers/invoices.d.mts +40 -0
- package/dist/esm/integrations/controllers/invoices.mjs +68 -0
- package/dist/esm/integrations/controllers/orders.d.mts +72 -0
- package/dist/esm/integrations/controllers/orders.mjs +189 -0
- package/dist/esm/integrations/controllers/provision-users.d.mts +62 -0
- package/dist/esm/integrations/controllers/provision-users.mjs +126 -0
- package/dist/esm/integrations/index.d.mts +29 -0
- package/dist/esm/integrations/index.mjs +28 -0
- package/dist/esm/integrations/types.d.mts +119 -0
- package/dist/esm/integrations/types.mjs +1 -0
- package/dist/esm/integrations/utils/api-fetch.d.mts +18 -0
- package/dist/esm/integrations/utils/api-fetch.mjs +50 -0
- package/dist/esm/integrations/utils/base-handler.d.mts +79 -0
- package/dist/esm/integrations/utils/base-handler.mjs +164 -0
- package/dist/esm/integrations/utils/create-adapter.d.mts +29 -0
- package/dist/esm/integrations/utils/create-adapter.mjs +53 -0
- package/dist/esm/version.d.mts +1 -1
- package/dist/esm/version.mjs +1 -1
- package/package.json +32 -3
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provisioning helpers for Paid SDK
|
|
3
|
+
* Orchestrates customer + contact + order creation in one operation
|
|
4
|
+
*/
|
|
5
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
6
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
7
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
8
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
9
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
10
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
11
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
12
|
+
});
|
|
13
|
+
};
|
|
14
|
+
import { createCustomerWithDefaults } from "./customers.mjs";
|
|
15
|
+
import { createContactWithDefaults } from "./contacts.mjs";
|
|
16
|
+
import { createOrderWithDefaults } from "./orders.mjs";
|
|
17
|
+
import { createHandler } from "../utils/base-handler.mjs";
|
|
18
|
+
/**
|
|
19
|
+
* Provision a new user with customer, contact, and optionally order
|
|
20
|
+
*
|
|
21
|
+
* This is a high-level helper that orchestrates the creation of all
|
|
22
|
+
* necessary Paid entities for a new user in one operation.
|
|
23
|
+
*
|
|
24
|
+
* @param client - PaidClient instance
|
|
25
|
+
* @param config - Provisioning configuration
|
|
26
|
+
* @returns Result with created customer, contact, and optional order
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* const result = await provisionNewUser(client, {
|
|
31
|
+
* customer: {
|
|
32
|
+
* externalId: 'user-123',
|
|
33
|
+
* email: 'user@example.com'
|
|
34
|
+
* },
|
|
35
|
+
* agentExternalId: 'agent-prod'
|
|
36
|
+
* });
|
|
37
|
+
* // Creates customer, contact, and order with defaults
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export function provisionNewUser(client, config) {
|
|
41
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
42
|
+
var _a, _b, _c, _d, _e, _f;
|
|
43
|
+
const customer = yield createCustomerWithDefaults(client, config.customer);
|
|
44
|
+
const contactData = {
|
|
45
|
+
customerExternalId: config.customer.externalId,
|
|
46
|
+
email: ((_a = config.contact) === null || _a === void 0 ? void 0 : _a.email) || config.customer.email,
|
|
47
|
+
firstName: (_b = config.contact) === null || _b === void 0 ? void 0 : _b.firstName,
|
|
48
|
+
lastName: (_c = config.contact) === null || _c === void 0 ? void 0 : _c.lastName,
|
|
49
|
+
phone: (_d = config.contact) === null || _d === void 0 ? void 0 : _d.phone,
|
|
50
|
+
billingAddress: (_e = config.contact) === null || _e === void 0 ? void 0 : _e.billingAddress,
|
|
51
|
+
salutation: (_f = config.contact) === null || _f === void 0 ? void 0 : _f.salutation,
|
|
52
|
+
};
|
|
53
|
+
const contact = yield createContactWithDefaults(client, contactData);
|
|
54
|
+
let order;
|
|
55
|
+
if (config.agentExternalId) {
|
|
56
|
+
order = yield createOrderWithDefaults(client, {
|
|
57
|
+
customerId: customer.id,
|
|
58
|
+
customerExternalId: config.customer.externalId,
|
|
59
|
+
agentExternalId: config.agentExternalId,
|
|
60
|
+
billingContactId: contact.id,
|
|
61
|
+
}, config.orderOptions);
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
customer,
|
|
65
|
+
contact,
|
|
66
|
+
order,
|
|
67
|
+
};
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Create a framework-agnostic handler for user provisioning
|
|
72
|
+
*
|
|
73
|
+
* This handler can be used with any framework adapter.
|
|
74
|
+
* Accepts partial provisioning configuration and fills in defaults for missing fields.
|
|
75
|
+
*
|
|
76
|
+
* @param orderOptions - Optional default order creation options
|
|
77
|
+
* @param defaultAgentExternalId - Optional default agent external ID
|
|
78
|
+
* @returns Handler function
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```typescript
|
|
82
|
+
* // Minimal - just externalId and email
|
|
83
|
+
* {
|
|
84
|
+
* customer: { externalId: 'org-123', email: 'user@example.com' }
|
|
85
|
+
* }
|
|
86
|
+
*
|
|
87
|
+
* // With full customer data
|
|
88
|
+
* {
|
|
89
|
+
* customer: {
|
|
90
|
+
* externalId: 'org-123',
|
|
91
|
+
* email: 'user@example.com',
|
|
92
|
+
* name: 'Acme Corp',
|
|
93
|
+
* billingAddress: { line1: '123 Main St', city: 'NYC', ... }
|
|
94
|
+
* },
|
|
95
|
+
* contact: {
|
|
96
|
+
* firstName: 'John',
|
|
97
|
+
* lastName: 'Doe'
|
|
98
|
+
* }
|
|
99
|
+
* }
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
export function createProvisioningHandler(orderOptions, defaultAgentExternalId) {
|
|
103
|
+
return createHandler((client, body) => __awaiter(this, void 0, void 0, function* () {
|
|
104
|
+
if (!body.customer) {
|
|
105
|
+
return {
|
|
106
|
+
success: false,
|
|
107
|
+
error: "'customer' is required",
|
|
108
|
+
status: 400,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
const effectiveAgentId = body.agentExternalId ||
|
|
112
|
+
defaultAgentExternalId ||
|
|
113
|
+
process.env.NEXT_PUBLIC_PAID_AGENT_ID ||
|
|
114
|
+
process.env.PAID_AGENT_ID;
|
|
115
|
+
const result = yield provisionNewUser(client, {
|
|
116
|
+
customer: body.customer,
|
|
117
|
+
contact: body.contact,
|
|
118
|
+
agentExternalId: effectiveAgentId,
|
|
119
|
+
orderOptions: body.orderOptions || orderOptions,
|
|
120
|
+
});
|
|
121
|
+
return {
|
|
122
|
+
success: true,
|
|
123
|
+
data: result,
|
|
124
|
+
};
|
|
125
|
+
}), { requiredFields: [] });
|
|
126
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Paid SDK Integrations
|
|
3
|
+
*
|
|
4
|
+
* This module provides simplified helpers for common Paid operations
|
|
5
|
+
* with defaults and reduced boilerplate.
|
|
6
|
+
*
|
|
7
|
+
* @module integrations
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { PaidClient } from '@paid-ai/paid-node';
|
|
12
|
+
* import { createCustomerWithDefaults, createOrderWithDefaults } from '@paid-ai/paid-node/integrations';
|
|
13
|
+
*
|
|
14
|
+
* const client = new PaidClient({ token: 'pk_xxx' });
|
|
15
|
+
*
|
|
16
|
+
* const customer = await createCustomerWithDefaults(client, {
|
|
17
|
+
* externalId: 'user-123',
|
|
18
|
+
* email: 'user@example.com'
|
|
19
|
+
* });
|
|
20
|
+
*
|
|
21
|
+
* const order = await createOrderWithDefaults(client, {
|
|
22
|
+
* customerId: customer.id,
|
|
23
|
+
* customerExternalId: 'user-123',
|
|
24
|
+
* agentExternalId: 'agent-prod'
|
|
25
|
+
* });
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export * from "./controllers/index.mjs";
|
|
29
|
+
export type { Address, CustomerData, CompleteCustomerData, ContactData, OrderConfig, CompleteOrderConfig, OrderLineConfig, OrderOptions as HelperOptions, CustomerCreationResult, ContactCreationResult, OrderCreationResult, ProvisioningConfig, ProvisioningResult, } from "./types.mjs";
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Paid SDK Integrations
|
|
3
|
+
*
|
|
4
|
+
* This module provides simplified helpers for common Paid operations
|
|
5
|
+
* with defaults and reduced boilerplate.
|
|
6
|
+
*
|
|
7
|
+
* @module integrations
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { PaidClient } from '@paid-ai/paid-node';
|
|
12
|
+
* import { createCustomerWithDefaults, createOrderWithDefaults } from '@paid-ai/paid-node/integrations';
|
|
13
|
+
*
|
|
14
|
+
* const client = new PaidClient({ token: 'pk_xxx' });
|
|
15
|
+
*
|
|
16
|
+
* const customer = await createCustomerWithDefaults(client, {
|
|
17
|
+
* externalId: 'user-123',
|
|
18
|
+
* email: 'user@example.com'
|
|
19
|
+
* });
|
|
20
|
+
*
|
|
21
|
+
* const order = await createOrderWithDefaults(client, {
|
|
22
|
+
* customerId: customer.id,
|
|
23
|
+
* customerExternalId: 'user-123',
|
|
24
|
+
* agentExternalId: 'agent-prod'
|
|
25
|
+
* });
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export * from "./controllers/index.mjs";
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
export interface Address {
|
|
2
|
+
line1?: string;
|
|
3
|
+
line2?: string;
|
|
4
|
+
city?: string;
|
|
5
|
+
state?: string;
|
|
6
|
+
zipCode?: string;
|
|
7
|
+
country?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface CustomerData {
|
|
10
|
+
externalId: string;
|
|
11
|
+
email?: string;
|
|
12
|
+
name?: string;
|
|
13
|
+
firstName?: string;
|
|
14
|
+
lastName?: string;
|
|
15
|
+
company?: string;
|
|
16
|
+
phone?: string;
|
|
17
|
+
billingAddress?: Address;
|
|
18
|
+
metadata?: Record<string, any>;
|
|
19
|
+
}
|
|
20
|
+
export interface CompleteCustomerData {
|
|
21
|
+
externalId: string;
|
|
22
|
+
email: string;
|
|
23
|
+
name: string;
|
|
24
|
+
firstName: string;
|
|
25
|
+
lastName: string;
|
|
26
|
+
company: string;
|
|
27
|
+
phone: string;
|
|
28
|
+
billingAddress: Required<Address>;
|
|
29
|
+
metadata: Record<string, any>;
|
|
30
|
+
}
|
|
31
|
+
export interface ContactData {
|
|
32
|
+
customerExternalId: string;
|
|
33
|
+
email?: string;
|
|
34
|
+
firstName?: string;
|
|
35
|
+
lastName?: string;
|
|
36
|
+
phone?: string;
|
|
37
|
+
billingAddress?: Address;
|
|
38
|
+
salutation?: "Mr" | "Mrs" | "Ms" | "Dr" | "Prof";
|
|
39
|
+
}
|
|
40
|
+
export interface OrderConfig {
|
|
41
|
+
customerId: string;
|
|
42
|
+
customerExternalId: string;
|
|
43
|
+
billingContactId?: string;
|
|
44
|
+
agentExternalId: string;
|
|
45
|
+
name?: string;
|
|
46
|
+
description?: string;
|
|
47
|
+
startDate?: string;
|
|
48
|
+
endDate?: string;
|
|
49
|
+
currency?: string;
|
|
50
|
+
orderLines?: OrderLineConfig[];
|
|
51
|
+
}
|
|
52
|
+
export interface OrderLineConfig {
|
|
53
|
+
agentExternalId: string;
|
|
54
|
+
name?: string;
|
|
55
|
+
description?: string;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Note: orderLines is not required because if agentExternalId is provided,
|
|
59
|
+
* order lines can be derived from that agent.
|
|
60
|
+
*/
|
|
61
|
+
export interface CompleteOrderConfig {
|
|
62
|
+
customerId: string;
|
|
63
|
+
customerExternalId: string;
|
|
64
|
+
billingContactId?: string;
|
|
65
|
+
agentExternalId: string;
|
|
66
|
+
name: string;
|
|
67
|
+
description: string;
|
|
68
|
+
startDate: string;
|
|
69
|
+
endDate: string;
|
|
70
|
+
currency: string;
|
|
71
|
+
orderLines: OrderLineConfig[];
|
|
72
|
+
}
|
|
73
|
+
export interface OrderOptions {
|
|
74
|
+
/**
|
|
75
|
+
* Whether to auto-activate orders after creation
|
|
76
|
+
* @default true
|
|
77
|
+
*/
|
|
78
|
+
autoActivate?: boolean;
|
|
79
|
+
/**
|
|
80
|
+
* Duration in days for order end date (if not specified)
|
|
81
|
+
* @default 365
|
|
82
|
+
*/
|
|
83
|
+
defaultDurationDays?: number;
|
|
84
|
+
}
|
|
85
|
+
export interface CustomerCreationResult {
|
|
86
|
+
id: string;
|
|
87
|
+
externalId: string;
|
|
88
|
+
}
|
|
89
|
+
export interface ContactCreationResult {
|
|
90
|
+
id: string;
|
|
91
|
+
}
|
|
92
|
+
export interface OrderCreationResult {
|
|
93
|
+
id: string;
|
|
94
|
+
creationState?: string;
|
|
95
|
+
}
|
|
96
|
+
export interface ProvisioningConfig {
|
|
97
|
+
/**
|
|
98
|
+
* Customer data (externalId required)
|
|
99
|
+
*/
|
|
100
|
+
customer: CustomerData;
|
|
101
|
+
/**
|
|
102
|
+
* Contact data (optional, will use customer email if not provided)
|
|
103
|
+
*/
|
|
104
|
+
contact?: Partial<ContactData>;
|
|
105
|
+
/**
|
|
106
|
+
* Agent external ID for order creation (optional)
|
|
107
|
+
* If not provided, no order will be created
|
|
108
|
+
*/
|
|
109
|
+
agentExternalId?: string;
|
|
110
|
+
/**
|
|
111
|
+
* Options for order creation
|
|
112
|
+
*/
|
|
113
|
+
orderOptions?: OrderOptions;
|
|
114
|
+
}
|
|
115
|
+
export interface ProvisioningResult {
|
|
116
|
+
customer: CustomerCreationResult;
|
|
117
|
+
contact: ContactCreationResult;
|
|
118
|
+
order?: OrderCreationResult;
|
|
119
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export interface PaidApiConfig {
|
|
2
|
+
apiUrl: string;
|
|
3
|
+
apiKey: string;
|
|
4
|
+
organizationId: string;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Make a direct API call to ap-signals
|
|
8
|
+
*
|
|
9
|
+
* @param config - API configuration with organization context
|
|
10
|
+
* @param endpoint - API endpoint path (will be appended to apiUrl)
|
|
11
|
+
* @param options - Fetch options (method, body)
|
|
12
|
+
* @returns Parsed JSON response
|
|
13
|
+
* @throws Error if request fails
|
|
14
|
+
*/
|
|
15
|
+
export declare function paidApiFetch<T = any>(config: PaidApiConfig, endpoint: string, options?: {
|
|
16
|
+
method?: "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
|
|
17
|
+
body?: any;
|
|
18
|
+
}): Promise<T>;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Make a direct API call to ap-signals
|
|
12
|
+
*
|
|
13
|
+
* @param config - API configuration with organization context
|
|
14
|
+
* @param endpoint - API endpoint path (will be appended to apiUrl)
|
|
15
|
+
* @param options - Fetch options (method, body)
|
|
16
|
+
* @returns Parsed JSON response
|
|
17
|
+
* @throws Error if request fails
|
|
18
|
+
*/
|
|
19
|
+
export function paidApiFetch(config, endpoint, options) {
|
|
20
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
21
|
+
if (!config.organizationId) {
|
|
22
|
+
throw new Error("organizationId is required");
|
|
23
|
+
}
|
|
24
|
+
if (!config.apiKey) {
|
|
25
|
+
throw new Error("apiKey is required");
|
|
26
|
+
}
|
|
27
|
+
if (!config.apiUrl) {
|
|
28
|
+
throw new Error("apiUrl is required");
|
|
29
|
+
}
|
|
30
|
+
try {
|
|
31
|
+
const response = yield fetch(`${config.apiUrl}${endpoint}`, {
|
|
32
|
+
method: (options === null || options === void 0 ? void 0 : options.method) || "GET",
|
|
33
|
+
headers: {
|
|
34
|
+
Authorization: `Bearer ${config.apiKey}`,
|
|
35
|
+
"Content-Type": "application/json",
|
|
36
|
+
},
|
|
37
|
+
body: (options === null || options === void 0 ? void 0 : options.body) ? JSON.stringify(options.body) : undefined,
|
|
38
|
+
});
|
|
39
|
+
if (!response.ok) {
|
|
40
|
+
const errorText = yield response.text();
|
|
41
|
+
throw new Error(`API request failed: ${errorText}`);
|
|
42
|
+
}
|
|
43
|
+
return yield response.json();
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
console.error("Error making Paid API request:", error);
|
|
47
|
+
throw error;
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Framework-agnostic base handler for Paid SDK integrations
|
|
3
|
+
* Provides consistent request handling, validation, and error handling
|
|
4
|
+
*/
|
|
5
|
+
import type { PaidClient } from "../../Client.mjs";
|
|
6
|
+
import { type PaidClientConfig } from "../client.mjs";
|
|
7
|
+
export interface BaseRequestContext<TBody = any, TParams = Record<string, string>> {
|
|
8
|
+
body: TBody;
|
|
9
|
+
headers: Record<string, string>;
|
|
10
|
+
method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
|
|
11
|
+
params?: TParams;
|
|
12
|
+
query?: Record<string, string>;
|
|
13
|
+
}
|
|
14
|
+
export interface BaseResponseContext {
|
|
15
|
+
json: (data: any, status?: number) => any;
|
|
16
|
+
error: (message: string, status: number) => any;
|
|
17
|
+
}
|
|
18
|
+
export interface BaseHandlerConfig extends PaidClientConfig {
|
|
19
|
+
validate?: (body: any) => Promise<void> | void;
|
|
20
|
+
transformRequest?: (body: any) => any;
|
|
21
|
+
transformResponse?: (data: any) => any;
|
|
22
|
+
}
|
|
23
|
+
export type HandlerFunction<TRequest, TResponse> = (client: PaidClient, request: TRequest, params?: Record<string, string>, organizationId?: string) => Promise<{
|
|
24
|
+
success: boolean;
|
|
25
|
+
data?: TResponse;
|
|
26
|
+
error?: string;
|
|
27
|
+
status?: number;
|
|
28
|
+
}>;
|
|
29
|
+
/**
|
|
30
|
+
* Creates a framework-agnostic handler with automatic client initialization,
|
|
31
|
+
* validation, and error handling
|
|
32
|
+
*
|
|
33
|
+
* @param handler - Business logic handler function
|
|
34
|
+
* @param options - Handler configuration options
|
|
35
|
+
* @returns Framework-agnostic handler function
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```typescript
|
|
39
|
+
* const customersHandler = createHandler(
|
|
40
|
+
* async (client, body) => {
|
|
41
|
+
* const customer = await createCustomerWithDefaults(client, body);
|
|
42
|
+
* return { success: true, data: customer };
|
|
43
|
+
* },
|
|
44
|
+
* { requiredFields: ['externalId'] }
|
|
45
|
+
* );
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export declare function createHandler<TRequest = any, TResponse = any>(handler: HandlerFunction<TRequest, TResponse>, options?: {
|
|
49
|
+
requiredFields?: (keyof TRequest)[];
|
|
50
|
+
allowedMethods?: Array<"GET" | "POST" | "PUT" | "DELETE" | "PATCH">;
|
|
51
|
+
requireOrganizationId?: boolean;
|
|
52
|
+
}): (request: BaseRequestContext<TRequest>, response: BaseResponseContext, config?: BaseHandlerConfig) => Promise<any>;
|
|
53
|
+
/**
|
|
54
|
+
* Helper to create a fetch-based handler for simple API calls
|
|
55
|
+
*
|
|
56
|
+
* @param endpoint - Function that generates the endpoint URL from org ID and request body
|
|
57
|
+
* @param options - Optional method, request/response transformations
|
|
58
|
+
* @returns Handler function that makes the API call
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```typescript
|
|
62
|
+
* const activateHandler = createHandler(
|
|
63
|
+
* createFetchHandler(
|
|
64
|
+
* (orgId, req) => `/api/organizations/${orgId}/orders/${req.orderId}/activate-sync`,
|
|
65
|
+
* {
|
|
66
|
+
* method: "POST",
|
|
67
|
+
* transformRequest: (req) => ({ confirmationToken: req.confirmationToken }),
|
|
68
|
+
* transformResponse: (data) => ({ order: data.order, billing: data.billing })
|
|
69
|
+
* }
|
|
70
|
+
* ),
|
|
71
|
+
* { requireOrganizationId: true }
|
|
72
|
+
* );
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
export declare function createFetchHandler<TRequest, TResponse>(endpoint: (orgId: string, body: TRequest, params?: Record<string, string>) => string, options?: {
|
|
76
|
+
method?: "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
|
|
77
|
+
transformRequest?: (body: TRequest) => any;
|
|
78
|
+
transformResponse?: (data: any) => TResponse;
|
|
79
|
+
}): HandlerFunction<TRequest, TResponse>;
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Framework-agnostic base handler for Paid SDK integrations
|
|
3
|
+
* Provides consistent request handling, validation, and error handling
|
|
4
|
+
*/
|
|
5
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
6
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
7
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
8
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
9
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
10
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
11
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
12
|
+
});
|
|
13
|
+
};
|
|
14
|
+
import { initializePaidClient, getOrganizationId } from "../client.mjs";
|
|
15
|
+
/**
|
|
16
|
+
* Creates a framework-agnostic handler with automatic client initialization,
|
|
17
|
+
* validation, and error handling
|
|
18
|
+
*
|
|
19
|
+
* @param handler - Business logic handler function
|
|
20
|
+
* @param options - Handler configuration options
|
|
21
|
+
* @returns Framework-agnostic handler function
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* const customersHandler = createHandler(
|
|
26
|
+
* async (client, body) => {
|
|
27
|
+
* const customer = await createCustomerWithDefaults(client, body);
|
|
28
|
+
* return { success: true, data: customer };
|
|
29
|
+
* },
|
|
30
|
+
* { requiredFields: ['externalId'] }
|
|
31
|
+
* );
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export function createHandler(handler, options = {}) {
|
|
35
|
+
const { requiredFields = [], allowedMethods = ["POST"], requireOrganizationId = false } = options;
|
|
36
|
+
return function (request_1, response_1) {
|
|
37
|
+
return __awaiter(this, arguments, void 0, function* (request, response, config = {}) {
|
|
38
|
+
if (!allowedMethods.includes(request.method)) {
|
|
39
|
+
return response.error("Method not allowed", 405);
|
|
40
|
+
}
|
|
41
|
+
for (const field of requiredFields) {
|
|
42
|
+
if (!request.body[field]) {
|
|
43
|
+
return response.error(`${String(field)} is required`, 400);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (config.validate) {
|
|
47
|
+
try {
|
|
48
|
+
yield config.validate(request.body);
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
return response.error(error instanceof Error ? error.message : "Validation failed", 400);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
try {
|
|
55
|
+
const client = initializePaidClient(config);
|
|
56
|
+
let organizationId;
|
|
57
|
+
if (requireOrganizationId) {
|
|
58
|
+
const orgId = yield getOrganizationId(config);
|
|
59
|
+
if (!orgId) {
|
|
60
|
+
return response.error("Organization not found", 500);
|
|
61
|
+
}
|
|
62
|
+
organizationId = orgId;
|
|
63
|
+
}
|
|
64
|
+
const requestData = config.transformRequest
|
|
65
|
+
? config.transformRequest(request.body)
|
|
66
|
+
: request.body;
|
|
67
|
+
const result = yield handler(client, requestData, request.params, organizationId);
|
|
68
|
+
if (!result.success) {
|
|
69
|
+
return response.error(result.error || "Operation failed", result.status || 500);
|
|
70
|
+
}
|
|
71
|
+
const responseData = config.transformResponse
|
|
72
|
+
? config.transformResponse(result.data)
|
|
73
|
+
: { data: result.data };
|
|
74
|
+
return response.json(responseData, 200);
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
console.error("Handler error:", error);
|
|
78
|
+
if (error instanceof TypeError && error.message.includes("fetch")) {
|
|
79
|
+
return response.error("Network error: Unable to reach Paid API", 503);
|
|
80
|
+
}
|
|
81
|
+
return response.error(error instanceof Error ? error.message : "Internal server error", 500);
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Helper to create a fetch-based handler for simple API calls
|
|
88
|
+
*
|
|
89
|
+
* @param endpoint - Function that generates the endpoint URL from org ID and request body
|
|
90
|
+
* @param options - Optional method, request/response transformations
|
|
91
|
+
* @returns Handler function that makes the API call
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```typescript
|
|
95
|
+
* const activateHandler = createHandler(
|
|
96
|
+
* createFetchHandler(
|
|
97
|
+
* (orgId, req) => `/api/organizations/${orgId}/orders/${req.orderId}/activate-sync`,
|
|
98
|
+
* {
|
|
99
|
+
* method: "POST",
|
|
100
|
+
* transformRequest: (req) => ({ confirmationToken: req.confirmationToken }),
|
|
101
|
+
* transformResponse: (data) => ({ order: data.order, billing: data.billing })
|
|
102
|
+
* }
|
|
103
|
+
* ),
|
|
104
|
+
* { requireOrganizationId: true }
|
|
105
|
+
* );
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
export function createFetchHandler(endpoint, options) {
|
|
109
|
+
return (client, requestBody, params, organizationId) => __awaiter(this, void 0, void 0, function* () {
|
|
110
|
+
var _a, _b;
|
|
111
|
+
if (!organizationId) {
|
|
112
|
+
return {
|
|
113
|
+
success: false,
|
|
114
|
+
error: "Organization ID is required for this operation",
|
|
115
|
+
status: 500,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
try {
|
|
119
|
+
const url = endpoint(organizationId, requestBody, params);
|
|
120
|
+
const body = (options === null || options === void 0 ? void 0 : options.transformRequest)
|
|
121
|
+
? options.transformRequest(requestBody)
|
|
122
|
+
: requestBody;
|
|
123
|
+
const baseUrl = ((_a = client._options) === null || _a === void 0 ? void 0 : _a.baseUrl) || "https://api.agentpaid.io/api/v1";
|
|
124
|
+
const apiKey = (_b = client._options) === null || _b === void 0 ? void 0 : _b.token;
|
|
125
|
+
if (!apiKey) {
|
|
126
|
+
return {
|
|
127
|
+
success: false,
|
|
128
|
+
error: "API key not found in client",
|
|
129
|
+
status: 500,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
const fetchResponse = yield fetch(`${baseUrl}${url}`, {
|
|
133
|
+
method: (options === null || options === void 0 ? void 0 : options.method) || "POST",
|
|
134
|
+
headers: {
|
|
135
|
+
Authorization: `Bearer ${apiKey}`,
|
|
136
|
+
"Content-Type": "application/json",
|
|
137
|
+
},
|
|
138
|
+
body: (options === null || options === void 0 ? void 0 : options.method) !== "GET" ? JSON.stringify(body) : undefined,
|
|
139
|
+
});
|
|
140
|
+
if (!fetchResponse.ok) {
|
|
141
|
+
const errorText = yield fetchResponse.text();
|
|
142
|
+
console.error("API error:", errorText);
|
|
143
|
+
return {
|
|
144
|
+
success: false,
|
|
145
|
+
error: `API request failed: ${errorText}`,
|
|
146
|
+
status: fetchResponse.status,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
const data = yield fetchResponse.json();
|
|
150
|
+
const transformedData = (options === null || options === void 0 ? void 0 : options.transformResponse)
|
|
151
|
+
? options.transformResponse(data)
|
|
152
|
+
: data;
|
|
153
|
+
return { success: true, data: transformedData };
|
|
154
|
+
}
|
|
155
|
+
catch (error) {
|
|
156
|
+
console.error("Fetch error:", error);
|
|
157
|
+
return {
|
|
158
|
+
success: false,
|
|
159
|
+
error: error instanceof Error ? error.message : "Unknown error",
|
|
160
|
+
status: 500,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic adapter factory - eliminates repetitive adapter code
|
|
3
|
+
* Works with any framework (Express, Fastify, Hono, Next.js, etc.)
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Creates a framework adapter for any HTTP framework
|
|
7
|
+
* Automatically extracts headers and converts request/response
|
|
8
|
+
*
|
|
9
|
+
* @param extractors - Framework-specific extraction functions
|
|
10
|
+
* @returns Adapter function that wraps core handlers
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* const nextjsAdapter = createFrameworkAdapter({
|
|
15
|
+
* getBody: async (req) => await req.json(),
|
|
16
|
+
* getHeaders: (req) => Object.fromEntries(req.headers),
|
|
17
|
+
* getMethod: (req) => req.method || "GET",
|
|
18
|
+
* sendJson: (data, status) => NextResponse.json(data, { status })
|
|
19
|
+
* });
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export declare function createFrameworkAdapter<TFrameworkReq = any, TFrameworkRes = any>(extractors: {
|
|
23
|
+
getBody: (req: TFrameworkReq) => any | Promise<any>;
|
|
24
|
+
getHeaders: (req: TFrameworkReq) => Record<string, string>;
|
|
25
|
+
getMethod: (req: TFrameworkReq) => string;
|
|
26
|
+
getParams?: (req: TFrameworkReq, context?: any) => Record<string, string> | Promise<Record<string, string>>;
|
|
27
|
+
getQuery?: (req: TFrameworkReq) => Record<string, string>;
|
|
28
|
+
sendJson: (data: any, status: number) => TFrameworkRes;
|
|
29
|
+
}): (innerHandler: Function) => (req: TFrameworkReq, context?: any, config?: any) => Promise<any>;
|