Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
jahow committed Jan 31, 2025
1 parent e05aa00 commit d6933a5
Showing 2 changed files with 45 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -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.toString().includes('multiple')) {
return Promise.resolve([
{ name: 'firstCollection' },
{ name: 'otherCollection' },
])
}
return Promise.resolve(this.collections_)
}
getCollectionInfo(collectionId) {
return Promise.resolve({
@@ -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'
)
})
})
@@ -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 () => {
@@ -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 () => {
@@ -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/multiple/collections/myCollection/items?limit=-1&f=application%2Fjson`
)
})
})
})

describe('#resetUrl', () => {
@@ -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}`
)
})
})
Original file line number Diff line number Diff line change
@@ -61,7 +61,6 @@ export class RecordApiFormComponent {
{ value: 'application/json', label: 'JSON' },
]
endpoint: WfsEndpoint | OgcApiEndpoint | undefined
firstCollection: string | undefined

apiQueryUrl$ = combineLatest([
this.offset$,
@@ -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
}
}
@@ -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)
}
@@ -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
)
}

0 comments on commit d6933a5

Please sign in to comment.