UNPKG

masto

Version:

Mastodon API client for JavaScript, TypeScript, Node.js, browsers

49 lines (48 loc) 1.57 kB
import { PaginatorHttp } from "./paginator-http.js"; export class HttpActionDispatcher { http; hook; constructor(http, hook) { this.http = http; this.hook = hook; } dispatch(action) { if (this.hook) { action = this.hook.beforeDispatch(action); } let result = this.hook.dispatch(action); if (result !== false) { return result; } switch (action.type) { case "fetch": { result = this.http.get(action.path, action.data, action.meta); break; } case "create": { result = this.http.post(action.path, action.data, action.meta); break; } case "update": { result = this.http.put(action.path, action.data, action.meta); break; } case "remove": { result = this.http.delete(action.path, action.data, action.meta); break; } case "list": { result = new PaginatorHttp(this.http, action.path, action.data); break; } } /* eslint-disable unicorn/prefer-ternary, prettier/prettier */ if (result instanceof Promise) { return result.then((result) => this.hook?.afterDispatch(action, result)); } else { return this.hook.afterDispatch(action, result); } /* eslint-enable unicorn/prefer-ternary, prettier/prettier */ } }