Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/weaponsforge 14 #99

Merged
merged 9 commits into from
Apr 7, 2023
12 changes: 11 additions & 1 deletion client/src/lib/services/contacts/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import axios from 'axios'
import { FirebaseFirestore } from '@/lib/utils/firebase/firestore'
import createRequestObject from '@/lib/utils/firebase/firebaserequest'
import { orderBy } from 'firebase/firestore'

export const createContact = async(user_uid, newContact) => {
Expand All @@ -24,4 +26,12 @@ export const updateContact = async(user_uid, contact_id, newContact) => {
export const deleteContact = async(user_uid) => {
const response = await FirebaseFirestore.deleteDoc(`users/${user_uid}`)
return response
}
}

export const exportContacts = async ({ responseType = 'arraybuffer', type = 'csv', ids = undefined }) => {
const CONTACTS_API_URL = `${process.env.NEXT_PUBLIC_BASE_API_URL}/api/contacts/export`
const body = { type, ids }

const obj = await createRequestObject({ body })
return await axios({ ...obj, url: CONTACTS_API_URL, method: 'POST', responseType })
}
50 changes: 50 additions & 0 deletions client/src/lib/utils/firebase/firebaserequest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { auth } from './config'

/**
* Attach a fresh Firebase Bearer Authorization header accessToken on an axios HTTP params object.
* @param {Object} obj - body or params input parameters for a REST API.
* @returns {Object} Input object with a fresh Firebase Bearer Authorization header.
*/
const setAuthHeaders = async (obj) => {
let token
const object = { ...obj }

try {
token = await auth.currentUser.getIdToken(true)
} catch (err) {
throw new Error(err.message)
}

if (token) {
object.headers.Authorization = `Bearer ${token}`
} else {
object.headers.Authorization = ''
}

return object
}

/**
*
* @typedef {Object} param - Input parameters
* @param {Object} param.body - Object input to place in an HTTP request body.
* @param {Object} param.params - Object input to place in an HTTP request params.
* @returns {Object} The original request body (or params) with attached Firebase accessToken in the Authorization header.
*/
const createRequestObject = async ({ body, params }) => {
const obj = {
headers: {}
}

if (body) {
obj.data = body
}

if (params) {
obj.params = params
}

return await setAuthHeaders(obj)
}

export default createRequestObject
Binary file added server/assets/fonts/roboto/Roboto-Bold.ttf
Binary file not shown.
Binary file added server/assets/fonts/roboto/Roboto-BoldItalic.ttf
Binary file not shown.
Binary file added server/assets/fonts/roboto/Roboto-Italic.ttf
Binary file not shown.
Binary file added server/assets/fonts/roboto/Roboto-Regular.ttf
Binary file not shown.
Loading