元のコード

  • apiPostForm や apiGet が直接書かれている。
  • apiv3 化するのと、SWR についても考える
/** * the upload event handler * @param {any} file */ const uploadHandler = useCallback(async(file) => { try { // eslint-disable-next-line @typescript-eslint/no-explicit-any let res: any = await apiGet('/attachments.limit', { fileSize: file.size, }); if (!res.isUploadable) { throw new Error(res.errorMessage); } const formData = new FormData(); ... res = await apiPostForm('/attachments.add', formData); const attachment = res.attachment; const fileName = attachment.originalName; ... }, [codeMirrorEditor, currentPagePath, mutateCurrentPage, mutateCurrentPageId, mutateIsLatestRevision, pageId]);

手順

  • 調査

    • apiv1 と apiv3 の違い
    • api とその実装
    • SWR とどんな関係がある、ない、
  • 実装

    • apiv3 化
    • できるなら SWR 化

メモ

  • axios
What is Axios? Axios is a promise-based HTTP Client for node.js and the browser. It is isomorphic (= it can run in the browser and nodejs with the same codebase). On the server-side it uses the native node.js http module, while on the client (browser) it uses XMLHttpRequests