Skip to content

Commit a7bd960

Browse files
.gitignore modification to allow us to commit the dist files
1 parent a607b8b commit a7bd960

62 files changed

Lines changed: 24683 additions & 2 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ out
9696

9797
# Nuxt.js build / generate output
9898
.nuxt
99-
dist
10099

101100
# Gatsby files
102101
.cache/

dist/client/base_client.js

Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
"use strict";
2+
var __assign = (this && this.__assign) || function () {
3+
__assign = Object.assign || function(t) {
4+
for (var s, i = 1, n = arguments.length; i < n; i++) {
5+
s = arguments[i];
6+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7+
t[p] = s[p];
8+
}
9+
return t;
10+
};
11+
return __assign.apply(this, arguments);
12+
};
13+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15+
return new (P || (P = Promise))(function (resolve, reject) {
16+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19+
step((generator = generator.apply(thisArg, _arguments || [])).next());
20+
});
21+
};
22+
var __generator = (this && this.__generator) || function (thisArg, body) {
23+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25+
function verb(n) { return function (v) { return step([n, v]); }; }
26+
function step(op) {
27+
if (f) throw new TypeError("Generator is already executing.");
28+
while (_) try {
29+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30+
if (y = 0, t) op = [op[0] & 2, t.value];
31+
switch (op[0]) {
32+
case 0: case 1: t = op; break;
33+
case 4: _.label++; return { value: op[1], done: false };
34+
case 5: _.label++; y = op[1]; op = [0]; continue;
35+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
36+
default:
37+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41+
if (t[2]) _.ops.pop();
42+
_.trys.pop(); continue;
43+
}
44+
op = body.call(thisArg, _);
45+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47+
}
48+
};
49+
var __importDefault = (this && this.__importDefault) || function (mod) {
50+
return (mod && mod.__esModule) ? mod : { "default": mod };
51+
};
52+
Object.defineProperty(exports, "__esModule", { value: true });
53+
exports.BaseClient = void 0;
54+
var axios_1 = __importDefault(require("axios"));
55+
var zod_1 = require("zod");
56+
var logger_1 = require("../common/logger");
57+
var errors_1 = require("../common/errors");
58+
var APIResponse = zod_1.z.object({}).catchall(zod_1.z.any());
59+
var APIErrorResponse = zod_1.z
60+
.object({
61+
error: zod_1.z.string().optional(),
62+
message: zod_1.z.string().optional(),
63+
statusCode: zod_1.z.number().optional(),
64+
})
65+
.catchall(zod_1.z.any());
66+
var BaseClient = /** @class */ (function () {
67+
function BaseClient(apiUrl, key, options) {
68+
if (options === void 0) { options = {}; }
69+
this.debug = options.debug || false;
70+
this.apiUrl = options.apiUrl || apiUrl;
71+
this.key = key;
72+
this.timeout = options.timeout || 5000;
73+
axios_1.default.defaults.timeout = this.timeout;
74+
}
75+
/**
76+
* Wraps any error into a CrowdHandlerError
77+
*/
78+
BaseClient.prototype.wrapError = function (error) {
79+
var _a;
80+
// Already a CrowdHandlerError
81+
if (error instanceof errors_1.CrowdHandlerError) {
82+
return error;
83+
}
84+
// Zod validation error
85+
if (error.name === 'ZodError') {
86+
return new errors_1.CrowdHandlerError(errors_1.ErrorCodes.API_INVALID_RESPONSE, 'Invalid response format from API', 'This might be a temporary issue. If it persists, contact support@crowdhandler.com', undefined, { parseError: error.message });
87+
}
88+
// Generic unknown error
89+
return new errors_1.CrowdHandlerError(errors_1.ErrorCodes.UNKNOWN_ERROR, error.message || 'An unexpected error occurred', 'Please try again. If the problem persists, contact support@crowdhandler.com', undefined, {
90+
errorType: (_a = error.constructor) === null || _a === void 0 ? void 0 : _a.name,
91+
stack: error.stack
92+
});
93+
};
94+
BaseClient.prototype.errorHandler = function (error) {
95+
var _a, _b, _c, _d;
96+
return __awaiter(this, void 0, void 0, function () {
97+
var status_1, data, retryAfter, urlMatch, resourceType, resourceId, errorMessage;
98+
return __generator(this, function (_e) {
99+
// If it's already a CrowdHandlerError, just re-throw it
100+
if (error instanceof errors_1.CrowdHandlerError) {
101+
throw error;
102+
}
103+
if (error.response) {
104+
status_1 = error.response.status;
105+
data = error.response.data;
106+
(0, logger_1.logger)(this.debug, "error", "API Error - Status: ".concat(status_1, " - ").concat(JSON.stringify(data)));
107+
(0, logger_1.logger)(this.debug, "error", "Response headers: ".concat(JSON.stringify(error.response.headers)));
108+
// Handle specific HTTP status codes
109+
if (status_1 === 401) {
110+
throw errors_1.createError.invalidApiKey('public');
111+
}
112+
if (status_1 === 429) {
113+
retryAfter = error.response.headers['retry-after'];
114+
throw errors_1.createError.rateLimited(retryAfter);
115+
}
116+
if (status_1 === 404) {
117+
urlMatch = (_b = (_a = error.config) === null || _a === void 0 ? void 0 : _a.url) === null || _b === void 0 ? void 0 : _b.match(/\/v1\/(\w+)\/(\w+)/);
118+
if (urlMatch) {
119+
resourceType = urlMatch[1], resourceId = urlMatch[2];
120+
throw errors_1.createError.resourceNotFound(resourceType, resourceId);
121+
}
122+
}
123+
errorMessage = (data === null || data === void 0 ? void 0 : data.error) || (data === null || data === void 0 ? void 0 : data.message) || "API request failed with status ".concat(status_1);
124+
throw new errors_1.CrowdHandlerError(errors_1.ErrorCodes.API_INVALID_RESPONSE, errorMessage, status_1 >= 500
125+
? 'This appears to be a server error. Please try again later or contact support@crowdhandler.com'
126+
: 'Please check your request parameters and try again', status_1, {
127+
url: (_c = error.config) === null || _c === void 0 ? void 0 : _c.url,
128+
method: (_d = error.config) === null || _d === void 0 ? void 0 : _d.method,
129+
responseData: JSON.stringify(data).substring(0, 200)
130+
});
131+
}
132+
else if (error.request) {
133+
// The request was made but no response was received
134+
(0, logger_1.logger)(this.debug, "error", "No response received: ".concat(error.message));
135+
throw errors_1.createError.apiConnection(error);
136+
}
137+
else {
138+
// Something happened in setting up the request
139+
(0, logger_1.logger)(this.debug, "error", "Request setup error: ".concat(error.message));
140+
// Use wrapError to ensure we always throw CrowdHandlerError
141+
throw this.wrapError(error);
142+
}
143+
return [2 /*return*/];
144+
});
145+
});
146+
};
147+
BaseClient.prototype.httpDELETE = function (path, body) {
148+
return __awaiter(this, void 0, void 0, function () {
149+
var response, error_1;
150+
return __generator(this, function (_a) {
151+
switch (_a.label) {
152+
case 0:
153+
_a.trys.push([0, 2, , 4]);
154+
return [4 /*yield*/, axios_1.default.delete(this.apiUrl + path, {
155+
headers: {
156+
"x-api-key": this.key,
157+
},
158+
})];
159+
case 1:
160+
response = _a.sent();
161+
try {
162+
return [2 /*return*/, APIResponse.parse(response.data)];
163+
}
164+
catch (parseError) {
165+
throw this.wrapError(parseError);
166+
}
167+
return [3 /*break*/, 4];
168+
case 2:
169+
error_1 = _a.sent();
170+
return [4 /*yield*/, this.errorHandler(error_1)];
171+
case 3:
172+
_a.sent();
173+
return [3 /*break*/, 4];
174+
case 4: return [2 /*return*/];
175+
}
176+
});
177+
});
178+
};
179+
BaseClient.prototype.httpGET = function (path, params) {
180+
return __awaiter(this, void 0, void 0, function () {
181+
var response, error_2;
182+
return __generator(this, function (_a) {
183+
switch (_a.label) {
184+
case 0:
185+
_a.trys.push([0, 2, , 4]);
186+
return [4 /*yield*/, axios_1.default.get(this.apiUrl + path, {
187+
params: params,
188+
headers: {
189+
"x-api-key": this.key,
190+
},
191+
})];
192+
case 1:
193+
response = _a.sent();
194+
try {
195+
return [2 /*return*/, APIResponse.parse(response.data)];
196+
}
197+
catch (parseError) {
198+
throw this.wrapError(parseError);
199+
}
200+
return [3 /*break*/, 4];
201+
case 2:
202+
error_2 = _a.sent();
203+
return [4 /*yield*/, this.errorHandler(error_2)];
204+
case 3:
205+
_a.sent();
206+
return [3 /*break*/, 4];
207+
case 4: return [2 /*return*/];
208+
}
209+
});
210+
});
211+
};
212+
BaseClient.prototype.httpPOST = function (path, body, headers, schema) {
213+
if (schema === void 0) { schema = APIResponse; }
214+
return __awaiter(this, void 0, void 0, function () {
215+
var response, error_3;
216+
return __generator(this, function (_a) {
217+
switch (_a.label) {
218+
case 0:
219+
_a.trys.push([0, 2, , 4]);
220+
return [4 /*yield*/, axios_1.default.post(this.apiUrl + path, body, {
221+
headers: __assign({ "x-api-key": this.key }, headers),
222+
})];
223+
case 1:
224+
response = _a.sent();
225+
try {
226+
return [2 /*return*/, schema.parse(response.data)];
227+
}
228+
catch (parseError) {
229+
throw this.wrapError(parseError);
230+
}
231+
return [3 /*break*/, 4];
232+
case 2:
233+
error_3 = _a.sent();
234+
return [4 /*yield*/, this.errorHandler(error_3)];
235+
case 3:
236+
_a.sent();
237+
return [3 /*break*/, 4];
238+
case 4: return [2 /*return*/];
239+
}
240+
});
241+
});
242+
};
243+
BaseClient.prototype.httpPUT = function (path, body) {
244+
return __awaiter(this, void 0, void 0, function () {
245+
var response, error_4;
246+
return __generator(this, function (_a) {
247+
switch (_a.label) {
248+
case 0:
249+
_a.trys.push([0, 2, , 3]);
250+
return [4 /*yield*/, axios_1.default.put(this.apiUrl + path, body, {
251+
headers: {
252+
"x-api-key": this.key,
253+
},
254+
})];
255+
case 1:
256+
response = _a.sent();
257+
return [2 /*return*/, APIResponse.parse(response.data)];
258+
case 2:
259+
error_4 = _a.sent();
260+
return [2 /*return*/, this.errorHandler(error_4)];
261+
case 3: return [2 /*return*/];
262+
}
263+
});
264+
});
265+
};
266+
return BaseClient;
267+
}());
268+
exports.BaseClient = BaseClient;

0 commit comments

Comments
 (0)