mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-06-06 23:59:01 +02:00
39a396f3a2
Use fetch-utils in web GitOrigin-RevId: cbd0298200bbe42567c6e94934bfb5114fa9b66f
52 lines
1.2 KiB
JavaScript
52 lines
1.2 KiB
JavaScript
const { callbackify } = require('util')
|
|
const OError = require('@overleaf/o-error')
|
|
const { fetchJson } = require('@overleaf/fetch-utils')
|
|
const settings = require('@overleaf/settings')
|
|
|
|
async function getContactIds(userId, options) {
|
|
options = options ?? { limit: 50 }
|
|
|
|
const url = new URL(`${settings.apis.contacts.url}/user/${userId}/contacts`)
|
|
|
|
for (const [key, val] of Object.entries(options)) {
|
|
url.searchParams.set(key, val)
|
|
}
|
|
|
|
let body
|
|
try {
|
|
body = await fetchJson(url)
|
|
} catch (err) {
|
|
throw OError.tag(err, 'failed request to contacts API', { userId })
|
|
}
|
|
|
|
return body?.contact_ids || []
|
|
}
|
|
|
|
async function addContact(userId, contactId) {
|
|
const url = new URL(`${settings.apis.contacts.url}/user/${userId}/contacts`)
|
|
|
|
let body
|
|
try {
|
|
body = await fetchJson(url, {
|
|
method: 'POST',
|
|
json: { contact_id: contactId },
|
|
})
|
|
} catch (err) {
|
|
throw OError.tag(err, 'failed request to contacts API', {
|
|
userId,
|
|
contactId,
|
|
})
|
|
}
|
|
|
|
return body?.contact_ids || []
|
|
}
|
|
|
|
module.exports = {
|
|
getContactIds: callbackify(getContactIds),
|
|
addContact: callbackify(addContact),
|
|
promises: {
|
|
getContactIds,
|
|
addContact,
|
|
},
|
|
}
|