31 lines
832 B
JavaScript
Executable File
31 lines
832 B
JavaScript
Executable File
import { URL } from 'url';
|
|
import { extendType, string } from '..';
|
|
/**
|
|
* Decodes a string into the `URL` type
|
|
*/
|
|
export const Url = extendType(string, {
|
|
displayName: 'url',
|
|
description: 'A valid URL',
|
|
async from(str) {
|
|
const url = new URL(str);
|
|
if (!url.protocol || !url.host) {
|
|
throw new Error('Malformed URL');
|
|
}
|
|
if (!['http:', 'https:'].includes(url.protocol)) {
|
|
throw new Error('Only allowed http and https URLs');
|
|
}
|
|
return url;
|
|
},
|
|
});
|
|
/**
|
|
* Decodes an http/https only URL
|
|
*/
|
|
export const HttpUrl = extendType(Url, {
|
|
async from(url) {
|
|
if (!['http:', 'https:'].includes(url.protocol)) {
|
|
throw new Error('Only allowed http and https URLs');
|
|
}
|
|
return url;
|
|
},
|
|
});
|
|
//# sourceMappingURL=url.js.map
|