36 lines
834 B
TypeScript
36 lines
834 B
TypeScript
import { URL } from 'https://deno.land/std@0.85.0/node/url.ts';
|
|
import { extendType, string } from '../index.ts';
|
|
|
|
/**
|
|
* Decodes a string into the `URL` type
|
|
*/
|
|
export const Url = extendType(string, {
|
|
displayName: 'url',
|
|
description: 'A valid URL',
|
|
async from(str): Promise<URL> {
|
|
const url = new URL(str);
|
|
if (!url.protocol || !url.host) {
|
|
throw new Error('Malformed URL');
|
|
}
|
|
|
|
if (!['http:', 'https:'].includes(url.protocol as string)) {
|
|
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): Promise<URL> {
|
|
if (!['http:', 'https:'].includes(url.protocol as string)) {
|
|
throw new Error('Only allowed http and https URLs');
|
|
}
|
|
|
|
return url;
|
|
},
|
|
});
|