@medalsocial/sdk 0.1.2 → 1.1.1
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/LICENSE +201 -21
- package/README.md +261 -86
- package/dist/pilot/index.d.mts +436 -0
- package/dist/pilot/index.d.ts +436 -0
- package/dist/pilot/index.js +114 -0
- package/dist/pilot/index.js.map +1 -0
- package/dist/pilot/index.mjs +89 -0
- package/dist/pilot/index.mjs.map +1 -0
- package/dist/src/index.d.mts +615 -0
- package/dist/src/index.d.ts +615 -0
- package/dist/src/index.js +394 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/index.mjs +358 -0
- package/dist/src/index.mjs.map +1 -0
- package/package.json +54 -16
- package/dist/index.d.mts +0 -108
- package/dist/index.d.ts +0 -108
- package/dist/index.js +0 -141
- package/dist/index.js.map +0 -1
- package/dist/index.mjs +0 -116
- package/dist/index.mjs.map +0 -1
package/dist/index.d.ts
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
type AuthOptions = {
|
|
2
|
-
kind: "basic";
|
|
3
|
-
clientId: string;
|
|
4
|
-
clientSecret: string;
|
|
5
|
-
} | {
|
|
6
|
-
kind: "bearer";
|
|
7
|
-
token: string;
|
|
8
|
-
};
|
|
9
|
-
interface ClientOptions {
|
|
10
|
-
baseUrl?: string;
|
|
11
|
-
auth: AuthOptions;
|
|
12
|
-
timeoutMs?: number;
|
|
13
|
-
userAgent?: string;
|
|
14
|
-
}
|
|
15
|
-
interface LeadItem {
|
|
16
|
-
name: string;
|
|
17
|
-
email: string;
|
|
18
|
-
company?: string;
|
|
19
|
-
source?: string;
|
|
20
|
-
[key: string]: unknown;
|
|
21
|
-
}
|
|
22
|
-
interface ContactNoteInput {
|
|
23
|
-
contactId: string;
|
|
24
|
-
note: string;
|
|
25
|
-
[key: string]: unknown;
|
|
26
|
-
}
|
|
27
|
-
interface NoteInput {
|
|
28
|
-
name: string;
|
|
29
|
-
email: string;
|
|
30
|
-
firstName?: string;
|
|
31
|
-
lastName?: string;
|
|
32
|
-
company?: string;
|
|
33
|
-
phone?: string;
|
|
34
|
-
content?: string;
|
|
35
|
-
metadata?: Record<string, unknown>;
|
|
36
|
-
}
|
|
37
|
-
interface CookieRecord {
|
|
38
|
-
cookie: string;
|
|
39
|
-
duration: string;
|
|
40
|
-
description: string;
|
|
41
|
-
}
|
|
42
|
-
interface CookieCategoryConsent {
|
|
43
|
-
allowed: boolean;
|
|
44
|
-
cookieRecords?: CookieRecord[];
|
|
45
|
-
}
|
|
46
|
-
interface CookieConsentInput {
|
|
47
|
-
domain: string;
|
|
48
|
-
consentStatus: "granted" | "denied" | "partial" | string;
|
|
49
|
-
consentTimestamp: string;
|
|
50
|
-
ipAddress?: string;
|
|
51
|
-
userAgent?: string;
|
|
52
|
-
cookiePreferences: {
|
|
53
|
-
necessary?: CookieCategoryConsent;
|
|
54
|
-
analytics?: CookieCategoryConsent;
|
|
55
|
-
marketing?: CookieCategoryConsent;
|
|
56
|
-
functional?: CookieCategoryConsent;
|
|
57
|
-
[key: string]: CookieCategoryConsent | undefined;
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
interface EventSignupInput {
|
|
61
|
-
contact: {
|
|
62
|
-
name: string;
|
|
63
|
-
email: string;
|
|
64
|
-
company?: string;
|
|
65
|
-
};
|
|
66
|
-
event: {
|
|
67
|
-
externalId: string;
|
|
68
|
-
name: string;
|
|
69
|
-
description?: string;
|
|
70
|
-
time: string;
|
|
71
|
-
location?: string;
|
|
72
|
-
thumbnail?: string;
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
interface ApiResponse<T> {
|
|
76
|
-
status: number;
|
|
77
|
-
data: T;
|
|
78
|
-
headers: Record<string, string>;
|
|
79
|
-
}
|
|
80
|
-
interface TransactionalEmailInput {
|
|
81
|
-
to: string;
|
|
82
|
-
slug: string;
|
|
83
|
-
additionalData?: Record<string, unknown>;
|
|
84
|
-
}
|
|
85
|
-
declare class MedalSocialClient {
|
|
86
|
-
private readonly baseUrl;
|
|
87
|
-
private readonly auth;
|
|
88
|
-
private readonly timeoutMs;
|
|
89
|
-
private readonly userAgent;
|
|
90
|
-
constructor(options: ClientOptions);
|
|
91
|
-
/** Create one or more leads */
|
|
92
|
-
createLead<T = unknown>(items: LeadItem[]): Promise<ApiResponse<T>>;
|
|
93
|
-
/** Create a note attached to a contact by id */
|
|
94
|
-
createContactNote<T = unknown>(input: ContactNoteInput): Promise<ApiResponse<T>>;
|
|
95
|
-
/** Record a user’s cookie consent preferences */
|
|
96
|
-
createCookieConsent<T = unknown>(input: CookieConsentInput): Promise<ApiResponse<T>>;
|
|
97
|
-
/** Create an event signup with contact and event details */
|
|
98
|
-
createEventSignup<T = unknown>(input: EventSignupInput): Promise<ApiResponse<T>>;
|
|
99
|
-
/** Create a free-form note for inbound messages */
|
|
100
|
-
createNote<T = unknown>(input: NoteInput): Promise<ApiResponse<T>>;
|
|
101
|
-
/** Send a transactional email by template slug */
|
|
102
|
-
sendTransactionalEmail<T = unknown>(input: TransactionalEmailInput): Promise<ApiResponse<T>>;
|
|
103
|
-
private post;
|
|
104
|
-
private fetchWithAuth;
|
|
105
|
-
private handleResponse;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
export { type ApiResponse, type AuthOptions, type ClientOptions, type ContactNoteInput, type CookieCategoryConsent, type CookieConsentInput, type CookieRecord, type EventSignupInput, type LeadItem, MedalSocialClient, type NoteInput, type TransactionalEmailInput, MedalSocialClient as default };
|
package/dist/index.js
DELETED
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var index_exports = {};
|
|
22
|
-
__export(index_exports, {
|
|
23
|
-
MedalSocialClient: () => MedalSocialClient,
|
|
24
|
-
default: () => index_default
|
|
25
|
-
});
|
|
26
|
-
module.exports = __toCommonJS(index_exports);
|
|
27
|
-
var MedalSocialClient = class {
|
|
28
|
-
baseUrl;
|
|
29
|
-
auth;
|
|
30
|
-
timeoutMs;
|
|
31
|
-
userAgent;
|
|
32
|
-
constructor(options) {
|
|
33
|
-
this.baseUrl = (options.baseUrl ?? "https://api.medalsocial.com").replace(/\/$/, "");
|
|
34
|
-
this.auth = options.auth;
|
|
35
|
-
this.timeoutMs = options.timeoutMs ?? 3e4;
|
|
36
|
-
this.userAgent = options.userAgent ?? "medal-social-sdk/0.1.0 (+https://github.com/Medal-Social/MedalSocial.git)";
|
|
37
|
-
}
|
|
38
|
-
// Public endpoints
|
|
39
|
-
/** Create one or more leads */
|
|
40
|
-
async createLead(items) {
|
|
41
|
-
return this.post("/v1/leads", items);
|
|
42
|
-
}
|
|
43
|
-
/** Create a note attached to a contact by id */
|
|
44
|
-
async createContactNote(input) {
|
|
45
|
-
return this.post("/v1/contacts/notes", input);
|
|
46
|
-
}
|
|
47
|
-
/** Record a user’s cookie consent preferences */
|
|
48
|
-
async createCookieConsent(input) {
|
|
49
|
-
return this.post("/v1/cookie-consent", input);
|
|
50
|
-
}
|
|
51
|
-
/** Create an event signup with contact and event details */
|
|
52
|
-
async createEventSignup(input) {
|
|
53
|
-
return this.post("/v1/event-signup", input);
|
|
54
|
-
}
|
|
55
|
-
/** Create a free-form note for inbound messages */
|
|
56
|
-
async createNote(input) {
|
|
57
|
-
return this.post("/v1/notes", input);
|
|
58
|
-
}
|
|
59
|
-
/** Send a transactional email by template slug */
|
|
60
|
-
async sendTransactionalEmail(input) {
|
|
61
|
-
return this.post("/v1/send-transactional-email", input);
|
|
62
|
-
}
|
|
63
|
-
// Internal HTTP helpers
|
|
64
|
-
async post(path, body) {
|
|
65
|
-
const maxAttempts = 3;
|
|
66
|
-
let attempt = 0;
|
|
67
|
-
while (attempt < maxAttempts) {
|
|
68
|
-
attempt++;
|
|
69
|
-
const res = await this.fetchWithAuth(path, {
|
|
70
|
-
method: "POST",
|
|
71
|
-
headers: { "content-type": "application/json" },
|
|
72
|
-
body: JSON.stringify(body)
|
|
73
|
-
});
|
|
74
|
-
if (res.status === 429 || res.status >= 500 && res.status <= 599) {
|
|
75
|
-
if (attempt < maxAttempts) {
|
|
76
|
-
const retryAfter = res.headers.get("retry-after");
|
|
77
|
-
let delayMs = 0;
|
|
78
|
-
if (retryAfter) {
|
|
79
|
-
const seconds = Number(retryAfter);
|
|
80
|
-
delayMs = Number.isFinite(seconds) ? seconds * 1e3 : 0;
|
|
81
|
-
}
|
|
82
|
-
if (delayMs <= 0) {
|
|
83
|
-
delayMs = 250 * attempt;
|
|
84
|
-
}
|
|
85
|
-
await new Promise((r) => setTimeout(r, delayMs));
|
|
86
|
-
continue;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
return this.handleResponse(res);
|
|
90
|
-
}
|
|
91
|
-
throw new Error("Request failed after retries");
|
|
92
|
-
}
|
|
93
|
-
async fetchWithAuth(path, init) {
|
|
94
|
-
const url = `${this.baseUrl}${path}`;
|
|
95
|
-
const headers = new Headers(init.headers);
|
|
96
|
-
try {
|
|
97
|
-
headers.set("user-agent", this.userAgent);
|
|
98
|
-
} catch {
|
|
99
|
-
}
|
|
100
|
-
if (this.auth.kind === "bearer") {
|
|
101
|
-
headers.set("authorization", `Bearer ${this.auth.token}`);
|
|
102
|
-
} else if (this.auth.kind === "basic") {
|
|
103
|
-
headers.set("Client-Id", this.auth.clientId);
|
|
104
|
-
headers.set("Client-Secret", this.auth.clientSecret);
|
|
105
|
-
}
|
|
106
|
-
const controller = new AbortController();
|
|
107
|
-
const timeout = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
108
|
-
try {
|
|
109
|
-
const res = await fetch(url, { ...init, headers, signal: controller.signal });
|
|
110
|
-
return res;
|
|
111
|
-
} finally {
|
|
112
|
-
clearTimeout(timeout);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
async handleResponse(res) {
|
|
116
|
-
const text = await res.text();
|
|
117
|
-
let parsed = void 0;
|
|
118
|
-
try {
|
|
119
|
-
parsed = text ? JSON.parse(text) : void 0;
|
|
120
|
-
} catch {
|
|
121
|
-
parsed = text;
|
|
122
|
-
}
|
|
123
|
-
const headers = {};
|
|
124
|
-
res.headers.forEach((v, k) => {
|
|
125
|
-
headers[k] = v;
|
|
126
|
-
});
|
|
127
|
-
if (!res.ok) {
|
|
128
|
-
const error = new Error(`HTTP ${res.status}: ${res.statusText}`);
|
|
129
|
-
error.status = res.status;
|
|
130
|
-
error.details = parsed;
|
|
131
|
-
throw error;
|
|
132
|
-
}
|
|
133
|
-
return { status: res.status, data: parsed, headers };
|
|
134
|
-
}
|
|
135
|
-
};
|
|
136
|
-
var index_default = MedalSocialClient;
|
|
137
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
138
|
-
0 && (module.exports = {
|
|
139
|
-
MedalSocialClient
|
|
140
|
-
});
|
|
141
|
-
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export type AuthOptions =\n | { kind: \"basic\"; clientId: string; clientSecret: string }\n | { kind: \"bearer\"; token: string };\n\nexport interface ClientOptions {\n baseUrl?: string;\n auth: AuthOptions;\n timeoutMs?: number;\n userAgent?: string;\n}\n\nexport interface LeadItem {\n name: string;\n email: string;\n company?: string;\n source?: string;\n [key: string]: unknown;\n}\n\nexport interface ContactNoteInput {\n contactId: string;\n note: string;\n [key: string]: unknown;\n}\n\nexport interface NoteInput {\n name: string;\n email: string;\n firstName?: string;\n lastName?: string;\n company?: string;\n phone?: string;\n content?: string;\n metadata?: Record<string, unknown>;\n}\n\nexport interface CookieRecord {\n cookie: string;\n duration: string;\n description: string;\n}\n\nexport interface CookieCategoryConsent {\n allowed: boolean;\n cookieRecords?: CookieRecord[];\n}\n\nexport interface CookieConsentInput {\n domain: string;\n consentStatus: \"granted\" | \"denied\" | \"partial\" | string;\n consentTimestamp: string; // ISO-8601\n ipAddress?: string;\n userAgent?: string;\n cookiePreferences: {\n necessary?: CookieCategoryConsent;\n analytics?: CookieCategoryConsent;\n marketing?: CookieCategoryConsent;\n functional?: CookieCategoryConsent;\n [key: string]: CookieCategoryConsent | undefined;\n };\n}\n\nexport interface EventSignupInput {\n contact: {\n name: string;\n email: string;\n company?: string;\n };\n event: {\n externalId: string;\n name: string;\n description?: string;\n time: string; // ISO-8601\n location?: string;\n thumbnail?: string;\n };\n}\n\nexport interface ApiResponse<T> {\n status: number;\n data: T;\n headers: Record<string, string>;\n}\n\nexport interface TransactionalEmailInput {\n to: string;\n slug: string;\n additionalData?: Record<string, unknown>;\n}\n\nexport class MedalSocialClient {\n private readonly baseUrl: string;\n private readonly auth: AuthOptions;\n private readonly timeoutMs: number;\n private readonly userAgent: string;\n\n constructor(options: ClientOptions) {\n this.baseUrl = (options.baseUrl ?? \"https://api.medalsocial.com\").replace(/\\/$/, \"\");\n this.auth = options.auth;\n this.timeoutMs = options.timeoutMs ?? 30000;\n this.userAgent =\n options.userAgent ??\n \"medal-social-sdk/0.1.0 (+https://github.com/Medal-Social/MedalSocial.git)\";\n }\n\n // Public endpoints\n /** Create one or more leads */\n async createLead<T = unknown>(items: LeadItem[]): Promise<ApiResponse<T>> {\n return this.post<T>(\"/v1/leads\", items);\n }\n\n /** Create a note attached to a contact by id */\n async createContactNote<T = unknown>(input: ContactNoteInput): Promise<ApiResponse<T>> {\n return this.post<T>(\"/v1/contacts/notes\", input);\n }\n\n /** Record a user’s cookie consent preferences */\n async createCookieConsent<T = unknown>(input: CookieConsentInput): Promise<ApiResponse<T>> {\n return this.post<T>(\"/v1/cookie-consent\", input);\n }\n\n /** Create an event signup with contact and event details */\n async createEventSignup<T = unknown>(input: EventSignupInput): Promise<ApiResponse<T>> {\n return this.post<T>(\"/v1/event-signup\", input);\n }\n\n /** Create a free-form note for inbound messages */\n async createNote<T = unknown>(input: NoteInput): Promise<ApiResponse<T>> {\n return this.post<T>(\"/v1/notes\", input);\n }\n\n /** Send a transactional email by template slug */\n async sendTransactionalEmail<T = unknown>(\n input: TransactionalEmailInput,\n ): Promise<ApiResponse<T>> {\n return this.post<T>(\"/v1/send-transactional-email\", input);\n }\n\n // Internal HTTP helpers\n private async post<T>(path: string, body: unknown): Promise<ApiResponse<T>> {\n const maxAttempts = 3;\n let attempt = 0;\n\n while (attempt < maxAttempts) {\n attempt++;\n const res = await this.fetchWithAuth(path, {\n method: \"POST\",\n headers: { \"content-type\": \"application/json\" },\n body: JSON.stringify(body),\n });\n\n // Retry on 429/5xx with basic backoff and Retry-After support\n if (res.status === 429 || (res.status >= 500 && res.status <= 599)) {\n if (attempt < maxAttempts) {\n const retryAfter = res.headers.get(\"retry-after\");\n let delayMs = 0;\n if (retryAfter) {\n const seconds = Number(retryAfter);\n delayMs = Number.isFinite(seconds) ? seconds * 1000 : 0;\n }\n if (delayMs <= 0) {\n delayMs = 250 * attempt; // linear backoff\n }\n await new Promise((r) => setTimeout(r, delayMs));\n continue;\n }\n }\n\n return this.handleResponse<T>(res);\n }\n\n throw new Error(\"Request failed after retries\");\n }\n\n private async fetchWithAuth(path: string, init: RequestInit): Promise<Response> {\n const url = `${this.baseUrl}${path}`;\n const headers = new Headers(init.headers);\n try {\n headers.set(\"user-agent\", this.userAgent);\n } catch {\n // Some environments (e.g., browsers) disallow setting user-agent; ignore.\n }\n\n if (this.auth.kind === \"bearer\") {\n headers.set(\"authorization\", `Bearer ${this.auth.token}`);\n } else if (this.auth.kind === \"basic\") {\n // Medal Social API expects explicit headers for client credentials\n headers.set(\"Client-Id\", this.auth.clientId);\n headers.set(\"Client-Secret\", this.auth.clientSecret);\n }\n\n const controller = new AbortController();\n const timeout = setTimeout(() => controller.abort(), this.timeoutMs);\n try {\n const res = await fetch(url, { ...init, headers, signal: controller.signal });\n return res;\n } finally {\n clearTimeout(timeout);\n }\n }\n\n private async handleResponse<T>(res: Response): Promise<ApiResponse<T>> {\n const text = await res.text();\n let parsed: unknown = undefined;\n try {\n parsed = text ? JSON.parse(text) : undefined;\n } catch {\n parsed = text as unknown;\n }\n\n const headers: Record<string, string> = {};\n res.headers.forEach((v, k) => {\n headers[k] = v;\n });\n\n if (!res.ok) {\n const error = new Error(`HTTP ${res.status}: ${res.statusText}`) as Error & {\n status?: number;\n details?: unknown;\n };\n error.status = res.status;\n error.details = parsed;\n throw error;\n }\n\n return { status: res.status, data: parsed as T, headers };\n }\n}\n\nexport default MedalSocialClient;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA0FO,IAAM,oBAAN,MAAwB;AAAA,EACZ;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAEjB,YAAY,SAAwB;AAClC,SAAK,WAAW,QAAQ,WAAW,+BAA+B,QAAQ,OAAO,EAAE;AACnF,SAAK,OAAO,QAAQ;AACpB,SAAK,YAAY,QAAQ,aAAa;AACtC,SAAK,YACH,QAAQ,aACR;AAAA,EACJ;AAAA;AAAA;AAAA,EAIA,MAAM,WAAwB,OAA4C;AACxE,WAAO,KAAK,KAAQ,aAAa,KAAK;AAAA,E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C1D;AACF;AAEA,IAAO,gBAAQ;","names":[]}
|
package/dist/index.mjs
DELETED
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
// src/index.ts
|
|
2
|
-
var MedalSocialClient = class {
|
|
3
|
-
baseUrl;
|
|
4
|
-
auth;
|
|
5
|
-
timeoutMs;
|
|
6
|
-
userAgent;
|
|
7
|
-
constructor(options) {
|
|
8
|
-
this.baseUrl = (options.baseUrl ?? "https://api.medalsocial.com").replace(/\/$/, "");
|
|
9
|
-
this.auth = options.auth;
|
|
10
|
-
this.timeoutMs = options.timeoutMs ?? 3e4;
|
|
11
|
-
this.userAgent = options.userAgent ?? "medal-social-sdk/0.1.0 (+https://github.com/Medal-Social/MedalSocial.git)";
|
|
12
|
-
}
|
|
13
|
-
// Public endpoints
|
|
14
|
-
/** Create one or more leads */
|
|
15
|
-
async createLead(items) {
|
|
16
|
-
return this.post("/v1/leads", items);
|
|
17
|
-
}
|
|
18
|
-
/** Create a note attached to a contact by id */
|
|
19
|
-
async createContactNote(input) {
|
|
20
|
-
return this.post("/v1/contacts/notes", input);
|
|
21
|
-
}
|
|
22
|
-
/** Record a user’s cookie consent preferences */
|
|
23
|
-
async createCookieConsent(input) {
|
|
24
|
-
return this.post("/v1/cookie-consent", input);
|
|
25
|
-
}
|
|
26
|
-
/** Create an event signup with contact and event details */
|
|
27
|
-
async createEventSignup(input) {
|
|
28
|
-
return this.post("/v1/event-signup", input);
|
|
29
|
-
}
|
|
30
|
-
/** Create a free-form note for inbound messages */
|
|
31
|
-
async createNote(input) {
|
|
32
|
-
return this.post("/v1/notes", input);
|
|
33
|
-
}
|
|
34
|
-
/** Send a transactional email by template slug */
|
|
35
|
-
async sendTransactionalEmail(input) {
|
|
36
|
-
return this.post("/v1/send-transactional-email", input);
|
|
37
|
-
}
|
|
38
|
-
// Internal HTTP helpers
|
|
39
|
-
async post(path, body) {
|
|
40
|
-
const maxAttempts = 3;
|
|
41
|
-
let attempt = 0;
|
|
42
|
-
while (attempt < maxAttempts) {
|
|
43
|
-
attempt++;
|
|
44
|
-
const res = await this.fetchWithAuth(path, {
|
|
45
|
-
method: "POST",
|
|
46
|
-
headers: { "content-type": "application/json" },
|
|
47
|
-
body: JSON.stringify(body)
|
|
48
|
-
});
|
|
49
|
-
if (res.status === 429 || res.status >= 500 && res.status <= 599) {
|
|
50
|
-
if (attempt < maxAttempts) {
|
|
51
|
-
const retryAfter = res.headers.get("retry-after");
|
|
52
|
-
let delayMs = 0;
|
|
53
|
-
if (retryAfter) {
|
|
54
|
-
const seconds = Number(retryAfter);
|
|
55
|
-
delayMs = Number.isFinite(seconds) ? seconds * 1e3 : 0;
|
|
56
|
-
}
|
|
57
|
-
if (delayMs <= 0) {
|
|
58
|
-
delayMs = 250 * attempt;
|
|
59
|
-
}
|
|
60
|
-
await new Promise((r) => setTimeout(r, delayMs));
|
|
61
|
-
continue;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
return this.handleResponse(res);
|
|
65
|
-
}
|
|
66
|
-
throw new Error("Request failed after retries");
|
|
67
|
-
}
|
|
68
|
-
async fetchWithAuth(path, init) {
|
|
69
|
-
const url = `${this.baseUrl}${path}`;
|
|
70
|
-
const headers = new Headers(init.headers);
|
|
71
|
-
try {
|
|
72
|
-
headers.set("user-agent", this.userAgent);
|
|
73
|
-
} catch {
|
|
74
|
-
}
|
|
75
|
-
if (this.auth.kind === "bearer") {
|
|
76
|
-
headers.set("authorization", `Bearer ${this.auth.token}`);
|
|
77
|
-
} else if (this.auth.kind === "basic") {
|
|
78
|
-
headers.set("Client-Id", this.auth.clientId);
|
|
79
|
-
headers.set("Client-Secret", this.auth.clientSecret);
|
|
80
|
-
}
|
|
81
|
-
const controller = new AbortController();
|
|
82
|
-
const timeout = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
83
|
-
try {
|
|
84
|
-
const res = await fetch(url, { ...init, headers, signal: controller.signal });
|
|
85
|
-
return res;
|
|
86
|
-
} finally {
|
|
87
|
-
clearTimeout(timeout);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
async handleResponse(res) {
|
|
91
|
-
const text = await res.text();
|
|
92
|
-
let parsed = void 0;
|
|
93
|
-
try {
|
|
94
|
-
parsed = text ? JSON.parse(text) : void 0;
|
|
95
|
-
} catch {
|
|
96
|
-
parsed = text;
|
|
97
|
-
}
|
|
98
|
-
const headers = {};
|
|
99
|
-
res.headers.forEach((v, k) => {
|
|
100
|
-
headers[k] = v;
|
|
101
|
-
});
|
|
102
|
-
if (!res.ok) {
|
|
103
|
-
const error = new Error(`HTTP ${res.status}: ${res.statusText}`);
|
|
104
|
-
error.status = res.status;
|
|
105
|
-
error.details = parsed;
|
|
106
|
-
throw error;
|
|
107
|
-
}
|
|
108
|
-
return { status: res.status, data: parsed, headers };
|
|
109
|
-
}
|
|
110
|
-
};
|
|
111
|
-
var index_default = MedalSocialClient;
|
|
112
|
-
export {
|
|
113
|
-
MedalSocialClient,
|
|
114
|
-
index_default as default
|
|
115
|
-
};
|
|
116
|
-
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export type AuthOptions =\n | { kind: \"basic\"; clientId: string; clientSecret: string }\n | { kind: \"bearer\"; token: string };\n\nexport interface ClientOptions {\n baseUrl?: string;\n auth: AuthOptions;\n timeoutMs?: number;\n userAgent?: string;\n}\n\nexport interface LeadItem {\n name: string;\n email: string;\n company?: string;\n source?: string;\n [key: string]: unknown;\n}\n\nexport interface ContactNoteInput {\n contactId: string;\n note: string;\n [key: string]: unknown;\n}\n\nexport interface NoteInput {\n name: string;\n email: string;\n firstName?: string;\n lastName?: string;\n company?: string;\n phone?: string;\n content?: string;\n metadata?: Record<string, unknown>;\n}\n\nexport interface CookieRecord {\n cookie: string;\n duration: string;\n description: string;\n}\n\nexport interface CookieCategoryConsent {\n allowed: boolean;\n cookieRecords?: CookieRecord[];\n}\n\nexport interface CookieConsentInput {\n domain: string;\n consentStatus: \"granted\" | \"denied\" | \"partial\" | string;\n consentTimestamp: string; // ISO-8601\n ipAddress?: string;\n userAgent?: string;\n cookiePreferences: {\n necessary?: CookieCategoryConsent;\n analytics?: CookieCategoryConsent;\n marketing?: CookieCategoryConsent;\n functional?: CookieCategoryConsent;\n [key: string]: CookieCategoryConsent | undefined;\n };\n}\n\nexport interface EventSignupInput {\n contact: {\n name: string;\n email: string;\n company?: string;\n };\n event: {\n externalId: string;\n name: string;\n description?: string;\n time: string; // ISO-8601\n location?: string;\n thumbnail?: string;\n };\n}\n\nexport interface ApiResponse<T> {\n status: number;\n data: T;\n headers: Record<string, string>;\n}\n\nexport interface TransactionalEmailInput {\n to: string;\n slug: string;\n additionalData?: Record<string, unknown>;\n}\n\nexport class MedalSocialClient {\n private readonly baseUrl: string;\n private readonly auth: AuthOptions;\n private readonly timeoutMs: number;\n private readonly userAgent: string;\n\n constructor(options: ClientOptions) {\n this.baseUrl = (options.baseUrl ?? \"https://api.medalsocial.com\").replace(/\\/$/, \"\");\n this.auth = options.auth;\n this.timeoutMs = options.timeoutMs ?? 30000;\n this.userAgent =\n options.userAgent ??\n \"medal-social-sdk/0.1.0 (+https://github.com/Medal-Social/MedalSocial.git)\";\n }\n\n // Public endpoints\n /** Create one or more leads */\n async createLead<T = unknown>(items: LeadItem[]): Promise<ApiResponse<T>> {\n return this.post<T>(\"/v1/leads\", items);\n }\n\n /** Create a note attached to a contact by id */\n async createContactNote<T = unknown>(input: ContactNoteInput): Promise<ApiResponse<T>> {\n return this.post<T>(\"/v1/contacts/notes\", input);\n }\n\n /** Record a user’s cookie consent preferences */\n async createCookieConsent<T = unknown>(input: CookieConsentInput): Promise<ApiResponse<T>> {\n return this.post<T>(\"/v1/cookie-consent\", input);\n }\n\n /** Create an event signup with contact and event details */\n async createEventSignup<T = unknown>(input: EventSignupInput): Promise<ApiResponse<T>> {\n return this.post<T>(\"/v1/event-signup\", input);\n }\n\n /** Create a free-form note for inbound messages */\n async createNote<T = unknown>(input: NoteInput): Promise<ApiResponse<T>> {\n return this.post<T>(\"/v1/notes\", input);\n }\n\n /** Send a transactional email by template slug */\n async sendTransactionalEmail<T = unknown>(\n input: TransactionalEmailInput,\n ): Promise<ApiResponse<T>> {\n return this.post<T>(\"/v1/send-transactional-email\", input);\n }\n\n // Internal HTTP helpers\n private async post<T>(path: string, body: unknown): Promise<ApiResponse<T>> {\n const maxAttempts = 3;\n let attempt = 0;\n\n while (attempt < maxAttempts) {\n attempt++;\n const res = await this.fetchWithAuth(path, {\n method: \"POST\",\n headers: { \"content-type\": \"application/json\" },\n body: JSON.stringify(body),\n });\n\n // Retry on 429/5xx with basic backoff and Retry-After support\n if (res.status === 429 || (res.status >= 500 && res.status <= 599)) {\n if (attempt < maxAttempts) {\n const retryAfter = res.headers.get(\"retry-after\");\n let delayMs = 0;\n if (retryAfter) {\n const seconds = Number(retryAfter);\n delayMs = Number.isFinite(seconds) ? seconds * 1000 : 0;\n }\n if (delayMs <= 0) {\n delayMs = 250 * attempt; // linear backoff\n }\n await new Promise((r) => setTimeout(r, delayMs));\n continue;\n }\n }\n\n return this.handleResponse<T>(res);\n }\n\n throw new Error(\"Request failed after retries\");\n }\n\n private async fetchWithAuth(path: string, init: RequestInit): Promise<Response> {\n const url = `${this.baseUrl}${path}`;\n const headers = new Headers(init.headers);\n try {\n headers.set(\"user-agent\", this.userAgent);\n } catch {\n // Some environments (e.g., browsers) disallow setting user-agent; ignore.\n }\n\n if (this.auth.kind === \"bearer\") {\n headers.set(\"authorization\", `Bearer ${this.auth.token}`);\n } else if (this.auth.kind === \"basic\") {\n // Medal Social API expects explicit headers for client credentials\n headers.set(\"Client-Id\", this.auth.clientId);\n headers.set(\"Client-Secret\", this.auth.clientSecret);\n }\n\n const controller = new AbortController();\n const timeout = setTimeout(() => controller.abort(), this.timeoutMs);\n try {\n const res = await fetch(url, { ...init, headers, signal: controller.signal });\n return res;\n } finally {\n clearTimeout(timeout);\n }\n }\n\n private async handleResponse<T>(res: Response): Promise<ApiResponse<T>> {\n const text = await res.text();\n let parsed: unknown = undefined;\n try {\n parsed = text ? JSON.parse(text) : undefined;\n } catch {\n parsed = text as unknown;\n }\n\n const headers: Record<string, string> = {};\n res.headers.forEach((v, k) => {\n headers[k] = v;\n });\n\n if (!res.ok) {\n const error = new Error(`HTTP ${res.status}: ${res.statusText}`) as Error & {\n status?: number;\n details?: unknown;\n };\n error.status = res.status;\n error.details = parsed;\n throw error;\n }\n\n return { status: res.status, data: parsed as T, headers };\n }\n}\n\nexport default MedalSocialClient;\n"],"mappings":";AA0FO,IAAM,oBAAN,MAAwB;AAAA,EACZ;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAEjB,YAAY,SAAwB;AAClC,SAAK,WAAW,QAAQ,WAAW,+BAA+B,QAAQ,OAAO,EAAE;AACnF,SAAK,OAAO,QAAQ;AACpB,SAAK,YAAY,QAAQ,aAAa;AACtC,SAAK,YACH,QAAQ,aACR;AAAA,EACJ;AAAA;AAAA;AAAA,EAIA,MAAM,WAAwB,OAA4C;AACxE,WAAO,KAAK,KAAQ,aAAa,KAAK;AAAA,EACxC;AAAA;AAAA,EAGA,MAAM,kBAA+B,OAAkD;AACrF,WAAO,KAAK,KAAQ,sBAAsB,KAAK;AAAA,EACjD;AAAA;AAAA,EAGA,MAAM,oBAAiC,OAAoD;AACzF,WAAO,KAAK,KAAQ,sBAAsB,KAAK;AAAA,EACjD;AAAA;AAAA,EAGA,MAAM,kBAA+B,OAAkD;AACrF,WAAO,KAAK,KAAQ,oBAAoB,KAAK;AAAA,EAC/C;AAAA;AAAA,EAGA,MAAM,WAAwB,OAA2C;AACvE,WAAO,KAAK,KAAQ,aAAa,KAAK;AAAA,EACxC;AAAA;AAAA,EAGA,MAAM,uBACJ,OACyB;AACzB,WAAO,KAAK,KAAQ,gCAAgC,KAAK;AAAA,EAC3D;AAAA;AAAA,EAGA,MAAc,KAAQ,MAAc,MAAwC;AAC1E,UAAM,cAAc;AACpB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