Skip to content

Commit

Permalink
fix: APIリクエストにx-requested-withヘッダーを追加 (#428)
Browse files Browse the repository at this point in the history
Fantia側の変更対応

#427
  • Loading branch information
mnao305 authored Jul 6, 2023
1 parent 3b8389b commit 805d341
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
10 changes: 10 additions & 0 deletions src/content/modules/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import ky from 'ky'

/**
* Fantiaのget APIにアクセスするためのヘッダーなどを設定済みのラッパー
*/
export const get = async <T>(url: string) => {
const csrfToken = document.getElementsByName('csrf-token')[0].getAttribute('content') ?? ''

return await ky.get(url, { headers: { 'x-csrf-token': csrfToken, 'x-requested-with': 'XMLHttpRequest' } }).json<T>()
}
5 changes: 2 additions & 3 deletions src/content/modules/backnumberPage.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import ky from 'ky'
import { Backnumber, BacknumberResponse } from '../../types/backnumber'
import { get } from './api'

export const fetchBacknumberData = async (): Promise<Backnumber> => {
const url = new URL((document.getElementsByClassName('tab-item tab-item-text active')[1] as HTMLAnchorElement).href)
const plan = url.searchParams.get('plan')
const month = url.searchParams.get('month')
const csrfToken = document.getElementsByName('csrf-token')[0].getAttribute('content') ?? ''
const json = await ky.get(`https://fantia.jp/api/v1/fanclub/backnumbers/monthly_contents/plan/${plan}/month/${month}`, { headers: { 'x-csrf-token': csrfToken } }).json<BacknumberResponse>()
const json = await get<BacknumberResponse>(`https://fantia.jp/api/v1/fanclub/backnumbers/monthly_contents/plan/${plan}/month/${month}`)

return json.backnumber
}
5 changes: 2 additions & 3 deletions src/content/modules/postPage.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import ky from 'ky'
import { idAndTitlePath, contentIdToTitle, urlToExt } from '../index'
import { PostData, PostDataResponse } from '../../types/index'
import { getImgList } from './img'
import { fileDownload } from './download'
import { blogDL } from './blog'
import { get } from './api'

export const fetchPostData = async (): Promise<PostData> => {
const id = location.pathname.split('/posts/')[1]
const csrfToken = document.getElementsByName('csrf-token')[0].getAttribute('content') ?? ''
const json = await ky.get(`https://fantia.jp/api/v1/posts/${id}`, { headers: { 'x-csrf-token': csrfToken } }).json<PostDataResponse>()
const json = await get<PostDataResponse>(`https://fantia.jp/api/v1/posts/${id}`)

return json.post
}
Expand Down

0 comments on commit 805d341

Please sign in to comment.