- Fixed url full path
- Made xhr response handler optional
- Different types
TParamsandTResulttypes now in right order (seeexamples.ts)- Dropped
loggersupport - Functions now throws if mocks were enabled but no mock function provided, except the case when
MOCK_MODE_ENABLEDset.
- You can pass a function to
sharedRequestOptions:
const api = new APIMaker({
base: "https://jsonplaceholder.typicode.com",
sharedRequestOptions: () => ({
credentials: "include",
}),
});- Passed
urlto response handler:
type ResponseHandler<TResult = any> = (response: Response, url: string, requestParams: RequestInit) => Promise<TResult>;- Improved log messages
- Passed extra params to status handler:
type StatusHandler = (response: Response, url: string, requestParams: RequestInit) => void;- Passed requestParams as the second argument for
responseHandlerfunction.
const getUserCustomResponseHandler = api.create<unknown, number>((id) => ({
path: `/users/${id}`,
// Now you can access requestParams here!
responseHandler: async (response, requestParams) => response.text(),
}));- Supported setter function as a parameter for
setSharedOptionsmethod.
Now you may pass a function which will return new request options for all the requests.
test("if pass a function it will override options defined in constructor", async () => {
const api = new APIMaker({
base: "https://jsonplaceholder.typicode.com",
sharedRequestOptions: {
credentials: "include",
},
});
api.setSharedRequestOptions((currentSharedRequestOptions) => ({
...currentSharedRequestOptions,
headers: { "Content-Type": "application/json" },
}));
const getUser = api.create<unknown, number>((id) => ({
path: `/users/${id}`,
}));
const user = await getUser(1);
expect(user).toEqual({ name: "John Doe" });
expect(globalThis.fetch).toHaveBeenCalledWith("https://jsonplaceholder.typicode.com/users/1", {
credentials: "include",
headers: { "Content-Type": "application/json" },
});
});