Skip to content

Commit

Permalink
Merge pull request #86 from amansinghbais/gitbook-api
Browse files Browse the repository at this point in the history
Implemented: centralized services for Gitbook api calls
  • Loading branch information
ymaheshwari1 authored Jul 9, 2024
2 parents f32b9a8 + 1ea01ba commit f899de8
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ const api = async (customConfig: any) => {
* @return {Promise} Response from API as returned by Axios
*/
const client = (config: any) => {
return axios.request({ paramsSerializer, ...config });
return axios.create().request({ paramsSerializer, ...config })
}

export { api as default, initialise, client, axios, getConfig, init, updateToken, updateInstanceUrl, resetConfig };
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { events, Product, Response, Stock, Order, OrderItem, OrderPart, OPERATOR, User } from './types'
import api, { client, getConfig, init, initialise, resetConfig, updateToken, updateInstanceUrl } from './api'
import { getTelecomCountryCode, hasError, isError } from './util'
import { getAvailableTimeZones, getNotificationEnumIds, getNotificationUserPrefTypeIds, getUserFacilities, fetchProducts, fetchProductsGroupedBy, fetchProductsGroupedByParent, fetchProductsStock, fetchProductsStockAtFacility, getOrderDetails, getProductIdentificationPref, getProfile, logout, removeClientRegistrationToken, setProductIdentificationPref, storeClientRegistrationToken, subscribeTopic, unsubscribeTopic, updateOrderStatus, getUserPreference, setUserPreference, setUserLocale, setUserTimeZone } from './modules'
import { askQuery, getAvailableTimeZones, getGitBookPage, getNotificationEnumIds, getNotificationUserPrefTypeIds, getUserFacilities, fetchProducts, fetchProductsGroupedBy, fetchProductsGroupedByParent, fetchProductsStock, fetchProductsStockAtFacility, getOrderDetails, getProductIdentificationPref, getProfile, logout, removeClientRegistrationToken, searchQuery, setProductIdentificationPref, storeClientRegistrationToken, subscribeTopic, unsubscribeTopic, updateOrderStatus, getUserPreference, setUserPreference, setUserLocale, setUserTimeZone } from './modules'

export {
api,
askQuery,
client,
getOrderDetails,
updateOrderStatus,
fetchProducts,
fetchProductsGroupedBy,
fetchProductsGroupedByParent,
getAvailableTimeZones,
getGitBookPage,
getNotificationEnumIds,
getNotificationUserPrefTypeIds,
getConfig,
Expand Down Expand Up @@ -39,6 +41,7 @@ export {
OrderPart,
OPERATOR,
removeClientRegistrationToken,
searchQuery,
storeClientRegistrationToken,
subscribeTopic,
unsubscribeTopic,
Expand Down
92 changes: 92 additions & 0 deletions src/modules/gitbook/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import api, {client} from "../../api";
import { hasError } from "../../util";

async function askQuery(payload: any): Promise<any> {
try {
const resp = await client({
url: `spaces/${payload.spaceId}/search/ask`,
method: "post",
baseURL: payload.baseURL,
data: {
"query": payload.queryString
},
headers: {
Authorization: 'Bearer ' + payload.token,
"Content-Type": "application/json"
}
}) as any;

if (!hasError(resp)) {
return Promise.resolve(resp)
} else {
throw resp.data;
}
} catch (err) {
return Promise.reject({
code: 'error',
message: 'Something went wrong',
serverResponse: err
})
}
}

async function getGitBookPage(payload: any): Promise<any> {
try {
const resp = await client({
url: `spaces/${payload.spaceId}/content/page/${payload.pageId}`,
method: "get",
baseURL: payload.baseURL,
headers: {
Authorization: 'Bearer ' + payload.token,
"Content-Type": "application/json"
}
}) as any;

if (!hasError(resp)) {
return Promise.resolve(resp)
} else {
throw resp.data;
}
} catch (err) {
return Promise.reject({
code: 'error',
message: 'Something went wrong',
serverResponse: err
})
}
}

async function searchQuery(payload: any): Promise<any> {
try {
const resp = await client({
url: `spaces/${payload.spaceId}/search`,
method: "get",
baseURL: payload.baseURL,
params: {
"query": payload.queryString
},
headers: {
Authorization: 'Bearer ' + payload.token,
"Content-Type": "application/json"
}
}) as any;

if (!hasError(resp)) {
return Promise.resolve(resp)
} else {
throw resp.data;
}
} catch (err) {
return Promise.reject({
code: 'error',
message: 'Something went wrong',
serverResponse: err
})
}
}

export {
askQuery,
getGitBookPage,
searchQuery
}
4 changes: 4 additions & 0 deletions src/modules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@ import { fetchProducts, fetchProductsGroupedBy, fetchProductsGroupedByParent } f
import { getAvailableTimeZones, getProductIdentificationPref, getProfile, getUserFacilities, getUserPreference, logout, setProductIdentificationPref, setUserPreference, setUserLocale, setUserTimeZone } from '../modules/user'
import { getNotificationEnumIds, getNotificationUserPrefTypeIds, removeClientRegistrationToken, storeClientRegistrationToken, subscribeTopic, unsubscribeTopic } from '../modules/notification'
import { fetchProductsStock, fetchProductsStockAtFacility } from '../modules/stock'
import { askQuery, getGitBookPage, searchQuery } from '../modules/gitbook'

export {
askQuery,
fetchProducts,
fetchProductsGroupedBy,
fetchProductsGroupedByParent,
fetchProductsStock,
fetchProductsStockAtFacility,
getAvailableTimeZones,
getGitBookPage,
getNotificationEnumIds,
getNotificationUserPrefTypeIds,
getOrderDetails,
getProductIdentificationPref,
getProfile,
removeClientRegistrationToken,
logout,
searchQuery,
setProductIdentificationPref,
storeClientRegistrationToken,
subscribeTopic,
Expand Down

0 comments on commit f899de8

Please sign in to comment.