Skip to content

Commit b72263f

Browse files
committed
Fix returned types
1 parent beacb2a commit b72263f

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

src/index.ts

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type ApiKeyResponse = ApiKeySuccessResponse | ApiKeyErrorResponse;
1818

1919
interface ContactSuccessResponse {
2020
success: true;
21+
/** The ID of the contact. */
2122
id: string;
2223
}
2324

@@ -78,8 +79,6 @@ interface EventSuccessResponse {
7879
success: boolean;
7980
}
8081

81-
type EventResponse = EventSuccessResponse | ErrorResponse;
82-
8382
interface TransactionalSuccess {
8483
success: true;
8584
}
@@ -101,11 +100,6 @@ interface TransactionalNestedError {
101100
transactionalId?: string;
102101
}
103102

104-
type TransactionalResponse =
105-
| TransactionalSuccess
106-
| TransactionalError
107-
| TransactionalNestedError;
108-
109103
type ContactProperties = Record<string, string | number | boolean | null>;
110104

111105
type EventProperties = Record<string, string | number | boolean>;
@@ -192,6 +186,15 @@ interface TransactionalEmail {
192186
dataVariables: string[];
193187
}
194188

189+
interface ContactProperty {
190+
/**
191+
*/
192+
label: string;
193+
/**
194+
* The type of property.
195+
*/
196+
type: "string" | "number" | "boolean" | "date";
197+
}
195198
interface ListTransactionalsResponse {
196199
pagination: PaginationData;
197200
data: TransactionalEmail[];
@@ -210,9 +213,16 @@ class RateLimitExceededError extends Error {
210213

211214
class APIError extends Error {
212215
statusCode: number;
213-
json: Record<string, unknown>;
214-
constructor(statusCode: number, json: Record<string, unknown>) {
215-
super(`${statusCode}${json.message ? ` - ${json.message}` : ""}`);
216+
json: ErrorResponse | TransactionalError | TransactionalNestedError;
217+
json: ErrorResponse | TransactionalError | TransactionalNestedError
218+
) {
219+
let message: string | undefined;
220+
if ("error" in json && json.error?.message) {
221+
message = json.error.message;
222+
} else if ("message" in json) {
223+
message = json.message;
224+
}
225+
super(`${statusCode}${message ? ` - ${message}` : ""}`);
216226
this.name = "APIError";
217227
this.statusCode = statusCode;
218228
this.json = json;
@@ -318,7 +328,7 @@ class LoopsClient {
318328
email: string,
319329
properties?: ContactProperties,
320330
mailingLists?: Record<string, boolean>
321-
): Promise<ContactSuccessResponse | ErrorResponse> {
331+
): Promise<ContactSuccessResponse> {
322332
const payload = { email, ...properties, mailingLists };
323333
return this._makeQuery({
324334
path: "v1/contacts/create",
@@ -342,7 +352,7 @@ class LoopsClient {
342352
email: string,
343353
properties: ContactProperties,
344354
mailingLists?: Record<string, boolean>
345-
): Promise<ContactSuccessResponse | ErrorResponse> {
355+
): Promise<ContactSuccessResponse> {
346356
const payload = { email, ...properties, mailingLists };
347357
return this._makeQuery({
348358
path: "v1/contacts/update",
@@ -397,7 +407,7 @@ class LoopsClient {
397407
}: {
398408
email?: string;
399409
userId?: string;
400-
}): Promise<DeleteSuccessResponse | ErrorResponse> {
410+
}): Promise<DeleteSuccessResponse> {
401411
if (email && userId) throw "Only one parameter is permitted.";
402412
const payload: { email?: string; userId?: string } = {};
403413
if (email) payload["email"] = email;
@@ -423,7 +433,7 @@ class LoopsClient {
423433
async createContactProperty(
424434
name: string,
425435
type: "string" | "number" | "boolean" | "date"
426-
): Promise<ContactPropertySuccessResponse | ErrorResponse> {
436+
): Promise<ContactPropertySuccessResponse> {
427437
return this._makeQuery({
428438
path: "v1/contacts/properties",
429439
method: "POST",
@@ -445,7 +455,7 @@ class LoopsClient {
445455
*/
446456
async getCustomProperties(
447457
list?: "all" | "custom"
448-
): Promise<Record<"key" | "label" | "type", string>[]> {
458+
): Promise<ContactProperty[]> {
449459
return this._makeQuery({
450460
path: "v1/contacts/properties",
451461
params: { list: list || "all" },
@@ -494,7 +504,7 @@ class LoopsClient {
494504
contactProperties?: ContactProperties;
495505
eventProperties?: EventProperties;
496506
mailingLists?: Record<string, boolean>;
497-
}): Promise<EventResponse> {
507+
}): Promise<EventSuccessResponse> {
498508
if (!userId && !email)
499509
throw "You must provide an `email` or `userId` value.";
500510
const payload: {
@@ -544,7 +554,7 @@ class LoopsClient {
544554
addToAudience?: boolean;
545555
dataVariables?: TransactionalVariables;
546556
attachments?: Array<TransactionalAttachment>;
547-
}): Promise<TransactionalResponse> {
557+
}): Promise<TransactionalSuccess> {
548558
const payload = {
549559
transactionalId,
550560
email,

0 commit comments

Comments
 (0)