Skip to content

Commit

Permalink
Clean up a few small things
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanHahn committed Oct 31, 2024
1 parent 92c84c7 commit 5fa6192
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 27 deletions.
15 changes: 6 additions & 9 deletions src/member-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { keyBy } from './lib/key-by.js'
import { abortSignalAny } from './lib/ponyfills.js'
import timingSafeEqual from 'string-timing-safe-equal'
import { isHostnameIpAddress } from './lib/is-hostname-ip-address.js'
import { ErrorWithCode } from './lib/error.js'
import { ErrorWithCode, getErrorMessage } from './lib/error.js'
import { wsCoreReplicator } from './lib/ws-core-replicator.js'
import { MEMBER_ROLE_ID, ROLES, isRoleIdForNewInvite } from './roles.js'
/**
Expand Down Expand Up @@ -285,7 +285,7 @@ export class MemberApi extends TypedEmitter {
*
* @param {string} baseUrl
* @param {object} [options]
* @param {boolean} [options.dangerouslyAllowInsecureConnections]
* @param {boolean} [options.dangerouslyAllowInsecureConnections] Allow insecure network connections. Should only be used in tests.
* @returns {Promise<void>}
*/
async addServerPeer(
Expand All @@ -300,8 +300,7 @@ export class MemberApi extends TypedEmitter {

const { serverDeviceId } = await this.#addServerToProject(baseUrl)

const roleId = MEMBER_ROLE_ID
await this.#roles.assignRole(serverDeviceId, roleId)
await this.#roles.assignRole(serverDeviceId, MEMBER_ROLE_ID)

await this.#waitForInitialSyncWithServer({
baseUrl,
Expand Down Expand Up @@ -344,13 +343,11 @@ export class MemberApi extends TypedEmitter {
headers: { 'Content-Type': 'application/json' },
})
} catch (err) {
const message =
err && typeof err === 'object' && 'message' in err
? err.message
: String(err)
throw new ErrorWithCode(
'NETWORK_ERROR',
`Failed to add server peer due to network error: ${message}`
`Failed to add server peer due to network error: ${getErrorMessage(
err
)}`
)
}

Expand Down
33 changes: 15 additions & 18 deletions src/server/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export default async function routes(
const allowedProjectsSetOrNumber = Array.isArray(allowedProjects)
? new Set(allowedProjects)
: allowedProjects

/**
* @param {FastifyRequest} req
*/
Expand Down Expand Up @@ -62,11 +63,11 @@ export default async function routes(
},
},
},
async function (_req, reply) {
async function () {
const { deviceId, name } = this.comapeo.getDeviceInfo()
reply.send({
return {
data: { deviceId, name: name || serverName },
})
}
}
)

Expand Down Expand Up @@ -115,17 +116,14 @@ export default async function routes(
verifyBearerAuth(req)
},
},
async function (req, reply) {
const existingProjects = await this.comapeo.listProjects()

reply.send({
data: existingProjects.map((project) => ({
async function () {
const projects = await this.comapeo.listProjects()
return {
data: projects.map((project) => ({
projectId: project.projectId,
name: project.name,
})),
})

return reply
}
}
)

Expand Down Expand Up @@ -154,7 +152,7 @@ export default async function routes(
},
},
},
async function (req, reply) {
async function (req) {
const { projectName } = req.body
const projectKey = Buffer.from(req.body.projectKey, 'hex')
const projectPublicId = projectKeyToPublicId(projectKey)
Expand Down Expand Up @@ -231,12 +229,11 @@ export default async function routes(
const project = await this.comapeo.getProject(projectPublicId)
project.$sync.start()

reply.send({
return {
data: {
deviceId: this.comapeo.deviceId,
},
})
return reply
}
}
)

Expand Down Expand Up @@ -291,11 +288,11 @@ export default async function routes(
await ensureProjectExists(this, req)
},
},
async function (req, reply) {
async function (req) {
const { projectPublicId } = req.params
const project = await this.comapeo.getProject(projectPublicId)

reply.send({
return {
data: (await project.observation.getMany({ includeDeleted: true })).map(
(obs) => ({
docId: obs.docId,
Expand All @@ -317,7 +314,7 @@ export default async function routes(
tags: obs.tags,
})
),
})
}
}
)

Expand Down

0 comments on commit 5fa6192

Please sign in to comment.