- Linting... Prettier...
This commit is contained in:
7
app/api/auctionStatus.js
Normal file
7
app/api/auctionStatus.js
Normal file
@@ -0,0 +1,7 @@
|
||||
import { API_ENDPOINTS, requestGet } from './index.js';
|
||||
|
||||
export const fetchAuctionStatus = (eventId, auth) => {
|
||||
const path = `${API_ENDPOINTS.GET_STATUS})/${eventId}`;
|
||||
const opts = { Authorization: auth ? `Bearer ${auth}` : null };
|
||||
return requestGet(path, null, opts);
|
||||
};
|
||||
10
app/api/bid.js
Normal file
10
app/api/bid.js
Normal file
@@ -0,0 +1,10 @@
|
||||
import { API_ENDPOINTS, requestPost } from './index.js';
|
||||
|
||||
export const placeBid = ({ bidAmount, bidderId, eventId, itemId, maxAmount }, auth) => {
|
||||
const requestOptions = { Authorization: auth ? `Bearer ${auth}` : null };
|
||||
return requestPost({
|
||||
path: `${API_ENDPOINTS.PLACE_BID}/${itemId}`,
|
||||
body: { bidAmount, bidderId, eventId, maxAmount },
|
||||
requestOptions,
|
||||
});
|
||||
};
|
||||
@@ -1,6 +1,6 @@
|
||||
import { API_ENDPOINTS, requestGet } from './index.js';
|
||||
|
||||
export const fetchEvents = (auth) => {
|
||||
const opts = { Authorization: auth ? `Bearer ${auth}` : null };
|
||||
return requestGet(API_ENDPOINTS.GET_EVENTS, null, opts);
|
||||
const opts = { Authorization: auth ? `Bearer ${auth}` : null };
|
||||
return requestGet(API_ENDPOINTS.GET_EVENTS, null, opts);
|
||||
};
|
||||
|
||||
@@ -1,21 +1,23 @@
|
||||
import { API_URL } from '../constants/constants.js';
|
||||
|
||||
export const constructUrl = (path, params, host = API_URL) => {
|
||||
if (!(/^\//g).test(path)) {
|
||||
throw new Error(`Invalid path! Expecting a valid relative path (path needs to start with /)(${path})`);
|
||||
}
|
||||
if (!/^\//g.test(path)) {
|
||||
throw new Error(
|
||||
`Invalid path! Expecting a valid relative path (path needs to start with /)(${path})`,
|
||||
);
|
||||
}
|
||||
|
||||
let url = path;
|
||||
let url = path;
|
||||
|
||||
if (host) {
|
||||
url = `${host}${url}`;
|
||||
}
|
||||
if (host) {
|
||||
url = `${host}${url}`;
|
||||
}
|
||||
|
||||
if (params) {
|
||||
url = `${url}?${params}`;
|
||||
}
|
||||
if (params) {
|
||||
url = `${url}?${params}`;
|
||||
}
|
||||
|
||||
return url;
|
||||
return url;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -26,59 +28,57 @@ export const constructUrl = (path, params, host = API_URL) => {
|
||||
* this serializes data in a way that helps support legacy endpoints
|
||||
*/
|
||||
export const formatPostData = (body) => {
|
||||
const postData = new FormData();
|
||||
Object.keys(body).forEach(key => postData.append(key, body[key]));
|
||||
return postData;
|
||||
const postData = new FormData();
|
||||
Object.keys(body).forEach((key) => postData.append(key, body[key]));
|
||||
return postData;
|
||||
};
|
||||
|
||||
const parseQueryParamsString = (queryParams) => {
|
||||
if (typeof queryParams !== 'string') {
|
||||
return null;
|
||||
}
|
||||
if (typeof queryParams !== 'string') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return queryParams;
|
||||
return queryParams;
|
||||
};
|
||||
|
||||
const parseQueryParamsObject = (queryParams) => {
|
||||
if (queryParams === null || typeof queryParams !== 'object' || Array.isArray(queryParams)) {
|
||||
return null;
|
||||
}
|
||||
if (queryParams === null || typeof queryParams !== 'object' || Array.isArray(queryParams)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Object
|
||||
.keys(queryParams)
|
||||
.map((key) => {
|
||||
const value = queryParams[key];
|
||||
return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
|
||||
})
|
||||
.join('&');
|
||||
return Object.keys(queryParams)
|
||||
.map((key) => {
|
||||
const value = queryParams[key];
|
||||
return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
|
||||
})
|
||||
.join('&');
|
||||
};
|
||||
|
||||
const parseQueryParamsArray = (queryParams) => {
|
||||
if (!Array.isArray(queryParams)) {
|
||||
return null;
|
||||
}
|
||||
if (!Array.isArray(queryParams)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return queryParams
|
||||
.map((param) => {
|
||||
const [key, value] = param.split('=');
|
||||
return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
|
||||
})
|
||||
.join('&');
|
||||
return queryParams
|
||||
.map((param) => {
|
||||
const [key, value] = param.split('=');
|
||||
return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
|
||||
})
|
||||
.join('&');
|
||||
};
|
||||
|
||||
export const parseQueryParams = queryParams => (
|
||||
parseQueryParamsString(queryParams) ||
|
||||
parseQueryParamsObject(queryParams) ||
|
||||
parseQueryParamsArray(queryParams) ||
|
||||
''
|
||||
);
|
||||
export const parseQueryParams = (queryParams) =>
|
||||
parseQueryParamsString(queryParams) ||
|
||||
parseQueryParamsObject(queryParams) ||
|
||||
parseQueryParamsArray(queryParams) ||
|
||||
'';
|
||||
|
||||
export const request = (url, options) => {
|
||||
try {
|
||||
return fetch(url, options).catch((err) => console.error(err));
|
||||
} catch (error) {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
try {
|
||||
return fetch(url, options).catch((err) => console.error(err));
|
||||
} catch (error) {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
};
|
||||
|
||||
export const unwrapJson = (response) => response.json();
|
||||
|
||||
126
app/api/index.js
126
app/api/index.js
@@ -1,95 +1,95 @@
|
||||
import {
|
||||
constructUrl,
|
||||
formatPostData,
|
||||
parseQueryParams,
|
||||
request,
|
||||
unwrapJson,
|
||||
validateResponse,
|
||||
constructUrl,
|
||||
formatPostData,
|
||||
parseQueryParams,
|
||||
request,
|
||||
unwrapJson,
|
||||
validateResponse,
|
||||
} from './helpers.js';
|
||||
|
||||
import { API_URL } from '../constants/constants.js';
|
||||
|
||||
const DefaultRequestOptions = {};
|
||||
|
||||
const endpoints = {
|
||||
// Events and Items
|
||||
GET_EVENTS: '/events',
|
||||
// GET_ITEMS: '/items?eventId=:event_id',
|
||||
GET_ITEMS: '/items',
|
||||
export const API_ENDPOINTS = {
|
||||
// Events and Items
|
||||
GET_EVENTS: '/events',
|
||||
// GET_ITEMS: '/items?eventId=:event_id',
|
||||
GET_ITEMS: '/items',
|
||||
|
||||
// Auction Interactions
|
||||
// GET_STATUS: '/auction/:event_id',
|
||||
GET_STATUS: '/auction',
|
||||
PLACE_BID: '/bids/:item_id',
|
||||
PURCHASE_ITEM: '/sales',
|
||||
// Auction Interactions
|
||||
// GET_STATUS: '/auction/:event_id',
|
||||
GET_STATUS: '/auction',
|
||||
PLACE_BID: '/bids',
|
||||
PURCHASE_ITEM: '/sales',
|
||||
|
||||
// User/Profile
|
||||
USER_SIGNUP: '/signup',
|
||||
USER_PROFILE: '/users/:user_id',
|
||||
// User/Profile
|
||||
USER_SIGNUP: '/signup',
|
||||
USER_PROFILE: '/users/:user_id',
|
||||
|
||||
VALIDATE_SIGNUP_EMAIL: '/signup/validate/email',
|
||||
VALIDATE_SIGNUP_NOM: '/signup/validate/nom',
|
||||
VALIDATE_SIGNUP_EMAIL: '/signup/validate/email',
|
||||
VALIDATE_SIGNUP_NOM: '/signup/validate/nom',
|
||||
|
||||
// Services
|
||||
APPLE_SIGNUP: '/auth/apple/login',
|
||||
APPLE_LINK: '/auth/apple/link',
|
||||
FACEBOOK_SIGNUP: '/auth/facebook/login',
|
||||
FACEBOOK_LINK: '/auth/facebook/link',
|
||||
GOOGLE_SIGNUP: '/auth/google/login',
|
||||
GOOGLE_LINK: '/auth/google/link',
|
||||
// Services
|
||||
APPLE_SIGNUP: '/auth/apple/login',
|
||||
APPLE_LINK: '/auth/apple/link',
|
||||
FACEBOOK_SIGNUP: '/auth/facebook/login',
|
||||
FACEBOOK_LINK: '/auth/facebook/link',
|
||||
GOOGLE_SIGNUP: '/auth/google/login',
|
||||
GOOGLE_LINK: '/auth/google/link',
|
||||
};
|
||||
|
||||
const cacheBuster = () => {
|
||||
const timestamp = String(Date.now());
|
||||
return `?buster=${timestamp}`;
|
||||
const timestamp = String(Date.now());
|
||||
return `?buster=${timestamp}`;
|
||||
};
|
||||
|
||||
export const getEndpointUrl = (endpoint) => {
|
||||
if (!endpoints[endpoint]) {
|
||||
throw new Error('Invalid API endpoint specified');
|
||||
}
|
||||
if (!endpoints[endpoint]) {
|
||||
throw new Error('Invalid API endpoint specified');
|
||||
}
|
||||
|
||||
return `${API_URL}${endpoints[endpoint]}`; //`${cacheBuster()}`;
|
||||
return `${API_URL}${endpoints[endpoint]}`; //`${cacheBuster()}`;
|
||||
};
|
||||
|
||||
export const requestGet = (path, queryParams = [], requestOptions = {}) => {
|
||||
const params = parseQueryParams(queryParams);
|
||||
const params = parseQueryParams(queryParams);
|
||||
|
||||
if (params === null) {
|
||||
throw new Error('Invalid queryParams');
|
||||
}
|
||||
if (params === null) {
|
||||
throw new Error('Invalid queryParams');
|
||||
}
|
||||
|
||||
return request(constructUrl(path, params), {
|
||||
...DefaultRequestOptions,
|
||||
...requestOptions
|
||||
return request(constructUrl(path, params), {
|
||||
...DefaultRequestOptions,
|
||||
...requestOptions,
|
||||
})
|
||||
.then(validateResponse)
|
||||
.then(unwrapJson);
|
||||
.then(validateResponse)
|
||||
.then(unwrapJson);
|
||||
};
|
||||
|
||||
export const requestPost = (options) => {
|
||||
const {
|
||||
path,
|
||||
body = {},
|
||||
queryParams = [],
|
||||
requestOptions = {},
|
||||
isFormattedPostData = false
|
||||
} = options;
|
||||
const {
|
||||
path,
|
||||
body = {},
|
||||
queryParams = [],
|
||||
requestOptions = {},
|
||||
isFormattedPostData = false,
|
||||
} = options;
|
||||
|
||||
const params = parseQueryParams(queryParams || []);
|
||||
const params = parseQueryParams(queryParams || []);
|
||||
|
||||
if (params === null) {
|
||||
throw new Error('invalid queryParams');
|
||||
}
|
||||
if (params === null) {
|
||||
throw new Error('invalid queryParams');
|
||||
}
|
||||
|
||||
// Additional formatting happens in `formatPostData()`
|
||||
// like handling what jQuery calls `traditional` form data
|
||||
return request(constructUrl(path, params), {
|
||||
...DefaultRequestOptions,
|
||||
...requestOptions,
|
||||
method: 'POST',
|
||||
body: isFormattedPostData ? body : formatPostData(body)
|
||||
// Additional formatting happens in `formatPostData()`
|
||||
// like handling what jQuery calls `traditional` form data
|
||||
return request(constructUrl(path, params), {
|
||||
...DefaultRequestOptions,
|
||||
...requestOptions,
|
||||
method: 'POST',
|
||||
body: isFormattedPostData ? body : formatPostData(body),
|
||||
})
|
||||
.then(validateResponse)
|
||||
.then(unwrapJson);
|
||||
.then(validateResponse)
|
||||
.then(unwrapJson);
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { API_ENDPOINTS, requestGet } from './index.js';
|
||||
|
||||
export const fetchItems = (eventId, auth) => {
|
||||
const path = String(API_ENDPOINTS.GET_ITEMS).replace(/:event_id$/, eventId);
|
||||
const opts = { Authorization: auth ? `Bearer ${auth}` : null };
|
||||
return requestGet(path, null, opts);
|
||||
const path = String(API_ENDPOINTS.GET_ITEMS).replace(/:event_id$/, eventId);
|
||||
const opts = { Authorization: auth ? `Bearer ${auth}` : null };
|
||||
return requestGet(path, null, opts);
|
||||
};
|
||||
|
||||
@@ -1,15 +1,19 @@
|
||||
import { API_ENDPOINTS, requestGet } from './index.js';
|
||||
|
||||
export const getEmailAvailability = (email) => requestGet(`${API_ENDPOINTS.VALIDATE_SIGNUP_EMAIL}/&{encodeURI(email)}`);
|
||||
export const getEmailAvailability = (email) =>
|
||||
requestGet(`${API_ENDPOINTS.VALIDATE_SIGNUP_EMAIL}/&{encodeURI(email)}`);
|
||||
|
||||
export const getNomAvailaibility = (nomDeBid) => requestGet(`${API_ENDPOINTS.VALIDATE_SIGNUP_NOM}/${encodeURI(nomDeBid)}`);
|
||||
export const getNomAvailaibility = (nomDeBid) =>
|
||||
requestGet(`${API_ENDPOINTS.VALIDATE_SIGNUP_NOM}/${encodeURI(nomDeBid)}`);
|
||||
|
||||
export const loginUser = (username, password) => requestPost({
|
||||
path: API_ENDPOINTS.LOGIN,
|
||||
body: { username, password },
|
||||
});
|
||||
export const loginUser = (username, password) =>
|
||||
requestPost({
|
||||
path: API_ENDPOINTS.LOGIN,
|
||||
body: { username, password },
|
||||
});
|
||||
|
||||
export const registerNewUser = (user) => requestPost({
|
||||
path: API_ENDPOINTS.USER_SIGNUP,
|
||||
body: { user },
|
||||
});
|
||||
export const registerNewUser = (user) =>
|
||||
requestPost({
|
||||
path: API_ENDPOINTS.USER_SIGNUP,
|
||||
body: { user },
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user