mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 08:28:19 +01:00
refactor(api): ♻️ Reduce complexity of a few functions
This commit is contained in:
parent
25ea870f71
commit
411fcd8af5
|
|
@ -45,20 +45,17 @@ const objectToFormData = (
|
||||||
} else if (Array.isArray(value)) {
|
} else if (Array.isArray(value)) {
|
||||||
for (const [index, item] of value.entries()) {
|
for (const [index, item] of value.entries()) {
|
||||||
const arrayKey = `${fullKey}[${index}]`;
|
const arrayKey = `${fullKey}[${index}]`;
|
||||||
|
|
||||||
if (item instanceof File) {
|
if (item instanceof File) {
|
||||||
formData.append(arrayKey, item as Blob);
|
formData.append(arrayKey, item);
|
||||||
} else if (typeof item === "object") {
|
} else if (typeof item === "object") {
|
||||||
objectToFormData(
|
objectToFormData(item, formData, arrayKey);
|
||||||
item as ConvertibleObject,
|
|
||||||
formData,
|
|
||||||
arrayKey,
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
formData.append(arrayKey, String(item));
|
formData.append(arrayKey, String(item));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (typeof value === "object") {
|
} else if (typeof value === "object") {
|
||||||
objectToFormData(value as ConvertibleObject, formData, fullKey);
|
objectToFormData(value, formData, fullKey);
|
||||||
} else {
|
} else {
|
||||||
formData.append(fullKey, String(value));
|
formData.append(fullKey, String(value));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -856,32 +856,30 @@ export class Client extends BaseClient {
|
||||||
): Promise<Output<z.infer<typeof Status>[]>> {
|
): Promise<Output<z.infer<typeof Status>[]>> {
|
||||||
const params = new URLSearchParams();
|
const params = new URLSearchParams();
|
||||||
|
|
||||||
if (options) {
|
if (options?.max_id) {
|
||||||
if (options.max_id) {
|
|
||||||
params.set("max_id", options.max_id);
|
params.set("max_id", options.max_id);
|
||||||
}
|
}
|
||||||
if (options.min_id) {
|
if (options?.min_id) {
|
||||||
params.set("min_id", options.min_id);
|
params.set("min_id", options.min_id);
|
||||||
}
|
}
|
||||||
if (options.since_id) {
|
if (options?.since_id) {
|
||||||
params.set("since_id", options.since_id);
|
params.set("since_id", options.since_id);
|
||||||
}
|
}
|
||||||
if (options.limit) {
|
if (options?.limit) {
|
||||||
params.set("limit", options.limit.toString());
|
params.set("limit", options.limit.toString());
|
||||||
}
|
}
|
||||||
if (options.only_media) {
|
if (options?.only_media) {
|
||||||
params.set("only_media", "true");
|
params.set("only_media", "true");
|
||||||
}
|
}
|
||||||
if (options.pinned) {
|
if (options?.pinned) {
|
||||||
params.set("pinned", "true");
|
params.set("pinned", "true");
|
||||||
}
|
}
|
||||||
if (options.exclude_replies) {
|
if (options?.exclude_replies) {
|
||||||
params.set("exclude_replies", "true");
|
params.set("exclude_replies", "true");
|
||||||
}
|
}
|
||||||
if (options.exclude_reblogs) {
|
if (options?.exclude_reblogs) {
|
||||||
params.set("exclude_reblogs", "true");
|
params.set("exclude_reblogs", "true");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return this.get<z.infer<typeof Status>[]>(
|
return this.get<z.infer<typeof Status>[]>(
|
||||||
`/api/v1/accounts/${id}/statuses?${params}`,
|
`/api/v1/accounts/${id}/statuses?${params}`,
|
||||||
|
|
@ -1681,28 +1679,26 @@ export class Client extends BaseClient {
|
||||||
): Promise<Output<z.infer<typeof Notification>[]>> {
|
): Promise<Output<z.infer<typeof Notification>[]>> {
|
||||||
const params = new URLSearchParams();
|
const params = new URLSearchParams();
|
||||||
|
|
||||||
if (options) {
|
if (options?.max_id) {
|
||||||
if (options.max_id) {
|
|
||||||
params.set("max_id", options.max_id);
|
params.set("max_id", options.max_id);
|
||||||
}
|
}
|
||||||
if (options.min_id) {
|
if (options?.min_id) {
|
||||||
params.set("min_id", options.min_id);
|
params.set("min_id", options.min_id);
|
||||||
}
|
}
|
||||||
if (options.since_id) {
|
if (options?.since_id) {
|
||||||
params.set("since_id", options.since_id);
|
params.set("since_id", options.since_id);
|
||||||
}
|
}
|
||||||
if (options.limit) {
|
if (options?.limit) {
|
||||||
params.set("limit", options.limit.toString());
|
params.set("limit", options.limit.toString());
|
||||||
}
|
}
|
||||||
if (options.exclude_types) {
|
if (options?.exclude_types) {
|
||||||
for (const type of options.exclude_types) {
|
for (const type of options.exclude_types) {
|
||||||
params.append("exclude_types[]", type);
|
params.append("exclude_types[]", type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (options.account_id) {
|
if (options?.account_id) {
|
||||||
params.set("account_id", options.account_id);
|
params.set("account_id", options.account_id);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return this.get<z.infer<typeof Notification>[]>(
|
return this.get<z.infer<typeof Notification>[]>(
|
||||||
`/api/v1/notifications?${params}`,
|
`/api/v1/notifications?${params}`,
|
||||||
|
|
@ -2686,7 +2682,7 @@ export class Client extends BaseClient {
|
||||||
*/
|
*/
|
||||||
public search(
|
public search(
|
||||||
q: string,
|
q: string,
|
||||||
options: Partial<{
|
options?: Partial<{
|
||||||
account_id: string;
|
account_id: string;
|
||||||
exclude_unreviewed: boolean;
|
exclude_unreviewed: boolean;
|
||||||
following: boolean;
|
following: boolean;
|
||||||
|
|
@ -2703,35 +2699,33 @@ export class Client extends BaseClient {
|
||||||
|
|
||||||
params.set("q", q);
|
params.set("q", q);
|
||||||
|
|
||||||
if (options) {
|
if (options?.account_id) {
|
||||||
if (options.account_id) {
|
|
||||||
params.set("account_id", options.account_id);
|
params.set("account_id", options.account_id);
|
||||||
}
|
}
|
||||||
if (options.exclude_unreviewed) {
|
if (options?.exclude_unreviewed) {
|
||||||
params.set("exclude_unreviewed", "true");
|
params.set("exclude_unreviewed", "true");
|
||||||
}
|
}
|
||||||
if (options.following) {
|
if (options?.following) {
|
||||||
params.set("following", "true");
|
params.set("following", "true");
|
||||||
}
|
}
|
||||||
if (options.limit) {
|
if (options?.limit) {
|
||||||
params.set("limit", options.limit.toString());
|
params.set("limit", options.limit.toString());
|
||||||
}
|
}
|
||||||
if (options.max_id) {
|
if (options?.max_id) {
|
||||||
params.set("max_id", options.max_id);
|
params.set("max_id", options.max_id);
|
||||||
}
|
}
|
||||||
if (options.min_id) {
|
if (options?.min_id) {
|
||||||
params.set("min_id", options.min_id);
|
params.set("min_id", options.min_id);
|
||||||
}
|
}
|
||||||
if (options.offset) {
|
if (options?.offset) {
|
||||||
params.set("offset", options.offset.toString());
|
params.set("offset", options.offset.toString());
|
||||||
}
|
}
|
||||||
if (options.resolve) {
|
if (options?.resolve) {
|
||||||
params.set("resolve", "true");
|
params.set("resolve", "true");
|
||||||
}
|
}
|
||||||
if (options.type) {
|
if (options?.type) {
|
||||||
params.set("type", options.type);
|
params.set("type", options.type);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return this.get<z.infer<typeof Search>>(
|
return this.get<z.infer<typeof Search>>(
|
||||||
`/api/v2/search?${params}`,
|
`/api/v2/search?${params}`,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue