infinitered / apisauce

Axios + standardized errors + request/response transforms.
MIT License
2.78k stars 184 forks source link

Singleton concern on addResponseTransform #325

Closed saaspeter closed 2 months ago

saaspeter commented 2 months ago

I follow the guide to create the apisauce instance, it is singleton instance, for some api endpoint, I want to transform the response data, so I want to try addResponseTransform, but one concern is: I only want to do the response transfrom in two apis, other api don't need this specific transform or need other different transform, so if I add transform like this: "this.apisauce.addResponseTransform(...)", then for this apisauce instance, it will keep the transform in its instance, will the transform apply other api endpoints automatically?

since the apisauce instance is singleton, I want to use it in all api endpoint. So should I use the addResponseTransform feature?

jamonholmgren commented 2 months ago

Hard to say without seeing code, but you could just add the transform to every instance as you create them.

saaspeter commented 2 months ago

I think I misunderstand the transform, the transform should be used for the common data convert transform, just like some data covert in java spring data convert, it should be applied to all the response field transform, am I right? So my concern is an invalid case.

I am using Ignite, it is a great framework! My original problem is: I defined a type: UserDrugInfo, which has two properties: startDate and endDate, both are Date type, and also I have another property which name in react native app is different with the rest api response, so I want to do the data convert, after I read the great apisauce doc about response transform, my first thought is to use addResponseTransform, but I found the apisauce is singleton by recommendation, so I post this question, and later I found I misunderstand this usage. Now, I change the startDate/endDate type to string, so I don't need the convert any more :) (this also be a problem when deserialization from asyncStorage store).

export type UserDrugInfo = { id?: string status?: number drugId?: string drugName?: string drugForm?: number drugUnitType?: number drugUnitValue?: number startDate?: Date endDate?: Date frequencyType?: number daysInterval?: number timesAday?: number drugRemindTimes?: DrugRemindTime[]|null // the property name is different with restful api response }`