Closed didierfranc closed 3 years ago
@didierfranc did you find your answer?
@abatilo Given a service definition like this:
static HTTPPostWithStarBodyPath(req: HttpPostRequest, initReq?: fm.InitReq): Promise<HttpPostResponse> {
return fm.fetchReq<HttpPostRequest, HttpPostResponse>(`/post/${req["a"]}/${req["c"]}`, {...initReq, method: "POST", body: JSON.stringify(req)})
}
Just pass the headers as fetch options like this as the second parameter (initReq
)
{
pathPrefix: `endpointURL`,
headers: {
Authorization: `Bearer token`,
},
}
Ah, that makes sense. It almost looks like initReq are just regular params for a fetch request!
Thank you very much.
@abatilo
Yes you are correct, initReq just extends the regular params object for a fetch request and adds pathPrefix
for appending the endpoint URL prefix 👍🏼
So you can add all the params that are applicable for the Fetch API.
What's your thought about setting those params one time (in a constructor) instead of passing them to every method call?
Yes, I did give that idea some consideration. Let's take React as an example and assume that you have two components: one calling a ListUsers
RPC and another calling an AddUser
RPC. In each of those components, you have to instantiate the UserService
class and pass the params to a constructor. So you are technically passing the params twice (once in each component). And this is the same as passing the params to every method call.
Yes, I did gave that idea some consideration. Let's take React as an example and assume that you have two components: one calling a
ListUsers
RPC and another calling anAddUser
RPC. In each of those components, you have to instantiate theUserService
class and pass the params to a constructor. So you are technically passing the params twice (once in each component). And this is the same as passing the params to every method call.
There are ways around that though. If you create a ContextProvider for the client, then you can have multiple components use the same instance.
@abatilo I see what you mean. Perhaps you can raise this as a separate issue and check the feasibility of making a PR where you can pass the params to a constructor.
It is useful to pass a header like
Authorization
to the query, I can't find a way to pass it