fix: update http methods to send authorization header

This commit is contained in:
Kang Ming committed 2021-12-06 20:35:24 +08:00
1 parent b01f6f0892
commit 95cc1ee9b5
5 files changed
+23 -4

No files matched your search

+11
View File
@@ -1,3 +1,4 @@
import { tryParseJson } from 'lib/helpers'
import { SupaResponse } from 'types/base'
export function handleError<T>(e: any, requestId: string): SupaResponse<T> {
@@ -62,3 +63,13 @@ export async function handleResponseError<T = unknown>(
return { error } as unknown as SupaResponse<T>
}
}
export function getAccessToken() {
const tokenData = window?.localStorage['supabase.auth.token']
if (!tokenData) return undefined
const tokenObj = tryParseJson(tokenData)
if (tokenObj === false) {
return ""
}
return tokenObj.currentSession.access_token
}
+3 -1
View File
@@ -1,4 +1,4 @@
import { handleError, handleResponse, handleResponseError } from './base'
import { handleError, handleResponse, handleResponseError, getAccessToken } from './base'
import { uuidv4 } from '../../helpers'
import { SupaResponse } from 'types/base'
@@ -9,6 +9,7 @@ export async function delete_<T = any>(
): Promise<SupaResponse<T>> {
const requestId = uuidv4()
const { headers, ...otherOptions } = options ?? {}
const accessToken = getAccessToken()
try {
const response = await fetch(url, {
method: 'DELETE',
@@ -19,6 +20,7 @@ export async function delete_<T = any>(
'Content-Type': 'application/json',
Accept: 'application/json',
'X-Request-Id': requestId,
'Authorization': `Bearer ${accessToken}`,
...headers,
},
...otherOptions,
+3 -1
View File
@@ -1,4 +1,4 @@
import { handleError, handleResponse, handleResponseError } from './base'
import { handleError, handleResponse, handleResponseError, getAccessToken } from './base'
import { uuidv4 } from '../../helpers'
import { SupaResponse } from 'types/base'
@@ -9,6 +9,7 @@ export async function get<T = any>(
const requestId = uuidv4()
try {
const { headers, ...otherOptions } = options ?? {}
const accessToken = getAccessToken()
const response = await fetch(url, {
method: 'GET',
credentials: 'include',
@@ -17,6 +18,7 @@ export async function get<T = any>(
'Content-Type': 'application/json',
Accept: 'application/json',
'X-Request-Id': requestId,
'Authorization': `Bearer ${accessToken}`,
...headers,
},
...otherOptions,
+3 -1
View File
@@ -1,4 +1,4 @@
import { handleError, handleResponse, handleResponseError } from './base'
import { handleError, handleResponse, handleResponseError, getAccessToken } from './base'
import { uuidv4 } from '../../helpers'
import { SupaResponse } from 'types/base'
@@ -10,6 +10,7 @@ export async function patch<T = any>(
const requestId = uuidv4()
try {
const { headers, ...otherOptions } = options ?? {}
const accessToken = getAccessToken()
const response = await fetch(url, {
method: 'PATCH',
body: JSON.stringify(data),
@@ -19,6 +20,7 @@ export async function patch<T = any>(
'Content-Type': 'application/json',
Accept: 'application/json',
'X-Request-Id': requestId,
'Authorization': `Bearer ${accessToken}`,
...headers,
},
...otherOptions,
+3 -1
View File
@@ -1,4 +1,4 @@
import { handleError, handleResponse, handleResponseError } from './base'
import { handleError, handleResponse, handleResponseError, getAccessToken } from './base'
import { uuidv4 } from 'lib/helpers'
import { SupaResponse } from 'types/base'
@@ -10,6 +10,7 @@ export async function post<T = any>(
const requestId = uuidv4()
try {
const { headers, ...otherOptions } = options ?? {}
const accessToken = getAccessToken()
const response = await fetch(url, {
method: 'POST',
body: JSON.stringify(data),
@@ -19,6 +20,7 @@ export async function post<T = any>(
'Content-Type': 'application/json',
Accept: 'application/json',
'X-Request-Id': requestId,
'Authorization': `Bearer ${accessToken}`,
...headers,
},
...otherOptions,