Skip to content

Commit

Permalink
feat: add self-hosted server details to DeviceInfo (#273)
Browse files Browse the repository at this point in the history
This adds a new object, `selfHostedServerDetails`, to `DeviceInfo`. It
currently has one property, `baseUrl`, but could have more later.

See [#268].

[#268]: #268
  • Loading branch information
EvanHahn authored Oct 22, 2024
1 parent faf89b5 commit f7ae02a
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 0 deletions.
5 changes: 5 additions & 0 deletions proto/deviceInfo/v1.proto
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ message DeviceInfo_1 {
selfHostedServer = 4;
}

message SelfHostedServerDetails {
string baseUrl = 1 [(required) = true];
}

string name = 5;
DeviceType deviceType = 6;
optional SelfHostedServerDetails selfHostedServerDetails = 7;
}
11 changes: 11 additions & 0 deletions schema/deviceInfo/v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@
"UNRECOGNIZED"
],
"description": "Type of device"
},
"selfHostedServerDetails": {
"type": "object",
"properties": {
"baseUrl": {
"description": "base URL for the server",
"type": "string",
"minLength": 1
}
},
"required": ["baseUrl"]
}
},
"required": ["schemaName", "name", "deviceType"],
Expand Down
11 changes: 11 additions & 0 deletions src/lib/encode-conversions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,17 @@ export const convertRole: ConvertFunction<'role'> = (mapeoDoc) => {
}

export const convertDeviceInfo: ConvertFunction<'deviceInfo'> = (mapeoDoc) => {
const { selfHostedServerDetails } = mapeoDoc
if (selfHostedServerDetails) {
try {
new URL(selfHostedServerDetails.baseUrl || '')
} catch (_err) {
throw new Error(
'deviceInfo.selfHostedServerDetails.baseUrl is not a valid URL'
)
}
}

return {
common: convertCommon(mapeoDoc),
...mapeoDoc,
Expand Down
36 changes: 36 additions & 0 deletions test/fixtures/bad-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,42 @@ export const badDocs = [
deleted: false,
},
},
{
text: 'server without base URL',
/** @type {import('../../dist/index.js').DeviceInfo} */
doc: {
docId: cachedValues.docId,
versionId: cachedValues.versionId,
originalVersionId: cachedValues.originalVersionId,
schemaName: 'deviceInfo',
createdAt: cachedValues.createdAt,
updatedAt: cachedValues.updatedAt,
links: [],
name: 'my server',
deviceType: 'selfHostedServer',
deleted: false,
selfHostedServerDetails: {},
},
},
{
text: 'server with bogus base URL',
/** @type {import('../../dist/index.js').DeviceInfo} */
doc: {
docId: cachedValues.docId,
versionId: cachedValues.versionId,
originalVersionId: cachedValues.originalVersionId,
schemaName: 'deviceInfo',
createdAt: cachedValues.createdAt,
updatedAt: cachedValues.updatedAt,
links: [],
name: 'my server',
deviceType: 'selfHostedServer',
deleted: false,
selfHostedServerDetails: {
baseUrl: 'foo',
},
},
},
{
text: 'icon without name',
doc: {
Expand Down
18 changes: 18 additions & 0 deletions test/fixtures/good-docs-completed.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,24 @@ export const goodDocsCompleted = [
deviceType: 'UNRECOGNIZED',
},
},
{
doc: {
docId: cachedValues.docId,
versionId: cachedValues.versionId,
originalVersionId: cachedValues.originalVersionId,
schemaName: 'deviceInfo',
createdAt: cachedValues.createdAt,
updatedAt: cachedValues.updatedAt,
links: [],
name: 'my server',
deviceType: 'selfHostedServer',
deleted: false,
selfHostedServerDetails: {
baseUrl: 'https://mapeo.example/foo',
},
},
expected: {},
},
{
doc: {
docId: cachedValues.docId,
Expand Down

0 comments on commit f7ae02a

Please sign in to comment.