@geogirafe/lib-geoportal
Version:
GeoGirafe is a flexible application to build online geoportals.
45 lines (44 loc) • 1.53 kB
JavaScript
import { generateQrCode } from '../../../tools/utils/qrcode.js';
class GeoGirafeShareManager {
serviceUrl;
urlManager;
constructor(serviceUrl, urlManager) {
this.serviceUrl = serviceUrl;
this.urlManager = urlManager;
}
async shortenUrl(longUrl, forcedBaseUrl) {
const errorResponse = {
success: false,
shorturl: longUrl
};
try {
const params = new URLSearchParams();
params.append('url', longUrl);
const response = await fetch(this.serviceUrl, {
method: 'POST',
headers: new Headers({
'Content-Type': 'application/x-www-form-urlencoded'
}),
body: params
});
const response_data = (await response.json());
if (response_data) {
const baseUrl = forcedBaseUrl ?? this.urlManager.getBaseUrlPath();
const hash = response_data.short_url.split('/').pop();
const shortUrl = `${baseUrl}#gg-${hash}`;
const qrcode = await generateQrCode(shortUrl);
return {
success: true,
shorturl: shortUrl,
qrcode: qrcode
};
}
return errorResponse;
}
catch (error) {
console.error('Error while shortening URL:', error);
return errorResponse;
}
}
}
export default GeoGirafeShareManager;