You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
4.4 KiB
109 lines
4.4 KiB
import got from 'got';
|
|
import dotenv from 'dotenv';
|
|
dotenv.config();
|
|
|
|
let miro_app_id = process.env.MIRO_APP_ID || "";
|
|
let miro_token = process.env.MIRO_TOKEN || "";
|
|
let miro_board = process.env.MIRO_BOARD || "";
|
|
|
|
let miroCreateWidgetUrl = `https://api.miro.com/v1/boards/${miro_board}/widgets`;
|
|
export async function CreateCard(title: string, metaData?: MetaData, x?: number, y?: number): Promise<MiroWidgetCard> {
|
|
let metadata: undefined | MetaData = undefined;
|
|
if (metaData) {
|
|
metadata = {};
|
|
metadata[miro_app_id] = metaData;
|
|
};
|
|
return await got.post<MiroWidgetCard>(miroCreateWidgetUrl, {
|
|
headers: { Authorization: `Bearer ${miro_token}` },
|
|
json: { "type": "card", "title": title, metadata, x, y },
|
|
responseType: 'json',
|
|
resolveBodyOnly: true
|
|
});
|
|
// console.log(JSON.stringify({ "type": "card", "title": title, metadata }));
|
|
// console.log(response.body);
|
|
}
|
|
|
|
export async function CreateText(title: string, metaData?: MetaData, x?: number, y?: number, callback?: MiroRateLimitingCallbackType): Promise<MiroWidgetText> {
|
|
let metadata: undefined | MetaData = undefined;
|
|
if (metaData) {
|
|
metadata = {};
|
|
metadata[miro_app_id] = metaData;
|
|
};
|
|
let result = await got.post<MiroWidgetText>(miroCreateWidgetUrl, {
|
|
headers: { Authorization: `Bearer ${miro_token}` },
|
|
json: { "type": "text", "text": title, metadata, x, y },
|
|
responseType: 'json'
|
|
});
|
|
callback && callback(Number(result.headers["x-ratelimit-reset"]), Number(result.headers["x-ratelimit-remaining"]));
|
|
return result.body;
|
|
// console.log(response.body);
|
|
}
|
|
|
|
// (async () => {
|
|
// let card = await CreateCard('simple card 3', { notionId: "2" });
|
|
// console.log(JSON.stringify(card));
|
|
// card = await CreateCard('simple card 3');
|
|
// console.log(JSON.stringify(card));
|
|
// })();
|
|
|
|
type MiroRateLimitingCallbackType = (rateLimitReset: number, rateLimitRemaining: number) => void;
|
|
|
|
let miroUpdateWidgetUrl = (widget_id: string) => `https://api.miro.com/v1/boards/${miro_board}/widgets/${widget_id}`;
|
|
export async function UpdateCard(widget_id: string, title: string, metaData?: MetaData, callback?: MiroRateLimitingCallbackType): Promise<MiroWidgetCard> {
|
|
let metadata: undefined | MetaData = undefined;
|
|
if (metaData) {
|
|
metadata = {};
|
|
metadata[miro_app_id] = metaData;
|
|
};
|
|
let result = await got.patch<MiroWidgetCard>(miroUpdateWidgetUrl(widget_id), {
|
|
headers: { Authorization: `Bearer ${miro_token}` },
|
|
json: { "title": title, metadata },
|
|
responseType: 'json'
|
|
});
|
|
callback && callback(Number(result.headers["x-ratelimit-reset"]), Number(result.headers["x-ratelimit-remaining"]));
|
|
return result.body;
|
|
}
|
|
|
|
export async function UpdateText(widget_id: string, title: string, metaData?: MetaData, callback?: MiroRateLimitingCallbackType): Promise<MiroWidgetText> {
|
|
let metadata: undefined | MetaData = undefined;
|
|
if (metaData) {
|
|
metadata = {};
|
|
metadata[miro_app_id] = metaData;
|
|
};
|
|
let result = await got.patch<MiroWidgetText>(miroUpdateWidgetUrl(widget_id), {
|
|
headers: { Authorization: `Bearer ${miro_token}` },
|
|
json: { "text": title, metadata },
|
|
responseType: 'json'
|
|
});
|
|
// clg
|
|
// if (callback) {
|
|
// console.log('rate limit callback: ', Number(result.headers["x-ratelimit-reset"]), Number(result.headers["x-ratelimit-remaining"]));
|
|
callback && callback(Number(result.headers["x-ratelimit-reset"]), Number(result.headers["x-ratelimit-remaining"]));
|
|
// }
|
|
return result.body;
|
|
}
|
|
|
|
|
|
let miroListWidgetUrl = `https://api.miro.com/v1/boards/${miro_board}/widgets/`
|
|
export async function GetAllWidgets(): Promise<Collection> {
|
|
return await got<Collection>(miroListWidgetUrl, {
|
|
headers: { Authorization: `Bearer ${miro_token}` },
|
|
responseType: 'json',
|
|
resolveBodyOnly: true
|
|
});
|
|
}
|
|
|
|
export async function GetAllTexts(): Promise<Collection> {
|
|
return await got<Collection>(miroListWidgetUrl + "?widgetType=text", {
|
|
headers: { Authorization: `Bearer ${miro_token}` },
|
|
responseType: 'json',
|
|
resolveBodyOnly: true
|
|
});
|
|
}
|
|
|
|
// (async () => {
|
|
// let collection = await GetAllWidgets();
|
|
// collection && collection.data
|
|
// .map((widget => ({ id: widget.id, type: widget.type, metadata: widget.metadata })))
|
|
// .forEach(c => console.log(c.id, c.type, c.metadata));
|
|
// })();
|