Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚡️ clean up result.text() #150

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v16.17.1
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this file please

27 changes: 10 additions & 17 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,19 +188,6 @@ const getClientSecret = (
return jwt.sign(claims, key, { algorithm: 'ES256', header });
};

/**
* populate function
*
* populate response as json if can be
*/
const _populateResAsJson = async (res) => {
const data = await res.text();
if (!data) {
return data;
}
return JSON.parse(data);
};

/** Gets an Apple authorization token */
const getAuthorizationToken = async (
code: string,
Expand Down Expand Up @@ -233,7 +220,7 @@ const getAuthorizationToken = async (
return fetch(url.toString(), {
method: 'POST',
body: params,
}).then((res) => _populateResAsJson(res));
}).then((res) => res.json());
};

/** Refreshes an Apple authorization token */
Expand Down Expand Up @@ -263,7 +250,7 @@ const refreshAuthorizationToken = async (
return fetch(url.toString(), {
method: 'POST',
body: params,
}).then((res) => _populateResAsJson(res));
}).then((res) => res.json());
};

/** Revoke Apple authorization token */
Expand Down Expand Up @@ -296,7 +283,13 @@ const revokeAuthorizationToken = async (
body: params,
});

return _populateResAsJson(result);
// on 200 apple will return an empty string.
// node-fetch will throw an error when calling .json() with an empty string
if ((await result.text()) === '') {
return '';
}

return result.json();
};

/** Gets an Array of Apple Public Keys that can be used to decode Apple's id tokens */
Expand All @@ -312,7 +305,7 @@ const _getApplePublicKeys = async ({
headers: {
'Content-Type': 'application/json',
},
}).then((res) => _populateResAsJson(res));
}).then((res) => res.json());

// Reset cache - will be refilled below
APPLE_KEYS_CACHE = {};
Expand Down
Loading