-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e3fd02f
commit 84e32b3
Showing
25 changed files
with
1,391 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import env from '../../env'; | ||
import useStore from '../state'; | ||
|
||
export default class API { | ||
private get accessToken() { | ||
const { accessToken } = useStore.getState(); | ||
|
||
if (!accessToken) { | ||
throw new Error('No access token'); | ||
} | ||
|
||
return accessToken; | ||
} | ||
|
||
private buildUrl(path: string, params?: Record<string, any>) { | ||
const url = new URL(`${env.apiUrl}${path}`); | ||
|
||
if (params) { | ||
Object.entries(params).forEach(([key, value]) => { | ||
if (value !== undefined && value !== null) { | ||
if (typeof value === 'object') { | ||
url.searchParams.append(key, JSON.stringify(value)); | ||
} else { | ||
url.searchParams.append(key, String(value)); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
return url.toString(); | ||
} | ||
|
||
public async post<T extends object>(path: string, body: T, needsAuth = true) { | ||
const response = await fetch(`${env.apiUrl}${path}`, { | ||
method: 'POST', | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
...(needsAuth ? { Authorization: `Bearer ${this.accessToken}` } : {}) | ||
}, | ||
body: JSON.stringify(body) | ||
}); | ||
|
||
const data = await response.json(); | ||
|
||
return data; | ||
} | ||
|
||
public async get(path: string, params?: Record<string, any>) { | ||
const url = this.buildUrl(path, params); | ||
|
||
const response = await fetch(url, { | ||
method: 'GET', | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${this.accessToken}` | ||
} | ||
}); | ||
|
||
const data = await response.json(); | ||
|
||
return data; | ||
} | ||
|
||
public async put<T extends object>(path: string, body: T) { | ||
const response = await fetch(`${env.apiUrl}${path}`, { | ||
method: 'PUT', | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${this.accessToken}` | ||
}, | ||
body: JSON.stringify(body) | ||
}); | ||
|
||
const data = await response.json(); | ||
|
||
return data; | ||
} | ||
|
||
public async patch<T extends object>(path: string, body: T) { | ||
const response = await fetch(`${env.apiUrl}${path}`, { | ||
method: 'PATCH', | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${this.accessToken}` | ||
}, | ||
body: JSON.stringify(body) | ||
}); | ||
|
||
const data = await response.json(); | ||
|
||
return data; | ||
} | ||
|
||
public async delete(path: string) { | ||
const response = await fetch(`${env.apiUrl}${path}`, { | ||
method: 'DELETE', | ||
headers: { | ||
Authorization: `Bearer ${this.accessToken}` | ||
} | ||
}); | ||
|
||
const data = await response.json(); | ||
|
||
return data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import APIService from './api'; | ||
|
||
export interface SignUpBody { | ||
name: string; | ||
email: string; | ||
password: string; | ||
} | ||
|
||
export interface LogInBody { | ||
email: string; | ||
password: string; | ||
} | ||
|
||
export interface ResetPasswordBody { | ||
token: string; | ||
password: string; | ||
} | ||
|
||
export interface AppleAuthBody { | ||
code: string; | ||
} | ||
|
||
export default class AuthService { | ||
private api: APIService; | ||
|
||
constructor(api: APIService) { | ||
this.api = api; | ||
} | ||
|
||
public signUp(body: SignUpBody) { | ||
return this.api.post('/auth/register', body, false); | ||
} | ||
|
||
public logIn(body: LogInBody) { | ||
return this.api.post('/auth/login', body, false); | ||
} | ||
|
||
public resetPassword(body: ResetPasswordBody) { | ||
return this.api.post('/auth/reset-password', body, false); | ||
} | ||
|
||
public authenticateWithApple(body: AppleAuthBody) { | ||
return this.api.post('/auth/apple-callback', body, false); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import APIService from './api'; | ||
|
||
export class CardService { | ||
constructor(private api: APIService) {} | ||
|
||
public getCards() { | ||
return this.api.get('/cards'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import APIService from './api'; | ||
|
||
export interface AddProductToCartBody { | ||
productId: string; | ||
storeId: string; | ||
quantity?: number; | ||
} | ||
|
||
export interface UpdateCartProductBody { | ||
productId: string; | ||
cartId: string; | ||
quantity: number; | ||
} | ||
|
||
export interface CartFilters { | ||
storeId?: string; | ||
} | ||
|
||
export default class CartService { | ||
private api: APIService; | ||
|
||
constructor(api: APIService) { | ||
this.api = api; | ||
} | ||
|
||
public getCarts() { | ||
return this.api.get('/carts'); | ||
} | ||
|
||
public getCart(cartId: string) { | ||
return this.api.get(`/carts/${cartId}`); | ||
} | ||
|
||
public addProductToCart(body: AddProductToCartBody) { | ||
return this.api.post(`/carts/${body.storeId}/products`, body); | ||
} | ||
|
||
public removeProductFromCart(body: AddProductToCartBody) { | ||
return this.api.delete(`/carts/${body.storeId}/products/${body.productId}`); | ||
} | ||
|
||
public updateCartProduct(productId: string, body: UpdateCartProductBody) { | ||
return this.api.patch(`/carts/${body.cartId}/products/${productId}`, body); | ||
} | ||
|
||
public removeFromCart(productId: string, cartId: string) { | ||
return this.api.delete(`/carts/${cartId}/products/${productId}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import APIService from './api'; | ||
import AuthService from './auth'; | ||
import { CardService } from './cards'; | ||
import CartService from './carts'; | ||
import OrderService from './orders'; | ||
import ProductService from './products'; | ||
import SearchService from './search'; | ||
import StoresService from './stores'; | ||
import UsersService from './users'; | ||
|
||
export class DataService { | ||
public auth: AuthService; | ||
public users: UsersService; | ||
public cards: CardService; | ||
public carts: CartService; | ||
public orders: OrderService; | ||
public products: ProductService; | ||
public stores: StoresService; | ||
public search: SearchService; | ||
|
||
constructor() { | ||
const api = new APIService(); | ||
|
||
this.auth = new AuthService(api); | ||
this.users = new UsersService(api); | ||
this.cards = new CardService(api); | ||
this.carts = new CartService(api); | ||
this.orders = new OrderService(api); | ||
this.products = new ProductService(api); | ||
this.stores = new StoresService(api); | ||
this.search = new SearchService(api); | ||
} | ||
} | ||
|
||
const dataService = new DataService(); | ||
|
||
export default dataService; |
Oops, something went wrong.