@checkoutpage/sdk 0.1.2
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/README.md +158 -0
- package/dist/index.d.mts +3077 -0
- package/dist/index.d.ts +3077 -0
- package/dist/index.js +175 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +167 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +55 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/errors.ts
|
|
4
|
+
var CheckoutPageError = class _CheckoutPageError extends Error {
|
|
5
|
+
constructor(message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = "CheckoutPageError";
|
|
8
|
+
Object.setPrototypeOf(this, _CheckoutPageError.prototype);
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
var AuthenticationError = class _AuthenticationError extends CheckoutPageError {
|
|
12
|
+
constructor(message = "Authentication failed. Please check your API key.") {
|
|
13
|
+
super(message);
|
|
14
|
+
this.name = "AuthenticationError";
|
|
15
|
+
Object.setPrototypeOf(this, _AuthenticationError.prototype);
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
var NotFoundError = class _NotFoundError extends CheckoutPageError {
|
|
19
|
+
constructor(message = "The requested resource was not found.") {
|
|
20
|
+
super(message);
|
|
21
|
+
this.name = "NotFoundError";
|
|
22
|
+
Object.setPrototypeOf(this, _NotFoundError.prototype);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
var RateLimitError = class _RateLimitError extends CheckoutPageError {
|
|
26
|
+
constructor(message = "Rate limit exceeded. Please try again later.") {
|
|
27
|
+
super(message);
|
|
28
|
+
this.name = "RateLimitError";
|
|
29
|
+
Object.setPrototypeOf(this, _RateLimitError.prototype);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
var ValidationError = class _ValidationError extends CheckoutPageError {
|
|
33
|
+
constructor(message = "Validation failed.") {
|
|
34
|
+
super(message);
|
|
35
|
+
this.name = "ValidationError";
|
|
36
|
+
Object.setPrototypeOf(this, _ValidationError.prototype);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
var APIError = class _APIError extends CheckoutPageError {
|
|
40
|
+
statusCode;
|
|
41
|
+
response;
|
|
42
|
+
constructor(message, statusCode, response) {
|
|
43
|
+
super(message);
|
|
44
|
+
this.name = "APIError";
|
|
45
|
+
this.statusCode = statusCode;
|
|
46
|
+
this.response = response;
|
|
47
|
+
Object.setPrototypeOf(this, _APIError.prototype);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
// src/client.ts
|
|
52
|
+
var CheckoutPageClient = class {
|
|
53
|
+
apiKey;
|
|
54
|
+
baseUrl;
|
|
55
|
+
constructor(options) {
|
|
56
|
+
if (!options.apiKey) {
|
|
57
|
+
throw new Error("API key is required");
|
|
58
|
+
}
|
|
59
|
+
this.apiKey = options.apiKey;
|
|
60
|
+
this.baseUrl = options.baseUrl || "https://api.checkoutpage.com";
|
|
61
|
+
}
|
|
62
|
+
async request(options) {
|
|
63
|
+
const url = this.buildUrl(options.path, options.query);
|
|
64
|
+
const headers = {
|
|
65
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
66
|
+
"Content-Type": "application/json",
|
|
67
|
+
"User-Agent": "@checkoutpage/sdk/0.1.0"
|
|
68
|
+
};
|
|
69
|
+
const fetchOptions = {
|
|
70
|
+
method: options.method,
|
|
71
|
+
headers
|
|
72
|
+
};
|
|
73
|
+
if (options.body) {
|
|
74
|
+
fetchOptions.body = JSON.stringify(options.body);
|
|
75
|
+
}
|
|
76
|
+
try {
|
|
77
|
+
const response = await fetch(url, fetchOptions);
|
|
78
|
+
return await this.handleResponse(response);
|
|
79
|
+
} catch (error) {
|
|
80
|
+
if (error instanceof APIError) {
|
|
81
|
+
throw error;
|
|
82
|
+
}
|
|
83
|
+
throw new APIError(
|
|
84
|
+
`Network error: ${error instanceof Error ? error.message : "Unknown error"}`,
|
|
85
|
+
0
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
buildUrl(path, query) {
|
|
90
|
+
const url = new URL(path, this.baseUrl);
|
|
91
|
+
if (query) {
|
|
92
|
+
Object.entries(query).forEach(([key, value]) => {
|
|
93
|
+
if (value !== void 0) {
|
|
94
|
+
url.searchParams.append(key, String(value));
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
return url.toString();
|
|
99
|
+
}
|
|
100
|
+
async handleResponse(response) {
|
|
101
|
+
const contentType = response.headers.get("content-type");
|
|
102
|
+
const isJson = contentType?.includes("application/json");
|
|
103
|
+
let data;
|
|
104
|
+
try {
|
|
105
|
+
data = isJson ? await response.json() : await response.text();
|
|
106
|
+
} catch {
|
|
107
|
+
data = null;
|
|
108
|
+
}
|
|
109
|
+
if (!response.ok) {
|
|
110
|
+
const message = this.extractErrorMessage(data);
|
|
111
|
+
switch (response.status) {
|
|
112
|
+
case 401:
|
|
113
|
+
case 403:
|
|
114
|
+
throw new AuthenticationError(message);
|
|
115
|
+
case 404:
|
|
116
|
+
throw new NotFoundError(message);
|
|
117
|
+
case 400:
|
|
118
|
+
case 422:
|
|
119
|
+
throw new ValidationError(message);
|
|
120
|
+
case 429:
|
|
121
|
+
throw new RateLimitError(message);
|
|
122
|
+
default:
|
|
123
|
+
throw new APIError(message, response.status, data);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return data;
|
|
127
|
+
}
|
|
128
|
+
extractErrorMessage(data) {
|
|
129
|
+
if (typeof data === "object" && data !== null) {
|
|
130
|
+
if ("message" in data && typeof data.message === "string") {
|
|
131
|
+
return data.message;
|
|
132
|
+
}
|
|
133
|
+
if ("error" in data && typeof data.error === "string") {
|
|
134
|
+
return data.error;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return "An unexpected error occurred";
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
// src/resources/customers/customers.ts
|
|
142
|
+
var CustomerResource = class {
|
|
143
|
+
constructor(client) {
|
|
144
|
+
this.client = client;
|
|
145
|
+
}
|
|
146
|
+
async get(customerId) {
|
|
147
|
+
if (!customerId) {
|
|
148
|
+
throw new Error("Customer ID is required");
|
|
149
|
+
}
|
|
150
|
+
return this.client.request({
|
|
151
|
+
method: "GET",
|
|
152
|
+
path: `/v1/customers/${customerId}`
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
// src/index.ts
|
|
158
|
+
var CheckoutPage = class {
|
|
159
|
+
customers;
|
|
160
|
+
client;
|
|
161
|
+
constructor(options) {
|
|
162
|
+
this.client = new CheckoutPageClient(options);
|
|
163
|
+
this.customers = new CustomerResource(this.client);
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
exports.APIError = APIError;
|
|
168
|
+
exports.AuthenticationError = AuthenticationError;
|
|
169
|
+
exports.CheckoutPage = CheckoutPage;
|
|
170
|
+
exports.CheckoutPageError = CheckoutPageError;
|
|
171
|
+
exports.NotFoundError = NotFoundError;
|
|
172
|
+
exports.RateLimitError = RateLimitError;
|
|
173
|
+
exports.ValidationError = ValidationError;
|
|
174
|
+
//# sourceMappingURL=index.js.map
|
|
175
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/errors.ts","../src/client.ts","../src/resources/customers/customers.ts","../src/index.ts"],"names":[],"mappings":";;;AAAO,IAAM,iBAAA,GAAN,MAAM,kBAAA,SAA0B,KAAA,CAAM;AAAA,EAC3C,YAAY,OAAA,EAAiB;AAC3B,IAAA,KAAA,CAAM,OAAO,CAAA;AACb,IAAA,IAAA,CAAK,IAAA,GAAO,mBAAA;AACZ,IAAA,MAAA,CAAO,cAAA,CAAe,IAAA,EAAM,kBAAA,CAAkB,SAAS,CAAA;AAAA,EACzD;AACF;AAEO,IAAM,mBAAA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O,OAAA,CAAkB;AAAA,MACnC,MAAA,EAAQ,KAAA;AAAA,MACR,IAAA,EAAM,iBAAiB,UAAU,CAAA;AAAA,KAClC,CAAA;AAAA,EACH;AACF,CAAA;;;ACbO,IAAM,eAAN,MAAmB;AAAA,EACR,SAAA;AAAA,EACC,MAAA;AAAA,EAEjB,YAAY,OAAA,EAAoC;AAC9C,IAAA,IAAA,CAAK,MAAA,GAAS,IAAI,kBAAA,CAAmB,OAAO,CAAA;AAC5C,IAAA,IAAA,CAAK,SAAA,GAAY,IAAI,gBAAA,CAAiB,IAAA,CAAK,MAAM,CAAA;AAAA,EACnD;AACF","file":"index.js","sourcesContent":["export class CheckoutPageError extends Error {\n constructor(message: string) {\n super(message)\n this.name = 'CheckoutPageError'\n Object.setPrototypeOf(this, CheckoutPageError.prototype)\n }\n}\n\nexport class AuthenticationError extends CheckoutPageError {\n constructor(message = 'Authentication failed. Please check your API key.') {\n super(message)\n this.name = 'AuthenticationError'\n Object.setPrototypeOf(this, AuthenticationError.prototype)\n }\n}\n\nexport class NotFoundError extends CheckoutPageError {\n constructor(message = 'The requested resource was not found.') {\n super(message)\n this.name = 'NotFoundError'\n Object.setPrototypeOf(this, NotFoundError.prototype)\n }\n}\n\nexport class RateLimitError extends CheckoutPageError {\n constructor(message = 'Rate limit exceeded. Please try again later.') {\n super(message)\n this.name = 'RateLimitError'\n Object.setPrototypeOf(this, RateLimitError.prototype)\n }\n}\n\nexport class ValidationError extends CheckoutPageError {\n constructor(message = 'Validation failed.') {\n super(message)\n this.name = 'ValidationError'\n Object.setPrototypeOf(this, ValidationError.prototype)\n }\n}\n\nexport class APIError extends CheckoutPageError {\n public readonly statusCode: number\n public readonly response?: unknown\n\n constructor(message: string, statusCode: number, response?: unknown) {\n super(message)\n this.name = 'APIError'\n this.statusCode = statusCode\n this.response = response\n Object.setPrototypeOf(this, APIError.prototype)\n }\n}\n","import {\n APIError,\n AuthenticationError,\n NotFoundError,\n RateLimitError,\n ValidationError,\n} from './errors'\n\nexport interface CheckoutPageClientOptions {\n apiKey: string\n baseUrl?: string\n}\n\nexport interface RequestOptions {\n method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'\n path: string\n body?: unknown\n query?: Record<string, string | number | boolean | undefined>\n}\n\nexport class CheckoutPageClient {\n private readonly apiKey: string\n private readonly baseUrl: string\n\n constructor(options: CheckoutPageClientOptions) {\n if (!options.apiKey) {\n throw new Error('API key is required')\n }\n\n this.apiKey = options.apiKey\n this.baseUrl = options.baseUrl || 'https://api.checkoutpage.com'\n }\n\n async request<T>(options: RequestOptions): Promise<T> {\n const url = this.buildUrl(options.path, options.query)\n\n const headers: Record<string, string> = {\n Authorization: `Bearer ${this.apiKey}`,\n 'Content-Type': 'application/json',\n 'User-Agent': '@checkoutpage/sdk/0.1.0',\n }\n\n const fetchOptions: RequestInit = {\n method: options.method,\n headers,\n }\n\n if (options.body) {\n fetchOptions.body = JSON.stringify(options.body)\n }\n\n try {\n const response = await fetch(url, fetchOptions)\n return await this.handleResponse<T>(response)\n } catch (error) {\n if (error instanceof APIError) {\n throw error\n }\n throw new APIError(\n `Network error: ${error instanceof Error ? error.message : 'Unknown error'}`,\n 0\n )\n }\n }\n\n private buildUrl(path: string, query?: Record<string, string | number | boolean | undefined>): string {\n const url = new URL(path, this.baseUrl)\n\n if (query) {\n Object.entries(query).forEach(([key, value]) => {\n if (value !== undefined) {\n url.searchParams.append(key, String(value))\n }\n })\n }\n\n return url.toString()\n }\n\n private async handleResponse<T>(response: Response): Promise<T> {\n const contentType = response.headers.get('content-type')\n const isJson = contentType?.includes('application/json')\n\n let data: unknown\n try {\n data = isJson ? await response.json() : await response.text()\n } catch {\n data = null\n }\n\n if (!response.ok) {\n const message = this.extractErrorMessage(data)\n\n switch (response.status) {\n case 401:\n case 403:\n throw new AuthenticationError(message)\n case 404:\n throw new NotFoundError(message)\n case 400:\n case 422:\n throw new ValidationError(message)\n case 429:\n throw new RateLimitError(message)\n default:\n throw new APIError(message, response.status, data)\n }\n }\n\n return data as T\n }\n\n private extractErrorMessage(data: unknown): string {\n if (typeof data === 'object' && data !== null) {\n if ('message' in data && typeof data.message === 'string') {\n return data.message\n }\n if ('error' in data && typeof data.error === 'string') {\n return data.error\n }\n }\n return 'An unexpected error occurred'\n }\n}\n","import type { CheckoutPageClient } from '../../client'\nimport type { Customer } from '../../types'\n\nexport class CustomerResource {\n constructor(private client: CheckoutPageClient) {}\n\n async get(customerId: string): Promise<Customer> {\n if (!customerId) {\n throw new Error('Customer ID is required')\n }\n\n return this.client.request<Customer>({\n method: 'GET',\n path: `/v1/customers/${customerId}`,\n })\n }\n}\n","import { CheckoutPageClient, CheckoutPageClientOptions } from './client'\nimport { CustomerResource } from './resources/customers/customers'\n\nexport class CheckoutPage {\n public readonly customers: CustomerResource\n private readonly client: CheckoutPageClient\n\n constructor(options: CheckoutPageClientOptions) {\n this.client = new CheckoutPageClient(options)\n this.customers = new CustomerResource(this.client)\n }\n}\n\n// Export types and errors for convenience\nexport type { CheckoutPageClientOptions } from './client'\nexport type { Customer, Address, Shipping } from './types'\nexport {\n CheckoutPageError,\n AuthenticationError,\n NotFoundError,\n RateLimitError,\n ValidationError,\n APIError,\n} from './errors'\n"]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
// src/errors.ts
|
|
2
|
+
var CheckoutPageError = class _CheckoutPageError extends Error {
|
|
3
|
+
constructor(message) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.name = "CheckoutPageError";
|
|
6
|
+
Object.setPrototypeOf(this, _CheckoutPageError.prototype);
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
var AuthenticationError = class _AuthenticationError extends CheckoutPageError {
|
|
10
|
+
constructor(message = "Authentication failed. Please check your API key.") {
|
|
11
|
+
super(message);
|
|
12
|
+
this.name = "AuthenticationError";
|
|
13
|
+
Object.setPrototypeOf(this, _AuthenticationError.prototype);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
var NotFoundError = class _NotFoundError extends CheckoutPageError {
|
|
17
|
+
constructor(message = "The requested resource was not found.") {
|
|
18
|
+
super(message);
|
|
19
|
+
this.name = "NotFoundError";
|
|
20
|
+
Object.setPrototypeOf(this, _NotFoundError.prototype);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
var RateLimitError = class _RateLimitError extends CheckoutPageError {
|
|
24
|
+
constructor(message = "Rate limit exceeded. Please try again later.") {
|
|
25
|
+
super(message);
|
|
26
|
+
this.name = "RateLimitError";
|
|
27
|
+
Object.setPrototypeOf(this, _RateLimitError.prototype);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
var ValidationError = class _ValidationError extends CheckoutPageError {
|
|
31
|
+
constructor(message = "Validation failed.") {
|
|
32
|
+
super(message);
|
|
33
|
+
this.name = "ValidationError";
|
|
34
|
+
Object.setPrototypeOf(this, _ValidationError.prototype);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
var APIError = class _APIError extends CheckoutPageError {
|
|
38
|
+
statusCode;
|
|
39
|
+
response;
|
|
40
|
+
constructor(message, statusCode, response) {
|
|
41
|
+
super(message);
|
|
42
|
+
this.name = "APIError";
|
|
43
|
+
this.statusCode = statusCode;
|
|
44
|
+
this.response = response;
|
|
45
|
+
Object.setPrototypeOf(this, _APIError.prototype);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
// src/client.ts
|
|
50
|
+
var CheckoutPageClient = class {
|
|
51
|
+
apiKey;
|
|
52
|
+
baseUrl;
|
|
53
|
+
constructor(options) {
|
|
54
|
+
if (!options.apiKey) {
|
|
55
|
+
throw new Error("API key is required");
|
|
56
|
+
}
|
|
57
|
+
this.apiKey = options.apiKey;
|
|
58
|
+
this.baseUrl = options.baseUrl || "https://api.checkoutpage.com";
|
|
59
|
+
}
|
|
60
|
+
async request(options) {
|
|
61
|
+
const url = this.buildUrl(options.path, options.query);
|
|
62
|
+
const headers = {
|
|
63
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
64
|
+
"Content-Type": "application/json",
|
|
65
|
+
"User-Agent": "@checkoutpage/sdk/0.1.0"
|
|
66
|
+
};
|
|
67
|
+
const fetchOptions = {
|
|
68
|
+
method: options.method,
|
|
69
|
+
headers
|
|
70
|
+
};
|
|
71
|
+
if (options.body) {
|
|
72
|
+
fetchOptions.body = JSON.stringify(options.body);
|
|
73
|
+
}
|
|
74
|
+
try {
|
|
75
|
+
const response = await fetch(url, fetchOptions);
|
|
76
|
+
return await this.handleResponse(response);
|
|
77
|
+
} catch (error) {
|
|
78
|
+
if (error instanceof APIError) {
|
|
79
|
+
throw error;
|
|
80
|
+
}
|
|
81
|
+
throw new APIError(
|
|
82
|
+
`Network error: ${error instanceof Error ? error.message : "Unknown error"}`,
|
|
83
|
+
0
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
buildUrl(path, query) {
|
|
88
|
+
const url = new URL(path, this.baseUrl);
|
|
89
|
+
if (query) {
|
|
90
|
+
Object.entries(query).forEach(([key, value]) => {
|
|
91
|
+
if (value !== void 0) {
|
|
92
|
+
url.searchParams.append(key, String(value));
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
return url.toString();
|
|
97
|
+
}
|
|
98
|
+
async handleResponse(response) {
|
|
99
|
+
const contentType = response.headers.get("content-type");
|
|
100
|
+
const isJson = contentType?.includes("application/json");
|
|
101
|
+
let data;
|
|
102
|
+
try {
|
|
103
|
+
data = isJson ? await response.json() : await response.text();
|
|
104
|
+
} catch {
|
|
105
|
+
data = null;
|
|
106
|
+
}
|
|
107
|
+
if (!response.ok) {
|
|
108
|
+
const message = this.extractErrorMessage(data);
|
|
109
|
+
switch (response.status) {
|
|
110
|
+
case 401:
|
|
111
|
+
case 403:
|
|
112
|
+
throw new AuthenticationError(message);
|
|
113
|
+
case 404:
|
|
114
|
+
throw new NotFoundError(message);
|
|
115
|
+
case 400:
|
|
116
|
+
case 422:
|
|
117
|
+
throw new ValidationError(message);
|
|
118
|
+
case 429:
|
|
119
|
+
throw new RateLimitError(message);
|
|
120
|
+
default:
|
|
121
|
+
throw new APIError(message, response.status, data);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return data;
|
|
125
|
+
}
|
|
126
|
+
extractErrorMessage(data) {
|
|
127
|
+
if (typeof data === "object" && data !== null) {
|
|
128
|
+
if ("message" in data && typeof data.message === "string") {
|
|
129
|
+
return data.message;
|
|
130
|
+
}
|
|
131
|
+
if ("error" in data && typeof data.error === "string") {
|
|
132
|
+
return data.error;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return "An unexpected error occurred";
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
// src/resources/customers/customers.ts
|
|
140
|
+
var CustomerResource = class {
|
|
141
|
+
constructor(client) {
|
|
142
|
+
this.client = client;
|
|
143
|
+
}
|
|
144
|
+
async get(customerId) {
|
|
145
|
+
if (!customerId) {
|
|
146
|
+
throw new Error("Customer ID is required");
|
|
147
|
+
}
|
|
148
|
+
return this.client.request({
|
|
149
|
+
method: "GET",
|
|
150
|
+
path: `/v1/customers/${customerId}`
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
// src/index.ts
|
|
156
|
+
var CheckoutPage = class {
|
|
157
|
+
customers;
|
|
158
|
+
client;
|
|
159
|
+
constructor(options) {
|
|
160
|
+
this.client = new CheckoutPageClient(options);
|
|
161
|
+
this.customers = new CustomerResource(this.client);
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
export { APIError, AuthenticationError, CheckoutPage, CheckoutPageError, NotFoundError, RateLimitError, ValidationError };
|
|
166
|
+
//# sourceMappingURL=index.mjs.map
|
|
167
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/errors.ts","../src/client.ts","../src/resources/customers/customers.ts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A,EACT;AACF,CAAA;;;ACxHO,IAAM,mBAAN,MAAuB;AAAA,EAC5B,YAAoB,MAAA,EAA4B;AAA5B,IAAA,IAAA,CAAA,MAAA,GAAA,MAAA;AAAA,EAA6B;AAAA,EAEjD,MAAM,IAAI,UAAA,EAAuC;AAC/C,IAAA,IAAI,CAAC,UAAA,EAAY;AACf,MAAA,MAAM,IAAI,MAAM,yBAAyB,CAAA;AAAA,IAC3C;AAEA,IAAA,OAAO,IAAA,CAAK,OAAO,OAAA,CAAkB;AAAA,MACnC,MAAA,EAAQ,KAAA;AAAA,MACR,IAAA,EAAM,iBAAiB,UAAU,CAAA;AAAA,KAClC,CAAA;AAAA,EACH;AACF,CAAA;;;ACbO,IAAM,eAAN,MAAmB;AAAA,EACR,SAAA;AAAA,EACC,MAAA;AAAA,EAEjB,YAAY,OAAA,EAAoC;AAC9C,IAAA,IAAA,CAAK,MAAA,GAAS,IAAI,kBAAA,CAAmB,OAAO,CAAA;AAC5C,IAAA,IAAA,CAAK,SAAA,GAAY,IAAI,gBAAA,CAAiB,IAAA,CAAK,MAAM,CAAA;AAAA,EACnD;AACF","file":"index.mjs","sourcesContent":["export class CheckoutPageError extends Error {\n constructor(message: string) {\n super(message)\n this.name = 'CheckoutPageError'\n Object.setPrototypeOf(this, CheckoutPageError.prototype)\n }\n}\n\nexport class AuthenticationError extends CheckoutPageError {\n constructor(message = 'Authentication failed. Please check your API key.') {\n super(message)\n this.name = 'AuthenticationError'\n Object.setPrototypeOf(this, AuthenticationError.prototype)\n }\n}\n\nexport class NotFoundError extends CheckoutPageError {\n constructor(message = 'The requested resource was not found.') {\n super(message)\n this.name = 'NotFoundError'\n Object.setPrototypeOf(this, NotFoundError.prototype)\n }\n}\n\nexport class RateLimitError extends CheckoutPageError {\n constructor(message = 'Rate limit exceeded. Please try again later.') {\n super(message)\n this.name = 'RateLimitError'\n Object.setPrototypeOf(this, RateLimitError.prototype)\n }\n}\n\nexport class ValidationError extends CheckoutPageError {\n constructor(message = 'Validation failed.') {\n super(message)\n this.name = 'ValidationError'\n Object.setPrototypeOf(this, ValidationError.prototype)\n }\n}\n\nexport class APIError extends CheckoutPageError {\n public readonly statusCode: number\n public readonly response?: unknown\n\n constructor(message: string, statusCode: number, response?: unknown) {\n super(message)\n this.name = 'APIError'\n this.statusCode = statusCode\n this.response = response\n Object.setPrototypeOf(this, APIError.prototype)\n }\n}\n","import {\n APIError,\n AuthenticationError,\n NotFoundError,\n RateLimitError,\n ValidationError,\n} from './errors'\n\nexport interface CheckoutPageClientOptions {\n apiKey: string\n baseUrl?: string\n}\n\nexport interface RequestOptions {\n method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'\n path: string\n body?: unknown\n query?: Record<string, string | number | boolean | undefined>\n}\n\nexport class CheckoutPageClient {\n private readonly apiKey: string\n private readonly baseUrl: string\n\n constructor(options: CheckoutPageClientOptions) {\n if (!options.apiKey) {\n throw new Error('API key is required')\n }\n\n this.apiKey = options.apiKey\n this.baseUrl = options.baseUrl || 'https://api.checkoutpage.com'\n }\n\n async request<T>(options: RequestOptions): Promise<T> {\n const url = this.buildUrl(options.path, options.query)\n\n const headers: Record<string, string> = {\n Authorization: `Bearer ${this.apiKey}`,\n 'Content-Type': 'application/json',\n 'User-Agent': '@checkoutpage/sdk/0.1.0',\n }\n\n const fetchOptions: RequestInit = {\n method: options.method,\n headers,\n }\n\n if (options.body) {\n fetchOptions.body = JSON.stringify(options.body)\n }\n\n try {\n const response = await fetch(url, fetchOptions)\n return await this.handleResponse<T>(response)\n } catch (error) {\n if (error instanceof APIError) {\n throw error\n }\n throw new APIError(\n `Network error: ${error instanceof Error ? error.message : 'Unknown error'}`,\n 0\n )\n }\n }\n\n private buildUrl(path: string, query?: Record<string, string | number | boolean | undefined>): string {\n const url = new URL(path, this.baseUrl)\n\n if (query) {\n Object.entries(query).forEach(([key, value]) => {\n if (value !== undefined) {\n url.searchParams.append(key, String(value))\n }\n })\n }\n\n return url.toString()\n }\n\n private async handleResponse<T>(response: Response): Promise<T> {\n const contentType = response.headers.get('content-type')\n const isJson = contentType?.includes('application/json')\n\n let data: unknown\n try {\n data = isJson ? await response.json() : await response.text()\n } catch {\n data = null\n }\n\n if (!response.ok) {\n const message = this.extractErrorMessage(data)\n\n switch (response.status) {\n case 401:\n case 403:\n throw new AuthenticationError(message)\n case 404:\n throw new NotFoundError(message)\n case 400:\n case 422:\n throw new ValidationError(message)\n case 429:\n throw new RateLimitError(message)\n default:\n throw new APIError(message, response.status, data)\n }\n }\n\n return data as T\n }\n\n private extractErrorMessage(data: unknown): string {\n if (typeof data === 'object' && data !== null) {\n if ('message' in data && typeof data.message === 'string') {\n return data.message\n }\n if ('error' in data && typeof data.error === 'string') {\n return data.error\n }\n }\n return 'An unexpected error occurred'\n }\n}\n","import type { CheckoutPageClient } from '../../client'\nimport type { Customer } from '../../types'\n\nexport class CustomerResource {\n constructor(private client: CheckoutPageClient) {}\n\n async get(customerId: string): Promise<Customer> {\n if (!customerId) {\n throw new Error('Customer ID is required')\n }\n\n return this.client.request<Customer>({\n method: 'GET',\n path: `/v1/customers/${customerId}`,\n })\n }\n}\n","import { CheckoutPageClient, CheckoutPageClientOptions } from './client'\nimport { CustomerResource } from './resources/customers/customers'\n\nexport class CheckoutPage {\n public readonly customers: CustomerResource\n private readonly client: CheckoutPageClient\n\n constructor(options: CheckoutPageClientOptions) {\n this.client = new CheckoutPageClient(options)\n this.customers = new CustomerResource(this.client)\n }\n}\n\n// Export types and errors for convenience\nexport type { CheckoutPageClientOptions } from './client'\nexport type { Customer, Address, Shipping } from './types'\nexport {\n CheckoutPageError,\n AuthenticationError,\n NotFoundError,\n RateLimitError,\n ValidationError,\n APIError,\n} from './errors'\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@checkoutpage/sdk",
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "Official JavaScript SDK for the CheckoutPage API",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsup",
|
|
20
|
+
"test": "vitest run",
|
|
21
|
+
"test:watch": "vitest",
|
|
22
|
+
"lint": "eslint src --ext .ts",
|
|
23
|
+
"type-check": "tsc --noEmit"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"checkoutpage",
|
|
27
|
+
"checkout",
|
|
28
|
+
"payments",
|
|
29
|
+
"stripe",
|
|
30
|
+
"sdk",
|
|
31
|
+
"api"
|
|
32
|
+
],
|
|
33
|
+
"author": "CheckoutPage",
|
|
34
|
+
"license": "MIT",
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "https://github.com/checkoutpage/checkoutpage-api-sdk.git",
|
|
38
|
+
"directory": "js"
|
|
39
|
+
},
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
42
|
+
},
|
|
43
|
+
"bugs": {
|
|
44
|
+
"url": "https://github.com/checkoutpage/checkoutpage-api-sdk/issues"
|
|
45
|
+
},
|
|
46
|
+
"homepage": "https://checkoutpage.com",
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@types/node": "^20.11.16",
|
|
49
|
+
"tsup": "^8.0.1",
|
|
50
|
+
"vitest": "^3.2.4"
|
|
51
|
+
},
|
|
52
|
+
"engines": {
|
|
53
|
+
"node": ">=18.0.0"
|
|
54
|
+
}
|
|
55
|
+
}
|