Skip to content

Commit

Permalink
feat(ui): use OGCAPI collection name if available
Browse files Browse the repository at this point in the history
If multiple collections are available in an endpoint then use the name in the
online resource instead of the first collection
  • Loading branch information
jahow committed Jan 31, 2025
1 parent aa17976 commit 3567b65
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@ const mockDatasetServiceDistribution: DatasetServiceDistribution = {

jest.mock('@camptocamp/ogc-client', () => ({
OgcApiEndpoint: class {
collections_ = [{ name: 'uniqueCollection' }]
constructor(private url) {}
get allCollections() {
return Promise.resolve([{ name: 'feature1' }])
if (this.url.href.includes('multiple')) {
return Promise.resolve([
{ name: 'firstCollection' },
{ name: 'otherCollection' },
])
}
return Promise.resolve(this.collections_)
}
getCollectionInfo(collectionId) {
return Promise.resolve({
Expand Down Expand Up @@ -91,7 +98,7 @@ describe('RecordApiFormComponent', () => {
expect(component.format$.getValue()).toBe('application/json')
const url = await firstValueFrom(component.apiQueryUrl$)
expect(url).toBe(
'https://api.example.com/data/collections/feature1/items?limit=-1&f=application%2Fjson'
'https://api.example.com/data/collections/uniqueCollection/items?limit=-1&f=application%2Fjson'
)
})
})
Expand All @@ -105,7 +112,7 @@ describe('RecordApiFormComponent', () => {
component.setFormat(mockFormat)
const url = await firstValueFrom(component.apiQueryUrl$)
expect(url).toBe(
`https://api.example.com/data/collections/feature1/items?limit=${mockLimit}&offset=${mockOffset}&f=${encodeURIComponent(mockFormat)}`
`https://api.example.com/data/collections/uniqueCollection/items?limit=${mockLimit}&offset=${mockOffset}&f=${encodeURIComponent(mockFormat)}`
)
})
it('should remove the param in url if value is null', async () => {
Expand All @@ -117,7 +124,7 @@ describe('RecordApiFormComponent', () => {
component.setFormat(mockFormat)
const url = await firstValueFrom(component.apiQueryUrl$)
expect(url).toBe(
`https://api.example.com/data/collections/feature1/items?limit=${mockLimit}&offset=${mockOffset}&f=${encodeURIComponent(mockFormat)}`
`https://api.example.com/data/collections/uniqueCollection/items?limit=${mockLimit}&offset=${mockOffset}&f=${encodeURIComponent(mockFormat)}`
)
})
it('should remove the param in url if value is zero', async () => {
Expand All @@ -129,9 +136,24 @@ describe('RecordApiFormComponent', () => {
component.setFormat(mockFormat)
const url = await firstValueFrom(component.apiQueryUrl$)
expect(url).toBe(
`https://api.example.com/data/collections/feature1/items?limit=${mockLimit}&offset=${mockOffset}&f=${encodeURIComponent(mockFormat)}`
`https://api.example.com/data/collections/uniqueCollection/items?limit=${mockLimit}&offset=${mockOffset}&f=${encodeURIComponent(mockFormat)}`
)
})

describe('when multiple collections available', () => {
it('uses the link name', async () => {
component.apiLink = {
...mockDatasetServiceDistribution,
url: new URL('https://api.example.com/multiple/'),
name: 'myCollection',
}
fixture.detectChanges()
const url = await firstValueFrom(component.apiQueryUrl$)
expect(url).toBe(
`https://api.example.com/data/collections/myCollection/items?limit=-1&f=application%2Fjson`
)
})
})
})

describe('#resetUrl', () => {
Expand Down Expand Up @@ -175,7 +197,16 @@ describe('RecordApiFormComponent', () => {
expect(component.format$.getValue()).toBe('application/json')
const url = await firstValueFrom(component.apiQueryUrl$)
expect(url).toBe(
`https://api.example.com/data?type=mockFeatureType&options={"outputFormat":"application/json","limit":-1}`
`https://api.example.com/data?type=mockFeatureType&options={"outputFormat":"application/json"}`
)
})

it('sets maxFeatures if a limit is set', async () => {
component.setLimit('12')
expect(component.limit$.getValue()).toBe('12')
const url = await firstValueFrom(component.apiQueryUrl$)
expect(url).toBe(
`https://api.example.com/data?type=mockFeatureType&options={"outputFormat":"application/json","maxFeatures":12}`
)
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export class RecordApiFormComponent {
{ value: 'application/json', label: 'JSON' },
]
endpoint: WfsEndpoint | OgcApiEndpoint | undefined
firstCollection: string | undefined

apiQueryUrl$ = combineLatest([
this.offset$,
Expand Down Expand Up @@ -128,7 +127,7 @@ export class RecordApiFormComponent {
this.supportOffset = this.endpoint.supportsStartIndex()
return this.endpoint.getServiceInfo().outputFormats
} else {
return (await this.endpoint.getCollectionInfo(this.firstCollection))
return (await this.endpoint.getCollectionInfo(this.apiFeatureType))
.itemFormats
}
}
Expand All @@ -140,7 +139,11 @@ export class RecordApiFormComponent {
await (this.endpoint as WfsEndpoint).isReady()
} else {
this.endpoint = new OgcApiEndpoint(this.apiBaseUrl)
this.firstCollection = (await this.endpoint.allCollections)[0].name
const collections = await this.endpoint.allCollections
// if there's only one collection, use this instead of the name given in the link.
if (collections.length === 1) {
this.apiFeatureType = collections[0].name
}
}
this.endpoint$.next(this.endpoint)
}
Expand All @@ -161,11 +164,12 @@ export class RecordApiFormComponent {
}

if (this.endpoint instanceof WfsEndpoint) {
delete options.limit
options.maxFeatures = limit !== '-1' ? Number(limit) : undefined
return this.endpoint.getFeatureUrl(this.apiFeatureType, options)
} else {
return await this.endpoint.getCollectionItemsUrl(
this.firstCollection,
this.apiFeatureType,
options
)
}
Expand Down

0 comments on commit 3567b65

Please sign in to comment.