Skip to content

Commit

Permalink
cleaning up result.text() since we always parsing to json
Browse files Browse the repository at this point in the history
  • Loading branch information
doron2402 committed Oct 12, 2022
1 parent ef55bf9 commit cec26ff
Show file tree
Hide file tree
Showing 3 changed files with 4,669 additions and 5,196 deletions.
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v16.17.1
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

0 comments on commit cec26ff

Please sign in to comment.