Skip to content

Commit 7341c22

Browse files
CopilotneetCopilot
authored
feat: Add configurable mediaTimeout option for media upload polling (#1341)
* Initial plan * Add mediaTimeout configuration option to HttpConfig Co-authored-by: neet <19276905+neet@users.noreply.github.com> * Refactor mediaTimeout to use MediaConfigProps pattern Co-authored-by: neet <19276905+neet@users.noreply.github.com> * Add @experimental tag to mediaTimeout documentation Co-authored-by: neet <19276905+neet@users.noreply.github.com> * Update clients.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: neet <19276905+neet@users.noreply.github.com> Co-authored-by: Ryō Igarashi (五十嵐 涼) <n33t5hin@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 12483d6 commit 7341c22

2 files changed

Lines changed: 40 additions & 2 deletions

File tree

src/adapters/action/dispatcher-http-hook-mastodon.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,29 @@ describe("DispatcherHttp", () => {
6161
await expect(promise).rejects.toBeInstanceOf(MastoTimeoutError);
6262
});
6363

64+
it("uses custom mediaTimeout value", async () => {
65+
const http = new HttpMockImpl();
66+
const customTimeout = 100; // Short timeout for fast test
67+
const dispatcher = new HttpActionDispatcher(
68+
http,
69+
new HttpActionDispatcherHookMastodon(http, customTimeout),
70+
);
71+
72+
httpPost.mockResolvedValueOnce({ id: "1" });
73+
httpGet.mockRejectedValue(
74+
new MastoHttpError({ statusCode: 404, message: "Not Found" }),
75+
);
76+
77+
const promise = dispatcher.dispatch({
78+
type: "create",
79+
path: "/api/v2/media",
80+
data: undefined,
81+
meta: {},
82+
});
83+
84+
await expect(promise).rejects.toBeInstanceOf(MastoTimeoutError);
85+
});
86+
6487
it("rethrows errors for media processing", async () => {
6588
const http = new HttpMockImpl();
6689
const dispatcher = new HttpActionDispatcher(

src/adapters/clients.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,29 @@ interface LogConfigProps {
3434
readonly log?: LogType;
3535
}
3636

37+
interface MediaConfigProps {
38+
/**
39+
* Timeout milliseconds for media upload processing.
40+
*
41+
* When uploading media via `/api/v2/media`, the library polls the server
42+
* to wait for media processing to complete before returning. This timeout
43+
* controls how long to wait before giving up.
44+
*
45+
* Defaults to 60000 (60 seconds).
46+
*
47+
* @experimental The behavior of this option may change without any announcement.
48+
*/
49+
readonly mediaTimeout?: number;
50+
}
51+
3752
export const createRestAPIClient = (
38-
props: MastoHttpConfigProps & LogConfigProps,
53+
props: MastoHttpConfigProps & LogConfigProps & MediaConfigProps,
3954
): mastodon.rest.Client => {
4055
const serializer = new SerializerNativeImpl();
4156
const config = new HttpConfigImpl(props, serializer);
4257
const logger = createLogger(props.log);
4358
const http = new HttpNativeImpl(serializer, config, logger);
44-
const hook = new HttpActionDispatcherHookMastodon(http);
59+
const hook = new HttpActionDispatcherHookMastodon(http, props.mediaTimeout);
4560
const actionDispatcher = new HttpActionDispatcher(http, hook);
4661
const actionProxy = createActionProxy(actionDispatcher, {
4762
context: ["api"],

0 commit comments

Comments
 (0)