@paid-ai/paid-node 0.4.0 → 0.5.0-alpha1
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/api/types/Signal.d.ts +5 -0
- 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 +15 -0
- package/dist/cjs/integrations/adapters/nextjs/routes.d.ts +174 -0
- package/dist/cjs/integrations/adapters/nextjs/routes.js +192 -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 +113 -0
- package/dist/cjs/integrations/controllers/billing.js +164 -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 +63 -0
- package/dist/cjs/integrations/controllers/customers.js +150 -0
- package/dist/cjs/integrations/controllers/index.d.ts +6 -0
- package/dist/cjs/integrations/controllers/index.js +26 -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/api/types/Signal.d.mts +5 -0
- 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 +174 -0
- package/dist/esm/integrations/adapters/nextjs/routes.mjs +183 -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 +113 -0
- package/dist/esm/integrations/controllers/billing.mjs +158 -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 +63 -0
- package/dist/esm/integrations/controllers/customers.mjs +145 -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,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>;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic adapter factory - eliminates repetitive adapter code
|
|
3
|
+
* Works with any framework (Express, Fastify, Hono, Next.js, etc.)
|
|
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
|
+
/**
|
|
15
|
+
* Creates a framework adapter for any HTTP framework
|
|
16
|
+
* Automatically extracts headers and converts request/response
|
|
17
|
+
*
|
|
18
|
+
* @param extractors - Framework-specific extraction functions
|
|
19
|
+
* @returns Adapter function that wraps core handlers
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* const nextjsAdapter = createFrameworkAdapter({
|
|
24
|
+
* getBody: async (req) => await req.json(),
|
|
25
|
+
* getHeaders: (req) => Object.fromEntries(req.headers),
|
|
26
|
+
* getMethod: (req) => req.method || "GET",
|
|
27
|
+
* sendJson: (data, status) => NextResponse.json(data, { status })
|
|
28
|
+
* });
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export function createFrameworkAdapter(extractors) {
|
|
32
|
+
return function adaptHandler(innerHandler) {
|
|
33
|
+
return (req, context, config) => __awaiter(this, void 0, void 0, function* () {
|
|
34
|
+
var _a;
|
|
35
|
+
const body = yield extractors.getBody(req);
|
|
36
|
+
const params = extractors.getParams
|
|
37
|
+
? yield extractors.getParams(req, context)
|
|
38
|
+
: undefined;
|
|
39
|
+
const requestContext = {
|
|
40
|
+
body,
|
|
41
|
+
headers: extractors.getHeaders(req),
|
|
42
|
+
method: extractors.getMethod(req),
|
|
43
|
+
params,
|
|
44
|
+
query: (_a = extractors.getQuery) === null || _a === void 0 ? void 0 : _a.call(extractors, req),
|
|
45
|
+
};
|
|
46
|
+
const responseContext = {
|
|
47
|
+
json: (data, status = 200) => extractors.sendJson(data, status),
|
|
48
|
+
error: (message, status) => extractors.sendJson({ error: message }, status),
|
|
49
|
+
};
|
|
50
|
+
return innerHandler(requestContext, responseContext, config);
|
|
51
|
+
});
|
|
52
|
+
};
|
|
53
|
+
}
|
package/dist/esm/version.d.mts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const SDK_VERSION = "0.
|
|
1
|
+
export declare const SDK_VERSION = "0.5.0-alpha1";
|
package/dist/esm/version.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const SDK_VERSION = "0.
|
|
1
|
+
export const SDK_VERSION = "0.5.0-alpha1";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@paid-ai/paid-node",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0-alpha1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"repository": "https://github.com/paid-ai/paid-node",
|
|
6
6
|
"type": "commonjs",
|
|
@@ -80,6 +80,30 @@
|
|
|
80
80
|
},
|
|
81
81
|
"default": "./dist/cjs/vercel-wrapper/index.js"
|
|
82
82
|
},
|
|
83
|
+
"./integrations": {
|
|
84
|
+
"types": "./dist/cjs/integrations/index.d.ts",
|
|
85
|
+
"import": {
|
|
86
|
+
"types": "./dist/esm/integrations/index.d.mts",
|
|
87
|
+
"default": "./dist/esm/integrations/index.mjs"
|
|
88
|
+
},
|
|
89
|
+
"require": {
|
|
90
|
+
"types": "./dist/cjs/integrations/index.d.ts",
|
|
91
|
+
"default": "./dist/cjs/integrations/index.js"
|
|
92
|
+
},
|
|
93
|
+
"default": "./dist/cjs/integrations/index.js"
|
|
94
|
+
},
|
|
95
|
+
"./integrations/nextjs": {
|
|
96
|
+
"types": "./dist/cjs/integrations/adapters/nextjs/index.d.ts",
|
|
97
|
+
"import": {
|
|
98
|
+
"types": "./dist/esm/integrations/adapters/nextjs/index.d.mts",
|
|
99
|
+
"default": "./dist/esm/integrations/adapters/nextjs/index.mjs"
|
|
100
|
+
},
|
|
101
|
+
"require": {
|
|
102
|
+
"types": "./dist/cjs/integrations/adapters/nextjs/index.d.ts",
|
|
103
|
+
"default": "./dist/cjs/integrations/adapters/nextjs/index.js"
|
|
104
|
+
},
|
|
105
|
+
"default": "./dist/cjs/integrations/adapters/nextjs/index.js"
|
|
106
|
+
},
|
|
83
107
|
"./package.json": "./package.json"
|
|
84
108
|
},
|
|
85
109
|
"files": [
|
|
@@ -114,7 +138,8 @@
|
|
|
114
138
|
"@langchain/core": ">=0.3.0",
|
|
115
139
|
"@mistralai/mistralai": ">=1.0.0",
|
|
116
140
|
"ai": ">=5.0.0",
|
|
117
|
-
"openai": ">=4.85.4"
|
|
141
|
+
"openai": ">=4.85.4",
|
|
142
|
+
"next": ">=13.0.0"
|
|
118
143
|
},
|
|
119
144
|
"peerDependenciesMeta": {
|
|
120
145
|
"@ai-sdk/openai": {
|
|
@@ -134,6 +159,9 @@
|
|
|
134
159
|
},
|
|
135
160
|
"openai": {
|
|
136
161
|
"optional": true
|
|
162
|
+
},
|
|
163
|
+
"next": {
|
|
164
|
+
"optional": true
|
|
137
165
|
}
|
|
138
166
|
},
|
|
139
167
|
"devDependencies": {
|
|
@@ -157,7 +185,8 @@
|
|
|
157
185
|
"@langchain/core": ">=0.3.0",
|
|
158
186
|
"@mistralai/mistralai": ">=1.0.0",
|
|
159
187
|
"ai": ">=5.0.0",
|
|
160
|
-
"openai": ">=4.85.4"
|
|
188
|
+
"openai": ">=4.85.4",
|
|
189
|
+
"next": ">=13.0.0"
|
|
161
190
|
},
|
|
162
191
|
"browser": {
|
|
163
192
|
"fs": false,
|