mirror of
https://github.com/bknd-io/bknd/
synced 2026-08-03 16:46:00 +00:00
modify MediaApi to support custom fetch implementation, defaults to native fetch (#158)
* modify MediaApi to support custom fetch implementation, defaults to native fetch added an optional `fetcher` parameter to allow usage of a custom fetch function in both `upload` and `fetcher` methods. Defaults to the standard `fetch` if none is provided. * fix tests and improve api fetcher types
This commit is contained in:
@@ -6,13 +6,17 @@ import {
|
||||
type TInput,
|
||||
} from "modules/ModuleApi";
|
||||
import type { FileWithPath } from "ui/elements/media/file-selector";
|
||||
import type { ApiFetcher } from "Api";
|
||||
|
||||
export type MediaApiOptions = BaseModuleApiOptions & {};
|
||||
export type MediaApiOptions = BaseModuleApiOptions & {
|
||||
upload_fetcher: ApiFetcher;
|
||||
};
|
||||
|
||||
export class MediaApi extends ModuleApi<MediaApiOptions> {
|
||||
protected override getDefaultOptions(): Partial<MediaApiOptions> {
|
||||
return {
|
||||
basepath: "/api/media",
|
||||
upload_fetcher: fetch,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -109,10 +113,12 @@ export class MediaApi extends ModuleApi<MediaApiOptions> {
|
||||
filename?: string;
|
||||
_init?: Omit<RequestInit, "body">;
|
||||
path?: TInput;
|
||||
fetcher?: ApiFetcher;
|
||||
} = {},
|
||||
) {
|
||||
if (item instanceof Request || typeof item === "string") {
|
||||
const res = await this.fetcher(item);
|
||||
const fetcher = opts.fetcher ?? this.options.upload_fetcher;
|
||||
const res = await fetcher(item);
|
||||
if (!res.ok || !res.body) {
|
||||
throw new Error("Failed to fetch file");
|
||||
}
|
||||
@@ -143,6 +149,7 @@ export class MediaApi extends ModuleApi<MediaApiOptions> {
|
||||
item: Request | Response | string | File | ReadableStream,
|
||||
opts?: {
|
||||
_init?: Omit<RequestInit, "body">;
|
||||
fetcher?: typeof fetch;
|
||||
},
|
||||
) {
|
||||
return this.upload(item, {
|
||||
|
||||
Reference in New Issue
Block a user