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

🐛 fixed response data as JSON #140

Merged
merged 7 commits into from
Jun 23, 2022
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
55 changes: 55 additions & 0 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,58 @@ describe('appleSignin test', () => {
expect(_getApplePublicKeys).toBeTruthy();
});
});

describe('test for each functions', () => {
describe('test revokeAuthorizationToken functions', () => {
const token = 'test token';
const option = {
clientID: 'clientID',
clientSecret: 'clientSecret',
tokenHintType: 'refresh_token',
};
it('Should not throw error when response is empty', async () => {
const result = await appleSignin.revokeAuthorizationToken(token, option);
expect(result).toEqual('');
});
});

describe('test getAuthorizationToken functions', () => {
const code = 'test code';
const option = {
clientID: 'clientID',
clientSecret: 'clientSecret',
tokenHintType: 'refresh_token',
};
it('Should not throw error when response is empty', async () => {
const result = await appleSignin.getAuthorizationToken(code, option);
expect(result).toEqual({
error: 'invalid_client',
});
});
});

describe('test refreshAuthorizationToken functions', () => {
const refreshToken = 'test refreshToken';
const option = {
clientID: 'clientID',
clientSecret: 'clientSecret',
tokenHintType: 'refresh_token',
};
it('Should not throw error when response is empty', async () => {
const result = await appleSignin.refreshAuthorizationToken(
refreshToken,
option,
);
expect(result).toEqual({
error: 'invalid_client',
});
});
});

describe('test _getApplePublicKeys functions', () => {
it('Should not throw error when response is empty', async () => {
const result = await appleSignin._getApplePublicKeys();
expect(result).not.toBeNull();
});
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apple-signin-auth",
"version": "1.7.2",
"version": "1.7.3",
"description": " Apple signin for node.",
"author": {
"name": "Ahmed Tarek",
Expand Down
21 changes: 14 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,13 @@ const getClientSecret = (
*
* populate response as json if can be
*/
const _populateDResAsJson = (res) =>
typeof res?.json === 'function' ? res.json() : res;
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 (
Expand Down Expand Up @@ -228,7 +233,7 @@ const getAuthorizationToken = async (
return fetch(url.toString(), {
method: 'POST',
body: params,
}).then((res) => _populateDResAsJson(res));
}).then((res) => _populateResAsJson(res));
};

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

/** Revoke Apple authorization token */
Expand Down Expand Up @@ -286,10 +291,12 @@ const revokeAuthorizationToken = async (
params.append('token', token);
params.append('token_hint_type', options.tokenHintType);

return fetch(url.toString(), {
const result = await fetch(url.toString(), {
method: 'POST',
body: params,
}).then((res) => _populateDResAsJson(res));
});

return _populateResAsJson(result);
};

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

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