Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
Implemented authorized requests with token argument in AniLink.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
RLAlpha49 committed Apr 26, 2024
1 parent b4f6a27 commit 2d9f334
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 29 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.idea
/dist
/node_modules
/node_modules
.env
4 changes: 3 additions & 1 deletion __tests__/anilist.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
require('dotenv').config()
import AniLink from '../dist/AniLink.js'
// import AniLink from '../src/AniLink'

describe('Anilist API Query', () => {
let aniLink: AniLink

beforeAll(() => {
aniLink = new AniLink()
const token = process.env.ANILIST_TOKEN
aniLink = new AniLink(token)
})

test('user query', async () => {
Expand Down
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@babel/preset-typescript": "^7.24.1",
"@types/jest": "^29.5.12",
"babel-jest": "^29.7.0",
"dotenv": "^16.4.5",
"jest": "^29.7.0",
"standard": "^17.1.0",
"ts-jest": "^29.1.2",
Expand Down
18 changes: 9 additions & 9 deletions src/AniLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ class AniLink {
}
}

constructor () {
const userQueryInstance = new UserQuery()
const mediaQueryInstance = new MediaQuery()
const mediaTrendQueryInstance = new MediaTrendQuery()
const airingScheduleQueryInstance = new AiringScheduleQuery()
const characterQueryInstance = new CharacterQuery()
const staffQueryInstance = new StaffQuery()
const mediaListQueryInstance = new MediaListQuery()
const updateUserMutationInstance = new UpdateUserMutation()
constructor (authToken: string) {
const userQueryInstance = new UserQuery(authToken)
const mediaQueryInstance = new MediaQuery(authToken)
const mediaTrendQueryInstance = new MediaTrendQuery(authToken)
const airingScheduleQueryInstance = new AiringScheduleQuery(authToken)
const characterQueryInstance = new CharacterQuery(authToken)
const staffQueryInstance = new StaffQuery(authToken)
const mediaListQueryInstance = new MediaListQuery(authToken)
const updateUserMutationInstance = new UpdateUserMutation(authToken)
this.anilist = {
query: {
user: userQueryInstance.user.bind(userQueryInstance),
Expand Down
7 changes: 5 additions & 2 deletions src/apis/anilist/mutation/UpdateUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,11 @@ interface UpdateUserResponse {
}

export class UpdateUserMutation extends APIWrapper {
constructor () {
private readonly authToken: string

constructor (authToken: string) {
super('https://graphql.anilist.co')
this.authToken = authToken
}

async updateUser (variables: UpdateUserVariables): Promise<UpdateUserResponse> {
Expand Down Expand Up @@ -154,6 +157,6 @@ export class UpdateUserMutation extends APIWrapper {
`

const data = { query: mutation, variables }
return await sendRequest(this.baseURL, 'POST', data)
return await sendRequest(this.baseURL, 'POST', data, this.authToken)
}
}
7 changes: 5 additions & 2 deletions src/apis/anilist/query/AiringSchedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ interface AiringScheduleVariables {
}

export class AiringScheduleQuery extends APIWrapper {
constructor () {
private readonly authToken: string

constructor (authToken: string) {
super('https://graphql.anilist.co')
this.authToken = authToken
}

async airingSchedule (variables?: AiringScheduleVariables): Promise<AiringScheduleResponse> {
Expand Down Expand Up @@ -152,6 +155,6 @@ export class AiringScheduleQuery extends APIWrapper {
`

const data = { query, variables }
return await sendRequest(this.baseURL, 'POST', data)
return await sendRequest(this.baseURL, 'POST', data, this.authToken)
}
}
7 changes: 5 additions & 2 deletions src/apis/anilist/query/Character.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ interface CharacterVariables {
}

export class CharacterQuery extends APIWrapper {
constructor () {
private readonly authToken: string

constructor (authToken: string) {
super('https://graphql.anilist.co')
this.authToken = authToken
}

async character (variables?: CharacterVariables): Promise<CharacterResponse> {
Expand Down Expand Up @@ -67,6 +70,6 @@ export class CharacterQuery extends APIWrapper {
`

const data = { query, variables }
return await sendRequest(this.baseURL, 'POST', data)
return await sendRequest(this.baseURL, 'POST', data, this.authToken)
}
}
7 changes: 5 additions & 2 deletions src/apis/anilist/query/Media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,11 @@ interface MediaVariables {
}

export class MediaQuery extends APIWrapper {
constructor () {
private readonly authToken: string

constructor (authToken: string) {
super('https://graphql.anilist.co')
this.authToken = authToken
}

async media (variables?: MediaVariables): Promise<MediaResponse> {
Expand Down Expand Up @@ -287,6 +290,6 @@ export class MediaQuery extends APIWrapper {
`

const data = { query, variables }
return await sendRequest(this.baseURL, 'POST', data)
return await sendRequest(this.baseURL, 'POST', data, this.authToken)
}
}
7 changes: 5 additions & 2 deletions src/apis/anilist/query/MediaList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ interface MediaListVariables {
}

export class MediaListQuery extends APIWrapper {
constructor () {
private readonly authToken: string

constructor (authToken: string) {
super('https://graphql.anilist.co')
this.authToken = authToken
}

async mediaList (variables?: MediaListVariables): Promise<MediaListResponse> {
Expand Down Expand Up @@ -184,6 +187,6 @@ export class MediaListQuery extends APIWrapper {
`

const data = { query, variables }
return await sendRequest(this.baseURL, 'POST', data)
return await sendRequest(this.baseURL, 'POST', data, this.authToken)
}
}
7 changes: 5 additions & 2 deletions src/apis/anilist/query/MediaTrend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ interface MediaTrendVariables {
}

export class MediaTrendQuery extends APIWrapper {
constructor () {
private readonly authToken: string

constructor (authToken: string) {
super('https://graphql.anilist.co')
this.authToken = authToken
}

async mediaTrend (variables?: MediaTrendVariables): Promise<MediaTrendResponse> {
Expand Down Expand Up @@ -161,6 +164,6 @@ export class MediaTrendQuery extends APIWrapper {
`

const data = { query, variables }
return await sendRequest(this.baseURL, 'POST', data)
return await sendRequest(this.baseURL, 'POST', data, this.authToken)
}
}
7 changes: 5 additions & 2 deletions src/apis/anilist/query/Staff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ interface StaffVariables {
}

export class StaffQuery extends APIWrapper {
constructor () {
private readonly authToken: string

constructor (authToken: string) {
super('https://graphql.anilist.co')
this.authToken = authToken
}

async staff (variables?: StaffVariables): Promise<StaffResponse> {
Expand Down Expand Up @@ -114,6 +117,6 @@ export class StaffQuery extends APIWrapper {
`

const data = { query, variables }
return await sendRequest(this.baseURL, 'POST', data)
return await sendRequest(this.baseURL, 'POST', data, this.authToken)
}
}
7 changes: 5 additions & 2 deletions src/apis/anilist/query/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ interface UserVariables {
}

export class UserQuery extends APIWrapper {
constructor () {
private readonly authToken: string

constructor (authToken: string) {
super('https://graphql.anilist.co')
this.authToken = authToken
}

async user (variables?: UserVariables): Promise<UserResponse> {
Expand Down Expand Up @@ -559,6 +562,6 @@ export class UserQuery extends APIWrapper {
`

const data = { query, variables: { isHTML, ...rest } }
return await sendRequest(this.baseURL, 'POST', data)
return await sendRequest(this.baseURL, 'POST', data, this.authToken)
}
}
19 changes: 17 additions & 2 deletions src/base/RequestHandler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
import axios, { AxiosResponse } from 'axios'

export const sendRequest = async (url: string, method: 'GET' | 'POST', data?: object): Promise<any> => {
const response: AxiosResponse = await axios({ url, method, data })
export const sendRequest = async (url: string, method: 'GET' | 'POST', data?: object, token?: string): Promise<any> => {
const headers: { [key: string]: string } = {
'Content-Type': 'application/json',
Accept: 'application/json'
}

if (token !== null && token !== undefined && token !== '') {
headers.Authorization = `Bearer ${token}`
}

const response: AxiosResponse = await axios({
url,
method,
data,
headers
})

return response.data
}

0 comments on commit 2d9f334

Please sign in to comment.