@http4t/core
Advanced tools
Comparing version 0.0.118 to 0.0.119
@@ -42,3 +42,3 @@ /** | ||
readonly scheme?: string; | ||
readonly authority?: string; | ||
readonly authority?: ParsedAuthority; | ||
readonly path: string; | ||
@@ -48,2 +48,7 @@ readonly query?: string; | ||
} | ||
export interface ParsedAuthority { | ||
readonly user?: string; | ||
readonly host: string; | ||
readonly port?: number; | ||
} | ||
/** | ||
@@ -50,0 +55,0 @@ * HttpMessage contain headers followed by an optional body |
@@ -98,3 +98,3 @@ /** | ||
readonly scheme?: string; | ||
readonly authority?: string; | ||
readonly authority?: ParsedAuthority; | ||
readonly path: string; | ||
@@ -105,2 +105,8 @@ readonly query?: string; | ||
export interface ParsedAuthority { | ||
readonly user?: string; | ||
readonly host: string; | ||
readonly port?: number; | ||
} | ||
/** | ||
@@ -107,0 +113,0 @@ * HttpMessage contain headers followed by an optional body |
import { HttpHandler, HttpRequest, HttpResponse } from "./contract"; | ||
/** | ||
* Sets Host header on requests | ||
*/ | ||
export declare class HostHandler implements HttpHandler { | ||
@@ -3,0 +6,0 @@ private handler; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var messages_1 = require("./messages"); | ||
/** | ||
* Sets Host header on requests | ||
*/ | ||
var HostHandler = /** @class */ (function () { | ||
@@ -5,0 +8,0 @@ function HostHandler(handler, host) { |
import {HttpHandler, HttpRequest, HttpResponse} from "./contract"; | ||
import {setHeader} from "./messages"; | ||
/** | ||
* Sets Host header on requests | ||
*/ | ||
export class HostHandler implements HttpHandler { | ||
@@ -5,0 +8,0 @@ constructor(private handler: HttpHandler, private host: string) { |
{ | ||
"name": "@http4t/core", | ||
"version": "0.0.118", | ||
"version": "0.0.119", | ||
"license": "Apache-2.0" | ||
} |
@@ -1,2 +0,2 @@ | ||
import { Header, HttpBody, HttpRequest, Method } from "./contract"; | ||
import { Header, HttpBody, HttpRequest, Method, ParsedAuthority } from "./contract"; | ||
import { Uri, UriLike } from "./uri"; | ||
@@ -9,4 +9,4 @@ export declare function request(method: Method, uri: UriLike, body?: HttpBody, ...headers: Header[]): HttpRequest; | ||
export declare function delete_(uri: UriLike | string, ...headers: Header[]): HttpRequest; | ||
export declare function host(request: HttpRequest): string; | ||
export declare function authority(request: HttpRequest): ParsedAuthority; | ||
export declare function uri(request: HttpRequest): Uri; | ||
export declare function uriString(request: HttpRequest): string; |
@@ -78,3 +78,3 @@ "use strict"; | ||
exports.delete_ = delete_; | ||
function host(request) { | ||
function authority(request) { | ||
if (typeof request.uri.authority != 'undefined') | ||
@@ -85,5 +85,5 @@ return request.uri.authority; | ||
throw new Error("Could not get authority from request uri '" + request.uri + "'"); | ||
return value; | ||
return uri_1.Authority.parse(value); | ||
} | ||
exports.host = host; | ||
exports.authority = authority; | ||
function uri(request) { | ||
@@ -90,0 +90,0 @@ return uri_1.Uri.of(request.uri); |
@@ -1,4 +0,4 @@ | ||
import {Header, HttpBody, HttpRequest, Method} from "./contract"; | ||
import {Header, HttpBody, HttpRequest, Method, ParsedAuthority} from "./contract"; | ||
import {getHeaderValue} from "./headers"; | ||
import {Uri, UriLike} from "./uri"; | ||
import {Authority, Uri, UriLike} from "./uri"; | ||
@@ -34,3 +34,3 @@ export function request(method: Method, uri: UriLike, body?: HttpBody, ...headers: Header[]): HttpRequest { | ||
export function host(request: HttpRequest): string { | ||
export function authority(request: HttpRequest): ParsedAuthority { | ||
if (typeof request.uri.authority != 'undefined') | ||
@@ -41,3 +41,4 @@ return request.uri.authority; | ||
if (typeof value != 'string') throw new Error(`Could not get authority from request uri '${request.uri}'`); | ||
return value; | ||
return Authority.parse(value); | ||
} | ||
@@ -44,0 +45,0 @@ |
15
uri.d.ts
@@ -1,2 +0,2 @@ | ||
import { ParsedUri } from "./contract"; | ||
import { ParsedAuthority, ParsedUri } from "./contract"; | ||
/** | ||
@@ -8,3 +8,3 @@ * UriLike is either unparsed or parsed | ||
readonly scheme?: string; | ||
readonly authority?: string; | ||
readonly authority?: ParsedAuthority; | ||
readonly path: string; | ||
@@ -23,2 +23,13 @@ readonly query?: string; | ||
} | ||
export declare class Authority implements ParsedAuthority { | ||
readonly user?: string; | ||
readonly host: string; | ||
readonly port?: number; | ||
constructor({ user, host, port }: ParsedAuthority); | ||
static of(authority: ParsedAuthority | string): Authority; | ||
private static readonly REGEX; | ||
static parse(input: string): Authority; | ||
toString(): string; | ||
toJSON(): string; | ||
} | ||
export declare const leading: RegExp; | ||
@@ -25,0 +36,0 @@ export declare const trailing: RegExp; |
50
uri.js
@@ -33,3 +33,4 @@ "use strict"; | ||
throw new Error("Invalid Uri: " + uri); | ||
var _a = __read(match, 10), scheme = _a[2], authority = _a[4], path = _a[5], query = _a[7], fragment = _a[9]; | ||
var _a = __read(match, 10), scheme = _a[2], authorityString = _a[4], path = _a[5], query = _a[7], fragment = _a[9]; | ||
var authority = typeof authorityString !== 'undefined' ? Authority.parse(authorityString) : undefined; | ||
return new Uri({ scheme: scheme, authority: authority, path: path, query: query, fragment: fragment }); | ||
@@ -51,3 +52,3 @@ }; | ||
if (typeof this.authority != 'undefined') | ||
result.push("//", this.authority); | ||
result.push("//", Authority.of(this.authority).toString()); | ||
result.push(this.path); | ||
@@ -67,2 +68,47 @@ if (typeof this.query != 'undefined') | ||
exports.Uri = Uri; | ||
var Authority = /** @class */ (function () { | ||
function Authority(_a) { | ||
var user = _a.user, host = _a.host, port = _a.port; | ||
if (typeof port !== 'undefined' && Number.isNaN(port)) | ||
throw Error("Port was NaN"); | ||
if (user) | ||
this.user = user; | ||
this.host = host; | ||
if (port) | ||
this.port = port; | ||
} | ||
Authority.of = function (authority) { | ||
if (authority instanceof Authority) | ||
return authority; | ||
return typeof authority === 'string' ? Authority.parse(authority) : new Authority(authority); | ||
}; | ||
Authority.parse = function (input) { | ||
if (input === '') | ||
return Authority.of({ host: '' }); | ||
var match = Authority.REGEX.exec(input); | ||
if (!match) | ||
throw new Error("Invalid Authority: " + input); | ||
var _a = __read(match, 4), user = _a[1], host = _a[2], portString = _a[3]; | ||
var port = portString ? Number.parseInt(portString) : undefined; | ||
if (typeof port !== 'undefined' && Number.isNaN(port)) | ||
throw new Error("Invalid port '" + portString + "' in Authority: " + input); | ||
return Authority.of({ user: user, host: host, port: port }); | ||
}; | ||
Authority.prototype.toString = function () { | ||
var result = []; | ||
if (typeof this.user != 'undefined') | ||
result.push(this.user, "@"); | ||
if (typeof this.host != 'undefined') | ||
result.push(this.host); | ||
if (typeof this.port != 'undefined') | ||
result.push(":", this.port.toString()); | ||
return result.join(''); | ||
}; | ||
Authority.prototype.toJSON = function () { | ||
return this.toString(); | ||
}; | ||
Authority.REGEX = /^(?:([^@]+)@)?(\[.+\]|[^:]+)(?:\:([\d]+))?$/; | ||
return Authority; | ||
}()); | ||
exports.Authority = Authority; | ||
exports.leading = /^\/*/; | ||
@@ -69,0 +115,0 @@ exports.trailing = /\/*$/; |
58
uri.ts
@@ -1,2 +0,2 @@ | ||
import {ParsedUri} from "./contract"; | ||
import {ParsedAuthority, ParsedUri} from "./contract"; | ||
@@ -10,3 +10,3 @@ /** | ||
readonly scheme?: string; | ||
readonly authority?: string; | ||
readonly authority?: ParsedAuthority; | ||
readonly path: string; | ||
@@ -30,3 +30,4 @@ readonly query?: string; | ||
if (!match) throw new Error(`Invalid Uri: ${uri}`); | ||
const [, , scheme, , authority, path, , query, , fragment] = match; | ||
const [, , scheme, , authorityString, path, , query, , fragment] = match; | ||
const authority = typeof authorityString !== 'undefined' ? Authority.parse(authorityString) : undefined; | ||
return new Uri({scheme, authority, path, query, fragment}); | ||
@@ -43,2 +44,3 @@ } | ||
} | ||
/** {@link https://tools.ietf.org/html/rfc3986#section-5.3} */ | ||
@@ -49,3 +51,3 @@ toString() { | ||
if (typeof this.scheme != 'undefined') result.push(this.scheme, ":"); | ||
if (typeof this.authority != 'undefined') result.push("//", this.authority); | ||
if (typeof this.authority != 'undefined') result.push("//", Authority.of(this.authority).toString()); | ||
result.push(this.path); | ||
@@ -62,2 +64,50 @@ if (typeof this.query != 'undefined') result.push("?", this.query); | ||
export class Authority implements ParsedAuthority { | ||
readonly user?: string; | ||
readonly host: string; | ||
readonly port?: number; | ||
constructor({user, host, port}: ParsedAuthority) { | ||
if (typeof port !== 'undefined' && Number.isNaN(port)) throw Error("Port was NaN"); | ||
if (user) this.user = user; | ||
this.host = host; | ||
if (port) this.port = port; | ||
} | ||
static of(authority: ParsedAuthority | string): Authority { | ||
if (authority instanceof Authority) return authority; | ||
return typeof authority === 'string' ? Authority.parse(authority) : new Authority(authority); | ||
} | ||
private static readonly REGEX = /^(?:([^@]+)@)?(\[.+\]|[^:]+)(?:\:([\d]+))?$/; | ||
static parse(input: string): Authority { | ||
if (input === '') return Authority.of({host: ''}); | ||
const match = Authority.REGEX.exec(input); | ||
if (!match) throw new Error(`Invalid Authority: ${input}`); | ||
const [, user, host, portString] = match; | ||
const port = portString ? Number.parseInt(portString) : undefined; | ||
if (typeof port !== 'undefined' && Number.isNaN(port)) throw new Error(`Invalid port '${portString}' in Authority: ${input}`); | ||
return Authority.of({user, host, port}); | ||
} | ||
toString() { | ||
const result: string[] = []; | ||
if (typeof this.user != 'undefined') result.push(this.user, "@"); | ||
if (typeof this.host != 'undefined') result.push(this.host); | ||
if (typeof this.port != 'undefined') result.push(":", this.port.toString()); | ||
return result.join(''); | ||
} | ||
toJSON() { | ||
return this.toString(); | ||
} | ||
} | ||
export const leading = /^\/*/; | ||
@@ -64,0 +114,0 @@ export const trailing = /\/*$/; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
134392
2458