Skip to content

Commit

Permalink
Start transition
Browse files Browse the repository at this point in the history
  • Loading branch information
koredefashokun committed Dec 29, 2024
1 parent e3fd02f commit 84e32b3
Show file tree
Hide file tree
Showing 25 changed files with 1,391 additions and 34 deletions.
1 change: 1 addition & 0 deletions apps/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@react-navigation/stack": "^6.2.1",
"@sentry/react-native": "^5.24.3",
"@shopify/flash-list": "1.6.4",
"@tanstack/react-query": "^5.62.11",
"@urql/exchange-graphcache": "^6.0.4",
"core-js": "^3.30.1",
"date-fns": "^2.29.3",
Expand Down
110 changes: 110 additions & 0 deletions apps/app/src/data/api.ts
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;
}
}
45 changes: 45 additions & 0 deletions apps/app/src/data/auth.ts
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);
}
}
9 changes: 9 additions & 0 deletions apps/app/src/data/cards.ts
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');
}
}
49 changes: 49 additions & 0 deletions apps/app/src/data/carts.ts
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}`);
}
}
37 changes: 37 additions & 0 deletions apps/app/src/data/index.ts
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;
Loading

0 comments on commit 84e32b3

Please sign in to comment.