meme-generator-rs-api
Version:
API Wrapper for MemeCrafters/meme-generator-rs using @cordisjs/plugin-http
350 lines (346 loc) • 9.21 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
// src/types.ts
var types_exports = {};
__export(types_exports, {
errorCodeDesc: () => errorCodeDesc,
memeListSortByVals: () => memeListSortByVals,
possibleMemeErrorCodes: () => possibleMemeErrorCodes
});
var memeListSortByVals = [
"key",
"keywords",
"keywords_pinyin",
"date_created",
"date_modified"
];
var errorCodeDesc = {
410: "RequestError",
420: "IOError",
510: "ImageDecodeError",
520: "ImageEncodeError",
530: "ImageAssetMissingError",
540: "DeserializeError",
550: "ImageNumberMismatchError",
551: "TextNumberMismatchError",
560: "TextOverLengthError",
570: "MemeFeedbackError"
};
var possibleMemeErrorCodes = Object.keys(errorCodeDesc).map(
(v) => parseInt(v, 10)
);
// src/error.ts
import { HTTP } from "@cordisjs/plugin-http";
var MemeError = class extends Error {
constructor(httpStatus, message) {
super(`(${httpStatus}) ${message}`);
this.httpStatus = httpStatus;
this.message = message;
this.name = "MemeError";
}
static {
__name(this, "MemeError");
}
};
var MemeDetailedError = class _MemeDetailedError extends MemeError {
constructor(data) {
super(
_MemeDetailedError.httpStatus,
`${errorCodeDesc[data.code] ?? "UnknownError"} (${data.code}): ${data.message}`
);
this.data = data;
this.name = "MemeDetailedError";
}
static {
__name(this, "MemeDetailedError");
}
};
((MemeDetailedError2) => {
MemeDetailedError2.httpStatus = 500;
})(MemeDetailedError || (MemeDetailedError = {}));
((MemeError2) => {
MemeError2.Detailed = MemeDetailedError;
function constructFromHTTPError(error) {
const { response } = error;
if (!response) return void 0;
const errResp = response;
const dataIsObj = typeof errResp.data === "object";
if (errResp.status === MemeError2.Detailed.httpStatus && dataIsObj) {
try {
return new MemeDetailedError(errResp.data);
} catch (_) {
}
}
return new MemeError2(
errResp.status,
dataIsObj ? JSON.stringify(errResp.data) : errResp.data
);
}
MemeError2.constructFromHTTPError = constructFromHTTPError;
__name(constructFromHTTPError, "constructFromHTTPError");
function promiseCatchHandler(e) {
if (HTTP.Error.is(e)) {
const memeError = constructFromHTTPError(e);
if (memeError) throw memeError;
}
throw e;
}
MemeError2.promiseCatchHandler = promiseCatchHandler;
__name(promiseCatchHandler, "promiseCatchHandler");
async function catchWrapper(func) {
try {
return await func();
} catch (e) {
MemeError2.promiseCatchHandler(e);
}
}
MemeError2.catchWrapper = catchWrapper;
__name(catchWrapper, "catchWrapper");
})(MemeError || (MemeError = {}));
// src/api.ts
async function reqCatchWrapper(fn) {
return (await MemeError.catchWrapper(fn)).data;
}
__name(reqCatchWrapper, "reqCatchWrapper");
var MemeAPI = class _MemeAPI {
constructor(http) {
this.http = http;
this.imgOps = new _MemeAPI.ImageOperations(http);
}
static {
__name(this, "MemeAPI");
}
imgOps;
async uploadImage(data) {
return reqCatchWrapper(
() => this.http(`/image/upload`, {
method: "POST",
responseType: "json",
data
})
);
}
async getImage(imageId) {
return reqCatchWrapper(
() => this.http(`/image/${imageId}`, {
method: "GET",
responseType: "blob"
})
);
}
async getVersion() {
return reqCatchWrapper(
() => this.http(`/meme/version`, { method: "GET", responseType: "text" })
);
}
async getKeys() {
return reqCatchWrapper(
() => this.http(`/meme/keys`, { method: "GET", responseType: "json" })
);
}
async getInfos() {
return reqCatchWrapper(
() => this.http(`/meme/infos`, { method: "GET", responseType: "json" })
);
}
async searchMemes(query, includeTags) {
return reqCatchWrapper(
() => this.http(`/meme/search`, {
method: "GET",
params: { query, include_tags: includeTags },
responseType: "json"
})
);
}
async getInfo(key) {
return reqCatchWrapper(
() => this.http(`/memes/${key}/info`, { method: "GET", responseType: "json" })
);
}
async renderPreview(key) {
return reqCatchWrapper(
() => this.http(`/memes/${key}/preview`, {
method: "GET",
responseType: "json"
})
);
}
async renderMeme(key, data) {
return reqCatchWrapper(
() => this.http(`/memes/${key}`, {
method: "POST",
responseType: "json",
data
})
);
}
async renderList(data) {
return reqCatchWrapper(
() => this.http(`/tools/render_list`, {
method: "POST",
responseType: "json",
data
})
);
}
async renderStatistics(data) {
return reqCatchWrapper(
() => this.http(`/tools/render_statistics`, {
method: "POST",
responseType: "json",
data
})
);
}
};
((MemeAPI2) => {
class ImageOperations {
constructor(http) {
this.http = http;
}
static {
__name(this, "ImageOperations");
}
async inspect(imageId) {
return reqCatchWrapper(
() => this.http(`/tools/image_operations/inspect`, {
method: "POST",
responseType: "json",
data: { image_id: imageId }
})
);
}
async flipHorizontal(imageId) {
return reqCatchWrapper(
() => this.http(`/tools/image_operations/flip_horizontal`, {
method: "POST",
responseType: "json",
data: { image_id: imageId }
})
);
}
async flipVertical(imageId) {
return reqCatchWrapper(
() => this.http(`/tools/image_operations/flip_vertical`, {
method: "POST",
responseType: "json",
data: { image_id: imageId }
})
);
}
async rotate(imageId, extra) {
return reqCatchWrapper(
() => this.http(`/tools/image_operations/rotate`, {
method: "POST",
responseType: "json",
data: { image_id: imageId, ...extra }
})
);
}
async resize(imageId, extra) {
return reqCatchWrapper(
() => this.http(`/tools/image_operations/resize`, {
method: "POST",
responseType: "json",
data: { image_id: imageId, ...extra }
})
);
}
async crop(imageId, extra) {
return reqCatchWrapper(
() => this.http(`/tools/image_operations/crop`, {
method: "POST",
responseType: "json",
data: { image_id: imageId, ...extra }
})
);
}
async grayscale(imageId) {
return reqCatchWrapper(
() => this.http(`/tools/image_operations/grayscale`, {
method: "POST",
responseType: "json",
data: { image_id: imageId }
})
);
}
async invert(imageId) {
return reqCatchWrapper(
() => this.http(`/tools/image_operations/invert`, {
method: "POST",
responseType: "json",
data: { image_id: imageId }
})
);
}
async mergeHorizontal(imageIds) {
return reqCatchWrapper(
() => this.http(`/tools/image_operations/merge_horizontal`, {
method: "POST",
responseType: "json",
data: { image_ids: imageIds }
})
);
}
async mergeVertical(imageIds) {
return reqCatchWrapper(
() => this.http(`/tools/image_operations/merge_vertical`, {
method: "POST",
responseType: "json",
data: { image_ids: imageIds }
})
);
}
async gifSplit(imageId) {
return reqCatchWrapper(
() => this.http(`/tools/image_operations/gif_split`, {
method: "POST",
responseType: "json",
data: { image_id: imageId }
})
);
}
async gifMerge(imageIds, extra) {
return reqCatchWrapper(
() => this.http(`/tools/image_operations/gif_merge`, {
method: "POST",
responseType: "json",
data: { image_ids: imageIds, ...extra }
})
);
}
async gifReverse(imageId) {
return reqCatchWrapper(
() => this.http(`/tools/image_operations/gif_reverse`, {
method: "POST",
responseType: "json",
data: { image_id: imageId }
})
);
}
async gifChangeDuration(imageId, extra) {
return reqCatchWrapper(
() => this.http(`/tools/image_operations/gif_change_duration`, {
method: "POST",
responseType: "json",
data: { image_id: imageId, ...extra }
})
);
}
}
MemeAPI2.ImageOperations = ImageOperations;
})(MemeAPI || (MemeAPI = {}));
export {
MemeAPI,
MemeDetailedError,
MemeError,
types_exports as MemeTypes,
errorCodeDesc,
memeListSortByVals,
possibleMemeErrorCodes,
reqCatchWrapper
};