Skip to content

Commit

Permalink
fix: netfily 404
Browse files Browse the repository at this point in the history
  • Loading branch information
Deturium committed Nov 1, 2018
1 parent f34a250 commit 60bea86
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 29 deletions.
7 changes: 7 additions & 0 deletions netlify.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@


[[redirects]]
from = "/*"
to = "/index.html"
status = 200

86 changes: 57 additions & 29 deletions src/utils/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getLocalStorage, setLocalStorage } from './storage'
import { Failure, Success, Try } from './fp/Try'

import host from '@/model/apiHost'
import { access } from 'fs';

export interface FetchError {
/**
Expand Down Expand Up @@ -73,29 +74,17 @@ interface GETOptions {
}

export async function GET<T = any>(url: string, options: GETOptions = {}) {
const requestInit: RequestInit = {
headers: new Headers({
// access_token
Authorization: options.noAuthorization ? '' : await getAccessToken(),
...options.headers,
}),
// credentials: "include",
...options.requestInit,
}

const queryStr = options.params ? `?${encodeParams(options.params)}` : ''

return await cc98Fetch<T>(url + queryStr, requestInit)
}
const headers: Record<string, string> = {}

type DELETEOptions = GETOptions
if (options.noAuthorization) {
const accessToken = await getAccessToken()
if (accessToken)
headers.Authorization = accessToken
}

export async function DELETE<T = any>(url: string, options: DELETEOptions = {}) {
const requestInit: RequestInit = {
method: "DELETE",
headers: new Headers({
// access_token
Authorization: options.noAuthorization ? '' : await getAccessToken(),
...headers,
...options.headers,
}),
// credentials: "include",
Expand Down Expand Up @@ -127,11 +116,18 @@ interface POSTOptions {
}

export async function POST<T = any>(url: string, options: POSTOptions = {}) {
const headers: Record<string, string> = {}

if (options.noAuthorization) {
const accessToken = await getAccessToken()
if (accessToken)
headers.Authorization = accessToken
}

const requestInit: RequestInit = {
method: 'POST',
headers: new Headers({
// access_token
Authorization: options.noAuthorization ? '' : await getAccessToken(),
...headers,
...(options.headers || {
'Content-Type': 'application/json',
}),
Expand All @@ -146,11 +142,18 @@ export async function POST<T = any>(url: string, options: POSTOptions = {}) {
type PUTOptions = POSTOptions

export async function PUT<T = any>(url: string, options: PUTOptions = {}) {
const headers: Record<string, string> = {}

if (options.noAuthorization) {
const accessToken = await getAccessToken()
if (accessToken)
headers.Authorization = accessToken
}

const requestInit: RequestInit = {
method: 'PUT',
headers: new Headers({
// access_token
Authorization: options.noAuthorization ? '' : await getAccessToken(),
...headers,
...(options.headers || {
'Content-Type': 'application/json',
}),
Expand All @@ -162,6 +165,30 @@ export async function PUT<T = any>(url: string, options: PUTOptions = {}) {
return await cc98Fetch<T>(url, requestInit)
}

type DELETEOptions = GETOptions

export async function DELETE<T = any>(url: string, options: DELETEOptions = {}) {
const headers: Record<string, string> = {}

if (options.noAuthorization) {
const accessToken = await getAccessToken()
if (accessToken)
headers.Authorization = accessToken
}

const requestInit: RequestInit = {
method: "DELETE",
headers: new Headers({
...headers,
...options.headers,
}),
...options.requestInit,
}

return await cc98Fetch<T>(url, requestInit)
}


/**
* just like $.param
*/
Expand Down Expand Up @@ -211,13 +238,14 @@ async function getTokenByRefreshToken(refreshToken: string): Promise<Try<Token,
export async function getAccessToken(): Promise<string> {
let accessToken = getLocalStorage('access_token')

if(!accessToken) {
const refreshToken = <string>getLocalStorage('refresh_token')
if (!accessToken) {
const refreshToken = getLocalStorage('refresh_token')

if(!refreshToken)
return ''
if (!refreshToken) {
accessToken = ''
}

const token = await getTokenByRefreshToken(refreshToken)
const token = await getTokenByRefreshToken(<string>refreshToken)

token
.fail(
Expand All @@ -233,7 +261,7 @@ export async function getAccessToken(): Promise<string> {
})
}

return `${accessToken}`
return <string>accessToken
}

interface Token {
Expand Down

0 comments on commit 60bea86

Please sign in to comment.