forked from openscd/open-scd
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #265 from com-pas/Fetch_NSDoc_Files_Locally
Feat: CoMPAS-OpenSCD should fetch the NSDoc files locally
- Loading branch information
Showing
4 changed files
with
139 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { handleError, handleResponse } from './foundation.js'; | ||
|
||
interface NsDocFile { | ||
filename: string; | ||
id: string; | ||
name: string; | ||
} | ||
|
||
// Temporary solution to map to the old logic | ||
const nsDocfiles: NsDocFile[] = [ | ||
{ | ||
filename: 'IEC_61850-7-2_2007B3-en.nsdoc', | ||
name: 'IEC 61850-7-2', | ||
id: '87e5bed8-2f27-4006-8673-f9d00b0a5426', | ||
}, | ||
{ | ||
filename: 'IEC_61850-7-3_2007B3-en.nsdoc', | ||
name: 'IEC_61850-7-3', | ||
id: '315b02ac-c4aa-4495-9b4f-f7175a75c315', | ||
}, | ||
{ | ||
filename: 'IEC_61850-7-4_2007B3-en.nsdoc', | ||
name: 'IEC 61850-7-4', | ||
id: 'da1b2ca0-1263-4b10-9b16-2f148ae3a1f1', | ||
}, | ||
{ | ||
filename: 'IEC_61850-8-1_2003A2-en.nsdoc', | ||
name: 'IEC 61850-8-1', | ||
id: '0c052ea7-a010-4ca8-b2c7-caa665cabc46', | ||
}, | ||
]; | ||
|
||
const createElement = ( | ||
name: string, | ||
textContent: string, | ||
document: XMLDocument | ||
): Element => { | ||
const element: Element = document.createElement(name); | ||
element.textContent = textContent; | ||
|
||
return element; | ||
}; | ||
|
||
/** TODO: Make this return JSON */ | ||
export function CompasNSDocFileService() { | ||
return { | ||
listNsdocFiles(): Promise<Document> { | ||
const document: XMLDocument = new DOMParser().parseFromString( | ||
'<NsdocListResponse></NsdocListResponse>', | ||
'text/xml' | ||
); | ||
|
||
nsDocfiles.forEach(nsDocFile => { | ||
const nsDocFileElement: Element = document.createElement('NsdocFile'); | ||
|
||
nsDocFileElement.appendChild( | ||
createElement('Id', nsDocFile.id, document) | ||
); | ||
nsDocFileElement.appendChild( | ||
createElement('NsdocId', nsDocFile.name, document) | ||
); | ||
nsDocFileElement.appendChild( | ||
createElement('Checksum', nsDocFile.id, document) | ||
); | ||
nsDocFileElement.appendChild( | ||
createElement('Filename', nsDocFile.filename, document) | ||
); | ||
|
||
document | ||
.querySelector('NsdocListResponse')! | ||
.appendChild(nsDocFileElement); | ||
}); | ||
|
||
return Promise.resolve(document); | ||
}, | ||
|
||
getNsdocFile(id: string): Promise<Document> { | ||
const nsDocFile: NsDocFile = nsDocfiles.find(f => f.id === id)!; | ||
|
||
if (!nsDocFile) { | ||
return Promise.reject(`Unable to find nsDoc file with id ${id}`); | ||
} | ||
return fetch(`./public/nsdoc/${nsDocFile.filename}`) | ||
.catch(handleError) | ||
.then(handleResponse) | ||
.then(res => { | ||
const document: XMLDocument = new DOMParser().parseFromString( | ||
'<NsdocResponse></NsdocResponse>', | ||
'text/xml' | ||
); | ||
|
||
document | ||
.querySelector('NsdocResponse')! | ||
.appendChild(createElement('NsdocFile', res, document)); | ||
|
||
return document; | ||
}); | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { expect } from '@open-wc/testing'; | ||
|
||
import { CompasNSDocFileService } from '../../../src/compas-services/CompasNSDocFileService.js'; | ||
|
||
describe('compas-nsdocfile-service', () => { | ||
it('Should list all NSDoc files', async () => { | ||
const res = await CompasNSDocFileService().listNsdocFiles(); | ||
|
||
const nsDocFiles: Element[] = Array.from(res.querySelectorAll('NsdocFile')); | ||
|
||
expect(nsDocFiles.length).to.equal(4); | ||
}); | ||
|
||
it('Should fail on invalid request', done => { | ||
const id = '315b02ac-c4aa-4495-9b4f-f7175a75c315'; | ||
CompasNSDocFileService() | ||
.getNsdocFile(id) | ||
.then(() => done('Failed')) | ||
.catch(err => { | ||
expect(err.status).to.equal(404); | ||
expect(err.type).to.equal('NotFoundError'); | ||
done(); | ||
}); | ||
}); | ||
it('Should fail on invalid id', done => { | ||
const id = '1'; | ||
|
||
CompasNSDocFileService() | ||
.getNsdocFile(id) | ||
.then(() => done('Failed')) | ||
.catch(err => { | ||
expect(err).to.equal(`Unable to find nsDoc file with id ${id}`); | ||
done(); | ||
}); | ||
}); | ||
}); |