Skip to content

Commit

Permalink
Add AniList provider (#92)
Browse files Browse the repository at this point in the history
* feat: implement anilist provider

* docs: add docs for provider

* minor fixes

* add state

---------

Co-authored-by: pilcrowOnPaper <[email protected]>
  • Loading branch information
Sayykii and pilcrowonpaper authored Mar 28, 2024
1 parent 98e91af commit eda8396
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions .changesets/w34ma.minor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add AniList provider.
1 change: 1 addition & 0 deletions docs/malta.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"title": "Providers",
"pages": [
["Amazon Cognito", "/providers/amazon-cognito"],
["AniList", "/providers/anilist"],
["Apple", "/providers/apple"],
["Atlassian", "/providers/atlassian"],
["Auth0", "/providers/auth0"],
Expand Down
18 changes: 18 additions & 0 deletions docs/pages/providers/anilist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
title: "AniList"
---

# AniList

For usage, see [OAuth 2.0 provider](/guides/oauth2).

```ts
import { AniList } from "arctic";

const anilist = new AniList(clientId, clientSecret, redirectURI);
```

```ts
const url: URL = await anilist.createAuthorizationURL(state);
const tokens: AniListTokens = await anilist.validateAuthorizationCode(code);
```
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { AmazonCognito } from "./providers/amazon-cognito.js";
export { AniList } from "./providers/anilist.js";
export { Apple } from "./providers/apple.js";
export { Atlassian } from "./providers/atlassian.js";
export { Auth0 } from "./providers/auth0.js";
Expand Down Expand Up @@ -44,6 +45,7 @@ export type {
AmazonCognitoRefreshedTokens,
AmazonCognitoTokens
} from "./providers/amazon-cognito.js";
export type { AniListTokens } from "./providers/anilist.js";
export type { AppleCredentials, AppleRefreshedTokens, AppleTokens } from "./providers/apple.js";
export type { AtlassianTokens } from "./providers/atlassian.js";
export type { Auth0Tokens } from "./providers/auth0.js";
Expand Down
41 changes: 41 additions & 0 deletions src/providers/anilist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { OAuth2Client } from "oslo/oauth2";
import type { OAuth2Provider } from "../index.js";

const authorizeEndpoint = "https://anilist.co/api/v2/oauth/authorize";
const tokenEndpoint = "https://anilist.co/api/v2/oauth/token";

export class AniList implements OAuth2Provider {
private client: OAuth2Client;
private clientSecret: string;

constructor(clientId: string, clientSecret: string, redirectURI: string) {
this.client = new OAuth2Client(clientId, authorizeEndpoint, tokenEndpoint, {
redirectURI
});
this.clientSecret = clientSecret;
}

public async createAuthorizationURL(state: string): Promise<URL> {
return await this.client.createAuthorizationURL({
state
});
}

public async validateAuthorizationCode(code: string): Promise<AniListTokens> {
const result = await this.client.validateAuthorizationCode<TokenResponseBody>(code, {
credentials: this.clientSecret
});
const tokens: AniListTokens = {
accessToken: result.access_token
};
return tokens;
}
}

interface TokenResponseBody {
access_token: string;
}

export interface AniListTokens {
accessToken: string;
}

0 comments on commit eda8396

Please sign in to comment.