smartid-calls
Version:
Smart-ID client module for Node.JS with proxy layer
78 lines (68 loc) • 2.04 kB
text/typescript
import { ModuleConfigI } from '../types/Request';
import axios from 'axios';
export class GetCertificate {
private config: ModuleConfigI;
private level: string = 'QUALIFIED';
constructor(config: ModuleConfigI, level: string) {
this.config = config;
this.level = level;
}
async certificateByDocumentNumber(documentNumber: string): Promise<any> {
try {
const response = await axios({
method: 'post',
url: `${this.config.host}/certificatechoice/document/${documentNumber}`,
proxy: this.config.proxy || false,
responseType: 'json',
validateStatus: (status) => status === 200,
data: Object.assign({
certificateLevel: this.level,
},
this.config.requestParams
)
});
const body = response.data;
if (typeof body !== 'object' || !body.sessionID) {
throw new Error('Invalid response')
}
return {
sessionId: response.data.sessionID,
}
} catch (error) {
if (error.response.status == 471) {
throw new Error('NO_CERTS');
} else {
return error;
}
}
}
async certificateByPersonalNumber(country: string, persNr: string): Promise<any> {
try {
const response = await axios({
method: 'post',
url: `${this.config.host}/certificatechoice/pno/${country.toUpperCase()}/${persNr}`,
responseType: 'json',
proxy: this.config.proxy || false,
validateStatus: (status) => status === 200,
data: Object.assign({
certificateLevel: this.level,
},
this.config.requestParams
)
});
const body = response.data;
if (typeof body !== 'object' || !body.sessionID) {
throw new Error('Invalid response')
}
return {
sessionId: response.data.sessionID,
}
} catch (error) {
if (error.response.status == 471) {
throw new Error('NO_CERTS');
} else {
return error;
}
}
}
}