Skip to content

Commit

Permalink
Merge pull request #22 from ImageMarkup/add-scope-support
Browse files Browse the repository at this point in the history
  • Loading branch information
brianhelba authored Feb 28, 2022
2 parents 434c8d8 + e65ea19 commit 967eebd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ A client library for authenticating with the ISIC Archive from an SPA (single pa
```js
const isicClient = new IsicClient(
process.env.CLIENT_ID,
'https://api-sandbox.isic-archive.com',
{
isicOrigin: new URL('https://api-sandbox.isic-archive.com'),
},
);
```
Expand Down
5 changes: 4 additions & 1 deletion example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import IsicClient from '@isic/client';

const isicClient = new IsicClient(
'v1odYySCetBht6DT9svQdAkvmVXrRHOwIIGNk6JG',
'https://api-sandbox.isic-archive.com/'
{
isicOrigin: new URL('https://api-sandbox.isic-archive.com/'),
scopes: ['identity'],
},
);
let legacyToken;

Expand Down
28 changes: 22 additions & 6 deletions src/isic-client.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
import OauthClient from '@girder/oauth-client';

export interface IsicClientOptions {
isicOrigin?: URL;
scopes?: string[];
}

export default class IsicClient extends OauthClient {
protected readonly isicBaseUrl: string;
protected readonly isicOrigin: URL;

constructor(
clientId: string,
isicBaseUrl = 'https://api.isic-archive.com',
{
isicOrigin = new URL('https://api.isic-archive.com'),
scopes = [],
}: IsicClientOptions = {},
) {
const trimmedIsicBaseUrl = isicBaseUrl.replace(/\/$/, '');
const cleanedIsicOrigin = new URL(isicOrigin);
// Origin cannot have path, query string, or fragment components
cleanedIsicOrigin.pathname = '';
cleanedIsicOrigin.search = '';
cleanedIsicOrigin.hash = '';

super(
new URL(`${trimmedIsicBaseUrl}/oauth/`),
new URL(`${cleanedIsicOrigin}/oauth/`),
clientId,
{
scopes,
},
);
this.isicBaseUrl = trimmedIsicBaseUrl;
this.isicOrigin = cleanedIsicOrigin;
}

protected get fetchOptions(): RequestInit {
Expand All @@ -28,7 +44,7 @@ export default class IsicClient extends OauthClient {
.replace(/^\//, '')
.replace(/\/$/, '')
.concat('/');
const resp = await fetch(`${this.isicBaseUrl}/${canonicalEndpoint}`, {
const resp = await fetch(`${this.isicOrigin}/${canonicalEndpoint}`, {
...this.fetchOptions,
method,
});
Expand Down

0 comments on commit 967eebd

Please sign in to comment.