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

feat: add self-hosted server details to DeviceInfo #273

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading