aspida / aspida

TypeScript friendly HTTP client wrapper for the browser and node.js.

Home Page:https://github.com/aspida/aspida

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

@aspida/swrからmutateをエクスポートし、内部でgetDefaultKeyを使って欲しい

TeXmeijin opened this issue · comments

Description

https://github.com/aspida/aspida/pull/733/files
こちらにて、getDefaultKeyという、SWRのKeyを取得できるメソッドを追加していただきましたが
これを利用する場合、SWR本体のmutateおよびgetDefaultKeyの両方をimportする(依存する)必要があります。

そこで、@aspida/swrパッケージからmutateメソッド自体をImportできるようにして、
そのmutateメソッド内でgetDefaultKeyが使われているようにすれば、アプリケーションからImportする数を減らすことができます。

Describe the solution you'd like

上記の通り、@aspida/swrパッケージからmutateメソッドをExportしていただくイメージです

import {mutate as swrMutate} from 'swr'

export const mutate = (api: 省略) => {
  swrMutate(getDefaultKey(api));
}

Describe alternatives you've considered

SWR本体のmutateおよびgetDefaultKeyの両方をimportすること

commented

v1 で空間が完全に別れた環境の mutate というのが使えるようになります。

import { useSWRConfig } from "swr"

// ...

const swrConfig = useSWRConfig()
swrConfig.mutate(aspidaDefaultKey /*, data?, shouldRevaliate? */)

がどのように置き換わるべきか、という案を募集したいです。

これの必要性ですが、まずテストを書く際にかなり困りそうです。(すくなくとも @aspida/swr 自体のテストが書けず困った)

また、これの対応方法によっては swr v0.x サポートを切ることになるので、今のある程度、少しめんどいが汎用性のある方法をメインのままにするというのも考えられるかもと思っています。

一応以下の案を考えています。

import { useSWRConfig } from "swr"
import { aspidaMutate } from "@aspida/swr"

const swrConfig = useSWRConfig()
aspidaMutate(aspidaDefaultKey, { swrConfig } /*, data?, shouldRevaliate? */)

これは swr v0.x を切らないとちょっときついかな?という感じがします。
(それ自体はいいと思います)

単に賛成であれば 👍 のリアクションをいただけると助かります。

cc @solufa