//native
type Gorgias = {
username: string;
apiKey: string;
domain: string;
};
/**
* List events
* List events, paginated, and ordered by their creation date, with the most recently created first.
*/
export async function main(
auth: Gorgias,
created_datetime: any,
object_id: string | undefined,
types: string | undefined,
object_type:
| "Account"
| "Macro"
| "Tag"
| "Customer"
| "Team"
| "View"
| "Widget"
| "User"
| "TicketMessage"
| "Ticket"
| "Rule"
| "Integration"
| undefined,
cursor: string | undefined,
order_dir: "desc" | "asc" | undefined,
page: string | undefined,
user_ids: string | undefined,
per_page: string | undefined,
limit: string | undefined,
order_by: "created_datetime" | undefined,
) {
const url = new URL(`https://${auth.domain}.gorgias.com/api/events`);
for (const [k, v] of [
["object_id", object_id],
["types", types],
["object_type", object_type],
["cursor", cursor],
["order_dir", order_dir],
["page", page],
["user_ids", user_ids],
["per_page", per_page],
["limit", limit],
["order_by", order_by],
]) {
if (v !== undefined && v !== "" && k !== undefined) {
url.searchParams.append(k, v);
}
}
encodeParams({ created_datetime }).forEach((v, k) => {
if (v !== undefined && v !== "") {
url.searchParams.append(k, v);
}
});
const response = await fetch(url, {
method: "GET",
headers: {
Authorization: "Basic " + btoa(`${auth.username}:${auth.apiKey}`),
},
body: undefined,
});
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status} ${text}`);
}
return await response.json();
}
function encodeParams(o: any) {
function iter(o: any, path: string) {
if (Array.isArray(o)) {
o.forEach(function (a) {
iter(a, path + "[]");
});
return;
}
if (o !== null && typeof o === "object") {
Object.keys(o).forEach(function (k) {
iter(o[k], path + "[" + k + "]");
});
return;
}
data.push(path + "=" + o);
}
const data: string[] = [];
Object.keys(o).forEach(function (k) {
if (o[k] !== undefined) {
iter(o[k], k);
}
});
return new URLSearchParams(data.join("&"));
}
Submitted by hugo697 235 days ago