Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/butter.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ export default function (apiToken, options = {}) {
if (!apiToken) {
throw 'ButterCMS API token not set';
}

return new Butter(apiToken, options);

if (!(this instanceof Butter)) {
return new Butter(apiToken, options);
}

return Butter
};

/**
Expand Down
13 changes: 6 additions & 7 deletions lib/useButter.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,13 @@ export default function useButter(type, butterConfig) {
try {
// use static abortSignal timeout functionality to relay a cancelation
// when timeout is reached
// if AbortSignal.timeout doesn't exist (React Native/Node.js), use a regular timeout
let timeoutId;
// if AbortSignal.timeout doestn't exist (React Native), use a regular timeotu
const timeoutSignal = AbortSignal.timeout
? AbortSignal.timeout(config.timeout)
: (timeoutId = setTimeout(
: setTimeout(
() => abortOnTimeout(config.timeout),
config.timeout
))
)

const response = await fetch(
`${apiEndpoint}?${new URLSearchParams(params)}`,
Expand All @@ -101,10 +100,10 @@ export default function useButter(type, butterConfig) {
}
);

if (!AbortSignal.timeout && timeoutId) {
// if we are running on a regular timeout,
if (!AbortSignal.timeout) {
// if we are running on a regular timout,
// clear it after its use
clearTimeout(timeoutId)
clearTimeout(timeoutSignal)
}

cleanup();
Expand Down