Skip to content

Commit 26048f4

Browse files
author
Kadi Kraman
committed
Update api to accept two arguments instead of one config
1 parent 3367b12 commit 26048f4

File tree

3 files changed

+38
-50
lines changed

3 files changed

+38
-50
lines changed

README.md

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,8 @@ const config = {
104104
scopes: '<YOUR_SCOPES_ARRAY>',
105105
};
106106

107-
const result = await refresh({
108-
...config,
109-
refreshToken: `<REFRESH_TOKEN>`,
107+
const result = await refresh(config, {
108+
refreshToken: `<REFRESH_TOKEN>`
110109
});
111110
```
112111

@@ -124,8 +123,7 @@ const config = {
124123
scopes: '<YOUR_SCOPES_ARRAY>',
125124
};
126125

127-
const result = await revoke({
128-
...config,
126+
const result = await revoke(config, {
129127
tokenToRevoke: `<TOKEN_TO_REVOKE>`
130128
});
131129
```
@@ -379,8 +377,7 @@ const refreshedState = await refresh({
379377
});
380378

381379
// Revoke token, note that Identity Server expects a client id on revoke
382-
await revoke({
383-
...config,
380+
await revoke(config, {
384381
tokenToRevoke: refreshedState.refreshToken,
385382
sendClientId: true
386383
});
@@ -402,14 +399,12 @@ const config = {
402399
const authState = await authorize(config);
403400

404401
// Refresh token
405-
const refreshedState = await refresh({
406-
...config,
402+
const refreshedState = await refresh(config, {
407403
refreshToken: authState.refreshToken
408404
});
409405

410406
// Revoke token
411-
await revoke({
412-
...config,
407+
await revoke(config, {
413408
tokenToRevoke: refreshedState.refreshToken
414409
});
415410
```
@@ -436,14 +431,12 @@ const config = {
436431
const authState = await authorize(config);
437432

438433
// Refresh token
439-
const refreshedState = await refresh({
440-
...config,
434+
const refreshedState = await refresh(config, {
441435
refreshToken: authState.refreshToken,
442436
});
443437

444438
// Revoke token
445-
await revoke({
446-
...config,
439+
await revoke(config, {
447440
tokenToRevoke: refreshedState.refreshToken
448441
});
449442
```
@@ -466,8 +459,7 @@ const config = {
466459
const authState = await authorize(config);
467460

468461
// Refresh token
469-
const refreshedState = await refresh({
470-
...config,
462+
const refreshedState = await refresh(config, {
471463
refreshToken: authState.refreshToken,
472464
});
473465
```

index.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,10 @@ export const authorize = ({ issuer, redirectUrl, clientId, scopes, additionalPar
2222
return RNAppAuth.authorize(issuer, redirectUrl, clientId, scopes, additionalParameters);
2323
};
2424

25-
export const refresh = ({
26-
issuer,
27-
redirectUrl,
28-
clientId,
29-
refreshToken,
30-
scopes,
31-
additionalParameters,
32-
}) => {
25+
export const refresh = (
26+
{ issuer, redirectUrl, clientId, scopes, additionalParameters },
27+
{ refreshToken }
28+
) => {
3329
validateScopes(scopes);
3430
validateIssuer(issuer);
3531
validateClientId(clientId);
@@ -47,7 +43,7 @@ export const refresh = ({
4743
);
4844
};
4945

50-
export const revoke = async ({ tokenToRevoke, sendClientId = false, clientId, issuer }) => {
46+
export const revoke = async ({ clientId, issuer }, { tokenToRevoke, sendClientId = false }) => {
5147
invariant(tokenToRevoke, 'Please include the token to revoke');
5248
validateClientId(clientId);
5349
validateIssuer(issuer);

index.spec.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('AppAuth', () => {
2121
mockRefresh.mockReturnValue('REFRESHED');
2222
});
2323

24-
const baseConfig = {
24+
const config = {
2525
issuer: 'test-issuer',
2626
redirectUrl: 'test-redirectUrl',
2727
clientId: 'test-clientId',
@@ -37,42 +37,42 @@ describe('AppAuth', () => {
3737

3838
it('throws an error when issuer is not a string', () => {
3939
expect(() => {
40-
authorize({ ...baseConfig, issuer: () => ({}) });
40+
authorize({ ...config, issuer: () => ({}) });
4141
}).toThrow('Config error: issuer must be a string');
4242
});
4343

4444
it('throws an error when redirectUrl is not a string', () => {
4545
expect(() => {
46-
authorize({ ...baseConfig, redirectUrl: {} });
46+
authorize({ ...config, redirectUrl: {} });
4747
}).toThrow('Config error: redirectUrl must be a string');
4848
});
4949

5050
it('throws an error when clientId is not a string', () => {
5151
expect(() => {
52-
authorize({ ...baseConfig, clientId: 123 });
52+
authorize({ ...config, clientId: 123 });
5353
}).toThrow('Config error: clientId must be a string');
5454
});
5555

5656
it('throws an error when no scopes are passed in', () => {
5757
expect(() => {
58-
authorize({ ...baseConfig, scopes: undefined });
58+
authorize({ ...config, scopes: undefined });
5959
}).toThrow('Scope error: please add at least one scope');
6060
});
6161

6262
it('throws an error when an empty scope array is passed in', () => {
6363
expect(() => {
64-
authorize({ ...baseConfig, scopes: [] });
64+
authorize({ ...config, scopes: [] });
6565
}).toThrow('Scope error: please add at least one scope');
6666
});
6767

6868
it('calls the native wrapper with the correct args', () => {
69-
authorize(baseConfig);
69+
authorize(config);
7070
expect(mockAuthorize).toHaveBeenCalledWith(
71-
baseConfig.issuer,
72-
baseConfig.redirectUrl,
73-
baseConfig.clientId,
74-
baseConfig.scopes,
75-
baseConfig.additionalParameters
71+
config.issuer,
72+
config.redirectUrl,
73+
config.clientId,
74+
config.scopes,
75+
config.additionalParameters
7676
);
7777
});
7878
});
@@ -85,49 +85,49 @@ describe('AppAuth', () => {
8585

8686
it('throws an error when issuer is not a string', () => {
8787
expect(() => {
88-
authorize({ ...baseConfig, issuer: () => ({}) });
88+
authorize({ ...config, issuer: () => ({}) });
8989
}).toThrow('Config error: issuer must be a string');
9090
});
9191

9292
it('throws an error when redirectUrl is not a string', () => {
9393
expect(() => {
94-
authorize({ ...baseConfig, redirectUrl: {} });
94+
authorize({ ...config }, { redirectUrl: {} });
9595
}).toThrow('Config error: redirectUrl must be a string');
9696
});
9797

9898
it('throws an error when clientId is not a string', () => {
9999
expect(() => {
100-
authorize({ ...baseConfig, clientId: 123 });
100+
authorize({ ...config, clientId: 123 });
101101
}).toThrow('Config error: clientId must be a string');
102102
});
103103

104104
it('throws an error when no refreshToken is passed in', () => {
105105
expect(() => {
106-
refresh(baseConfig);
106+
refresh(config);
107107
}).toThrow('Please pass in a refresh token');
108108
});
109109

110110
it('throws an error when no scopes are passed in', () => {
111111
expect(() => {
112-
refresh({ ...baseConfig, refreshToken: 'such-token', scopes: undefined });
112+
refresh({ ...config, scopes: undefined }, { refreshToken: 'such-token' });
113113
}).toThrow('Scope error: please add at least one scope');
114114
});
115115

116116
it('throws an error when an empty scope array is passed in', () => {
117117
expect(() => {
118-
refresh({ ...baseConfig, refreshToken: 'such-token', scopes: [] });
118+
refresh({ ...config, scopes: [] }, { refreshToken: 'such-token' });
119119
}).toThrow('Scope error: please add at least one scope');
120120
});
121121

122122
it('calls the native wrapper with the correct args', () => {
123-
refresh({ ...baseConfig, refreshToken: 'such-token' });
123+
refresh({ ...config }, { refreshToken: 'such-token' });
124124
expect(mockRefresh).toHaveBeenCalledWith(
125-
baseConfig.issuer,
126-
baseConfig.redirectUrl,
127-
baseConfig.clientId,
125+
config.issuer,
126+
config.redirectUrl,
127+
config.clientId,
128128
'such-token',
129-
baseConfig.scopes,
130-
baseConfig.additionalParameters
129+
config.scopes,
130+
config.additionalParameters
131131
);
132132
});
133133
});

0 commit comments

Comments
 (0)